Skip to content

Commit 4779a52

Browse files
authored
Improve printing of GradedArray (#34)
1 parent 852934d commit 4779a52

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GradedArrays"
22
uuid = "bc96ca6e-b7c8-4bb6-888e-c93f838762c2"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.4.1"
4+
version = "0.4.2"
55

66
[deps]
77
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"

src/gradedarray.jl

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ using BlockSparseArrays:
99
using LinearAlgebra: Adjoint
1010
using TypeParameterAccessors: similartype, unwrap_array_type
1111

12-
const GradedArray{T,M,A,Blocks,Axes} = BlockSparseArray{
13-
T,M,A,Blocks,Axes
14-
} where {Axes<:Tuple{AbstractGradedUnitRange,Vararg{AbstractGradedUnitRange}}}
12+
const GradedArray{T,N,A<:AbstractArray{T,N},Blocks<:AbstractArray{A,N},Axes<:Tuple{AbstractGradedUnitRange{<:Integer},Vararg{AbstractGradedUnitRange{<:Integer}}}} = BlockSparseArray{
13+
T,N,A,Blocks,Axes
14+
}
1515
const GradedMatrix{T,A,Blocks,Axes} = GradedArray{
1616
T,2,A,Blocks,Axes
1717
} where {Axes<:Tuple{AbstractGradedUnitRange,AbstractGradedUnitRange}}
@@ -119,3 +119,77 @@ function Base.getindex(
119119
end
120120

121121
ungrade(a::GradedArray) = sparsemortar(blocks(a), ungrade.(axes(a)))
122+
123+
# Copy of `Base.dims2string` defined in `show.jl`.
124+
function dims_to_string(d)
125+
isempty(d) && return "0-dimensional"
126+
length(d) == 1 && return "$(d[1])-element"
127+
return join(map(string, d), '×')
128+
end
129+
130+
# Copy of `BlockArrays.block2string` from `BlockArrays.jl`.
131+
block_to_string(b, s) = string(join(map(string, b), '×'), "-blocked ", dims_to_string(s))
132+
133+
using TypeParameterAccessors: type_parameters, unspecify_type_parameters
134+
function base_type_and_params(type::Type)
135+
alias = Base.make_typealias(type)
136+
base_type, params = if isnothing(alias)
137+
unspecify_type_parameters(type), type_parameters(type)
138+
else
139+
base_type_globalref, params_svec = alias
140+
base_type_globalref.name, params_svec
141+
end
142+
return base_type, params
143+
end
144+
145+
function base_type_and_params(type::Type{<:GradedArray})
146+
return :GradedArray, type_parameters(type)
147+
end
148+
function base_type_and_params(type::Type{<:GradedVector})
149+
params = type_parameters(type)
150+
params′ = [params[1:1]..., params[3:end]...]
151+
return :GradedVector, params′
152+
end
153+
function base_type_and_params(type::Type{<:GradedMatrix})
154+
params = type_parameters(type)
155+
params′ = [params[1:1]..., params[3:end]...]
156+
return :GradedMatrix, params′
157+
end
158+
159+
# Modified version of `BlockSparseArrays.concretetype_to_string_truncated`.
160+
# This accounts for the fact that the GradedArray alias is not defined in
161+
# BlockSparseArrays so for the sake of printing, Julia doesn't show it as
162+
# an alias: https://github.com/JuliaLang/julia/issues/40448
163+
function concretetype_to_string_truncated(type::Type; param_truncation_length=typemax(Int))
164+
isconcretetype(type) || throw(ArgumentError("Type must be concrete."))
165+
base_type, params = base_type_and_params(type)
166+
str = string(base_type)
167+
if isempty(params)
168+
return str
169+
end
170+
str *= '{'
171+
param_strings = map(params) do param
172+
param_string = string(param)
173+
if length(param_string) > param_truncation_length
174+
return ""
175+
end
176+
return param_string
177+
end
178+
str *= join(param_strings, ", ")
179+
str *= '}'
180+
return str
181+
end
182+
183+
using BlockArrays: blocksize
184+
function Base.summary(io::IO, a::GradedArray)
185+
print(io, block_to_string(blocksize(a), size(a)))
186+
print(io, ' ')
187+
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))
188+
return nothing
189+
end
190+
191+
function Base.showarg(io::IO, a::GradedArray, toplevel::Bool)
192+
!toplevel && print(io, "::")
193+
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))
194+
return nothing
195+
end

test/test_gradedarray.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,23 @@ end
420420
@test space_isequal(axes(ad, 1), dual(axes(a, 1)))
421421
@test space_isequal(axes(ad, 2), dual(axes(a, 2)))
422422
end
423+
424+
@testset "show" begin
425+
elt = Float64
426+
r = gradedrange([U1(0) => 2, U1(1) => 2])
427+
428+
a = zeros(elt, r)
429+
a[1] = one(elt)
430+
@test sprint(show, "text/plain", a) ==
431+
"2-blocked 4-element GradedVector{$(elt), Vector{$(elt)}, …, …}:\n $(one(elt))\n $(zero(elt))\n ───\n\n"
432+
433+
a = zeros(elt, r, r)
434+
a[1, 1] = one(elt)
435+
@test sprint(show, "text/plain", a) ==
436+
"2×2-blocked 4×4 GradedMatrix{$(elt), Matrix{$(elt)}, …, …}:\n $(one(elt)) $(zero(elt)) │ ⋅ ⋅ \n $(zero(elt)) $(zero(elt)) │ ⋅ ⋅ \n ──────────┼──────────\n ⋅ ⋅ │ ⋅ ⋅ \n ⋅ ⋅ │ ⋅ ⋅ "
437+
438+
a = zeros(elt, r, r, r)
439+
a[1, 1, 1] = one(elt)
440+
@test sprint(show, "text/plain", a) ==
441+
"2×2×2-blocked 4×4×4 GradedArray{$(elt), 3, Array{$(elt), 3}, …, …}:\n[:, :, 1] =\n $(one(elt)) $(zero(elt)) ⋅ ⋅ \n $(zero(elt)) $(zero(elt)) ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n\n[:, :, 2] =\n $(zero(elt)) $(zero(elt)) ⋅ ⋅ \n $(zero(elt)) $(zero(elt)) ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n\n[:, :, 3] =\n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n\n[:, :, 4] =\n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ "
442+
end

0 commit comments

Comments
 (0)