寻找最低特征值的特征向量 - 矩阵乘法的维数错误
Finding eigenvector of the lowest eigenvalue - matrix multiplication has wrong dimensions
Python 这里是新手。
我正在尝试验证特征值问题 - ACmin = EminCmin
但是矩阵乘法不起作用,因为提取相应的特征向量以一种奇怪的方式存储([[[ ]]] 而不是 [[ ]] )
如果您对此或其他方法有任何帮助,我们将不胜感激!
谢谢!!!
代码如下:
import numpy as np
# 5X5 matrix
S = np.zeros((5,5))
S[0,0] = S[1,1] = S[2,2] = S[3,3] = S[4,4] = 2/3
S[0,1] = S[1,0] = S[2,1] = S[1,2] = S[2,3]= S[3,2] = S[3,4] = S[4,3] = 1/6
S_inv = np.linalg.inv(S)
I = np.matmul(S,S_inv)
## check if true I==I
H = np.zeros((5,5))
H[0,0] = H[4,4] = 71/30
H[1,1] = H[3,3] = 41/30
H[2,2] = 31/30
H[0,1] = H[1,0] = H[3,4] = H[4,3] = -37/120
H[1,2] = H[2,1] = H[2,3] = H[3,2] = -19/40
A = np.matmul(S_inv,H) ## A = s^-1*H
w, v = np.linalg.eig(A) ## find eigenvectros and eigenvalues
E_min = np.amin(w) ## find lowest eigenvalue
c_min = v[:,np.where(w == np.amin(w))]
print (c_min)
print('c_min =', c_min, 'has eigenvalue',E_min)
ACmin = np.matmul(A,c_min)
Emincmin = np.matmul(E_min,c_min)
In [26]: w.shape
Out[26]: (5,)
In [27]: v.shape
Out[27]: (5, 5)
In [28]: w
Out[28]: array([5.85888748, 4.57205335, 2.90645081, 0.53081555, 1.64794665])
In [29]: w[3]
Out[29]: 0.530815550482961
按列索引 v
工作正常:
In [30]: v[:,3]
Out[30]: array([-0.08971467, -0.45507958, -0.75478984, -0.45507958, -0.08971467])
In [31]: w[3]*v[:,3]
Out[31]: array([-0.04762194, -0.24156332, -0.40065418, -0.24156332, -0.04762194])
您对 np.where
的使用为 c_min
添加了维度:
In [32]: E_min = np.amin(w) ## find lowest eigenvalue
...: c_min = v[:,np.where(w == np.amin(w))]
In [33]: E_min.shape
Out[33]: ()
In [34]: E_min
Out[34]: 0.530815550482961
In [35]: c_min.shape
Out[35]: (5, 1, 1)
In [36]: E_min * c_min.squeeze()
Out[36]: array([-0.04762194, -0.24156332, -0.40065418, -0.24156332, -0.04762194])
np.matmul
不喜欢你的标量 E_min
。但是 dot
没问题:
In [37]: np.dot(E_min, c_min.squeeze())
Out[37]: array([-0.04762194, -0.24156332, -0.40065418, -0.24156332, -0.04762194])
您可以使用 argmin
:
而不是 where
In [44]: v[:,np.argmin(w)].shape
Out[44]: (5,)
Python 这里是新手。 我正在尝试验证特征值问题 - ACmin = EminCmin 但是矩阵乘法不起作用,因为提取相应的特征向量以一种奇怪的方式存储([[[ ]]] 而不是 [[ ]] )
如果您对此或其他方法有任何帮助,我们将不胜感激!
谢谢!!! 代码如下:
import numpy as np
# 5X5 matrix
S = np.zeros((5,5))
S[0,0] = S[1,1] = S[2,2] = S[3,3] = S[4,4] = 2/3
S[0,1] = S[1,0] = S[2,1] = S[1,2] = S[2,3]= S[3,2] = S[3,4] = S[4,3] = 1/6
S_inv = np.linalg.inv(S)
I = np.matmul(S,S_inv)
## check if true I==I
H = np.zeros((5,5))
H[0,0] = H[4,4] = 71/30
H[1,1] = H[3,3] = 41/30
H[2,2] = 31/30
H[0,1] = H[1,0] = H[3,4] = H[4,3] = -37/120
H[1,2] = H[2,1] = H[2,3] = H[3,2] = -19/40
A = np.matmul(S_inv,H) ## A = s^-1*H
w, v = np.linalg.eig(A) ## find eigenvectros and eigenvalues
E_min = np.amin(w) ## find lowest eigenvalue
c_min = v[:,np.where(w == np.amin(w))]
print (c_min)
print('c_min =', c_min, 'has eigenvalue',E_min)
ACmin = np.matmul(A,c_min)
Emincmin = np.matmul(E_min,c_min)
In [26]: w.shape
Out[26]: (5,)
In [27]: v.shape
Out[27]: (5, 5)
In [28]: w
Out[28]: array([5.85888748, 4.57205335, 2.90645081, 0.53081555, 1.64794665])
In [29]: w[3]
Out[29]: 0.530815550482961
按列索引 v
工作正常:
In [30]: v[:,3]
Out[30]: array([-0.08971467, -0.45507958, -0.75478984, -0.45507958, -0.08971467])
In [31]: w[3]*v[:,3]
Out[31]: array([-0.04762194, -0.24156332, -0.40065418, -0.24156332, -0.04762194])
您对 np.where
的使用为 c_min
添加了维度:
In [32]: E_min = np.amin(w) ## find lowest eigenvalue
...: c_min = v[:,np.where(w == np.amin(w))]
In [33]: E_min.shape
Out[33]: ()
In [34]: E_min
Out[34]: 0.530815550482961
In [35]: c_min.shape
Out[35]: (5, 1, 1)
In [36]: E_min * c_min.squeeze()
Out[36]: array([-0.04762194, -0.24156332, -0.40065418, -0.24156332, -0.04762194])
np.matmul
不喜欢你的标量 E_min
。但是 dot
没问题:
In [37]: np.dot(E_min, c_min.squeeze())
Out[37]: array([-0.04762194, -0.24156332, -0.40065418, -0.24156332, -0.04762194])
您可以使用 argmin
:
where
In [44]: v[:,np.argmin(w)].shape
Out[44]: (5,)