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 all commits
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
29 changes: 29 additions & 0 deletions base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ isperm(p::Tuple{}) = true
isperm(p::Tuple{Int}) = p[1] == 1
isperm(p::Tuple{Int,Int}) = ((p[1] == 1) & (p[2] == 2)) | ((p[1] == 2) & (p[2] == 1))

#swap columns i and j of a, in-place
function swapcols!(a::AbstractMatrix, i, j)
i == j && return
cols = indices(a,2)
(i in cols && j in cols) || throw(BoundsError())
for k in indices(a,1)
@inbounds a[k,i],a[k,j] = a[k,j],a[k,i]
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

would be clearer with spaces after the outer commas

end
end
# like permute!! applied to each row of a, in-place in a (overwriting p).
function permutecols!!(a::AbstractMatrix, p::AbstractVector{<:Integer})
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

might be generally useful to have and export, along with corresponding permutecols!, permutecols, permuterows!!, permuterows! and permuterows (yikes, that's a lot variations).

count = 0
start = 0
while count < length(p)
ptr = start = findnext(!iszero, p, start+1)
next = p[start]
count += 1
while next != start
swapcols!(a, ptr, next)
p[ptr] = 0
ptr = next
next = p[next]
count += 1
end
p[ptr] = 0
end
a
end

function permute!!(a, p::AbstractVector{<:Integer})
count = 0
start = 0
Expand Down
49 changes: 49 additions & 0 deletions base/linalg/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,53 @@ function getindex(A::Union{Eigen,GeneralizedEigen}, d::Symbol)
throw(KeyError(d))
end

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

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

Sort the eigenvectors and eigenvalues of an eigenfactorization `F` in place using the eigenvalues
for comparison. See [`sort`](@ref) for a variant that returns a sorted copy leaving `F` itself
unmodified.
"""
function sort!(F::Eigen; by=eigsortby, kw...)
if !issorted(F[:values], by=by, kw...)
perm = sortperm(F[:values]; by=by, kw...)
permute!(F[:values], perm)
Base.permutecols!!(F[:vectors], perm)
end
return F
end

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

Sort the eigenvectors and eigenvalues of an eigenfactorization `F` using the eigenvalues for
comparison. See [`sort!`](@ref) for a variant that sorts `F` in place. See
[`sort(::AbstractVector)`](@ref) for a description of possible keyword arguments.

# Examples
```jldoctest
julia> F1 = eigfact([4.0 1.0; 0 1.0])
Base.LinAlg.Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}([4.0, 1.0], [1.0 -0.316228; 0.0 0.948683])

julia> F1[:values]
2-element Array{Float64,1}:
4.0
1.0

julia> F2 = sort(F1)
Base.LinAlg.Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}([1.0, 4.0], [-0.316228 1.0; 0.948683 0.0])

julia> F2[:values]
2-element Array{Float64,1}:
1.0
4.0
```
"""
sort(F::Eigen; kw...) = sort!(copy(F), kw...)

isposdef(A::Union{Eigen,GeneralizedEigen}) = isreal(A.values) && all(x -> x > 0, A.values)

"""
Expand Down Expand Up @@ -435,3 +482,5 @@ convert(::Type{AbstractMatrix}, F::Eigen) = F.vectors * Diagonal(F.values) / F.v
convert(::Type{AbstractArray}, F::Eigen) = convert(AbstractMatrix, F)
convert(::Type{Matrix}, F::Eigen) = convert(Array, convert(AbstractArray, F))
convert(::Type{Array}, F::Eigen) = convert(Matrix, F)

copy(F::Eigen) = Eigen(copy(F.values), copy(F.vectors))
2 changes: 1 addition & 1 deletion base/linalg/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
csch, eltype, exp, eye, findmax, findmin, fill!, floor, getindex, hcat, imag, indices,
inv, isapprox, isone, IndexStyle, kron, length, log, map, ndims, oneunit, parent,
power_by_squaring, print_matrix, promote_rule, real, round, sec, sech, setindex!, show, similar,
sin, sincos, sinh, size, sqrt, tan, tanh, transpose, trunc, typed_hcat, vec
sin, sincos, sinh, size, sort, sort!, sqrt, tan, tanh, transpose, trunc, typed_hcat, vec
using Base: hvcat_fill, iszero, IndexLinear, _length, promote_op, promote_typeof,
@propagate_inbounds, @pure, reduce, typed_vcat
# We use `_length` because of non-1 indices; releases after julia 0.5
Expand Down
2 changes: 2 additions & 0 deletions doc/src/stdlib/linalg.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ Base.LinAlg.eigs(::Any, ::Any)
Base.LinAlg.svds
Base.LinAlg.peakflops
Base.LinAlg.stride1
Base.sort(::Base.LinAlg.Eigen)
Base.sort!(::Base.LinAlg.Eigen)
```

## Low-level matrix operations
Expand Down
4 changes: 2 additions & 2 deletions doc/src/stdlib/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ can be specified via the `lt` keyword.
## Sorting Functions

```@docs
Base.sort!
Base.sort
Base.sort!(::AbstractVector)
Base.sort(::AbstractVector)
Base.sortperm
Base.Sort.sortperm!
Base.Sort.sortrows
Expand Down
10 changes: 10 additions & 0 deletions test/linalg/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ end
end
end

let aa = rand(10, 10)
bb = aa'aa
for a in (aa, bb)
f = sort(eigfact(a))
@test issorted(f[:values], by=Base.LinAlg.eigsortby)
@test a ≈ f[:vectors] * Diagonal(f[:values]) / f[:vectors]
end
end


# test a matrix larger than 140-by-140 for #14174
let aa = rand(200, 200)
for a in (aa, view(aa, 1:n, 1:n))
Expand Down