Skip to content

The total derivative in an unknown function, a partial-derivative node, and substitution into a derivative over an expression - #2477

Open
Rafael-SOWNet wants to merge 2 commits into
masterfrom
feature/derivative-by-a-function
Open

Rafael-SOWNet wants to merge 2 commits into
masterfrom
feature/derivative-by-a-function

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

The design decided on #230 (comment of 2026-11-15, edited 2026-11-16), and a wrong answer in 2.7.0 that it would otherwise have made more reachable. Two commits, meant to be merged as they are (rebase, not squash).

1. The total derivative in an unknown function, and a partial-derivative node

derivative stays the total, change-of-variables derivative, and now answers the mixed case it declined: d(expr)/d(u) = (d expr/dx)/(du/dx). It declined because apply(f, x) reported f as a variable, so two variables were seen, and f' read as a derivative it had failed to take. The scope is one underlying variable, as decided: other variables are held fixed, and a target in two free variables stays unevaluated.

input, f = apply(f, x) 2.7.0 now
derivative(x*f, f) unevaluated f/f' + x
derivative(f^2+f'^2, f) unevaluated 2(f + f'') provided not f' = 0
derivative(f^2*f', f) unevaluated f^2 f''/f' + 2 f f'
derivative(y sin x, cos x) -y csc(x) cos(x) unchanged
derivative(x+y, x*y) unevaluated unchanged

partialderivative(L, q[, n]), a new node (PartialDerivativef, MathS.PartialDerivative), named after the "Partial derivative" of Wikipedia and MathWorld: its slot q (a variable, an unknown function applied, or a derivative of one) is varied with everything else held fixed, f' and x included, as in Euler–Lagrange. partialderivative(f^2+f'^2, f) is 2f, partialderivative(x*f, f) is x. Over a variable it is the same as derivative, as SymPy's diff is, so partialderivative(apply(f, x, y), x) is ∂f/∂x. It does not bind. LaTeX \frac{\partial}{\partial q}, in the operator form derivative uses; SymPy sympy.diff; refused by both compilers through Binders.Refusal, so MathS.Shaders declines it with the same message. Syntax.md sets the two derivatives out side by side. partialderivative(x) was the product partialderivative * x; it is now an arity error.

2. A value for a variable a derivative is taken over goes into its answer

A derivative or indefinite integral taken over an expression of x, rather than over x, put a substituted value into the body alone, which changed the question and simplified to wrong values. Now the derivative is taken first and the value goes into the answer; where it cannot be taken, the value is held outside as apply(lambda(x, …), v), and lambda application takes such a node before putting its argument in. Over a plain variable nothing changes: derivative(x*y, y) at x = 2 is still derivative(2 * y, y), that is 2.

at x = 2, with Simplify 2.7.0 now
derivative(x^3, x^2) 0 3
derivative(y sin x, cos x) 0 -y csc(2) cos(2)
derivative(sin(x!), x^2) 0 cos(2)(3/2 - eulergamma)/2
integral(x, x^2) C + 2x^2 16/3 + C

Both are in BREAKING-CHANGES.md, measured on the 2.7.0 package. A limit and a definite integral bind their variable and are left as they were.

Checks

DerivativeInAFunctionTest: every answer checked by value with f = sin, eᵗ and t³ + t at three points, total derivatives against (d expr/dx)/(du/dx), partial ones against the derivative in a plain variable standing for the slot, and the substitution at x = 0.3, 0.7, 1.3 against f^2/f' + 2xf. The grammar gains one alternative after lambda(; master's grammar was regenerated unchanged first and matched byte for byte.

Full check on the second commit before the rebase onto 1a80be1 (clean): 20,695 unit tests, 0 failed; MCP 450, Terminal 69, F# 135, Interactive 19; AOT OK; BoundCheck 0 disagreements, RootCheck 596/596, CasBench 120/121 with 0 wrong, SimpSweep 10463/10463, RuleCheck clean, CanonCheck and Confluence at their baselines.

Closes #230.

🤖 Generated with Claude Code

Rafael-SOWNet and others added 2 commits November 16, 2026 02:47
… node

derivative(expr, u) over an expression u in one variable x is the total
derivative, (d expr/dx) / (du/dx), and it declined where u was an unknown
function applied, apply(f, x), or a derivative of one: f was counted as a
variable u varies in, and the derivative of an unknown function, f', was read
as a derivative not taken. The name of an unknown function is now not a
variable it varies in, and f' is taken as the value it is. So
derivative(x f, f) is f/f' + x, and derivative(f^2 + f'^2, f) is
2(f + f'') provided not f' = 0. What answered before is unchanged, and over
two variables, derivative(x + y, x * y), it is still left as written.

partialderivative(L, q) and partialderivative(L, q, n) are the partial
derivative dL/dq as its own node, PartialDerivativef, built by
MathS.PartialDerivative: q is a slot -- a variable, an unknown function
applied, or a derivative of one in a variable -- and every other slot is held
fixed, as in the Euler-Lagrange equation. Every slot is renamed to a fresh
variable, the derivatives first so that f' is not read as a function of f,
and an unknown function of the slot, g(f), is not renamed. So
partialderivative(f^2 + f'^2, f) is 2f and partialderivative(x f, f) is x.
Over a variable it is the derivative; over anything else it is left as
written, since x^2 is a slot of x^2 and not of x * x. It does not bind; a
value put in for x goes into the slot too. Wired through printing, LaTeX
(\frac{\partial}{\partial q}), SymPy's diff, Substitute, differentiation, the
limit and solver hooks, serialization, the compilers' refusal, the F#
wrapper, the MCP syntax document and classifier, and Syntax.md beside the
total derivative. Tests check every answer by value with f made concrete.

Grammar: one alternative, 'partialderivative(', after lambda( so that no
literal token is renumbered; the unmodified grammar regenerated to an empty
diff first. partialderivative(x) with one argument was the product
partialderivative * x and is now an arity error; both changes are in
BREAKING-CHANGES.md, measured on the 2.7.0 package.

Part of #230

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A derivative or an indefinite integral over an expression u of x is taken by
letting x move, and Substitute put a value for x into the expression alone:
derivative(x^3, x^2) at x = 2 became derivative(8, x^2), which is 0 where the
answer is 3, and integral(x, x^2) kept an x. With the mixed case now
answered, derivative(x f(x)^2, f(x)) at 2 would have been 0 as well, where it
is f(2)^2/f'(2) + 4 f(2). Such a node is now taken first and the value put
into its answer, or, where it cannot be taken, the value is held outside it as
apply(lambda(x, ...), 2), which lambda application now takes in the same
order. Over a variable nothing changes. The partial derivative holds a value
outside the same way where its slot stops being one. Recorded in
BREAKING-CHANGES.md, measured on the 2.7.0 package.

The shader printer declines a partial derivative with Compile's message, and
a test says so.

Part of #230

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

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.90%. Comparing base (1a80be1) to head (6fd1a2b).

Files with missing lines Patch % Lines
Sources/Wrappers/AngouriMath.FSharp/Functions.fs 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2477      +/-   ##
==========================================
- Coverage   86.97%   86.90%   -0.08%     
==========================================
  Files         324      324              
  Lines       71840    71446     -394     
  Branches    20952    20948       -4     
==========================================
- Hits        62483    62090     -393     
- Misses       5024     5025       +1     
+ Partials     4333     4331       -2     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
fsharp 32.79% <0.00%> (-0.18%) ⬇️
interactive 39.81% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
Sources/AngouriMath/Convenience/MathS.cs 90.77% <ø> (ø)
Sources/AngouriMath/Core/Antlr/AngouriMathLexer.cs 99.26% <ø> (ø)
...ources/AngouriMath/Core/Antlr/AngouriMathParser.cs 86.65% <ø> (-1.40%) ⬇️
Sources/AngouriMath/Core/Domains.Classes.cs 100.00% <ø> (ø)
...y/Continuous/Entity.Continuous.Calculus.Classes.cs 100.00% <ø> (ø)
...urces/AngouriMath/Functions/Compilation/Binders.cs 93.33% <ø> (ø)
...ngouriMath/Functions/Continuous/Differentiation.cs 86.48% <ø> (ø)
...nctions/Continuous/Limits/Solvers/Limit.Classes.cs 77.80% <ø> (ø)
...nuous/Solvers/EquationSolver/InvertNode.Classes.cs 61.85% <ø> (ø)
...ntinuous/Evaluation.Continuous.Calculus.Classes.cs 86.79% <ø> (+0.81%) ⬆️
... and 7 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Status: Todo

Development

Successfully merging this pull request may close these issues.

Differentiation with respect to functions

1 participant