Expand description
Generalized, recursive, scalar and vector (hyper) dual numbers for the automatic and exact calculation of (partial) derivatives.
Example
This example defines a generic scalar and a generic vector function that can be called using any (hyper-) dual number and automatically calculates derivatives.
use num_dual::*;
use nalgebra::SVector;
fn foo<D: DualNum<f64>>(x: D) -> D {
x.powi(3)
}
fn bar<D: DualNum<f64>, const N: usize>(x: SVector<D, N>) -> D {
x.dot(&x).sqrt()
}
fn main() {
// Calculate a simple derivative
let (f, df) = first_derivative(foo, 5.0);
assert_eq!(f, 125.0);
assert_eq!(df, 75.0);
// Manually construct the dual number
let x = Dual64::new(5.0, 1.0);
println!("{}", foo(x)); // 125 + [75]ε
// Calculate a gradient
let (f, g) = gradient(bar, SVector::from([4.0, 3.0]));
assert_eq!(f, 5.0);
assert_eq!(g[0], 0.8);
// Calculate a Hessian
let (f, g, h) = hessian(bar, SVector::from([4.0, 3.0]));
println!("{h}"); // [[0.072, -0.096], [-0.096, 0.128]]
// for x=cos(t) calculate the third derivative of foo w.r.t. t
let (f0, f1, f2, f3) = third_derivative(|t| foo(t.cos()), 1.0);
println!("{f3}"); // 1.5836632930100278
}
Structs
- Wrapper struct for a derivative vector or matrix.
- A scalar dual number for the calculations of first derivatives.
- A scalar second order dual number for the calculation of second derivatives.
- A vector second order dual number for the calculation of Hessians.
- A scalar third order dual number for the calculation of third derivatives.
- A vector dual number for the calculations of gradients or Jacobians.
- A scalar hyper-dual number for the calculation of second partial derivatives.
- A vector hyper-dual number for the calculation of partial Hessians.
- A scalar hyper-hyper-dual number for the calculation of third partial derivatives.
Traits
- Implementation of bessel functions for double precision (hyper) dual numbers.
- A generalized (hyper) dual number.
- The underlying data type of individual derivatives. Usually f32 or f64.
Functions
- Calculate the first derivative of a scalar function.
- Calculate the gradient of a scalar function
- Calculate the Hessian of a scalar function.
- Calculate the Jacobian of a vector function.
- Calculate second partial derivatives with respect to vectors.
- Calculate the second derivative of a univariate function.
- Calculate second partial derivatives with respect to scalars.
- Calculate the third derivative of a univariate function.
- Calculate third partial derivatives with respect to scalars.
- Calculate the third partial derivative of a scalar function with arbitrary many variables.
- Variant of first_derivative for fallible functions.
- Variant of gradient for fallible functions.
- Variant of hessian for fallible functions.
- Variant of jacobian for fallible functions.
- Variant of partial_hessian for fallible functions.
- Variant of second_derivative for fallible functions.
- Variant of second_partial_derivative for fallible functions.
- Variant of third_derivative for fallible functions.
- Variant of third_partial_derivative for fallible functions.
- Variant of third_partial_derivative_vec for fallible functions.