From 109a507f9c9e8acbe951e13169b8a02cee89dc0b Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 5 Mar 2025 14:47:47 -0500 Subject: [PATCH 1/3] Define isstored for SubArray --- Project.toml | 2 +- src/abstractsparsearrayinterface.jl | 4 ++++ test/test_basics.jl | 11 ++++++++++- test/test_sparsearraydok.jl | 27 +++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 8a0ae2a..e533e14 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SparseArraysBase" uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208" authors = ["ITensor developers and contributors"] -version = "0.3.1" +version = "0.3.2" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" diff --git a/src/abstractsparsearrayinterface.jl b/src/abstractsparsearrayinterface.jl index e9d6c40..eb54bb4 100644 --- a/src/abstractsparsearrayinterface.jl +++ b/src/abstractsparsearrayinterface.jl @@ -102,6 +102,10 @@ end SparseArraysBase.storedvalues(::T) end +function isstored(a::SubArray, I::Int...) + return isstored(parent(a), Base.reindex(parentindices(a), I)...) +end + # TODO: Add `ndims` type parameter, like `Base.Broadcast.AbstractArrayStyle`. # TODO: This isn't used to define interface functions right now. # Currently, `@interface` expects an instance, probably it should take a diff --git a/test/test_basics.jl b/test/test_basics.jl index d968acd..d434504 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -11,7 +11,7 @@ using SparseArraysBase: storedlength, storedpairs, storedvalues -using Test: @test, @testset +using Test: @test, @test_throws, @testset elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) arrayts = (Array, JLArray) @@ -43,6 +43,15 @@ arrayts = (Array, JLArray) @test iszero(getunstoredindex(a, I)) end + n = 2 + a = @view dev(randn(elt, n, n))[1:2, 1] + @test storedlength(a) == length(a) + for indexstyle in (IndexLinear(), IndexCartesian()) + for I in eachindex(indexstyle, a) + @test isstored(a, I) + end + end + a = dev(randn(elt, n, n)) for I in ((1, 2), (CartesianIndex(1, 2),)) b = copy(a) diff --git a/test/test_sparsearraydok.jl b/test/test_sparsearraydok.jl index f32cadd..8409958 100644 --- a/test/test_sparsearraydok.jl +++ b/test/test_sparsearraydok.jl @@ -50,6 +50,33 @@ arrayts = (Array,) @test b == [0 24; 0 0] @test storedlength(b) == 1 + # isstored + a = SparseArrayDOK{elt}(undef, 4, 4) + a[2, 3] = 23 + for I in CartesianIndices(a) + if I == CartesianIndex(2, 3) + @test isstored(a, I) + @test isstored(a, Tuple(I)...) + else + @test !isstored(a, I) + @test !isstored(a, Tuple(I)...) + end + end + + # isstored SubArray + a′ = SparseArrayDOK{elt}(undef, 4, 4) + a′[2, 3] = 23 + a = @view a′[2:3, 2:3] + for I in CartesianIndices(a) + if I == CartesianIndex(1, 2) + @test isstored(a, I) + @test isstored(a, Tuple(I)...) + else + @test !isstored(a, I) + @test !isstored(a, Tuple(I)...) + end + end + a = SparseArrayDOK{elt}(undef, 3, 3, 3) a[1, 2, 3] = 123 b = permutedims(a, (2, 3, 1)) From 81df0abbaccf77c5f803df21a5dc35b926e41dac Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 5 Mar 2025 15:05:16 -0500 Subject: [PATCH 2/3] Turn into an interface function --- src/abstractsparsearrayinterface.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/abstractsparsearrayinterface.jl b/src/abstractsparsearrayinterface.jl index eb54bb4..5ba7763 100644 --- a/src/abstractsparsearrayinterface.jl +++ b/src/abstractsparsearrayinterface.jl @@ -54,6 +54,10 @@ end return error("Not implemented.") end +@interface ::AbstractArrayInterface function isstored(a::SubArray, I::Int...) + return isstored(parent(a), Base.reindex(parentindices(a), I)...) +end + # TODO: Use `Base.to_indices`? isstored(a::AbstractArray, I::CartesianIndex) = isstored(a, Tuple(I)...) # TODO: Use `Base.to_indices`? @@ -102,8 +106,8 @@ end SparseArraysBase.storedvalues(::T) end -function isstored(a::SubArray, I::Int...) - return isstored(parent(a), Base.reindex(parentindices(a), I)...) +@derive (T=SubArray,) begin + SparseArraysBase.isstored(::T, ::Int...) end # TODO: Add `ndims` type parameter, like `Base.Broadcast.AbstractArrayStyle`. From 5d4579719086479d68361ea2703b71837e7ad3c3 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 5 Mar 2025 18:40:05 -0500 Subject: [PATCH 3/3] Better definition --- src/abstractsparsearrayinterface.jl | 8 -------- src/wrappers.jl | 4 ++++ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/abstractsparsearrayinterface.jl b/src/abstractsparsearrayinterface.jl index 5ba7763..e9d6c40 100644 --- a/src/abstractsparsearrayinterface.jl +++ b/src/abstractsparsearrayinterface.jl @@ -54,10 +54,6 @@ end return error("Not implemented.") end -@interface ::AbstractArrayInterface function isstored(a::SubArray, I::Int...) - return isstored(parent(a), Base.reindex(parentindices(a), I)...) -end - # TODO: Use `Base.to_indices`? isstored(a::AbstractArray, I::CartesianIndex) = isstored(a, Tuple(I)...) # TODO: Use `Base.to_indices`? @@ -106,10 +102,6 @@ end SparseArraysBase.storedvalues(::T) end -@derive (T=SubArray,) begin - SparseArraysBase.isstored(::T, ::Int...) -end - # TODO: Add `ndims` type parameter, like `Base.Broadcast.AbstractArrayStyle`. # TODO: This isn't used to define interface functions right now. # Currently, `@interface` expects an instance, probably it should take a diff --git a/src/wrappers.jl b/src/wrappers.jl index bf4fc78..f35b1a1 100644 --- a/src/wrappers.jl +++ b/src/wrappers.jl @@ -106,6 +106,10 @@ function storedparentvalues(a::SubArray) return StoredValues(parent(a), collect(eachstoredparentindex(a))) end +@interface ::AbstractArrayInterface function isstored(a::SubArray, I::Int...) + return isstored(parent(a), index_to_parentindex(a, I...)...) +end + using LinearAlgebra: Transpose function parentindex_to_index(a::Transpose, I::CartesianIndex{2}) return cartesianindex_reverse(I)