I have noticed there is a difference between how matlab calculates the eigenvalue and eigenvector of a matrix, where matlab returns the real valued while numpy's return the complex valued eigen valus and vector. For example:
for matrix:
A=
1 -3 3
3 -5 3
6 -6 4
Numpy:
w, v = np.linalg.eig(A)
w
array([ 4. +0.00000000e+00j, -2. +1.10465796e-15j, -2. -1.10465796e-15j])
v
array([[-0.40824829+0.j , 0.24400118-0.40702229j,
0.24400118+0.40702229j],
[-0.40824829+0.j , -0.41621909-0.40702229j,
-0.41621909+0.40702229j],
[-0.81649658+0.j , -0.66022027+0.j , -0.66022027-0.j ]])
Matlab:
[E, D] = eig(A)
E
-0.4082 -0.8103 0.1933
-0.4082 -0.3185 -0.5904
-0.8165 0.4918 -0.7836
D
4.0000 0 0
0 -2.0000 0
0 0 -2.0000
Is there a way of getting the real eigen values in python as it is in matlab?
To get NumPy to return a diagonal array of real eigenvalues when the complex part is small, you could use
According to the Matlab docs,
[E, D] = eig(A)
returnsE
andD
which satisfyA*E = E*D
: I don't have Matlab, so I'll use Octave to check the result you posted:The magnitude of the errors is mainly due to the values reported by Matlab being displayed to a lower precision than the actual values Matlab holds in memory.
In NumPy,
w, v = np.linalg.eig(A)
returnsw
andv
which satisfynp.dot(A, v) = np.dot(v, np.diag(w))
: