# Sort the output of eigs by the eigenvalues

**URL:** <https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455>\
**Category:** New to Julia\
**Tags:** sort, arpack\
**Created:** [April 13, 2022, 8:26pm UTC](https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455 "2022-04-13T20:26:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://discourse.julialang.org/u/ahmed)\
**Post date:** [April 13, 2022, 8:26pm UTC](https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455/1 "2022-04-13T20:26:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 13, 2022, 8:44pm UTC](https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455/2 "2022-04-13T20:44:05Z")

</div>

> [@ahmed](#):
>
> 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:

```julia
i = sortperm(λ, by=real)
λ = λ[i]
Χ = Χ[:, i]

```

---

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://discourse.julialang.org/u/ahmed)\
**Post date:** [April 13, 2022, 9:19pm UTC](https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455/3 "2022-04-13T21:19:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [April 13, 2022, 9:37pm UTC](https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455/4 "2022-04-13T21:37:23Z")

</div>

Try it like this:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 13, 2022, 11:17pm UTC](https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455/5 "2022-04-13T23:17:33Z")

</div>

> [@ahmed](#):
>
> I have applied Hermitian(A) on my matrix but the tiny imaginary parts remain which is weird.

Which eigensolver package are you using?

---

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://discourse.julialang.org/u/ahmed)\
**Post date:** [April 13, 2022, 11:42pm UTC](https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455/6 "2022-04-13T23:42:11Z")

</div>

I’m using Arpack.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 14, 2022, 12:14am UTC](https://discourse.julialang.org/t/sort-the-output-of-eigs-by-the-eigenvalues/79455/7 "2022-04-14T00:14:07Z")

</div>

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

Maybe try [`KrylovKit.eigsolve`](https://jutho.github.io/KrylovKit.jl/stable/man/eig/#KrylovKit.eigsolve) and pass `ishermitian=true`.
