forward design
This page explains what the scalar drivers diff and value_and_diff
compute, what they cost, and why they are plain functions over
Dual[T] -> Dual[T].
Design goal
Give the common case, the derivative of a function of one variable, a
one-line API that hides the seeding of the variable, while keeping all
semantics in Dual[T] so that the drivers cannot disagree with the type.
Constraints
- A MoonBit closure has one concrete type, so a driver can only take a
function that is already written for
Dual[T]. - The drivers must not depend on containers, so that scalar users do not
pull in
linear-algebraorluna-poly.
Mathematical background
Forward mode
A program computes through intermediates , each an operation on earlier ones. Forward mode carries, next to every , its derivative , and updates it with the chain rule in the same order as the program:
Dual arithmetic performs exactly this update: is
Dual::new(v_k, dv_k), and the rules of the
dual design are the partial
derivatives of each . Seeding is
Dual::variable(x).
Higher derivatives by nesting
If f is generic in its scalar, it can be applied to Dual[Dual[T]].
Write for the outer perturbation (the tangent of
Dual[T]) and for the inner one (the tangent of
Dual[Dual[T]]), with and
. The outer
diff seeds ; the inner diff seeds that number as its
variable, so f runs on . Applying the
first-order identity twice,
The inner diff returns the coefficient of , which is
, and the outer diff returns its
coefficient, so
The two perturbations live at different nesting levels, so they cannot be
mixed up without an explicit conversion: a value of the outer level enters
the inner computation only through Dual::constant, which gives it inner
tangent zero. This rules out the classic perturbation confusion of
untyped nested forward mode, where the inner derivative picks up the outer
perturbation.11 J. M. Siskind and B. A. Pearlmutter, “Perturbation confusion and
referential transparency”, IFL 2005, describes the problem for untyped
implementations.
Nesting is expensive. Level stores components of T. If a
product at level costs multiplications and additions of
T, then and , because a dual
product makes three products and one addition one level down, and an
addition at level costs additions. With and
,
so the -th derivative of a multiplication-heavy program costs about times a plain evaluation, and many of the components are duplicates (both first-order components hold ). A truncated Taylor series would need only components and work per product.
Design decisions
Functions, not a trait or a wrapper type
Problem. How should a user hand a function to the driver?
Options. A trait implemented by a user type; a function on Double
plus a separate derivative; a closure over Dual[T].
Choice. A closure (Dual[T]) -> Dual[T]. MoonBit closures are cheap,
the type makes it explicit that the function must be written for dual
numbers, and generic functions instantiate at Dual[T] without wrappers.
Minimal bound
value_and_diff needs only T : One, for the seed tangent. Everything
else is required by the operations inside f, where the compiler checks it
at the call site.
Value from the same evaluation
The value is read from the same dual evaluation as the derivative. By the
projection homomorphism it
is bit-for-bit the value f would compute on T, so no second evaluation
is needed.
Location in its own package
The drivers live in forward so that the dual package stays a pure
number type, and the root package re-exports them. forward depends only on
dual and luna-generic.
Correctness and invariants
value_and_diff(f, x) == (f(variable(x)).value(), f(variable(x)).tangent())anddiff(f, x)is its second component.- For a program of ring operations, divisions with non-zero divisors and the elementary functions in their domains, the result is computed with the rounding bounds of the dual design.
- The cost is one evaluation of
fonDual[T], a small constant factor over one evaluation onT.
Alternatives rejected
- Numerical differentiation inside the driver. Rejected for the accuracy reasons derived in the dual design.
- A
diff_nfor the -th derivative. Nesting already gives it for generic functions, and a truncated Taylor type would be the efficient solution; neither is a reason for a second API today.
Boundaries
- Scalar input only. Several inputs are handled by the
linalg drivers or by seeding
Dual::newby hand. - No checked variant:
fdecides whether it uses checked operations, and a checkedfreturns its ownResult. - No higher-order driver and no reverse mode.
Footnotes
-
J. M. Siskind and B. A. Pearlmutter, “Perturbation confusion and referential transparency”, IFL 2005, describes the problem for untyped implementations. ↩