pub type SparseMat = igraph_sparsemat_t;Expand description
An owned sparse matrix of reals (igraph_sparsemat_t), backed by the
CXSparse library bundled with igraph.
A sparse matrix is stored in one of two formats (see SparseMatType):
- triplet (a.k.a. coordinate) format: a list of
(row, col, value)entries. It is the format in which matrices are built: new entries are appended withentry, and entries at the same position are summed. Constructors such asnew,from_tripletsandfrom_densecreate triplet matrices. - column-compressed (CSC) format: the format in which most computations
happen. Convert with
compress.
Most read-only operations accept both formats: when the C function
requires a column-compressed matrix, the wrapper compresses a temporary
copy. Mutating operations that need the compressed format (such as
dupl or fkeep) convert self in place.
Row and column indices are zero-based. The matrix owns its storage and frees
it on Drop; it is Clone (igraph_sparsemat_init_copy) and
Send/Sync.
See the igraph documentation on sparse matrices.
See also the graph matrices of other modules, which convert naturally
with SparseMat::from_triplets (casting the indices between
VertexId and usize):
Graph::get_adjacency_sparse,
Graph::get_stochastic_sparse
(conversion) and
Graph::get_laplacian_sparse
(structural); in the other direction
Graph::sparse_adjacency and
Graph::sparse_weighted_adjacency
(constructors) build a graph from triplets.
§Examples
use igraph::linalg::SparseMat;
// [ 4 1 0 ]
// [ 1 3 0 ]
// [ 0 0 2 ]
let a = SparseMat::from_triplets(3, 3, &[(0, 0, 4.0), (0, 1, 1.0), (1, 0, 1.0), (1, 1, 3.0), (2, 2, 2.0)])
.unwrap()
.compress()
.unwrap();
assert_eq!(a.shape(), (3, 3));
assert_eq!(a.get(0, 1), 1.0);
assert_eq!(a.mul_vec(&[1.0, 1.0, 1.0]).unwrap(), vec![5.0, 4.0, 2.0]);
// Solve A x = b by Cholesky factorization (A is symmetric positive definite).
let x = a.cholsol(&[5.0, 4.0, 2.0], igraph::linalg::SparseOrdering::Natural).unwrap();
for xi in x {
assert!((xi - 1.0).abs() < 1e-12);
}
// Arithmetic with operators returns `Result`s.
let twice = (&a + &a).unwrap();
assert_eq!(twice.get(0, 0), 8.0);Aliased Type§
pub struct SparseMat { /* private fields */ }