Eigen values and vectors sorting

Note that the eigen function takes a parameter sortby that is a function specifying how you want them to be sorted. If you want them sorted in descending order by the real part, you can just pass sortby=-:

julia> A = [i^2 + i*j + j^2 for i=1:4, j=1:4]
4×4 Matrix{Int64}:
  3   7  13  21
  7  12  19  28
 13  19  27  37
 21  28  37  48

julia> eigen(A, sortby=-)
Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}}
values:
4-element Vector{Float64}:
 97.34671140716996
  0.11020703271515231
  3.552713678800501e-15
 -7.456918439885142
vectors:
4×4 Matrix{Float64}:
 -0.260069  -0.629181   0.223607  -0.697492
 -0.374035   0.436083  -0.67082   -0.468966
 -0.526594   0.519101   0.67082   -0.0568572
 -0.717747  -0.380128  -0.223607   0.538835

Or if you wanted them in descending order by magnitude, you could pass sortby = λ -> -abs(λ).

This is usually more convenient than re-ordering them afterwards.

3 Likes