SymPy无法计算此矩阵的特征值 [英] SymPy could not compute the eigenvalues of this matrix
问题描述
我想计算拉普拉斯矩阵的第二个特征值,以检查对应的图是否已连接,但是当我尝试使用SymPy的eigenvals
时,很多时候会发生错误
I want to compute the second eigenvalue of a Laplacian matrix to check if the corresponding graph is connected or not, but when I try to use SymPy's eigenvals
, a lot of times it happens that it throws an error
MatrixError: Could not compute eigenvalues for
Matrix([[1.00000000000000, 0.0, 0.0, 0.0, -1.00000000000000, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 1.00000000000000, 0.0, 0.0, 0.0, -1.00000000000000, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.00000000000000, 0.0, 0.0, 0.0, 0.0, 0.0, -1.00000000000000, 0.0],
[0.0, 0.0, 0.0, 1.00000000000000, 0.0, 0.0, 0.0, 0.0, -1.00000000000000, 0.0],
[-1.00000000000000, 0.0, 0.0, 0.0, 1.00000000000000, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, -1.00000000000000, 0.0, 0.0, 0.0, 3.00000000000000, 0.0, 0.0, -1.00000000000000, -1.00000000000000],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.00000000000000, 0.0, -1.00000000000000],
[0.0, 0.0, -1.00000000000000, -1.00000000000000, 0.0, -1.00000000000000, 0.0, 0.0, 3.00000000000000, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, -1.00000000000000, 0.0, -1.00000000000000, 0.0, 2.00000000000000]])
环顾四周,我发现由于SymPy进行符号计算,因此浮点可能会成为问题.所以我尝试了:
Looking around I found out that since SymPy does symbolic computation, floating points can be a problem for it. So I tried:
- 要降低浮点数
Float(tmp[i][j], 3)
的精度,但这没有帮助. - 我试图将浮点数转换为Rational
list(map(nsimplify, tmp[i]))
,但这没有帮助. - 我试图将浮点数转换为int
list(map(int, tmp[i]))
,但这都没有帮助.
- To reduce the precision of the floating point
Float(tmp[i][j], 3)
, but it didn't help. - I have tried to convert floats to Rational
list(map(nsimplify, tmp[i]))
, but it didn't help. - I have tried to convert floats to int
list(map(int, tmp[i]))
, but it didn't help neither.
即使我将每个元素都转换为int
,我也真的不明白为什么它无法解决问题.
I really can't understand why it doesn't work out, even if I convert every element to int
.
推荐答案
由于拉普拉斯算子是整数矩阵,让我们使用整数:
Since the Laplacian is an integer matrix, let us use integers:
L = Matrix([[ 1, 0, 0, 0, -1, 0, 0, 0, 0, 0],
[ 0, 1, 0, 0, 0, -1, 0, 0, 0, 0],
[ 0, 0, 1, 0, 0, 0, 0, 0, -1, 0],
[ 0, 0, 0, 1, 0, 0, 0, 0, -1, 0],
[-1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[ 0, -1, 0, 0, 0, 3, 0, 0, -1, -1],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 1, 0, -1],
[ 0, 0, -1, -1, 0, -1, 0, 0, 3, 0],
[ 0, 0, 0, 0, 0, -1, 0, -1, 0, 2]])
计算特征值:
>>> L.eigenvals()
{0: 3, 1: 1, 2: 1}
这很奇怪,因为矩阵是10×10,而不是5×5.
This is very strange, as the matrix is 10-by-10, not 5-by-5.
我试图计算约旦范式,但无法执行,因为函数jordan_form
生成了错误消息IndexError: list index out of range
.
I tried to compute the Jordan normal form, but could not do it, as function jordan_form
produced the error message IndexError: list index out of range
.
计算特征多项式:
>>> s = Symbol('s')
>>> p = (s * eye(10) - L).det()
>>> p
s**10 - 14*s**9 + 77*s**8 - 214*s**7 + 321*s**6 - 256*s**5 + 99*s**4 - 14*s**3
请注意,最低度的单项式为立方.这使我们可以得出结论,特征值0的多重性为3,因此该图为未连接.
Note that the monomial of lowest degree is cubic. This allows us to conclude that the multiplicity of eigenvalue 0 is 3 and, thus, the graph is not connected.
让我们尝试找到特征多项式的根:
Let us try to find the roots of the characteristic polynomial:
>>> solve(p,s)
[0, 0, 0, 1, 2, CRootOf(s**5 - 11*s**4 + 42*s**3 - 66*s**2 + 39*s - 7, 0), CRootOf(s**5 - 11*s**4 + 42*s**3 - 66*s**2 + 39*s - 7, 1), CRootOf(s**5 - 11*s**4 + 42*s**3 - 66*s**2 + 39*s - 7, 2), CRootOf(s**5 - 11*s**4 + 42*s**3 - 66*s**2 + 39*s - 7, 3), CRootOf(s**5 - 11*s**4 + 42*s**3 - 66*s**2 + 39*s - 7, 4)]
请注意,实际上只找到了5个根(eigenvals
也只产生了5个特征值). 5个缺失的根是五次方s**5 - 11*s**4 + 42*s**3 - 66*s**2 + 39*s - 7
的根.
Note that only 5 roots were actually found (eigenvals
produced only 5 eigenvalues, too). The 5 missing roots are the roots of the quintic s**5 - 11*s**4 + 42*s**3 - 66*s**2 + 39*s - 7
.
自19世纪以来,人们知道并非所有5级(或更高)的多项式都具有可以使用算术运算和部首表示的根.因此,我们可能会要求SymPy进行不可能.最好使用NumPy计算10个特征值的近似值.
It has been known since the 19th century that not all polynomials of degree 5 (or higher) have roots that can be expressed using arithmetic operations and radicals. Hence, we may be asking SymPy to do the impossible. Better use NumPy to compute approximate values of the 10 eigenvalues.
这篇关于SymPy无法计算此矩阵的特征值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!