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.pkgmanifests. - Indexing is provided by ordinary methods (
at,set,get) with#alias("_[_]"); only the trait methods listed in each package’sextends.mbtare 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.
| Part | Tutorial | API | Design |
|---|---|---|---|
immut: immutable Matrix, Vector, lazy MatrixFn; exact determinants and powers | tutorial | API | design |
mutable: in-place Matrix, Vector, views; LU, Cholesky, eigenvalues, statistics | tutorial | API | design |
error: LinearAlgebraError for every checked API | tutorial | API | design |
arithmetic: scalar operation traits and re-exports | tutorial | API | design |
algebra: structure traits for whole vectors and matrices (experimental) | tutorial | API | design |
backends/default: dense wrappers implementing the algebra traits | tutorial | API | design |
container: storage-independent operation dictionaries and generic conversions (experimental) | tutorial | API | design |
container/adapters: dictionaries for every type of this repository | tutorial | API | design |
internal: shared shape guards (importable only inside this module) | tutorial | API | design |
consistency: cross-package agreement tests (no public items) | tutorial | API | design |
perf_support: benchmark cases, fixtures and execution | tutorial | API | design |
perf: moon bench entry package | tutorial | API | design |
perf_runner: single-case benchmark executable | tutorial | API | design |
Guides and chapters that span packages:
| Page | Content |
|---|---|
| Architecture | how the packages depend on each other, layers, targets, tests |
| Conventions | repository rules on top of the Luna Flow documentation standard |
| Integration: algebra | how an external type joins the algebra traits |
| Integration: container | how an external library publishes container dictionaries |
Exported items
Concrete types
immut:Matrix,Vector,MatrixFn, the row accessorIndexed, the aliasesVecLibandVecCore,DeterminantAlgorithm,DeterminantScalar, and the top-levellin_combmutable:Matrix,Vector, the viewsRowView,ColView,Transpose, the row accessorLens, the traitTolerance, the re-exportedSqrt, and the top-levelidentityandlin_combbackends/default:DenseMatrix,DenseVector,ImmutableDenseMatrix,ImmutableDenseVector, and the generic helpersmatmul,transpose,shape_of
Numerical routines
- Exact (
immut, overInt,Int64,BigInt):determinantby Bareiss elimination,powby repeated squaring,trace,matmul - Floating determinant (
immut, overFloat,Double): LU with partial pivoting, selected byDeterminantScalar, without a zero tolerance - Floating point (
mutable, overFloat,Double):determinant,inverse,is_invertibleandrankby LU with partial pivoting,reduce_row_elimination,cholesky_decomposition,is_positive_definite,eigenfor 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 anunchecked_*form that aborts
Capability layers
algebra:MatrixShape,VectorShape,AdditiveVector,VecMulVector,TransposeMatrix,AdditiveMatrix,MatMulMatrixcontainer:VectorReadOps,VectorBuildOps,VectorPersistentEditOps,VectorMutableEditOps, theirMatrix*counterparts, and the algorithmsvector_map,vector_convert,matrix_map,matrix_convert,matrix_transposecontainer/adapters: one factory per type and capability, such asmutable_matrix_read_ops
Scalars and errors
arithmetic:Abs,ApproxEq,CheckedDiv,CheckedSqrt,CheckedCompare, and re-exports ofZero,One,Inverse,Conjugate,Sqrtand the other upstream scalar traits and typeserror:LinearAlgebraError,LinearAlgebraErrorKind, constructors andis_*predicates
Numerical caveats
The numerical routines of mutable decide “zero” with one absolute threshold,
Tolerance::tolerance() , 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.
Where to read next
- New to the package: start with the mutable tutorial
for numerical work on
Doublematrices, or the immut tutorial for value semantics and exact integer results. Read the error tutorial next, since every operation that can fail returns aLinearAlgebraError. - Using it in a library: read the algebra tutorial and the backends/default tutorial to write generic code, then the container tutorial for moving data between representations, and the integration guides (algebra, container) before publishing implementations for your own types. Keep the API pages at hand; each item lists its errors, edge cases and cost.
- Contributing: read the architecture guide, the conventions, the design pages of the package you change, and the internal and consistency tutorials. Benchmarks are covered by the perf pages.
Status in 0.5.1
algebra, the trait layer ofbackends/default, andcontainerare 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.