autodiff
An auto-differentiation library.
Currently supported features:
-
Forward auto-differentiation
- Single variable higher order differentiation
-
Reverse auto-differentiation
To compute a derivative with respect to a variable using this library:
-
create a variable of type
F
, which implements theFloat
trait from thenum-traits
crate. -
compute your function using this variable as the input.
-
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 = ;
// Differentiate `f` at zero.
println!; // 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 = ;
// Differentiate `f` at `(1,2)`.
let g = grad;
println!; // prints `(4, 4)`
Compute a specific derivative of a multi-variable function:
// Define a function `f(x,y) = x*y^2`.
let f = ;
// Differentiate `f` at `(1,2)` with respect to `x` (the first unknown) only.
let v = vec!;
println!; // prints `4`
Compute higher order derivatives by nesting the generic parameter of F
. For convenience we provide
type aliases for the first 3 orders:
type F1 = ;
type F2 = ;
type F3 = ;
To compute the third order derivative, we can use the F3
type as follows.
// Define a function `f(x) = (x - 1)^3`.
let f = ;
// Compute the 3rd derivative of `f` at `x = 0`.
println!; // prints `6`
License
This repository is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Acknowledgements
This library started as a fork of rust-ad.