Friday, March 1, 2019

[python][tutorial] Eigenvalues and Eigenvectors

Example 1
Let use python calculate what’s the determinant of first:  
import numpy as np
from scipy import linalg as la
A = np.array([[1,2],[3,2]])
print(A)
B = la.det(A)
print(B)
Result:
[[1 2]
 [3 2]]
-4.0
 That’s means:

In this case, 4 is the eigenvalues λ


import numpy as np
from scipy import linalg as la

A = np.array([[1,5,2],[2,4,1],[3,6,2]])
lna,v = la.eig(A)
l1,l2,l3 =lna
#Eigenvalue
print(l1,l2,l3)
print("----------------")
#Eigenvector
print(v)
print("----------------")
print(v[:,0])
print(v[:,1])
print(v[:,2])
v1 = np.array(v[:,0]).T
print("----------------")
print(v1)
print(la.norm(A.dot(v1)-l1*v1))
Result:
(7.95791620491+0j) (-1.25766470568+0j) (0.299748500767+0j)
----------------
[[-0.5297175  -0.90730751  0.28380519]
 [-0.44941741  0.28662547 -0.39012063]
 [-0.71932146  0.30763439  0.87593408]]
----------------
[-0.5297175  -0.44941741 -0.71932146]
[-0.90730751  0.28662547  0.30763439]
[ 0.28380519 -0.39012063  0.87593408]
----------------
[-0.5297175  -0.44941741 -0.71932146]
3.233018248352212e-15
V[:,0] means 1st line of matrix

No comments :

Post a Comment