PineNuts0
PineNuts0

Reputation: 5234

Linear Algebra in Python: Calculating Eigenvectors for 3x3 Matrix

I am using Python to derive the eigenvectors associated with the eigenvalues in a 3x3 matrix. My code is returning correct eigenvalues but wrong eigenvectors.

A = np.array([[-2, -4,  2],
              [-2,  1,  2],
              [4,   2,  5]])
print (A)
print ('-------------------------------------------------------')

eigenvalues, eigenvectors = np.linalg.eig(A) # must use this line of code exactly 
print(f'eigenvalues of matrix A are:{eigenvalues}')
print ('-------------------------------------------------------')
print(f'eigenvectors of matrix A are:{eigenvectors}')

enter image description here

For example, the eigenvector associated with value 6 should be [1, 6, 16], not what the code output.

Upvotes: 2

Views: 2197

Answers (2)

yudhiesh
yudhiesh

Reputation: 6799

It is correct and you can check it by the eigenvector/eigenvalue condition for the second eigenvalue and eigenvector.

enter image description here

Where u is the eigenvector and lambda is its eigenvalue. So we multiply the eigenvector v[:,1] by A and check that it is the same as multiplying the same eigenvector by its eigenvalue w[1].

import numpy as np

>>> w, v = np.linalg.eig(A)
# w contains the eigenvalues. 
# v contains the corresponding eigenvectors, one eigenvector per column. 
# The eigenvectors are normalized so their Euclidean norms are 1
>>> u = v[:,1]
>>> print(u)
[ 0.53452248, -0.80178373, -0.26726124]

>>> lam = w[1]
>>> lam
3.0

>>> print(np.dot(A,u))
[ 1.60356745 -2.40535118 -0.80178373]
>>> print(lam*u)
[ 1.60356745 -2.40535118 -0.80178373]

Upvotes: 3

عبدو عيد
عبدو عيد

Reputation: 27

See the key here is as said above the normalization process made by the NumPy library to display the array of the "Eigenvectors" numerically as it is not programmed to display something like

# when λ = 6
x_dash = np.array([[ 1*x],
                   [ 6*x],
                   [16*x]])
# and then you replace x with your lucky number that would save you some effort in math manipulation

, so in your case, you expect [1, 6, 16] as eigenvector for the 6 eigenvalues, that is OK, don't panic. You just have to recognize that the whole vector underwent a dot multiplication process with some constant that came from the vectorization, and in your case, it happens to be

0.05842062

  1. 1 * 0.05842062 = 0.05842062
  2. 6 * 0.05842062 = 0.35052372
  3. 16 * 0.05842062 = 0.93472992

that is what you get with np.linalg library

Upvotes: 0

Related Questions