2021-01-23 00:57:03 +08:00
|
|
|
//! Sparse matrices and algorithms for [nalgebra](https://www.nalgebra.org).
|
2020-09-22 16:51:29 +08:00
|
|
|
//!
|
2021-01-23 00:57:03 +08:00
|
|
|
//! This crate extends `nalgebra` with sparse matrix formats and operations on sparse matrices.
|
|
|
|
//!
|
|
|
|
//! ## Goals
|
|
|
|
//! The long-term goals for this crate are listed below.
|
|
|
|
//!
|
|
|
|
//! - Provide proven sparse matrix formats in an easy-to-use and idiomatic Rust API that
|
|
|
|
//! naturally integrates with `nalgebra`.
|
|
|
|
//! - Provide additional expert-level APIs for fine-grained control over operations.
|
|
|
|
//! - Integrate well with external sparse matrix libraries.
|
|
|
|
//! - Provide native Rust high-performance routines, including parallel matrix operations.
|
|
|
|
//!
|
|
|
|
//! ## Highlighted current features
|
|
|
|
//!
|
|
|
|
//! - [CSR](csr::CsrMatrix), [CSC](csc::CscMatrix) and [COO](coo::CooMatrix) formats, and
|
|
|
|
//! [conversions](`convert`) between them.
|
|
|
|
//! - Common arithmetic operations are implemented. See the [`ops`] module.
|
|
|
|
//! - Sparsity patterns in CSR and CSC matrices are explicitly represented by the
|
|
|
|
//! [SparsityPattern](pattern::SparsityPattern) type, which encodes the invariants of the
|
|
|
|
//! associated index data structures.
|
2021-12-01 18:07:47 +08:00
|
|
|
//! - [Matrix market format support](`io`) when the `io` feature is enabled.
|
2021-01-23 00:57:03 +08:00
|
|
|
//! - [proptest strategies](`proptest`) for sparse matrices when the feature
|
|
|
|
//! `proptest-support` is enabled.
|
|
|
|
//! - [matrixcompare support](https://crates.io/crates/matrixcompare) for effortless
|
|
|
|
//! (approximate) comparison of matrices in test code (requires the `compare` feature).
|
|
|
|
//!
|
|
|
|
//! ## Current state
|
|
|
|
//!
|
|
|
|
//! The library is in an early, but usable state. The API has been designed to be extensible,
|
|
|
|
//! but breaking changes will be necessary to implement several planned features. While it is
|
|
|
|
//! backed by an extensive test suite, it has yet to be thoroughly battle-tested in real
|
|
|
|
//! applications. Moreover, the focus so far has been on correctness and API design, with little
|
|
|
|
//! focus on performance. Future improvements will include incremental performance enhancements.
|
|
|
|
//!
|
|
|
|
//! Current limitations:
|
|
|
|
//!
|
|
|
|
//! - Limited or no availability of sparse system solvers.
|
|
|
|
//! - Limited support for complex numbers. Currently only arithmetic operations that do not
|
|
|
|
//! rely on particular properties of complex numbers, such as e.g. conjugation, are
|
|
|
|
//! supported.
|
|
|
|
//! - No integration with external libraries.
|
|
|
|
//!
|
|
|
|
//! # Usage
|
|
|
|
//!
|
|
|
|
//! Add the following to your `Cargo.toml` file:
|
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [dependencies]
|
|
|
|
//! nalgebra_sparse = "0.1"
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! # Supported matrix formats
|
|
|
|
//!
|
|
|
|
//! | Format | Notes |
|
|
|
|
//! | ------------------------|--------------------------------------------- |
|
|
|
|
//! | [COO](`coo::CooMatrix`) | Well-suited for matrix construction. <br /> Ill-suited for algebraic operations. |
|
|
|
|
//! | [CSR](`csr::CsrMatrix`) | Immutable sparsity pattern, suitable for algebraic operations. <br /> Fast row access. |
|
2021-02-01 15:41:37 +08:00
|
|
|
//! | [CSC](`csc::CscMatrix`) | Immutable sparsity pattern, suitable for algebraic operations. <br /> Fast column access. |
|
2021-01-23 00:57:03 +08:00
|
|
|
//!
|
|
|
|
//! What format is best to use depends on the application. The most common use case for sparse
|
|
|
|
//! matrices in science is the solution of sparse linear systems. Here we can differentiate between
|
|
|
|
//! two common cases:
|
|
|
|
//!
|
|
|
|
//! - Direct solvers. Typically, direct solvers take their input in CSR or CSC format.
|
|
|
|
//! - Iterative solvers. Many iterative solvers require only matrix-vector products,
|
|
|
|
//! for which the CSR or CSC formats are suitable.
|
|
|
|
//!
|
|
|
|
//! The [COO](coo::CooMatrix) format is primarily intended for matrix construction.
|
|
|
|
//! A common pattern is to use COO for construction, before converting to CSR or CSC for use
|
|
|
|
//! in a direct solver or for computing matrix-vector products in an iterative solver.
|
|
|
|
//! Some high-performance applications might also directly manipulate the CSR and/or CSC
|
|
|
|
//! formats.
|
|
|
|
//!
|
|
|
|
//! # Example: COO -> CSR -> matrix-vector product
|
|
|
|
//!
|
2021-07-07 10:05:25 +08:00
|
|
|
//! ```
|
2021-01-23 00:57:03 +08:00
|
|
|
//! use nalgebra_sparse::{coo::CooMatrix, csr::CsrMatrix};
|
|
|
|
//! use nalgebra::{DMatrix, DVector};
|
|
|
|
//! use matrixcompare::assert_matrix_eq;
|
|
|
|
//!
|
|
|
|
//! // The dense representation of the matrix
|
|
|
|
//! let dense = DMatrix::from_row_slice(3, 3,
|
|
|
|
//! &[1.0, 0.0, 3.0,
|
|
|
|
//! 2.0, 0.0, 1.3,
|
|
|
|
//! 0.0, 0.0, 4.1]);
|
|
|
|
//!
|
|
|
|
//! // Build the equivalent COO representation. We only add the non-zero values
|
|
|
|
//! let mut coo = CooMatrix::new(3, 3);
|
|
|
|
//! // We can add elements in any order. For clarity, we do so in row-major order here.
|
|
|
|
//! coo.push(0, 0, 1.0);
|
|
|
|
//! coo.push(0, 2, 3.0);
|
|
|
|
//! coo.push(1, 0, 2.0);
|
|
|
|
//! coo.push(1, 2, 1.3);
|
|
|
|
//! coo.push(2, 2, 4.1);
|
|
|
|
//!
|
2021-06-07 22:13:43 +08:00
|
|
|
//! // ... or add entire dense matrices like so:
|
|
|
|
//! // coo.push_matrix(0, 0, &dense);
|
|
|
|
//!
|
2021-01-23 00:57:03 +08:00
|
|
|
//! // The simplest way to construct a CSR matrix is to first construct a COO matrix, and
|
|
|
|
//! // then convert it to CSR. The `From` trait is implemented for conversions between different
|
|
|
|
//! // sparse matrix types.
|
|
|
|
//! // Alternatively, we can construct a matrix directly from the CSR data.
|
|
|
|
//! // See the docs for CsrMatrix for how to do that.
|
|
|
|
//! let csr = CsrMatrix::from(&coo);
|
|
|
|
//!
|
|
|
|
//! // Let's check that the CSR matrix and the dense matrix represent the same matrix.
|
|
|
|
//! // We can use macros from the `matrixcompare` crate to easily do this, despite the fact that
|
|
|
|
//! // we're comparing across two different matrix formats. Note that these macros are only really
|
|
|
|
//! // appropriate for writing tests, however.
|
|
|
|
//! assert_matrix_eq!(csr, dense);
|
|
|
|
//!
|
|
|
|
//! let x = DVector::from_column_slice(&[1.3, -4.0, 3.5]);
|
|
|
|
//!
|
|
|
|
//! // Compute the matrix-vector product y = A * x. We don't need to specify the type here,
|
|
|
|
//! // but let's just do it to make sure we get what we expect
|
|
|
|
//! let y: DVector<_> = &csr * &x;
|
|
|
|
//!
|
|
|
|
//! // Verify the result with a small element-wise absolute tolerance
|
|
|
|
//! let y_expected = DVector::from_column_slice(&[11.8, 7.15, 14.35]);
|
|
|
|
//! assert_matrix_eq!(y, y_expected, comp = abs, tol = 1e-9);
|
|
|
|
//!
|
|
|
|
//! // The above expression is simple, and gives easy to read code, but if we're doing this in a
|
|
|
|
//! // loop, we'll have to keep allocating new vectors. If we determine that this is a bottleneck,
|
|
|
|
//! // then we can resort to the lower level APIs for more control over the operations
|
|
|
|
//! {
|
|
|
|
//! use nalgebra_sparse::ops::{Op, serial::spmm_csr_dense};
|
|
|
|
//! let mut y = y;
|
|
|
|
//! // Compute y <- 0.0 * y + 1.0 * csr * dense. We store the result directly in `y`, without
|
2021-01-25 19:09:16 +08:00
|
|
|
//! // any intermediate allocations
|
2021-01-23 00:57:03 +08:00
|
|
|
//! spmm_csr_dense(0.0, &mut y, 1.0, Op::NoOp(&csr), Op::NoOp(&x));
|
|
|
|
//! assert_matrix_eq!(y, y_expected, comp = abs, tol = 1e-9);
|
|
|
|
//! }
|
|
|
|
//! ```
|
2021-07-28 07:18:29 +08:00
|
|
|
#![deny(
|
|
|
|
nonstandard_style,
|
|
|
|
unused,
|
|
|
|
missing_docs,
|
|
|
|
rust_2018_idioms,
|
|
|
|
rust_2018_compatibility,
|
|
|
|
future_incompatible,
|
|
|
|
missing_copy_implementations
|
|
|
|
)]
|
2020-07-21 23:39:06 +08:00
|
|
|
|
2021-02-25 18:14:25 +08:00
|
|
|
pub extern crate nalgebra as na;
|
2021-11-02 00:25:16 +08:00
|
|
|
#[cfg(feature = "io")]
|
|
|
|
extern crate pest;
|
|
|
|
#[macro_use]
|
|
|
|
#[cfg(feature = "io")]
|
|
|
|
extern crate pest_derive;
|
|
|
|
|
2021-01-26 00:26:27 +08:00
|
|
|
pub mod convert;
|
2020-09-23 15:34:19 +08:00
|
|
|
pub mod coo;
|
2020-09-28 17:36:00 +08:00
|
|
|
pub mod csc;
|
2020-09-23 15:34:19 +08:00
|
|
|
pub mod csr;
|
2021-01-11 22:14:54 +08:00
|
|
|
pub mod factorization;
|
2021-11-02 00:25:16 +08:00
|
|
|
#[cfg(feature = "io")]
|
|
|
|
pub mod io;
|
2021-01-26 00:26:27 +08:00
|
|
|
pub mod ops;
|
|
|
|
pub mod pattern;
|
2020-07-14 00:44:40 +08:00
|
|
|
|
2020-12-30 23:09:46 +08:00
|
|
|
pub(crate) mod cs;
|
2020-12-22 17:19:17 +08:00
|
|
|
|
2020-11-12 18:49:19 +08:00
|
|
|
#[cfg(feature = "proptest-support")]
|
|
|
|
pub mod proptest;
|
|
|
|
|
2021-01-13 20:10:21 +08:00
|
|
|
#[cfg(feature = "compare")]
|
|
|
|
mod matrixcompare;
|
|
|
|
|
2021-01-26 00:26:27 +08:00
|
|
|
use num_traits::Zero;
|
2020-07-14 00:44:40 +08:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
|
|
|
|
2021-02-25 18:03:27 +08:00
|
|
|
pub use self::coo::CooMatrix;
|
|
|
|
pub use self::csc::CscMatrix;
|
|
|
|
pub use self::csr::CsrMatrix;
|
|
|
|
|
2020-07-21 23:39:06 +08:00
|
|
|
/// Errors produced by functions that expect well-formed sparse format data.
|
2020-07-14 00:44:40 +08:00
|
|
|
#[derive(Debug)]
|
2020-09-22 23:50:47 +08:00
|
|
|
pub struct SparseFormatError {
|
|
|
|
kind: SparseFormatErrorKind,
|
|
|
|
// Currently we only use an underlying error for generating the `Display` impl
|
2021-01-26 00:26:27 +08:00
|
|
|
error: Box<dyn Error>,
|
2020-09-22 23:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SparseFormatError {
|
|
|
|
/// The type of error.
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2020-09-22 23:50:47 +08:00
|
|
|
pub fn kind(&self) -> &SparseFormatErrorKind {
|
|
|
|
&self.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_kind_and_error(kind: SparseFormatErrorKind, error: Box<dyn Error>) -> Self {
|
2021-01-26 00:26:27 +08:00
|
|
|
Self { kind, error }
|
2020-09-22 23:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper functionality for more conveniently creating errors.
|
|
|
|
pub(crate) fn from_kind_and_msg(kind: SparseFormatErrorKind, msg: &'static str) -> Self {
|
|
|
|
Self::from_kind_and_error(kind, Box::<dyn Error>::from(msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The type of format error described by a [SparseFormatError](struct.SparseFormatError.html).
|
2020-09-25 21:11:43 +08:00
|
|
|
#[non_exhaustive]
|
2021-07-28 07:18:29 +08:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2020-09-22 23:50:47 +08:00
|
|
|
pub enum SparseFormatErrorKind {
|
2020-07-14 00:44:40 +08:00
|
|
|
/// Indicates that the index data associated with the format contains at least one index
|
|
|
|
/// out of bounds.
|
2020-09-22 23:50:47 +08:00
|
|
|
IndexOutOfBounds,
|
2020-07-14 00:44:40 +08:00
|
|
|
|
|
|
|
/// Indicates that the provided data contains at least one duplicate entry, and the
|
|
|
|
/// current format does not support duplicate entries.
|
2020-09-22 23:50:47 +08:00
|
|
|
DuplicateEntry,
|
2020-07-14 00:44:40 +08:00
|
|
|
|
|
|
|
/// Indicates that the provided data for the format does not conform to the high-level
|
|
|
|
/// structure of the format.
|
|
|
|
///
|
|
|
|
/// For example, the arrays defining the format data might have incompatible sizes.
|
2020-09-22 23:50:47 +08:00
|
|
|
InvalidStructure,
|
2020-07-14 00:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SparseFormatError {
|
2021-07-28 07:18:29 +08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-09-22 23:50:47 +08:00
|
|
|
write!(f, "{}", self.error)
|
2020-07-14 00:44:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:25:16 +08:00
|
|
|
impl Error for SparseFormatError {}
|
|
|
|
|
2021-01-22 21:32:13 +08:00
|
|
|
/// An entry in a sparse matrix.
|
|
|
|
///
|
|
|
|
/// Sparse matrices do not store all their entries explicitly. Therefore, entry (i, j) in the matrix
|
|
|
|
/// can either be a reference to an explicitly stored element, or it is implicitly zero.
|
2020-12-09 22:25:16 +08:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum SparseEntry<'a, T> {
|
2021-01-22 21:32:13 +08:00
|
|
|
/// The entry is a reference to an explicitly stored element.
|
|
|
|
///
|
|
|
|
/// Note that the naming here is a misnomer: The element can still be zero, even though it
|
|
|
|
/// is explicitly stored (a so-called "explicit zero").
|
2020-12-09 22:25:16 +08:00
|
|
|
NonZero(&'a T),
|
2021-01-22 21:32:13 +08:00
|
|
|
/// The entry is implicitly zero, i.e. it is not explicitly stored.
|
2021-01-26 00:26:27 +08:00
|
|
|
Zero,
|
2020-12-09 22:25:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Clone + Zero> SparseEntry<'a, T> {
|
2021-01-22 21:32:13 +08:00
|
|
|
/// Returns the value represented by this entry.
|
|
|
|
///
|
|
|
|
/// Either clones the underlying reference or returns zero if the entry is not explicitly
|
|
|
|
/// stored.
|
2021-01-26 16:28:15 +08:00
|
|
|
pub fn into_value(self) -> T {
|
2020-12-09 22:25:16 +08:00
|
|
|
match self {
|
|
|
|
SparseEntry::NonZero(value) => value.clone(),
|
2021-01-26 00:26:27 +08:00
|
|
|
SparseEntry::Zero => T::zero(),
|
2020-12-09 22:25:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 21:32:13 +08:00
|
|
|
/// A mutable entry in a sparse matrix.
|
|
|
|
///
|
|
|
|
/// See also `SparseEntry`.
|
2020-12-09 22:25:16 +08:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum SparseEntryMut<'a, T> {
|
2021-01-22 21:32:13 +08:00
|
|
|
/// The entry is a mutable reference to an explicitly stored element.
|
|
|
|
///
|
|
|
|
/// Note that the naming here is a misnomer: The element can still be zero, even though it
|
|
|
|
/// is explicitly stored (a so-called "explicit zero").
|
2020-12-09 22:25:16 +08:00
|
|
|
NonZero(&'a mut T),
|
2021-01-22 21:32:13 +08:00
|
|
|
/// The entry is implicitly zero i.e. it is not explicitly stored.
|
2021-01-26 00:26:27 +08:00
|
|
|
Zero,
|
2020-12-09 22:25:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Clone + Zero> SparseEntryMut<'a, T> {
|
2021-01-22 21:32:13 +08:00
|
|
|
/// Returns the value represented by this entry.
|
|
|
|
///
|
|
|
|
/// Either clones the underlying reference or returns zero if the entry is not explicitly
|
|
|
|
/// stored.
|
2021-01-26 16:28:15 +08:00
|
|
|
pub fn into_value(self) -> T {
|
2020-12-09 22:25:16 +08:00
|
|
|
match self {
|
|
|
|
SparseEntryMut::NonZero(value) => value.clone(),
|
2021-01-26 00:26:27 +08:00
|
|
|
SparseEntryMut::Zero => T::zero(),
|
2020-12-09 22:25:16 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 00:26:27 +08:00
|
|
|
}
|