pub struct SparseMatrix { /* private fields */ }Expand description
Main sparse matrix implementation supporting multiple storage formats.
Implementations§
Source§impl SparseMatrix
impl SparseMatrix
Sourcepub fn from_triplets(
triplets: Vec<(usize, usize, Precision)>,
rows: DimensionType,
cols: DimensionType,
) -> Result<Self>
pub fn from_triplets( triplets: Vec<(usize, usize, Precision)>, rows: DimensionType, cols: DimensionType, ) -> Result<Self>
Create a new sparse matrix from coordinate (triplet) format.
§Arguments
triplets- Vector of (row, col, value) tripletsrows- Number of rowscols- Number of columns
§Example
use sublinear_solver::SparseMatrix;
let matrix = SparseMatrix::from_triplets(
vec![(0, 0, 4.0), (0, 1, 1.0), (1, 0, 2.0), (1, 1, 5.0)],
2, 2
).unwrap();Sourcepub fn from_dense(
data: &[Precision],
rows: DimensionType,
cols: DimensionType,
) -> Result<Self>
pub fn from_dense( data: &[Precision], rows: DimensionType, cols: DimensionType, ) -> Result<Self>
Create a sparse matrix from dense row-major data.
Zero elements are automatically filtered out.
Sourcepub fn identity(size: DimensionType) -> Result<Self>
pub fn identity(size: DimensionType) -> Result<Self>
Create an identity matrix of the given size.
Sourcepub fn diagonal(diag: &[Precision]) -> Result<Self>
pub fn diagonal(diag: &[Precision]) -> Result<Self>
Create a diagonal matrix from the given diagonal values.
Sourcepub fn convert_to_format(&mut self, new_format: SparseFormat) -> Result<()>
pub fn convert_to_format(&mut self, new_format: SparseFormat) -> Result<()>
Convert the matrix to a different storage format.
This operation may be expensive for large matrices.
Sourcepub fn to_triplets(&self) -> Result<Vec<(usize, usize, Precision)>>
pub fn to_triplets(&self) -> Result<Vec<(usize, usize, Precision)>>
Extract the matrix as coordinate triplets.
Sourcepub fn format(&self) -> SparseFormat
pub fn format(&self) -> SparseFormat
Get the current storage format.
Sourcepub fn as_csr(&mut self) -> Result<&CSRStorage>
pub fn as_csr(&mut self) -> Result<&CSRStorage>
Get a reference to the underlying CSR storage.
Converts to CSR format if necessary.
Sourcepub fn as_csc(&mut self) -> Result<&CSCStorage>
pub fn as_csc(&mut self) -> Result<&CSCStorage>
Get a reference to the underlying CSC storage.
Converts to CSC format if necessary.
Sourcepub fn as_graph(&mut self) -> Result<&GraphStorage>
pub fn as_graph(&mut self) -> Result<&GraphStorage>
Get a reference to the underlying graph storage.
Converts to graph format if necessary.
Sourcepub fn add_diagonal(&mut self, alpha: Precision) -> Result<()>
pub fn add_diagonal(&mut self, alpha: Precision) -> Result<()>
Add a scalar multiple of the identity matrix: A = A + alpha * I
Trait Implementations§
Source§impl Clone for SparseMatrix
impl Clone for SparseMatrix
Source§fn clone(&self) -> SparseMatrix
fn clone(&self) -> SparseMatrix
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more