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:
master
← jishnub/symhermshow
opened 05:57AM - 06 Oct 24 UTC
After this,
```julia
julia> A = rand(2,2);
julia> H = Hermitian(A)
2×2 Her… mitian{Float64, Matrix{Float64}}:
0.889108 0.264298
0.264298 0.234985
julia> println(H)
Hermitian([0.8891078167175059 0.26429820656950753; 0.26429820656950753 0.23498548977257172])
julia> S = Symmetric(A)
2×2 Symmetric{Float64, Matrix{Float64}}:
0.889108 0.264298
0.264298 0.234985
julia> println(S)
Symmetric([0.8891078167175059 0.26429820656950753; 0.26429820656950753 0.23498548977257172])
julia> U = UpperTriangular(A)
2×2 UpperTriangular{Float64, Matrix{Float64}}:
0.889108 0.264298
⋅ 0.234985
julia> println(U)
UpperTriangular([0.8891078167175059 0.26429820656950753; 0.0 0.23498548977257172])
julia> U = UnitUpperTriangular(A)
2×2 UnitUpperTriangular{Float64, Matrix{Float64}}:
1.0 0.264298
⋅ 1.0
julia> println(U)
UnitUpperTriangular([1.0 0.26429820656950753; 0.0 1.0])
julia> L = LowerTriangular(A)
2×2 LowerTriangular{Float64, Matrix{Float64}}:
0.889108 ⋅
0.2504 0.234985
julia> println(L)
LowerTriangular([0.8891078167175059 0.0; 0.2503997651590879 0.23498548977257172])
julia> L = UnitLowerTriangular(A)
2×2 UnitLowerTriangular{Float64, Matrix{Float64}}:
1.0 ⋅
0.2504 1.0
julia> println(L)
UnitLowerTriangular([1.0 0.0; 0.2503997651590879 1.0])
```
The displayed form is now a valid constructor, and may be copy-pasted to reconstruct the object.
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.