|
cppduals
0.10.0
Dual numbers for C++
|
cppduals is a standalone, dependency-free, header-only C++17 template library for dual number arithmetic and forward-mode automatic differentiation. It requires only a standard-conforming C++ compiler — no external dependencies.
A dual number has the form \( a + b\epsilon \) where \( \epsilon \ne 0 \) and \( \epsilon^2 = 0 \). Evaluating any differentiable function \( f \) at a dual argument \( x + \epsilon \) yields \( f(x) + f'(x)\epsilon \) — the exact derivative, with no finite-difference approximation.
The Taylor expansion of \( f(a + b\epsilon) \) truncates at first order:
\[f(a + b\epsilon) = f(a) + f'(a)\,b\,\epsilon \]
Setting \( b = 1 \) places the derivative \( f'(a) \) in the dual part of the result. Higher derivatives are obtained by nesting: dual<dual<T>> carries both \( f' \) and \( f'' \).
For functions of multiple variables, duals/multidual provides dual<T, int N> — a dual number carrying N independent partial derivatives. This computes the full gradient \( \nabla f = (\partial f/\partial x_1, \ldots, \partial f/\partial x_N) \) in a single evaluation pass, rather than requiring N separate passes:
With N=1 (the default), dual<T> and dual<T,1> are identical — all existing code works unchanged.
| Header | Purpose |
|---|---|
| duals/dual | Core dual<T> class, math functions, IO, complex overloads |
| duals/multidual | Multivariate dual<T,N> with compile-time gradient support |
| duals/dual_eigen | Eigen NumTraits, type promotion, SIMD packet ops |