Ticket #601 (closed enhancement: fixed)
function for computing powers of a matrix
| Reported by: | LevGivon | Owned by: | somebody |
|---|---|---|---|
| Priority: | normal | Milestone: | 1.1.0 |
| Component: | numpy.linalg | Version: | devel |
| Keywords: | Cc: |
Description
It would be nice to add the following function to numpy to allow for the raising of matricies to arbitrary exponents (and perhaps modify matrix.__pow__() to use this approach when one attempts to raise a matrix to a noninteger exponent):
from numpy import diag,dot,shape,eye
from numpy.linalg import eig,inv
def mpower(x,y):
'''Compute x raised to the power y when x is a square matrix and y
is a scalar.'''
s = shape(x)
if len(s) != 2 or s[0] != s[1]:
raise ValueError('matrix must be square')
if y == 0:
return eye(s[0])
[e,v] = eig(x)
d = diag(e)
return dot(dot(v,d**y),inv(v))
Attachments
Change History
Note: See
TracTickets for help on using
tickets.
