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]
Re: st: MATA function modifies global variables
From
George Vega Yon <[email protected]>
To
[email protected]
Subject
Re: st: MATA function modifies global variables
Date
Fri, 7 Jun 2013 09:30:02 -0400
Now I get why the performance other functions didn't change after I
replace several variables by pointers... Great news though! A must
know for MATA users.
Thanks!
George Vega Yon
7 647 2552
http://cl.linkedin.com/in/georgevegayon
2013/6/6 Sergiy Radyakin <[email protected]>:
> That's right. See the chapter "The by-address calling convention" in
> the mata manual and a quote:
>
> "Arguments are passed to functions by address, not by value. If you
> change the value of an argument,
> you will change the caller’s argument."
>
> Here are some simpler examples:
> mata mata clear
> mata
> Y=1,2,3,4,5
> real matrix foobar(real matrix y) {
> y=y'
> return(y)
> }
>
> Y
> foobar(Y)
> foobar(Y)
>
>
> S="abcdefg"
> strlen(S)
> string foobars(string s) {
> s=substr(s,1,strlen(s)-1)
> return(s)
> }
>
> S
> foobars(S)
> foobars(S)
>
>
> N=17
> real foobarn(real n) {
> n=n-1
> return(n)
> }
>
> foobarn(N)
> foobarn(N)
>
>
> end
>
>
>
> Best, Sergiy
>
> On Thu, Jun 6, 2013 at 4:23 PM, George Vega Yon <[email protected]> wrote:
>> Sorry, I forgot to change that, blop_vec() is "myfun" and neig is "X".
>> I'll just rewrite the code with the corrections. Thank you!
>>
>> __________________________
>> mata:
>> /* Function definition */
>> real colvector myfun(real colvector y, real matrix x) {
>>
>> real matrix z
>>
>> y = y'
>> x = x'
>>
>> ... some code lines (no poiter to Y or X)...
>>
>> return(z)
>> }
>>
>> X = (
>> 0,0 \
>> 1,0 \
>> 1,1 \
>> 0,1 \
>> 1/2,1/2\
>> .9,1/2 \
>> .2,.5 \
>> .8,.3
>> )
>> Y = runiform(rows(X),1)
>>
>>
>> /* Testing */
>> timer_clear()
>> for(i=1;i<=100;i++) {
>> timer_on(1)
>> YC = myfun(Y,X)
>> timer_off(1)
>> }
>> timer()
>>
>> end
>> __________________________
>> George Vega Yon
>> 7 647 2552
>> http://cl.linkedin.com/in/georgevegayon
>>
>>
>> 2013/6/6 Sergiy Radyakin <[email protected]>:
>>> Can you tell us what neig is? what blop_vec is?
>>>
>>> On Thu, Jun 6, 2013 at 4:10 PM, George Vega Yon <[email protected]> wrote:
>>>> Dear Statalisters,
>>>>
>>>> I'm having a weird problem on variables' scope. I have a function that
>>>> takes two objects (a real colvector and a matrix) wich, once inside
>>>> the function, are transposed. This function returns a matrix (builded
>>>> inside of the function of course). The problem is that the function
>>>> seems to actually transpose the global objects (not only the copies
>>>> within the function), thing that, as far as I know, should not be
>>>> happening as I'm not using pointers to the global objects.
>>>>
>>>> My code looks something like this
>>>> __________________________
>>>> mata:
>>>> /* Function definition */
>>>> real colvector myfun(real colvector y, real matrix x) {
>>>>
>>>> real matrix z
>>>>
>>>> y = y'
>>>> x = x'
>>>>
>>>> ... some code lines (no poiter to Y or X)...
>>>>
>>>> return(z)
>>>> }
>>>>
>>>> Y = runiform(rows(neig),1)
>>>> X = (
>>>> 0,0 \
>>>> 1,0 \
>>>> 1,1 \
>>>> 0,1 \
>>>> 1/2,1/2\
>>>> .9,1/2 \
>>>> .2,.5 \
>>>> .8,.3
>>>> )
>>>>
>>>>
>>>> /* Testing */
>>>> timer_clear()
>>>> for(i=1;i<=100;i++) {
>>>> timer_on(1)
>>>> YC = blop_vec(Y,X)
>>>> timer_off(1)
>>>> }
>>>> timer()
>>>>
>>>> end
>>>> __________________________
>>>>
>>>>
>>>> I get the error
>>>>
>>>> __________________________
>>>> myfun: 3203 Y[1,8] found where colvector required
>>>> <istmt>: - function returned error
>>>> __________________________
>>>>
>>>> Thanks in advanced,
>>>>
>>>> George Vega Yon
>>>> 7 647 2552
>>>> http://cl.linkedin.com/in/georgevegayon
>>>> *
>>>> * 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/