packages

autodiff

On this page

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

  • ops Calculus notation (feature ops, on by default): 2.0ε the dual number 0 + 2ε: x + 1.0ε seeds a derivative ∂(f) the derivative of f, in f's form: a function for a function, a spline for a spline ∂(f, n) the n-th derivative (functions over Jet, splines) ∇(f) the gradient (or Jacobian) of f, 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 Dual

A 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.

nabi
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)
  • pub re: f64§

    Source

    The value.

  • pub du: f64§

    Source

    The derivative part, the coefficient of ε.

  • pub const fn new(re: f64, du: f64) -> Dual§

    Source

    re + du ε.

    nabi
    assert(autodiff::Dual::new(1.0, 2.0).du == 2.0);
    
  • pub const fn constant(x: f64) -> Dual§

    Source

    A constant: x + 0 ε.

    nabi
    assert(autodiff::Dual::constant(4.0).du == 0.0);
    
  • pub const fn var(x: f64) -> Dual§

    Source

    The variable to differentiate by: x + 1 ε.

    nabi
    let x = autodiff::Dual::var(3.0);
    assert((x * x).du == 6.0);
    
  • pub fn sqrt(self) -> Dual§

    Source

    The square root; at 0 the derivative is infinite.

    nabi
    let y = autodiff::Dual::var(4.0).sqrt();
    assert(y.re == 2.0 && y.du == 0.25);
    
  • pub fn exp(self) -> Dual§

    Source

    e^x.

    nabi
    let y = autodiff::Dual::var(0.0).exp();
    assert(y.re == 1.0 && y.du == 1.0);
    
  • pub fn exp_m1(self) -> Dual§

    Source

    e^x - 1, accurate near 0.

    nabi
    assert(autodiff::Dual::var(0.0).exp_m1().du == 1.0);
    
  • pub fn ln(self) -> Dual§

    Source

    The natural logarithm.

    nabi
    assert(autodiff::Dual::var(2.0).ln().du == 0.5);
    
  • pub fn ln_1p(self) -> Dual§

    Source

    ln(1 + x), accurate near 0.

    nabi
    assert(autodiff::Dual::var(1.0).ln_1p().du == 0.5);
    
  • pub fn sin(self) -> Dual§

    Source

    The sine.

    nabi
    assert(autodiff::Dual::var(0.0).sin().du == 1.0);
    
  • pub fn cos(self) -> Dual§

    Source

    The cosine.

    nabi
    assert(autodiff::Dual::var(0.0).cos().du == 0.0);
    
  • pub fn tan(self) -> Dual§

    Source

    The tangent.

    nabi
    assert(autodiff::Dual::var(0.0).tan().du == 1.0);
    
  • pub fn asin(self) -> Dual§

    Source

    The arcsine.

    nabi
    assert(autodiff::Dual::var(0.0).asin().du == 1.0);
    
  • pub fn acos(self) -> Dual§

    Source

    The arccosine.

    nabi
    assert(autodiff::Dual::var(0.0).acos().du == -1.0);
    
  • pub fn atan(self) -> Dual§

    Source

    The arctangent.

    nabi
    assert(autodiff::Dual::var(1.0).atan().du == 0.5);
    
  • pub fn atan2(self, x: Dual) -> Dual§

    Source

    atan2(self, x), the angle of the point (x, self).

    nabi
    use autodiff::Dual;
    
    let a = Dual::var(1.0).atan2(Dual::constant(1.0));
    assert((a.du - 0.5).abs() < 1e-15);
    
  • pub fn sinh(self) -> Dual§

    Source

    The hyperbolic sine.

    nabi
    assert(autodiff::Dual::var(0.0).sinh().du == 1.0);
    
  • pub fn cosh(self) -> Dual§

    Source

    The hyperbolic cosine.

    nabi
    assert(autodiff::Dual::var(0.0).cosh().du == 0.0);
    
  • pub fn tanh(self) -> Dual§

    Source

    The hyperbolic tangent.

    nabi
    assert(autodiff::Dual::var(0.0).tanh().du == 1.0);
    
  • pub fn abs(self) -> Dual§

    Source

    |x|; at 0 the derivative taken is 0.

    nabi
    assert(autodiff::Dual::var(-2.0).abs().du == -1.0);
    
  • pub fn powi(self, n: i32) -> Dual§

    Source

    x^n for an integer n.

    nabi
    assert(autodiff::Dual::var(2.0).powi(3).du == 12.0);
    
  • pub fn powf(self, p: f64) -> Dual§

    Source

    x^p for a real p.

    nabi
    assert(autodiff::Dual::var(4.0).powf(0.5).du == 0.25);
    
  • pub fn pow(self, y: Dual) -> Dual§

    Source

    x^y with both parts dual, exp(y ln x); needs x > 0 unless y is a constant.

    nabi
    use 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);
    
  • pub fn hypot(self, y: Dual) -> Dual§

    Source

    sqrt(x^2 + y^2) without overflow.

    nabi
    use autodiff::Dual;
    
    assert(Dual::var(3.0).hypot(Dual::constant(4.0)).du == 0.6);
    
  • pub fn max(self, o: Dual) -> Dual§

    Source

    The larger by value (self on a tie).

    nabi
    use autodiff::Dual;
    
    assert(Dual::var(1.0).max(Dual::constant(2.0)).du == 0.0);
    
  • pub fn min(self, o: Dual) -> Dual§

    Source

    The smaller by value (self on a tie).

    nabi
    use autodiff::Dual;
    
    assert(Dual::var(1.0).min(Dual::constant(2.0)).du == 1.0);
    
  • pub fn log2(self) -> Dual§

    Source

    log2 x.

    nabi
    assert(autodiff::Dual::var(2.0).log2().re == 1.0);
    
  • pub fn log10(self) -> Dual§

    Source

    log10 x.

    nabi
    assert(autodiff::Dual::var(10.0).log10().re == 1.0);
    
  • pub fn exp2(self) -> Dual§

    Source

    2^x.

    nabi
    assert(autodiff::Dual::var(0.0).exp2().du == 0.6931471805599453);
    
  • pub fn cbrt(self) -> Dual§

    Source

    The cube root; at 0 the derivative is infinite.

    nabi
    assert(autodiff::Dual::var(8.0).cbrt().du == 1.0 / 12.0);
    
  • pub fn recip(self) -> Dual§

    Source

    1 / x.

    nabi
    assert(autodiff::Dual::var(2.0).recip().du == -0.25);
    
  • pub fn asinh(self) -> Dual§

    Source

    The inverse hyperbolic sine.

    nabi
    assert(autodiff::Dual::var(0.0).asinh().du == 1.0);
    
  • pub fn acosh(self) -> Dual§

    Source

    The inverse hyperbolic cosine (x >= 1).

    nabi
    let d = autodiff::Dual::var(2.0).acosh().du;
    assert(d.is_close(1.0 / 3.0f64.sqrt()));
    
  • pub fn atanh(self) -> Dual§

    Source

    The inverse hyperbolic tangent (|x| < 1).

    nabi
    assert(autodiff::Dual::var(0.5).atanh().du == 1.0 / 0.75);
    
  • pub fn sigmoid(self) -> Dual§

    Source

    1 / (1 + e^-x), the logistic function.

    nabi
    assert(autodiff::Dual::var(0.0).sigmoid().du == 0.25);
    
  • pub fn mul_add(self, y: Dual, z: Dual) -> Dual§

    Source

    x * y + z with one rounding in the value.

    nabi
    use 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);
    
  • pub fn signum(self) -> Dual§

    Source

    The sign of the value (1, -1, or NaN for NaN); derivative 0.

    nabi
    assert(autodiff::Dual::var(-3.0).signum().re == -1.0);
    
  • pub fn is_finite(self) -> bool§

    Source

    Whether both parts are finite.

    nabi
    assert(autodiff::Dual::var(1.0).is_finite());
    
Trait implementations 22
  • impl Display for Dual
  • impl Default for Dual
  • impl num::Zero for Dual
  • impl num::One for Dual
  • impl ops::Add for Dual
  • impl ops::Sub for Dual
  • impl ops::Mul for Dual
  • impl ops::Div for Dual
  • impl ops::Neg for Dual
  • impl ops::Add<f64> for Dual
  • impl ops::Sub<f64> for Dual
  • impl ops::Mul<f64> for Dual
  • impl ops::Div<f64> for Dual
  • impl ops::AddAssign for Dual
  • impl ops::SubAssign for Dual
  • impl ops::MulAssign for Dual
  • impl ops::Pow<i32> for Dual
  • impl ops::Pow<f64> for Dual
  • impl ops::Pow<Dual> for Dual
  • impl ops::Sqrt for Dual
  • impl PartialOrd for Dual
  • impl ops::DivAssign for Dual

HyperDualSource

pub struct HyperDual

re + e1 ε1 + e2 ε2 + e12 ε1ε2. The operators and methods are those of Dual; e12 carries the mixed second derivative.

nabi
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''
  • pub re: f64§

    Source

    The value.

  • pub e1: f64§

    Source

    The ε1 part: the derivative along the first direction.

  • pub e2: f64§

    Source

    The ε2 part: the derivative along the second direction.

  • pub e12: f64§

    Source

    The ε1ε2 part: the mixed second derivative.

  • pub const fn new(re: f64, e1: f64, e2: f64, e12: f64) -> HyperDual§

    Source

    re + e1 ε1 + e2 ε2 + e12 ε1ε2.

  • pub const fn constant(x: f64) -> HyperDual§

    Source

    A constant.

    nabi
    assert(autodiff::HyperDual::constant(3.0).e12 == 0.0);
    
  • pub const fn var(x: f64) -> HyperDual§

    Source

    The variable seeded in both directions: f(var(x)).e12 is f''(x).

    nabi
    let y = autodiff::HyperDual::var(0.0).sin();
    assert(y.e1 == 1.0 && y.e12 == 0.0);
    
  • pub fn sqrt(self) -> HyperDual§

    Source

    The square root.

  • pub fn cbrt(self) -> HyperDual§

    Source

    The cube root.

  • pub fn exp(self) -> HyperDual§

    Source

    e^x.

  • pub fn exp_m1(self) -> HyperDual§

    Source

    e^x - 1, accurate near 0.

  • pub fn exp2(self) -> HyperDual§

    Source

    2^x.

  • pub fn ln(self) -> HyperDual§

    Source

    The natural logarithm.

  • pub fn ln_1p(self) -> HyperDual§

    Source

    ln(1 + x), accurate near 0.

  • pub fn log2(self) -> HyperDual§

    Source

    log2 x.

  • pub fn log10(self) -> HyperDual§

    Source

    log10 x.

  • pub fn sin(self) -> HyperDual§

    Source

    The sine.

  • pub fn cos(self) -> HyperDual§

    Source

    The cosine.

  • pub fn tan(self) -> HyperDual§

    Source

    The tangent.

  • pub fn asin(self) -> HyperDual§

    Source

    The arcsine.

  • pub fn acos(self) -> HyperDual§

    Source

    The arccosine.

  • pub fn atan(self) -> HyperDual§

    Source

    The arctangent.

  • pub fn sinh(self) -> HyperDual§

    Source

    The hyperbolic sine.

  • pub fn cosh(self) -> HyperDual§

    Source

    The hyperbolic cosine.

  • pub fn tanh(self) -> HyperDual§

    Source

    The hyperbolic tangent.

  • pub fn asinh(self) -> HyperDual§

    Source

    The inverse hyperbolic sine.

  • pub fn acosh(self) -> HyperDual§

    Source

    The inverse hyperbolic cosine (x >= 1).

  • pub fn atanh(self) -> HyperDual§

    Source

    The inverse hyperbolic tangent (|x| < 1).

  • pub fn recip(self) -> HyperDual§

    Source

    1 / x.

  • pub fn sigmoid(self) -> HyperDual§

    Source

    1 / (1 + e^-x).

  • pub fn abs(self) -> HyperDual§

    Source

    |x|; at 0 the derivatives taken are 0.

  • pub fn powi(self, n: i32) -> HyperDual§

    Source

    x^n for an integer n.

  • pub fn powf(self, p: f64) -> HyperDual§

    Source

    x^p for a real p.

  • pub fn pow(self, y: HyperDual) -> HyperDual§

    Source

    x^y, exp(y ln x).

  • pub fn hypot(self, y: HyperDual) -> HyperDual§

    Source

    sqrt(x^2 + y^2).

  • pub fn max(self, o: HyperDual) -> HyperDual§

    Source

    The larger by value.

  • pub fn min(self, o: HyperDual) -> HyperDual§

    Source

    The smaller by value.

  • pub fn is_finite(self) -> bool§

    Source

    Whether every part is finite.

Trait implementations 21
  • impl Display for HyperDual
  • impl Default for HyperDual
  • impl num::Zero for HyperDual
  • impl num::One for HyperDual
  • impl ops::Add for HyperDual
  • impl ops::Sub for HyperDual
  • impl ops::Mul for HyperDual
  • impl ops::Div for HyperDual
  • impl ops::Neg for HyperDual
  • impl ops::Add<f64> for HyperDual
  • impl ops::Sub<f64> for HyperDual
  • impl ops::Mul<f64> for HyperDual
  • impl ops::Div<f64> for HyperDual
  • impl ops::AddAssign for HyperDual
  • impl ops::SubAssign for HyperDual
  • impl ops::MulAssign for HyperDual
  • impl ops::DivAssign for HyperDual
  • impl ops::Pow<i32> for HyperDual
  • impl ops::Pow<f64> for HyperDual
  • impl ops::Sqrt for HyperDual
  • impl PartialOrd for HyperDual

JetSource

pub struct Jet

A 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.

nabi
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]);
  • pub fn var(x: f64, k: usize) -> Jet§

    Source

    The variable x + t up to order k.

    nabi
    assert(autodiff::Jet::var(2.0, 3).coefficients() == [2.0, 1.0, 0.0, 0.0]);
    
  • pub fn constant(x: f64, k: usize) -> Jet§

    Source

    The constant x up to order k.

  • pub fn value(self) -> f64§

    Source

    The value, c[0].

  • pub fn order(self) -> usize§

    Source

    The order K.

  • pub fn coefficients(self) -> List<f64>§

    Source

    The Taylor coefficients c[0] .. c[K].

  • pub fn derivatives(self) -> Vector<f64>§

    Source

    The derivatives f(x), f'(x), .., f^(K)(x): k! c[k].

    nabi
    let x = autodiff::Jet::var(1.0, 3);
    assert((x * x * x).derivatives().to_list() == [1.0, 3.0, 6.0, 6.0]);
    
  • pub fn derivative(self, k: usize) -> f64§

    Source

    The k-th derivative, 0 above the order.

  • pub fn exp(self) -> Jet§

    Source

    e^x.

  • pub fn ln(self) -> Jet§

    Source

    The natural logarithm.

  • pub fn sqrt(self) -> Jet§

    Source

    The square root.

  • pub fn sin(self) -> Jet§

    Source

    The sine.

  • pub fn cos(self) -> Jet§

    Source

    The cosine.

  • pub fn tan(self) -> Jet§

    Source

    The tangent.

  • pub fn sinh(self) -> Jet§

    Source

    The hyperbolic sine.

  • pub fn cosh(self) -> Jet§

    Source

    The hyperbolic cosine.

  • pub fn tanh(self) -> Jet§

    Source

    The hyperbolic tangent.

  • pub fn atan(self) -> Jet§

    Source

    The arctangent.

  • pub fn asin(self) -> Jet§

    Source

    The arcsine.

  • pub fn acos(self) -> Jet§

    Source

    The arccosine.

  • pub fn abs(self) -> Jet§

    Source

    |x|, by the sign of the value; at 0 the zero series.

  • pub fn powf(self, p: f64) -> Jet§

    Source

    x^p for a real p; needs x != 0 unless p is a whole number.

  • pub fn powi(self, n: i32) -> Jet§

    Source

    x^n for an integer n, by squaring.

  • pub fn is_finite(self) -> bool§

    Source

    Whether the value is finite.

Trait implementations 15
  • impl Debug for Jet
  • impl PartialEq for Jet
  • impl PartialOrd for Jet
  • impl ops::Add for Jet
  • impl ops::Sub for Jet
  • impl ops::Mul for Jet
  • impl ops::Div for Jet
  • impl ops::Neg for Jet
  • impl ops::Add<f64> for Jet
  • impl ops::Sub<f64> for Jet
  • impl ops::Mul<f64> for Jet
  • impl ops::Div<f64> for Jet
  • impl ops::Pow<i32> for Jet
  • impl ops::Pow<f64> for Jet
  • impl 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.

nabi
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));
  • pub re: f64§

    Source

    The value.

  • pub du: [f64; N]§

    Source

    The derivative parts.

  • pub fn constant(x: f64) -> Duals<N>§

    Source

    A constant: every derivative part 0.

    nabi
    let c: autodiff::Duals<3> = autodiff::Duals::constant(2.0);
    assert(c.du == [0.0, 0.0, 0.0]);
    
  • pub fn var(x: f64, k: usize) -> Duals<N>§

    Source

    The variable of lane k: derivative part k is 1, the others 0 (k >= N seeds none).

    nabi
    let x: autodiff::Duals<2> = autodiff::Duals::var(1.0, 1);
    assert(x.du == [0.0, 1.0]);
    
  • pub fn sqrt(self) -> Duals<N>§

    Source

    The square root.

  • pub fn cbrt(self) -> Duals<N>§

    Source

    The cube root.

  • pub fn exp(self) -> Duals<N>§

    Source

    e^x.

  • pub fn exp_m1(self) -> Duals<N>§

    Source

    e^x - 1, accurate near 0.

  • pub fn exp2(self) -> Duals<N>§

    Source

    2^x.

  • pub fn ln(self) -> Duals<N>§

    Source

    The natural logarithm.

  • pub fn ln_1p(self) -> Duals<N>§

    Source

    ln(1 + x), accurate near 0.

  • pub fn log2(self) -> Duals<N>§

    Source

    log2 x.

  • pub fn log10(self) -> Duals<N>§

    Source

    log10 x.

  • pub fn sin(self) -> Duals<N>§

    Source

    The sine.

  • pub fn cos(self) -> Duals<N>§

    Source

    The cosine.

  • pub fn tan(self) -> Duals<N>§

    Source

    The tangent.

  • pub fn asin(self) -> Duals<N>§

    Source

    The arcsine.

  • pub fn acos(self) -> Duals<N>§

    Source

    The arccosine.

  • pub fn atan(self) -> Duals<N>§

    Source

    The arctangent.

  • pub fn sinh(self) -> Duals<N>§

    Source

    The hyperbolic sine.

  • pub fn cosh(self) -> Duals<N>§

    Source

    The hyperbolic cosine.

  • pub fn tanh(self) -> Duals<N>§

    Source

    The hyperbolic tangent.

  • pub fn asinh(self) -> Duals<N>§

    Source

    The inverse hyperbolic sine.

  • pub fn acosh(self) -> Duals<N>§

    Source

    The inverse hyperbolic cosine (x >= 1).

  • pub fn atanh(self) -> Duals<N>§

    Source

    The inverse hyperbolic tangent (|x| < 1).

  • pub fn recip(self) -> Duals<N>§

    Source

    1 / x.

  • pub fn sigmoid(self) -> Duals<N>§

    Source

    1 / (1 + e^-x), the logistic function.

  • pub fn abs(self) -> Duals<N>§

    Source

    |x|; at 0 the derivative taken is 0.

  • pub fn powi(self, n: i32) -> Duals<N>§

    Source

    x^n for an integer n.

  • pub fn powf(self, p: f64) -> Duals<N>§

    Source

    x^p for a real p.

  • pub fn pow(self, y: Duals<N>) -> Duals<N>§

    Source

    x^y, exp(y ln x).

  • pub fn hypot(self, y: Duals<N>) -> Duals<N>§

    Source

    sqrt(x^2 + y^2) without overflow.

  • pub fn atan2(self, x: Duals<N>) -> Duals<N>§

    Source

    atan2(self, x).

  • pub fn max(self, o: Duals<N>) -> Duals<N>§

    Source

    The larger by value.

  • pub fn min(self, o: Duals<N>) -> Duals<N>§

    Source

    The smaller by value.

  • pub fn is_finite(self) -> bool§

    Source

    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 Adjoints

The derivatives of one output toward everything on its tape, from Var::backward.

  • pub fn wrt(self, x: Var) -> f64§

    Source

    d out / d x; 0 for a constant or a Var the output does not depend on.

    nabi
    let 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);
    
  • pub fn wrt_all(self, x: Vector<Var>) -> Vector<f64>§

    Source

    d out / d x[i] for each input.

    nabi
    let 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 Tape

A tape to record operations on: make the inputs with var, compute with them, then call backward on the result.

nabi
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);
  • pub fn new() -> Tape§

    Source

    An empty tape.

  • pub fn var(self, x: f64) -> Var§

    Source

    A new input with value x.

  • pub fn vars(self, x: Vector<f64>) -> Vector<Var>§

    Source

    The inputs x[0], x[1], ... in order.

    nabi
    let t = autodiff::Tape::new();
    let x = t.vars([1.0, 2.0]);
    assert(x.len() == 2 && x[1].value() == 2.0);
    
  • pub fn len(self) -> usize§

    Source

    The number of nodes recorded.

    nabi
    let t = autodiff::Tape::new();
    let x = t.var(1.0);
    let _ = x * x;
    assert(t.len() == 2);
    
  • pub fn is_empty(self) -> bool§

    Source

    Whether nothing is recorded.

Trait implementations 1
  • impl Clone for Tape

VarSource

pub struct Var

A 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.

  • pub fn constant(x: f64) -> Var§

    Source

    A constant: no tape, derivative 0.

    nabi
    let c = autodiff::Var::constant(2.0);
    assert(c.value() == 2.0);
    
  • pub fn value(self) -> f64§

    Source

    The value.

  • pub fn backward(self) -> Adjoints§

    Source

    The derivatives of self toward every Var of its tape: one sweep from self back to the inputs, O(tape length).

    nabi
    let t = autodiff::Tape::new();
    let x = t.var(3.0);
    assert((x * x * x).backward().wrt(x) == 27.0);
    
  • pub fn sqrt(self) -> Var§

    Source

    The square root.

  • pub fn cbrt(self) -> Var§

    Source

    The cube root.

  • pub fn exp(self) -> Var§

    Source

    e^x.

    nabi
    let t = autodiff::Tape::new();
    let x = t.var(0.0);
    assert(x.exp().backward().wrt(x) == 1.0);
    
  • pub fn exp_m1(self) -> Var§

    Source

    e^x - 1, accurate near 0.

  • pub fn exp2(self) -> Var§

    Source

    2^x.

  • pub fn ln(self) -> Var§

    Source

    The natural logarithm.

  • pub fn ln_1p(self) -> Var§

    Source

    ln(1 + x), accurate near 0.

  • pub fn log2(self) -> Var§

    Source

    log2 x.

  • pub fn log10(self) -> Var§

    Source

    log10 x.

  • pub fn sin(self) -> Var§

    Source

    The sine.

  • pub fn cos(self) -> Var§

    Source

    The cosine.

  • pub fn tan(self) -> Var§

    Source

    The tangent.

  • pub fn asin(self) -> Var§

    Source

    The arcsine.

  • pub fn acos(self) -> Var§

    Source

    The arccosine.

  • pub fn atan(self) -> Var§

    Source

    The arctangent.

  • pub fn atan2(self, x: Var) -> Var§

    Source

    atan2(self, x), the angle of the point (x, self).

  • pub fn sinh(self) -> Var§

    Source

    The hyperbolic sine.

  • pub fn cosh(self) -> Var§

    Source

    The hyperbolic cosine.

  • pub fn tanh(self) -> Var§

    Source

    The hyperbolic tangent.

  • pub fn asinh(self) -> Var§

    Source

    The inverse hyperbolic sine.

  • pub fn acosh(self) -> Var§

    Source

    The inverse hyperbolic cosine (x >= 1).

  • pub fn atanh(self) -> Var§

    Source

    The inverse hyperbolic tangent (|x| < 1).

  • pub fn sigmoid(self) -> Var§

    Source

    1 / (1 + e^-x).

  • pub fn recip(self) -> Var§

    Source

    1 / x.

  • pub fn abs(self) -> Var§

    Source

    |x|; at 0 the derivative taken is 0.

  • pub fn powi(self, n: i32) -> Var§

    Source

    x^n for an integer n.

  • pub fn powf(self, p: f64) -> Var§

    Source

    x^p for a real p.

  • pub fn pow(self, y: Var) -> Var§

    Source

    x^y, exp(y ln x); needs x > 0 unless y is a constant.

  • pub fn hypot(self, y: Var) -> Var§

    Source

    sqrt(x^2 + y^2) without overflow.

  • pub fn max(self, o: Var) -> Var§

    Source

    The larger by value (self on a tie).

  • pub fn min(self, o: Var) -> Var§

    Source

    The smaller by value (self on a tie).

  • pub fn is_finite(self) -> bool§

    Source

    Whether the value is finite.

Trait implementations 25
  • impl Clone for Var
  • impl Display for Var
  • impl Debug for Var
  • impl PartialEq for Var
  • impl PartialOrd for Var
  • impl Default for Var
  • impl num::Zero for Var
  • impl num::One for Var
  • impl ops::Add for Var
  • impl ops::Sub for Var
  • impl ops::Mul for Var
  • impl ops::Div for Var
  • impl ops::Neg for Var
  • impl ops::Add<f64> for Var
  • impl ops::Sub<f64> for Var
  • impl ops::Mul<f64> for Var
  • impl ops::Div<f64> for Var
  • impl ops::AddAssign for Var
  • impl ops::SubAssign for Var
  • impl ops::MulAssign for Var
  • impl ops::DivAssign for Var
  • impl ops::Pow<i32> for Var
  • impl ops::Pow<f64> for Var
  • impl ops::Pow<Var> for Var
  • impl ops::Sqrt for Var

Traits

ScalarFnSource

pub trait ScalarFn

A 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).

  • fn value_grad_at(self, x: Vector<f64>) -> (f64, Vector<f64>)§

    Source

    The value and the gradient at x.

VectorFnSource

pub trait VectorFn

A 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).

  • fn value_jacobian_at(self, x: Vector<f64>) -> (Vector<f64>, Matrix<f64>)§

    Source

    The value and the m x n Jacobian at x.

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.

nabi
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);
  • fn of(x: f64) -> Self§

    Source

    x as this type, a constant.

  • fn value(self) -> f64§

    Source

    The real value.

  • fn sqrt(self) -> Self§

    Source

    The square root.

  • fn exp(self) -> Self§

    Source

    e^x.

  • fn ln(self) -> Self§

    Source

    The natural logarithm.

  • fn sin(self) -> Self§

    Source

    The sine.

  • fn cos(self) -> Self§

    Source

    The cosine.

  • fn tan(self) -> Self§

    Source

    The tangent.

  • fn atan(self) -> Self§

    Source

    The arctangent.

  • fn sinh(self) -> Self§

    Source

    The hyperbolic sine.

  • fn cosh(self) -> Self§

    Source

    The hyperbolic cosine.

  • fn tanh(self) -> Self§

    Source

    The hyperbolic tangent.

  • fn abs(self) -> Self§

    Source

    The absolute value.

  • fn powi(self, n: i32) -> Self§

    Source

    x^n.

  • fn powf(self, p: f64) -> Self§

    Source

    x^p.

Functions

derivativeSource

pub fn derivative(f: fn(Dual) -> Dual, x: f64) -> f64

f'(x) by one evaluation of f at x + ε.

nabi
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.

nabi
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.

nabi
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.

nabi
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).

nabi
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.

nabi
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).

nabi
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).

nabi
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.

nabi
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);