简体   繁体   中英

In Python, how can I raise a square matrix represented as a numpy.ndarray to non-integer powers?

Assume I have a square matrix which can be raised to the -1/2 power. I want to raise the square matrix represented as a numpy.ndarray to -1/2.

Note I want to raise the matrix to a non-integer power. I do not want raise each element of the matrix to a non-integer power.

I know I can raise a matrix to an integer power using numpy.linalg.matrix_power as described in How to raise a numpy array to a power?

How can I raise a numpy.ndarray to non-integer powers?

SciPy has scipy.linalg.sqrtm , which computes a matrix square root. It's not clear whether it attempts to compute any particular square root - for example, the principal square root - but if the input has square roots, sqrtm will compute one. Thus, you can do

invsqrt = scipy.linalg.sqrtm(scipy.linalg.inv(input_matrix))

though you'll likely want to do some error-handling.

There is no guarantee that a general nxn matrix can be raised to a given non-integer power. This operation is well-defined for positive integer powers, and using Maclaurin series you can then define a matrix exponential function for approximating other functions of matrices.

However, to be able to raise a matrix to an arbitrary power, you must also have a coherent definition of a matrix logarithm, which is only well-defined for invertible matrices and involves some subtlety about uniqueness and the field of elements over which it is defined.

This is covered reasonably well at this math.stackexchange.com answer .

So in general, this is not a well-defined operation on arbitrary nxn matrices, and thus it wouldn't make sense as a generically available function on ndarray .

It's like asking for a function called " inverse " that calculates an inverse (not a psuedo-inverse or any approximation, but the "actual" inverse) for arbitrary 2D arrays. Such a function can't exist in general, since there are non-invertible 2D arrays.

It's somewhat of a parochial API decision as to whether there is some function that purports to compute it and merely throws an exception if it can detect an invalid input argument, such as numpy.linalg.inv , versus just not providing that functionality and expecting the user to write their own function to do it and to handle checking argument validity, raising exceptions, or whatever failure-case behavior is required.

inv is ubiquitous enough to warrant this effort, whereas out-of-the-box arbitrary powers are not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM