About the show method for LinearAlgebra.jl objects

I find it convenient to type this command @show A; in Julia REPL, to get an executable code, e.g.,

julia> using LinearAlgebra

julia> A = rand(-9:9, 3, 3);

julia> @show A;
A = [0 -8 -2; -7 -2 3; 1 9 8]

Note that the last line can be readily copy-paste to a text editor and saved there.
And after a while, we can recover the object A in REPL through the code saved in the text editor.

But we cannot enjoy the same experience for some other objects, e.g.

julia> S = Symmetric(A)
3×3 Symmetric{Int64, Matrix{Int64}}:
  0  -8  -2
 -8  -2   3
 -2   3   8

julia> @show S;
S = [0 -8 -2; -8 -2 3; -2 3 8]

Note that the style is plain, although S is Symmetric.

Another example,

julia> B = Bidiagonal(A, :U)
3×3 Bidiagonal{Int64, Vector{Int64}}:
 0  -8  ⋅
 ⋅  -2  3
 ⋅   ⋅  8

julia> @show B;
B = 3×3 Bidiagonal{Int64, Vector{Int64}}:
 diag: 0  -2  8
 super: -8  3

This time, there is a proper “show”, but the generated lines are not executable.

Can we improve this somehow?

Yes, the Bidiagonal show has been updated in v1.12 (I think). There is a slight issue with show for a Symmetric, but it’s also in the works.

1 Like

What is that slight issue?

And here is a Symmetric-related issue, please take a look: Retaining the property of Symmetric? - #4 by WalterMadelim

1 Like

You can have a look at this PR:

There were issues with how block arrays were being printed, but it’s worth reviving this again.

What do you mean by “block array”? is this one?

julia> S = [rand(-9:9, 2, 2) for i in 1:2, j in 1:2]
2×2 Matrix{Matrix{Int64}}:
 [4 4; 3 7]    [6 -4; 8 -1]
 [3 7; -4 -9]  [5 -7; -2 9]

julia> S = Symmetric(S)
2×2 Symmetric{AbstractMatrix, Matrix{Matrix{Int64}}}:
 [4 4; 4 7]    [6 -4; 8 -1]
 [6 8; -4 -1]  [5 -7; -7 9]

julia> @show S;
S = AbstractMatrix[[4 4; 4 7] [6 -4; 8 -1]; [6 8; -4 -1] [5 -7; -7 9]]

(This is my first time to come up with code like this. Who would write this? I think)

I guess it suffices to have the normal case work properly.