Karol Borkowski - 1 year ago 90
C++ Question

# How to get original order of eigenvalues using Eigen?

I have a diagonal matrix with eigenvalues e.g. 1, 2 and 3. I disturb its values with some noise but it is small enough to change the sequence. When I obtain the eigenvalues of this matrix they are 1,2,3 in 50% cases and 1,3,2 in another 50%.
When I do the same thing without the noise the order is always 1,2,3.

I obtain the eigenvalues using:

``````matrix.eigenvalues().real();
``````

or using:

``````Eigen::EigenSolver<Eigen::Matrix3d> es(matrix, false);
es.eigenvalues().real();
``````

The result is the same. Any ideas how to fix it?

If your matrix happens to be self-adjoint you should use the `SelfAdjointEigenSolver`, of course (which does sort the eigenvalues, since they are all real and therefore sortable). Otherwise, you need to sort the eigenvalues manually by whatever criterion you prefer.
N.B.: The result of `matrix.eigenvalues()` and `es.eigenvalues()` should indeed be the same, since exactly the same algorithm is applied. Essentially the first variant is just a short-hand, if you are only interested in the eigenvalues.