This README matches the v0.4.7 repository state. This release introduces a
storage-independent container layer for vector and matrix capabilities,
generic algorithms, and adapters across the concrete immutable, mutable,
default dense, view, and OpenBLAS representations.
For earlier release notes and repository history, see CHANGELOG.md.
- The new
containerlayer exposes read, build, persistent-edit, and mutable-edit operation dictionaries without requiring a concrete storage representation. - Generic vector/matrix map and conversion algorithms, plus matrix transpose, can now operate through container capabilities and adapters.
- Algebra integration guidance now documents shape, additive, transpose, Hadamard, and matrix-multiplication capability levels for external types.
immutno longer exposes runtime backend-selection APIs. Backend choice is now expressed by the concrete type you use, not by a runtime ADT.backends/defaultnow provides backend methodsscale,dot,axpy, andmatvecon its dense vector and matrix wrappers.backends/openblasnow exposes bothBlasMatrix[T]andBlasVector[T]forFloatandDouble, using OpenBLAS GEMM for matrix multiplication plus BLAS-backeddot,scal,axpy, andgemvfor vector and matrix-vector work.- Scalar-valued vector products and BLAS-style linear combinations remain
backend methods. They were not promoted into new
@algebratraits in this release. - The default test gate now exercises the container packages and default backend across Wasm GC, JavaScript, native, and Wasm targets.
The checked 0.4.x line keeps runtime matrix failures explicit and exposes the
first layered capability packages for backend-independent linear algebra code.
Experimental features: The
algebraandcontainercapability layers are available for integration experiments and ecosystem feedback, but their trait hierarchy, operation dictionaries, error contracts, and function signatures are not yet stable. Downstream libraries should not treat these packages as compatibility-stable public boundaries until they graduate from experimental status. The concreteimmut,mutable, and backend APIs are not covered by this experimental designation solely because they implement or provide adapters for these layers.
arithmetic: Linear-algebra-facing operation capabilities. It reuses scalar operation traits fromLuna-Flow/luna-genericandLuna-Flow/arithmetic, and adds small operation-only traits such asApproxEq,Abs,CheckedDiv,CheckedSqrt, andCheckedCompare.algebra: Semantic mathematical structure capabilities. It defines only the linear-algebra-owned structure traits such asMatrixShape,AdditiveVector,TransposeMatrix, andMatMulMatrix.container: Storage-independent read, build, persistent-edit, and mutable-edit operation dictionaries, plus generic map, conversion, and transpose algorithms. Concrete adapters live incontainer/adapters.backends/default: The reference dense backend layer. It exposes wrapper typesDenseVector/DenseMatrixovermutable, andImmutableDenseVector/ImmutableDenseMatrixoverimmut, plus backend methods for scaling, dot products, AXPY-style combinations, and matrix-vector multiplication.backends/openblas: A native-only OpenBLAS backend. It exposes the ownedBlasMatrix[T]andBlasVector[T]wrappers forFloatandDouble, uses OpenBLAS GEMM for matrix multiplication, BLAS vector kernels for backend methods likedot/axpy, and keeps backend choice explicit through the concrete type rather than a runtime selector.- Trait-driven algorithms: New backend-independent algorithms should depend
on the smallest capability they need, such as
MatrixShape,AdditiveVector,VecMulVector,TransposeMatrix, orMatMulMatrix, not directly on one concrete matrix or vector type.
The default dense implementation is a backend, not the center of the ecosystem. Algorithms should depend on minimal linear algebra traits, not concrete dense matrix/vector types.
This repository is intended to be a linear-algebra substrate for higher-level math, geometry, and solver-style libraries. Domain-specific solve, regression, or optimization workflows belong in downstream packages built on these traits, backend wrappers, and concrete matrix/vector types.
immut: Immutable, value-orientedMatrix,Vector, andMatrixFntypes for persistent data and explicit copy-on-update semantics.mutable: Execution-orientedMatrixandVectortypes with in-place updates,Transposeviews,RowView/ColView, and backend-specific implementations forjs,wasm,wasm-gc, andnative.- Shared Core, Different Execution Model: Constructors and core algebraic operators remain aligned across packages, but mutation and access semantics are intentionally different.
The default backend wrappers are built on top of these concrete types:
backends/default.DenseVector and backends/default.DenseMatrix wrap
mutable.Vector and mutable.Matrix, while
backends/default.ImmutableDenseVector and
backends/default.ImmutableDenseMatrix wrap immut.Vector and
immut.Matrix. If you want the trait-oriented default backend entry point, see
the backends/default docs.
For OpenBLAS-backed native matrix multiplication and vector kernels, use
backends/openblas explicitly; it is a
separate concrete backend, not a runtime backend option inside @immut.Matrix.
If you want to write backend-independent code against the shared abstract
layers, install linear-algebra together with the upstream scalar abstraction
packages it builds on:
moon add Luna-Flow/linear-algebra@0.4.7
moon add Luna-Flow/luna-generic@0.3.3
moon add Luna-Flow/arithmetic@0.2.2Then import the packages with explicit aliases in your moon.pkg:
import {
"Luna-Flow/linear-algebra/algebra",
"Luna-Flow/linear-algebra/arithmetic" @la_arithmetic,
"Luna-Flow/luna-generic" @lf_alg,
"Luna-Flow/arithmetic" @lf_arith,
}Use @algebra for linear-algebra structure traits, @la_arithmetic for
linear-algebra-facing operation traits, @lf_alg for shared upstream algebraic
abstractions, and @lf_arith for shared upstream arithmetic types such as
ArithmeticContext.
- Checked Matrix Contracts: Shape, exponent, empty-matrix, and singular
matrix failures are now represented by
LinearAlgebraErroron the checked matrix APIs. - Legacy Behavior Is Explicit:
unchecked_*methods preserve the previous aborting behavior, andunchecked_inversepreserves the previousOption-returning inverse contract. - Public Error Package:
linear-algebra/errorexposesLinearAlgebraError,LinearAlgebraErrorKind, constructors, andis_*predicates for callers that need structured error handling. - Shared Square-Root Capability: Numerical matrix APIs now use
Luna-Flow/arithmetic.Sqrtinstead of a package-local trait.mutablere-exports the shared trait for source-level discoverability. - Target-Side Integral Embedding: Generic integer conversions use
IntegralHomomorphism::from_integral, matching the currentLuna-Flow/luna-genericalgebraic model. - Ecosystem-Oriented Constraints: Custom scalar types can implement the shared Luna Flow traits once and use them across compatible ecosystem packages.
- Backend Consistency: Native, JS, Wasm, and Wasm GC matrix implementations use the same arithmetic capability identity and explicit trait invocation.
- Compatibility Boundary:
Toleranceremains amutablepackage trait in this release; it has not yet moved toarithmetic. - Backend Choice:
@immut.Matrixdoes not expose a runtime backend selector. Choosebackends/defaultfor the repository dense wrappers orbackends/openblasfor the native-only OpenBLAS matrix wrapper.
- Core Algebraic API: Shared operations such as
make,transpose,+,-,*,trace, and matrix/vector conversions are intended to stay semantically aligned acrossimmutandmutable. - Checked vs. Unchecked: Prefer checked methods in user-facing code. Use
unchecked_*only when shape and domain preconditions are already enforced by surrounding logic. - Random Access: In
mutable, for high-performance random access, prefer.get(i, j)and.set(i, j, val)directly. - Structured Views: For repeated row or column work in
mutable, preferrow_view()/col_view()instead of relying onmatrix[row]convenience syntax. - Strict Bounds: Public matrix, view, and transpose accessors consistently reject out-of-bounds indices, including
0xNandNx0edge cases. - MatrixFn Alignment:
immut.MatrixFnnow shares the same non-negative dimension and empty-matrix semantics as the concrete matrix implementations. - Public Surface: Internal decomposition helpers remain implementation details. Package users should rely on the documented public matrix methods instead.
- Mutable & Immutable Support: Full
MatrixandVectorsuites with distinct semantics for value-oriented and execution-oriented workloads. - Advanced Operations: Includes determinant, inverse, rank, Cholesky decomposition, eigen-related routines, row elimination, transpose views, and matrix/vector conversions.
- Shared Data Model, Backend-Tuned Kernels:
mutablestill ships backend-tuned execution paths for Native, Wasm, JS, and Wasm GC targets, but the core matrix storage model is now unified. - Benchmark Infrastructure:
bench/,src/perf_support, andsrc/perf_runnernow form a full steady-state benchmarking subsystem for backend comparison and diagnostic replay. - Correctness First: Coverage now includes immutable laws, cross-package consistency checks, determinant/rank/inverse alignment, and regression tests for numerical behavior.
- Auditable Public Contracts: Bounds behavior, swap semantics, benchmark fixtures, and documentation are now tracked more explicitly as part of the repository’s correctness story.
perf: Benchmark entry package used bymoon benchfor the steady-state matrix suite.perf_support: Public fixture metadata, case registry, runtime loaders, and checksum-oriented execution helpers for benchmark cases.perf_runner: Single-case diagnostic and sampling runner used for replay, local investigation, and richer benchmark artifact generation.
These benchmark-facing packages are part of the local performance-analysis
tooling. They are not part of the default CI or publish acceptance gate unless
you explicitly opt in with LINEAR_ALGEBRA_TEST_BENCH=1.
///|
test "linear-algebra basic workflow" {
let imm = @immut.Matrix::from_2d_array([[1, 2], [3, 4]])
let imm_updated = imm.set(0, 1, 9)
inspect(imm_updated, content="|1, 9|\n|3, 4|")
let m = @mutable.Matrix::from_2d_array([[1.0, 2.0], [3.0, 4.0]])
m.set(0, 1, 9.0)
inspect(m.determinant().unwrap(), content="-23")
inspect(m.inverse() is Ok(_), content="true")
inspect(m.row_view(0)[1], content="9")
}- General application developers: Start with
mutableandimmut. These are the concrete APIs for application code such as business tools, utilities, numeric processing, small games, and visualization logic. - Math library / general algorithm developers: Read in this order:
arithmetic->algebra->container->backends/default->backends/openblas->immut/mutable. Start from operation capabilities, then structure capabilities, then the default backend wrappers, then the optional OpenBLAS native wrapper, and finally the concrete implementations. This is the intended entry path if you are building a higher-level linear-algebra application library, geometry package, or solver-style library on top of this repository.
immutconcrete API:immut.MatrixAPI,immut.Matrixtutorial,immut.VectorAPI,immut.Vectortutorialmutableconcrete API:mutable.MatrixAPI,mutable.Matrixtutorial,mutable.VectorAPI,mutable.Vectortutorial- Capability and backend layers:
arithmeticAPI,algebraAPI,algebraecosystem integration,algebratutorial,containerAPI,containertutorial,containerecosystem integration,backends/defaultAPI,backends/openblasAPI,backends/openblastutorial,errorAPI
Luna-Flow/geometry3d: a compact MoonBit 3D geometry foundation built onLuna-Flow/linear-algebra, with core geometry, camera/view math, backend-neutral frontend rendering, and TUI / Canvas / GSAP backends. See its English docs for a concrete downstream package layout built on this repository.
Comprehensive API documentation is available at mooncakes.io.
We provide documentation in multiple languages:
- 🇺🇸 English (
doc/en_US) - 🇨🇳 简体中文 (
doc/zh_CN) - 🇯🇵 日本語 (
doc/ja_JP)
doc/* is the hand-written documentation source. The src/doc_* packages are
MoonBit documentation exposure layers made of symlinks back to doc/*.
Localized README files:
Older release notes, historical version summaries, and pre-0.4.7 repository
highlights now live in CHANGELOG.md. This README keeps the
current baseline and entry points front and center.
Useful local commands:
moon fmt
moon info
moon check
moon test -p perf_support
moon test -p perf_runner
moon test --enable-coverage
./run_test.sh
LINEAR_ALGEBRA_TEST_BENCH=1 ./run_test.shrun_test.sh runs the default repository gate: immut, consistency,
container, container/adapters, backends/default, and mutable, with the
container, default-backend, and mutable packages covered on wasm-gc, js,
native, and wasm.
perf_support and perf_runner stay opt-in for local fixture-recovery checks
and performance diagnostics. Run them explicitly with moon test -p ... or use
LINEAR_ALGEBRA_TEST_BENCH=1 ./run_test.sh when you want that path.
Runnable entry points:
# This repository is primarily a library, so use an explicit package target.
moon run src/perf_runner mul_baseline_dense_64
# Optional: materialize benchmark fixtures ahead of time.
python3 bench/generate_fixtures.py
# Full benchmark flow.
just benchmoon run src/perf_runner ... defaults to bench/datasets/cases/<case-id>.json.
If that fixture file is missing on a clean checkout, perf_support will
recreate it automatically from the tracked dataset registry before executing the
case.
Before triggering the publish workflow:
- Bump
moon.modto the intended next release version before publishing. - Update
README.mdandCHANGELOG.mdso the current release notes and historical version notes match the package contents. - Run
moon check --target alland./run_test.sh; both are required before publishing. - If the change touches benchmark fixtures, fixture recovery, or diagnostic runners, also run
LINEAR_ALGEBRA_TEST_BENCH=1 ./run_test.sh. - Trigger
publish-package; it will publish the version currently declared inmoon.mod.
If the workflow reports a duplicate version, the package manager already contains that version and a new version bump is required.
Contribution guidance is available in CONTRIBUTING.md.