Skip to content

Commit 4943431

Browse files
authored
More compact show for block sparse arrays (#42)
1 parent 2f14c41 commit 4943431

File tree

7 files changed

+124
-5
lines changed

7 files changed

+124
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.2.15"
4+
version = "0.2.16"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/BlockSparseArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ include("blocksparsearrayinterface/cat.jl")
2525

2626
# functions defined for any abstractblocksparsearray
2727
include("abstractblocksparsearray/abstractblocksparsearray.jl")
28-
include("abstractblocksparsearray/wrappedabstractblocksparsearray.jl")
2928
include("abstractblocksparsearray/abstractblocksparsematrix.jl")
3029
include("abstractblocksparsearray/abstractblocksparsevector.jl")
30+
include("abstractblocksparsearray/wrappedabstractblocksparsearray.jl")
3131
include("abstractblocksparsearray/views.jl")
3232
include("abstractblocksparsearray/arraylayouts.jl")
3333
include("abstractblocksparsearray/sparsearrayinterface.jl")

src/abstractblocksparsearray/abstractblocksparsearray.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,42 @@ function Base.setindex!(
7777
blocks(a)[Int.(I)...] = value
7878
return a
7979
end
80+
81+
using TypeParameterAccessors: unspecify_type_parameters
82+
function show_typeof_blocksparse(io::IO, a::AbstractBlockSparseArray)
83+
Base.show(io, unspecify_type_parameters(typeof(a)))
84+
print(io, '{')
85+
show(io, eltype(a))
86+
print(io, ", ")
87+
show(io, ndims(a))
88+
print(io, ", ")
89+
show(io, blocktype(a))
90+
print(io, ", …")
91+
print(io, '}')
92+
return nothing
93+
end
94+
95+
# Copied from `BlockArrays.jl`.
96+
block2string(b, s) = string(join(map(string, b), '×'), "-blocked ", Base.dims2string(s))
97+
98+
function summary_blocksparse(io::IO, a::AbstractArray)
99+
print(io, block2string(blocksize(a), size(a)))
100+
print(io, ' ')
101+
show_typeof_blocksparse(io, a)
102+
return nothing
103+
end
104+
105+
function Base.summary(io::IO, a::AbstractBlockSparseArray)
106+
summary_blocksparse(io, a)
107+
return nothing
108+
end
109+
110+
function Base.showarg(io::IO, a::AbstractBlockSparseArray, toplevel::Bool)
111+
if toplevel
112+
show_typeof_blocksparse(io, a)
113+
else
114+
print(io, "::")
115+
show_typeof_blocksparse(io, a)
116+
end
117+
return nothing
118+
end

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using BlockArrays:
1010
mortar,
1111
unblock
1212
using DerivableInterfaces: DerivableInterfaces, @interface
13+
using GPUArraysCore: @allowscalar
1314
using SplitApplyCombine: groupcount
1415
using TypeParameterAccessors: similartype
1516

@@ -390,6 +391,6 @@ function Base.show(io::IO, mime::MIME"text/plain", a::AnyAbstractBlockSparseArra
390391
print(io, ":")
391392
println(io)
392393
a′ = ReplacedUnstoredBlockSparseArray(a, GetUnstoredBlockShow(axes(a)))
393-
Base.print_array(io, a′)
394+
@allowscalar Base.print_array(io, a′)
394395
return nothing
395396
end

src/blocksparsearray/blocksparsearray.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,40 @@ blockstype(arraytype::Type{<:BlockSparseArray}) = SparseArrayDOK{AbstractArray}
199199
## # TODO: Preserve GPU data!
200200
## return BlockSparseArray{elt}(undef, axes)
201201
## end
202+
203+
# TypeParameterAccessors.jl interface
204+
using TypeParameterAccessors: TypeParameterAccessors, Position, set_type_parameters
205+
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(eltype)) = Position(1)
206+
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(ndims)) = Position(2)
207+
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blocktype)) = Position(3)
208+
function TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blockstype))
209+
return Position(4)
210+
end
211+
212+
# TODO: Make this generic to `AbstractBlockSparseVector` using
213+
# TypeParameterAccessors.jl, for example using:
214+
# `set_ndims(unspecify_type_parameters(typeof(a)), 1)`.
215+
function show_typeof_blocksparse(io::IO, a::BlockSparseVector)
216+
print(io, "BlockSparseVector")
217+
print(io, '{')
218+
show(io, eltype(a))
219+
print(io, ", ")
220+
show(io, blocktype(a))
221+
print(io, ", …")
222+
print(io, '}')
223+
return nothing
224+
end
225+
226+
# TODO: Make this generic to `AbstractBlockSparseMatrix` using
227+
# TypeParameterAccessors.jl, for example using:
228+
# `set_ndims(unspecify_type_parameters(typeof(a)), 2)`.
229+
function show_typeof_blocksparse(io::IO, a::BlockSparseMatrix)
230+
print(io, "BlockSparseMatrix")
231+
print(io, '{')
232+
show(io, eltype(a))
233+
print(io, ", ")
234+
show(io, blocktype(a))
235+
print(io, ", …")
236+
print(io, '}')
237+
return nothing
238+
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ SymmetrySectors = "f8a8ad64-adbc-4fce-92f7-ffe2bb36a86e"
1919
TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
2020
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2121
TestExtras = "5ed8adda-3752-4e41-b88a-e8b09835ee3a"
22+
TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138"

test/test_basics.jl

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ using SparseArraysBase: SparseArrayDOK, SparseMatrixDOK, SparseVectorDOK, stored
3838
using TensorAlgebra: contract
3939
using Test: @test, @test_broken, @test_throws, @testset, @inferred
4040
using TestExtras: @constinferred
41+
using TypeParameterAccessors: TypeParameterAccessors, Position
4142
include("TestBlockSparseArraysUtils.jl")
4243

4344
arrayts = (Array, JLArray)
@@ -1084,13 +1085,53 @@ arrayts = (Array, JLArray)
10841085
@test storedlength(b) == 17
10851086
end
10861087
@testset "show" begin
1088+
vectort_elt = arrayt{elt,1}
1089+
matrixt_elt = arrayt{elt,2}
1090+
arrayt_elt = arrayt{elt,3}
1091+
1092+
a = BlockSparseVector{elt,arrayt{elt,1}}([2, 2])
1093+
# Either option is possible depending on namespacing.
1094+
@test (
1095+
sprint(summary, a) ==
1096+
"2-blocked 4-element BlockSparseVector{$(elt), $(vectort_elt), …}"
1097+
) || (
1098+
sprint(summary, a) ==
1099+
"2-blocked 4-element BlockSparseArrays.BlockSparseVector{$(elt), $(vectort_elt), …}"
1100+
)
1101+
1102+
a = BlockSparseMatrix{elt,arrayt{elt,2}}([2, 2], [2, 2])
1103+
# Either option is possible depending on namespacing.
1104+
@test (
1105+
sprint(summary, a) == "2×2-blocked 4×4 BlockSparseMatrix{$(elt), $(matrixt_elt), …}"
1106+
) || (
1107+
sprint(summary, a) ==
1108+
"2×2-blocked 4×4 BlockSparseArrays.BlockSparseMatrix{$(elt), $(matrixt_elt), …}"
1109+
)
1110+
1111+
a = BlockSparseArray{elt,3,arrayt{elt,3}}([2, 2], [2, 2], [2, 2])
1112+
1113+
# Either option is possible depending on namespacing.
1114+
@test (
1115+
sprint(summary, a) ==
1116+
"2×2×2-blocked 4×4×4 BlockSparseArray{$(elt), 3, $(arrayt_elt), …}"
1117+
) || (
1118+
sprint(summary, a) ==
1119+
"2×2×2-blocked 4×4×4 BlockSparseArrays.BlockSparseArray{$(elt), 3, $(arrayt_elt), …}"
1120+
)
1121+
10871122
if elt === Float64
10881123
# Not testing other element types since they change the
10891124
# spacing so it isn't easy to make the test general.
1090-
a = BlockSparseArray{elt}([2, 2], [2, 2])
1091-
a[1, 2] = 12
1125+
a = BlockSparseMatrix{elt,arrayt{elt,2}}([2, 2], [2, 2])
1126+
@allowscalar a[1, 2] = 12
10921127
@test sprint(show, "text/plain", a) ==
10931128
"$(summary(a)):\n $(zero(eltype(a))) $(eltype(a)(12)) │ . .\n $(zero(eltype(a))) $(zero(eltype(a))) │ . .\n ───────────┼──────\n . . │ . .\n . . │ . ."
10941129
end
10951130
end
1131+
@testset "TypeParameterAccessors.position" begin
1132+
@test TypeParameterAccessors.position(BlockSparseArray, eltype) == Position(1)
1133+
@test TypeParameterAccessors.position(BlockSparseArray, ndims) == Position(2)
1134+
@test TypeParameterAccessors.position(BlockSparseArray, blocktype) == Position(3)
1135+
@test TypeParameterAccessors.position(BlockSparseArray, blockstype) == Position(4)
1136+
end
10961137
end

0 commit comments

Comments
 (0)