pub struct CsrMatrix<T> {
pub indptr: Vec<usize>,
pub indices: Vec<usize>,
pub data: Vec<T>,
/* private fields */
}Expand description
Compressed Sparse Row (CSR) matrix
A sparse matrix format that compresses rows, making it efficient for row operations and matrix-vector multiplication.
Fields§
§indptr: Vec<usize>Row pointers (size rows+1)
indices: Vec<usize>Column indices
data: Vec<T>Data values
Implementations§
Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn get_triplets(&self) -> (Vec<usize>, Vec<usize>, Vec<T>)
pub fn get_triplets(&self) -> (Vec<usize>, Vec<usize>, Vec<T>)
Get the triplets (row indices, column indices, data)
Sourcepub fn new(
data: Vec<T>,
rowindices: Vec<usize>,
colindices: Vec<usize>,
shape: (usize, usize),
) -> SparseResult<Self>
pub fn new( data: Vec<T>, rowindices: Vec<usize>, colindices: Vec<usize>, shape: (usize, usize), ) -> SparseResult<Self>
Create a new CSR matrix from raw data
§Arguments
data- Vector of non-zero valuesrowindices- Vector of row indices for each non-zero valuecolindices- Vector of column indices for each non-zero valueshape- Tuple containing the matrix dimensions (rows, cols)
§Returns
- A new CSR matrix
§Examples
use scirs2_sparse::csr::CsrMatrix;
// Create a 3x3 sparse matrix with 5 non-zero elements
let rows = vec![0, 0, 1, 2, 2];
let cols = vec![0, 2, 2, 0, 1];
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let shape = (3, 3);
let matrix = CsrMatrix::new(data.clone(), rows, cols, shape).unwrap();Sourcepub fn from_triplets(
nrows: usize,
ncols: usize,
row_indices: Vec<usize>,
col_indices: Vec<usize>,
values: Vec<T>,
) -> SparseResult<Self>
pub fn from_triplets( nrows: usize, ncols: usize, row_indices: Vec<usize>, col_indices: Vec<usize>, values: Vec<T>, ) -> SparseResult<Self>
Create a CSR matrix from triplet format (COO-like construction)
This is a convenience constructor that builds a CSR matrix from separate row indices, column indices, and values vectors.
§Arguments
nrows- Number of rows in the matrixncols- Number of columns in the matrixrow_indices- Vector of row indices for each non-zero valuecol_indices- Vector of column indices for each non-zero valuevalues- Vector of non-zero values
§Returns
Ok(CsrMatrix)- A new CSR matrixErr(SparseError)- If input is invalid
§Examples
use scirs2_sparse::csr::CsrMatrix;
let row_indices = vec![0, 0, 1, 2, 2];
let col_indices = vec![0, 2, 2, 0, 1];
let values = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let matrix = CsrMatrix::from_triplets(3, 3, row_indices, col_indices, values).unwrap();
assert_eq!(matrix.nnz(), 5);Sourcepub fn try_from_triplets(
nrows: usize,
ncols: usize,
triplets: &[(usize, usize, T)],
) -> SparseResult<Self>
pub fn try_from_triplets( nrows: usize, ncols: usize, triplets: &[(usize, usize, T)], ) -> SparseResult<Self>
Create a CSR matrix from triplet tuples
This constructor accepts a slice of (row, col, value) tuples, which is convenient for constructing matrices from coordinate lists.
§Arguments
nrows- Number of rows in the matrixncols- Number of columns in the matrixtriplets- Slice of (row_index, col_index, value) tuples
§Returns
Ok(CsrMatrix)- A new CSR matrixErr(SparseError)- If input is invalid
§Examples
use scirs2_sparse::csr::CsrMatrix;
let triplets = vec![
(0, 0, 1.0),
(0, 2, 2.0),
(1, 2, 3.0),
(2, 0, 4.0),
(2, 1, 5.0),
];
let matrix = CsrMatrix::try_from_triplets(3, 3, &triplets).unwrap();
assert_eq!(matrix.nnz(), 5);
assert_eq!(matrix.get(0, 0), 1.0);
assert_eq!(matrix.get(2, 1), 5.0);Sourcepub fn from_raw_csr(
data: Vec<T>,
indptr: Vec<usize>,
indices: Vec<usize>,
shape: (usize, usize),
) -> SparseResult<Self>
pub fn from_raw_csr( data: Vec<T>, indptr: Vec<usize>, indices: Vec<usize>, shape: (usize, usize), ) -> SparseResult<Self>
Source§impl<T: Clone + Copy + AddAssign + MulAssign + PartialEq + Debug + Zero + Add<Output = T> + Mul<Output = T> + SparseElement> CsrMatrix<T>
impl<T: Clone + Copy + AddAssign + MulAssign + PartialEq + Debug + Zero + Add<Output = T> + Mul<Output = T> + SparseElement> CsrMatrix<T>
Source§impl CsrMatrix<f64>
impl CsrMatrix<f64>
Sourcepub fn gpu_dot(&self, vec: &[f64]) -> SparseResult<Vec<f64>>
pub fn gpu_dot(&self, vec: &[f64]) -> SparseResult<Vec<f64>>
GPU-accelerated matrix-vector multiplication
This method automatically uses GPU acceleration when beneficial, falling back to optimized CPU implementation when appropriate.
§Arguments
vec- Vector to multiply with
§Returns
- Result of matrix-vector multiplication
§Examples
use scirs2_sparse::csr::CsrMatrix;
let rows = vec![0, 0, 1, 2, 2];
let cols = vec![0, 2, 2, 0, 1];
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let shape = (3, 3);
let matrix = CsrMatrix::new(data, rows, cols, shape).unwrap();
let vec = vec![1.0, 2.0, 3.0];
let result = matrix.gpu_dot(&vec).unwrap();Sourcepub fn gpu_dot_with_backend(
&self,
vec: &[f64],
backend: GpuBackend,
) -> SparseResult<Vec<f64>>
pub fn gpu_dot_with_backend( &self, vec: &[f64], backend: GpuBackend, ) -> SparseResult<Vec<f64>>
Source§impl<T> CsrMatrix<T>where
T: Float + Debug + Copy + Default + GpuDataType + Send + Sync + SparseElement + AddAssign + Mul<Output = T> + 'static,
impl<T> CsrMatrix<T>where
T: Float + Debug + Copy + Default + GpuDataType + Send + Sync + SparseElement + AddAssign + Mul<Output = T> + 'static,
Sourcepub fn gpu_dot_generic(&self, vec: &[T]) -> SparseResult<Vec<T>>
pub fn gpu_dot_generic(&self, vec: &[T]) -> SparseResult<Vec<T>>
Sourcepub fn should_use_gpu(&self) -> bool
pub fn should_use_gpu(&self) -> bool
Check if this matrix should benefit from GPU acceleration
§Returns
trueif GPU acceleration is likely to provide benefits
Sourcepub fn gpu_backend_info() -> SparseResult<(GpuBackend, String)>
pub fn gpu_backend_info() -> SparseResult<(GpuBackend, String)>
Trait Implementations§
Source§impl<F: Float + SparseElement + NumAssign + Sum + 'static + Debug> AsLinearOperator<F> for CsrMatrix<F>
impl<F: Float + SparseElement + NumAssign + Sum + 'static + Debug> AsLinearOperator<F> for CsrMatrix<F>
Source§fn as_linear_operator(&self) -> Box<dyn LinearOperator<F>>
fn as_linear_operator(&self) -> Box<dyn LinearOperator<F>>
Auto Trait Implementations§
impl<T> Freeze for CsrMatrix<T>
impl<T> RefUnwindSafe for CsrMatrix<T>where
T: RefUnwindSafe,
impl<T> Send for CsrMatrix<T>where
T: Send,
impl<T> Sync for CsrMatrix<T>where
T: Sync,
impl<T> Unpin for CsrMatrix<T>where
T: Unpin,
impl<T> UnwindSafe for CsrMatrix<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more