Skip to content

NonlinearFunction docs: jac_prototype may be an AbstractSciMLOperator - #1640

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ParadaCarleton:nonlinearfunction-jac-prototype-operator
Oct 11, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ParadaCarleton:nonlinearfunction-jac-prototype-operator

Conversation

@ParadaCarleton

Copy link
Copy Markdown
Contributor

NonlinearSolve.jl (NonlinearSolveBase construct_jacobian_cache) already passes a jac_prototype that is an AbstractSciMLOperator, and an operator returned by jac(u, p), to the linear solver as is, but the NonlinearFunction docstring only mentions prototype matrices. I hit this wiring a bordered Jacobian: a custom Jacobian type that is not an operator fails with Cannot convert ... to Matrix, with no hint of the accepted types. This adds three lines to the jac_prototype entry saying operators are accepted, and when they must be convertible. I checked the behaviour with NonlinearSolve 4.32 and a NewtonRaphson using a LinearSolveFunction on Float64 and Float32.

@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

Independent review (Devin CLI 3000.11.3, model fusion-claude-opus-5-5-high-sidekick-swe-2-medium): MERGE, risk low.

Full review

VERDICT: MERGE
RISK: low

PR: #1640 adds 3 lines to the NonlinearFunction docstring (src/scimlfunctions.jl:1972-1974). No code, tests, exports or dependencies change. The PR links no issue and no tracking issue.

Blocking findings

None. Everything the three added lines claim matches the current NonlinearSolveBase/LinearSolve code, and I confirmed each claim by running it (see "What I ran").

  • "jac_prototype may be an AbstractSciMLOperator, jac(u,p) may return an operator, passed to the linear solver as is": NonlinearSolveBase.construct_jacobian_cache (v2.54.1 src/jacobian.jl:42,77-89) uses the operator directly, and construct_linear_solver (src/linear_solve.jl:94-99) aliases it. The same code is in the registered 2.35.0, 2.47.0 and 2.51.1 releases in the depot. Confirmed by running: with an OOP jac that returns a MatrixOperator, NewtonRaphson converges to sqrt.(p) with error 0.0 under LU, the default solver and GMRES.
  • "mul! for Krylov methods, and convert(AbstractMatrix, op) for factorizations, so it must be convertible": confirmed by running. A non-convertible FunctionOperator converges with KrylovJL_GMRES. With LUFactorization() or the default solver it throws NonlinearSolveBase's ArgumentError ("not convertible to a concrete matrix…"). LinearSolve 5.18.2 calls convert(AbstractMatrix, A) throughout factorization.jl.
  • The PR body says Float32 works. Confirmed by running with the default solver (error 0.0f0) and GMRES (error 4.8f-7). LU failed, but only because of the Diagonal issue in non-blocking finding 3, which is unrelated to this PR.

Non-blocking findings

  1. Doc gap: the most common trap is not documented (confirmed by running). If jac_prototype is an operator and jac is omitted, the in-place constructor sets jac = update_coefficients! (src/scimlfunctions.jl:4746-4752). NonlinearSolveBase (jacobian.jl:264-283) then uses the prototype as a fixed Jacobian: it is never AD'd and never refreshed. I ran an IIP NonlinearFunction with jac_prototype = MatrixOperator(zeros(2,2)) and no jac. The result was InternalLinearSolveFailed under LU and Stalled under the default solver and GMRES, with u left at u0. A paragraph whose whole purpose is to explain operator prototypes should say "if jac is omitted, the operator is used as the Jacobian unchanged; supply jac to update it". The results are not silently wrong, because NonlinearSolve still checks residual convergence, so I'm not blocking on this.
  2. Doc gap: the in-place form is left out (confirmed by running). The text only mentions jac(u,p). An in-place jac!(J, u, p) that updates the operator J also works (jacobian.jl:270-271): Success with error 0.0 under all three linear solvers. Suggested wording: "jac(J,u,p) may update it in place, or jac(u,p) may return a new operator".
  3. Existing bug next to this text, not caused by this PR (confirmed by running). Out-of-place NonlinearFunction{false}(f; jac_prototype = <operator>) with jac omitted throws NonconformingFunctionsError at construction. The auto-wired closure at src/scimlfunctions.jl:4750 is (u, p, t) -> …, which has arity 3, so isinplace(jac, 3, …) treats it as in-place. ODEFunction builds this case without error. So the operator-prototype path in this docstring currently works OOP only if the user supplies jac. This belongs in a separate issue or fix PR, not here.
  4. Downstream bug, not this PR's (confirmed by running). A MatrixOperator(Diagonal(...)) Jacobian with LUFactorization() fails with TypeError: in setfield!, expected LU{…Tridiagonal…}, got LU{…Diagonal…}, for both Float64 and Float32. The same thing with a plain Diagonal prototype works, and so does a dense MatrixOperator. So "convertible" is necessary for factorizations but not sufficient under LinearSolve 5.18.2. The fix belongs in LinearSolve, not in this docstring.
  5. Style (from reading). An interface docstring in SciMLBase now names one downstream package's internals (mul! vs convert(AbstractMatrix, op) in NonlinearSolve.jl). That ties the docs to one implementation, and the surrounding docstrings stay solver-agnostic. Acceptable, but the wording could be generic ("solvers apply it via mul!; factorizations require it to be convertible").
  6. Evidence and provenance (from reading). The PR body asserts a check ("NonlinearSolve 4.32 … Float64 and Float32") without the script or its output. It also says a non-operator custom Jacobian fails with Cannot convert ... to Matrix, with no reproducer. I did not reproduce that exact message: a custom AbstractMatrix Jacobian worked, and a non-AbstractMatrix type failed earlier with MethodError: no method matching similar(::NotAMatrix). This motivating claim has no effect on whether the doc text is correct. The commit has no agent-attribution trailers. There is no AGENTS.md or CLAUDE.md at the repo root, so no repo-level rule binds the author on this.

What I ran

  • gh pr view 1640 -R SciML/SciMLBase.jl and git diff origin/master...HEAD: one commit, src/scimlfunctions.jl, +3/−0. There is no linked issue to gh issue view.
  • Read NonlinearSolveBase v2.54.1 src/jacobian.jl and src/linear_solve.jl, and checked older registered versions in the depot for the same operator path. Read LinearSolve v5.18.2 for convert(AbstractMatrix, …) and isconvertible. Read the SciMLBase NonlinearFunction constructor.
  • Scratch env (scratch/, Julia 1.11): SciMLBase dev'd from this PR head (3.57.0), NonlinearSolve 4.32.0, NonlinearSolveBase 2.54.1, LinearSolve 5.18.2, SciMLOperators 1.30.2.
    • scratch/probe.jl and scratch/probe2.jl: NewtonRaphson on u.^2 .- p with LU, default and GMRES linear solvers, covering:
      • an OOP jac that returns a MatrixOperator, dense and Diagonal
      • an IIP jac! that updates a MatrixOperator
      • Float32
      • a non-convertible FunctionOperator
      • an operator prototype with jac omitted, IIP and OOP
      • a custom AbstractMatrix Jacobian and a non-AbstractMatrix Jacobian
    • Results are as quoted above. The first run of probe.jl had two errors caused by my own probe (sol.cache), which probe2.jl replaced.
  • git diff origin/master...HEAD | typos -: clean.
  • julia +1.12 --project=@runic -m Runic --check src/scimlfunctions.jl: clean.
  • All Julia processes I started have exited. The Julia processes still running belong to another review (OrdinaryDiffEq 4730).

What I did not verify

  • The docs build (docs/make.jl). The change is plain docstring prose with no @ref or new admonitions, so the risk is negligible. Not run.
  • SciMLBase's own test suite, QA group included. No code changed, so I did not run it.
  • The exact Cannot convert ... to Matrix error from the PR body, which has no reproducer.
  • Behaviour with NonlinearSolve versions other than 4.32.0 / NonlinearSolveBase 2.54.1, or with solvers other than NewtonRaphson (trust region, Levenberg–Marquardt, quasi-Newton).

🤖 Posted by an AI agent — harness: Devin CLI 3000.11.3 (review), Claude Code 2.1.285 (posting) · model: fusion-claude-opus-5-5-high-sidekick-swe-2-medium
Conversation: local Claude session 3c311569-dda6-4687-bc9f-65febb212c33 (fleet master)

@ChrisRackauckas
ChrisRackauckas merged commit 75c15fd into SciML:master Oct 11, 2026
ChrisRackauckas referenced this pull request Oct 13, 2026
* Add a specialization marker to `OptimizationFunction`

`OptimizationFunction` had no specialization marker: its second type parameter is
the AD type, and `specialization(::OptimizationFunction)` returned that instead of
an `AbstractSpecialization`. The marker is now stored as the last type parameter,
after the field types, so code that spells out the existing parameters keeps
working. It defaults to `FullSpecialize`, so existing functions are unchanged.

`OptimizationFunction{iip, specialize}(f, adtype; kwargs...)` selects it, and
`specialization` returns it. `remake`, `ConstructionBase.setproperties` (and with
it `@set`) and `OptimizationProblem` keep it. The spelling that passes the AD
type as the second type parameter (`OptimizationFunction{iip, AD}(f; adtype)`)
still builds a `FullSpecialize` function.

What `AutoSpecialize` and `AutoDespecialize` do is up to the solver packages,
as for the other SciML functions.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* Convert parameter objects to `DespecializedParameters`

Storage typed for despecialized parameters could not be assigned a new parameter
object. For example, an optimization cache built from an `AutoDespecialize`
problem stores `p` as a `DespecializedParameters`, and `reinit!(cache; p = new_p)`
threw `Cannot convert an object of type ... to an object of type
SciMLBase.DespecializedParameters`. `convert` now wraps the object, and returns an
already wrapped one unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* Bump version to 3.58.0

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants