Single variable automatic differentiation in Rust, with support for:
- Basic arithmetic:
$u + v$ ,$u - v$ ,$u*v$ ,$\frac{u}{v}$ - Power:
$u^n$ ,$\sqrt{u}$ - Exponentation:
$e^u$ - Trigonometry:
$\sin{u}$ and$\cos{u}$ - Inverse trigonometry:
$\arctan{u}$ - Logarithm:
$\ln{u}$ - Composition:
$u \circ v$
Create a function with operators and methods
For example:
use autodiff::X;
let f = X.pow(3.0) / 2.0 + (2.0 * X).sin();
Compute the value of the function and its derivative
For example:
use autodiff::Fn; // Import the Fn trait to use .eval()
let (value, derivative) = f.eval(3.0);
println!("f(3) = {value}"); // 13.220585
println!("f'(3) = {derivative}"); // 15.420341
Do cool things with the derivative like finding the local minima/maxima
For example:
let f = 0.8 * X.pow(4.0) - 1.5 * X.pow(3.0) - X.pow(2.0) + 2.0 * X + 2.5;
let mut input = 0.0;
for _ in 0..100 {
let (_, grad) = f.eval(input);
input -= grad * 0.1;
}
let (output, _) = f.eval(input);
println!("a local minima of f(x) is f({input}) = {output}");
- Multiple variables
- High order derivative
- Visual examples
This project is licensed under the MIT License.