Skip to content

Commit f55f2dc

Browse files
authored
Fix printing higher dimensional block sparse arrays (#74)
1 parent 2b4ab96 commit f55f2dc

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
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.3.0"
4+
version = "0.3.1"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -43,7 +43,7 @@ LabelledNumbers = "0.1.0"
4343
LinearAlgebra = "1.10"
4444
MacroTools = "0.5.13"
4545
MapBroadcast = "0.1.5"
46-
SparseArraysBase = "0.3"
46+
SparseArraysBase = "0.3.2"
4747
SplitApplyCombine = "1.2.3"
4848
TensorAlgebra = "0.1.0, 0.2"
4949
Test = "1.10"

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using BlockArrays:
99
blockedrange,
1010
mortar,
1111
unblock
12-
using DerivableInterfaces: DerivableInterfaces, @interface
12+
using DerivableInterfaces: DerivableInterfaces, @interface, DefaultArrayInterface
1313
using GPUArraysCore: @allowscalar
1414
using SplitApplyCombine: groupcount
1515
using TypeParameterAccessors: similartype
@@ -338,11 +338,20 @@ function Base.Array(a::AnyAbstractBlockSparseArray)
338338
end
339339

340340
function SparseArraysBase.isstored(
341-
A::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}
341+
a::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}
342342
) where {N}
343-
bI = BlockIndex(findblockindex.(axes(A), I))
344-
bA = blocks(A)
345-
return isstored(bA, bI.I...) && isstored(bA[bI.I...], bI.α...)
343+
bI = BlockIndex(findblockindex.(axes(a), I))
344+
blocks_a = blocks(a)
345+
return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...)
346+
end
347+
348+
# This circumvents issues passing certain kinds of SubArrays
349+
# to the more generic block sparse `isstored` definition,
350+
# for example `blocks(a)` is broken for certain slices.
351+
function SparseArraysBase.isstored(
352+
a::SubArray{<:Any,N,<:AbstractBlockSparseArray}, I::Vararg{Int,N}
353+
) where {N}
354+
return @interface DefaultArrayInterface() isstored(a, I...)
346355
end
347356

348357
function Base.replace_in_print_matrix(

test/test_basics.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,10 +1148,16 @@ arrayts = (Array, JLArray)
11481148
if elt === Float64
11491149
# Not testing other element types since they change the
11501150
# spacing so it isn't easy to make the test general.
1151+
11511152
a = BlockSparseMatrix{elt,arrayt{elt,2}}(undef, [2, 2], [2, 2])
11521153
@allowscalar a[1, 2] = 12
11531154
@test sprint(show, "text/plain", a) ==
11541155
"$(summary(a)):\n $(zero(eltype(a))) $(eltype(a)(12)) │ ⋅ ⋅ \n $(zero(eltype(a))) $(zero(eltype(a))) │ ⋅ ⋅ \n ───────────┼──────────\n ⋅ ⋅ │ ⋅ ⋅ \n ⋅ ⋅ │ ⋅ ⋅ "
1156+
1157+
a = BlockSparseArray{elt,3,arrayt{elt,3}}(undef, [2, 2], [2, 2], [2, 2])
1158+
@allowscalar a[1, 2, 1] = 121
1159+
@test sprint(show, "text/plain", a) ==
1160+
"$(summary(a)):\n[:, :, 1] =\n $(zero(eltype(a))) $(eltype(a)(121)) ⋅ ⋅ \n $(zero(eltype(a))) $(zero(eltype(a))) ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n\n[:, :, 2] =\n $(zero(eltype(a))) $(zero(eltype(a))) ⋅ ⋅ \n $(zero(eltype(a))) $(zero(eltype(a))) ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n\n[:, :, 3] =\n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n\n[:, :, 4] =\n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ \n ⋅ ⋅ ⋅ ⋅ "
11551161
end
11561162
end
11571163
@testset "TypeParameterAccessors.position" begin

0 commit comments

Comments
 (0)