Eigenvector在Sympy中不起作用

时间:2015-04-15 18:10:04

标签: python sympy

我想在Sympy中找到矩阵的特征向量并编写以下程序,但它不起作用。另一方面,Sympy中的A.eigenvects()函数计算矩阵A的特征值和特征向量,我在这里使用了类似的东西但是当我想打印结果时,会显示一个空列表。你能指导我吗?

from sympy import *

H=Matrix([  [215.0   ,-104.1   ,5.1    ,-4.3    ,4.7    ,-15.1   ,-7.8],
        [-104.1  , 220.0   ,32.6   , 7.1    ,5.4    , 8.3    ,0.8],
        [ 5.1    , 32.6    ,  0.   , -46.8  , 1.0   , -8.1   , 5.1 ],
        [ -4.3   , 7.1     ,-46.8  ,125.0   ,-70.7  ,-14.7   ,-61.5],
        [ 4.7    , 5.4     , 1.0   ,-70.7   ,450.0  ,89.7    ,-2.5],
        [-15.1   , 8.3     ,-8.1   ,-14.7   ,89.7   ,330.0   ,32.7],
        [-7.8    ,0.8      ,5.1    ,-61.5   ,-2.5   ,32.7    ,280.0]])


zz=H.eigenvects()
pprint(zz)

3 个答案:

答案 0 :(得分:7)

问题源于Floats和Rationals的使用。 eigenvects重写Floats到Rationals。当根不能分解多项式时,eigenvals例程失败。当您直接致电eigenvals时,Floats也会重铸,但您可以选择来重铸它们;当你这样做时,roots能够返回值并返回特征值。

>>> H.eigenvals()
{}
>>> H.eigenvals(rational=False)
{513.317044781366: 1, 101.965215714556: 1, 332.004505895816: 1, 268.369453977695: 1, 120.955771704237: 1, -23.7383543150805: 1, 307.126362241411: 1}

似乎roots应该为这个7阶多项式返回RootOf实例,而不是返回没有根。将此报告为SymPy的问题会很好:

  

roots(p) - >在p = 5000000*x**7 - 8100000000*x**6 + 5146847850000*x**5 - 1623625381660000*x**4 + 261251048199624000*x**3 - 18813344309673222800*x**2 + 248990094307079384205*x + 20562650438939697400552

时代替RootOf实例

答案 1 :(得分:2)

这看起来对我来说可能是个错误,因为某些原因它不喜欢你的矩阵。尝试使用eigenvals()也不会返回任何内容,但使用Berkowitz算法会产生预期结果(这些是正确的):

>>> H.berkowitz_eigenvals()
{−23.7383543150805:1,101.965215714556:1,120.955771704237:1,
   268.369453977695:1,307.126362241411:1,332.004505895816:1,513.317044781366:1}

或者,找到字符多项式根得到相同的结果:

>>> roots(H.charpoly(x),x)
{−23.7383543150805:1,101.965215714556:1,120.955771704237:1,
   268.369453977695:1,307.126362241411:1,332.004505895816:1,513.317044781366:1}

至于解决方法,除了使用另一个库,或许NumPy / SciPy之外,我现在还没有任何想法:

>>> from numpy import linalg as LA
>>> w,v = LA.eig(np.array([  [215.0   ,-104.1   ,5.1    ,-4.3    ,4.7    ,-15.1   ,-7.8],
        [-104.1  , 220.0   ,32.6   , 7.1    ,5.4    , 8.3    ,0.8],
        [ 5.1    , 32.6    ,  0.   , -46.8  , 1.0   , -8.1   , 5.1 ],
        [ -4.3   , 7.1     ,-46.8  ,125.0   ,-70.7  ,-14.7   ,-61.5],
        [ 4.7    , 5.4     , 1.0   ,-70.7   ,450.0  ,89.7    ,-2.5],
        [-15.1   , 8.3     ,-8.1   ,-14.7   ,89.7   ,330.0   ,32.7],
        [-7.8    ,0.8      ,5.1    ,-61.5   ,-2.5   ,32.7    ,280.0]])
>>> w;v
array([[ 0.0211232 , -0.0863685 ,  0.31060486,  0.64800412,  0.58825511,
         0.34578278, -0.1004976 ],
       [-0.03360278, -0.17141713,  0.28577077,  0.60531169, -0.57444552,
        -0.41080118,  0.15058085],
       [-0.01492258,  0.91780802, -0.23783515,  0.29790711, -0.04561479,
        -0.00789624,  0.09974215],
       [ 0.19183148,  0.33999268,  0.79845203, -0.30609739,  0.01552874,
        -0.18077   , -0.2889039 ],
       [-0.86037599,  0.04835763,  0.171535  , -0.10783263,  0.27161704,
        -0.27390267,  0.25993089],
       [-0.45801107,  0.01859027, -0.05846719,  0.07732967, -0.35064091,
         0.32022588, -0.74497537],
       [-0.1066849 ,  0.05006013,  0.30810033, -0.11677503, -0.35344244,
         0.70807431,  0.50125772]])

答案 2 :(得分:2)

    >>> H
    Matrix([
    [ 215.0, -104.1,   5.1,  -4.3,   4.7, -15.1,  -7.8],
    [-104.1,  220.0,  32.6,   7.1,   5.4,   8.3,   0.8],
    [   5.1,   32.6,     0, -46.8,   1.0,  -8.1,   5.1],
    [  -4.3,    7.1, -46.8, 125.0, -70.7, -14.7, -61.5],
    [   4.7,    5.4,   1.0, -70.7, 450.0,  89.7,  -2.5],
    [ -15.1,    8.3,  -8.1, -14.7,  89.7, 330.0,  32.7],
    [  -7.8,    0.8,   5.1, -61.5,  -2.5,  32.7, 280.0]])
    >>> x = H.charpoly(symbols('lamda'))
    >>> factor(x)
    1.0*(1.0*lamda**7 - 1620.0*lamda**6 + 1029369.57*lamda**5 - 
324725076.332*lamda**4 + 52250209639.9248*lamda**3 - 
3762668861934.64*lamda**2 + 49798018861415.7*lamda + 
4.112530087788e+15)

最后一个方程有7个实根=> (lamda +23.738) *(lamda -101.965) *(lamda -120.956) *(lamda -268.369) *(lamda -307.126) *(lamda -332.005) *(lamda -513.317) = 0

[-23.738, 101.965, 120.956, 268.369, 307.126, 332.005, 513.317]

这真的很奇怪,为什么同情者一直说这个矩阵没有特征向量。