# LAPACK sorting eigenvalues differently each time

I'm using LAPACK zgeev routine to get eigenvalues and eigenvectors of a symmetric matrix in C++. Problem is zgeev is being called in a loop but it sorts eigenvalues (and eigenvectors) differently sometimes.

For example, this is the eigenvalues from the first round of loop:

(-1.29007e-5 - 5.207e-6*i) (1.28782e-5 + 7.40505e-6*i)

And this is it's result from the second time:

(1.28782e-5 + 7.40505e-6*i) (-1.29007e-5 - 5.207e-6*i)

I need to plot the evolution of these eigenvalues and vectors as a function of the loop's variable but they keep getting swapped each time and gives me a combination of the plots I need.

How to fix this?

• When eigenvalues are complex, sorting them becomes "problematic" because there is no notion of A>B for two complex numbers. You might want to multiply the eigen values by their complex conjugate and use that to "sort them" in order to recuperate a similar structure as a function of time. Otherwise, I do not see how you could prevent such swapping since by definition there is no way to "sort" the eigenvalues (like largest to smallest, etc.) – BlaB Jul 31 '17 at 14:32
• @BlaisB So is there any way around it? – Alireza Jul 31 '17 at 14:33
• see my edited comment. – BlaB Jul 31 '17 at 14:34
• @BlaisB What about eigenvectors? They're my main problem and they get all normalized and there is no way of first sorting eigenvalues and then computing the vectors. – Alireza Jul 31 '17 at 14:40

In general, sorting complex eigenvalues does not solve your problem. Consider the matrix $$A(t) = \begin{bmatrix} e^{\mathrm{i} t} & 0 \\ 0 & e^{\mathrm{i} (t + \pi)} \end{bmatrix}$$ for $t = [0, 2\pi)$. The matrix has two eigenvalues, both lie on the circle of radius one. The two eigenvalues lie on opposit sides of the circle and with increasing $t$ they rotate around zero. When $t$ is large enough the first eigenvalue reaches the point where the second eigenvalue has been, and vice versa. Hence, any sorting technique will (at latest) at that point, switch the roles of the two eigenvalues, even though the first eigenvalue moved slowly to the position of the second, meaning it has not changed its role.