Announcement

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

  • Spectral decomposition in Mata

    Hi Mata users,

    I would like to know if there is a built-in function in Mata that calculates the square root of a matrix using the spectral decomposition or do I have to write the routine to do so?


    Thanks,

    Anna

  • #2
    It is not clear from your question exactly what your starting point is, either in terms of the matrix or in terms of your current knowledge of Mata. Perhaps your answer lies in the Mata routines documented following the links in the Eigensystems, powers, & transcendental section of the output of
    Code:
    help m4_matrix

    Comment


    • #3
      Thank you William for your reply. That's true, I haven't expressed myself well.

      Precisely, I wanted to do in Mata what sqrtm does in Matlab, where "X = sqrtm(A) returns the principal square root of the matrix A, that is, X*X = A".

      Starting from a mxm matrix A, I want to find A-1/2. I procededed manually with the decomposition as follows A-1/2 = X*D*X-1, where X is the matrix of eigenvectors and D the diagonal matrix of eigenvalues of inverse square-roots.
      In Mata,

      Code:
      mata:
      
      A = (5, -4, 1, 0, 0\ -4, 6, -4, 1, 0\ 1, -4, 6, -4, 1\ 0, 1, -4, 6, -4\ 0, 0, 1, -4, 6)
      X = J(1,5,0)
      L = J(5,5,0)
      
      symeigensystem(A, X, L)
      
      D=diag((sqrt(L)):^(-1))
      
      invsqrtA = X*D*invsym(X)
      
      end
      I was just wondering whether there was a built-in function able to compute the square-root of a function immidiately without doing it manually.

      Thanks again.

      Comment


      • #4
        Perhaps this example will start you in a useful direction.
        Code:
        . mata:
        ------------------------------------------------- mata (type end to exit) ----------------------
        : A = (5, -4, 1, 0, 0\ -4, 6, -4, 1, 0\ 1, -4, 6, -4, 1\ 0, 1, -4, 6, -4\ 0, 0, 1, -4, 6)
        
        : sA = matpowersym(A,-.5)
        
        : A
        [symmetric]
                1    2    3    4    5
            +--------------------------+
          1 |   5                      |
          2 |  -4    6                 |
          3 |   1   -4    6            |
          4 |   0    1   -4    6       |
          5 |   0    0    1   -4    6  |
            +--------------------------+
        
        : invsym(sA*sA)
        [symmetric]
                          1              2              3              4              5
            +----------------------------------------------------------------------------+
          1 |             5                                                              |
          2 |            -4              6                                               |
          3 |             1             -4              6                                |
          4 |  -1.66533e-15              1             -4              6                 |
          5 |  -1.66533e-16   -2.99760e-15              1             -4              6  |
            +----------------------------------------------------------------------------+
        
        : end
        ------------------------------------------------------------------------------------------------
        Last edited by William Lisowski; 08 Feb 2021, 14:56.

        Comment


        • #5
          Thanks, William, for your useful suggestion.

          Comment

          Working...
          X