Expand description
§xad-rs — Automatic Differentiation for Rust
Exact, machine-precision derivatives of arbitrary numerical programs — no finite-difference error, no symbolic manipulation.
xad-rs ships four AD modes in a single crate, each suited to a
different problem shape.
Conceptually, the crate is built around the Real trait — a unified
active-scalar abstraction that lets the same numerical body run
against f64, forward-mode, or reverse-mode types.
See also (long-form theory): docs/README.md on GitHub.
§Choosing what to program against
Program your numerical logic once against the trait Real; pick
the concrete mode at the call site that matches your problem shape:
use xad_rs::prelude::*;
// Same body, four call sites — see below.
fn quadratic<R: Real>(x: &R) -> R {
x.clone() * x.clone() + R::from(2.0_f64) * x.clone() + R::from(1.0_f64)
}§Choosing a mode
| Type | Mode | Order | Use when |
|---|---|---|---|
f64 | none (passive) | 0 | no derivatives needed |
Jet1<T> | Forward | 1st | 1 input direction, many outputs |
Jet1Vec | Forward, multi-var | 1st | full gradient in one pass |
Jet2<T> | Forward, 2nd-order | 1st + 2nd | diagonal Hessian / gamma |
AReal<T> + Tape | Reverse (adjoint) | 1st | many inputs, scalar output |
Reverse mode breaks even with forward around n ~ 4 inputs. For
n >> 4 (e.g. 30-input swap pricer), reverse is dramatically faster.
§Quick start — reverse mode
use xad_rs::{AReal, Tape, math};
let mut tape = Tape::<f64>::new(true);
tape.activate();
let x = AReal::input(3.0, &mut tape);
let y = AReal::input(4.0, &mut tape);
// f(x, y) = x^2 * y + sin(x)
let mut f = &(&x * &x) * &y + math::ad::sin(&x);
f.register(&mut tape);
f.set_adjoint(&mut tape, 1.0);
tape.compute_adjoints();
let dfdx = x.adjoint(&tape); // 2xy + cos(x)
let dfdy = y.adjoint(&tape); // x^2
assert!((dfdx - (2.0 * 3.0 * 4.0 + 3.0_f64.cos())).abs() < 1e-12);
assert!((dfdy - 9.0).abs() < 1e-12);§Quick start — forward mode
Seed all inputs in one pass and read the full gradient:
use xad_rs::Jet1Vec;
let (x, y) = (Jet1Vec::variable(3.0, 0, 2), Jet1Vec::variable(4.0, 1, 2));
let f = &(&x * &x) * &y; // x^2 * y
assert_eq!(f.partial(0), 24.0); // df/dx = 2xy
assert_eq!(f.partial(1), 9.0); // df/dy = x^2§Second-order derivatives
use xad_rs::Jet2;
let x: Jet2<f64> = Jet2::variable(2.0);
let y = x * x * x; // x^3
assert_eq!(y.first_derivative(), 12.0); // 3x^2
assert_eq!(y.second_derivative(), 12.0); // 6x§Module overview
| Module | Contents |
|---|---|
real | The unified active-scalar trait Real |
real_stats | The RealStats extension trait (erf, norm_cdf, …) |
passive | The passive-scalar bound Passive (f32, f64) |
prelude | Real, RealStats, Passive, AReal, Jet1, Jet2, Tape, TapeStorage |
forward | Jet1, Jet1Vec, Jet2, Jet2Vec, JetK |
reverse | AReal |
math | AD-aware transcendentals (sin, exp, erf, norm_cdf, …) |
tape | Reverse-mode tape and thread-local active-tape slot |
ops | compute_jacobian_*, compute_hessian{,_par,_k,_k_par}, compute_full_hessian |
Re-exports§
pub use forward::Jet1;pub use forward::Jet1Vec;pub use forward::Jet2;pub use forward::Jet2Vec;pub use forward::JetK;pub use reverse::AReal;pub use tape::Tape;pub use tape::TapeGuard;pub use tape::TapeStorage;pub use passive::Passive;pub use real::Real;pub use real_stats::RealStats;pub use ops::compute_hessian;pub use ops::compute_hessian_k;pub use ops::compute_hessian_k_par;pub use ops::compute_hessian_par;pub use ops::compute_jacobian_fwd;pub use ops::compute_jacobian_rev;pub use ops::compute_jacobian_rev_par;pub use ops::DenseHessian;pub use ops::compute_full_hessian;pub use parallel::par_map_tape;pub use parallel::par_map_tape_with_capacity;
Modules§
- forward
- Forward-mode AD types.
- math
- AD-aware transcendental functions.
- ops
- Composite AD operations: Jacobian and Hessian computations.
- parallel
- Parallel valuation over per-thread reused tapes.
- passive
- The
Passivetrait — the bound for the underlying passive (non-AD) scalar storage type that the active scalars (AReal<T>,Jet1<T>,Jet2<T>) wrap. - prelude
- Convenience re-exports —
use xad_rs::prelude::*;brings the mode-agnostic traitReal, the passive boundPassive, and the most-used concrete types into scope. - real
- The
Realtrait — the unified active-scalar trait that abstracts over every AD mode this crate ships (reverse viacrate::AReal, forward first-order viacrate::Jet1, forward second-order viacrate::Jet2) plus the no-AD case (plainf64). - real_
stats - The
RealStatsextension trait — statistical functions (erf,erfc,norm_cdf,inv_norm_cdf) for the active-scalar surface. - reverse
- Reverse-mode (adjoint) AD types.
- tape
- Tape for recording operations in reverse-mode automatic differentiation.