diff --git a/src/implementations/SparseArrays.jl b/src/implementations/SparseArrays.jl index c698856..bea621b 100644 --- a/src/implementations/SparseArrays.jl +++ b/src/implementations/SparseArrays.jl @@ -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]) diff --git a/test/SparseArrays.jl b/test/SparseArrays.jl index 8bcf15c..a455445 100644 --- a/test/SparseArrays.jl +++ b/test/SparseArrays.jl @@ -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()