Bookmark and Share

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: Unexpected end of file in mata.


From   "William Gould, StataCorp LP" <[email protected]>
To   [email protected]
Subject   Re: st: Unexpected end of file in mata.
Date   Thu, 23 Jan 2014 12:01:02 -0600

Amadou Diallo <[email protected]> has more Mata questions. 

His first question -- paraphrased -- is 

     I understand this:

        : i = 1 

        : if (i==1) {
        >     i 
        > } ;
        1

        : _

     i is one, the statement is true, and i is displayed. 

     I don't understand the following, however, at all 

        : if (i=2) {            <- note, =, not ==
        :     i
        : } ;
        2

     What is going on?  Even worse, why is i now 2?

In order to explain this, let me define what Mata means by the 
term expression:  An expression is a string of characters that, 
interpreted as mathematical/Mata notation, returns a value. 

The following are examples of expressions:

        2+3
        2*3
        i==1
        i
        sqrt(a^2+b^2)

That 2+3 and 2*3 are expressions is hardly surprising.  

Think hard, however, about what it means for i to be an expression. 
Isn't i a variable name?  Well, yes it is, but in an expression context, 
i is just an expression.  It is the expression that returns the current 
contents (value) of i.  I admit this is a fine point, but it's important.

Here's another fine point.  Look at this expression:

        sqrt(a)

You may look at that and say, "That's the instruction to calculate the
square root of a."  I look at it, however, and say, "That's an
expression.  sqrt() is a function that happens to take one argument.
The argument in this case is a, which is also an expression.  The way
this whole thing works out is that the expression a is evaluated,
returning the value of a, then the function sqrt() is evaluated on the
value of a, and it returns the squareroot."

In Mata, the following is an expression, too:

        i = 2

You look at that and think, "i = 2 is the instruction to assign i to 2". 
I look at it and say, "i = 2 is an expression.  = is an operator, just 
as +, -, *, >, are <= are operators.  (i=2), the whole thing, which is 
why I bound it in parenthesis, is an expression, just as (2+3) is an 
expression.  Expressions evaluate to a value.  (i=2) evaluates to 2 
in the same sense that (2+3) evaluate to 5.  

+ is an operator.  a+b means add a and b and return that value.

= is an operator.  a=b means assign the value of b to a and return that
value.

Get it?  

In mata, you can type things like this:

        c=(a=2+3)*(b=2*4)

Stored in a will be 5.  Stored in b will be 8.  Stored in c will be 
40.

So let's dissect what happened when Amadou typed

        : if (i=2) {            <- note, =, not ==
        :     i
        : } ;

i=2 is an expression that returns 2.  The = operator also stored 2 in
i.  Type typing -if (i=2)- Amadou was asking whether 2 is not equal to
0.  2 is indeed not equal to 0, so -if- entered the body of the -if-.
The body of the -if- said to display the current value i.  Mata
displayed 2.

Amadou also asked, 

>   1. The almost impossibility to trace down a mata code when it
>      crashes.  In mata v12 manual p.907, you gave a tracelog example.
>      However, if the function solve() itself is very big (some
>      millions of line), how do you detect the error? Maybe the
>      tracelog should indicate the exact line where the problem arises
>      (as in C++ compiler or VB).

First, I respectfully request that Amadou stop using the word crash. 
For us at StataCorp crash means crash as in crash and burn.  It means 
Stata collapses in on itself, stops running, and perhaps writes a file
containing a dump of memory.  For us, crashes are Level 2 events.  The
only thing worse than crash is a Level 1 event -- giving a wrong answer
that a knowledgeable person might interpret as a correct answer.

Mata did not crash; the program Amadou aborted with error. 

The way I debug is by adding lines to my code that display what's 
happening.  See slides 56-63 of "Mata, the missing manual", a talk 
I gave in 2010 in London.  The talk can be found at 

       www.stata.com/meeting/uk10/UKSUG10.Gould.pdf


> Are there any equivalent command such as "capture" and "assert" or
> other useful commands in mata?

-assert- most certainly exists.  You can always code, 

        if (!(2==2)) { 
               "assumption failure: 2 not equal to itself"
                "Mata is broken!"
                _error()
        }

You don't have to code that, however, because Mata has an -assert()-
function; see -help mata assert()-.

        assert(2==2)

There is no equivalent to -capture- because you don't need it. 
It is absolutely true that there are things you can do that can 
fail for reasons outside of your control.  For instance, I want to 
open file bill.txt and it might not exist.  I code, 

        fh = fopen("bill.txt", "r") 

and if the file does not exist, my program stops.  However, there is 
another function, _fopen(), that will not abort execution and instead 
communicate back that the file didn't exist,

         fh = _fopen("bill.txt", "r") 
         if (fh<0) {
                 /* the file could not be opened, and fh contains 
                    a code telling you why that happened
                 */
         }
         else {
                  /* the file opened and now ready for reading */
                  ...
                  fclose(fh)
          }

Whenever a function can fail for a reason outside of your control, there 
is a variant of the function that, rather than abort execution (note, I 
did not say crash), will send you back a code telling you whether the 
operation succeeded. 

Admittedly, Stata's -capture- command is an elegant solution for these 
kinds of problems.  The problem is that -capture- just does not fit 
with a compiled as opposed to interpreted language.

-- Bill
[email protected]

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index