Calculus
The rules of differentiation as laws, so a derivative written with ∂ is reduced to an ordinary formula at compile time.
Hilbert has no built-in differentiation. A derivative is written as a term — ∂ x² / ∂ x — and this
module states the rules that rewrite such a term, step by step, into a formula with no ∂ left in it.
Each law is oriented in the direction that takes a derivative, and the result is compiled like any
other formula: ∂ x² / ∂ x meets power, then self, then Arithmetic’s power_one and unit, and
is emitted as 2 · x. Every step is a law you can read on this page.
The module opens Arithmetic, whose laws tidy what the derivative rules leave behind
(2 · 1 + x · 0 becomes 2). As with every law, the rules are in force across the whole document once
any file in it opens Prelude.Calculus. A derivative that no law can take — in a document where no
file opens this module, or of a function with no rule below — is not reduced, and the compiler
generates no method for the doorway that contains it, without an error message. If an expected method
is missing from the generated class, check that the module is open and that every function you
differentiate has a rule.
Two laws carry a guard, written where x ∉ …, meaning “the variable of differentiation does not
appear in this part”: constant differentiates anything that does not mention the variable to zero,
and power applies only when the exponent does not mention it. Differentiation is symbolic and exact;
there are rules for sums, differences, negation, products, quotients, constant powers, sin, cos,
exp, ln and √, and the chain rule is built into each of the function rules. Other intrinsics
(tan, sinh, abs and so on) have no law here; write sin(x) / cos(x) rather than tan(x) when
you need its derivative.
open Prelude.Calculus
Rules of differentiation
In every law, x stands for the variable of differentiation and u, v, n for any sub-formula.
The rules apply repeatedly until no ∂ remains.
self
law self = ∂ x / ∂ x = 1
Let \(x\) be the variable of differentiation. Then
\[\frac{\partial x}{\partial x} = 1 .\]Fires when the formula being differentiated is the variable itself.
Intuition The base case of every derivative: moving \(x\) by a small step moves \(x\) by exactly that step, so the rate is one. Every other rule breaks a formula down until only ∂x/∂x and constants are left, and this law ends the descent.
Use it when you rely on any other rule — you never call it directly. The ones it leaves (2 · 1) are removed by Arithmetic’s unit, which this module opens for you.
differentiationsymbolic-simplification
constant
law constant = ∂ u / ∂ x = 0 where x ∉ u
For all expressions \(u\) in which \(x\) does not occur,
\[\frac{\partial u}{\partial x} = 0 .\]Fires on any formula that does not mention the variable: a literal, a constant such as π, or another
parameter. In ∂ (2x + y) / ∂ x, the y is differentiated to zero by this law.
Intuition What does not depend on \(x\) does not move when \(x\) moves. The guard makes this a partial derivative: every other parameter is held fixed, which is exactly what a gradient component or a sensitivity needs.
Use it when a formula has several parameters and you differentiate with respect to one of them — each coordinate of a gradient is one ∂ … / ∂ x with the others treated as constants. The zeros it leaves are removed by Arithmetic’s nil, zero and friends.
differentiationsensitivity-analysisoptimisation
sum
law sum = ∂ (u + v) / ∂ x = ∂ u / ∂ x + ∂ v / ∂ x
For all expressions \(u\) and \(v\),
\[\frac{\partial (u + v)}{\partial x} = \frac{\partial u}{\partial x} + \frac{\partial v}{\partial x} .\]Fires on a sum, differentiating each term.
Intuition Rates add: if two quantities each change with \(x\), their total changes at the sum of the two rates. Linearity is what lets a long formula be differentiated one term at a time.
Use it when you differentiate a polynomial, a loss built as a sum of penalties, or an energy made of several contributions. Each term is then handled by its own rule, and terms that do not mention \(x\) drop out through constant.
differentiationoptimisation
difference
law difference = ∂ (u − v) / ∂ x = ∂ u / ∂ x − ∂ v / ∂ x
For all expressions \(u\) and \(v\),
\[\frac{\partial (u - v)}{\partial x} = \frac{\partial u}{\partial x} - \frac{\partial v}{\partial x} .\]Fires on a difference, differentiating each side.
Intuition The rate of a gap is the difference of the rates. Subtraction is its own operator in Hilbert, not a sum with a negated term, so it needs its own rule.
Use it when you differentiate residuals and margins such as prediction − target or revenue − cost. When the subtracted side is constant, constant and Arithmetic’s minus_zero leave just the derivative of the first side.
differentiationoptimisation
negation
law negation = ∂ (−u) / ∂ x = −(∂ u / ∂ x)
For every expression \(u\),
\[\frac{\partial (-u)}{\partial x} = -\frac{\partial u}{\partial x} .\]Fires on a negated formula.
Intuition Mirroring a curve upside down mirrors its slope: where \(u\) rises, \(-u\) falls at the same rate. The minus sign simply moves outside the derivative.
Use it when you differentiate a negated quantity, for example turning a reward into a cost to minimise, or a term like −cos(x). Two stacked signs that result are collapsed by Arithmetic’s neg_neg.
differentiationsign-normalisation
product
law product = ∂ (u · v) / ∂ x = ∂ u / ∂ x · v + u · ∂ v / ∂ x
For all expressions \(u\) and \(v\),
\[\frac{\partial (u \cdot v)}{\partial x} = \frac{\partial u}{\partial x} \cdot v + u \cdot \frac{\partial v}{\partial x} .\]The product rule. Fires on any product; a constant factor is handled by the same rule, with
constant and Arithmetic’s nil removing the term that vanishes.
Intuition Picture a rectangle with sides \(u\) and \(v\): nudging \(x\) grows its area by a strip along each side, \(u' v\) plus \(u v'\). Each factor’s change is weighted by the size of the other factor.
Use it when you differentiate anything multiplied — a coefficient times a variable, x · sin(x), a price times a quantity. You never write the constant-factor rule separately: for 2 · x this law gives 0 · x + 2 · 1, and Arithmetic reduces it to 2.
differentiationsensitivity-analysis
quotient
law quotient = ∂ (u / v) / ∂ x = (∂ u / ∂ x · v − u · ∂ v / ∂ x) / v²
For all expressions \(u\) and \(v\),
\[\frac{\partial (u / v)}{\partial x} = \frac{\frac{\partial u}{\partial x} \cdot v - u \cdot \frac{\partial v}{\partial x}}{v^2} .\]The quotient rule. Fires on any division.
Intuition A ratio grows when its numerator grows and shrinks when its denominator grows; the two effects are combined over \(v^2\) because the denominator’s influence weakens as it gets larger. It is the product rule applied to \(u \cdot v^{-1}\).
Use it when you differentiate rates, averages, efficiencies or saturating curves like \(x/(x+1)\). The result keeps v² as written, and like the ratio itself it is undefined where \(v = 0\); no law checks that. Terms that do not cancel structurally, such as (x + 1) − x, stay in the emitted code.
differentiationsensitivity-analysis
power
law power = ∂ (u^n) / ∂ x = n · u^(n − 1) · ∂ u / ∂ x where x ∉ n
For all expressions \(u\), and all expressions \(n\) in which \(x\) does not occur,
\[\frac{\partial u^n}{\partial x} = n \cdot u^{n-1} \cdot \frac{\partial u}{\partial x} .\]The power rule with the chain rule. Fires on a power whose exponent does not mention the variable,
whether the exponent is a literal (x², x^3), a fraction or another parameter. A power such as
x^x, whose exponent mentions the variable, has no rule.
Intuition Raising \(u\) to the \(n\) amplifies a small change in \(u\) by \(n u^{n-1}\): for a square, a strip on each of two sides. The trailing \(\partial u/\partial x\) is the chain rule, so (3x + 1)^2 is handled without expanding it.
Use it when you differentiate polynomials, squared errors, inverse powers or fractional powers with a fixed exponent. The guard matters: for a variable exponent, rewrite with exp and ln (e.g. \(x^x = e^{x \ln x}\)) so exponential and logarithm apply. A result with exponent one is tidied by Arithmetic’s power_one.
differentiationoptimisationregression
sine
law sine = ∂ sin(u) / ∂ x = cos(u) · ∂ u / ∂ x
For every expression \(u\),
\[\frac{\partial \sin u}{\partial x} = \cos u \cdot \frac{\partial u}{\partial x} .\]Fires on the sine of any formula, applying the chain rule to its argument.
Intuition Sine is steepest where it crosses zero and flat at its peaks, which is exactly the shape of cosine. The factor \(\partial u/\partial x\) scales by how fast the angle itself turns, so a frequency \(\omega\) in \(\sin(\omega t)\) reappears as a factor.
Use it when you differentiate oscillations, waves, rotations or periodic features. Angles are in radians; with degrees the derivative would carry an extra \(\pi/180\). For tan, which has no rule, write sin(x) / cos(x).
differentiationsignal-processinggeometry
cosine
law cosine = ∂ cos(u) / ∂ x = −sin(u) · ∂ u / ∂ x
For every expression \(u\),
\[\frac{\partial \cos u}{\partial x} = -\sin u \cdot \frac{\partial u}{\partial x} .\]Fires on the cosine of any formula.
Intuition Cosine starts at its peak and heads down, so its slope is a negative sine; the chain-rule factor again scales by how fast the angle moves. Together with sine it closes the loop: two derivatives of either function give back its negative.
Use it when you differentiate oscillations, projections onto a direction, or velocities of circular motion. Angles are in radians. The minus sign it introduces combines with negation and Arithmetic’s neg_neg when the formula is itself negated.
differentiationsignal-processinggeometry
exponential
law exponential = ∂ exp(u) / ∂ x = exp(u) · ∂ u / ∂ x
For every expression \(u\),
\[\frac{\partial e^{u}}{\partial x} = e^{u} \cdot \frac{\partial u}{\partial x} .\]Fires on exp of any formula.
Intuition The exponential grows at a rate equal to its current size — that is what makes it the model of compounding. With the chain rule, \(e^{kx}\) changes at \(k\) times its value.
Use it when you differentiate growth and decay, softmax and logistic terms, Gaussian densities or likelihoods. Values of exp overflow quickly, so for losses prefer differentiating the logarithm of a probability rather than the probability itself.
differentiationgrowth-modellingprobabilistic-modelling
logarithm
law logarithm = ∂ ln(u) / ∂ x = ∂ u / ∂ x / u
For every expression \(u\),
\[\frac{\partial \ln u}{\partial x} = \frac{\partial u / \partial x}{u} .\]Fires on the natural logarithm of any formula. The derivative, like ln itself, is meaningful only
where \(u > 0\); no law checks that.
Intuition The log measures change in relative terms: \(u'/u\) is the growth rate per unit of size, so a 1% change reads the same whether \(u\) is 10 or 10 million. That is why log-likelihoods and log-returns have gradients that are easy to work with.
Use it when you differentiate log-likelihoods, cross-entropy, log-returns or elasticities. The rule is for the natural log ln; for another base write the change of base, ln(u) / ln(10), and the quotient rule handles the constant divisor.
differentiationprobabilistic-modellingoptimisation
root
law root = ∂ √u / ∂ x = ∂ u / ∂ x / (2√u)
For every expression \(u\),
\[\frac{\partial \sqrt{u}}{\partial x} = \frac{\partial u / \partial x}{2\sqrt{u}} .\]Fires on the square root of any formula, written with √.
Intuition A square root flattens as it grows: doubling \(u\) adds less and less to \(\sqrt{u}\), so the slope shrinks like \(1/(2\sqrt{u})\). Near zero the slope blows up, which is why a distance is not smooth at the origin.
Use it when you differentiate distances, norms, standard deviations or root-mean-square errors. The result divides by \(\sqrt{u}\), so it is infinite where \(u = 0\); no law guards that. It fires only on √; an explicit power such as u^0.5 goes through power instead.
differentiationgeometryspread
Examples
The slope of a parabola
open Prelude.Calculus
fn slopeOf(x) ↦ ∂ x² / ∂ x
using Examples;
var r = Slopes.SlopeOf(3.0);
Output (verified):
6
The generated real-valued method is the derivative, reduced:
public static double SlopeOf(double x) =>
(2d * x);
The same doorway accepts a tensor and differentiates elementwise:
using Examples;
using MLambda.Hilbert.Runtime;
var v = Slopes.SlopeOf((Tensor)new[] { 1d, 2d, 3d });
Output (verified):
[2, 4, 6]
A polynomial
sum, power, product, constant and Arithmetic’s laws together.
open Prelude.Calculus
fn cubicSlope(x) ↦ ∂ (x^3 + 5x) / ∂ x
using Examples;
var r = Slopes.CubicSlope(2.0); // 3·2² + 5
Output (verified):
17
The generated method is ((3d * System.Math.Pow(x, 2d)) + 5d).
Another parameter is a constant
Only the variable named in the denominator is differentiated; y is treated as a constant.
open Prelude.Calculus
fn linearSlope(x, y) ↦ ∂ (2x + y) / ∂ x
using Examples;
var r = Slopes.LinearSlope(5.0, 7.0);
Output (verified):
2
The generated method is the constant 2d.
The chain rule and the product rule
open Prelude.Calculus
fn waveSlope(x) ↦ ∂ sin(x²) / ∂ x
fn productSlope(x) ↦ ∂ (x · sin(x)) / ∂ x
fn growthSlope(x) ↦ ∂ exp(3x) / ∂ x
using Examples;
var a = Slopes.WaveSlope(1.0); // 2·cos(1)
var b = Slopes.ProductSlope(Math.PI); // sin(π) + π·cos(π)
var c = Slopes.GrowthSlope(0.0); // 3·e⁰
Output (verified, rounded to 6 significant digits):
1.0806
-3.14159
3
The generated methods are cos(x²) · 2x, sin(x) + x · cos(x) and exp(3x) · 3.
Quotients, logarithms and roots
open Prelude.Calculus
fn ratioSlope(x) ↦ ∂ (x / (x + 1)) / ∂ x
fn logSlope(x) ↦ ∂ ln(x² + 1) / ∂ x
fn rootSlope(x) ↦ ∂ √x / ∂ x
using Examples;
var a = Slopes.RatioSlope(1.0); // 1 / (1 + 1)²
var b = Slopes.LogSlope(1.0); // 2·1 / (1 + 1)
var c = Slopes.RootSlope(4.0); // 1 / (2·√4)
Output (verified):
0.25
1
0.25
The quotient is generated as ((x + 1) − x) / (x + 1)²: Arithmetic’s cancels matches only two
identical terms, so (x + 1) − x is left for the machine to evaluate.