diff --git a/stdlib/SparseArrays/src/linalg.jl b/stdlib/SparseArrays/src/linalg.jl index 86ab4b78f50f0..9be76da96dc98 100644 --- a/stdlib/SparseArrays/src/linalg.jl +++ b/stdlib/SparseArrays/src/linalg.jl @@ -472,8 +472,7 @@ function _ldiv!(L::LowerTriangularPlain, B::StridedVecOrMat) # find diagonal element ii = searchsortedfirst(ja, j, i1, i2, Base.Order.Forward) - ii > i2 && ( ii = i1) - jai = ja[ii] + jai = ii > i2 ? zero(eltype(ja)) : ja[ii] bj = B[joff + j] # check for zero pivot and divide with pivot @@ -515,8 +514,7 @@ function _ldiv!(U::UpperTriangularPlain, B::StridedVecOrMat) # find diagonal element ii = searchsortedlast(ja, j, i1, i2, Base.Order.Forward) - ii < i1 && ( ii = i2) - jai = ja[ii] + jai = ii < i1 ? zero(eltype(ja)) : ja[ii] bj = B[joff + j] # check for zero pivot and divide with pivot diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index ac91d66614fa5..fd13fc9189233 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -2312,6 +2312,13 @@ end @test AW \ B ≈ MAW \ B end end + + A = LowerTriangular(sparse([0 2.0;0 1])) + @test_throws SingularException(1) A \ ones(2) + A = UpperTriangular(sparse([1.0 0;0 0])) + @test_throws SingularException(2) A \ ones(2) + + end end # module