nalgebra_sparse/lib.rs
1//! Sparse matrices and algorithms for [nalgebra](https://www.nalgebra.rs).
2//!
3//! This crate extends `nalgebra` with sparse matrix formats and operations on sparse matrices.
4//!
5//! ## Goals
6//! The long-term goals for this crate are listed below.
7//!
8//! - Provide proven sparse matrix formats in an easy-to-use and idiomatic Rust API that
9//! naturally integrates with `nalgebra`.
10//! - Provide additional expert-level APIs for fine-grained control over operations.
11//! - Integrate well with external sparse matrix libraries.
12//! - Provide native Rust high-performance routines, including parallel matrix operations.
13//!
14//! ## Highlighted current features
15//!
16//! - [CSR](csr::CsrMatrix), [CSC](csc::CscMatrix) and [COO](coo::CooMatrix) formats, and
17//! [conversions](`convert`) between them.
18//! - Common arithmetic operations are implemented. See the [`ops`] module.
19//! - Sparsity patterns in CSR and CSC matrices are explicitly represented by the
20//! [SparsityPattern](pattern::SparsityPattern) type, which encodes the invariants of the
21//! associated index data structures.
22//! - [Matrix market format support] when the `io` feature is enabled.
23//! - [proptest strategies] for sparse matrices when the feature
24//! `proptest-support` is enabled.
25//! - [matrixcompare support](https://crates.io/crates/matrixcompare) for effortless
26//! (approximate) comparison of matrices in test code (requires the `compare` feature).
27//!
28//! [Matrix market format support]: io/
29//! [proptest strategies]: proptest/
30//!
31//! ## Current state
32//!
33//! The library is in an early, but usable state. The API has been designed to be extensible,
34//! but breaking changes will be necessary to implement several planned features. While it is
35//! backed by an extensive test suite, it has yet to be thoroughly battle-tested in real
36//! applications. Moreover, the focus so far has been on correctness and API design, with little
37//! focus on performance. Future improvements will include incremental performance enhancements.
38//!
39//! Current limitations:
40//!
41//! - Limited or no availability of sparse system solvers.
42//! - Limited support for complex numbers. Currently only arithmetic operations that do not
43//! rely on particular properties of complex numbers, such as e.g. conjugation, are
44//! supported.
45//! - No integration with external libraries.
46//!
47//! # Usage
48//!
49//! Add the following to your `Cargo.toml` file:
50//!
51//! ```toml
52//! [dependencies]
53//! nalgebra_sparse = "0.1"
54//! ```
55//!
56//! # Supported matrix formats
57//!
58//! | Format | Notes |
59//! | ------------------------|--------------------------------------------- |
60//! | [COO](`coo::CooMatrix`) | Well-suited for matrix construction. <br /> Ill-suited for algebraic operations. |
61//! | [CSR](`csr::CsrMatrix`) | Immutable sparsity pattern, suitable for algebraic operations. <br /> Fast row access. |
62//! | [CSC](`csc::CscMatrix`) | Immutable sparsity pattern, suitable for algebraic operations. <br /> Fast column access. |
63//!
64//! What format is best to use depends on the application. The most common use case for sparse
65//! matrices in science is the solution of sparse linear systems. Here we can differentiate between
66//! two common cases:
67//!
68//! - Direct solvers. Typically, direct solvers take their input in CSR or CSC format.
69//! - Iterative solvers. Many iterative solvers require only matrix-vector products,
70//! for which the CSR or CSC formats are suitable.
71//!
72//! The [COO](coo::CooMatrix) format is primarily intended for matrix construction.
73//! A common pattern is to use COO for construction, before converting to CSR or CSC for use
74//! in a direct solver or for computing matrix-vector products in an iterative solver.
75//! Some high-performance applications might also directly manipulate the CSR and/or CSC
76//! formats.
77//!
78//! # Example: COO -> CSR -> matrix-vector product
79//!
80//! ```
81//! use nalgebra_sparse::{coo::CooMatrix, csr::CsrMatrix};
82//! use nalgebra::{DMatrix, DVector};
83//! use matrixcompare::assert_matrix_eq;
84//!
85//! // The dense representation of the matrix
86//! let dense = DMatrix::from_row_slice(3, 3,
87//! &[1.0, 0.0, 3.0,
88//! 2.0, 0.0, 1.3,
89//! 0.0, 0.0, 4.1]);
90//!
91//! // Build the equivalent COO representation. We only add the non-zero values
92//! let mut coo = CooMatrix::new(3, 3);
93//! // We can add elements in any order. For clarity, we do so in row-major order here.
94//! coo.push(0, 0, 1.0);
95//! coo.push(0, 2, 3.0);
96//! coo.push(1, 0, 2.0);
97//! coo.push(1, 2, 1.3);
98//! coo.push(2, 2, 4.1);
99//!
100//! // ... or add entire dense matrices like so:
101//! // coo.push_matrix(0, 0, &dense);
102//!
103//! // The simplest way to construct a CSR matrix is to first construct a COO matrix, and
104//! // then convert it to CSR. The `From` trait is implemented for conversions between different
105//! // sparse matrix types.
106//! // Alternatively, we can construct a matrix directly from the CSR data.
107//! // See the docs for CsrMatrix for how to do that.
108//! let csr = CsrMatrix::from(&coo);
109//!
110//! // Let's check that the CSR matrix and the dense matrix represent the same matrix.
111//! // We can use macros from the `matrixcompare` crate to easily do this, despite the fact that
112//! // we're comparing across two different matrix formats. Note that these macros are only really
113//! // appropriate for writing tests, however.
114//! assert_matrix_eq!(csr, dense);
115//!
116//! let x = DVector::from_column_slice(&[1.3, -4.0, 3.5]);
117//!
118//! // Compute the matrix-vector product y = A * x. We don't need to specify the type here,
119//! // but let's just do it to make sure we get what we expect
120//! let y: DVector<_> = &csr * &x;
121//!
122//! // Verify the result with a small element-wise absolute tolerance
123//! let y_expected = DVector::from_column_slice(&[11.8, 7.15, 14.35]);
124//! assert_matrix_eq!(y, y_expected, comp = abs, tol = 1e-9);
125//!
126//! // The above expression is simple, and gives easy to read code, but if we're doing this in a
127//! // loop, we'll have to keep allocating new vectors. If we determine that this is a bottleneck,
128//! // then we can resort to the lower level APIs for more control over the operations
129//! {
130//! use nalgebra_sparse::ops::{Op, serial::spmm_csr_dense};
131//! let mut y = y;
132//! // Compute y <- 0.0 * y + 1.0 * csr * dense. We store the result directly in `y`, without
133//! // any intermediate allocations
134//! spmm_csr_dense(0.0, &mut y, 1.0, Op::NoOp(&csr), Op::NoOp(&x));
135//! assert_matrix_eq!(y, y_expected, comp = abs, tol = 1e-9);
136//! }
137//! ```
138#![deny(
139 nonstandard_style,
140 unused,
141 missing_docs,
142 rust_2018_idioms,
143 rust_2024_compatibility,
144 future_incompatible,
145 missing_copy_implementations
146)]
147
148pub extern crate nalgebra as na;
149#[macro_use]
150#[cfg(feature = "io")]
151extern crate pest_derive;
152
153pub mod convert;
154pub mod coo;
155pub mod csc;
156pub mod csr;
157pub mod factorization;
158#[cfg(feature = "io")]
159pub mod io;
160pub mod ops;
161pub mod pattern;
162
163pub(crate) mod cs;
164pub(crate) mod utils;
165
166#[cfg(feature = "proptest-support")]
167pub mod proptest;
168
169#[cfg(feature = "compare")]
170mod matrixcompare;
171
172use num_traits::Zero;
173use std::error::Error;
174use std::fmt;
175
176pub use self::coo::CooMatrix;
177pub use self::csc::CscMatrix;
178pub use self::csr::CsrMatrix;
179
180/// Errors produced by functions that expect well-formed sparse format data.
181#[derive(Debug)]
182pub struct SparseFormatError {
183 kind: SparseFormatErrorKind,
184 // Currently we only use an underlying error for generating the `Display` impl
185 error: Box<dyn Error>,
186}
187
188impl SparseFormatError {
189 /// The type of error.
190 #[must_use]
191 pub fn kind(&self) -> &SparseFormatErrorKind {
192 &self.kind
193 }
194
195 pub(crate) fn from_kind_and_error(kind: SparseFormatErrorKind, error: Box<dyn Error>) -> Self {
196 Self { kind, error }
197 }
198
199 /// Helper functionality for more conveniently creating errors.
200 pub(crate) fn from_kind_and_msg(kind: SparseFormatErrorKind, msg: &'static str) -> Self {
201 Self::from_kind_and_error(kind, Box::<dyn Error>::from(msg))
202 }
203}
204
205/// The type of format error described by a [`SparseFormatError`].
206#[non_exhaustive]
207#[derive(Debug, Copy, Clone, PartialEq, Eq)]
208pub enum SparseFormatErrorKind {
209 /// Indicates that the index data associated with the format contains at least one index
210 /// out of bounds.
211 IndexOutOfBounds,
212
213 /// Indicates that the provided data contains at least one duplicate entry, and the
214 /// current format does not support duplicate entries.
215 DuplicateEntry,
216
217 /// Indicates that the provided data for the format does not conform to the high-level
218 /// structure of the format.
219 ///
220 /// For example, the arrays defining the format data might have incompatible sizes.
221 InvalidStructure,
222}
223
224impl fmt::Display for SparseFormatError {
225 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
226 write!(f, "{}", self.error)
227 }
228}
229
230impl Error for SparseFormatError {}
231
232/// An entry in a sparse matrix.
233///
234/// Sparse matrices do not store all their entries explicitly. Therefore, entry (i, j) in the matrix
235/// can either be a reference to an explicitly stored element, or it is implicitly zero.
236#[derive(Debug, PartialEq, Eq)]
237pub enum SparseEntry<'a, T> {
238 /// The entry is a reference to an explicitly stored element.
239 ///
240 /// Note that the naming here is a misnomer: The element can still be zero, even though it
241 /// is explicitly stored (a so-called "explicit zero").
242 NonZero(&'a T),
243 /// The entry is implicitly zero, i.e. it is not explicitly stored.
244 Zero,
245}
246
247impl<'a, T: Clone + Zero> SparseEntry<'a, T> {
248 /// Returns the value represented by this entry.
249 ///
250 /// Either clones the underlying reference or returns zero if the entry is not explicitly
251 /// stored.
252 pub fn into_value(self) -> T {
253 match self {
254 SparseEntry::NonZero(value) => value.clone(),
255 SparseEntry::Zero => T::zero(),
256 }
257 }
258}
259
260/// A mutable entry in a sparse matrix.
261///
262/// See also `SparseEntry`.
263#[derive(Debug, PartialEq, Eq)]
264pub enum SparseEntryMut<'a, T> {
265 /// The entry is a mutable reference to an explicitly stored element.
266 ///
267 /// Note that the naming here is a misnomer: The element can still be zero, even though it
268 /// is explicitly stored (a so-called "explicit zero").
269 NonZero(&'a mut T),
270 /// The entry is implicitly zero i.e. it is not explicitly stored.
271 Zero,
272}
273
274impl<'a, T: Clone + Zero> SparseEntryMut<'a, T> {
275 /// Returns the value represented by this entry.
276 ///
277 /// Either clones the underlying reference or returns zero if the entry is not explicitly
278 /// stored.
279 pub fn into_value(self) -> T {
280 match self {
281 SparseEntryMut::NonZero(value) => value.clone(),
282 SparseEntryMut::Zero => T::zero(),
283 }
284 }
285}