Skip to content

Commit 006efd4

Browse files
blegatodow
andauthored
Implement zero for SparseVector (#290)
* Implement zero for SparseVector * Update src/implementations/SparseArrays.jl * Update test/SparseArrays.jl Co-authored-by: Oscar Dowson <[email protected]> --------- Co-authored-by: Oscar Dowson <[email protected]>
1 parent 7344c94 commit 006efd4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/implementations/SparseArrays.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ function undef_array(
1717
return SparseArrays.spzeros(Tv, Ti, length(rows), length(cols))
1818
end
1919

20+
function operate!(::typeof(zero), v::SparseArrays.SparseVector)
21+
empty!(v.nzind)
22+
empty!(v.nzval)
23+
return v
24+
end
25+
2026
function operate!(::typeof(zero), A::_SparseMat)
2127
for i in eachindex(A.colptr)
2228
A.colptr[i] = one(A.colptr[i])

test/SparseArrays.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ function test_sparse_broadcast()
127127
return
128128
end
129129

130+
function test_zero()
131+
v = SparseArrays.sparsevec([1, 2], [3.0, 4.0])
132+
MA.operate!(zero, v)
133+
@test isempty(SparseArrays.nonzeroinds(v))
134+
@test isempty(SparseArrays.nonzeros(v))
135+
A = SparseArrays.sprand(Float64, 2, 2, 0.5)
136+
MA.operate!(zero, A)
137+
@test iszero(SparseArrays.nnz(A))
138+
@test isempty(SparseArrays.nonzeros(A))
139+
return
140+
end
141+
130142
end # module
131143

132144
TestInterfaceSparseArrays.runtests()

0 commit comments

Comments
 (0)