Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sorting for Eigen #24536

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions base/linalg/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function getindex(A::Union{Eigen,GeneralizedEigen}, d::Symbol)
throw(KeyError(d))
end

eigsortby(λ::Real) = λ
eigsortby(λ::Complex) = reim(λ)

"""
sort!(F::Base.LinAlg.Eigen; kw...)

Expand All @@ -52,10 +55,12 @@ julia> F[:values]
4.0
```
"""
function sort!(F::Eigen; kw...)
perm = sortperm(F[:values]; kw...)
permute!(F[:values], perm)
Base.permutecols!!(F[:vectors], perm)
function sort!(F::Eigen; by=eigsortby, kw...)
if !issorted(F[:values], by=by)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to pass kw... to issorted too.

perm = sortperm(F[:values]; by=by, kw...)
permute!(F[:values], perm)
Base.permutecols!!(F[:vectors], perm)
end
return F
end

Expand Down
2 changes: 1 addition & 1 deletion test/linalg/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ end

let a = rand(10, 10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add a b = a'a and test the case with real eigenvalues too?

f = eigfact(a)
sort!(f, by=real)
sort!(f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably f = sort(eigfact(a)) instead to make sure that both sort and sort! are tested.

@test a ≈ f[:vectors] * Diagonal(f[:values]) / f[:vectors]
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the purpose is to sort, should we test that too? E.g. @test issorted(...)


Expand Down