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
}
Macros§
- chain_
rule - forward_
binop - impl_
add_ sub_ rem - impl_
assign_ ops - impl_
derivatives - impl_
dual - impl_
first_ derivatives - impl_
float_ const - impl_
from_ f - impl_
from_ primitive - impl_
inv - impl_
iterator - impl_
lift - impl_
neg - impl_
num - impl_
scalar_ op - impl_
second_ derivatives - impl_
signed - impl_
third_ derivatives - impl_
zero_ one - second
- third
Structs§
- Derivative
- Wrapper struct for a derivative vector or matrix.
- Dual
- A scalar dual number for the calculations of first derivatives.
- Dual2
- A scalar second order dual number for the calculation of second derivatives.
- Dual3
- A scalar third order dual number for the calculation of third derivatives.
- Dual2
Vec - A vector second order dual number for the calculation of Hessians.
- DualVec
- A vector dual number for the calculations of gradients or Jacobians.
- Hyper
Dual - A scalar hyper-dual number for the calculation of second partial derivatives.
- Hyper
Dual Vec - A vector hyper-dual number for the calculation of partial Hessians.
- Hyper
Hyper Dual - A scalar hyper-hyper-dual number for the calculation of third partial derivatives.
Traits§
- Bessel
Dual - Implementation of bessel functions for double precision (hyper) dual numbers.
- DualNum
- A generalized (hyper) dual number.
- Dual
NumFloat - The underlying data type of individual derivatives. Usually f32 or f64.
- Lift
Functions§
- first_
derivative - Calculate the first derivative of a scalar function.
- gradient
- Calculate the gradient of a scalar function
- hessian
- Calculate the Hessian of a scalar function.
- implicit_
derivative - Calculate the derivative of the multivariate implicit function g(x, args) = 0
- implicit_
derivative_ binary - Calculate the derivative of the binary implicit function g(x, y, args) = 0
- implicit_
derivative_ unary - Calculate the derivative of the unary implicit function g(x, args) = 0
- jacobian
- Calculate the Jacobian of a vector function.
- partial_
hessian - Calculate second partial derivatives with respect to vectors.
- second_
derivative - Calculate the second derivative of a univariate function.
- second_
partial_ derivative - Calculate second partial derivatives with respect to scalars.
- third_
derivative - Calculate the third derivative of a univariate function.
- third_
partial_ derivative - Calculate third partial derivatives with respect to scalars.
- third_
partial_ derivative_ vec - Calculate the third partial derivative of a scalar function with arbitrary many variables.
- try_
first_ derivative - Variant of first_derivative for fallible functions.
- try_
gradient - Variant of gradient for fallible functions.
- try_
hessian - Variant of hessian for fallible functions.
- try_
jacobian - Variant of jacobian for fallible functions.
- try_
partial_ hessian - Variant of partial_hessian for fallible functions.
- try_
second_ derivative - Variant of second_derivative for fallible functions.
- try_
second_ partial_ derivative - Variant of second_partial_derivative for fallible functions.
- try_
third_ derivative - Variant of third_derivative for fallible functions.
- try_
third_ partial_ derivative - Variant of third_partial_derivative for fallible functions.
- try_
third_ partial_ derivative_ vec - Variant of third_partial_derivative_vec for fallible functions.
Type Aliases§
- Dual2D
Vec32 - Dual2D
Vec64 - Dual2S
Vec32 - Dual2S
Vec64 - Dual2
Vec32 - Dual2
Vec64 - Dual2_
32 - Dual2_
64 - Dual3_
32 - Dual3_
64 - Dual32
- Dual64
- DualD
Vec32 - DualD
Vec64 - DualS
Vec32 - DualS
Vec64 - Dual
Vec32 - Dual
Vec64 - Hyper
Dual32 - Hyper
Dual64 - Hyper
DualD Vec32 - Hyper
DualD Vec64 - Hyper
DualS Vec32 - Hyper
DualS Vec64 - Hyper
Dual Vec32 - Hyper
Dual Vec64 - Hyper
Hyper Dual32 - Hyper
Hyper Dual64