Announcement

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

  • Saving pointer matrixes using -mata matsave-

    I am relatively new to the use of pointers in Mata and have thusfar been impressed with their utility.

    Specific to this query, I have been curious about using pointers in conjunction with mata matsave. Specifically, if I define a pointer matrix and save it using mata matsave it appears from a few instances I've tried that the contexts of the "pointee" matrixes are saved as well (i.e. are restored even after clear or starting a new session).

    I wasn't able to find anything specific in the documentation speaking to this, but is my novice's observation accurate, i.e. that mata matsave of pointer matrixes will result in the contents being saved and, as such, restorable with mata matuse even after the original "pointee" matrixes have been cleared?

    For instance:
    Code:
    . mata
    ------------------------------------------------- mata (type end to exit) ----------------------------------------------------
    :
    : p=J(3,1,NULL)
    
    :
    : for (j=1;j<=3;j++) {
    >  p[j]=&I(j)
    > }
    
    :
    : mata matsave pointmat p, replace
    (saving p[3,1])
    file pointmat.mmat saved
    
    :
    : mata clear
    
    :
    : mata matuse pointmat
    (loading p[3,1])
    
    :
    : for (j=1;j<=3;j++) {
    > *p[j]
    > }
      1
    [symmetric]
           1   2
        +---------+
      1 |  1      |
      2 |  0   1  |
        +---------+
    [symmetric]
           1   2   3
        +-------------+
      1 |  1          |
      2 |  0   1      |
      3 |  0   0   1  |
        +-------------+
    
    :
    : end
    Thanks for any insights and/or references.

    P.S. I posted this as a response in an existing thread but realized it should probably be a new posting
    https://www.statalist.org/forums/for...ciative-arrays

  • #2
    The contents being pointed are saved in the file and restored and linked when reading back. Here is some advice from the manual for fputmatrix() which applies to matamatsave as well.

    "
    When writing pointer matrices, fputmatrix() writes NULL for any pointer-to-function value. It is also recommended that you do not write self-referential matrices (matrices that point to themselves, either directly or indirectly), although the elements of the matrix may be cross linked and even recursive themselves. If you are writing pointer matrix p, no element of p, *p, **p, etc., should contain &p. That one address cannot be preserved because in the assignment associated with reading back the matrix (the “result =” part of result = fgetmatrix(fh)), a new matrix with a different address is associated with the contents.
    "

    Comment


    • #3
      Thanks Hua. Much appreciated.

      Comment

      Working...
      X