DvM
DvM

Reputation: 91

Python: Raise square matrix to negative half power

I am trying to compute the matrix B which has the following equation: B = A^2.

Assume that A is a matrix:

array([[1, 2], [3, 4]])

When I take the negative half power of this matrix in Matlab (A^-0.5). I get the following result:

ans =

   0.1031 - 1.2474i   0.1502 + 0.5706i

   0.2253 + 0.8559i   0.3284 - 0.3915i

However, in Python using the code (A**-0.5) I get different results

array([[ 1.        ,  0.70710678],
       [ 0.57735027,  0.5       ]]).

My question is how can I use Python to replicate the Matlab output. I have tried numpy function numpy.linalg.matrix_power(M, n), but this fails as it only takes integers as a second argument, whereas I need a float. Thank you!!!

Upvotes: 8

Views: 7544

Answers (1)

jpp
jpp

Reputation: 164673

scipy.linalg supports fractional matrix powers:

import numpy as np
from scipy.linalg import fractional_matrix_power

A = np.array([[1, 2], [3, 4]])

fractional_matrix_power(A, -0.5)

# array([[ 0.10306396-1.24742804j,  0.15020820+0.5706074j ],
#        [ 0.22531231+0.8559111j ,  0.32837626-0.39151694j]])

Upvotes: 10

Related Questions