pub struct CooMatrix {
pub nrow: usize,
pub ncol: usize,
pub entries: Vec<(i64, i64, f64)>,
}Expand description
A sparse real matrix in coordinate (triplet) format.
Returned by the sparse conversion functions of this module
(Graph::get_adjacency_sparse, Graph::get_stochastic_sparse).
Each stored element appears exactly once in entries
as a (row, column, value) triple; entries are sorted by row, then by
column. Elements that are not listed are zero.
It is a plain-Rust snapshot of an igraph SparseMat, with a few
helpers (products with vectors, row and column sums, transposition);
to_sparsemat and to_dense
convert it to igraph’s sparse and dense matrices.
use igraph::prelude::*;
let g = Graph::from_edges(&[(0, 1), (1, 2)], 3, false).unwrap();
let a = g.get_adjacency_sparse(GetAdjacency::Upper, None, Loops::Twice).unwrap();
assert_eq!((a.nrow, a.ncol), (3, 3));
assert_eq!(a.entries, vec![(0, 1, 1.0), (1, 2, 1.0)]);
assert_eq!(a.get(1, 2), 1.0);
assert_eq!(a.get(2, 1), 0.0);
assert_eq!(a.to_dense(), g.get_adjacency(GetAdjacency::Upper, None, Loops::Twice).unwrap());Fields§
§nrow: usizeNumber of rows.
ncol: usizeNumber of columns.
entries: Vec<(i64, i64, f64)>The stored (row, column, value) triples, sorted by (row, column),
without duplicate positions.
Implementations§
Source§impl CooMatrix
impl CooMatrix
Sourcepub fn nnz(&self) -> usize
pub fn nnz(&self) -> usize
Number of stored entries.
Every structurally non-zero position is stored once. A stored value
may still be 0.0, e.g. for an edge of weight zero, or in the rows
of zero-strength vertices of a stochastic matrix.
Sourcepub fn get(&self, row: i64, col: i64) -> f64
pub fn get(&self, row: i64, col: i64) -> f64
The element at (row, col), zero if it is not stored.
Runs in O(log nnz) time with a binary search, so it relies on the
entries being sorted and unique, as they are in every matrix returned
by this module.
Sourcepub fn iter(&self) -> impl Iterator<Item = (i64, i64, f64)> + '_
pub fn iter(&self) -> impl Iterator<Item = (i64, i64, f64)> + '_
Iterates over the stored (row, column, value) triples, in
row-major order.
Sourcepub fn transpose(&self) -> CooMatrix
pub fn transpose(&self) -> CooMatrix
The transposed matrix (ncol × nrow), again sorted in row-major
order.
use igraph::prelude::*;
let g = Graph::from_edges(&[(0, 1), (0, 2)], 3, true).unwrap();
let a = g.get_adjacency_sparse(GetAdjacency::Both, None, Loops::Once).unwrap();
assert_eq!(a.transpose().entries, vec![(1, 0, 1.0), (2, 0, 1.0)]);Sourcepub fn mul_vec(&self, x: &[f64]) -> Vec<f64>
pub fn mul_vec(&self, x: &[f64]) -> Vec<f64>
The matrix-vector product A · x, a vector of length
nrow. Runs in O(nrow + nnz) time.
§Panics
If x.len() != ncol, or if an entry lies outside the matrix.
use igraph::prelude::*;
// Out-degrees of a directed graph: A · 1.
let g = Graph::from_edges(&[(0, 1), (0, 2), (2, 1)], 3, true).unwrap();
let a = g.get_adjacency_sparse(GetAdjacency::Both, None, Loops::Once).unwrap();
assert_eq!(a.mul_vec(&[1.0; 3]), vec![2.0, 0.0, 1.0]);Sourcepub fn vec_mul(&self, x: &[f64]) -> Vec<f64>
pub fn vec_mul(&self, x: &[f64]) -> Vec<f64>
The vector-matrix product xᵀ · A, a vector of length
ncol. Runs in O(ncol + nnz) time.
With a row-stochastic matrix P (see
Graph::get_stochastic_sparse) this is one step of a random walk:
if x is the distribution of the walker, xᵀ · P is its distribution
after one more step.
§Panics
If x.len() != nrow, or if an entry lies outside the matrix.
use igraph::prelude::*;
// A walker on the directed cycle 0 → 1 → 2 → 0 moves one step ahead.
let g = Graph::from_edges(&[(0, 1), (1, 2), (2, 0)], 3, true).unwrap();
let p = g.get_stochastic_sparse(false, None).unwrap();
assert_eq!(p.vec_mul(&[1.0, 0.0, 0.0]), vec![0.0, 1.0, 0.0]);Sourcepub fn to_sparsemat(&self) -> Result<SparseMat>
pub fn to_sparsemat(&self) -> Result<SparseMat>
Converts to an igraph sparse matrix (SparseMat, in triplet
format), for the sparse linear algebra of crate::linalg. Duplicate
positions (possible only in a hand-built CooMatrix) are summed.
Most SparseMat operations accept the triplet format and compress a
temporary copy when they need to; call SparseMat::compress once
up front when the matrix is used repeatedly.
§Errors
ErrorKind::InvalidValue if an
entry has a negative index or lies outside the matrix (only possible
for a hand-built CooMatrix).
§Examples
use igraph::prelude::*;
// The squared adjacency matrix of a path counts the walks of length 2.
let path = Graph::from_edges(&[(0, 1), (1, 2), (2, 3)], 4, false).unwrap();
let a = path
.get_adjacency_sparse(GetAdjacency::Both, None, Loops::Twice)
.unwrap()
.to_sparsemat()
.unwrap()
.compress()
.unwrap();
let a2 = a.multiply(&a).unwrap();
assert_eq!(a2.get(0, 2), 1.0); // 0 - 1 - 2
assert_eq!(a2.get(1, 1), 2.0); // 1 - 0 - 1 and 1 - 2 - 1
assert_eq!(a2.get(0, 3), 0.0);