The **
operator for numpy.matrix
does not support non-integer power:
>>> m
matrix([[ 1. , 0. ],
[ 0.5, 0.5]])
>>> m ** 2.5
TypeError: exponent must be an integer
What I want is
octave:14> [1 0; .5 .5] ^ 2.5
ans =
1.00000 0.00000
0.82322 0.17678
Can I do this with numpy
or scipy
?
this is NOT an element-wise operation. It is an matrix (in linear algebra) raised to some power, as talked in this post.
From this question you can see that the power of a matrix can be rewritten as: .
This code, making use of scipy.linalg, gives as a result the same as Octave:
import numpy as np
from scipy.linalg import logm, expm
M = np.matrix([[ 1. , 0. ],[ 0.5, 0.5]])
x = 2.5
A = logm(M)*x
P = expm(A)
This is the output for P:
Out[19]:
array([[ 1. , -0. ],
[ 0.8232233, 0.1767767]])
You could use scipy.linalg.fractional_matrix_power:
>>> m
matrix([[ 1. , 0. ],
[ 0.5, 0.5]])
>>> scipy.linalg.fractional_matrix_power(m, 2.5)
array([[ 1. , 0. ],
[ 0.8232233, 0.1767767]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With