Incorrect EigenValues/Vectors with Numpy
Asked Answered
J

1

6

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]
Jeromyjerreed answered 12/9, 2013 at 18:11 Comment(0)
S
6

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)
Sambo answered 12/9, 2013 at 18:22 Comment(5)
Hi unutbu, however isn't there still an issue? If you multiplied A by the first vector result [0, 0.70710678, 0] you would get [0, 0.70710678, 0.70710678] which can't be scaled back into the eigenvector?Jeromyjerreed
After transposing e_vecs, the eigenvectors are the rows of e_vecs. I transposed e_vecs to make it easier to iterate over the eigenvectors using zip(e_vals, e_vecs).Sambo
+1 for transposing the eigenvectors, I have been fighting this for several hours and still the results were weird...Linker
Thank you! transposing is key.Gamboa
Well said. For solving that transpose hassle use numpy.matrix.transpose(e_vecs). Refer docs.scipy.org/doc/numpy/reference/generated/…. And another one thing to be noted is that those vectors are normalized unit vectors.Bobbe

© 2022 - 2024 — McMap. All rights reserved.