Skip to content

Commit

Permalink
Carry forward implementation from PR JuliaLang#49673
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindh-krishnamoorthy committed Jul 5, 2023
1 parent d70ee20 commit ece7669
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
21 changes: 21 additions & 0 deletions stdlib/LinearAlgebra/src/symmetriceigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ function eigen!(A::AbstractMatrix, C::Cholesky; sortby::Union{Function,Nothing}=
GeneralizedEigen(sorteig!(vals, vecs, sortby)...)
end

function eigen(A::AbstractMatrix{T}, B::BunchKaufman{T,<:StridedMatrix}; sortby::Union{Function,Nothing}=nothing) where {T<:Number}
# Bunchkaufman decomposition based eigenvalues and eigenvectors
if B.uplo == 'U'
vals, w = eigen!(ldiv(lu!(copy(B.D)), UtiAUi!(A[B.p,B.p], B.U')); sortby)
vecs = (B.U' \ w)[invperm(B.p),:]
else # B.uplo == 'L'
vals, w = eigen!(ldiv(lu!(copy(B.D)), UtiAUi!(A[B.p,B.p], B.L')); sortby)
vecs = (B.L' \ w)[invperm(B.p),:]
end
GeneralizedEigen(sorteig!(vals, vecs, sortby)...)
end

# Perform U' \ A / U in-place, where U::Union{UpperTriangular,Diagonal}
UtiAUi!(A, U) = _UtiAUi!(A, U)
UtiAUi!(A::Symmetric, U) = Symmetric(_UtiAUi!(copytri!(parent(A), A.uplo), U), sym_uplo(A.uplo))
Expand Down Expand Up @@ -218,3 +230,12 @@ function eigvals!(A::AbstractMatrix{T}, C::Cholesky{T, <:AbstractMatrix}; sortby
# Cholesky decomposition based eigenvalues
return eigvals!(UtiAUi!(A, C.U); sortby)
end

# Bunch-Kaufmann (LDLT) based solution for generalized eigenvalues
function eigvals(A::AbstractMatrix{T}, B::BunchKaufman{T,<:StridedMatrix}; sortby::Union{Function,Nothing}=nothing) where {T<:Number}
if B.uplo == 'U'
return eigvals!(ldiv(lu!(copy(B.D)), UtiAUi!(A[B.p,B.p], B.U')); sortby)
else # B.uplo == 'L'
return eigvals!(ldiv(lu!(copy(B.D)), UtiAUi!(A[B.p,B.p], B.L')); sortby)
end
end
48 changes: 48 additions & 0 deletions stdlib/LinearAlgebra/test/symmetriceigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,52 @@ end
@test eigvals(AH, BH; sortby=sf) eigvals(Hermitian(AH), Hermitian(BH); sortby=sf)
end

@testset "bk-eigen-eigals" begin
# Bunchkaufman decomposition based

# eigenvalue sorting
sf = x->(real(x),imag(x))

# Real-valued random matrix
N = 10
A = randn(N,N)
B = randn(N,N)
BH = (B+B')/2
# eigen
e0 = eigvals(A,BH; sortby=sf)
e,v = eigen(A,bunchkaufman(Hermitian(BH,:L)); sortby=sf)
@test e0 e
@test A*v BH*v*Diagonal(e)
e,v = eigen(A,bunchkaufman(Hermitian(BH,:U)); sortby=sf)
@test e0 e
@test A*v BH*v*Diagonal(e)
# eigvals
e0 = eigvals(A,BH; sortby=sf)
el = eigvals(A,bunchkaufman(Hermitian(BH,:L)); sortby=sf)
eu = eigvals(A,bunchkaufman(Hermitian(BH,:U)); sortby=sf)
@test e0 el
@test e0 eu

# Complex-valued random matrix
N = 10
A = complex.(randn(N,N),randn(N,N))
B = complex.(randn(N,N),randn(N,N))
BH = (B+B')/2
sf = x->(real(x),imag(x))
# eigen
e0 = eigvals(A,BH; sortby=sf)
e,v = eigen(A,bunchkaufman(Hermitian(BH,:L)); sortby=sf)
@test e0 e
@test A*v BH*v*Diagonal(e)
e,v = eigen(A,bunchkaufman(Hermitian(BH,:U)); sortby=sf)
@test e0 e
@test A*v BH*v*Diagonal(e)
# eigvals
e0 = eigvals(A,BH; sortby=sf)
el = eigvals(A,bunchkaufman(Hermitian(BH,:L)); sortby=sf)
eu = eigvals(A,bunchkaufman(Hermitian(BH,:U)); sortby=sf)
@test e0 el
@test e0 eu
end

end # module TestSymmetricEigen

0 comments on commit ece7669

Please sign in to comment.