对角化符号矩阵 [英] Diagonalize symbolic matrix
问题描述
我需要用python对角化符号矩阵.在Mathematica中可以轻松完成,但是使用模块numpy.linalg
时会遇到问题.
I need to diagonalize a symbolic matrix with python. In Mathematica it can be done easily, but when using the module numpy.linalg
I get problems.
为具体起见,请考虑矩阵
For concreteness, consider the matrix
[[2, x], [x, 3]]
其中,x
是符号变量.我想我会遇到问题,因为numpy包是为数值计算而不是符号提供的,但是我找不到如何用sympy做到这一点.
where x
is a symbolic variable. I guess I get problems because the numpy package is provided for numerical computations, not symbolic, but I cannot find how to do it with sympy.
推荐答案
您可以根据特征值进行计算,但是实际上有一种方法可以为您做到这一点,
You can compute it from the eigenvalues, but there is actually a method that will do it for you, diagonalize
In [13]: M.diagonalize()
Out[13]:
⎛ ⎡ __________ ⎤⎞
⎜ ⎢ ╱ 2 ⎥⎟
⎜⎡ -2⋅x 2⋅x ⎤ ⎢ ╲╱ 4⋅x + 1 5 ⎥⎟
⎜⎢───────────────── ─────────────────⎥, ⎢- ───────────── + ─ 0 ⎥⎟
⎜⎢ __________ __________ ⎥ ⎢ 2 2 ⎥⎟
⎜⎢ ╱ 2 ╱ 2 ⎥ ⎢ ⎥⎟
⎜⎢╲╱ 4⋅x + 1 - 1 ╲╱ 4⋅x + 1 + 1⎥ ⎢ __________ ⎥⎟
⎜⎢ ⎥ ⎢ ╱ 2 ⎥⎟
⎜⎣ 1 1 ⎦ ⎢ ╲╱ 4⋅x + 1 5⎥⎟
⎜ ⎢ 0 ───────────── + ─⎥⎟
⎝ ⎣ 2 2⎦⎠
M.diagonalize()
返回一对矩阵(P, D)
,使得M = P*D*P**-1
.如果由于矩阵不可对角化或由于solve()
找不到特征多项式的所有根而不能计算出足够多的特征值,则会提高MatrixError
.
M.diagonalize()
returns a pair of matrices (P, D)
such that M = P*D*P**-1
. If it can't compute enough eigenvalues, either because the matrix is not diagonalizable or because solve()
can't find all the roots of the characteristic polynomial, it will raise MatrixError
.
另请参见本节 SymPy教程.
See also this section of the SymPy tutorial.
这篇关于对角化符号矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!