Skip to content

Commit

Permalink
Fill, zero
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Dec 12, 2024
1 parent 1b9dfe8 commit e38b9ce
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ using SparseArraysBase:
setunstoredindex!,
storedlength,
storedpairs,
storedvalues
storedvalues,
zero!
using Test: @test, @test_throws

a = SparseArrayDOK{Float64}(2, 2)
Expand Down Expand Up @@ -120,6 +121,37 @@ b = a[1:2, 2]
@test b isa SparseVectorDOK{Float64}
@test b == [12, 0]
@test storedlength(b) == 1

a = SparseArrayDOK{Float64}(2, 2)
a .= 2
for I in eachindex(a)
@test a[I] == 2
end
@test storedlength(a) == length(a)

a = SparseArrayDOK{Float64}(2, 2)
fill!(a, 2)
for I in eachindex(a)
@test a[I] == 2
end
@test storedlength(a) == length(a)

a = SparseArrayDOK{Float64}(2, 2)
fill!(a, 0)
@test iszero(a)
@test iszero(storedlength(a))

a = SparseArrayDOK{Float64}(2, 2)
a[1, 2] = 12
zero!(a)
@test iszero(a)
@test iszero(storedlength(a))

a = SparseArrayDOK{Float64}(2, 2)
a[1, 2] = 12
b = zero(a)
@test iszero(b)
@test iszero(storedlength(b))
````

---
Expand Down
34 changes: 33 additions & 1 deletion examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ using SparseArraysBase:
setunstoredindex!,
storedlength,
storedpairs,
storedvalues
storedvalues,
zero!
using Test: @test, @test_throws

a = SparseArrayDOK{Float64}(2, 2)
Expand Down Expand Up @@ -116,3 +117,34 @@ b = a[1:2, 2]
@test b isa SparseVectorDOK{Float64}
@test b == [12, 0]
@test storedlength(b) == 1

a = SparseArrayDOK{Float64}(2, 2)
a .= 2
for I in eachindex(a)
@test a[I] == 2
end
@test storedlength(a) == length(a)

a = SparseArrayDOK{Float64}(2, 2)
fill!(a, 2)
for I in eachindex(a)
@test a[I] == 2
end
@test storedlength(a) == length(a)

a = SparseArrayDOK{Float64}(2, 2)
fill!(a, 0)
@test iszero(a)
@test iszero(storedlength(a))

a = SparseArrayDOK{Float64}(2, 2)
a[1, 2] = 12
zero!(a)
@test iszero(a)
@test iszero(storedlength(a))

a = SparseArrayDOK{Float64}(2, 2)
a[1, 2] = 12
b = zero(a)
@test iszero(b)
@test iszero(storedlength(b))
31 changes: 26 additions & 5 deletions src/abstractsparsearrayinterface.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# This is to bring `ArrayLayouts.zero!` into the namespace
# since it is considered part of the sparse array interface.
using ArrayLayouts: zero!

# Minimal interface for `SparseArrayInterface`.
isstored(a::AbstractArray, I::Int...) = true
eachstoredindex(a::AbstractArray) = eachindex(a)
Expand Down Expand Up @@ -111,15 +115,31 @@ end
end

@interface ::AbstractSparseArrayInterface function Base.map!(
f, dest::AbstractArray, as::AbstractArray...
f, a_dest::AbstractArray, as::AbstractArray...
)
# Check `f` preserves zeros.
# Define as `map_stored!`.
# TODO: Define a function `preserves_unstored(a_dest, f, as...)`
# to determine if a function preserves the stored values
# of the destination sparse array.
# The current code may be inefficient since it actually
# accesses an unstored element, which in the case of a
# sparse array of arrays can allocate an array.
# Sparse arrays could be expected to define a cheap
# unstored element allocator, for example
# `get_prototypical_unstored(a::AbstractArray)`.
I = first(eachindex(as...))
preserves_unstored = iszero(f(map(a -> getunstoredindex(a, I), as)...))
if !preserves_unstored
# Doesn't preserve unstored values, loop over all elements.
for I in eachindex(as...)
a_dest[I] = map(f, map(a -> a[I], as)...)
end
return a_dest
end
# Define `eachstoredindex` promotion.
for I in eachstoredindex(as...)
dest[I] = f(map(a -> a[I], as)...)
a_dest[I] = f(map(a -> a[I], as)...)
end
return dest
return a_dest
end

# `f::typeof(norm)`, `op::typeof(max)` used by `norm`.
Expand Down Expand Up @@ -175,6 +195,7 @@ function mul_indices(I1::CartesianIndex{2}, I2::CartesianIndex{2})
return CartesianIndex(I1[1], I2[2])
end

using LinearAlgebra: mul!
function default_mul!!(

Check warning on line 199 in src/abstractsparsearrayinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearrayinterface.jl#L199

Added line #L199 was not covered by tests
a_dest::AbstractMatrix,
a1::AbstractMatrix,
Expand Down
5 changes: 5 additions & 0 deletions src/sparsearraydok.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ end

# Optional, but faster than the default.
storedpairs(a::SparseArrayDOK) = pairs(storage(a))

function ArrayLayouts.zero!(a::SparseArrayDOK)
empty!(storage(a))
return a
end

0 comments on commit e38b9ce

Please sign in to comment.