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. The power rule uses
$d(x^y)=x^y(y'\log x+yx'/x)$ and requires the logarithm and quotient to be defined. Mathematical
derivative theorems state their domain assumptions in the separate fderiv proof layer.
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
Convenience constructor using the (primal, tangent) order.
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. Each tangent is scaled before the sum: adding two large finite tangents first can overflow even when their average is representable.
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.
We use the standard identity $d(x^y)=x^y(y'\log x+yx'/x)$, which is mathematically justified only on domains where the right-hand side is defined.
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.
Map a tensor of primals to a tensor of duals with zero tangents.
Instances For
Lift a tensor pack to dual numbers 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
Apply withTangents pointwise to a tensor pack.
Instances For
Project the tangent part of a dual tensor.
Instances For
Extract every tangent in a tensor pack.