From 411777b5ba8e880c1315a10a05e6a61b21daf417 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 18 Aug 2025 11:57:14 -0400 Subject: [PATCH] Fix KLUFactorization for AbstractSparseMatrixCSC subtypes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #737 by handling the case where the cached KLU factorization is uninitialized (PREALLOCATED_KLU) or has a dimension mismatch with the input matrix. The issue occurred when using AbstractSparseMatrixCSC subtypes because: 1. The cacheval would be set to PREALLOCATED_KLU (a 0x0 matrix) 2. pattern_changed would return false for this case 3. klu! would be called with mismatched dimensions, causing an error The fix checks if the cacheval is uninitialized or has a size mismatch, and creates a new factorization in those cases instead of trying to reuse the incompatible cached value. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- ext/LinearSolveSparseArraysExt.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ext/LinearSolveSparseArraysExt.jl b/ext/LinearSolveSparseArraysExt.jl index 57e135164..6b9662764 100644 --- a/ext/LinearSolveSparseArraysExt.jl +++ b/ext/LinearSolveSparseArraysExt.jl @@ -259,6 +259,13 @@ function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::KLUFactorization; SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A), nonzeros(A)), check = false) + elseif cacheval === nothing || cacheval === PREALLOCATED_KLU || + length(nonzeros(A)) != length(cacheval.nzval) + # Create new factorization if cacheval is uninitialized or size mismatch + fact = KLU.klu( + SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A), + nonzeros(A)), + check = false) else fact = KLU.klu!(cacheval, nonzeros(A), check = false) end