Skip to main content

ferrolearn_sparse/
lib.rs

1//! # ferrolearn-sparse
2//!
3//! Sparse matrix types for the ferrolearn machine learning framework.
4//!
5//! This crate provides three sparse matrix formats:
6//!
7//! - [`CsrMatrix<T>`] — Compressed Sparse Row, backed by [`sprs::CsMat<T>`] in CSR storage.
8//!   Efficient for row slicing and matrix-vector products. Implements the
9//!   [`ferrolearn_core::Dataset`] trait when `T: Float + Send + Sync + 'static`.
10//! - [`CscMatrix<T>`] — Compressed Sparse Column, backed by [`sprs::CsMat<T>`] in CSC storage.
11//!   Efficient for column slicing and transpose products.
12//! - [`CooMatrix<T>`] — Coordinate (triplet) format, backed by [`sprs::TriMat<T>`].
13//!   Convenient for incremental construction before converting to CSR/CSC.
14//!
15//! All three types support conversion between formats, conversion to/from dense
16//! [`ndarray::Array2<T>`], slicing, scalar multiplication, element-wise addition,
17//! and matrix-vector multiplication.
18//!
19//! # Quick Start
20//!
21//! ```
22//! use ferrolearn_sparse::{CooMatrix, CsrMatrix};
23//!
24//! // Build in COO format, then convert.
25//! let mut coo = CooMatrix::new(3, 3);
26//! coo.push(0, 0, 1.0_f64);
27//! coo.push(1, 2, 4.0);
28//! coo.push(2, 1, 7.0);
29//!
30//! let csr = CsrMatrix::from_coo(&coo).unwrap();
31//! let dense = csr.to_dense();
32//! assert_eq!(dense[[0, 0]], 1.0);
33//! assert_eq!(dense[[1, 2]], 4.0);
34//! assert_eq!(dense[[2, 1]], 7.0);
35//! ```
36
37pub mod coo;
38pub mod csc;
39pub mod csr;
40
41pub use coo::CooMatrix;
42pub use csc::CscMatrix;
43pub use csr::CsrMatrix;