Struct nalgebra_sparse::pattern::SparsityPattern[][src]

pub struct SparsityPattern { /* fields omitted */ }
Expand description

A representation of the sparsity pattern of a CSR or CSC matrix.

CSR and CSC matrices store matrices in a very similar fashion. In fact, in a certain sense, they are transposed. More precisely, when reinterpreting the three data arrays of a CSR matrix as a CSC matrix, we obtain the CSC representation of its transpose.

SparsityPattern is an abstraction built on this observation. Whereas CSR matrices store a matrix row-by-row, and a CSC matrix stores a matrix column-by-column, a SparsityPattern represents only the index data structure of a matrix lane-by-lane. Here, a lane is a generalization of rows and columns. We further define major lanes and minor lanes. The sparsity pattern of a CSR matrix is then obtained by interpreting major/minor as row/column. Conversely, we obtain the sparsity pattern of a CSC matrix by interpreting major/minor as column/row.

This allows us to use a common abstraction to talk about sparsity patterns of CSR and CSC matrices. This is convenient, because at the abstract level, the invariants of the formats are the same. Hence we may encode the invariants of the index data structure separately from the scalar values of the matrix. This is especially useful in applications where the sparsity pattern is built ahead of the matrix values, or the same sparsity pattern is re-used between different matrices. Finally, we can use SparsityPattern to encode adjacency information in graphs.

Format

The format is exactly the same as for the index data structures of CSR and CSC matrices. This means that the sparsity pattern of an m x n sparse matrix with nnz non-zeros, where in this case m x n does not mean rows x columns, but rather majors x minors, is represented by the following two arrays:

  • major_offsets, an array of integers with length m + 1.
  • minor_indices, an array of integers with length nnz.

The invariants and relationship between major_offsets and minor_indices remain the same as for row_offsets and col_indices in the CSR format specification.

Implementations

Create a sparsity pattern of the given dimensions without explicitly stored entries.

The offsets for the major dimension.

The indices for the minor dimension.

The number of major lanes in the pattern.

The number of minor lanes in the pattern.

The number of “non-zeros”, i.e. explicitly stored entries in the pattern.

Get the lane at the given index.

Panics

Panics if major_index is out of bounds.

Get the lane at the given index, or None if out of bounds.

Try to construct a sparsity pattern from the given dimensions, major offsets and minor indices.

Returns an error if the data does not conform to the requirements.

An iterator over the explicitly stored “non-zero” entries (i, j).

The iteration happens in a lane-major fashion, meaning that the lane index i increases monotonically, and the minor index j increases monotonically within each lane i.

Examples

let offsets = vec![0, 2, 3, 4];
let minor_indices = vec![0, 2, 1, 0];
let pattern = SparsityPattern::try_from_offsets_and_indices(3, 4, offsets, minor_indices)
    .unwrap();

let entries: Vec<_> = pattern.entries().collect();
assert_eq!(entries, vec![(0, 0), (0, 2), (1, 1), (2, 0)]);

Returns the raw offset and index data for the sparsity pattern.

Examples

let offsets = vec![0, 2, 3, 4];
let minor_indices = vec![0, 2, 1, 0];
let pattern = SparsityPattern::try_from_offsets_and_indices(
        3,
        4,
        offsets.clone(),
        minor_indices.clone())
    .unwrap();
let (offsets2, minor_indices2) = pattern.disassemble();
assert_eq!(offsets2, offsets);
assert_eq!(minor_indices2, minor_indices);

Computes the transpose of the sparsity pattern.

This is analogous to matrix transposition, i.e. an entry (i, j) becomes (j, i) in the new pattern.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

Checks if self is actually part of its subset T (and can be converted to it).

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

The inclusion map: converts self to the equivalent element of its superset.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.