I have calculated the eigenvalues of a 5x5 jacobian matrix while looping over a parameter R.
I get both real and complex eigenvalues as expected, but all are given in complex form. The issue I am having is that numpy.sort
sorts these values by the real part of each eigenvalue which is useful to a point, but the real part of each eigenvalue is either increasing/decreasing as I vary R.
Thus there becomes a point where I need to sort by imaginary part also to keep each eigenvalue in the same entry in the output array. The picture below shows what I mean. I get 3 real eigenvalues displayed in positions 2, 3 and 4 in the array jumping to positions 0,1 and 4. How can I sort by imaginary part? I have added my code below. Eigenvalues switching column
import numpy.linalg as la
for a,b,c,d,e,f in zip(T_S, T_C, S_S, S_C, w, R):
eigvals = la.eigvals(np.array([[-f, e, 0, 0, b],
[-e, -f, 0, 0, -a],
[0, 0, -f, e, d],
[0, 0, -e, -d, -c],
[-1/(2*F), 0, D/(2*F), 0, -1/F]]))
eigvals = np.sort(eigvals)
print(np.round(eigvals, decimals = 3))
EDIT: Upon thinking about this, I don't think sorting is going to help me here. Thanks to those who answered.
I wish to keep the purely real eigenvalue in the same position in the array for each iteration. The sorting is precisely what is making it change position.
for i = 4.2 eigvals = [-0.33-4.16j -0.33+4.16j -0.33+0.j 0. -4.2j 0. +4.2j ]
for i = 4.3 eigvals = [-0.35+0.j -0.33-4.26j -0.33+4.26j 0. -4.3j 0. +4.3j ]
In the i = 4.2 case the real root is in position 2 but for i = 4.3 the real part decreases so np.sort moves it to position 0.
I am not sure there is an easy way around this but any suggestions would be great.