aad 0.4.0

Automatic adjoint differentiation library
Documentation

AAD: Adjoint Automatic Differentiation

CI Crates.io Docs.rs

A Rust library for reverse-mode automatic differentiation (AD), enabling efficient gradient computation for scalar-valued functions.

Features

  • Intuitive API: Write equations as naturally as primitive f64 operations.
  • High Performance: Optimized for minimal runtime overhead.
    • Benchmarks show competitive performance, often outperforming alternatives in gradient computation ( see Benchmarks).
  • Zero Dependencies: Core library has no external dependencies.
    • RustQuant_autodiff includes extra dependencies, which may require additional system setup when installing on Linux.
    • (Optional criterion and RustQuant_autodiff for benchmarking only.)
  • Extensive Math Support:
    • Trigonometric (sin, cos, tanh), exponential (exp, powf), logarithmic (ln, log10), and more.
    • Full list in supported operations.

Installation

Add to your Cargo.toml:

[dependencies]
aad = "0.3.0"

Quick Start

use aad::Tape;
use aad::ScalarLike;

fn main() {
    // Initialize a computation tape
    let tape = Tape::default();

    // Create variables
    let x = tape.create_variable(2.0);
    let y = tape.create_variable(3.0);

    // Create type-agnostic mathematical functions that work with both Variable and f64:
    fn f<S: ScalarLike>(x: S, y: S) -> S {
        (x + y) * x.sin()
    }
    
    let z = f(x, y);

    // Forward pass: compute value
    println!("z = {:.2}", z.value()); // Output: z = 4.55

    // Reverse pass: compute gradients
    let grads = z.compute_gradients();
    println!("Gradients: dx = {:.2}, dy = {:.2}",
             grads.get_gradient(&x),
             grads.get_gradient(&y)
    ); // Output: dx = -2.83, dy = 0.14
}

Supported Operations

Basic Arithmetic

  • +, -, *, /, negation
  • Assignment operators (+=, *=, etc.)

Mathematical Functions

  • Exponential: exp, powf, sqrt, hypot
  • Logarithmic: ln, log, log2, log10
  • Trigonometric: sin, cos, tan, asin, acos, sinh, cosh
  • Other: abs, recip, cbrt

See math.rs for full details.

Benchmarks

Detailed results

Design Notes

  • Tape-based: All operations are recorded to a Tape for efficient reverse-mode traversal.
  • Lightweight: Variables are Copy-enabled structs with minimal memory footprint.

Contributing

Contributions are welcome! Open an issue or PR for feature requests, bug fixes, or documentation improvements.

License

MIT License. See LICENSE for details.