axb
A lightweight, zero-dependency linear algebra library for Rust with compile-time dimension checking using const generics.
Features
- š Compile-time dimension checking - Catch dimension mismatches at compile time
- š¦ Zero dependencies - No external dependencies for the core library
- š”ļø Result-based error handling - No panics, all errors are returned as
Result - ā Operator overloading - Natural syntax with
+,-,*operators - š Matrix factorizations - QR, LU, and LDU decompositions
- š§® Comprehensive operations - Determinant, inverse, transpose, trace, rank, and more
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Quick Start
use Matrix;
Constructors
use Matrix;
// From 2D array
let m = new;
// From nested Vec
let v = vec!;
let m = from_vec;
// Special matrices
let zeros = zeros;
let identity = identity;
let diagonal = diagonal;
let filled = fill;
Matrix Factorizations
use Matrix;
let m = new;
// QR Factorization (A = QR)
let = m.qr.unwrap;
// LU Factorization (A = LU)
let = m.lu.unwrap;
// LDU Factorization (A = LDU)
let = m.ldu.unwrap;
Solving Linear Systems
use Matrix;
// Solve Ax = b
let a = new;
let b = new;
let x = a.solve.unwrap;
// x ā [1.0, 3.0]
Vector Operations
use Matrix;
let v1 = new;
let v2 = new;
let dot = v1.dot; // 32.0
let norm = v1.norm; // ā14
let unit = v1.normalize.unwrap;
let cross = v1.cross; // 3D cross product
Matrix Properties
use Matrix;
let m = new;
m.is_symmetric; // true
m.is_orthogonal; // false
m.is_projection; // false
m.rank; // matrix rank
m.frobenius_norm; // Frobenius norm
Error Handling
All fallible operations return Result<T, MatrixError>:
use ;
let singular = new;
match singular.inverse
// Non-square matrix operations
let rect = new;
assert!; // NonSquare error
API Reference
Basic Operations
| Method | Description |
|---|---|
new(arr) |
Create from 2D array |
from_vec(v) |
Create from nested Vec |
zeros() |
Create zero matrix |
identity() |
Create identity matrix |
diagonal(values) |
Create diagonal matrix |
transpose() |
Transpose matrix |
determinant() |
Compute determinant |
inverse() |
Compute inverse |
trace() |
Sum of diagonal elements |
rank() |
Compute matrix rank |
pow(n) |
Matrix exponentiation |
Factorizations
| Method | Description |
|---|---|
qr() |
QR factorization |
lu() |
LU factorization |
ldu() |
LDU factorization |
Vector Operations (for Matrix<R, 1>)
| Method | Description |
|---|---|
norm() |
Euclidean norm |
normalize() |
Unit vector |
dot(other) |
Dot product |
cross(other) |
Cross product (3D only) |
Properties
| Method | Description |
|---|---|
is_symmetric() |
Check if A = A^T |
is_orthogonal() |
Check if A * A^T = I |
is_projection() |
Check if P² = P |
frobenius_norm() |
Frobenius norm |
Minimum Supported Rust Version
This crate requires Rust 1.51 or later (for const generics).
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.