Python newbie here. I'm trying to verify an eigenvalue problem - ACmin = EminCmin but the matrix multiplication doesn't work since extracting the corresponding eigenvector gets stored in a weird way ([[[ ]]] instead of [[ ]] )
Would appreciate any help with this or other ways of doing it!
Thanks!!! code below:
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)
CodePudding user response:
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
Indexing v
by column works fine:
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])
It's your use of np.where
that's adding dimensions to 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
does not like your scalar E_min
. But dot
is ok with it:
In [37]: np.dot(E_min, c_min.squeeze())
Out[37]: array([-0.04762194, -0.24156332, -0.40065418, -0.24156332, -0.04762194])
Instead of the where
you could use argmin
:
In [44]: v[:,np.argmin(w)].shape
Out[44]: (5,)