MCPcopy Create free account
hub / github.com/SingleRust/SingleRust / convert_csr_sparse_matrix

Function convert_csr_sparse_matrix

src/memory/utils/mod.rs:468–484  ·  view source on GitHub ↗

Convert CSR sparse matrix between numeric types. Transforms a Compressed Sparse Row matrix from one numeric type to another while preserving the sparse structure. Particularly useful for converting integer count data to floating-point for analysis. ## Parameters `matrix` - Source CSR matrix to convert ## Type Parameters `T` - Source numeric type (e.g., i32, u32) `U` - Target floating-point type

(matrix: CsrMatrix<T>)

Source from the content-addressed store, hash-verified

466/// - **Matrix dimensions**: Rows and columns preserved
467/// - **Memory efficiency**: No unnecessary densification
468fn convert_csr_sparse_matrix<T, U>(matrix: CsrMatrix<T>) -> anyhow::Result<CsrMatrix<U>>
469where
470 T: NumericOps + NumCast + Copy, // Base numeric traits for source
471 U: NumericOps + NumCast + Copy + Float, // Ensure target is float (f32/f64)
472{
473 let nrows = matrix.nrows();
474 let ncols = matrix.ncols();
475 let (row_offsets, col_indices, values) = matrix.disassemble();
476
477 let new_values: Vec<U> = values
478 .into_iter()
479 .map(|x| NumCast::from(x).unwrap())
480 .collect();
481
482 CsrMatrix::try_from_csr_data(nrows, ncols, row_offsets, col_indices, new_values)
483 .map_err(|e| anyhow::anyhow!("Failed to create CSR matrix: {}", e))
484}
485
486/// Convert CSC sparse matrix between numeric types.
487///

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected