Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Alan Riley <ariley@stata.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Question concerning pointers in Mata |
Date | Thu, 24 Mar 2011 17:10:50 -0500 |
Matthew J Baker <matthew.baker@hunter.cuny.edu> asked a followup question regarding a good way to collect a series of results in a vector using pointers: > Thanks to Alan for the very insightful answer! By way of a quick > follow up, the reason why I asked the question is because > sometimes one wishes to do a series of complicated > computations, and then collect results systematically in a > pointer. In these instances, it is easy to get tripped up on the > aforementioned problem. > > Anyways, given the explanation, my thought is that a good > approach is to put the "complicated" computations in a stand- > alone routine and reference this with the pointer, along the lines > of the following example (the computations aren't complicated in > the example, but hopefully the idea is clear): > > // Begin > clear all > mata > real matrix foo(real scalar k) > { > real matrix Z > X=J(k,k,1) > return(X) > } > Z=J(10,1,NULL) > for (i=1;i<=10;i++) { > Z[i]=&foo(i) > } > *Z[1] > *Z[10] > // End example > > This seems to me to be a systematic way of being sure that the > possibility of a case of mistaken pointer identity is avoided, but > there may be alternative means of dealing with the issue. If so, > I'd like to hear about them! In the case where the results to be stored are all of the same type (i.e. they are all real scalars or they are all 3 x 1 colvectors or they are all 100 x 100 matrices), then there is no need to use pointers. Simply store the results in a vector. Unlike C, where to create a vector of 100 x 100 matrices you would need to store pointers to each of those matrices, Mata understands matrices as a true type and you can just directly store each of them in separate elements of a vector (as long as they are all conformable with each other). In Matt's example, however, he first creates a 1 x 1 matrix, then a 2 x 2, ..., and finally a 10 x 10. In this case, all the results he wants to store are non-comformable with each other and thus they cannot all be stored directly in the same vector. His approach of storing a pointer to each result, rather than the result itself, in a vector is exactly right. In Matt's original post in this thread and my reply, it was shown that you cannot put each result in another variable over and over and store a pointer to that variable. If that is done, you will end up with 10 identical pointers, all pointing at the final result. Matt's suggestion of returning a result from a function and obtaining a pointer to that result without ever actually storing it is a good way to obtain a unique pointer to each result, which pointers can be stored in a result vector. --Alan ariley@stata.com * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/