Skip to content

Fix isstored with trailing singletons, vector show #45

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 3 commits into from
Mar 8, 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SparseArraysBase"
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.4.0"
version = "0.4.1"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
4 changes: 2 additions & 2 deletions src/abstractsparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ using LinearAlgebra: LinearAlgebra
@derive AnyAbstractSparseArray AbstractArrayOps

function Base.replace_in_print_matrix(
A::AnyAbstractSparseArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString
a::AnyAbstractSparseVecOrMat, i::Integer, j::Integer, s::AbstractString
)
return isstored(A, CartesianIndex(i, j)) ? s : Base.replace_with_centered_mark(s)
return isstored(a, i, j) ? s : Base.replace_with_centered_mark(s)
end

# Special-purpose constructors
Expand Down
25 changes: 24 additions & 1 deletion src/abstractsparsearrayinterface.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Base: @_propagate_inbounds_meta
using DerivableInterfaces: DerivableInterfaces, @derive, @interface, AbstractArrayInterface

# This is to bring `ArrayLayouts.zero!` into the namespace
Expand Down Expand Up @@ -34,7 +35,23 @@ end

# Minimal interface for `SparseArrayInterface`.
# Fallbacks for dense/non-sparse arrays.
@interface ::AbstractArrayInterface isstored(a::AbstractArray, I::Int...) = true
@interface ::AbstractArrayInterface function isstored(
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
) where {N}
@_propagate_inbounds_meta
@boundscheck checkbounds(a, I...)
return true
end
@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int)
@_propagate_inbounds_meta
return @interface interface isstored(a, Tuple(CartesianIndices(a)[I])...)
end
@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int...)
@_propagate_inbounds_meta
@boundscheck checkbounds(a, I...)
I′ = ntuple(i -> I[i], ndims(a))
return @inbounds @interface interface isstored(a, I′...)
end
@interface ::AbstractArrayInterface eachstoredindex(a::AbstractArray) = eachindex(a)
@interface ::AbstractArrayInterface getstoredindex(a::AbstractArray, I::Int...) =
getindex(a, I...)
Expand Down Expand Up @@ -153,6 +170,12 @@ function Base.setindex!(a::StoredValues, value, I::Int)
return setstoredindex!(a.array, value, a.storedindices[I])
end

@interface ::AbstractSparseArrayInterface function isstored(
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
) where {N}
return CartesianIndex(I) in eachstoredindex(a)
end

@interface ::AbstractSparseArrayInterface storedvalues(a::AbstractArray) = StoredValues(a)

@interface ::AbstractSparseArrayInterface function eachstoredindex(
Expand Down
3 changes: 2 additions & 1 deletion src/sparsearraydok.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Accessors: @set
using DerivableInterfaces: @interface, interface
using Dictionaries: Dictionary, IndexError, set!

function getzero(a::AbstractArray{<:Any,N}, I::Vararg{Int,N}) where {N}
Expand Down Expand Up @@ -74,7 +75,7 @@ Base.size(a::SparseArrayDOK) = a.size

storedvalues(a::SparseArrayDOK) = values(storage(a))
function isstored(a::SparseArrayDOK, I::Int...)
return CartesianIndex(I) in keys(storage(a))
return @interface interface(a) isstored(a, I...)
end
function eachstoredindex(a::SparseArrayDOK)
return keys(storage(a))
Expand Down
3 changes: 3 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ arrayts = (Array, JLArray)
for indexstyle in (IndexLinear(), IndexCartesian())
for I in eachindex(indexstyle, a)
@test isstored(a, I)
if indexstyle == IndexCartesian()
@test isstored(a, Tuple(I)..., 1)
end
end
end
@test eachstoredindex(a) == eachindex(a)
Expand Down
4 changes: 4 additions & 0 deletions test/test_sparsearraydok.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ arrayts = (Array,)
a[1, 2] = 12
@test sprint(show, "text/plain", a) ==
"$(summary(a)):\n ⋅ $(eltype(a)(12))\n ⋅ ⋅ "

a = SparseArrayDOK{elt}(undef, 2)
a[1] = 1
@test sprint(show, "text/plain", a) == "$(summary(a)):\n $(eltype(a)(1))\n ⋅ "
end

# Regression test for:
Expand Down
Loading