5

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]
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235

1 Answers1

5

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)
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 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? –  Sep 12 '13 at 18:52
  • 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)`. – unutbu Sep 12 '13 at 19:02
  • +1 for transposing the eigenvectors, I have been fighting this for several hours and still the results were weird... – dominecf Nov 02 '16 at 12:59
  • Thank you! transposing is key. – yunzhan Apr 12 '17 at 14:51
  • Well said. For solving that transpose hassle use numpy.matrix.transpose(e_vecs). Refer https://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.transpose.html. And another one thing to be noted is that those vectors are normalized unit vectors. – Seeni Mar 04 '18 at 10:52