简体   繁体   English

在Python中,如何将表示为numpy.ndarray的方阵提升为非整数幂?

[英]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. 假设我有一个方形矩阵,可以提升到-1/2的功率。 I want to raise the square matrix represented as a numpy.ndarray to -1/2. 我想将表示为numpy.ndarray的方阵提高到-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? 我知道我可以使用numpy.linalg.matrix_power将矩阵提升为整数幂,如如何将numpy数组提升为幂?

How can I raise a numpy.ndarray to non-integer powers? 如何将numpy.ndarray提升为非整数幂?

SciPy has scipy.linalg.sqrtm , which computes a matrix square root. SciPy有scipy.linalg.sqrtm ,它计算矩阵平方根。 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. 目前尚不清楚它是否试图计算任何特定的平方根 - 例如,主平方根 - 但如果输入具有平方根,则sqrtm将计算一个。 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. 无法保证可以将一般nxn矩阵提升到给定的非整数幂。 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. 此操作是针对正整数幂定义的,并且使用Maclaurin系列,您可以定义矩阵指数函数以近似矩阵的其他函数。

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 . 这个math.stackexchange.com答案很好地涵盖了这一点

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 . 所以一般来说,这不是对任意nxn矩阵的明确定义的操作,因此它作为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. 这就像要求一个称为“ inverse ”的函数来计算任意2D数组的逆(不是伪逆或任何近似,而是“实际”逆)。 Such a function can't exist in general, since there are non-invertible 2D arrays. 这种功能通常不存在,因为存在不可逆的2D阵列。

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. 这是一个狭隘的API决定,是否有一些声称计算它的函数,如果它可以检测到无效的输入参数,如numpy.linalg.inv ,只是抛出异常,而不是提供该功能并期望用户编写自己的函数来执行此操作并处理检查参数有效性,引发异常或任何需要的故障情况行为。

inv is ubiquitous enough to warrant this effort, whereas out-of-the-box arbitrary powers are not. inv无处不在以保证这种努力,而开箱即用的任意权力则不然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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