Struct CsrMatrix

Source
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>
where T: Clone + Copy + Zero + PartialEq,

Source

pub fn get(&self, row: usize, col: usize) -> T

Get the value at the specified position

Source

pub fn get_triplets(&self) -> (Vec<usize>, Vec<usize>, Vec<T>)

Get the triplets (row indices, column indices, data)

Source

pub fn new( data: Vec<T>, row_indices: Vec<usize>, col_indices: Vec<usize>, shape: (usize, usize), ) -> SparseResult<Self>

Create a new CSR matrix from raw data

§Arguments
  • data - Vector of non-zero values
  • row_indices - Vector of row indices for each non-zero value
  • col_indices - Vector of column indices for each non-zero value
  • shape - 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();
Source

pub fn from_raw_csr( data: Vec<T>, indptr: Vec<usize>, indices: Vec<usize>, shape: (usize, usize), ) -> SparseResult<Self>

Create a new CSR matrix from raw CSR format

§Arguments
  • data - Vector of non-zero values
  • indptr - Vector of row pointers (size rows+1)
  • indices - Vector of column indices
  • shape - Tuple containing the matrix dimensions (rows, cols)
§Returns
  • A new CSR matrix
Source

pub fn empty(shape: (usize, usize)) -> Self

Create a new empty CSR matrix

§Arguments
  • shape - Tuple containing the matrix dimensions (rows, cols)
§Returns
  • A new empty CSR matrix
Source

pub fn rows(&self) -> usize

Get the number of rows in the matrix

Source

pub fn cols(&self) -> usize

Get the number of columns in the matrix

Source

pub fn shape(&self) -> (usize, usize)

Get the shape (dimensions) of the matrix

Source

pub fn nnz(&self) -> usize

Get the number of non-zero elements in the matrix

Source

pub fn to_dense(&self) -> Vec<Vec<T>>
where T: Zero + Copy,

Convert to dense matrix (as Vec<Vec>)

Source

pub fn transpose(&self) -> Self

Transpose the matrix

Source§

impl<T: Clone + Copy + AddAssign + MulAssign + PartialEq + Debug + Zero + Add<Output = T> + Mul<Output = T>> CsrMatrix<T>

Source

pub fn is_symmetric(&self) -> bool

Check if matrix is symmetric

§Returns
  • true if the matrix is symmetric, false otherwise
Source

pub fn matmul(&self, other: &CsrMatrix<T>) -> SparseResult<CsrMatrix<T>>

Matrix-matrix multiplication

§Arguments
  • other - Matrix to multiply with
§Returns
  • Result containing the product matrix
Source

pub fn row_range(&self, row: usize) -> Range<usize>

Get row range for iterating over elements in a row

§Arguments
  • row - Row index
§Returns
  • Range of indices in the data and indices arrays for this row
Source

pub fn col_indices(&self) -> &[usize]

Get column indices array

Source§

impl CsrMatrix<f64>

Source

pub fn dot(&self, vec: &[f64]) -> SparseResult<Vec<f64>>

Matrix-vector multiplication

§Arguments
  • vec - Vector to multiply with
§Returns
  • Result of matrix-vector multiplication

Trait Implementations§

Source§

impl<F: Float + NumAssign + Sum + 'static + Debug> AsLinearOperator<F> for CsrMatrix<F>

Source§

fn as_linear_operator(&self) -> Box<dyn LinearOperator<F>>

Convert to a linear operator
Source§

impl<T: Clone> Clone for CsrMatrix<T>

Source§

fn clone(&self) -> CsrMatrix<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V