Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: MATA function modifies global variables


From   George Vega Yon <g.vegayon@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: MATA function modifies global variables
Date   Thu, 6 Jun 2013 16:23:28 -0400

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 <serjradyakin@gmail.com>:
> Can you tell us what neig is? what blop_vec is?
>
> On Thu, Jun 6, 2013 at 4:10 PM, George Vega Yon <g.vegayon@gmail.com> 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index