Crate hyperdual[][src]

Expand description

Dual Numbers

Fully-featured Dual Number implementation with features for automatic differentiation of multivariate vectorial functions into gradients.

Usage

extern crate hyperdual;

use hyperdual::{Dual, Hyperdual, Float, differentiate};

fn main() {
    // find partial derivative at x=4.0
    let univariate = differentiate(4.0f64, |x| x.sqrt() + Dual::from_real(1.0));
    assert!((univariate - 0.25).abs() < 1e-16, "wrong derivative");

    // find the partial derivatives of a multivariate function
    let x: Hyperdual<f64, 3> = Hyperdual::from_slice(&[4.0, 1.0, 0.0]);
    let y: Hyperdual<f64, 3> = Hyperdual::from_slice(&[5.0, 0.0, 1.0]);

    let multivariate = x * x + (x * y).sin() + y.powi(3);
    assert!((multivariate[0] - 141.91294525072763).abs() < 1e-13, "f(4, 5) incorrect");
    assert!((multivariate[1] - 10.04041030906696).abs() < 1e-13, "df/dx(4, 5) incorrect");
    assert!((multivariate[2] - 76.63232824725357).abs() < 1e-13, "df/dy(4, 5) incorrect");

    // You may also use the new Const approach (both U* and Const<*> use the const generics)
    let x: Hyperdual<f64, 3> = Hyperdual::from_slice(&[4.0, 1.0, 0.0]);
    let y: Hyperdual<f64, 3> = Hyperdual::from_slice(&[5.0, 0.0, 1.0]);

    let multivariate = x * x + (x * y).sin() + y.powi(3);
    assert!((multivariate[0] - 141.91294525072763).abs() < 1e-13, "f(4, 5) incorrect");
    assert!((multivariate[1] - 10.04041030906696).abs() < 1e-13, "df/dx(4, 5) incorrect");
    assert!((multivariate[2] - 76.63232824725357).abs() < 1e-13, "df/dy(4, 5) incorrect");
}
Previous Work

Modules

Structs

An allocator based on GenericArray and VecStorage for statically-sized and dynamically-sized matrices respectively.

Dim of dynamically-sized algebraic entities.

Dual Number structure

Traits

A matrix allocator of a memory buffer that may contain R::to_usize() * C::to_usize() elements of type T.

Trait implemented by any type that can be used as a dimension. This includes type-level integers and Dynamic (for dimensions not known at compile-time).

Trait implemented exclusively by type-level integers.

Generic trait for floating point numbers

Trait implemented by Dynamic.

Trait implemented by Dynamic and type-level integers different from U1.

The base trait for numeric types, covering 0 and 1 values, comparisons, basic numeric operations, and string conversion.

Defines a multiplicative identity element for Self.

Defines an additive identity element for Self.

Functions

Evaluates the function using dual numbers to get the partial derivative at the input point

Type Definitions