ekmett/ad
Automatic Differentiation
ad – Automatic Differentiation for Haskell
What it is
- A Haskell library that lets you compute exact derivatives of numeric functions directly in code. It implements both forward‑mode and reverse‑mode automatic differentiation (AD) behind a small, type‑safe API.
Why it matters
- In machine‑learning, optimisation, physics simulation, and many other fields you often need gradients, Jacobians, Hessians, etc. Writing these by hand is error‑prone; using finite differences is slow and inaccurate.
adgives you the speed of compiled code with the precision of symbolic differentiation, while staying pure Haskell.
Key features
- Forward‑mode and reverse‑mode AD with a common, easy‑to‑learn interface.
- Higher‑order derivatives (gradients, Jacobians, Hessians, arbitrary‑order tensors) available lazily and efficiently.
- Type‑level branding to prevent accidental mixing of “infinitesimal” AD values with ordinary numbers.
- Modular design – each mode lives in its own module (
Numeric.AD.Mode.Forward,...Reverse,...Sparse,...Tower, etc.) so you can pick the implementation that fits your problem size. - Root‑finding helpers (
Newton,Halley) built on top of the AD engine. - Rank‑1 combinators for maximum flexibility when you need to manipulate AD values with higher‑order functions (at the cost of some type safety).
Typical usage
import Numeric.AD
-- First derivative of sin at 0
> diff sin 0
1.0
-- Value and derivative of exp . log at 2
> diff' (exp . log) 2
(2.0,1.0)
-- Gradient of a multivariate function
> grad (\[x,y,z] -> x * sin (x + log y)) [1,2,3]
[ sin (1 + log 2) + cos (1 + log 2) * 1
, recip 2 * cos (1 + log 2) * 1
, 0 ]
The library also supports streams of higher‑order derivatives (diffs, grads) and a Jet type for inspecting each “layer” of the derivative tower.
Who should use it
- Haskell developers building machine‑learning models, optimisation algorithms, or scientific simulations that need fast, exact gradients.
- Researchers who want to experiment with custom loss functions or differential equations without leaving the Haskell ecosystem.
- Anyone needing reliable root‑finding (
Newton,Halley) that benefits from automatic derivatives.
Getting started
- Install from Hackage:
cabal install ador addadto your project’s.cabalfile. - Import
Numeric.AD(or a specific mode module) and start callingdiff,grad,jacobian,hessian, etc.
Where to ask questions
- Open issues or pull requests on the GitHub repo: https://github.com/ekmett/ad
- Join the Haskell community on IRC (
#haskellon irc.libera.chat) or the project's issue tracker.
This summary is based solely on the repository’s README.
Related
- Project
- Project
- Project
- Project