Incorrect EigenValues/Vectors with Numpy

こ雲淡風輕ζ 提交于 2019-11-30 17:53:10

问题


I am trying to find the eigenvalues/vectors for the following matrix:

A = np.array([[1, 0, 0],
              [0, 1, 0],
              [1, 1, 0]])

using the code:

from numpy import linalg as LA
e_vals, e_vecs = LA.eig(A)

I'm getting this as the answer:

print(e_vals)
[ 0.  1.  1.]

print(e_vecs)
[[ 0.          0.70710678  0.        ]
 [ 0.          0.          0.70710678]
 [ 1.          0.70710678  0.70710678]]

However, I believe the following should be the answer.

[1] Real Eigenvalue = 0.00000
[1] Real Eigenvector:
0.00000
0.00000
1.00000

[2] Real Eigenvalue = 1.00000
[2] Real Eigenvector:
1.00000
0.00000
1.00000

[3] Real Eigenvalue = 1.00000
[3] Real Eigenvector:
0.00000
1.00000
1.00000

That is, the eigenvalue-eigenvector problem says that the follow should hold true:

# A * e_vecs = e_vals * e_vecs
print(A.dot(e_vecs))
[[ 0.          0.70710678  0.        ]
 [ 0.          0.          0.70710678]
 [ 0.          0.70710678  0.70710678]]

print(e_vals.dot(e_vecs))
[ 1.          0.70710678  1.41421356]

回答1:


The eigenvalues returned by linalg.eig are columns vectors, so you need to iterate over the transpose of e_vecs (since iteration over a 2D array returns row vectors by default):

import numpy as np
import numpy.linalg as LA
A = np.array([[1, 0, 0], [0, 1, 0], [1, 1, 0]])
e_vals, e_vecs = LA.eig(A)

print(e_vals)
# [ 0.  1.  1.]
print(e_vecs)
# [[ 0.          0.          1.        ]
#  [ 0.70710678  0.          0.70710678]
#  [ 0.          0.70710678  0.70710678]]

for val, vec in zip(e_vals, e_vecs.T):
    assert np.allclose(np.dot(A, vec), val * vec)


来源:https://stackoverflow.com/questions/18771486/incorrect-eigenvalues-vectors-with-numpy

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