1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (C) 2020-2025 algebra-sparse authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # algebra-sparse
//!
//! A sparse linear algebra library built on top of [nalgebra], providing efficient sparse matrix
//! and vector operations.
//!
//! ## Features
//!
//! - **Compressed Sparse Matrix Storage**: CSR (Compressed Sparse Row) and CSC (Compressed Sparse
//! Column) formats
//! - **Block Diagonal Matrices**: Efficient storage and operations for block diagonal matrices
//! - **Matrix Operations**: Sparse-sparse, sparse-dense, and sparse-vector multiplications
//! - **Zero Threshold Support**: Automatic filtering of near-zero values for memory efficiency
//! - **View-based API**: Efficient borrowing and slicing without allocation
//! - **nalgebra Integration**: Seamless conversion between sparse and dense representations
//!
//! ## Quick Start
//!
//! ```rust
//! use algebra_sparse::CsrMatrix;
//! use nalgebra::DMatrix;
//!
//! // Create a dense matrix and convert to sparse
//! let dense = DMatrix::from_row_slice(3, 3, &[
//! 1.0, 0.0, 2.0,
//! 0.0, 3.0, 0.0,
//! 4.0, 0.0, 5.0,
//! ]);
//! let sparse = CsrMatrix::from_dense(dense.as_view());
//!
//! // Perform sparse matrix operations
//! let result = sparse.as_view() * dense.column(0);
//! println!("Result: {:?}", result);
//! ```
//!
//! ## Matrix Formats
//!
//! This library supports several sparse matrix formats optimized for different use cases:
//!
//! - **CSR (Compressed Sparse Row)**: Efficient for row-wise operations and matrix-vector products
//! - **CSC (Compressed Sparse Column)**: Efficient for column-wise operations
//! - **Block Diagonal**: Optimized for block diagonal structure common in physical simulations
//!
//! ## Performance Considerations
//!
//! - Zero values below the threshold are automatically filtered during construction
//! - View-based operations avoid unnecessary allocations
//! - Sparse-sparse operations use efficient merging algorithms
//! - Block diagonal operations leverage structure for optimal performance
extern crate nalgebra as na;
pub use *;
pub use *;
pub use *;
pub use *;
// pub const ZERO_THRESHOLD: Real = 1e-10;
/// Trait for types that have a zero threshold.
///
/// This trait defines the threshold below which values are considered zero
/// for sparse matrix operations. Values below this threshold are automatically
/// filtered out during construction to maintain sparsity.
/// Trait combining nalgebra's RealField with ZeroThreshold for use in sparse operations.
///
/// This trait is implemented for all types that satisfy both `RealField` and `ZeroThreshold`.
/// It's used as a constraint throughout the library for sparse matrix elements.