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_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>> CsrMatrix<T>
impl<T: Clone + Copy + AddAssign + MulAssign + PartialEq + Debug + Zero + Add<Output = T> + Mul<Output = T>> 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>
impl<T> CsrMatrix<T>
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
true
if 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 + NumAssign + Sum + 'static + Debug> AsLinearOperator<F> for CsrMatrix<F>
impl<F: Float + 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>>
Convert to a linear operator
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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