验证本征值和本征向量

验证python中根据np.linalg.eig生成的本征值和本征向量的对应关系:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import numpy as np

dim = 4
M = np.random.randn(dim, dim)
M = M + M.T
print(M)

lm, u = np.linalg.eig(M)
print('Eigenvalues:')
print(lm)

print('Eigenvectors:')
print(u)

for i in range(0, 4):
e_val = lm[i]
v = []
for j in range(0, 4):
e_vec = u[j][i]
v.append(e_vec)
a = np.array(v)
a = a.reshape(-1, 1) # 将行向量转置为列向量,这里先转换为np.array,再用reshape
print('M * a:')
print(M.dot(a))
print('r * a:')
print(e_val * a)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import numpy as np

dim = 4
M = np.random.randn(dim, dim)
M = M + M.T
print(M)

lm, u = np.linalg.eig(M)
print('Eigenvalues:')
print(lm)

print('Eigenvectors:')
print(u)

for i in range(0, 4):
e_val = lm[i]
a = u[:, i] # 这种方法可以验证,但是得到的向量是行向量
print('M * a:')
print(M.dot(a))
print('r * a:')
print(e_val * a)