autodiff::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). They dispatch through Derive and Gradient, which other
packages implement for their own types (interpolate's splines and
polynomials), so one ∂ and one ∇ serve every package that
re-exports them. ∇ and ∂ are identifier characters, so they are
function names here, not prefix operators.
Traits
DeriveSource
pub trait DeriveWhat ∂ differentiates: Output is the derivative in the same form.
use autodiff::ops::{Derive, ∂};
// A polynomial by its coefficients.
struct Poly { c: List<f64>; }
impl Derive for Poly {
type Output = Poly;
fn derive(self, nu: usize) -> Poly {
let c = self.c.clone();
for _ in 0..nu { c = List::from_fn(c.len().max(1) - 1, |i| c[i + 1] * ((i + 1) as f64)); }
Poly { c }
}
}
let p = Poly { c: [1.0, 2.0, 3.0] }; // 1 + 2x + 3x^2
assert(∂(p).c == [2.0, 6.0] && ∂(p, 2).c == [6.0]);
The
nu-th derivative.
GradientSource
pub trait GradientWhat ∇ takes the gradient of: Output is the gradient in the same
form (a function for a function, samples for samples).
The gradient.
Functions
dual_literalSource
pub fn dual_literal(n: f64) -> Dual 2.0ε: the dual number 0 + 2ε, so 3.0 + 1.0ε is the variable 3
seeded for a derivative.
let x = 3.0 + 1.0ε;
let y = x * x + 2.0 * x;
assert(y.re == 15.0 && y.du == 8.0);
∂Source
pub fn ∂<F: Derive>(f: F, nu: usize = 1) -> F::Output ∂(f): the derivative of f in f's form; ∂(f, n) the n-th. For
a function over Dual or Var it is f' as a function (first order
only), over HyperDual orders 1 and 2, over Jet any order;
interpolate's splines and polynomials give a spline or polynomial.
use autodiff::{Dual, Jet};
use autodiff::ops::∂;
let fp = ∂(|x: Dual| -> Dual { x.sin() });
assert(fp(0.0) == 1.0);
let f3 = ∂(|x: Jet| -> Jet { x.powi(4) }, 3); // 24 x
assert(f3(2.0) == 48.0);
∇Source
pub fn ∇<A: Gradient>(a: A) -> A::Output ∇(f): the gradient of f as a function. For f: R^n -> R over
Dual (n evaluations) or Var (one backward sweep) it is
x -> grad f(x); for a vector function, x -> J(x). For samples
on a unit grid it is the samples of the derivative (central
differences): a vector for rank 1, one array per axis for ranks 2
and 3.
use autodiff::Var;
use autodiff::ops::∇;
use core::array::{Matrix, Vector};
let g = ∇(|x: Vector<Var>| -> Var { x[0] * x[0] + 3.0 * x[1] });
assert(g([2.0, 0.0]).to_list() == [4.0, 3.0]);
let y: Vector<f64> = [0, 1, 4, 9];
assert(∇(y).to_list() == [1.0, 2.0, 4.0, 5.0]);
let z = Matrix::<f64>::from_fn([3, 4], |i: [usize; 2]| -> f64 { (2 * i[0] + i[1]) as f64 });
assert(∇(z)[0][1, 1] == 2.0 && ∇(z)[1][1, 1] == 1.0);