Arrays of complex values with very small imaginary part

See e.g.

For example, if you just have some roundoff errors in your construction of the complex arrays, you could get a tiny imaginary part in the eigenvalues.

If the real-ness of eigenvalues is physically meaningful, the best approach would be to construct your matrices so that this is guaranteed. e.g. construct your matrices so that they are Hermitian (and use Hermitian(M)) in the cases where you expect real eigenvalues. In a physical problem, often you can do this by expressing the physics in the right way…

If this is not possible, you’ll have to resort to some kind of precision test, e.g. replace isreal(v) with something like norm(imag(v)) ≤ norm(imag(v)) * eps(eltype(v)) * length(v). But these kinds of tests are hard to make 100% robust.

1 Like