oxiblas_sparse/
lib.rs

1//! OxiBLAS Sparse - Sparse matrix support.
2//!
3//! This crate provides sparse matrix formats and operations:
4//!
5//! - **CSR**: Compressed Sparse Row format
6//! - **CSC**: Compressed Sparse Column format
7//! - **COO**: Coordinate format (for construction)
8//! - **DIA**: Diagonal format (for banded matrices)
9//! - **ELL**: ELLPACK format (for GPU computation)
10//! - **BSR**: Block Sparse Row format (for block-structured matrices)
11//! - **BSC**: Block Sparse Column format (for column-oriented block structure)
12//! - **HYB**: Hybrid ELL+COO format (for irregular sparsity patterns)
13//! - **SELL**: Sliced ELLPACK format (for GPU-optimized row-variable matrices)
14//!
15//! # Sparse Matrix Formats
16//!
17//! ## CSR (Compressed Sparse Row)
18//!
19//! Efficient for row-wise operations and matrix-vector products.
20//! Stores values row by row.
21//!
22//! ## CSC (Compressed Sparse Column)
23//!
24//! Efficient for column-wise operations and direct solvers.
25//! Stores values column by column.
26//!
27//! ## DIA (Diagonal)
28//!
29//! Efficient for banded matrices (tridiagonal, pentadiagonal, etc.).
30//! Stores diagonals explicitly.
31//!
32//! ## ELL (ELLPACK)
33//!
34//! Efficient for GPU computation with uniform row lengths.
35//! Fixed number of entries per row.
36//!
37//! ## BSR (Block Sparse Row)
38//!
39//! Efficient for block-structured matrices (FEM, etc.).
40//! Stores dense blocks in CSR-like structure.
41//!
42//! # Example
43//!
44//! ```
45//! use oxiblas_sparse::{CsrMatrix, CscMatrix, DiaMatrix, EllMatrix, BsrMatrix};
46//!
47//! // Create a sparse matrix in CSR format
48//! // [1 0 2]
49//! // [0 3 0]
50//! // [4 0 5]
51//! let values = vec![1.0, 2.0, 3.0, 4.0, 5.0];
52//! let col_indices = vec![0, 2, 1, 0, 2];
53//! let row_ptrs = vec![0, 2, 3, 5];
54//!
55//! let csr = CsrMatrix::new(3, 3, row_ptrs, col_indices, values).unwrap();
56//! assert_eq!(csr.nnz(), 5);
57//!
58//! // Convert to CSC
59//! let csc = csr.to_csc();
60//! assert_eq!(csc.nnz(), 5);
61//!
62//! // Create a tridiagonal matrix in DIA format
63//! let sub = vec![1.0, 1.0];
64//! let main = vec![2.0, 2.0, 2.0];
65//! let super_diag = vec![1.0, 1.0];
66//! let dia = DiaMatrix::tridiagonal(sub, main, super_diag).unwrap();
67//! assert_eq!(dia.ndiag(), 3);
68//! ```
69
70#![warn(missing_docs)]
71#![warn(clippy::all)]
72#![allow(clippy::module_name_repetitions)]
73// Sparse library uses Clone::clone() for generic T where Copy isn't always available
74#![allow(clippy::clone_on_copy)]
75// Manual assign patterns are often clearer in numerical code
76#![allow(clippy::assign_op_pattern)]
77// Numerical code often has complex generic types for performance
78#![allow(clippy::type_complexity)]
79// Loop index variables are common in matrix operations
80#![allow(clippy::needless_range_loop)]
81// Sparse functions have many parameters by design
82#![allow(clippy::too_many_arguments)]
83// Partial ordering comparisons are intentional
84#![allow(clippy::neg_cmp_op_on_partial_ord)]
85// Bounds in two places for clarity in generic numerical code
86#![allow(clippy::multiple_bound_locations)]
87// Manual slice copying for explicit control
88#![allow(clippy::manual_memcpy)]
89// Vec vs slice in internal APIs is acceptable
90#![allow(clippy::ptr_arg)]
91// Iterative solver implementations may have unusual control flow
92#![allow(clippy::never_loop)]
93#![allow(clippy::collapsible_if)]
94#![allow(clippy::iter_cloned_collect)]
95#![allow(clippy::only_used_in_recursion)]
96#![allow(clippy::manual_strip)]
97#![allow(clippy::doc_overindented_list_items)]
98#![allow(clippy::unnecessary_unwrap)]
99
100pub mod bsc;
101pub mod bsr;
102pub mod convert;
103pub mod coo;
104pub mod csc;
105pub mod csr;
106pub mod dia;
107pub mod ell;
108pub mod graph;
109pub mod hyb;
110pub mod linalg;
111pub mod mtx;
112pub mod ops;
113pub mod sell;
114
115pub use bsc::{BscError, BscMatrix};
116pub use bsr::{BsrError, BsrMatrix, DenseBlock};
117pub use convert::{
118    RecommendedFormat,
119    SparsityAnalysis,
120    // Analysis utilities
121    analyze_sparsity_pattern,
122    // New format conversions
123    bsc_to_bsr,
124    bsc_to_csr,
125    bsr_to_bsc,
126    // Core conversions
127    bsr_to_csr,
128    bsr_to_dia,
129    bsr_to_ell,
130    coo_to_csc,
131    coo_to_csr,
132    csc_to_coo,
133    csc_to_csr,
134    csr_to_bsc,
135    csr_to_bsr,
136    csr_to_coo,
137    csr_to_csc,
138    csr_to_dia,
139    csr_to_ell,
140    csr_to_hyb,
141    csr_to_sell,
142    dia_to_bsr,
143    dia_to_csr,
144    dia_to_ell,
145    ell_to_bsr,
146    ell_to_csr,
147    ell_to_dia,
148    ell_to_hyb,
149    hyb_to_csr,
150    hyb_to_ell,
151    sell_to_csr,
152};
153pub use coo::{CooMatrix, CooMatrixBuilder};
154pub use csc::CscMatrix;
155pub use csr::CsrMatrix;
156pub use dia::{DiaError, DiaMatrix};
157pub use ell::{EllError, EllMatrix};
158pub use graph::{
159    BandwidthProfileResult, BipartiteMatchingResult, BipartiteResult, ConnectedComponentsResult,
160    LevelSetResult, PartitionResult, WeightedMatchingResult, bandwidth_profile,
161    bandwidth_profile_csc, bipartite_matching, connected_components, connected_components_csc,
162    degree_sequence, is_bipartite, is_structurally_symmetric, level_sets, partition_graph_bisect,
163    partition_graph_kway, pseudo_peripheral_vertex, weighted_bipartite_matching,
164};
165pub use hyb::{HybError, HybMatrix, HybStats, HybWidthStrategy};
166pub use mtx::{
167    MtxError, MtxField, MtxFormat, MtxHeader, MtxObject, MtxSymmetry, read_matrix_market,
168    read_matrix_market_coo, read_matrix_market_str, write_matrix_market, write_matrix_market_str,
169    write_matrix_market_symmetric,
170};
171pub use sell::{SellError, SellMatrix, SellStats, SliceSize};
172
173/// Prelude module for convenient imports.
174pub mod prelude {
175    pub use crate::bsc::BscMatrix;
176    pub use crate::bsr::{BsrMatrix, DenseBlock};
177    pub use crate::coo::{CooMatrix, CooMatrixBuilder};
178    pub use crate::csc::CscMatrix;
179    pub use crate::csr::CsrMatrix;
180    pub use crate::dia::DiaMatrix;
181    pub use crate::ell::EllMatrix;
182    pub use crate::graph::{
183        BandwidthProfileResult, BipartiteMatchingResult, BipartiteResult,
184        ConnectedComponentsResult, LevelSetResult, PartitionResult, WeightedMatchingResult,
185        bandwidth_profile, bipartite_matching, connected_components, degree_sequence, is_bipartite,
186        is_structurally_symmetric, level_sets, partition_graph_bisect, partition_graph_kway,
187        pseudo_peripheral_vertex, weighted_bipartite_matching,
188    };
189    pub use crate::hyb::{HybMatrix, HybWidthStrategy};
190    pub use crate::linalg::prelude::*;
191    pub use crate::ops::{spmm, spmm_sparse, spmv};
192    pub use crate::sell::{SellMatrix, SliceSize};
193}