Expand description
ยงla-stack
Fast, stack-allocated linear algebra for fixed dimensions in Rust.
This crate grew from the need to support delaunay with fast, stack-allocated linear algebra primitives and algorithms
while keeping the API intentionally small and explicit.
ยง๐ Introduction
la-stack provides a handful of const-generic, stack-backed building blocks:
Vector<const D: usize>for fixed-length vectors ([f64; D]today)Matrix<const D: usize>for fixed-size square matrices ([[f64; D]; D]today)Lu<const D: usize>for LU factorization with partial pivoting (solve + det)
ยงโจ Design goals
- โ Const-generic dimensions (no dynamic sizes)
- โ Stack storage only (no heap allocation in core types)
- โ
Copytypes where possible - โ Explicit algorithms (LU, solve, determinant)
- โ
unsafeforbidden - โ No runtime dependencies (dev-dependencies are for contributors only)
ยง๐ข Scalar types
Today, the core types are implemented for f64. The intent is to support f32 and f64
(and f128 if/when Rust gains a stable primitive for it). Longer term, we may add optional
arbitrary-precision support (e.g. via rug) depending on performance.
ยง๐ Quickstart
Add this to your Cargo.toml:
[dependencies]
la-stack = "0.1"Solve a 5ร5 system via LU:
use la_stack::prelude::*;
// This system requires pivoting (a[0][0] = 0), so it's a good LU demo.
// A = J - I: zeros on diagonal, ones elsewhere.
let a = Matrix::<5>::from_rows([
[0.0, 1.0, 1.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0, 1.0],
[1.0, 1.0, 0.0, 1.0, 1.0],
[1.0, 1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 1.0, 1.0, 0.0],
]);
let b = Vector::<5>::new([14.0, 13.0, 12.0, 11.0, 10.0]);
let lu = a.lu(DEFAULT_PIVOT_TOL).unwrap();
let x = lu.solve_vec(b).unwrap().into_array();
// Floating-point rounding is expected; compare with a tolerance.
let expected = [1.0, 2.0, 3.0, 4.0, 5.0];
for (x_i, e_i) in x.iter().zip(expected.iter()) {
assert!((*x_i - *e_i).abs() <= 1e-12);
}ยง๐งฉ API at a glance
| Type | Storage | Purpose | Key methods |
|---|---|---|---|
Vector<D> | [f64; D] | Fixed-length vector | new, zero, dot, norm2_sq |
Matrix<D> | [[f64; D]; D] | Fixed-size square matrix | from_rows, zero, identity, lu, det |
Lu<D> | Matrix<D> + pivot array | Factorization for solves/det | solve_vec, det |
Storage shown above reflects the current f64 implementation.
ยง๐ Examples
The examples/ directory contains small, runnable programs:
just examples
# or:
cargo run --example solve_5x5
cargo run --example det_5x5ยง๐ค Contributing
A short contributor workflow:
cargo install just
just ci # lint + fast tests + bench compile
just commit-check # lint + all tests + examplesFor the full set of developer commands, see just --list and WARP.md.
ยง๐ License
BSD 3-Clause License. See LICENSE.
Modulesยง
- prelude
- Common imports for ergonomic usage.
Structsยง
- Lu
- LU decomposition (PA = LU) with partial pivoting.
- Matrix
- Fixed-size square matrix
DรD, stored inline. - Vector
- Fixed-size vector of length
D, stored inline.
Enumsยง
- LaError
- Linear algebra errors.
Constantsยง
- DEFAULT_
PIVOT_ TOL - Default absolute pivot tolerance used for singularity detection.