prepare_user_sparsity: unwrap operator mass matrices before findall (#2929)#3944
Open
singhharsh1708 wants to merge 1 commit into
Open
Conversation
singhharsh1708
force-pushed
the
fix-2929-matrixoperator-massmatrix-sparsity
branch
from
July 18, 2026 07:33
39aa6be to
dd6b78a
Compare
findall(!iszero, mm) and mm[idx] need a concrete matrix. A SciMLOperator mass matrix (e.g. MatrixOperator) supports neither, so a sparse jac_prototype + operator mass matrix crashed with MethodError: no method matching keys(::MatrixOperator...). Partially addresses SciML#2929. convert(AbstractMatrix, mm) only reads the operator's currently cached array, not a live re-evaluation, so this is safe even for operators whose in-place update_coefficients! path is broken upstream (that part of SciML#2929's repro is a separate SciMLOperators.jl bug, not fixable here).
singhharsh1708
force-pushed
the
fix-2929-matrixoperator-massmatrix-sparsity
branch
from
July 21, 2026 14:31
dd6b78a to
9410f9b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Partially addresses #2929.
Bug
prepare_user_sparsity(lib/OrdinaryDiffEqDifferentiation/src/alg_utils.jl) seeds the sparsity pattern from the mass matrix with:when the mass matrix isn't a
UniformScaling.findall/getindexneed a concrete matrix; aSciMLOperatormass matrix (e.g.MatrixOperator) supports neither directly:matching #2929's original report exactly.
Fix
Unwrap the operator to its concrete matrix first:
convert(AbstractMatrix, mm). This only reads the operator's currently-cached array (SciMLOperators.matrix.jl'sconvert(::Type{AbstractMatrix}, L::MatrixOperator) = convert(AbstractMatrix, L.A)), not a live re-evaluation, so noupdate_coefficients!call is involved.What this doesn't fix
While tracing #2929's repro I found the baseline case (no
jac_prototypeat all) has separately regressed since the issue was filed — it now fails with a different error,setfield!: immutable struct of type MatrixOperator cannot be changed. That's entirely inside SciMLOperators.jl:update_coefficients!on aMatrixOperatorbuilt with only an out-of-placeupdate_func(noupdate_func!) does a rawL.A = ...field assignment on what's declared an immutable struct —update_coefficients(no bang) handles the same case correctly via@reset. Root-caused tosrc/matrix.jl:227in SciMLOperators v1.24.3, confirmed with a minimal SciMLOperators-only reproducer (no OrdinaryDiffEq involved). That's a separate upstream bug I can't fix here; will report it in SciMLOperators.jl. #2929's repro won't fully pass end-to-end until that's also fixed, but this PR fixes the part that's actually ours.Tests
New
prepare_user_sparsity_tests.jl:MatrixOperatormass matrix (fails on master with thekeysMethodError, passes here with the correct merged sparsity pattern), plus regression checks that theUniformScalingand plain-sparse-matrix paths are unchanged.OrdinaryDiffEqDifferentiationCore suite passes (39/39, including 6 new).AI Disclosure
Claude assisted with this work.