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 lengthm + 1
.minor_indices
, an array of integers with lengthnnz
.
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 “non-zeros”, i.e. explicitly stored entries in the pattern.
Get the lane at the given index, or None
if out of bounds.
pub fn try_from_offsets_and_indices(
major_dim: usize,
minor_dim: usize,
major_offsets: Vec<usize>,
minor_indices: Vec<usize>
) -> Result<Self, SparsityPatternFormatError>
pub fn try_from_offsets_and_indices(
major_dim: usize,
minor_dim: usize,
major_offsets: Vec<usize>,
minor_indices: Vec<usize>
) -> Result<Self, SparsityPatternFormatError>
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.
pub fn entries(&self) -> SparsityPatternIter<'_>ⓘNotable traits for SparsityPatternIter<'a>
impl<'a> Iterator for SparsityPatternIter<'a> type Item = (usize, usize);
pub fn entries(&self) -> SparsityPatternIter<'_>ⓘNotable traits for SparsityPatternIter<'a>
impl<'a> Iterator for SparsityPatternIter<'a> type Item = (usize, usize);
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);
Trait Implementations
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
Auto Trait Implementations
impl RefUnwindSafe for SparsityPattern
impl Send for SparsityPattern
impl Sync for SparsityPattern
impl Unpin for SparsityPattern
impl UnwindSafe for SparsityPattern
Blanket Implementations
Mutably borrows from an owned value. Read more
type Output = T
type Output = T
Should always be Self
The inverse inclusion map: attempts to construct self
from the equivalent element of its
superset. Read more
pub fn is_in_subset(&self) -> bool
pub fn is_in_subset(&self) -> bool
Checks if self
is actually part of its subset T
(and can be converted to it).
pub fn to_subset_unchecked(&self) -> SS
pub fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset
but without any property checks. Always succeeds.
pub fn from_subset(element: &SS) -> SP
pub fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.
pub fn vzip(self) -> V