Skip to content

Commit 1f8cb95

Browse files
committed
Fix show for higher dimensional block sparse arrays
1 parent 2b4ab96 commit 1f8cb95

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,22 @@ 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+
# such as `blocks(a)`.
351+
# TODO: Fix those issues and delete this in favor of using the generic
352+
# version.
353+
function SparseArraysBase.isstored(
354+
a::SubArray{<:Any,N,<:AbstractBlockSparseArray}, I::Vararg{Int,N}
355+
) where {N}
356+
return isstored(parent(a), Base.reindex(parentindices(a), I)...)
346357
end
347358

348359
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)