linear-algebra

This manual documents release 0.5.1 of Luna-Flow/linear-algebra.

Overview

Luna-Flow/linear-algebra is the linear-algebra layer of Luna Flow. It provides dense matrices and vectors in two execution models, value-oriented (immut) and in-place (mutable), numerical routines for floating-point matrices, exact algorithms for integer matrices, and small capability layers (algebra, container) that let generic algorithms run on any matrix or vector type that implements them.

Release 0.5.1 centers on three changes:

  • The module builds with the MoonBit 0.10 toolchain and its moon.mod / moon.pkg manifests.
  • Indexing is provided by ordinary methods (at, set, get) with #alias("_[_]"); only the trait methods listed in each package’s extends.mbt are callable with method syntax.
  • The native OpenBLAS backend is withdrawn from the module, because its upstream binding does not compile with MoonBit 0.10 (see Outside the module).

Install

moon add Luna-Flow/linear-algebra@0.5.1

Then import the packages you use in your moon.pkg:

///|
import {
  "Luna-Flow/linear-algebra/immut",
  "Luna-Flow/linear-algebra/mutable",
  "Luna-Flow/linear-algebra/error" @la_error,
}

Generic code over scalar types also needs the upstream packages Luna-Flow/luna-generic (0.4.0) and Luna-Flow/arithmetic (0.5.1). The module needs the MoonBit toolchain 0.10.20 or later (moonc ≥ 0.10.20) and builds without warnings with moon check --target all. All packages support the wasm-gc, js, wasm and native targets; mutable has a tuned kernel file per target, and the benchmark packages run on native.

Pages

Every package of the module has a tutorial, an API reference and a design page. The package src/doc_en_us is not documented separately: it links every page of this manual as a .mbt.md file so that the examples compile and run as tests.

PartTutorialAPIDesign
immut: immutable Matrix, Vector, lazy MatrixFn; exact determinants and powerstutorialAPIdesign
mutable: in-place Matrix, Vector, views; LU, Cholesky, eigenvalues, statisticstutorialAPIdesign
error: LinearAlgebraError for every checked APItutorialAPIdesign
arithmetic: scalar operation traits and re-exportstutorialAPIdesign
algebra: structure traits for whole vectors and matrices (experimental)tutorialAPIdesign
backends/default: dense wrappers implementing the algebra traitstutorialAPIdesign
container: storage-independent operation dictionaries and generic conversions (experimental)tutorialAPIdesign
container/adapters: dictionaries for every type of this repositorytutorialAPIdesign
internal: shared shape guards (importable only inside this module)tutorialAPIdesign
consistency: cross-package agreement tests (no public items)tutorialAPIdesign
perf_support: benchmark cases, fixtures and executiontutorialAPIdesign
perf: moon bench entry packagetutorialAPIdesign
perf_runner: single-case benchmark executabletutorialAPIdesign

Guides and chapters that span packages:

PageContent
Architecturehow the packages depend on each other, layers, targets, tests
Conventionsrepository rules on top of the Luna Flow documentation standard
Integration: algebrahow an external type joins the algebra traits
Integration: containerhow an external library publishes container dictionaries

Exported items

Concrete types

  • immut: Matrix, Vector, MatrixFn, the row accessor Indexed, the aliases VecLib and VecCore, DeterminantAlgorithm, DeterminantScalar, and the top-level lin_comb
  • mutable: Matrix, Vector, the views RowView, ColView, Transpose, the row accessor Lens, the trait Tolerance, the re-exported Sqrt, and the top-level identity and lin_comb
  • backends/default: DenseMatrix, DenseVector, ImmutableDenseMatrix, ImmutableDenseVector, and the generic helpers matmul, transpose, shape_of

Numerical routines

  • Exact (immut, over Int, Int64, BigInt): determinant by Bareiss elimination, pow by repeated squaring, trace, matmul
  • Floating determinant (immut, over Float, Double): LU with partial pivoting, selected by DeterminantScalar, without a zero tolerance
  • Floating point (mutable, over Float, Double): determinant, inverse, is_invertible and rank by LU with partial pivoting, reduce_row_elimination, cholesky_decomposition, is_positive_definite, eigen for symmetric matrices, power_method, mean, variance, std_dev, frobenius_norm
  • Every operation with a runtime failure mode has a checked form returning Result[_, LinearAlgebraError] and an unchecked_* form that aborts

Capability layers

  • algebra: MatrixShape, VectorShape, AdditiveVector, VecMulVector, TransposeMatrix, AdditiveMatrix, MatMulMatrix
  • container: VectorReadOps, VectorBuildOps, VectorPersistentEditOps, VectorMutableEditOps, their Matrix* counterparts, and the algorithms vector_map, vector_convert, matrix_map, matrix_convert, matrix_transpose
  • container/adapters: one factory per type and capability, such as mutable_matrix_read_ops

Scalars and errors

  • arithmetic: Abs, ApproxEq, CheckedDiv, CheckedSqrt, CheckedCompare, and re-exports of Zero, One, Inverse, Conjugate, Sqrt and the other upstream scalar traits and types
  • error: LinearAlgebraError, LinearAlgebraErrorKind, constructors and is_* predicates

Numerical caveats

The numerical routines of mutable decide “zero” with one absolute threshold, Tolerance::tolerance() =10−11= 10^{-11}, so their results depend on the scale of the input; scale data to order one (eigen is the exception: its decisions are relative to the entries). @immut.Matrix::determinant selects Bareiss for integer scalars and pivoted LU for floating point. Its LU path has no zero tolerance and may still round, overflow or underflow; it is not a numerical-rank test. The mutable design and the immut design derive the algorithms and state these limits precisely.

Status in 0.5.1

  • algebra, the trait layer of backends/default, and container are experimental; their signatures may change.
  • Release history is in the changelog.

Outside the module

The native OpenBLAS backend (backends/openblas, with BlasMatrix, BlasVector and its container adapters) is not part of this release. Its upstream binding, Kaida-Amethyst/openblas 0.1.3, does not compile with MoonBit 0.10. The migrated source, its own documentation and a patch for the binding are preserved in contrib/openblas_backend, which is not a package of the module, is not built and is not covered by this manual. Users who need the backend can stay on 0.4.7.

Validation

Recommended release checks, from the repository root:

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

run_test.sh runs immut and consistency, and container, container/adapters, backends/default, mutable and the documentation package doc_en_us on all four targets. Set LINEAR_ALGEBRA_TEST_BENCH=1 to add the benchmark packages.