Struct CooMatrix

Source
pub struct CooMatrix<T> { /* private fields */ }
Expand description

A COO representation of a sparse matrix.

A COO matrix stores entries in coordinate-form, that is triplets (i, j, v), where i and j correspond to row and column indices of the entry, and v to the value of the entry. The format is of limited use for standard matrix operations. Its main purpose is to facilitate easy construction of other, more efficient matrix formats (such as CSR/COO), and the conversion between different formats.

§Format

For given dimensions nrows and ncols, the matrix is represented by three same-length arrays row_indices, col_indices and values that constitute the coordinate triplets of the matrix. The indices must be in bounds, but duplicate entries are explicitly allowed. Upon conversion to other formats, the duplicate entries may be summed together. See the documentation for the respective conversion functions.

§Examples

use nalgebra_sparse::{coo::CooMatrix, csr::CsrMatrix, csc::CscMatrix};

// Initialize a matrix with all zeros (no explicitly stored entries).
let mut coo = CooMatrix::new(4, 4);
// Or initialize it with a set of triplets
coo = CooMatrix::try_from_triplets(4, 4, vec![1, 2], vec![0, 1], vec![3.0, 4.0]).unwrap();

// Push a few triplets
coo.push(2, 0, 1.0);
coo.push(0, 1, 2.0);

// Convert to other matrix formats
let csr = CsrMatrix::from(&coo);
let csc = CscMatrix::from(&coo);

Implementations§

Source§

impl<T: Scalar> CooMatrix<T>

Source

pub fn push_matrix<R: Dim, C: Dim, S: RawStorage<T, R, C>>( &mut self, r: usize, c: usize, m: &Matrix<T, R, C, S>, )

Pushes a dense matrix into the sparse one.

This adds the dense matrix m starting at the rth row and cth column to the matrix.

§Panics

Panics if any part of the dense matrix is out of bounds of the sparse matrix when inserted at (r, c).

Source§

impl<T> CooMatrix<T>

Source

pub fn new(nrows: usize, ncols: usize) -> Self

Construct a zero COO matrix of the given dimensions.

Specifically, the collection of triplets - corresponding to explicitly stored entries - is empty, so that the matrix (implicitly) represented by the COO matrix consists of all zero entries.

Source

pub fn zeros(nrows: usize, ncols: usize) -> Self

Construct a zero COO matrix of the given dimensions.

Specifically, the collection of triplets - corresponding to explicitly stored entries - is empty, so that the matrix (implicitly) represented by the COO matrix consists of all zero entries.

Source

pub fn try_from_triplets( nrows: usize, ncols: usize, row_indices: Vec<usize>, col_indices: Vec<usize>, values: Vec<T>, ) -> Result<Self, SparseFormatError>

Try to construct a COO matrix from the given dimensions and a collection of (i, j, v) triplets.

Returns an error if either row or column indices contain indices out of bounds, or if the data arrays do not all have the same length. Note that the COO format inherently supports duplicate entries.

Source

pub fn try_from_triplets_iter( nrows: usize, ncols: usize, triplets: impl IntoIterator<Item = (usize, usize, T)>, ) -> Result<Self, SparseFormatError>

Try to construct a COO matrix from the given dimensions and a finite iterator of (i, j, v) triplets.

Returns an error if either row or column indices contain indices out of bounds. Note that the COO format inherently supports duplicate entries, but they are not eagerly summed.

Implementation note: Calls try_from_triplets so each value is scanned twice.

Source

pub fn triplet_iter(&self) -> impl Iterator<Item = (usize, usize, &T)>

An iterator over triplets (i, j, v).

Source

pub fn triplet_iter_mut( &mut self, ) -> impl Iterator<Item = (usize, usize, &mut T)>

A mutable iterator over triplets (i, j, v).

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for COO matrix by at least additional elements.

This increase the capacities of triplet holding arrays by reserving more space to avoid frequent reallocations in push operations.

§Panics

Panics if any of the individual allocation of triplet arrays fails.

§Example
let mut coo = CooMatrix::new(4, 4);
// Reserve capacity in advance
coo.reserve(10);
coo.push(1, 0, 3.0);
Source

pub fn push(&mut self, i: usize, j: usize, v: T)

Push a single triplet to the matrix.

This adds the value v to the ith row and jth column in the matrix.

§Panics

Panics if i or j is out of bounds.

Source

pub fn clear_triplets(&mut self)

Clear all triplets from the matrix.

Source

pub fn nrows(&self) -> usize

The number of rows in the matrix.

Source

pub fn ncols(&self) -> usize

The number of columns in the matrix.

Source

pub fn nnz(&self) -> usize

The number of explicitly stored entries in the matrix.

This number includes duplicate entries. For example, if the CooMatrix contains duplicate entries, then it may have a different number of non-zeros as reported by nnz() compared to its CSR representation.

Source

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

The row indices of the explicitly stored entries.

Source

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

The column indices of the explicitly stored entries.

Source

pub fn values(&self) -> &[T]

The values of the explicitly stored entries.

Source

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

Disassembles the matrix into individual triplet arrays.

§Examples
let row_indices = vec![0, 1];
let col_indices = vec![1, 2];
let values = vec![1.0, 2.0];
let coo = CooMatrix::try_from_triplets(2, 3, row_indices, col_indices, values)
    .unwrap();

let (row_idx, col_idx, val) = coo.disassemble();
assert_eq!(row_idx, vec![0, 1]);
assert_eq!(col_idx, vec![1, 2]);
assert_eq!(val, vec![1.0, 2.0]);

Trait Implementations§

Source§

impl<T: Clone> Clone for CooMatrix<T>

Source§

fn clone(&self) -> CooMatrix<T>

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<T: Debug> Debug for CooMatrix<T>

Source§

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

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

impl<'a, T> From<&'a CooMatrix<T>> for CscMatrix<T>

Source§

fn from(matrix: &'a CooMatrix<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a CooMatrix<T>> for CsrMatrix<T>

Source§

fn from(matrix: &'a CooMatrix<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a CooMatrix<T>> for DMatrix<T>

Source§

fn from(coo: &'a CooMatrix<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a CscMatrix<T>> for CooMatrix<T>
where T: Scalar + Zero,

Source§

fn from(matrix: &'a CscMatrix<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a CsrMatrix<T>> for CooMatrix<T>

Source§

fn from(matrix: &'a CsrMatrix<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T, R, C, S> From<&'a Matrix<T, R, C, S>> for CooMatrix<T>
where T: Scalar + Zero, R: Dim, C: Dim, S: RawStorage<T, R, C>,

Source§

fn from(matrix: &'a Matrix<T, R, C, S>) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone> Matrix<T> for CooMatrix<T>

Source§

fn rows(&self) -> usize

Source§

fn cols(&self) -> usize

Source§

fn access(&self) -> Access<'_, T>

Expose dense or sparse access to the matrix.
Source§

impl<T: PartialEq> PartialEq for CooMatrix<T>

Source§

fn eq(&self, other: &CooMatrix<T>) -> 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<T: Clone> SparseAccess<T> for CooMatrix<T>

Source§

fn nnz(&self) -> usize

Number of non-zero elements in the matrix.
Source§

fn fetch_triplets(&self) -> Vec<(usize, usize, T)>

Retrieve the triplets that identify the coefficients of the sparse matrix.
Source§

impl<T: Eq> Eq for CooMatrix<T>

Source§

impl<T: MatrixMarketScalar> MatrixMarketExport<T> for CooMatrix<T>

Source§

impl<T> StructuralPartialEq for CooMatrix<T>

Auto Trait Implementations§

§

impl<T> Freeze for CooMatrix<T>

§

impl<T> RefUnwindSafe for CooMatrix<T>
where T: RefUnwindSafe,

§

impl<T> Send for CooMatrix<T>
where T: Send,

§

impl<T> Sync for CooMatrix<T>
where T: Sync,

§

impl<T> Unpin for CooMatrix<T>
where T: Unpin,

§

impl<T> UnwindSafe for CooMatrix<T>
where T: UnwindSafe,

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 »