Skip to content

eigen and eigvals do not sort eigenvalues of Diagonal matrix #1471

@araujoms

Description

@araujoms

As far as I can tell the eigenvalues are always sorted, unless the input is Diagonal:

julia> m = Diagonal([1 0;0 -1]);

julia> eigvals(m)
2-element Vector{Int64}:
  1
 -1

julia> eigen(m)
Eigen{Float64, Int64, Diagonal{Float64, Vector{Float64}}, Vector{Int64}}
values:
2-element Vector{Int64}:
  1
 -1
vectors:
2×2 Diagonal{Float64, Vector{Float64}}:
 1.0    
     1.0

This is rather inconvenient. I think it should be made consistent, and documented. Currently the docstring of eigvals doesn't say anything about sorting, whereas the docstring of eigen says that "By default, the eigenvalues and vectors are sorted lexicographically by (real(λ),imag(λ)). A different comparison function by(λ) can be passed to sortby, or you can pass sortby=nothing to leave the eigenvalues in an arbitrary order. Some special matrix types (e.g. Diagonal or SymTridiagonal) may implement their own sorting convention and not accept a sortby keyword."

I really dislike when docstrings are so vague. Which special matrix types? Can we at least count on Symmetric and Hermitian to be sorted? Or is this undocumented behaviour and the user must always sort the output of again to be sure, lest it break without warning? This is very easy to fix, and I want to do it myself, I'm just asking here first to see if anyone objects.

A consequence of this vagueness is that eigmin and eigmax can't just take the first or last element of the vector of eigenvalues, but must take the minimum or maximum. A related issue is that the docstrings of eigmin and eigmax contain the absurd sentence "Note that if the eigenvalues of A are complex, this method will fail, since complex numbers cannot be sorted.", which will then get fixed for free.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions