autodiff
Automatic differentiation for f64 code on core's Array.
Forward mode carries derivatives alongside values: Dual (one
direction), Duals<N> (N directions per evaluation), HyperDual
(exact second derivatives) and Jet (Taylor series of any order).
Reverse mode records a Tape of Var operations and sweeps it back
once, so the gradient of f: R^n -> R costs a small multiple of one
evaluation whatever n is. A function written once over Smooth runs
in every mode. grad, jacobian, hessian, jvp, vjp and
derivatives pick the mode from the number type the function takes.
∂ and ∇ (feature ops) return a derivative in the form of what
they are given, never a value at a point: a function for a function,
samples for samples; the value is ∂(f)(x). They dispatch through
the Derive and Gradient traits, so other packages extend the same
two names. Every result is exact to rounding: no step sizes.
Pure Nabi: every target, wasm included, gets the same results.
Modules
opsCalculus notation (featureops, on by default): 2.0ε the dual number 0 + 2ε:x + 1.0εseeds a derivative ∂(f) the derivative off, inf's form: a function for a function, a spline for a spline ∂(f, n) the n-th derivative (functions overJet, splines) ∇(f) the gradient (or Jacobian) off, a function for a function; for an array of samples, the samples of the gradient∂and∇never give a value at a point: write∂(f)(x)and∇(f)(x).
Structs
DualSource
pub struct DualA dual number re + du ε with ε^2 = 0: du carries the
derivative through every operation. The arithmetic operators take a
Dual or an f64 on either side, ** takes an i32, f64 or
Dual exponent, and prefix √ is the square root; the math
functions are methods (x.sin(), x.exp()). Comparisons look at
re only.
use autodiff::Dual;
let x = Dual::var(2.0); // 2 + 1 ε
let y = 3.0 * x ** 2 + x.sin();
assert(y.re == 12.0 + 2.0f64.sin());
assert(y.du == 12.0 + 2.0f64.cos()); // d/dx (3x^2 + sin x)
The value.
The derivative part, the coefficient of ε.
re + du ε.nabiassert(autodiff::Dual::new(1.0, 2.0).du == 2.0);A constant:
x + 0 ε.nabiassert(autodiff::Dual::constant(4.0).du == 0.0);The variable to differentiate by:
x + 1 ε.nabilet x = autodiff::Dual::var(3.0); assert((x * x).du == 6.0);The square root; at 0 the derivative is infinite.
nabilet y = autodiff::Dual::var(4.0).sqrt(); assert(y.re == 2.0 && y.du == 0.25);e^x.nabilet y = autodiff::Dual::var(0.0).exp(); assert(y.re == 1.0 && y.du == 1.0);e^x - 1, accurate near 0.nabiassert(autodiff::Dual::var(0.0).exp_m1().du == 1.0);The natural logarithm.
nabiassert(autodiff::Dual::var(2.0).ln().du == 0.5);ln(1 + x), accurate near 0.nabiassert(autodiff::Dual::var(1.0).ln_1p().du == 0.5);The sine.
nabiassert(autodiff::Dual::var(0.0).sin().du == 1.0);The cosine.
nabiassert(autodiff::Dual::var(0.0).cos().du == 0.0);The tangent.
nabiassert(autodiff::Dual::var(0.0).tan().du == 1.0);The arcsine.
nabiassert(autodiff::Dual::var(0.0).asin().du == 1.0);The arccosine.
nabiassert(autodiff::Dual::var(0.0).acos().du == -1.0);The arctangent.
nabiassert(autodiff::Dual::var(1.0).atan().du == 0.5);atan2(self, x), the angle of the point(x, self).nabiuse autodiff::Dual; let a = Dual::var(1.0).atan2(Dual::constant(1.0)); assert((a.du - 0.5).abs() < 1e-15);The hyperbolic sine.
nabiassert(autodiff::Dual::var(0.0).sinh().du == 1.0);The hyperbolic cosine.
nabiassert(autodiff::Dual::var(0.0).cosh().du == 0.0);The hyperbolic tangent.
nabiassert(autodiff::Dual::var(0.0).tanh().du == 1.0);|x|; at 0 the derivative taken is 0.nabiassert(autodiff::Dual::var(-2.0).abs().du == -1.0);x^nfor an integern.nabiassert(autodiff::Dual::var(2.0).powi(3).du == 12.0);x^pfor a realp.nabiassert(autodiff::Dual::var(4.0).powf(0.5).du == 0.25);x^ywith both parts dual,exp(y ln x); needsx > 0unlessyis a constant.nabiuse autodiff::Dual; let y = Dual::constant(2.0).pow(Dual::var(3.0)); // d/dy 2^y assert((y.du - 8.0 * 2.0f64.ln()).abs() < 1e-14);sqrt(x^2 + y^2)without overflow.nabiuse autodiff::Dual; assert(Dual::var(3.0).hypot(Dual::constant(4.0)).du == 0.6);The larger by value (
selfon a tie).nabiuse autodiff::Dual; assert(Dual::var(1.0).max(Dual::constant(2.0)).du == 0.0);The smaller by value (
selfon a tie).nabiuse autodiff::Dual; assert(Dual::var(1.0).min(Dual::constant(2.0)).du == 1.0);log2 x.nabiassert(autodiff::Dual::var(2.0).log2().re == 1.0);log10 x.nabiassert(autodiff::Dual::var(10.0).log10().re == 1.0);2^x.nabiassert(autodiff::Dual::var(0.0).exp2().du == 0.6931471805599453);The cube root; at 0 the derivative is infinite.
nabiassert(autodiff::Dual::var(8.0).cbrt().du == 1.0 / 12.0);1 / x.nabiassert(autodiff::Dual::var(2.0).recip().du == -0.25);The inverse hyperbolic sine.
nabiassert(autodiff::Dual::var(0.0).asinh().du == 1.0);The inverse hyperbolic cosine (
x >= 1).nabilet d = autodiff::Dual::var(2.0).acosh().du; assert(d.is_close(1.0 / 3.0f64.sqrt()));The inverse hyperbolic tangent (
|x| < 1).nabiassert(autodiff::Dual::var(0.5).atanh().du == 1.0 / 0.75);1 / (1 + e^-x), the logistic function.nabiassert(autodiff::Dual::var(0.0).sigmoid().du == 0.25);x * y + zwith one rounding in the value.nabiuse autodiff::Dual; let y = Dual::var(2.0).mul_add(Dual::constant(3.0), Dual::constant(1.0)); assert(y.re == 7.0 && y.du == 3.0);The sign of the value (
1,-1, or NaN for NaN); derivative 0.nabiassert(autodiff::Dual::var(-3.0).signum().re == -1.0);Whether both parts are finite.
nabiassert(autodiff::Dual::var(1.0).is_finite());
Trait implementations 22
impl Display for Dualimpl Default for Dualimpl num::Zero for Dualimpl num::One for Dualimpl ops::Add for Dualimpl ops::Sub for Dualimpl ops::Mul for Dualimpl ops::Div for Dualimpl ops::Neg for Dualimpl ops::Add<f64> for Dualimpl ops::Sub<f64> for Dualimpl ops::Mul<f64> for Dualimpl ops::Div<f64> for Dualimpl ops::AddAssign for Dualimpl ops::SubAssign for Dualimpl ops::MulAssign for Dualimpl ops::Pow<i32> for Dualimpl ops::Pow<f64> for Dualimpl ops::Pow<Dual> for Dualimpl ops::Sqrt for Dualimpl PartialOrd for Dualimpl ops::DivAssign for Dual
HyperDualSource
pub struct HyperDualre + e1 ε1 + e2 ε2 + e12 ε1ε2. The operators and methods are those
of Dual; e12 carries the mixed second derivative.
use autodiff::HyperDual;
let x = HyperDual::new(2.0, 1.0, 1.0, 0.0);
let y = x * x * x;
assert(y.re == 8.0 && y.e1 == 12.0 && y.e12 == 12.0); // f, f', f''
The value.
The ε1 part: the derivative along the first direction.
The ε2 part: the derivative along the second direction.
The ε1ε2 part: the mixed second derivative.
re + e1 ε1 + e2 ε2 + e12 ε1ε2.A constant.
nabiassert(autodiff::HyperDual::constant(3.0).e12 == 0.0);The variable seeded in both directions:
f(var(x)).e12is f''(x).nabilet y = autodiff::HyperDual::var(0.0).sin(); assert(y.e1 == 1.0 && y.e12 == 0.0);The square root.
The cube root.
e^x.e^x - 1, accurate near 0.2^x.The natural logarithm.
ln(1 + x), accurate near 0.log2 x.log10 x.The sine.
The cosine.
The tangent.
The arcsine.
The arccosine.
The arctangent.
The hyperbolic sine.
The hyperbolic cosine.
The hyperbolic tangent.
The inverse hyperbolic sine.
The inverse hyperbolic cosine (
x >= 1).The inverse hyperbolic tangent (
|x| < 1).1 / x.1 / (1 + e^-x).|x|; at 0 the derivatives taken are 0.x^nfor an integern.x^pfor a realp.x^y,exp(y ln x).sqrt(x^2 + y^2).The larger by value.
The smaller by value.
Whether every part is finite.
Trait implementations 21
impl Display for HyperDualimpl Default for HyperDualimpl num::Zero for HyperDualimpl num::One for HyperDualimpl ops::Add for HyperDualimpl ops::Sub for HyperDualimpl ops::Mul for HyperDualimpl ops::Div for HyperDualimpl ops::Neg for HyperDualimpl ops::Add<f64> for HyperDualimpl ops::Sub<f64> for HyperDualimpl ops::Mul<f64> for HyperDualimpl ops::Div<f64> for HyperDualimpl ops::AddAssign for HyperDualimpl ops::SubAssign for HyperDualimpl ops::MulAssign for HyperDualimpl ops::DivAssign for HyperDualimpl ops::Pow<i32> for HyperDualimpl ops::Pow<f64> for HyperDualimpl ops::Sqrt for HyperDualimpl PartialOrd for HyperDual
JetSource
pub struct JetA truncated Taylor series c[0] + c[1] t + .. + c[K] t^K. Arithmetic
with another Jet (the lower order of the two wins) or an f64 on
either side (an order-0 jet acts as a constant), ** with an i32 or f64 exponent, prefix √,
comparisons by value and the elementary functions as methods.
use autodiff::Jet;
let x = Jet::var(0.0, 4);
let d = x.sin().derivatives(); // sin, cos, -sin, -cos, sin at 0
assert(d.to_list() == [0.0, 1.0, 0.0, -1.0, 0.0]);
The variable
x + tup to orderk.nabiassert(autodiff::Jet::var(2.0, 3).coefficients() == [2.0, 1.0, 0.0, 0.0]);The constant
xup to orderk.The value,
c[0].The order
K.The Taylor coefficients
c[0] .. c[K].The derivatives
f(x), f'(x), .., f^(K)(x):k! c[k].nabilet x = autodiff::Jet::var(1.0, 3); assert((x * x * x).derivatives().to_list() == [1.0, 3.0, 6.0, 6.0]);The
k-th derivative, 0 above the order.e^x.The natural logarithm.
The square root.
The sine.
The cosine.
The tangent.
The hyperbolic sine.
The hyperbolic cosine.
The hyperbolic tangent.
The arctangent.
The arcsine.
The arccosine.
|x|, by the sign of the value; at 0 the zero series.x^pfor a realp; needsx != 0unlesspis a whole number.x^nfor an integern, by squaring.Whether the value is finite.
Trait implementations 15
impl Debug for Jetimpl PartialEq for Jetimpl PartialOrd for Jetimpl ops::Add for Jetimpl ops::Sub for Jetimpl ops::Mul for Jetimpl ops::Div for Jetimpl ops::Neg for Jetimpl ops::Add<f64> for Jetimpl ops::Sub<f64> for Jetimpl ops::Mul<f64> for Jetimpl ops::Div<f64> for Jetimpl ops::Pow<i32> for Jetimpl ops::Pow<f64> for Jetimpl ops::Sqrt for Jet
DualsSource
pub struct Duals<const N: usize>re + du[0] ε_0 + .. + du[N-1] ε_{N-1} with every ε_i ε_j = 0: a
dual number carrying N directional derivatives. The operators and
methods are those of Dual.
use autodiff::Duals;
let x: Duals<2> = Duals::var(3.0, 0);
let y: Duals<2> = Duals::var(4.0, 1);
let r = (x * x + y * y).sqrt();
assert(r.re == 5.0 && r.du[0].is_close(0.6) && r.du[1].is_close(0.8));
The value.
The derivative parts.
A constant: every derivative part 0.
nabilet c: autodiff::Duals<3> = autodiff::Duals::constant(2.0); assert(c.du == [0.0, 0.0, 0.0]);The variable of lane
k: derivative partkis 1, the others 0 (k >= Nseeds none).nabilet x: autodiff::Duals<2> = autodiff::Duals::var(1.0, 1); assert(x.du == [0.0, 1.0]);The square root.
The cube root.
e^x.e^x - 1, accurate near 0.2^x.The natural logarithm.
ln(1 + x), accurate near 0.log2 x.log10 x.The sine.
The cosine.
The tangent.
The arcsine.
The arccosine.
The arctangent.
The hyperbolic sine.
The hyperbolic cosine.
The hyperbolic tangent.
The inverse hyperbolic sine.
The inverse hyperbolic cosine (
x >= 1).The inverse hyperbolic tangent (
|x| < 1).1 / x.1 / (1 + e^-x), the logistic function.|x|; at 0 the derivative taken is 0.x^nfor an integern.x^pfor a realp.x^y,exp(y ln x).sqrt(x^2 + y^2)without overflow.atan2(self, x).The larger by value.
The smaller by value.
Whether every part is finite.
Trait implementations 21
impl<const N: usize> Display for Duals<N>impl<const N: usize> Default for Duals<N>impl<const N: usize> num::Zero for Duals<N>impl<const N: usize> num::One for Duals<N>impl<const N: usize> ops::Add for Duals<N>impl<const N: usize> ops::Sub for Duals<N>impl<const N: usize> ops::Mul for Duals<N>impl<const N: usize> ops::Div for Duals<N>impl<const N: usize> ops::Neg for Duals<N>impl<const N: usize> ops::Add<f64> for Duals<N>impl<const N: usize> ops::Sub<f64> for Duals<N>impl<const N: usize> ops::Mul<f64> for Duals<N>impl<const N: usize> ops::Div<f64> for Duals<N>impl<const N: usize> ops::AddAssign for Duals<N>impl<const N: usize> ops::SubAssign for Duals<N>impl<const N: usize> ops::MulAssign for Duals<N>impl<const N: usize> ops::DivAssign for Duals<N>impl<const N: usize> ops::Pow<i32> for Duals<N>impl<const N: usize> ops::Pow<f64> for Duals<N>impl<const N: usize> ops::Sqrt for Duals<N>impl<const N: usize> PartialOrd for Duals<N>
AdjointsSource
pub struct AdjointsThe derivatives of one output toward everything on its tape, from
Var::backward.
d out / d x; 0 for a constant or aVarthe output does not depend on.nabilet t = autodiff::Tape::new(); let (x, y) = (t.var(1.0), t.var(5.0)); let d = (x * 4.0).backward(); assert(d.wrt(x) == 4.0 && d.wrt(y) == 0.0);d out / d x[i]for each input.nabilet t = autodiff::Tape::new(); let x = t.vars([1.0, 2.0]); let g = (x[0] * x[1]).backward().wrt_all(x); assert(g[0] == 2.0 && g[1] == 1.0);
TapeSource
pub struct TapeA tape to record operations on: make the inputs with var, compute
with them, then call backward on the result.
use autodiff::Tape;
let t = Tape::new();
let x = t.var(2.0);
let y = t.var(3.0);
let z = x * y + x.sin();
let d = z.backward();
assert(d.wrt(x) == 3.0 + 2.0f64.cos() && d.wrt(y) == 2.0);
An empty tape.
A new input with value
x.The inputs
x[0],x[1], ... in order.nabilet t = autodiff::Tape::new(); let x = t.vars([1.0, 2.0]); assert(x.len() == 2 && x[1].value() == 2.0);The number of nodes recorded.
nabilet t = autodiff::Tape::new(); let x = t.var(1.0); let _ = x * x; assert(t.len() == 2);Whether nothing is recorded.
Trait implementations 1
impl Clone for Tape
VarSource
pub struct VarA number recorded on a Tape. Arithmetic with another Var or an
f64 on either side, ** with an i32, f64 or Var exponent,
prefix √, comparisons by value and the elementary functions as
methods, as Dual has. Every Var of one computation must come from
one tape; constants (Var::constant) belong to none.
A constant: no tape, derivative 0.
nabilet c = autodiff::Var::constant(2.0); assert(c.value() == 2.0);The value.
The derivatives of
selftoward everyVarof its tape: one sweep fromselfback to the inputs, O(tape length).nabilet t = autodiff::Tape::new(); let x = t.var(3.0); assert((x * x * x).backward().wrt(x) == 27.0);The square root.
The cube root.
e^x.nabilet t = autodiff::Tape::new(); let x = t.var(0.0); assert(x.exp().backward().wrt(x) == 1.0);e^x - 1, accurate near 0.2^x.The natural logarithm.
ln(1 + x), accurate near 0.log2 x.log10 x.The sine.
The cosine.
The tangent.
The arcsine.
The arccosine.
The arctangent.
atan2(self, x), the angle of the point(x, self).The hyperbolic sine.
The hyperbolic cosine.
The hyperbolic tangent.
The inverse hyperbolic sine.
The inverse hyperbolic cosine (
x >= 1).The inverse hyperbolic tangent (
|x| < 1).1 / (1 + e^-x).1 / x.|x|; at 0 the derivative taken is 0.x^nfor an integern.x^pfor a realp.x^y,exp(y ln x); needsx > 0unlessyis a constant.sqrt(x^2 + y^2)without overflow.The larger by value (
selfon a tie).The smaller by value (
selfon a tie).Whether the value is finite.
Trait implementations 25
impl Clone for Varimpl Display for Varimpl Debug for Varimpl PartialEq for Varimpl PartialOrd for Varimpl Default for Varimpl num::Zero for Varimpl num::One for Varimpl ops::Add for Varimpl ops::Sub for Varimpl ops::Mul for Varimpl ops::Div for Varimpl ops::Neg for Varimpl ops::Add<f64> for Varimpl ops::Sub<f64> for Varimpl ops::Mul<f64> for Varimpl ops::Div<f64> for Varimpl ops::AddAssign for Varimpl ops::SubAssign for Varimpl ops::MulAssign for Varimpl ops::DivAssign for Varimpl ops::Pow<i32> for Varimpl ops::Pow<f64> for Varimpl ops::Pow<Var> for Varimpl ops::Sqrt for Var
Traits
ScalarFnSource
pub trait ScalarFnA function R^n -> R written over an AD number: what grad and
value_grad take. fn(Vector<Dual>) -> Dual costs n evaluations,
fn(Vector<Duals<N>>) -> Duals<N> (N = 2, 4, 8 or 16) costs ceil(n / N), and
fn(Vector<Var>) -> Var one evaluation and one backward sweep
(reverse mode: the choice for many inputs).
The value and the gradient at
x.
VectorFnSource
pub trait VectorFnA function R^n -> R^m written over an AD number: what jacobian
takes. Dual builds the Jacobian by columns (n evaluations),
Duals<N> by N columns at once, Var by rows (one evaluation, m
backward sweeps).
The value and the
m x nJacobian atx.
SmoothSource
pub trait Smooth: Clone + ops::Add<Output = Self> + ops::Sub<Output = Self> + ops::Mul<Output = Self>
+ ops::Div<Output = Self> + ops::Neg<Output = Self> + ops::Add<f64, Output = Self> + ops::Sub<f64, Output = Self>
+ ops::Mul<f64, Output = Self> + ops::Div<f64, Output = Self>A number a function is written over: f64, Dual, Duals<N>,
HyperDual, Jet, Var or Complex<f64>. It has the arithmetic operators, with an f64 on
the right too (x * 2.0), and the elementary functions; write an
f64 constant on the left as T::of(c). For Complex<f64>, abs
keeps the sign of the real part, so the complex step still
differentiates it; in a function generic over T: Smooth,
x.abs() is this method, not core's modulus.
use autodiff::{Dual, Smooth, Var};
use core::array::Vector;
fn rosen<T: Smooth>(x: Vector<T>) -> T {
let a = x[1] - x[0] * x[0];
let b = T::of(1.0) - x[0];
a * a * 100.0 + b * b
}
let x: Vector<f64> = [1.0, 1.0];
assert(rosen(x) == 0.0);
let g = autodiff::grad(rosen::<Dual>, [-1.0, 1.0]);
assert(g[0] == -4.0 && g[1] == 0.0);
assert(autodiff::grad(rosen::<Var>, [-1.0, 1.0]) == g);
xas this type, a constant.The real value.
The square root.
e^x.The natural logarithm.
The sine.
The cosine.
The tangent.
The arctangent.
The hyperbolic sine.
The hyperbolic cosine.
The hyperbolic tangent.
The absolute value.
x^n.x^p.
Functions
derivativeSource
pub fn derivative(f: fn(Dual) -> Dual, x: f64) -> f64 f'(x) by one evaluation of f at x + ε.
use autodiff::Dual;
let d = autodiff::derivative(|x: Dual| -> Dual { x.exp() * x }, 1.0);
assert(d.is_close(2.0 * 1.0f64.exp()));
derivativesSource
pub fn derivatives(f: fn(Jet) -> Jet, x: f64, n: usize) -> Vector<f64> f(x), f'(x), .., f^(n)(x) by one Taylor-mode evaluation of f over
jets of order n, O(n^2) per operation.
use autodiff::Jet;
let d = autodiff::derivatives(|x: Jet| -> Jet { x.exp() * x }, 0.0, 3);
assert(d.to_list() == [0.0, 1.0, 2.0, 3.0]); // (x + k) e^x at 0
gradSource
pub fn grad<F: ScalarFn, X: AsVector<f64>>(f: F, x: X) -> Vector<f64> The gradient of f: R^n -> R at x.
use autodiff::{Duals, Var};
use core::array::Vector;
let g = autodiff::grad(|x: Vector<Var>| -> Var { x[0] ** 2 + 3.0 * x[1] }, [1.0, 2.0]);
assert(g[0] == 2.0 && g[1] == 3.0);
let h = autodiff::grad(|x: Vector<Duals<4>>| -> Duals<4> { x[0] ** 2 + 3.0 * x[1] }, [1.0, 2.0]);
assert(h == g);
hessianSource
pub fn hessian<X: AsVector<f64>>(f: fn(Vector<HyperDual>) -> HyperDual, x: X) -> Matrix<f64> The Hessian of f: R^n -> R at x by hyper-dual numbers: entry
(i, j) from one evaluation seeded ε1 in x[i] and ε2 in x[j],
n (n + 1) / 2 evaluations, exact to rounding, symmetric.
use autodiff::HyperDual;
use core::array::Vector;
let h = autodiff::hessian(|x: Vector<HyperDual>| -> HyperDual { x[0] * x[0] * x[1] }, [3.0, 2.0]);
assert(h[0, 0] == 4.0 && h[0, 1] == 6.0 && h[1, 0] == 6.0 && h[1, 1] == 0.0);
jacobianSource
pub fn jacobian<F: VectorFn, X: AsVector<f64>>(f: F, x: X) -> Matrix<f64> The m x n Jacobian of f: R^n -> R^m at x (see VectorFn).
use autodiff::{Dual, Var};
use core::array::{Array, Vector};
let f = |x: Vector<Dual>| -> Vector<Dual> { Array::from([x[0] * x[1], x[0] + x[1]]) };
let j = autodiff::jacobian(f, [2.0, 5.0]);
assert(j[0, 0] == 5.0 && j[0, 1] == 2.0 && j[1, 1] == 1.0);
let r = |x: Vector<Var>| -> Vector<Var> { Array::from([x[0] * x[1], x[0] + x[1]]) };
assert(autodiff::jacobian(r, [2.0, 5.0]) == j);
jvpSource
pub fn jvp<X: AsVector<f64>, V: AsVector<f64>>(f: fn(Vector<Dual>) -> Vector<Dual>, x: X, v: V) -> Vector<f64> The Jacobian-vector product J(x) v by one forward evaluation, the
directional derivative of f along v.
use autodiff::Dual;
use core::array::{Array, Vector};
let f = |x: Vector<Dual>| -> Vector<Dual> { Array::from([x[0] * x[1], x[0].sin()]) };
let u = autodiff::jvp(f, [2.0, 3.0], [1.0, 1.0]);
assert(u[0] == 5.0 && u[1] == 2.0f64.cos());
seedSource
pub fn seed(x: Vector<f64>, k: usize) -> Vector<Dual> x as dual numbers with the derivative part of element k set to 1,
the others 0 (k >= x.len() seeds none).
use core::array::Vector;
let x: Vector<f64> = [1.0, 2.0];
assert(autodiff::seed(x, 1)[1].du == 1.0 && autodiff::seed(x, 1)[0].du == 0.0);
value_gradSource
pub fn value_grad<F: ScalarFn, X: AsVector<f64>>(f: F, x: X) -> (f64, Vector<f64>) The value and gradient of f: R^n -> R at x (see ScalarFn for
the cost of each number type).
use autodiff::{Dual, Var};
use core::array::Vector;
let (v, g) = autodiff::value_grad(|x: Vector<Dual>| -> Dual { x[0] * x[1] + x[0].sin() }, [0.0, 3.0]);
assert(v == 0.0 && g[0] == 4.0 && g[1] == 0.0);
let (w, h) = autodiff::value_grad(|x: Vector<Var>| -> Var { x[0] * x[1] + x[0].sin() }, [0.0, 3.0]);
assert(w == v && h == g);
value_jacobianSource
pub fn value_jacobian<F: VectorFn, X: AsVector<f64>>(f: F, x: X) -> (Vector<f64>, Matrix<f64>) The value and Jacobian of f at x.
vjpSource
pub fn vjp<X: AsVector<f64>, W: AsVector<f64>>(f: fn(Vector<Var>) -> Vector<Var>, x: X, w: W) -> Vector<f64> The vector-Jacobian product w^T J(x) by one evaluation and one
backward sweep: the gradient of w . f.
use autodiff::Var;
use core::array::{Array, Vector};
let f = |x: Vector<Var>| -> Vector<Var> { Array::from([x[0] * x[1], x[0].sin()]) };
let u = autodiff::vjp(f, [2.0, 3.0], [1.0, 0.0]);
assert(u[0] == 3.0 && u[1] == 2.0);