Skip to main content

SparseMat

Type Alias SparseMat 

Source
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 with entry, and entries at the same position are summed. Constructors such as new, from_triplets and from_dense create 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 */ }

Trait Implementations§

Source§

impl Add for &SparseMat

Source§

fn add(self, rhs: &SparseMat) -> Result<SparseMat>

a + b, see SparseMat::add.

Source§

type Output = Result<igraph_sparsemat_t, Error>

The resulting type after applying the + operator.
Source§

impl Mul for &SparseMat

Source§

fn mul(self, rhs: &SparseMat) -> Result<SparseMat>

a * b, see SparseMat::multiply.

Source§

type Output = Result<igraph_sparsemat_t, Error>

The resulting type after applying the * operator.
Source§

impl Sub for &SparseMat

Source§

fn sub(self, rhs: &SparseMat) -> Result<SparseMat>

a - b, see SparseMat::add.

Source§

type Output = Result<igraph_sparsemat_t, Error>

The resulting type after applying the - operator.
Source§

impl TryFrom<&igraph_matrix_t> for SparseMat

Source§

fn try_from(m: &Matrix) -> Result<Self>

Keeps the non-zero elements, see SparseMat::from_dense.

Source§

type Error = Error

The type returned in the event of a conversion error.