Dual #
Dual numbers for forward-mode differentiation.
This runtime-oriented Dual α scalar can be used to compute:
- Jacobian-vector products by running a function once with dual inputs, and
- Hessian-vector products by running reverse-mode over dual scalars (forward-over-reverse).
The rules follow the primal branch. Absolute value and guarded square root select zero tangent at zero; minimum and maximum split ties equally. ReLU separately selects zero at its kink, and tensor clamp selects zero tangent at either endpoint. These choices agree with the recorded graph rules.
detach removes the tangent and recursively clears differentiation metadata from the primal,
so nested dual computations respect the same boundary. A power with a constant exponent uses
$yx^{y-1}x'$, without evaluating a logarithm. A varying exponent uses
$x^y(y'\log x+yx'/x)$ on its domain. Mathematical derivative theorems state their domain
assumptions in the separate fderiv proof layer.
Compensated log1p and expm1 use analytic chain rules rather than differentiating their
rounding-dependent branches. This also preserves higher derivatives when dual numbers are nested.
A dual number $x+\varepsilon\,dx$ used for forward-mode automatic differentiation.
re is the primal value and du is the tangent (directional derivative). When you run a
computation once on dual inputs, the resulting du component computes a Jacobian-vector product.
- re : α
Primal part: the value the ordinary computation would have produced.
- du : α
Tangent part, the coefficient of
ε. Truncating atε² = 0is what makes the arithmetic rules below exactly the chain rule.
Instances For
Instances For
Project the primal component of a dual number.
Instances For
Project the tangent component of a dual number.
Instances For
Embed a primal value as a dual number with zero tangent.
Instances For
Evaluate a complete quotient layer while retaining all enclosed range flags.
Instances For
Lift checked arithmetic through both coefficients without discarding inner range flags.
Division uses the quotient rule $(x/y)'=(x'y-xy')/y^2$ at every nesting depth.
For native binary32/64 coefficient trees, ordinary operations retain this evaluation order. If any enclosed operation exposes a nonfinite or subnormal value, including a nonzero product or quotient rounded to zero, we replay the complete quotient over exact rational coefficients. Every final coefficient is then rounded independently to its native format. A representable mixed derivative can therefore survive an intermediate overflow or underflow at an inner depth. Replay requires all input coefficients finite and the denominator's scalar primal nonzero. Otherwise the native quotient-rule result is retained. An unrepresentable exact coefficient still rounds to infinity or zero; coefficients are never clamped or deleted.
The scope is one quotient of the given coefficient trees. Rounding before entry is irreversible; ordinary rounding and cancellation outside a detected range event retain their native semantics. Carriers without checked coefficient arithmetic retain the previous operational rearrangement.
Take the larger primal and its tangent. Equal primals split the tangent equally between the two arguments, matching the graph's elementwise maximum rule. The shared average scales before adding and replays finite coefficients if halving loses range. Neither an overflowing raw sum nor two rounded-zero halves should erase a representable average.
Elementwise minimum uses the same equal split at a tie as the graph's minimum rule.
Coerce naturals into dual numbers with zero tangent.
Forward-mode chain rule implementations for MathFunctions over dual numbers.
Power rule for x^y over dual numbers.
If the exponent tangent is identically zero, use $yx^{y-1}x'$ instead of evaluating an unused logarithm or dividing by the base. This includes polynomial derivatives at zero and negative inputs. The zero test inspects all nested coefficients, not just their primals. A varying exponent uses the logarithmic rule on the scalar backend's domain. When its arithmetic loses range, a bounded positive-base enclosure combines both directions before rounding.
Lift a scalar Context to dual numbers by deciding comparisons on primals.
Differentiate the polar angle in real coordinates away from the origin and branch cut.
Scaling both coordinates avoids squaring a large magnitude. At the origin we select zero tangent, where the angle has no classical derivative. The primal retains the component backend's signed-zero and branch-cut convention. The shared tangent rule combines the original finite coefficients if scaled seeds lose range or their products cancel.
Map a tensor of primals to a tensor of duals with zero tangents.
Instances For
Combine a primal tensor and a tangent tensor into a dual tensor.
This is the tensor-level analogue of Dual.mk.
Instances For
Project the tangent part of a dual tensor.