Skip to content

Commit

Permalink
Fix printing of Cholesky and LU factors (#24665)
Browse files Browse the repository at this point in the history
* Fix printing of Cholesky and LU factors

* Fix doctests
  • Loading branch information
andreasnoack authored Nov 21, 2017
1 parent d148437 commit f9db0ab
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
21 changes: 16 additions & 5 deletions base/linalg/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,12 @@ julia> A = [4. 12. -16.; 12. 37. -43.; -16. -43. 98.]
-16.0 -43.0 98.0
julia> C = cholfact(A)
Base.LinAlg.Cholesky{Float64,Array{Float64,2}} with factor:
[2.0 6.0 -8.0; 0.0 1.0 5.0; 0.0 0.0 3.0]
Base.LinAlg.Cholesky{Float64,Array{Float64,2}}
U factor:
3×3 UpperTriangular{Float64,Array{Float64,2}}:
2.0 6.0 -8.0
⋅ 1.0 5.0
⋅ ⋅ 3.0
julia> C[:U]
3×3 UpperTriangular{Float64,Array{Float64,2}}:
Expand Down Expand Up @@ -399,15 +403,22 @@ end

issuccess(C::Cholesky) = C.info == 0

function show(io::IO, C::Cholesky{<:Any,<:AbstractMatrix})
function show(io::IO, mime::MIME{Symbol("text/plain")}, C::Cholesky{<:Any,<:AbstractMatrix})
if issuccess(C)
println(io, "$(typeof(C)) with factor:")
show(io, C[:UL])
println(io, summary(C), "\n$(C.uplo) factor:")
show(io, mime, C[:UL])
else
print(io, "Failed factorization of type $(typeof(C))")
end
end

function show(io::IO, mime::MIME{Symbol("text/plain")}, C::CholeskyPivoted{<:Any,<:AbstractMatrix})
println(io, summary(C), "\n$(C.uplo) factor with rank $(rank(C)):")
show(io, mime, C.uplo == 'U' ? C[:U] : C[:L])
println(io, "\npermutation:")
show(io, mime, C[:p])
end

A_ldiv_B!(C::Cholesky{T,<:AbstractMatrix}, B::StridedVecOrMat{T}) where {T<:BlasFloat} =
@assertposdef LAPACK.potrs!(C.uplo, C.factors, B) C.info

Expand Down
22 changes: 14 additions & 8 deletions base/linalg/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,15 @@ julia> A = [4 3; 6 3]
6 3
julia> F = lufact(A)
Base.LinAlg.LU{Float64,Array{Float64,2}} with factors L and U:
[1.0 0.0; 1.5 1.0]
[4.0 3.0; 0.0 -1.5]
Base.LinAlg.LU{Float64,Array{Float64,2}}
L factor:
2×2 Array{Float64,2}:
1.0 0.0
1.5 1.0
U factor:
2×2 Array{Float64,2}:
4.0 3.0
0.0 -1.5
julia> F[:L] * F[:U] == A[F[:p], :]
true
Expand Down Expand Up @@ -232,12 +238,12 @@ end

issuccess(F::LU) = F.info == 0

function show(io::IO, F::LU)
function show(io::IO, mime::MIME{Symbol("text/plain")}, F::LU)
if issuccess(F)
println(io, "$(typeof(F)) with factors L and U:")
show(io, F[:L])
println(io)
show(io, F[:U])
println(io, summary(F), "\nL factor:")
show(io, mime, F[:L])
println(io, "\nU factor:")
show(io, mime, F[:U])
else
print(io, "Failed factorization of type $(typeof(F))")
end
Expand Down
10 changes: 5 additions & 5 deletions test/linalg/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ end
capds = cholfact!(copy(apds))
unary_ops_tests(apds, capds, ε*κ*n)
end
ulstring = sprint(show, capds[:UL])
@test sprint(show,capds) == "$(typeof(capds)) with factor:\n$ulstring"
ulstring = sprint((t, s) -> show(t, "text/plain", s), capds[:UL])
@test sprint((t, s) -> show(t, "text/plain", s), capds) == "$(typeof(capds))\nU factor:\n$ulstring"
else
capdh = cholfact(apdh)
unary_ops_tests(apdh, capdh, ε*κ*n)
capdh = cholfact!(copy(apdh))
unary_ops_tests(apdh, capdh, ε*κ*n)
capdh = cholfact!(copy(apd))
unary_ops_tests(apd, capdh, ε*κ*n)
ulstring = sprint(show, capdh[:UL])
@test sprint(show,capdh) == "$(typeof(capdh)) with factor:\n$ulstring"
ulstring = sprint((t, s) -> show(t, "text/plain", s), capdh[:UL])
@test sprint((t, s) -> show(t, "text/plain", s), capdh) == "$(typeof(capdh))\nU factor:\n$ulstring"
end

# test chol of 2x2 Strang matrix
Expand Down Expand Up @@ -180,7 +180,7 @@ end
C = cholfact(A)
@test !isposdef(C)
@test !LinAlg.issuccess(C)
Cstr = sprint(show, C)
Cstr = sprint((t, s) -> show(t, "text/plain", s), C)
@test Cstr == "Failed factorization of type $(typeof(C))"
@test_throws PosDefException C\B
@test_throws PosDefException det(C)
Expand Down
25 changes: 23 additions & 2 deletions test/linalg/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dimg = randn(n)/2
@testset "Singular LU" begin
lua = lufact(zeros(eltya, 3, 3))
@test !LinAlg.issuccess(lua)
@test sprint(show, lua) == "Failed factorization of type $(typeof(lua))"
@test sprint((t, s) -> show(t, "text/plain", s), lua) == "Failed factorization of type $(typeof(lua))"
end
κ = cond(a,1)
@testset "(Automatic) Square LU decomposition" begin
Expand All @@ -77,9 +77,10 @@ dimg = randn(n)/2
bflua = convert(bft, lua)
@test bflua[:L]*bflua[:U] big.(a)[p,:] rtol=ε
end
# compact printing
lstring = sprint(show,l)
ustring = sprint(show,u)
@test sprint(show,lua) == "$(typeof(lua)) with factors L and U:\n$lstring\n$ustring"
# @test sprint(show,lua) == "$(typeof(lua)) with factors L and U:\n$lstring\n$ustring"
end
κd = cond(Array(d),1)
@testset "Tridiagonal LU" begin
Expand Down Expand Up @@ -241,3 +242,23 @@ end
@testset "Issue 21453" begin
@test_throws ArgumentError LinAlg._cond1Inf(lufact(randn(5,5)), 2, 2.0)
end

@testset "REPL printing" begin
bf = IOBuffer()
show(bf, "text/plain", lufact(Matrix(I, 4, 4)))
seekstart(bf)
@test String(take!(bf)) == """
Base.LinAlg.LU{Float64,Array{Float64,2}}
L factor:
4×4 Array{Float64,2}:
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
U factor:
4×4 Array{Float64,2}:
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0"""
end

0 comments on commit f9db0ab

Please sign in to comment.