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   Amadou DIALLO <[email protected]>
To   [email protected]
Subject   Re: st: Unexpected end of file in mata.
Date   Thu, 23 Jan 2014 20:47:36 +0100

Dear Bill,
Many thanks indeed.
I've just landed at home but couldn't resist the temptation to send
you this mail.
I apologize for the term "crash" that we too familiary use at work
(for a whole bunch of software, not just stata). But fair enough, it
is inappropriate and I'll try to avoid it in the future.
Let me also state that since I first discover stata in 2001, I've
become a big fan. I currently work for a big organization (over 2000
staff) and I am pushing for a greater use of stata in our various
departments, including the research one. I've created a new internal
Stata Users group to promote it and help each other and the motto I
claim to the group is that "you can do anything you want with stata"
(I hope I don't push that argument too far :-)).
Coming back to this thread, I am a bit surprised to learn that
mata/stata interprets "if (i=2) {" as "whether 2 is not equal to 0."
(I'd have thought equal to 2!). But this is noted, I think I
understand better mata's logic.
I also thank you for the missing manual. It's very rich and I'll read
and use extensively.
Coming to my questions, I new of assert. In fact, I wanted to ask for
"trace", "capture", "return" (especially return codes - I am aware of
the "return" at the end of user written mata functions) or commands
alike. For instance, I was writting a mata code and I wanted to have a
"if" condition associated with a returned value (for instance
if iscolvector(x) return a certain value,
otherwise return another one). I couldn't find a way to ask mata to
check whether x was indeed a colvector...
More generally, when a mata program aborts (with a error code as it
seems to me it is almost always the case), how to capture that error
code, so that we use that _rc as in stata mode?
The reason I'm asking so much questions is that as you said in the
manual, Stata has so nice features that I want to make sure mata also
has (and if not, how to combine).
Finally, if I understand correctly, all (or most) of mata functions
have their variants (preceded by _) to prevent abortion?
Really sorry to ask so many questions, my spouse is pressing me for dinner.
Bon appétit à tous et bonne soirée.
Bachir.

2014/1/23, William Gould, StataCorp LP <[email protected]>:
> 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/
>


-- 
Amadou B. DIALLO, PhD.
Senior Economist, AfDB.
[email protected]
+21671101789

*
*   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