Sort the output of eigs by the eigenvalues

I’m using eigs to find the two eigenvalues with the largest magnitude near a value sigma. One of the eigenvalues is negative while the other is positive. How can I get eigs to give me the eigenvalues/eigenvectors sorted according to sorting the eigenvalues from negative to positive?
The other issue is although my matrix is Hermitian, The eigenvalues have a tiny imaginary part (something like 10^-16) so I’d like to sort the eigenvalues by their real part. Anyone know how to do that?

You should tell eigs that your matrix is Hermitian (by using Hermitian(A)), and then it can use better algorithms (and will give you purely real eigenvalues).

In any case, you can use sort(λ, by=real) to sort an array λ by its real parts, for example. To sort both the eigenvalues λ and eigenvectors Χ, you could do something like:

i = sortperm(λ, by=real)
λ = λ[i]
Χ = Χ[:, i]
3 Likes

Thanks for your reply. I have applied Hermitian(A) on my matrix but the tiny imaginary parts remain which is weird.

Try it like this:

julia> A = rand(4,4);

julia> B = Hermitian(A' * A);

julia> eigvals(B)
4-element Vector{Float64}:
 0.00032801015074382795
 0.16413528628451352
 0.8127243537979139
 5.157242997370142

Which eigensolver package are you using?

I’m using Arpack.

I’m not sure Arpack supports complex Hermitian matrices (https://github.com/JuliaLinearAlgebra/Arpack.jl/issues/142)?

Maybe try KrylovKit.eigsolve and pass ishermitian=true.