autodiff

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.

This manual documents version 0.2.0 on MoonBit 0.10.

Packages

PackageImport pathRolePages
autodiffLuna-Flow/autodiffone-stop facade: Dual, diff, value_and_diff, re-exported traitsAPI · tutorial · design
dualLuna-Flow/autodiff/dualthe Dual[T] type, its arithmetic, rules and instancesAPI · tutorial · design
forwardLuna-Flow/autodiff/forwardscalar drivers diff and value_and_diffAPI · tutorial · design
coreLuna-Flow/autodiff/corealgebraic facade: Dual and the luna-generic structure traitsAPI · tutorial · design
elementaryLuna-Flow/autodiff/elementaryanalytic facade: Dual and the arithmetic function traitsAPI · tutorial · design
checkedLuna-Flow/autodiff/checkedchecked facade: DivChecked, SqrtChecked, context and errorsAPI · tutorial · design
linalgLuna-Flow/autodiff/linalggradients and Jacobians over linear-algebra/immutAPI · tutorial · design
polyLuna-Flow/autodiff/polyderivatives of dense and univariate sparse luna-poly polynomialsAPI · tutorial · design

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. The architecture guide shows how the packages depend on each other, and the repository conventions record the naming rules of this manual.

Reading paths

New to automatic differentiation. Start with the autodiff tutorial, then the dual tutorial, which compares dual numbers with finite differences.

Using the library. Import Luna-Flow/autodiff and keep the autodiff API and dual API at hand; add the linalg tutorial or the poly tutorial for vectors and polynomials, and the checked tutorial when failures must be values.

Contributing. Read the dual design for the algebra, the derivative rules and the error bounds that every new operation must respect, then the architecture guide and the design page of the package you change.

Installation

moon add Luna-Flow/autodiff@0.2.0

The bridges need their libraries too: moon add Luna-Flow/linear-algebra for linalg and moon add Luna-Flow/luna-poly for poly.

Toolchain

The code and the examples in this manual require MoonBit moonc 0.10 or newer and the moon.mod / moon.pkg manifest format. The test suite runs on the wasm-gc, wasm, js and native targets.

Validation

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