pub struct CsrMatrix<T> { /* private fields */ }Expand description
A Compressed Sparse Row Matrix.
Implementations§
Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn add_matrix(&self, other: &Self) -> Result<Self, SparseMatrixError>
pub fn add_matrix(&self, other: &Self) -> Result<Self, SparseMatrixError>
Performs matrix addition: ( C = A + B ).
Given two matrices ( A ) (self) and ( B ) (other) of the same shape ( m \times n ), their sum ( C ) is a matrix of the same shape where each element ( C_{ij} ) is the sum of the corresponding elements in ( A ) and ( B ): ( C_{ij} = A_{ij} + B_{ij} ).
Returns a new CsrMatrix representing the sum of the two matrices,
or a SparseMatrixError::ShapeMismatch if their shapes are not compatible.
§Arguments
other- The matrix to add.
§Errors
Returns SparseMatrixError::ShapeMismatch if the matrices have different dimensions.
§Examples
use deep_causality_sparse::CsrMatrix;
let a = CsrMatrix::from_triplets(2, 2, &[(0, 0, 1.0), (1, 1, 2.0)]).unwrap();
// A = [[1.0, 0.0], [0.0, 2.0]]
let b = CsrMatrix::from_triplets(2, 2, &[(0, 1, 3.0), (1, 0, 4.0)]).unwrap();
// B = [[0.0, 3.0], [4.0, 0.0]]
let c = a.add_matrix(&b).unwrap();
// C = A + B = [[1.0, 3.0], [4.0, 2.0]]
assert_eq!(c.get_value_at(0, 0), 1.0);
assert_eq!(c.get_value_at(0, 1), 3.0);
assert_eq!(c.get_value_at(1, 0), 4.0);
assert_eq!(c.get_value_at(1, 1), 2.0);Sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Computes the transpose of the matrix: ( B = A^T ).
Given a matrix ( A ) of shape ( m \times n ), its transpose ( B ) is a matrix of shape ( n \times m ), where the rows of ( A ) become the columns of ( B ) and the columns of ( A ) become the rows of ( B ). Formally, ( B_{ij} = A_{ji} ).
Returns a new CsrMatrix representing the transpose.
§Examples
use deep_causality_sparse::CsrMatrix;
let a = CsrMatrix::from_triplets(2, 3, &[(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0)]).unwrap();
// A (2x3) = [[1.0, 0.0, 2.0], [0.0, 3.0, 0.0]]
let a_t = a.transpose();
// A^T (3x2) = [[1.0, 0.0], [0.0, 3.0], [2.0, 0.0]]
assert_eq!(a_t.shape(), (3, 2));
assert_eq!(a_t.get_value_at(0, 0), 1.0);
assert_eq!(a_t.get_value_at(1, 1), 3.0);
assert_eq!(a_t.get_value_at(2, 0), 2.0);Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn sub_matrix(&self, other: &Self) -> Result<Self, SparseMatrixError>
pub fn sub_matrix(&self, other: &Self) -> Result<Self, SparseMatrixError>
Performs matrix subtraction: ( C = A - B ).
Given two matrices ( A ) (self) and ( B ) (other) of the same shape ( m \times n ), their difference ( C ) is a matrix of the same shape where each element ( C_{ij} ) is the difference of the corresponding elements in ( A ) and ( B ): ( C_{ij} = A_{ij} - B_{ij} ).
Returns a new CsrMatrix representing the difference of the two matrices,
or a SparseMatrixError::ShapeMismatch if their shapes are not compatible.
§Arguments
other- The matrix to subtract.
§Errors
Returns SparseMatrixError::ShapeMismatch if the matrices have different dimensions.
§Examples
use deep_causality_sparse::CsrMatrix;
let a = CsrMatrix::from_triplets(2, 2, &[(0, 0, 5.0), (0, 1, 2.0), (1, 1, 3.0)]).unwrap();
// A = [[5.0, 2.0], [0.0, 3.0]]
let b = CsrMatrix::from_triplets(2, 2, &[(0, 0, 1.0), (1, 1, 1.0)]).unwrap();
// B = [[1.0, 0.0], [0.0, 1.0]]
let c = a.sub_matrix(&b).unwrap();
// C = A - B = [[4.0, 2.0], [0.0, 2.0]]
assert_eq!(c.get_value_at(0, 0), 4.0);
assert_eq!(c.get_value_at(0, 1), 2.0);
assert_eq!(c.get_value_at(1, 0), 0.0);
assert_eq!(c.get_value_at(1, 1), 2.0);Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn scalar_mult(&self, scalar: T) -> Self
pub fn scalar_mult(&self, scalar: T) -> Self
Performs scalar multiplication: ( B = s \cdot A ).
Given a matrix ( A ) and a scalar ( s ), their product ( B ) is a matrix of the same shape as ( A ), where each element ( B_{ij} ) is the product of the scalar ( s ) and the corresponding element ( A_{ij} ): ( B_{ij} = s \cdot A_{ij} ).
Returns a new CsrMatrix where each element is multiplied by the scalar.
§Arguments
scalar- The scalar value to multiply by.
§Examples
use deep_causality_sparse::CsrMatrix;
let a = CsrMatrix::from_triplets(2, 2, &[(0, 0, 1.0), (1, 1, 2.0)]).unwrap();
// A = [[1.0, 0.0], [0.0, 2.0]]
let c = a.scalar_mult(3.0);
// C = 3 * A = [[3.0, 0.0], [0.0, 6.0]]
assert_eq!(c.get_value_at(0, 0), 3.0);
assert_eq!(c.get_value_at(1, 1), 6.0);Sourcepub fn vec_mult(&self, vector: &[T]) -> Result<Vec<T>, SparseMatrixError>
pub fn vec_mult(&self, vector: &[T]) -> Result<Vec<T>, SparseMatrixError>
Performs matrix-vector multiplication: ( y = Ax ).
Given a matrix ( A ) of shape ( m \times n ) (self) and a vector ( x ) of length ( n ), their product ( y ) is a vector of length ( m ), where each element ( y_i ) is the dot product of the ( i )-th row of ( A ) and the vector ( x ): ( y_i = \sum_{j=0}^{n-1} A_{ij} x_j ).
§Arguments
x- The vector to multiply by. It is expected to have a length equal to the number of columns in the matrix.
§Returns
A Result<Vec<T>, SparseMatrixError> representing the resulting vector, or an error.
§Errors
Returns SparseMatrixError::DimensionMismatch if the length of x does not match
the number of columns in the matrix (self.shape.1).
§Examples
use deep_causality_sparse::CsrMatrix;
use deep_causality_num::Zero;
let a = CsrMatrix::from_triplets(2, 3, &[(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0)]).unwrap();
// A = [[1.0, 0.0, 2.0], [0.0, 3.0, 0.0]]
let x = vec![1.0, 2.0, 3.0];
let y = a.vec_mult(&x).unwrap();
// y = Ax = [(1.0*1.0 + 0.0*2.0 + 2.0*3.0), (0.0*1.0 + 3.0*2.0 + 0.0*3.0)] = [7.0, 6.0]
assert_eq!(y, vec![7.0, 6.0]);Sourcepub fn mat_mult(&self, other: &Self) -> Result<Self, SparseMatrixError>
pub fn mat_mult(&self, other: &Self) -> Result<Self, SparseMatrixError>
Performs matrix multiplication: ( C = A \cdot B ).
Given two matrices ( A ) (self) of shape ( m \times k ) and ( B ) (other) of shape ( k \times n ), their product ( C ) is a matrix of shape ( m \times n ). Each element ( C_{ij} ) is the dot product of the ( i )-th row of ( A ) and the ( j )-th column of ( B ): ( C_{ij} = \sum_{p=0}^{k-1} A_{ip} B_{pj} ).
Returns a new CsrMatrix representing the product of the two matrices,
or a SparseMatrixError::DimensionMismatch if their dimensions are not compatible.
§Arguments
other- The matrix to multiply by.
§Errors
Returns SparseMatrixError::DimensionMismatch if the matrices have incompatible dimensions
for multiplication (self.cols != other.rows).
§Examples
use deep_causality_sparse::CsrMatrix;
use deep_causality_num::Zero;
let a = CsrMatrix::from_triplets(2, 3, &[(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0)]).unwrap();
// A (2x3) = [[1.0, 0.0, 2.0], [0.0, 3.0, 0.0]]
let b = CsrMatrix::from_triplets(3, 2, &[(0, 0, 4.0), (1, 1, 5.0), (2, 0, 6.0)]).unwrap();
// B (3x2) = [[4.0, 0.0], [0.0, 5.0], [6.0, 0.0]]
let c = a.mat_mult(&b).unwrap();
// C = A * B (2x2) = [[(1*4+0*0+2*6), (1*0+0*5+2*0)], [(0*4+3*0+0*6), (0*0+3*5+0*0)]]
// = [[16.0, 0.0], [0.0, 15.0]]
assert_eq!(c.get_value_at(0, 0), 16.0);
assert_eq!(c.get_value_at(0, 1), 0.0);
assert_eq!(c.get_value_at(1, 0), 0.0);
assert_eq!(c.get_value_at(1, 1), 15.0);Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn row_indices(&self) -> &Vec<usize>
pub fn row_indices(&self) -> &Vec<usize>
Returns a reference to the internal row_indices vector.
This vector stores the starting index in col_indices and values
for each row of the matrix, plus one extra element at the end indicating
the total number of non-zero elements.
§Returns
A reference to a Vec<usize> representing the row pointers.
§Examples
use deep_causality_sparse::CsrMatrix;
let matrix: CsrMatrix<f64> = CsrMatrix::from_triplets(
2, 3, &[(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0)]
).unwrap();
assert_eq!(matrix.row_indices(), &vec![0, 2, 3]);Sourcepub fn col_indices(&self) -> &Vec<usize>
pub fn col_indices(&self) -> &Vec<usize>
Returns a reference to the internal col_indices vector.
This vector stores the column index for each non-zero element in the matrix, ordered by row, then by column within each row.
§Returns
A reference to a Vec<usize> representing the column indices of non-zero elements.
§Examples
use deep_causality_sparse::CsrMatrix;
let matrix: CsrMatrix<f64> = CsrMatrix::from_triplets(
2, 3, &[(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0)]
).unwrap();
assert_eq!(matrix.col_indices(), &vec![0, 2, 1]);Sourcepub fn values(&self) -> &Vec<T>
pub fn values(&self) -> &Vec<T>
Returns a reference to the internal values vector.
This vector stores the actual non-zero values of the matrix,
corresponding to the column indices in col_indices.
§Returns
A reference to a Vec<T> representing the non-zero values.
§Examples
use deep_causality_sparse::CsrMatrix;
let matrix: CsrMatrix<f64> = CsrMatrix::from_triplets(
2, 3, &[(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0)]
).unwrap();
assert_eq!(matrix.values(), &vec![1.0, 2.0, 3.0]);Sourcepub fn shape(&self) -> (usize, usize)
pub fn shape(&self) -> (usize, usize)
Returns the shape (dimensions) of the matrix.
The shape is returned as a tuple (rows, cols).
§Returns
A tuple (usize, usize) representing the number of rows and columns.
§Examples
use deep_causality_sparse::CsrMatrix;
let matrix: CsrMatrix<f64> = CsrMatrix::with_capacity(5, 10, 0);
assert_eq!(matrix.shape(), (5, 10));Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty CsrMatrix.
The matrix will have zero rows and columns and no stored elements.
Its shape will be (0, 0).
§Returns
A new, empty CsrMatrix.
§Examples
use deep_causality_sparse::CsrMatrix;
let matrix: CsrMatrix<f64> = CsrMatrix::new();
assert_eq!(matrix.shape(), (0, 0));
assert!(matrix.values().is_empty());Sourcepub fn with_capacity(rows: usize, cols: usize, capacity: usize) -> Self
pub fn with_capacity(rows: usize, cols: usize, capacity: usize) -> Self
Creates a new CsrMatrix with pre-allocated capacity for its internal vectors.
This can improve performance by reducing reallocations when a large number of
elements are expected to be added to the matrix after creation.
The matrix is initially logically empty (all zeros) with the specified shape.
§Arguments
rows- The number of rows the matrix will have.cols- The number of columns the matrix will have.capacity- The estimated number of non-zero elements the matrix will store.
§Returns
A new CsrMatrix with the specified shape and allocated capacity.
§Examples
use deep_causality_sparse::CsrMatrix;
let matrix: CsrMatrix<f64> = CsrMatrix::with_capacity(5, 5, 10);
assert_eq!(matrix.shape(), (5, 5));
assert!(matrix.values().capacity() >= 10);
assert_eq!(matrix.row_indices().len(), 6); // rows + 1
assert!(matrix.col_indices().is_empty());Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn from_triplets(
rows: usize,
cols: usize,
triplets: &[(usize, usize, T)],
) -> Result<Self, SparseMatrixError>
pub fn from_triplets( rows: usize, cols: usize, triplets: &[(usize, usize, T)], ) -> Result<Self, SparseMatrixError>
Creates a new CsrMatrix from a list of (row, col, value) triplets.
The from_triplets function constructs a sparse matrix ( A ) of size ( m \times n )
(where ( m ) is rows and ( n ) is cols) from a list of ( (r, c, v) ) triplets.
Each triplet represents a non-zero element ( A_{rc} = v ).
If multiple triplets specify the same ( (r, c) ) position, their values are summed:
( A_{rc} = \sum v_i ) for all ( v_i ) at ( (r, c) ).
Triplets whose summed value is zero are discarded.
The triplets are sorted, and duplicate (row, col) entries have their
values summed.
§Arguments
rows- The number of rows in the matrix.cols- The number of columns in the matrix.triplets- A slice of(row_idx, col_idx, value)tuples.
§Errors
Returns SparseMatrixError::IndexOutOfBounds if any triplet’s indices
are outside the specified matrix dimensions.
§Examples
use deep_causality_sparse::CsrMatrix;
use deep_causality_num::Zero;
// Example 1: Basic construction
let triplets1 = vec![(0, 0, 1.0), (0, 2, 2.0), (1, 1, 3.0)];
let matrix1 = CsrMatrix::from_triplets(2, 3, &triplets1).unwrap();
// matrix1 will represent:
// [1.0, 0.0, 2.0]
// [0.0, 3.0, 0.0]
assert_eq!(matrix1.values(), &vec![1.0, 2.0, 3.0]);
assert_eq!(matrix1.col_indices(), &vec![0, 2, 1]);
assert_eq!(matrix1.row_indices(), &vec![0, 2, 3]);
assert_eq!(matrix1.shape(), (2, 3));
// Example 2: With duplicate entries
let triplets2 = vec![(0, 0, 1.0), (0, 0, 0.5), (1, 1, 3.0)];
let matrix2 = CsrMatrix::from_triplets(2, 2, &triplets2).unwrap();
// matrix2 will represent:
// [1.5, 0.0]
// [0.0, 3.0]
assert_eq!(matrix2.values(), &vec![1.5, 3.0]);
assert_eq!(matrix2.col_indices(), &vec![0, 1]);
assert_eq!(matrix2.row_indices(), &vec![0, 1, 2]);
assert_eq!(matrix2.shape(), (2, 2));
// Example 3: With zero-valued entries after summation
let triplets3 = vec![(0, 0, 1.0), (0, 0, -1.0), (1, 1, 3.0)];
let matrix3 = CsrMatrix::from_triplets(2, 2, &triplets3).unwrap();
// matrix3 will represent:
// [0.0, 0.0]
// [0.0, 3.0]
assert_eq!(matrix3.values(), &vec![3.0]);
assert_eq!(matrix3.col_indices(), &vec![1]);
assert_eq!(matrix3.row_indices(), &vec![0, 0, 1]); // Note: row_indices[0]=0, row_indices[1]=0 as row 0 has no non-zeros
assert_eq!(matrix3.shape(), (2, 2));Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn from_triplets_with_zero(
rows: usize,
cols: usize,
triplets: &[(usize, usize, T)],
zero: T,
) -> Result<Self, SparseMatrixError>
pub fn from_triplets_with_zero( rows: usize, cols: usize, triplets: &[(usize, usize, T)], zero: T, ) -> Result<Self, SparseMatrixError>
Creates a new CsrMatrix from a list of (row, col, value) triplets, using an explicit zero value.
This method is similar to from_triplets, but instead of using T::zero(), it uses the provided
zero value to determine which elements should be considered “zero” (and thus excluded from the sparse structure).
This is useful for types that do not implement the Zero trait or when a different “zero” context is needed.
§Arguments
rows- The number of rows in the matrix.cols- The number of columns in the matrix.triplets- A slice of(row_idx, col_idx, value)tuples.zero- The value to treat as zero.
§Errors
Returns SparseMatrixError::IndexOutOfBounds if any triplet’s indices
are outside the specified matrix dimensions.
Sourcepub fn add_with_zero(
&self,
other: &Self,
zero: T,
) -> Result<Self, SparseMatrixError>where
T: Add<Output = T>,
pub fn add_with_zero(
&self,
other: &Self,
zero: T,
) -> Result<Self, SparseMatrixError>where
T: Add<Output = T>,
Performs matrix addition with an explicit zero value: ( C = A + B ).
This method allows adding matrices of types that do not implement Zero,
provided an explicit zero value is given for sparsity checks.
§Arguments
other- The matrix to add.zero- The value to treat as zero.
Trait Implementations§
Source§impl<T> AddAssign<&CsrMatrix<T>> for CsrMatrix<T>
impl<T> AddAssign<&CsrMatrix<T>> for CsrMatrix<T>
Source§fn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
+= operation. Read moreSource§impl<T> AddAssign for CsrMatrix<T>
impl<T> AddAssign for CsrMatrix<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<T> MulAssign<&CsrMatrix<T>> for CsrMatrix<T>
impl<T> MulAssign<&CsrMatrix<T>> for CsrMatrix<T>
Source§fn mul_assign(&mut self, rhs: &Self)
fn mul_assign(&mut self, rhs: &Self)
*= operation. Read moreSource§impl<T, S> MulAssign<S> for CsrMatrix<T>
impl<T, S> MulAssign<S> for CsrMatrix<T>
Source§fn mul_assign(&mut self, scalar: S)
fn mul_assign(&mut self, scalar: S)
*= operation. Read moreSource§impl<T> MulAssign for CsrMatrix<T>
impl<T> MulAssign for CsrMatrix<T>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<T> SubAssign<&CsrMatrix<T>> for CsrMatrix<T>
impl<T> SubAssign<&CsrMatrix<T>> for CsrMatrix<T>
Source§fn sub_assign(&mut self, rhs: &Self)
fn sub_assign(&mut self, rhs: &Self)
-= operation. Read moreSource§impl<T> SubAssign for CsrMatrix<T>
impl<T> SubAssign for CsrMatrix<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more