Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Mata: Drop matrix in a program


From   [email protected] (William Gould, Stata)
To   [email protected]
Subject   Re: st: Mata: Drop matrix in a program
Date   Fri, 05 Jan 2007 08:43:21 -0600

Thomas Cornelissen <[email protected]> wrote, 

> according to the Mata manual -mata drop- "is for use in Mata mode following
> Mata's colon prompt."  Does that mean it's not intended for use in a
> program?  (I get the error message "invalid expression" when using -mata
> drop- in a mata program.

Yes.

Thomas went on to ask, "Is there an alternative for dropping a matrix in a
mata program?"

Yes there are.  There are two cases.


Case 1:  Variable is local to the program
-----------------------------------------

Consider a function

        function myfunc(....)
        {
                real matrix X
                ...
        }

Variable X is local to the function (program) myfunc().  What that means 
is 

    1.  Variable X is unconnected with any other variable X in any other
        function (program) and is unconnected to any global variable named X
        that happens to exist

    2.  A new copy (called an "instance") of X is created each time 
        myfunc() is invoked.

    3.  The instance of X is destroyed when execution of myfunc() concludes.

There is no way to get rid of X, but there is no reason you should want to.

In myfunc(), however, there may come a point where X contains a really 
big matrix and then later, you don't need that matrix any more.  You may 
be concerned about the memory that X consumes, particularly if you are 
about to create another really big matrix in local variable Y.

In that case, code 

        X = .                                           (1)
or 
        X = J(0, 0, .)                                  (2)

The first reduces the memory consumed by X to that of one double.  The second
reduces the memory consumed to that of a 0 x 0 matrix.  Now, you might 
suspect a 0 x 0 matrix takes 0 bytes, but it doesn't.  It takes 8 bytes, 
just like a double, so I could (1) because it takes less typing.


Case 2:  Variable is global
---------------------------

Let us imagine that variable X is an external global.  You can 
use the method outlined in case 1 to reduce X's memory consumption.
In this case, however, it is possible to get rid of X altogether.

The method is

        rmexternal("<name of variable>")

For instance, if X is the name of the global variable, code 

        rmexternal("X")

There's a detail here that is worth appreciating, and will please you, 
but might confuse you in some circumstances.  You code -rmexternal("X")-, in
some cases, were you to look, you might discover that X still exists!  That
will happen if another function (say your caller or your caller's caller, etc.)
is using X.  In that case, Mata will automatically postpone the elimination of
X until the every function stops using X, and then Mata will eliminate it.


-- Bill
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index