From 6ed4d80cc8442a3af9b16f050c37808699a481c3 Mon Sep 17 00:00:00 2001 From: Rasmus Munk Larsen Date: Thu, 22 Feb 2024 22:22:21 +0000 Subject: [PATCH] Fix crash in IncompleteCholesky when the input has zeros on the diagonal. --- .../src/IterativeLinearSolvers/IncompleteCholesky.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Eigen/src/IterativeLinearSolvers/IncompleteCholesky.h b/Eigen/src/IterativeLinearSolvers/IncompleteCholesky.h index 14ae6ea94..f456c96bd 100644 --- a/Eigen/src/IterativeLinearSolvers/IncompleteCholesky.h +++ b/Eigen/src/IterativeLinearSolvers/IncompleteCholesky.h @@ -214,6 +214,19 @@ void IncompleteCholesky::factorize(const MatrixType m_L.template selfadjointView() = mat.template selfadjointView(); } + // The algorithm will insert increasingly large shifts on the diagonal until + // factorization succeeds. Therefore we have to make sure that there is a + // space in the datastructure to store such values, even if the original + // matrix has a zero on the diagonal. + bool modified = false; + for (Index i = 0; i < mat.cols(); ++i) { + if (numext::is_exactly_zero(m_L.coeff(i, i))) { + m_L.insert(i, i) = Scalar(0); + modified = true; + } + } + if (modified) m_L.makeCompressed(); + Index n = m_L.cols(); Index nnz = m_L.nonZeros(); Map vals(m_L.valuePtr(), nnz); // values