circuits-diff
Safe HaskellNone
LanguageGHC2024

Circuit.Diff.Operators

Description

Eshkol-style AD operators on top of the Diff carrier.

These are convenience wrappers around 'runDiff. They expose a JAX-like surface --- derivative, gradient, jacobian, hessian, divergence, curl, laplacian --- while keeping the substrate's exact reverse-mode engine underneath for first-order operators.

Higher-order scalar towers now use the Taylor carrier: a Diff function is sampled near the expansion point and a truncated Taylor morphism is built from the finite-difference table. This is an approximation (the exact tower would require a dedicated forward-mode or Taylor-mode carrier from the start), but it is enough to make derivativeN, taylor, hessian and laplacian usable.

Synopsis

First-order operators

derivative :: forall {k} (p :: k). Diff p Double Double -> Double -> Double Source #

Scalar derivative: f : R -> R at x.

>>> let sq = Diff (\x -> (x * x, \d -> 2 * x * d)) :: Diff () Double Double
>>> derivative sq 3.0
6.0

gradient :: forall {k} (p :: k). Diff p [Double] Double -> [Double] -> [Double] Source #

Gradient of a scalar function: f : R^n -> R at v.

Returns the vector ∇f(v) by applying the pullback to the unit scalar.

jacobian :: forall {k} (p :: k). Diff p [Double] [Double] -> [Double] -> [[Double]] Source #

Jacobian of a vector function: f : R^n -> R^m at v.

Returns an m × n matrix: outer index is output component, inner index is input component. Each row is obtained by applying the pullback to one output basis vector.

Second-order operators

hessian :: forall {k} (p :: k). Diff p [Double] Double -> [Double] -> [[Double]] Source #

Hessian of a scalar function: f : R^n -> R at v.

Implemented by central second-order finite differences; approximate.

divergence :: forall {k} (p :: k). Diff p [Double] [Double] -> [Double] -> Double Source #

Trace of the Jacobian: div f v = Σ_i ∂f_i/∂x_i.

curl :: forall {k} (p :: k). Diff p [Double] [Double] -> [Double] -> [Double] Source #

Curl of a 3-D vector field: f : R^3 -> R^3 at v.

laplacian :: forall {k} (p :: k). Diff p [Double] Double -> [Double] -> Double Source #

Laplacian of a scalar function: f : R^n -> R at v.

Trace of the finite-difference Hessian.

Higher-order towers (finite-difference bridge from Diff)

derivativeN :: forall {k} (p :: k). Diff p Double Double -> Double -> Int -> Double Source #

N-th derivative of a scalar function: f : R -> R at x.

For n <= 1 this is exact reverse-mode AD. Higher orders are read from a finite-difference Taylor tower built with Taylor.

taylor :: forall {k} (p :: k). Diff p Double Double -> Double -> Int -> [Double] Source #

First k raw derivatives of f : R -> R at x0.

The result is [f(x0), f'(x0), f''(x0), ..., f^(k)(x0)]. The constant and linear terms are exact; higher terms come from a finite-difference Taylor tower built with Taylor.

Exact higher-order towers (compositional Jet)

derivativeNJ :: (Jet Double -> Jet Double) -> Double -> Int -> Double Source #

Exact N-th derivative of a scalar function expressed as a compositional Jet tower.

The function must be built from NumHask-polymorphic operations; the tower is propagated by the closed recurrences in Jet. There is no finite-difference approximation.

taylorJ :: (Jet Double -> Jet Double) -> Double -> Int -> [Double] Source #

Exact first k raw derivatives via a compositional Jet tower.