Skip to content

Commit

Permalink
Implement zero for SparseVector
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed May 27, 2024
1 parent 7344c94 commit eb04073
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/implementations/SparseArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ function undef_array(
return SparseArrays.spzeros(Tv, Ti, length(rows), length(cols))
end

function operate!(::typeof(zero), v::SparseArrays.SparseVec)
empty!(v.nzind)
empty!(v.nzval)
return v
end

function operate!(::typeof(zero), A::_SparseMat)
for i in eachindex(A.colptr)
A.colptr[i] = one(A.colptr[i])
Expand Down
12 changes: 12 additions & 0 deletions test/SparseArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ function test_sparse_broadcast()
return
end

function test_zero()
v = SparseArrays.sparsevec([1, 2], [3.0, 4.0])
MA.operate!(zero, v)
@test isempty(SparseArrays.nonzeroinds(v))
@test isempty(SparseArrays.nonzeros(v))
A = SparseArrays.sprand(Float64, 2, 2, 0.5)'
MA.operate!(zero, A)
@test iszero(SparseArrays.nnz(A))
@test isempty(SparseArrays.nonzeros(A))
return
end

end # module

TestInterfaceSparseArrays.runtests()

0 comments on commit eb04073

Please sign in to comment.