Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Curious behavior on function definition

    Colleagues: If I execute the following code in a new Stata session I encounter no difficulties
    Code:
    capture mata mata drop reindex()
    
    mata
    
    function reindex(v,updown) {
    real matrix rv,chg
    
    rv=sort(v,1)
    chg=1
    while (chg>0) {
     chg=0
     for (j=1;j<=(rows(v)-1);j++) {
       if (rv[j]==rv[j+1]) {
        chg=chg+1
        rv[j]=rv[j]+updown
       }
     }
    }
    return(rv)
    }
    
    end
    However, my Mata session has been open and performing other tasks and now I'm receiving this r(3000) error message when executing the same code:
    Code:
    . do "/var/folders/d5/6w9t_lzx3plbpmk6zpyz93dm0000gp/T//SD09710.000000"
    
    . capture mata mata drop reindex()
    
    .
    . mata
    ------------------------------------------------- mata (type end to exit) -------------------------------------
    :
    : function reindex(v,updown) {
    > real matrix rv,chg
    >
    > rv=sort(v,1)
    > chg=1
    > while (chg>0) {
    >  chg=0
    >  for (j=1;j<=(rows(v)-1);j++) {
    >    if (rv[j]==rv[j+1]) {
    >     chg=chg+1
    >     rv[j]=rv[j]+updown
    >    }
    >  }
    > }
    > return(rv)
    > }
    variable j undeclared
    r(3000);
    
    :
    : end
    ---------------------------------------------------------------------------------------------------------------
    
    .
    end of do-file
    While I'm encountering this error in this particular instance it also seems to happen rarely but every so often in function definitions. If I change the declaration to real matrix rv,chg,j the code runs fine. I generally don't declare loop/index variables in my declarations and in most cases that's not problematic.

    Does anyone have insights into why this r(3000) error might be arising? I can certainly do a work-around, but mostly I'm curious why such an error would arise at all.

    Thanks for any insights you might be able to share.

  • #2
    I have only seen this error when I set -matastrict on. Makes me wonder if some of the other tasks you mention are changing this setting. Also, the setting does not apply in interactive sessions.

    Comment


    • #3
      Thanks very much, German. I reran the code with mata set matastrict off at the top of the mata session and it ran without error. This is still a little curious since I never specify matastrict in any of my code, so I wonder how it might have (perhaps) got turned to on. But when I rerun the code with mata set matastrict on the same error does arise. So that seems to be the solution.

      I will try mata set matastrict off, permanently and see if the problem goes away.

      Thanks again for your helpful reply.

      Comment


      • #4
        As stated above, it is because you have not declared i and j with matastrict on. Also heads up if you have a very large matrix you are iterating on, defining something like
        Code:
        row = rows(v) - 1
        will save you some execution time as rows() won't be executing on every iteration. Sergio Correa's general Mata tips have helped me speed up many of my Mata functions, even if incrementally.

        Comment


        • #5
          Thanks Brian.

          Comment


          • #6
            So bizarre. I just got the same errors from a program that worked last week. Set matastrict off and everything worked fine again, even though I had never set matastrict in the first place. Thanks for your posts!

            Comment

            Working...
            X