Skip to content

Commit

Permalink
Add missing logabsdet(LDLt{SymTridiagonal}) method (#24085)
Browse files Browse the repository at this point in the history
* Add missing logabsdet(LDLt{SymTridiagonal}) method

* Fix seed for some of the sparse tests calling factorize
  • Loading branch information
andreasnoack authored Oct 11, 2017
1 parent cd8bda2 commit 93a38b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
5 changes: 5 additions & 0 deletions base/linalg/ldlt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ function A_ldiv_B!(S::LDLt{T,M}, B::AbstractVecOrMat{T}) where {T,M<:SymTridiago
return B
end

function logabsdet(F::LDLt{<:Any,<:SymTridiagonal})
it = (F.data[i,i] for i in 1:size(F, 1))
return sum(logabs, it), prod(sign, it)
end

# Conversion methods
function convert(::Type{SymTridiagonal}, F::LDLt)
e = copy(F.data.ev)
Expand Down
6 changes: 6 additions & 0 deletions test/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ guardsrand(123) do
@test isa(similar(Ts, Int), SymTridiagonal{Int})
@test isa(similar(Ts, Int, (3,2)), Matrix{Int})
end

@test first(logabsdet(Tldlt)) first(logabsdet(Fs))
@test last(logabsdet(Tldlt)) last(logabsdet(Fs))
# just test that the det method exists. The numerical value of the
# determinant is unreliable
det(Tldlt)
end
end
else # mat_type is Tridiagonal
Expand Down
46 changes: 24 additions & 22 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1696,28 +1696,30 @@ end
end

@testset "factorization" begin
local A
A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2) + im*sprandn(5, 5, 0.2)
A = A + A'
@test !Base.USE_GPL_LIBS || abs(det(factorize(Hermitian(A)))) abs(det(factorize(Array(A))))
A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2) + im*sprandn(5, 5, 0.2)
A = A*A'
@test !Base.USE_GPL_LIBS || abs(det(factorize(Hermitian(A)))) abs(det(factorize(Array(A))))
A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2)
A = A + A.'
@test !Base.USE_GPL_LIBS || abs(det(factorize(Symmetric(A)))) abs(det(factorize(Array(A))))
A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2)
A = A*A.'
@test !Base.USE_GPL_LIBS || abs(det(factorize(Symmetric(A)))) abs(det(factorize(Array(A))))
@test factorize(triu(A)) == triu(A)
@test isa(factorize(triu(A)), UpperTriangular{Float64, SparseMatrixCSC{Float64, Int}})
@test factorize(tril(A)) == tril(A)
@test isa(factorize(tril(A)), LowerTriangular{Float64, SparseMatrixCSC{Float64, Int}})
@test !Base.USE_GPL_LIBS || factorize(A[:, 1:4])\ones(size(A, 1)) Array(A[:, 1:4])\ones(size(A, 1))
@test_throws ErrorException chol(A)
@test_throws ErrorException lu(A)
@test_throws ErrorException eig(A)
@test_throws ErrorException inv(A)
guardsrand(123) do
local A
A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2) + im*sprandn(5, 5, 0.2)
A = A + A'
@test !Base.USE_GPL_LIBS || abs(det(factorize(Hermitian(A)))) abs(det(factorize(Array(A))))
A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2) + im*sprandn(5, 5, 0.2)
A = A*A'
@test !Base.USE_GPL_LIBS || abs(det(factorize(Hermitian(A)))) abs(det(factorize(Array(A))))
A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2)
A = A + A.'
@test !Base.USE_GPL_LIBS || abs(det(factorize(Symmetric(A)))) abs(det(factorize(Array(A))))
A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2)
A = A*A.'
@test !Base.USE_GPL_LIBS || abs(det(factorize(Symmetric(A)))) abs(det(factorize(Array(A))))
@test factorize(triu(A)) == triu(A)
@test isa(factorize(triu(A)), UpperTriangular{Float64, SparseMatrixCSC{Float64, Int}})
@test factorize(tril(A)) == tril(A)
@test isa(factorize(tril(A)), LowerTriangular{Float64, SparseMatrixCSC{Float64, Int}})
@test !Base.USE_GPL_LIBS || factorize(A[:, 1:4])\ones(size(A, 1)) Array(A[:, 1:4])\ones(size(A, 1))
@test_throws ErrorException chol(A)
@test_throws ErrorException lu(A)
@test_throws ErrorException eig(A)
@test_throws ErrorException inv(A)
end
end

@testset "issue #13792, use sparse triangular solvers for sparse triangular solves" begin
Expand Down

0 comments on commit 93a38b7

Please sign in to comment.