Numpy seems to produce incorrect eigenvectors

倖福魔咒の 提交于 2019-12-23 04:14:09

问题


I want to use Numpy to calculate eigenvalues and eigenvectors. Here is my code:

import numpy as np
from numpy import linalg as LA

lapl = np.array(
       [[ 2, -1, -1,  0,  0,  0],
        [-1,  3,  0, -1,  0, -1],
        [-1,  0,  2, -1,  0,  0],
        [ 0, -1, -1,  3, -1,  0],
        [ 0,  0,  0, -1,  2, -1],
        [ 0, -1,  0,  0, -1,  2]])

w, v = LA.eigh(lapl)

print ('Eigenvalues:', np.round(w,0))
print ('Eigenvectors:', np.round(v,2))

Here is the result:

Eigenvalues: [ 0.  1.  2.  3.  3.  5.]
Eigenvectors: [[ 0.41  0.5   0.41 -0.46  0.34  0.29]
               [ 0.41  0.    0.41  0.53  0.23 -0.58]
               [ 0.41  0.5  -0.41 -0.07 -0.57 -0.29]
               [ 0.41  0.   -0.41  0.53  0.23  0.58]
               [ 0.41 -0.5  -0.41 -0.46  0.34 -0.29]
               [ 0.41 -0.5   0.41 -0.07 -0.57  0.29]]

However, when I run the same matrix in Wolfram Alpha, I am getting a different result - eigenvalues are the same, but the eigenvectors are different:

v1 = ( 1, -2, -1,  2, -1,  1)
v2 = ( 0, -1,  1, -1,  0,  1)
v3 = ( 1, -1,  0, -1,  1,  0)
v4 = ( 1,  1, -1, -1, -1,  1)
v5 = (-1,  0, -1,  0,  1,  1)
v6 = ( 1,  1,  1,  1,  1,  1)

Here is the link to the Wolfram Alpha calculations: http://bit.ly/1wC9EHJ

Why am I getting a different result? What should I do in Python to get the same result as produced by Alpha?

Thanks you very much for the help!


回答1:


The results are different due to multiple reasons:

  1. You probably noticed, that the numpy matrix v contains the eigenvectors as horizontally stacked columns, while you're printing the Wolfram results v1 to v6 as rows.

  2. The scale (or length) of an eigenvector is undefined. So it's usually scaled to length 1. The Wolfram result is scaled differently, which causes some confusion, I guess.

  3. Note that by scaling the vectors, even the sign can change. That's why positive and negative elements might get flipped.

  4. And last but not least: The order of eigenvectors is undefined, as long as they are in the same order like their corresponding eigenvalues. So you'd need to look at Wolfram's eigenvalues as well and possibly reorder v1 to v6 accordingly. (It's a common convention to sort by size of the eigenvalues. Wolfram seems to sort in descending order, while numpy sorts in ascending order.)

  5. In case of matrices with non-unique eigenvalues, the corresponding eigenvectors can arbitrarily rotate, as long as they span the corresponding subspace. However, this doesn't seem to be the case in your example.

Considering the first 4 issues the results are actually pretty close. The singularity shouldn't matter, since there is only one zero eigenvalue, thus the corresponding eigenvector is unique (up to sign and length).



来源:https://stackoverflow.com/questions/26589448/numpy-seems-to-produce-incorrect-eigenvectors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!