Reputation: 151
I want to calculate the -1/2 power of the degree matrix in python. I know there is a great package to calculate the normalized graph laplacian(L_norm = I - D^{-1/2}AD^{-1/2}, A is the adjacency matrix) in networkx
. But I only need the D^{-1/2}.
I tried numpy.linalg.matrix_power
, but it supports only integer.
raise TypeError("exponent must be an integer") TypeError: exponent must be an integer
is there any way to calculate the -1/2 power of the matrix?
Upvotes: 2
Views: 1788
Reputation: 7693
You can use scipy.linalg.fractional_matrix_power for fractional power of a matrix.
Example from Docs
>>> from scipy.linalg import fractional_matrix_power
>>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
# fractional power of a matrix
>>> b = fractional_matrix_power(a, 0.5)
>>> b
array([[ 0.75592895, 1.13389342],
[ 0.37796447, 1.88982237]])
>>> np.dot(b, b) # Verify square root
array([[ 1., 3.],
[ 1., 4.]])
Upvotes: 1