algebra-sparse 0.4.0-beta.1

Efficient sparse linear algebra library built on nalgebra with CSR/CSC formats and block diagonal matrix support
Documentation
// 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;
mod csm;
mod csv;
mod diagonal;
pub mod ops;
pub mod set;
pub mod traits;

pub use csm::*;
pub use csv::*;
pub use diagonal::*;
pub use set::*;

// 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.
pub trait ZeroThreshold {
    /// Returns the zero threshold for this type.
    fn zero_threshold() -> Self;
}

impl ZeroThreshold for f32 {
    #[inline]
    fn zero_threshold() -> Self {
        1e-10
    }
}

impl ZeroThreshold for f64 {
    #[inline]
    fn zero_threshold() -> Self {
        1e-10
    }
}

/// 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.
pub trait Real: na::RealField + ZeroThreshold + Copy {}
impl<T: na::RealField + ZeroThreshold + Copy> Real for T {}