From 1f8cb953ce66a43b7dd31302e9cda0b646e49e7f Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 5 Mar 2025 13:42:00 -0500 Subject: [PATCH 1/4] Fix show for higher dimensional block sparse arrays --- .../wrappedabstractblocksparsearray.jl | 19 +++++++++++++++---- test/test_basics.jl | 6 ++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index 39225d1a..a5bf0296 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -338,11 +338,22 @@ function Base.Array(a::AnyAbstractBlockSparseArray) 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.α...) +end + +# This circumvents issues passing certain kinds of SubArrays +# to the more generic block sparse `isstored` definition, +# such as `blocks(a)`. +# TODO: Fix those issues and delete this in favor of using the generic +# version. +function SparseArraysBase.isstored( + a::SubArray{<:Any,N,<:AbstractBlockSparseArray}, I::Vararg{Int,N} +) where {N} + return isstored(parent(a), Base.reindex(parentindices(a), I)...) end function Base.replace_in_print_matrix( diff --git a/test/test_basics.jl b/test/test_basics.jl index e39e2667..144128c3 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -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 From 54b3b6c59799d9c6b1c9d0caad03745c2130602e Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 5 Mar 2025 13:42:30 -0500 Subject: [PATCH 2/4] Bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 0a2cd552..36c7edd2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.3.0" +version = "0.3.1" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" From f88fa981b426674eef64debc6c369dd9ad09bf25 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Wed, 5 Mar 2025 13:45:54 -0500 Subject: [PATCH 3/4] Update comment --- src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index a5bf0296..7df00699 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -347,7 +347,7 @@ end # This circumvents issues passing certain kinds of SubArrays # to the more generic block sparse `isstored` definition, -# such as `blocks(a)`. +# for example `blocks(a)` is broken for certain slices. # TODO: Fix those issues and delete this in favor of using the generic # version. function SparseArraysBase.isstored( From 46aaa7ba6bd630b261d558f5717fc3fa8c00dc8e Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 5 Mar 2025 15:05:59 -0500 Subject: [PATCH 4/4] Use SparseArraysBase version of isstored --- Project.toml | 2 +- .../wrappedabstractblocksparsearray.jl | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 36c7edd2..a550e68e 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index 7df00699..1f1a8619 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -9,7 +9,7 @@ using BlockArrays: blockedrange, mortar, unblock -using DerivableInterfaces: DerivableInterfaces, @interface +using DerivableInterfaces: DerivableInterfaces, @interface, DefaultArrayInterface using GPUArraysCore: @allowscalar using SplitApplyCombine: groupcount using TypeParameterAccessors: similartype @@ -348,12 +348,10 @@ 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. -# TODO: Fix those issues and delete this in favor of using the generic -# version. function SparseArraysBase.isstored( a::SubArray{<:Any,N,<:AbstractBlockSparseArray}, I::Vararg{Int,N} ) where {N} - return isstored(parent(a), Base.reindex(parentindices(a), I)...) + return @interface DefaultArrayInterface() isstored(a, I...) end function Base.replace_in_print_matrix(