autodiff

This manual documents the unreleased main branch of Luna-Flow/autodiff (version 0.3.1 in moon.mod) after its migration to MoonBit 0.10.

Overview

Luna-Flow/autodiff computes exact first derivatives of ordinary MoonBit programs by forward-mode automatic differentiation. A program written against the Luna Flow traits is run on dual numbers a+bεa + b\varepsilon with ε2=0\varepsilon^2 = 0, and the derivative appears in the ε\varepsilon component. The repository provides the dual-number type, scalar drivers, checked domains for division and square roots, gradients and Jacobians over linear-algebra vectors, and derivatives of luna-poly polynomials.

  • Dual[T] is generic over the scalar and implements exactly the ring-level traits of luna-generic: no Field, Inverse or order, because ε\varepsilon has no inverse.
  • Every elementary rule is f(a+bε)=f(a)+f′(a) b εf(a + b\varepsilon) = f(a) + f'(a)\,b\,\varepsilon, and the chain rule follows from composition.
  • Gradients and Jacobians take one forward pass per input; there is no reverse mode.

Install

moon add Luna-Flow/autodiff@0.3.1

Then import the root package in your moon.pkg:

import {
  "Luna-Flow/autodiff",
}

The bridges need their libraries too: moon add Luna-Flow/linear-algebra for linalg and moon add Luna-Flow/luna-poly for poly. The code needs the MoonBit toolchain 0.10.20 or later (moonc ≥ 0.10.20) with moon.mod / moon.pkg manifests, and the test suite runs on the wasm-gc, wasm, js and native targets.

Pages

The root package Luna-Flow/autodiff is documented as autodiff, because the name core belongs to the package src/core. Every other page is named after its directory under src/.

PartTutorialAPIDesign
autodiff: one-stop facade with Dual, diff, value_and_diff and the re-exported traitstutorialAPIdesign
dual: the Dual[T] type, its arithmetic, derivative rules and instancestutorialAPIdesign
forward: scalar drivers diff and value_and_difftutorialAPIdesign
core: algebraic facade, Dual and the luna-generic structure traitstutorialAPIdesign
elementary: analytic facade, Dual and the arithmetic function traitstutorialAPIdesign
checked: checked facade, DivChecked, SqrtChecked, context and errorstutorialAPIdesign
linalg: gradients and Jacobians over linear-algebra/immuttutorialAPIdesign
poly: derivatives of dense and univariate sparse luna-poly polynomialstutorialAPIdesign
Guidesarchitectureconventions

Two more packages have no manual pages. examples holds five small functions (basic_diff_example, square_diff_example, gradient_example, jacobian_example, polynomial_derivative_example) that show each layer in a few lines; read src/examples/examples.mbt. tests is the black-box test suite of all packages, including the linear-algebra and luna-poly integration tests.

Exported items

The number type

  • Dual[T] with Dual::new, constant, variable, value, tangent, zero, one, from_natural and from_integer
  • Arithmetic: add, sub, neg, mul, div and equal, also as operators
  • Elementary rules: sqrt, exp, exp2, ln, log2, log10, sin, cos, tan
  • Checked forms: div_checked, sqrt_checked

Drivers

  • Scalar: diff, value_and_diff
  • Vector: gradient, value_and_gradient, jacobian, value_and_jacobian
  • Polynomial: eval_dual, dense_value_and_derivative_at, dense_derivative_at, the sparse_univariate_* functions and their compatibility names

Re-exported vocabulary

  • From luna-generic: Zero, One, AddMonoid, AddGroup, MulMonoid, Semiring, Ring, FromNat, FromInteger
  • From arithmetic: Sqrt, SqrtChecked, Exponential, Logarithmic, Trigonometric, Constants, DivChecked, ArithmeticContext, ArithmeticError, ArithmeticErrorKind, RoundingMode
  • From linear-algebra and luna-poly: Vector, Matrix, DensePolynomial, SparsePolynomial in the bridge packages

The autodiff tutorial differentiates a function with one import. The dual API lists every rule and instance of the number type, and the dual design derives them, with the rounding error bounds and the comparison with finite differences.

Validation

Recommended release checks:

moon check --target all
./run_test.sh
moon info