From eb0407358e349e3a5758cfeb0a52a2675312bbd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 27 May 2024 17:20:05 +0200 Subject: [PATCH 1/3] Implement zero for SparseVector --- src/implementations/SparseArrays.jl | 6 ++++++ test/SparseArrays.jl | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/implementations/SparseArrays.jl b/src/implementations/SparseArrays.jl index c6988569..bea621bd 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 8bcf15cc..a455445f 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() From fccdf2ced51815b26870c1c70c7e3c0c089a7097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 27 May 2024 17:38:40 +0200 Subject: [PATCH 2/3] Update src/implementations/SparseArrays.jl --- src/implementations/SparseArrays.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/implementations/SparseArrays.jl b/src/implementations/SparseArrays.jl index bea621bd..254c5571 100644 --- a/src/implementations/SparseArrays.jl +++ b/src/implementations/SparseArrays.jl @@ -17,7 +17,7 @@ function undef_array( return SparseArrays.spzeros(Tv, Ti, length(rows), length(cols)) end -function operate!(::typeof(zero), v::SparseArrays.SparseVec) +function operate!(::typeof(zero), v::SparseArrays.SparseVector) empty!(v.nzind) empty!(v.nzval) return v From 2c10afd14a14f722f31f7edb8eafdeb9ebc7dc0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 28 May 2024 07:41:41 +0200 Subject: [PATCH 3/3] Update test/SparseArrays.jl Co-authored-by: Oscar Dowson --- test/SparseArrays.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/SparseArrays.jl b/test/SparseArrays.jl index a455445f..02dfa17f 100644 --- a/test/SparseArrays.jl +++ b/test/SparseArrays.jl @@ -132,7 +132,7 @@ function test_zero() MA.operate!(zero, v) @test isempty(SparseArrays.nonzeroinds(v)) @test isempty(SparseArrays.nonzeros(v)) - A = SparseArrays.sprand(Float64, 2, 2, 0.5)' + A = SparseArrays.sprand(Float64, 2, 2, 0.5) MA.operate!(zero, A) @test iszero(SparseArrays.nnz(A)) @test isempty(SparseArrays.nonzeros(A))