Skip to content

Register derivatives of symbolic NDInterpolations - #105

Draft
pankgeorg wants to merge 1 commit into
SciML:mainfrom
pankgeorg:symbolic-callable-derivative
Draft

pankgeorg wants to merge 1 commit into
SciML:mainfrom
pankgeorg:symbolic-callable-derivative

Conversation

@pankgeorg

Copy link
Copy Markdown

Fixes #69.

Adds derivative rules for a symbolic NDInterpolation, such as a ModelingToolkit callable parameter @parameters (itp::NDInterpolation)(..). A partial derivative becomes NDPartialDerivative(orders, shape)(itp, args...), which keeps itp as an argument so that it can stay symbolic, and has its own rule for higher orders. Rules for interpolation objects used directly are unchanged.

Needs JuliaSymbolics/Symbolics.jl#2022. The Symbolics compat bound is bumped to the release containing it (7.42), and the version to 0.1.11.

Tested with new testsets in test/Extensions/test_symbolics_ext.jl: each partial, the chain rule through both arguments, a mixed second derivative, substitution, and array output, checked numerically through build_function. The MWE from #69 compiles with fully_determined = true and solves with and without jac = true.

🤖 Generated with Claude Code

Partial derivatives of a symbolic `NDInterpolation`, e.g. a ModelingToolkit
callable parameter, expand to `NDPartialDerivative(orders)(itp, args...)`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mcgarnett

Copy link
Copy Markdown

could we merge this? I could really use it

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

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

Independent review (Codex CLI 0.166.0 / gpt-6.1-sol), session 01a1983e-5145-77e1-bbb9-789c0675687e, reviewed head a1ca7a8

  • Verdict: CHANGES
  • Risk: medium-low (ML4: higher minimum Symbolics version; L6 applies to the verified local fix for previously unexpanded derivatives, with existing direct-interpolation assertions passing on both revisions.)
  • Round / slot: 1 / 1
  • Blocking priorities: P2

Review written by the reviewer named above in a session other than the PR author's, and copied here by the posting agent without change to its findings. The posting agent did not re-run the reviewer's commands.

Summary: Registers coordinate derivatives of symbolic NDInterpolation callables, including repeated derivatives and array shapes, and raises the Symbolics minimum to 7.42. The added tests pass on the head and fail on the merge-base, but an independent analytic probe exposes an exception when differentiating an array component again after substituting a concrete interpolation.

Blast radius: Symbolics extension, symbolic callable partial derivatives, shape/type propagation, substitution and generated derivative functions; numeric interpolation kernels are unchanged.

Evidence: Julia 1.13.2, Symbolics 7.42.0 and SymbolicUtils 4.54.0: head extension file passes 40/40 assertions; the identical file against merge-base 3093d8d passes the 23 existing assertions, then the new scalar symbolic testset reports 4 passes, 3 failures and 5 errors. The analytic probe passes 74 assertions and errors on a matrix mixed derivative after substitution. The draft PR reports no CI checks; the recent main-branch Tests run is successful.

Blocking findings

  1. [P2] ext/DataInterpolationsNDSymbolicsExt.jl:133 — confirmed by running: repeated differentiation of an array component fails after substituting the interpolation. For a matrix table whose component (2,2) represents 2 + 7x - 4y + 5xy, let q be declared by @variables (q::NDInterpolation)(..)[1:2, 1:2], compute d = Symbolics.derivative(q(x,y)[2,2], x), substitute q => mat, then call Symbolics.derivative(substitute(d, Dict(q => mat)), y). This throws ArgumentError: Cannot take derivative with respect to constant; the analytic mixed derivative is the scalar 5. The new derivative term retains the concrete interpolation as its first argument, while its rule returns nothing for that argument. Symbolics' indexed-array differentiation consequently attempts to construct a Differential with respect to that constant interpolation. Handle the concrete-interpolation case, for example by lowering it to the existing concrete derivative wrapper with the interpolation captured in the operator, and add a differentiate–substitute–differentiate regression for an array component against the analytic mixed derivative.

Non-blocking findings

None.

What I ran

All three Julia processes ran sequentially with JULIA_NUM_THREADS=4, OPENBLAS_NUM_THREADS=1, JULIA_PKG_PRECOMPILE_AUTO=0, an isolated TMPDIR, and JULIA_DEPOT_PATH set to the designated shared depot followed by a colon. No package-wide suite was run. The temporary environment is outside the source checkout.

Git inspection commands (exit 0):

git -C src fetch origin '+refs/pull/105/head:refs/remotes/pr/105' main
git -C src checkout --detach pr/105
git -C src fetch origin '+refs/heads/main:refs/remotes/origin/main'
git -C src rev-parse HEAD origin/main
git -C src diff origin/main...HEAD
git -C src merge-base origin/main HEAD
git -C src diff 3093d8ddb43710d05164963ada68255fde32563c d41d499767c99a5232db1a112fb083f6f4f092ba -- .github/workflows/Tests.yml .github/workflows/Downgrade.yml .github/workflows/GPU.yml
git -C src diff --name-only 3093d8ddb43710d05164963ada68255fde32563c d41d499767c99a5232db1a112fb083f6f4f092ba
git -C src worktree add --detach ../base 3093d8ddb43710d05164963ada68255fde32563c
git -C src log -1 --format=%B a1ca7a8231b3ec4f36a54c6889e350effd5d0a99

The current origin/main is the Base recorded above; regression execution uses its merge-base with the PR. The intervening main commit changes CI concurrency only. Read the extension, numeric evaluation callers, documentation and test-group wiring. Froze the diff-only assessment in INITIAL.md and created INITIAL.sha256 before reading author context.

Exact Julia invocations (the environment variables above were applied to each):

timeout 3600 julia +1.13.2 --startup-file=no --compiled-modules=existing run-head.jl
timeout 3600 julia +1.13.2 --startup-file=no --compiled-modules=existing run-base.jl
timeout 3600 julia +1.13.2 --startup-file=no --compiled-modules=existing probe.jl
  • Head script: Pkg.activate a temporary environment, Pkg.develop the head checkout, Pkg.add([PackageSpec(name="Symbolics", version="7.42.0"), PackageSpec(name="SymbolicUtils")]), then include test/Extensions/test_symbolics_ext.jl. Exit 0. Output tails: Basics 5/5; Differentiation 6/6; Scalar output basics 4/4; Scalar output differentiation 8/8; Symbolic interpolation differentiation 12/12; Symbolic array-output interpolation differentiation 5/5.
  • Base script: activate the same environment, develop the merge-base worktree and include the head's identical extension test file. Exit 1. Existing testsets pass 23/23; new scalar symbolic testset reports Pass 4, Fail 3, Error 5, Total 12. Unexpanded derivatives fail !Symbolics.hasderiv and generated numerical comparisons error. Execution stops before the new array-output testset. This establishes failing-before/passing-after for the added scalar regression; the added array testset was verified passing only on the head.
  • Analytic probe: develop the head again and test interpolation tables sampling the exactly representable bilinear polynomial P(x,y)=2+3x-4y+5xy. Compare generated functions and folded substitutions with closed-form first, mixed and repeated partials at three points, including zero pure second derivatives (35/35). Check nonlinear coordinate composition p(s^2,s) through third order and p(sin(s),s^2) through first order (13/13). Check all components of a 2-by-2 output, generated numerical derivatives, full substitution and preserved matrix type/shape (26 assertions pass). Exit 1 on subsequent mixed differentiation after substituting the concrete interpolation, with the exception quoted in the blocking finding. Numeric comparisons use relative and absolute tolerances of 1e-12 for these exact polynomial identities; expected values are analytic, not copied interpolation outputs.

Remote inspection:

gh pr view 105 --repo SciML/DataInterpolationsND.jl --json headRefOid,baseRefOid,baseRefName,state,isDraft,body,url,title,statusCheckRollup
gh issue view 69 --repo SciML/DataInterpolationsND.jl --json title,body,url
gh pr checks 105 --repo SciML/DataInterpolationsND.jl
gh run list --repo SciML/DataInterpolationsND.jl --branch main --event push --limit 5 --json databaseId,conclusion,headSha,url,name
gh api repos/JuliaSymbolics/Symbolics.jl/pulls/2022 --jq '{title,body,state,merged,merge_commit_sha}'

PR checks exits 1 with no checks reported; the other remote inspections exit 0. Inspected main-run URLs: Tests, Downgrade, GPU Tests, Documentation, and format-check, all reported successful at the current main head. These are not PR-head CI evidence. Inspected the public SymbolicCallable/derivative registration and SymbolicUtils shape/type APIs in the installed dependency sources.

What I did not verify

No ModelingToolkit compilation or ODE solve of issue #69, with or without a generated Jacobian. No package-wide or downstream suites, GPU execution, ForwardDiff, other AD systems, Reactant or MPI runs; those implementations are unchanged. No Julia 1.10, other platforms, latest Symbolics, or minimum compatible SymbolicUtils matrix. The probe's planned three-input tests never executed because the preceding matrix testset errored. No second execution of any test was performed; exactly three Julia runs were used. No PR CI checks were reported to inspect, and main-branch success does not validate the changed extension. No upstream writes were made.

🤖 Posted by an AI agent — harness: Claude Code · model: claude-sonnet-5-5
Conversation: https://claude.ai/code/session_01379heiiB35TtywhR7uSU74

This branch has not been deployed

No deployments
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.

Differentiating through DataInterpolationsND functions throws error

3 participants