# Crate num_dual

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.