diff --git a/base/linalg/cholesky.jl b/base/linalg/cholesky.jl index 7555b0ca02125..938a066027c13 100644 --- a/base/linalg/cholesky.jl +++ b/base/linalg/cholesky.jl @@ -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}}: @@ -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 diff --git a/base/linalg/lu.jl b/base/linalg/lu.jl index 265d80109325c..8370a7e566bdb 100644 --- a/base/linalg/lu.jl +++ b/base/linalg/lu.jl @@ -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 @@ -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 diff --git a/test/linalg/cholesky.jl b/test/linalg/cholesky.jl index fac4696d48df7..8c77fb4cefe55 100644 --- a/test/linalg/cholesky.jl +++ b/test/linalg/cholesky.jl @@ -102,8 +102,8 @@ 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) @@ -111,8 +111,8 @@ end 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 @@ -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) diff --git a/test/linalg/lu.jl b/test/linalg/lu.jl index a607f6278a03b..a22ed82002256 100644 --- a/test/linalg/lu.jl +++ b/test/linalg/lu.jl @@ -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 @@ -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 @@ -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