Unwrap operator mass matrices before findall in Differentiation - #3944
Merged
ChrisRackauckas merged 1 commit intoAug 1, 2026
Merged
Conversation
singhharsh1708
force-pushed
the
fix-2929-matrixoperator-massmatrix-sparsity
branch
2 times, most recently
from
July 21, 2026 14:31
dd6b78a to
9410f9b
Compare
singhharsh1708
force-pushed
the
fix-2929-matrixoperator-massmatrix-sparsity
branch
3 times, most recently
from
August 1, 2026 21:26
f5a6694 to
ca27c50
Compare
Seeding a sparsity pattern from the mass matrix uses findall and getindex, which a SciMLOperator does not support, so a MatrixOperator mass matrix combined with a jac_prototype threw MethodError: keys(::MatrixOperator). The same pattern appeared at four places: prepare_user_sparsity and its sparsity unwrap in alg_utils.jl, and build_jac_config and sparsity_colorvec in derivative_wrappers.jl. Route all four through one concrete_mass_matrix helper, which reads the operator's cached array rather than re-evaluating it, since only the structural pattern matters and this runs before init.
singhharsh1708
force-pushed
the
fix-2929-matrixoperator-massmatrix-sparsity
branch
from
August 1, 2026 22:16
ca27c50 to
f52f8e1
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.
Removes the
MethodErrorcrash sites on the path reported in #2929. #2929 itself stays open — see below.Bug
Several places in
OrdinaryDiffEqDifferentiationseed a sparsity pattern from the mass matrix withBoth
findallandgetindexneed a concrete matrix. A SciMLOperator mass matrix supports neither, so combining one with ajac_prototypethrowsMethodError: no method matching keys(::MatrixOperator{...}):Fix
One helper, used at all four sites that had the pattern —
prepare_user_sparsityand itssparsityunwrap inalg_utils.jl, andbuild_jac_configandsparsity_colorvecinderivative_wrappers.jl:convertreads the operator's currently-cached array rather than re-evaluating it, so noupdate_coefficients!is involved. That is what is wanted here: only the structural pattern matters, andprepare_algruns beforeinit, so the cached array is the one the operator was constructed with. Non-operator mass matrices dispatch to the identity method, so plain-matrix behaviour is unchanged.Besides
MatrixOperatorthis also coversDiagonalOperator,IdentityOperator,ScaledOperator,AddedOperator,ComposedOperatorandTensorProductOperator, which all defineconvert(AbstractMatrix, ·).ScalarOperator,FunctionOperatorandAffineOperatorhave no such method and still error, but they were already failing earlier onkeysand none is a plausible mass matrix here.What this does not fix
#2929's reproducer calls
solve, and that still fails — but for a different reason and further along. With these four sites fixed it gets past everykeyserror and reaches the sparse-Wbranch ofjacobian2W!(derivative_utils.jl), which broadcasts the operator directly into aSparseMatrixCSCand stack-overflows. So #2929 needs at least one more change and should stay open; this PR is a strict improvement rather than a resolution.Measured, same MWE: master fails with
MethodError: keys(::MatrixOperator); this branch fails withStackOverflowErrorfromjacobian2W!.An earlier version of this description blamed a SciMLOperators
setfield!bug for the remaining failure. That was SciML/SciMLOperators.jl#409, fixed by #416 and released in SciMLOperators v1.25.2, and is no longer relevant.Tests
New
prepare_user_sparsity_tests.jl:MatrixOperatorandDiagonalOperatormass matrices, plus a case wheresparsityis left to default tojac_prototype(how #2929 constructs the problem). Three of those error on master with thekeysMethodErrorand pass here. Two further testsets pin theUniformScalingand plain-sparse paths as unchanged, and pass on both sides.Branch: 9/9.
OrdinaryDiffEqDifferentiationCore suite goes from 61 passes on master to 67 here.AI Disclosure
Claude assisted with this work.