From 5e46525b23fd86f4037e693287e905d17d982b7f Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 7 Mar 2025 17:43:19 -0500 Subject: [PATCH 1/7] [WIP] Fix vector show --- .../wrappedabstractblocksparsearray.jl | 10 ++++++++-- src/blocksparsearray/blocksparsearray.jl | 6 ++++++ test/test_basics.jl | 9 +++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index 1f1a8619..dae32d4f 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -23,6 +23,12 @@ const AnyAbstractBlockSparseArray{T,N} = Union{ <:AbstractBlockSparseArray{T,N},<:WrappedAbstractBlockSparseArray{T,N} } +const AnyAbstractBlockSparseVector{T} = AnyAbstractBlockSparseArray{T,1} +const AnyAbstractBlockSparseMatrix{T} = AnyAbstractBlockSparseArray{T,2} +const AnyAbstractBlockSparseVecOrMat{T,N} = Union{ + AnyAbstractBlockSparseVector{T},AnyAbstractBlockSparseMatrix{T} +} + function DerivableInterfaces.interface(::Type{<:AnyAbstractBlockSparseArray}) return BlockSparseArrayInterface() end @@ -355,9 +361,9 @@ function SparseArraysBase.isstored( end function Base.replace_in_print_matrix( - A::AnyAbstractBlockSparseArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString + a::AnyAbstractBlockSparseVecOrMat, i::Integer, j::Integer, s::AbstractString ) - return isstored(A, i, j) ? s : Base.replace_with_centered_mark(s) + return isstored(a, i, j) ? s : Base.replace_with_centered_mark(s) end # attempt to catch things that wrap GPU arrays diff --git a/src/blocksparsearray/blocksparsearray.jl b/src/blocksparsearray/blocksparsearray.jl index ea762ea1..c4d05c0e 100644 --- a/src/blocksparsearray/blocksparsearray.jl +++ b/src/blocksparsearray/blocksparsearray.jl @@ -174,6 +174,12 @@ function BlockSparseArray{T,N}( return BlockSparseArray{T,N,Array{T,N}}(undef, axes) end +function BlockSparseArray{T,N}( + ::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}} +) where {T,N} + return throw(ArgumentError("Length of axes doesn't match number of dimensions.")) +end + function BlockSparseArray{T,N}( ::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer},N} ) where {T,N} diff --git a/test/test_basics.jl b/test/test_basics.jl index 144128c3..06263fed 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -130,6 +130,15 @@ arrayts = (Array, JLArray) @test iszero(storedlength(a)) end end + + for dims in ( + ([2, 2], [2, 2]), + (([2, 2], [2, 2]),), + blockedrange.(([2, 2], [2, 2])), + (blockedrange.(([2, 2], [2, 2])),), + ) + @test_throws ArgumentError BlockSparseVector{elt}(undef, dims...) + end end @testset "blockstype, blocktype" begin a = arrayt(randn(elt, 2, 2)) From a0e61c47e7cc9cef0307565ff159964058588bfb Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sat, 8 Mar 2025 09:21:19 -0500 Subject: [PATCH 2/7] Small refactor --- .../wrappedabstractblocksparsearray.jl | 8 +++----- .../blocksparsearrayinterface.jl | 7 +++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index dae32d4f..be2e901f 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -344,11 +344,9 @@ function Base.Array(a::AnyAbstractBlockSparseArray) end function SparseArraysBase.isstored( - a::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N} -) where {N} - bI = BlockIndex(findblockindex.(axes(a), I)) - blocks_a = blocks(a) - return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...) + a::AnyAbstractBlockSparseArray, I::Int... +) + return @interface interface(a) isstored(a, I...) end # This circumvents issues passing certain kinds of SubArrays diff --git a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl index 6f4200aa..416f8aba 100644 --- a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl +++ b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl @@ -99,6 +99,13 @@ struct BlockSparseArrayInterface <: AbstractBlockSparseArrayInterface end @interface ::AbstractBlockSparseArrayInterface BlockArrays.blocks(a::AbstractArray) = error("Not implemented") +@interface ::AbstractBlockSparseArrayInterface function SparseArraysBase.isstored( + a::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N} + ) where {N} + bI = BlockIndex(findblockindex.(axes(a), I)) + return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...) + end + @interface ::AbstractBlockSparseArrayInterface function Base.getindex( a::AbstractArray{<:Any,N}, I::Vararg{Int,N} ) where {N} From 7564dc725e8a4e54a4a8ff94daffaea1761f8d7b Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sat, 8 Mar 2025 09:25:07 -0500 Subject: [PATCH 3/7] Generalize definition --- src/blocksparsearrayinterface/blocksparsearrayinterface.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl index 416f8aba..d37a8f63 100644 --- a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl +++ b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl @@ -100,7 +100,7 @@ struct BlockSparseArrayInterface <: AbstractBlockSparseArrayInterface end error("Not implemented") @interface ::AbstractBlockSparseArrayInterface function SparseArraysBase.isstored( - a::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N} + a::AbstractArray{<:Any,N}, I::Vararg{Int,N} ) where {N} bI = BlockIndex(findblockindex.(axes(a), I)) return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...) From 484722f003e0d3edef97904f295ecd377792e76c Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sat, 8 Mar 2025 09:26:54 -0500 Subject: [PATCH 4/7] Format --- .../wrappedabstractblocksparsearray.jl | 4 +--- .../blocksparsearrayinterface.jl | 10 +++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index be2e901f..fefd77f0 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -343,9 +343,7 @@ function Base.Array(a::AnyAbstractBlockSparseArray) return Array{eltype(a)}(a) end -function SparseArraysBase.isstored( - a::AnyAbstractBlockSparseArray, I::Int... -) +function SparseArraysBase.isstored(a::AnyAbstractBlockSparseArray, I::Int...) return @interface interface(a) isstored(a, I...) end diff --git a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl index d37a8f63..8ce1375f 100644 --- a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl +++ b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl @@ -100,11 +100,11 @@ struct BlockSparseArrayInterface <: AbstractBlockSparseArrayInterface end error("Not implemented") @interface ::AbstractBlockSparseArrayInterface function SparseArraysBase.isstored( - a::AbstractArray{<:Any,N}, I::Vararg{Int,N} - ) where {N} - bI = BlockIndex(findblockindex.(axes(a), I)) - return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...) - end + a::AbstractArray{<:Any,N}, I::Vararg{Int,N} +) where {N} + bI = BlockIndex(findblockindex.(axes(a), I)) + return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...) +end @interface ::AbstractBlockSparseArrayInterface function Base.getindex( a::AbstractArray{<:Any,N}, I::Vararg{Int,N} From d607e9bf1d744326cc7ae5869326e6c82169924d Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sat, 8 Mar 2025 10:32:32 -0500 Subject: [PATCH 5/7] Small fix --- src/blocksparsearrayinterface/blocksparsearrayinterface.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl index 8ce1375f..f4e09008 100644 --- a/src/blocksparsearrayinterface/blocksparsearrayinterface.jl +++ b/src/blocksparsearrayinterface/blocksparsearrayinterface.jl @@ -103,7 +103,7 @@ struct BlockSparseArrayInterface <: AbstractBlockSparseArrayInterface end a::AbstractArray{<:Any,N}, I::Vararg{Int,N} ) where {N} bI = BlockIndex(findblockindex.(axes(a), I)) - return isstored(blocks_a, bI.I...) && isstored(blocks_a[bI.I...], bI.α...) + return isstored(blocks(a), bI.I...) && isstored(blocks(a)[bI.I...], bI.α...) end @interface ::AbstractBlockSparseArrayInterface function Base.getindex( From 0c12a638157f6954fcf5e6b1ccd3cf42d0c43d55 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Sun, 9 Mar 2025 14:54:10 -0400 Subject: [PATCH 6/7] Fix tests --- .../wrappedabstractblocksparsearray.jl | 11 ++--------- test/test_basics.jl | 9 +++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index c9e11318..29d38e51 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -343,17 +343,10 @@ function Base.Array(a::AnyAbstractBlockSparseArray) return Array{eltype(a)}(a) end -function SparseArraysBase.isstored(a::AnyAbstractBlockSparseArray, I::Int...) - return @interface interface(a) isstored(a, I...) -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( - a::SubArray{<:Any,N,<:AbstractBlockSparseArray}, I::Vararg{Int,N} + a::AbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N} ) where {N} - return @interface DefaultArrayInterface() isstored(a, I...) + return @interface interface(a) isstored(a, I...) end function Base.replace_in_print_matrix( diff --git a/test/test_basics.jl b/test/test_basics.jl index 353a2e75..17ef5db1 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -1179,6 +1179,15 @@ arrayts = (Array, JLArray) # Not testing other element types since they change the # spacing so it isn't easy to make the test general. + a′ = BlockSparseVector{elt,arrayt{elt,1}}(undef, [2, 2]) + @allowscalar a′[1] = 1 + a = a′ + @test sprint(show, "text/plain", a) == + "$(summary(a)):\n $(eltype(a)(1))\n $(zero(eltype(a)))\n ───\n ⋅ \n ⋅ " + a = @view a′[:] + @test sprint(show, "text/plain", a) == + "$(summary(a)):\n $(eltype(a)(1))\n $(zero(eltype(a)))\n ⋅ \n ⋅ " + a′ = BlockSparseMatrix{elt,arrayt{elt,2}}(undef, [2, 2], [2, 2]) @allowscalar a′[1, 2] = 12 for a in (a′, @view(a′[:, :])) From 5b328182b2881bf5c43ef42b3d2b541c70a81baa Mon Sep 17 00:00:00 2001 From: mtfishman Date: Mon, 10 Mar 2025 10:07:06 -0400 Subject: [PATCH 7/7] Bump versions --- Project.toml | 4 ++-- test/Project.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 8bd54ba6..81051c5b 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.4" +version = "0.3.6" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" @@ -43,7 +43,7 @@ LabelledNumbers = "0.1.0" LinearAlgebra = "1.10" MacroTools = "0.5.13" MapBroadcast = "0.1.5" -SparseArraysBase = "0.4" +SparseArraysBase = "0.5" SplitApplyCombine = "1.2.3" TensorAlgebra = "0.1.0, 0.2" Test = "1.10" diff --git a/test/Project.toml b/test/Project.toml index ed10b4ae..7cc17c05 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -36,7 +36,7 @@ LinearAlgebra = "1" Pkg = "1" Random = "1" SafeTestsets = "0.1" -SparseArraysBase = "0.4" +SparseArraysBase = "0.5" Suppressor = "0.2" SymmetrySectors = "0.1" TensorAlgebra = "0.2"