Skip to main content

Module matrix

Module matrix 

Source
Expand description

Owned, column-major igraph matrices.

Matrix (igraph_matrix_t, reals), MatrixInt (igraph_matrix_int_t), MatrixBool (igraph_matrix_bool_t), MatrixChar (igraph_matrix_char_t) and MatrixComplex (igraph_matrix_complex_t) are the C structs themselves, enriched with Rusty behaviour: they own their storage (freed on Drop), are indexed with m[(row, col)], and convert from and to row-major Vec<Vec<T>>. The elements are stored in column-major order, as in igraph, and as_slice exposes them in that order.

use igraph::prelude::*;

let mut m = Matrix::from_rows(&[vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]]).unwrap();
assert_eq!((m.nrow(), m.ncol()), (3, 2));
assert_eq!(m[(2, 1)], 6.0);
m[(0, 0)] = 10.0;
assert_eq!(m.as_slice(), &[10.0, 3.0, 5.0, 2.0, 4.0, 6.0]); // column-major
assert_eq!(m.row(1), vec![3.0, 4.0]);
assert_eq!(m.column(1), &[2.0, 4.0, 6.0]);

The module covers igraph_matrix_pmt.h:

GroupMethodsTypes
constructionzeros, new, from_rows, from_row_major, from_column_major, view (zero-copy input), identity (real)all
access[(i, j)], get, row, column, rows, columns, diagonal, indexed_iter, to_rows, as_sliceall
structureset_row, set_col, select_rows, select_cols, select_rows_cols, swap_rows, swap_cols, transpose, transposed, rbind, cbind, add_rows, add_cols, remove_row, remove_col, resizeall
queriesis_symmetric, contains, search, fill, capacity, shrink_to_fitall
ordermin, max, which_min, which_max, minmax, maxdifference, all_l, all_g, all_le, all_gereal, int, char
arithmeticadd, sub, mul_elements, div_elements, scale, add_constant, sum, prod, rowsums, colsumsreal, int, complex
real onlyidentity, matmul, mul_vec, all_almost_e, zapsmallreal
complex onlyfrom_parts, from_polar, real, imag, realimag, all_almost_e, zapsmallcomplex
bool onlycount_truebool

Integer arithmetic is done in Rust with wrapping semantics (signed overflow and division by zero are undefined behaviour in the C implementation); char matrices have no arithmetic. Every index is checked before reaching igraph, which does not check them.

use igraph::prelude::*;

// The transition matrix of a random walk on the path 0 - 1 - 2.
let path = Graph::ring(3, false, false, false).unwrap();
let adj = path.get_adjacency(GetAdjacency::Both, None, Loops::Twice).unwrap();
assert_eq!(adj, Matrix::from_rows(&[[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]).unwrap());
assert!(adj.is_symmetric());
// Row sums of the adjacency matrix are the degrees...
let degrees = adj.rowsums();
assert_eq!(degrees, vec![1.0, 2.0, 1.0]);
// ...and dividing each row by them gives the row-stochastic matrix.
let mut p = adj.clone();
for (i, d) in degrees.iter().enumerate() {
    let row: Vec<f64> = p.row(i).iter().map(|x| x / d).collect();
    p.set_row(i, &row).unwrap();
}
assert_eq!(p, path.get_stochastic(false, None).unwrap());
assert_eq!(p.rowsums(), vec![1.0, 1.0, 1.0]);
// Two steps from the middle vertex bring the walker back with probability 1.
assert_eq!(p.matmul(&p).unwrap()[(1, 1)], 1.0);

See also Graph::get_adjacency and Graph::adjacency to convert between graphs and matrices, MatrixList for lists of matrices, linalg for eigenvalue problems, BLAS/LAPACK routines and sparse matrices, and layout, whose layouts are n × 2 (or n × 3) coordinate matrices.

Type Aliases§

Matrix
Owned column-major matrix of reals (igraph_matrix_t), see the module docs.
MatrixBool
Owned column-major matrix of booleans (igraph_matrix_bool_t), see the module docs.
MatrixChar
Owned column-major matrix of chars (igraph_matrix_char_t), see the module docs.
MatrixComplex
Owned column-major matrix of complex numbers (igraph_matrix_complex_t), see the module docs; igraph returns such matrices e.g. as the eigenvectors of non-symmetric matrices.
MatrixInt
Owned column-major matrix of integers (igraph_matrix_int_t), see the module docs.