## autodiff

An automatic differentiation library

### 10 releases

✓ Uses Rust 2018 edition

 0.1.9 Nov 7, 2019 Nov 5, 2019 Aug 28, 2019 Jan 17, 2019 Nov 10, 2018

#71 in Rust patterns

Used in 2 crates

MIT/Apache

36KB
1K SLoC

# `autodiff`

An auto-differentiation library.

Currently supported features:

• Forward auto-differentiation

• Reverse auto-differentiation

To compute a derivative with respect to a variable using this library:

1. create a variable of type `F`, which implements the `Float` trait from the `num-traits` crate.

2. compute your function using this variable as the input.

3. request the derivative from this variable using the `deriv` method.

# Disclaimer

This library is a work in progress and is not ready for production use.

# Examples

The following example differentiates a 1D function defined by a closure.

``````    // Define a function `f(x) = e^{-0.5*x^2}`.
let f = |x: F| (-x * x / F::cst(2.0)).exp();

// Differentiate `f` at zero.
println!("{}", diff(f, 0.0)); // prints `0`
``````

To compute the gradient of a function, use the function `grad` as follows:

``````    // Define a function `f(x,y) = x*y^2`.
let f = |x: &[F]| x * x * x;

// Differentiate `f` at `(1,2)`.
let g = grad(f, &vec![1.0, 2.0]);
println!("({}, {})", g, g); // prints `(4, 4)`
``````

Compute a specific derivative of a multi-variable function:

``````     // Define a function `f(x,y) = x*y^2`.
let f = |v: &[F]| v * v * v;

// Differentiate `f` at `(1,2)` with respect to `x` (the first unknown) only.
let v = vec![
F::var(1.0), // Create a variable.
F::cst(2.0), // Create a constant.
];
println!("{}", f(&v).deriv()); // prints `4`
``````