| Crates.io | oxiblas-matrix |
| lib.rs | oxiblas-matrix |
| version | 0.1.2 |
| created_at | 2025-12-27 22:19:12.33613+00 |
| updated_at | 2025-12-29 20:58:50.164251+00 |
| description | Matrix types and views for OxiBLAS |
| homepage | |
| repository | https://github.com/cool-japan/oxiblas |
| max_upload_size | |
| id | 2007917 |
| size | 360,518 |
Matrix types and views for OxiBLAS
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).
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 viewBandedMatrix<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)LazyMat<T> for deferred computation and expression templatesCowMat<T> for efficient memory managementMmapMat<T> for large out-of-core matrices (with mmap feature)nalgebra feature)Add this to your Cargo.toml:
[dependencies]
oxiblas-matrix = "0.1"
# With serialization
oxiblas-matrix = { version = "0.1", features = ["serde"] }
# With memory-mapped matrices
oxiblas-matrix = { version = "0.1", features = ["mmap"] }
# With nalgebra interop
oxiblas-matrix = { version = "0.1", features = ["nalgebra"] }
# All features
oxiblas-matrix = { version = "0.1", features = ["serde", "mmap", "nalgebra"] }
use oxiblas_matrix::Mat;
// From dimensions (initialized to zero)
let a = Mat::<f64>::zeros(3, 3);
// From a slice (column-major order)
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let b = Mat::from_slice(2, 3, &data);
// From rows (more intuitive for initialization)
let c = Mat::from_rows(&[
&[1.0, 2.0, 3.0],
&[4.0, 5.0, 6.0],
]);
// Identity matrix
let id = Mat::<f64>::identity(4);
// Diagonal matrix
let diag = Mat::from_diag(&[1.0, 2.0, 3.0, 4.0]);
use oxiblas_matrix::Mat;
let mut a = Mat::from_rows(&[
&[1.0, 2.0, 3.0],
&[4.0, 5.0, 6.0],
]);
// Element access (row, col)
let val = a[(0, 1)]; // 2.0
a[(1, 2)] = 9.0; // Modify element
// Row/column access
let row = a.row(0); // [1.0, 2.0, 3.0]
let col = a.col(1); // [2.0, 5.0]
// Dimensions
let (nrows, ncols) = (a.nrows(), a.ncols());
use oxiblas_matrix::Mat;
let a = Mat::from_rows(&[
&[1.0, 2.0, 3.0, 4.0],
&[5.0, 6.0, 7.0, 8.0],
&[9.0, 10.0, 11.0, 12.0],
]);
// Create immutable view
let view = a.as_ref();
// Submatrix view (zero-copy!)
let sub = view.submatrix(0, 0, 2, 2); // 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[(0, 0)] = 99.0;
use oxiblas_matrix::BandedMatrix;
// Tridiagonal matrix (1 subdiagonal, 1 superdiagonal)
let mut band = BandedMatrix::<f64>::new(5, 5, 1, 1);
// Set diagonal
for i in 0..5 {
band.set(i, i, 2.0);
}
// Set super/subdiagonals
for i in 0..4 {
band.set(i, i + 1, -1.0); // Superdiagonal
band.set(i + 1, i, -1.0); // Subdiagonal
}
// Use in BLAS operations
// (Much more efficient than full dense storage)
use oxiblas_matrix::{PackedMatrix, Uplo};
// Symmetric matrix in packed storage (only upper triangle stored)
let mut packed = PackedMatrix::<f64>::new(4, Uplo::Upper);
// Set elements (only upper triangle)
packed.set(0, 0, 1.0);
packed.set(0, 1, 2.0);
packed.set(0, 2, 3.0);
// ... symmetric elements accessed via symmetry
// Saves 50% memory for large symmetric matrices!
use oxiblas_matrix::{Mat, LazyMat};
let a = Mat::from_rows(&[[1.0, 2.0], [3.0, 4.0]]);
let b = Mat::from_rows(&[[5.0, 6.0], [7.0, 8.0]]);
// Build expression tree without computation
let lazy = LazyMat::add(
LazyMat::from_mat(a.as_ref()),
LazyMat::from_mat(b.as_ref()),
);
// Evaluate when needed
let result = lazy.eval(); // Now computes a + b
#[cfg(feature = "mmap")]
{
use oxiblas_matrix::MmapMat;
use std::path::Path;
// Create or open memory-mapped matrix
let path = Path::new("large_matrix.bin");
let mmap = MmapMat::<f64>::create(path, 10000, 10000)?;
// Access like regular matrix (backed by disk)
let val = mmap[(100, 200)];
// Efficient for matrices larger than RAM
}
#[cfg(feature = "nalgebra")]
{
use oxiblas_matrix::Mat;
use nalgebra as na;
// From nalgebra
let na_mat = na::DMatrix::<f64>::zeros(3, 3);
let ox_mat = Mat::from_nalgebra(&na_mat);
// To nalgebra
let back = ox_mat.to_nalgebra();
}
Standard BLAS-compatible storage:
Matrix: Storage:
[a b c] [a d b e c f]
[d e f]
m × n elementsStores 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]
(kl + ku + 1) × n where kl=lower bandwidth, ku=upper bandwidthStores 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]
n × (n + 1) / 2 (50% savings)| 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 |
All view types are zero-copy:
use oxiblas_matrix::Mat;
let a = Mat::from_rows(&[
&[1.0, 2.0, 3.0, 4.0],
&[5.0, 6.0, 7.0, 8.0],
]);
// No allocation - just pointer arithmetic
let sub1 = a.as_ref().submatrix(0, 0, 2, 2);
let sub2 = a.as_ref().submatrix(0, 2, 2, 2);
let transposed = a.as_ref().t();
// All views reference the same underlying data!
| Feature | Description | Default |
|---|---|---|
default |
Core matrix types | ✓ |
serde |
Serialization/deserialization support | |
mmap |
Memory-mapped matrices via memmap2 | |
nalgebra |
Interoperability with nalgebra |
Send and Sync where appropriateoxiblas-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
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 (requires mmap feature)# Run all tests
cargo test --package oxiblas-matrix
# With all features
cargo test --package oxiblas-matrix --all-features
# Property-based tests (QuickCheck)
cargo test --package oxiblas-matrix -- --ignored
oxiblas-core - Core traits and SIMD abstractionsoxiblas-blas - BLAS operations on matricesoxiblas-lapack - LAPACK decompositionsoxiblas - Meta-crate with unified API| Feature | oxiblas-matrix | ndarray | nalgebra |
|---|---|---|---|
| Zero-copy views | ✓ | ✓ | ✓ |
| Banded storage | ✓ | ||
| Packed storage | ✓ | ||
| Memory-mapped | ✓ | ||
| Lazy evaluation | ✓ | ||
| BLAS-compatible layout | ✓ | ✓ (optional) | |
| Pure Rust | ✓ | ✓ | ✓ |
Licensed under either of:
at your option.
Contributions are welcome! Areas of interest: