From ad292905260123aa0158e609cb713ad04f4b60d6 Mon Sep 17 00:00:00 2001 From: "Viral B. Shah" Date: Mon, 26 Oct 2026 13:12:23 +0000 Subject: [PATCH] Fall back to `lu` in `factorize` of a sparse Hermitian matrix whose LDLt fails `factorize` of an indefinite Hermitian sparse matrix whose LDLt needs pivoting threw `ZeroPivotException`, while `\` falls back to `lu`. It now does the same. Co-Authored-By: Claude Opus 5.5 --- src/linalg.jl | 11 +++++------ test/solvers/issues.jl | 5 +++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/linalg.jl b/src/linalg.jl index f4feb7c2..0d1cfc66 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -1494,12 +1494,11 @@ factorize(A::AbstractSparseMatrixCSC) = _sparse_factorize(_factorize_choice, A) function factorize(A::RealHermSymComplexHerm{<:Union{Float32,Float64},<:AbstractSparseMatrixCSC}) F = cholesky(A; check = false) - if LinearAlgebra.issuccess(F) - return F - else - ldlt!(F, A) - return F - end + LinearAlgebra.issuccess(F) && return F + ldlt!(F, A; check = false) + LinearAlgebra.issuccess(F) && return F + # an indefinite matrix may need pivoting, which only the LU does, as in `\` + return lu(A) end eigen(A::AbstractSparseMatrixCSC) = diff --git a/test/solvers/issues.jl b/test/solvers/issues.jl index 588c8acb..7154b74f 100644 --- a/test/solvers/issues.jl +++ b/test/solvers/issues.jl @@ -241,4 +241,9 @@ end @test ldiv!(lu(A), ComplexF32[1, 2]) ≈ ComplexF32.(Matrix(A) \ [1, 2]) end +@testset "factorize of an indefinite Hermitian matrix whose LDLt needs pivoting" begin + b = [1.0, 2.0] + @test factorize(sparse([0.0 1; 1 0])) \ b ≈ [0.0 1; 1 0] \ b +end + end # module