scirs2-linalg 0.4.4

Linear algebra module for SciRS2 (scirs2-linalg)
Documentation
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use scirs2_core::ndarray::{s, Array1, Array2, ArrayView1, ArrayView2, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use scirs2_core::random::prelude::*;
use std::iter::Sum;

/// Convert a dense matrix to sparse format for eigenvalue computations.
///
/// This is a utility function that can detect sparsity in dense matrices and
/// convert them to an appropriate sparse format for more efficient eigenvalue
/// computations when the matrix is sufficiently sparse.
///
/// # Arguments
///
/// * `densematrix` - Dense matrix to convert
/// * `threshold` - Sparsity threshold (elements with absolute value below this are considered zero)
///
/// # Returns
///
/// * A sparse matrix representation suitable for sparse eigenvalue algorithms
///
/// # Examples
///
/// ```rust,ignore
/// use scirs2_core::ndarray::Array2;
/// use scirs2_linalg::eigen::sparse::dense_to_sparse;
///
/// // This is a placeholder example - actual implementation pending
/// // let dense = Array2::eye(1000);
/// // let sparse = dense_to_sparse(&dense.view(), 1e-12).expect("Operation failed");
/// ```
///
/// # Note
///
/// This function is currently a placeholder and will be implemented in a future version.
/// CSR (Compressed Sparse Row) matrix for eigenvalue computations.
///
/// Stores non-zero entries in three arrays:
/// - `data`: the non-zero values, stored row by row
/// - `indices`: the column index for each non-zero
/// - `indptr`: row pointers -- `indptr[i]..indptr[i+1]` gives the range
///   into `data`/`indices` for row `i`
pub struct CsrMatrix<F> {
    pub(super) nrows: usize,
    pub(super) ncols: usize,
    pub(super) data: Vec<F>,
    pub(super) indices: Vec<usize>,
    pub(super) indptr: Vec<usize>,
}
impl<F> CsrMatrix<F>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    /// Create a new CSR matrix.
    ///
    /// # Arguments
    /// * `nrows` - Number of rows
    /// * `ncols` - Number of columns
    /// * `data` - Non-zero values
    /// * `indices` - Column indices for each non-zero
    /// * `indptr` - Row pointers (length should be nrows + 1 for well-formed CSR)
    pub fn new(
        nrows: usize,
        ncols: usize,
        data: Vec<F>,
        indices: Vec<usize>,
        indptr: Vec<usize>,
    ) -> Self {
        Self {
            nrows,
            ncols,
            data,
            indices,
            indptr,
        }
    }
    /// Number of stored non-zero entries.
    pub fn nnz(&self) -> usize {
        self.data.len()
    }
    /// Create a CSR matrix from a dense array, dropping entries below `threshold`.
    pub fn from_dense(dense: &ArrayView2<F>, threshold: F) -> Self {
        let (m, n) = dense.dim();
        let mut data = Vec::new();
        let mut indices = Vec::new();
        let mut indptr = Vec::with_capacity(m + 1);
        indptr.push(0);
        for i in 0..m {
            for j in 0..n {
                let val = dense[[i, j]];
                if val.abs() >= threshold {
                    data.push(val);
                    indices.push(j);
                }
            }
            indptr.push(data.len());
        }
        Self {
            nrows: m,
            ncols: n,
            data,
            indices,
            indptr,
        }
    }
}