Skip to content

Fix printing higher dimensional block sparse arrays #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.3.0"
version = "0.3.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down Expand Up @@ -43,7 +43,7 @@ LabelledNumbers = "0.1.0"
LinearAlgebra = "1.10"
MacroTools = "0.5.13"
MapBroadcast = "0.1.5"
SparseArraysBase = "0.3"
SparseArraysBase = "0.3.2"
SplitApplyCombine = "1.2.3"
TensorAlgebra = "0.1.0, 0.2"
Test = "1.10"
Expand Down
19 changes: 14 additions & 5 deletions src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
blockedrange,
mortar,
unblock
using DerivableInterfaces: DerivableInterfaces, @interface
using DerivableInterfaces: DerivableInterfaces, @interface, DefaultArrayInterface
using GPUArraysCore: @allowscalar
using SplitApplyCombine: groupcount
using TypeParameterAccessors: similartype
Expand Down Expand Up @@ -338,11 +338,20 @@
end

function SparseArraysBase.isstored(
A::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}
a::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}
) where {N}
bI = BlockIndex(findblockindex.(axes(A), I))
bA = blocks(A)
return isstored(bA, bI.I...) && isstored(bA[bI.I...], bI.α...)
bI = BlockIndex(findblockindex.(axes(a), I))
blocks_a = blocks(a)
return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...)

Check warning on line 345 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L343-L345

Added lines #L343 - L345 were not covered by tests
end

# This circumvents issues passing certain kinds of SubArrays
# to the more generic block sparse `isstored` definition,
# for example `blocks(a)` is broken for certain slices.
function SparseArraysBase.isstored(

Check warning on line 351 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L351

Added line #L351 was not covered by tests
a::SubArray{<:Any,N,<:AbstractBlockSparseArray}, I::Vararg{Int,N}
) where {N}
return @interface DefaultArrayInterface() isstored(a, I...)

Check warning on line 354 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L354

Added line #L354 was not covered by tests
end

function Base.replace_in_print_matrix(
Expand Down
6 changes: 6 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1148,10 +1148,16 @@ arrayts = (Array, JLArray)
if elt === Float64
# Not testing other element types since they change the
# spacing so it isn't easy to make the test general.

a = BlockSparseMatrix{elt,arrayt{elt,2}}(undef, [2, 2], [2, 2])
@allowscalar a[1, 2] = 12
@test sprint(show, "text/plain", a) ==
"$(summary(a)):\n $(zero(eltype(a))) $(eltype(a)(12)) │ ⋅ ⋅ \n $(zero(eltype(a))) $(zero(eltype(a))) │ ⋅ ⋅ \n ───────────┼──────────\n ⋅ ⋅ │ ⋅ ⋅ \n ⋅ ⋅ │ ⋅ ⋅ "

a = BlockSparseArray{elt,3,arrayt{elt,3}}(undef, [2, 2], [2, 2], [2, 2])
@allowscalar a[1, 2, 1] = 121
@test sprint(show, "text/plain", a) ==
"$(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 ⋅ ⋅ ⋅ ⋅ "
end
end
@testset "TypeParameterAccessors.position" begin
Expand Down
Loading