Struct SparsityPattern

Source
pub struct SparsityPattern { /* private fields */ }
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§

Source§

impl SparsityPattern

Source

pub fn zeros(major_dim: usize, minor_dim: usize) -> Self

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

Source

pub fn major_offsets(&self) -> &[usize]

The offsets for the major dimension.

Source

pub fn minor_indices(&self) -> &[usize]

The indices for the minor dimension.

Source

pub fn major_dim(&self) -> usize

The number of major lanes in the pattern.

Source

pub fn minor_dim(&self) -> usize

The number of minor lanes in the pattern.

Source

pub fn nnz(&self) -> usize

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

Source

pub fn lane(&self, major_index: usize) -> &[usize]

Get the lane at the given index.

§Panics

Panics if major_index is out of bounds.

Source

pub fn get_lane(&self, major_index: usize) -> Option<&[usize]>

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

Source

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.

Source

pub unsafe fn from_offset_and_indices_unchecked( major_dim: usize, minor_dim: usize, major_offsets: Vec<usize>, minor_indices: Vec<usize>, ) -> Self

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

§Panics

Panics if the number of major offsets is not exactly one greater than the major dimension or if major offsets do not start with 0 and end with the number of minor indices.

§Safety

Assumes that the major offsets and indices adhere to the requirements of being a valid sparsity pattern. Specifically, that major offsets is monotonically increasing, and major_offsets[i]..major_offsets[i+1] refers to a major lane in the sparsity pattern, and minor_indices[major_offsets[i]..major_offsets[i+1]] is monotonically increasing.

Source

pub fn entries(&self) -> SparsityPatternIter<'_>

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

pub fn disassemble(self) -> (Vec<usize>, Vec<usize>)

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

pub fn transpose(&self) -> Self

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§

Source§

impl Clone for SparsityPattern

Source§

fn clone(&self) -> SparsityPattern

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SparsityPattern

Source§

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

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

impl Default for SparsityPattern

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for SparsityPattern

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Eq for SparsityPattern

Source§

impl StructuralPartialEq for SparsityPattern

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

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

fn is_in_subset(&self) -> bool

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

fn to_subset_unchecked(&self) -> SS

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

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

OSZAR »