Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Sergiy Radyakin <serjradyakin@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: Passing a subvector to a void function |

Date |
Mon, 29 Jul 2013 12:20:50 -0400 |

Joe, on using Fortran to extend stat software capabilities have a look at this: http://www.amazon.com/Developing-Statistical-Software-Statistics-Computing/dp/B009JUWEK6 The relevant part is on developing DLLs (but the whole book is interesting of course!) On plugins for Stata in general, this http://www.stata.com/plugins/ remains the only (and sufficient) source. Be aware that plugins are being criticized all over here, so your Mata effort will probably have a much warmer reception. Best, Sergiy On Mon, Jul 29, 2013 at 12:06 PM, Joe Canner <jcanner1@jhmi.edu> wrote: > Sergiy, > > Thanks, I figured as much and in the meantime created a temporary variable to pass to the function which I then inserted back into the original vector after the function returned. Although not very elegant, this seemed to work, at least on my test data sets. > > I am interested, however, in your reference to Fortran plugins, since I am not 100% confident that my conversion from Fortran to Mata captured the entire essence of what the original Fortran code was meant to accomplish. Can you provide a link to information on how to do what you are suggesting? > > Thanks for all your help. > > Regards, > Joe > > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Sergiy Radyakin > Sent: Monday, July 29, 2013 11:52 AM > To: statalist@hsphsun2.harvard.edu > Subject: Re: st: Passing a subvector to a void function > > Joe, I am afraid that's not going to work. Mata passes structures like matrices and vectors as pointers. In the help for pointers the following is applicable (imho) to your case: > > ---quote1--- > You cannot, however, point to the interior of objects. That is, you cannot code p = &X[2,3] and get a pointer that is equivalent to X[2,3] in the sense that if you later coded *p=2, you would see the change reflected in X[2,3]. > --- end of quote 1 --- > > --- quote 2 --- > The expression is evaluated and the result of the expression stored in a temporary variable. That is why subsequently coding *p=2 does not change X[2,3]. All *p=2 does is change the value of the temporary variable. > -- end quote --- > > So in your case the subvector is extracted correctly, stored in a temp var, passed to the function, modified, but then disposed of. Instead I suggest you pass enough information to the procedure being called to be able to restrict the vector to a certain subvector itself and receive a full vector as an argument. > > Hope this helps. Note that since you are translating a Fortran code, it might be easier just to create a Fortran plugin. In that case you would only need to access some Stata objects, but keep the main logic. > > Best, Sergiy > > On Mon, Jul 29, 2013 at 9:31 AM, Joe Canner <jcanner1@jhmi.edu> wrote: >> Sergiy, >> >> Thanks for looking into this. Sorry, I should have simplified the example before posting. The issue is not whether the subroutine will modify the original vector, the issue is whether you can pass a sub-vector and have it come back modified. To use your suggested code to illustrate: >> >> clear all >> mata >> void arrsum(real rowvector A, real k) { real scalar S, i >> S=0 >> for(i=1;i<=cols(A);i++) { >> S=S+A[1,i] >> } >> A[k]=S >> } >> >> A=1,2,3,4 >> A >> arrsum(A[|2\.|],1) >> A >> end >> >> So, instead of passing the entire vector A, I am passing a subvector starting with the 2nd element of A. What I expect (and want) is for arrsum to put the sum of elements 2-4 into element 2 (since it arrsum is putting the sum of elements 1-3 of the subvector into element 1 of the subvector). However, if you run this code you will find that A is unmodified upon return. >> >> Regards, >> Joe >> >> -----Original Message----- >> From: owner-statalist@hsphsun2.harvard.edu >> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Sergiy >> Radyakin >> Sent: Monday, July 29, 2013 2:56 AM >> To: statalist@hsphsun2.harvard.edu >> Subject: Re: st: Passing a subvector to a void function >> >> Joe, you have too many irrelevant arguments in the procedure you are discussing, they complicate understanding. As to the subject you ask, consider the following example: it computes the sum of the array elements, then replaces the specified element of the array with the computed sum. The modified array is returned to the caller. >> Sergiy >> >> clear all >> mata >> void arrsum(real rowvector A, real k) { >> S=0 >> for(i=1;i<=cols(A);i++) { >> S=S+A[1,i] >> } >> A[k]=S >> } >> >> A=1,2,3,4 >> A >> arrsum(A,1) >> A >> end >> >> On Fri, Jul 26, 2013 at 12:12 PM, Joe Canner <jcanner1@jhmi.edu> wrote: >>> A question for all you Mata programmers: >>> >>> I am converting a Fortran 90 program to Mata and the Fortran program passes subarrays as arguments to subroutines like this: >>> >>> call idtang ( ndp, xd, yd, nt, iwk(jwipt), nl, iwk(jwipl), >>> iwk(jwiwl), iwk(jwiwp), wk ) >>> >>> where iwk is an array and iwk(jwipt) means "pass a subarray of iwk starting at element jwipt". The subroutine modifies this subarray and passes it back. >>> >>> I converted this to Mata as follows: >>> >>> idtang ( ndp, xd, yd, nt, iwk[|jwipt\.|], nl, iwk[|jwipl\.|], >>> iwk[|jwiwl\.|], iwk[|jwiwp\.|], wk ) >>> >>> but iwk is empty upon return. >>> >>> Is there a way to do this that will return the modified subvector and put it back in iwk at position jwipt? >>> >>> I got it to work by creating a new vector ipt, setting it to iwk[|jwipt\.|] and passing it to the function, but I was hoping to avoid having to make so many substantive changes to the original code. >>> >>> Thanks for your help! >>> >>> Joe Canner >>> Johns Hopkins University School of Medicine >>> >>> * >>> * For searches and help try: >>> * http://www.stata.com/help.cgi?search >>> * http://www.stata.com/support/faqs/resources/statalist-faq/ >>> * http://www.ats.ucla.edu/stat/stata/ >> * >> * For searches and help try: >> * http://www.stata.com/help.cgi?search >> * http://www.stata.com/support/faqs/resources/statalist-faq/ >> * http://www.ats.ucla.edu/stat/stata/ >> >> * >> * For searches and help try: >> * http://www.stata.com/help.cgi?search >> * http://www.stata.com/support/faqs/resources/statalist-faq/ >> * http://www.ats.ucla.edu/stat/stata/ > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**References**:**st: Passing a subvector to a void function***From:*Joe Canner <jcanner1@jhmi.edu>

**Re: st: Passing a subvector to a void function***From:*Sergiy Radyakin <serjradyakin@gmail.com>

**RE: st: Passing a subvector to a void function***From:*Joe Canner <jcanner1@jhmi.edu>

**Re: st: Passing a subvector to a void function***From:*Sergiy Radyakin <serjradyakin@gmail.com>

**RE: st: Passing a subvector to a void function***From:*Joe Canner <jcanner1@jhmi.edu>

- Prev by Date:
**st: Multicollinearity Problem in Stata** - Next by Date:
**Re: st: loop until "0 real changes made"** - Previous by thread:
**RE: st: Passing a subvector to a void function** - Next by thread:
**st: should estat sd reports same sd before and after clustering?** - Index(es):