diff --git a/src/Sparse/Row.jl b/src/Sparse/Row.jl index 26756935e0..e0fbdfa49a 100644 --- a/src/Sparse/Row.jl +++ b/src/Sparse/Row.jl @@ -425,8 +425,15 @@ function scale_row!(a::SRow{T}, b::T) where T if isone(b) return end - for i=1:length(a.pos) + i = 1 + while i <= length(a) a.values[i] *= b + if iszero(a.values[i]) + deleteat!(a.values, i) + deleteat!(a.pos, i) + else + i += 1 + end end end diff --git a/test/Sparse/Row.jl b/test/Sparse/Row.jl index d20983ecbb..86c7100c17 100644 --- a/test/Sparse/Row.jl +++ b/test/Sparse/Row.jl @@ -1,5 +1,6 @@ @testset "Row" begin R = FlintZZ + S, _ = residue_ring(ZZ, 4) # Construction @@ -82,7 +83,10 @@ A = sparse_row(FlintZZ, [1, 2, 4], ZZRingElem[1, 2, 3]) scale_row!(A, ZZRingElem(2)) + B = sparse_row(S, [1, 3, 4], [1, 2, 3]) + scale_row!(B, S(2)) @test A == sparse_row(FlintZZ, [1, 2, 4], ZZRingElem[2, 4, 6]) + @test B == sparse_row(S, [1, 4], [2, 2]) # Addition A = sparse_row(FlintZZ, [1, 2, 3, 5], ZZRingElem[1, 2, 3, 5])