oxiblas-matrix
Matrix types and views for OxiBLAS
Overview
oxiblas-matrix provides efficient, type-safe matrix types and views for the OxiBLAS library. It implements zero-copy views, lazy evaluation, and various matrix storage formats (dense, banded, packed, symmetric, triangular).
Features
Core Matrix Types
Mat<T>- Owned, column-major dense matrixMatRef<'a, T>- Immutable view into a matrixMatMut<'a, T>- Mutable view into a matrixDiagRef<'a, T>- Diagonal matrix view
Specialized Storage
BandedMatrix<T>- Banded matrix storage (efficient for tridiagonal, pentadiagonal, etc.)PackedMatrix<T>- Packed storage for symmetric/triangular matrices (saves 50% memory)SymmetricMatrix<T>- Symmetric matrix with upper/lower storageTriangularMatrix<T>- Triangular matrix (upper/lower with unit/non-unit diagonal)
Advanced Features
- Lazy evaluation -
LazyMat<T>for deferred computation and expression templates - Copy-on-write -
CowMat<T>for efficient memory management - Memory-mapped matrices -
MmapMat<T>for large out-of-core matrices (withmmapfeature) - Nalgebra compatibility - Convert to/from nalgebra types (with
nalgebrafeature)
Safety & Performance
- Zero-copy views - No allocation for submatrices
- Compile-time size checks - Where possible using const generics
- SIMD-friendly layout - Proper alignment for vectorization
- Cache-aware - Column-major (Fortran) order for BLAS compatibility
- Prefetching support - Manual prefetch hints for performance-critical code
Installation
Add this to your Cargo.toml:
[]
= "0.1"
# With serialization
= { = "0.1", = ["serde"] }
# With memory-mapped matrices
= { = "0.1", = ["mmap"] }
# With nalgebra interop
= { = "0.1", = ["nalgebra"] }
# All features
= { = "0.1", = ["serde", "mmap", "nalgebra"] }
Usage
Creating Matrices
use Mat;
// From dimensions (initialized to zero)
let a = zeros;
// From a slice (column-major order)
let data = vec!;
let b = from_slice;
// From rows (more intuitive for initialization)
let c = from_rows;
// Identity matrix
let id = identity;
// Diagonal matrix
let diag = from_diag;
Matrix Access
use Mat;
let mut a = from_rows;
// Element access (row, col)
let val = a; // 2.0
a = 9.0; // Modify element
// Row/column access
let row = a.row; // [1.0, 2.0, 3.0]
let col = a.col; // [2.0, 5.0]
// Dimensions
let = ;
Matrix Views
use Mat;
let a = from_rows;
// Create immutable view
let view = a.as_ref;
// Submatrix view (zero-copy!)
let sub = view.submatrix; // Top-left 2×2 block
// Transpose view (zero-copy!)
let at = view.t;
// Mutable view
let mut b = a.clone;
let mut_view = b.as_mut;
mut_view = 99.0;
Banded Matrices
use BandedMatrix;
// Tridiagonal matrix (1 subdiagonal, 1 superdiagonal)
let mut band = new;
// Set diagonal
for i in 0..5
// Set super/subdiagonals
for i in 0..4
// Use in BLAS operations
// (Much more efficient than full dense storage)
Packed Matrices
use ;
// Symmetric matrix in packed storage (only upper triangle stored)
let mut packed = new;
// Set elements (only upper triangle)
packed.set;
packed.set;
packed.set;
// ... symmetric elements accessed via symmetry
// Saves 50% memory for large symmetric matrices!
Lazy Evaluation
use ;
let a = from_rows;
let b = from_rows;
// Build expression tree without computation
let lazy = add;
// Evaluate when needed
let result = lazy.eval; // Now computes a + b
Memory-Mapped Matrices
Nalgebra Interop
Matrix Storage Formats
Dense (Column-Major)
Standard BLAS-compatible storage:
Matrix: Storage:
[a b c] [a d b e c f]
[d e f]
- Memory:
m × nelements - Cache: Column-wise access is cache-friendly
- Use for: General matrices
Banded
Stores only diagonals:
Matrix: Storage (banded):
[a b 0 0] [0 a b c d]
[e f g 0] [a b c d 0]
[0 h i j] [b c d 0 0]
[0 0 k l]
- Memory:
(kl + ku + 1) × nwhere kl=lower bandwidth, ku=upper bandwidth - Cache: Excellent for narrow banded systems
- Use for: Tridiagonal, pentadiagonal, sparse banded systems
Packed (Symmetric/Triangular)
Stores only upper or lower triangle:
Symmetric: Packed storage:
[a b c] [a b c d e f]
[b d e] (only upper triangle)
[c e f]
- Memory:
n × (n + 1) / 2(50% savings) - Cache: Compact storage
- Use for: Symmetric or triangular matrices
Performance Characteristics
| Operation | Dense | Banded (k<<n) | Packed |
|---|---|---|---|
| Element access | O(1) | O(1) | O(1) |
| Row access | O(n) | O(k) | O(n) |
| Memory (n×n) | n² | n×k | n×(n+1)/2 |
| BLAS ops | Optimized | Optimized | Optimized |
Zero-Copy Design
All view types are zero-copy:
use Mat;
let a = from_rows;
// No allocation - just pointer arithmetic
let sub1 = a.as_ref.submatrix;
let sub2 = a.as_ref.submatrix;
let transposed = a.as_ref.t;
// All views reference the same underlying data!
Feature Flags
| Feature | Description | Default |
|---|---|---|
default |
Core matrix types | ✓ |
serde |
Serialization/deserialization support | |
mmap |
Memory-mapped matrices via memmap2 | |
nalgebra |
Interoperability with nalgebra |
Safety
- Bounds checking - All indexing operations check bounds in debug mode
- No unsafe in public API - Unsafe code is internal and carefully audited
- Lifetime safety - Views cannot outlive their underlying data
- Thread safety -
SendandSyncwhere appropriate
Architecture
oxiblas-matrix/
├── mat.rs # Owned Mat<T> type
├── mat_ref.rs # Immutable MatRef<'a, T>
├── mat_mut.rs # Mutable MatMut<'a, T>
├── banded.rs # BandedMatrix<T>
├── packed.rs # PackedMatrix<T>
├── symmetric.rs # SymmetricMatrix<T>
├── triangular.rs # TriangularMatrix<T>
├── lazy.rs # LazyMat<T> expression templates
├── cow.rs # CowMat<T> copy-on-write
├── mmap.rs # MmapMat<T> (feature-gated)
├── nalgebra_compat.rs # Nalgebra conversions (feature-gated)
├── ops.rs # Operator overloads
└── prefetch.rs # Prefetch utilities
Examples
See the examples directory in the main repository:
matrix_basics.rs- Creating and manipulating matricesmatrix_views.rs- Zero-copy submatrix viewsbanded_matrices.rs- Working with banded storagemmap_matrices.rs- Large out-of-core matrices (requiresmmapfeature)
Testing
# Run all tests
# With all features
# Property-based tests (QuickCheck)
Related Crates
oxiblas-core- Core traits and SIMD abstractionsoxiblas-blas- BLAS operations on matricesoxiblas-lapack- LAPACK decompositionsoxiblas- Meta-crate with unified API
Comparison with Other Libraries
| Feature | oxiblas-matrix | ndarray | nalgebra |
|---|---|---|---|
| Zero-copy views | ✓ | ✓ | ✓ |
| Banded storage | ✓ | ||
| Packed storage | ✓ | ||
| Memory-mapped | ✓ | ||
| Lazy evaluation | ✓ | ||
| BLAS-compatible layout | ✓ | ✓ (optional) | |
| Pure Rust | ✓ | ✓ | ✓ |
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contributing
Contributions are welcome! Areas of interest:
- Sparse matrix views - Integration with oxiblas-sparse
- Block matrix - Hierarchical block structure
- Strided matrices - Non-contiguous storage patterns
- GPU support - CUDA/ROCm memory views
References
- BLAS Standard - Matrix storage conventions
- LAPACK Documentation - Packed and banded formats
- ndarray - General-purpose array library
- nalgebra - Linear algebra library