Home > Software design >  How to get `A^(-1/2)` in Python?
How to get `A^(-1/2)` in Python?

Time:09-22

If I have a matrix

A=array([[ 0.59484625,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.58563893,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.58280472,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.58216725]])

How to get A^(-1/2)?

It seems that linalg.matrix_power(D,-1/2) does not work in Python.

In my opinion, A^(-1/2) is just

A=array([[ 0.59484625**(-1/2),  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.58563893**(-1/2),  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.58280472**(-1/2),  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.58216725**(-1/2)]])

But how to do that for a larger matrix?

CodePudding user response:

Do the following :

import numpy as np
A=np.array([[ 0.59484625,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.58563893,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.58280472,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.58216725]])

d = np.diag(A)
D = np.array(d)
diagonal = np.diag(D**(-1/2))
print(diagonal)

Note: Because, when you try to calculate it directly 0^(-1/2) is an undetermined form .So, I firstly calculate the power of the diagonal then I convert it into a matrix .

  • Related