JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.74k stars 5.48k forks source link

Better printing of matrix factorizations #24588

Closed andreasnoack closed 4 years ago

andreasnoack commented 7 years ago

Many of the factorizations fall back on the default printing method for structs which is not that useful for the factorizations. I think the best show method is BunchKaufman, e.g.

julia> bkfact(Symmetric(randn(4,4)))
Base.LinAlg.BunchKaufman{Float64,Array{Float64,2}}
D factor:
4×4 Tridiagonal{Float64,Array{Float64,1}}:
 -0.0676221   1.28035     ⋅        ⋅
  1.28035    -0.145229   0.0       ⋅
   ⋅          0.0       -1.62917  0.0
   ⋅           ⋅         0.0      2.03571
U factor:
4×4 Base.LinAlg.UnitUpperTriangular{Float64,Array{Float64,2}}:
 1.0  0.0  -1.00163    0.376568
 0.0  1.0  -0.677938  -0.46118
 0.0  0.0   1.0       -0.345449
 0.0  0.0   0.0        1.0
permutation:
4-element Array{Int64,1}:
 1
 2
 3
 4

because the factors are not printed compactly as in the LU:

julia> lufact(randn(4,4))
Base.LinAlg.LU{Float64,Array{Float64,2}} with factors L and U:
[1.0 0.0 0.0 0.0; -0.832472 1.0 0.0 0.0; 0.194257 -0.260626 1.0 0.0; 0.200069 -0.390519 -0.0486198 1.0]
[-1.29024 -0.943395 -0.567306 -1.07753; 0.0 -1.69623 -0.210781 -0.144626; 0.0 0.0 1.02621 -0.260425; 0.0 0.0 0.0 -0.378657]

Below is a list of what we remains to be done

Dense

Sparse

This supersedes https://github.com/JuliaLang/julia/issues/10586

andreasnoack commented 6 years ago

I'm trying to figure out the proper way to avoid the compact printing of matrices in factorizations. I've realized the difference between

julia> show(STDOUT, Matrix(I, 3, 3))
Bool[true false false; false true false; false false true]

and

julia> show(STDOUT, MIME"text/plain"(), Matrix(I, 3, 3))
3×3 Array{Bool,2}:
  true  false  false
 false   true  false
 false  false   true

but isn't text/plain supposed to be the plainest way to show text? The documentation for show states that

The default MIME type is MIME"text/plain". There is a fallback definition for text/plain output that calls show with 2 arguments. Therefore, this case should be handled by defining a 2-argument show(stream::IO, x::MyType) method.

which suggests that we shouldn't define a show(::IO, ""MIME"text/plain", ::Array) method. Has this already been settled somewhere or should I open a separate issue for this?

StefanKarpinski commented 6 years ago

cc @vtjnash – this distinction was your idea IIRC. What was the thinking here again? Perhaps it should be documented or changed?

andreasnoack commented 6 years ago

The relevant issue is https://github.com/JuliaLang/julia/issues/18004

hochB commented 5 years ago

I was going to ask why the display of svd(A) was so much less readable than lu(A). Good to see you guys are working on it.

Just to show what I meant by less readable with the example I was working with

julia> A = [1 4; 3 5]
2×2 Array{Int64,2}:
 1  4
 3  5

julia> lu(A)
LU{Float64,Array{Float64,2}}
L factor:
2×2 Array{Float64,2}:
 1.0  0.0
 3.0  1.0
U factor:
2×2 Array{Float64,2}:
 1.0   4.0
 0.0  -7.0

julia> svd(A)
SVD{Float64,Float64,Array{Float64,2}}([-0.5715548351332405 -0.8205638734649572; -0.8205638734649574 0.5715548351332405], [7.072510139298385, 0.9897476090001649], [-0.42887834669531066 -0.903362254987387; 0.903362254987387 -0.42887834669531066])
ViralBShah commented 4 years ago

I think we have pretty much done everything here.