Fast, transparent first- and second-order automatic differentiation for Python
The ad package allows you to easily and transparently perform first- and
second-order automatic differentiation. Advanced math involving trigonometric,
logarithmic, hyperbolic, and related functions can be evaluated directly using
the ad.admath submodule.
All base numeric types are supported (int, float, complex, etc.). The
package is designed so underlying numeric types interact as they normally do
during calculations. In practice, ad behaves like a lightweight wrapper that
tracks derivatives while preserving standard numeric behavior.
From the Wikipedia entry on Automatic differentiation:
"AD exploits the fact that every computer program, no matter how complicated, executes a sequence of elementary arithmetic operations (addition, subtraction, multiplication, division, etc.) and elementary functions (exp, log, sin, cos, etc.). By applying the chain rule repeatedly to these operations, derivatives of arbitrary order can be computed automatically, and accurate to working precision."
See the package documentation for details and examples.
- Transparent calculations with derivatives, requiring little or no modification to existing code (including NumPy-based code).
- Broad mathematical operation support, including most functions from
mathandcmath, plus convenience trigonometric, hyperbolic, and logarithmic helpers (csc,acoth,ln, etc.). Comparison operators follow the same rules as the wrapped numeric values. - Seamless real and complex arithmetic through
ad.admathcounterparts. - Automatic gradient and Hessian function generator for optimization workflows
with
scipy.optimizeviagh(your_func_here). - Linear algebra routines in
ad.linalgsimilar to NumPy'slinalg, without LAPACK dependency.
Decompositions
chol: Cholesky decompositionlu: LU decompositionqr: QR decomposition
Solving equations and matrix inversion
solve: General solver for linear systemslstsq: Least-squares solver for linear systemsinv: Multiplicative inverse of a matrix
uv pip install adRequires Python 3.10+. See the full installation guide.
- Theory — mathematical background, hierarchical basis, algorithms
- Quickstart — runnable examples
- API Reference — class and function signatures
- References — literature citations
BSD-3-Clause.