diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 02439922341e2..7ebaae7c0b5fd 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -2745,7 +2745,7 @@ function dropstored!(A::SparseMatrixCSC, i::Integer, j::Integer) # Entry A[i,j] is stored. Drop and return. deleteat!(A.rowval, searchk) deleteat!(A.nzval, searchk) - @simd for m in j:(A.n + 1) + @simd for m in (j+1):(A.n + 1) @inbounds A.colptr[m] -= 1 end end diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index f8e52f273dcd7..f6bed5fe0dfe3 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -1705,3 +1705,19 @@ end @testset "issue #14398" begin @test transpose(view(speye(10), 1:5, 1:5)) ≈ eye(5,5) end + +@testset "dropstored issue #20513" begin + x = sparse(rand(3,3)) + Base.SparseArrays.dropstored!(x, 1, 1) + @test x[1, 1] == 0.0 + @test x.colptr == [1, 3, 6, 9] + Base.SparseArrays.dropstored!(x, 2, 1) + @test x.colptr == [1, 2, 5, 8] + @test x[2, 1] == 0.0 + Base.SparseArrays.dropstored!(x, 2, 2) + @test x.colptr == [1, 2, 4, 7] + @test x[2, 2] == 0.0 + Base.SparseArrays.dropstored!(x, 2, 3) + @test x.colptr == [1, 2, 4, 6] + @test x[2, 3] == 0.0 +end