Skip to main content

CooMatrix

Struct CooMatrix 

Source
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: usize

Number of rows.

§ncol: usize

Number of columns.

§entries: Vec<(i64, i64, f64)>

The stored (row, column, value) triples, sorted by (row, column), without duplicate positions.

Implementations§

Source§

impl CooMatrix

Source

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.

Source

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.

Source

pub fn iter(&self) -> impl Iterator<Item = (i64, i64, f64)> + '_

Iterates over the stored (row, column, value) triples, in row-major order.

Source

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)]);
Source

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]);
Source

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]);
Source

pub fn to_dense(&self) -> Matrix

Converts to a dense Matrix of size nrow × ncol.

§Panics

If an entry lies outside the matrix (only possible for a hand-built CooMatrix).

Source

pub fn row_sums(&self) -> Vec<f64>

Row sums, a vector of length nrow.

§Panics

If an entry lies outside the matrix.

Source

pub fn col_sums(&self) -> Vec<f64>

Column sums, a vector of length ncol.

§Panics

If an entry lies outside the matrix.

Source

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);

Trait Implementations§

Source§

impl Clone for CooMatrix

Source§

fn clone(&self) -> CooMatrix

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CooMatrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&CooMatrix> for Matrix

Source§

fn from(m: &CooMatrix) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for CooMatrix

Source§

fn eq(&self, other: &CooMatrix) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for CooMatrix

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.