Skip to content

Commit fafa4cc

Browse files
authored
(Even) Better LinearMap display (#115)
1 parent c218831 commit fafa4cc

File tree

2 files changed

+38
-58
lines changed

2 files changed

+38
-58
lines changed

src/show.jl

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,75 @@
11
# summary
2-
function Base.summary(io::IO, A::LinearMap)
3-
print(io, Base.dims2string(size(A)))
4-
print(io, ' ')
5-
_show_typeof(io, A)
2+
Base.summary(io::IO, A::LinearMap) = print(io, map_summary(A))
3+
function map_summary(A::LinearMap)
4+
Base.dims2string(size(A)) * ' ' * _show_typeof(A)
65
end
76

87
# show
9-
Base.show(io::IO, A::LinearMap) = (summary(io, A); _show(io, A))
10-
_show(io::IO, ::LinearMap) = nothing
11-
function _show(io::IO, A::FunctionMap{T,F,Nothing}) where {T,F}
12-
print(io, "($(A.f); ismutating=$(A._ismutating), issymmetric=$(A._issymmetric), ishermitian=$(A._ishermitian), isposdef=$(A._isposdef))")
8+
Base.show(io::IO, A::LinearMap) = print(io, map_show(io, A, 0))
9+
10+
map_show(io::IO, A::LinearMap, i) = " "^i * map_summary(A) * _show(io, A, i)
11+
map_show(io::IO, A::AbstractMatrix, i) = " "^i * string(typeof(A))
12+
_show(io::IO, ::LinearMap, _) = ""
13+
function _show(io::IO, A::FunctionMap{T,F,Nothing}, _) where {T,F}
14+
"($(A.f); ismutating=$(A._ismutating), issymmetric=$(A._issymmetric), ishermitian=$(A._ishermitian), isposdef=$(A._isposdef))"
1315
end
14-
function _show(io::IO, A::FunctionMap)
15-
print(io, "($(A.f), $(A.fc); ismutating=$(A._ismutating), issymmetric=$(A._issymmetric), ishermitian=$(A._ishermitian), isposdef=$(A._isposdef))")
16+
function _show(io::IO, A::FunctionMap, _)
17+
"($(A.f), $(A.fc); ismutating=$(A._ismutating), issymmetric=$(A._issymmetric), ishermitian=$(A._ishermitian), isposdef=$(A._isposdef))"
1618
end
17-
function _show(io::IO, A::Union{CompositeMap,LinearCombination,KroneckerMap,KroneckerSumMap})
19+
function _show(io::IO, A::Union{CompositeMap,LinearCombination,KroneckerMap,KroneckerSumMap}, i)
1820
n = length(A.maps)
19-
println(io, " with $n map", n>1 ? "s" : "", ":")
20-
print_maps(io, A.maps)
21+
" with $n map" * (n>1 ? "s" : "") * ":\n" * print_maps(io, A.maps, i+2)
2122
end
22-
function _show(io::IO, A::Union{AdjointMap,TransposeMap,WrappedMap})
23-
print(io, " of ")
24-
L = A.lmap
25-
if A isa MatrixMap
26-
# summary(io, L)
27-
# println(io, ":")
28-
# Base.print_matrix(io, L)
29-
print(io, typeof(L))
30-
else
31-
show(io, L)
32-
end
23+
function _show(io::IO, A::Union{AdjointMap,TransposeMap,WrappedMap}, i)
24+
" of\n" * map_show(io, parent(A), i+2)
3325
end
34-
function _show(io::IO, A::BlockMap)
26+
function _show(io::IO, A::BlockMap, i)
3527
nrows = length(A.rows)
3628
n = length(A.maps)
37-
println(io, " with $n block map", n>1 ? "s" : "", " in $nrows block row", nrows>1 ? "s" : "")
38-
print_maps(io, A.maps)
29+
" with $n block map" * (n>1 ? "s" : "") * " in $nrows block row" * (nrows>1 ? "s" : "") *
30+
"\n" * print_maps(io, A.maps, i+2)
3931
end
40-
function _show(io::IO, A::BlockDiagonalMap)
32+
function _show(io::IO, A::BlockDiagonalMap, i)
4133
n = length(A.maps)
42-
println(io, " with $n diagonal block map", n>1 ? "s" : "")
43-
print_maps(io, A.maps)
34+
" with $n diagonal block map" * (n>1 ? "s" : "") * ":\n" * print_maps(io, A.maps, i+2)
4435
end
45-
function _show(io::IO, J::UniformScalingMap)
46-
s = "$(J.λ)"
47-
print(io, " with scaling factor: $s")
36+
function _show(io::IO, J::UniformScalingMap, _)
37+
" with scaling factor: $(J.λ)"
4838
end
49-
function _show(io::IO, A::ScaledMap{T}) where {T}
50-
println(io, " with scale: $(A.λ) of")
51-
show(io, A.lmap)
39+
function _show(io::IO, A::ScaledMap{T}, i) where {T}
40+
" with scale: $(A.λ) of\n" * map_show(io, A.lmap, i+2)
5241
end
5342

5443
# helper functions
55-
function _show_typeof(io::IO, A::LinearMap{T}) where {T}
56-
Base.show_type_name(io, typeof(A).name)
57-
print(io, '{')
58-
show(io, T)
59-
print(io, '}')
44+
function _show_typeof(A::LinearMap{T}) where {T}
45+
split(string(typeof(A)), '{')[1] * '{' * string(T) * '}'
6046
end
6147

62-
function print_maps(io::IO, maps::Tuple{Vararg{LinearMap}})
48+
function print_maps(io::IO, maps::Tuple{Vararg{LinearMap}}, k)
6349
n = length(maps)
50+
str = ""
6451
if get(io, :limit, true) && n > 10
6552
s = 1:5
6653
e = n-5:n
6754
if e[1] - s[end] > 1
6855
for i in s
69-
# print(io, ' ')
70-
show(io, maps[i])
71-
println(io, "")
56+
str *= map_show(io, maps[i], k) * "\n"
7257
end
73-
print(io, "")
58+
str *= " "^k * ""
7459
for i in e
75-
println(io, "")
76-
show(io, maps[i])
60+
str *= "\n" * map_show(io, maps[i], k)
7761
end
7862
else
7963
for i in 1:n-1
80-
# print(io, ' ')
81-
show(io, maps[i])
82-
println(io, "")
64+
str *= map_show(io, maps[i], k) * "\n"
8365
end
84-
# print(io, ' ')
85-
show(io, last(maps))
66+
str *= map_show(io, last(maps), k)
8667
end
8768
else
8869
for i in 1:n-1
89-
# print(io, ' ')
90-
show(io, maps[i])
91-
println(io, "")
70+
str *= map_show(io, maps[i], k) * "\n"
9271
end
93-
# print(io, ' ')
94-
show(io, last(maps))
72+
str *= map_show(io, last(maps), k)
9573
end
74+
return str
9675
end

test/linearcombination.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using Test, LinearMaps, LinearAlgebra, BenchmarkTools
1212
L = sum(fill(CS!, n))
1313
@test_throws AssertionError LinearMaps.LinearCombination{Float64}((CS!, CS!))
1414
@test occursin("10×10 LinearMaps.LinearCombination{$(eltype(L))}", sprint((t, s) -> show(t, "text/plain", s), L))
15+
@test occursin("10×10 LinearMaps.LinearCombination{$(eltype(L))}", sprint((t, s) -> show(t, "text/plain", s), L+CS!))
1516
@test parent(L) == ntuple(_ -> CS!, 10)
1617
@test mul!(u, L, v) n * cumsum(v)
1718
b = @benchmarkable mul!($u, $L, $v, 2, 2)

0 commit comments

Comments
 (0)