Struct sprs::TriMatBase

source ·
pub struct TriMatBase<IStorage, DStorage> { /* private fields */ }
Expand description

Sparse matrix in the triplet format.

Sparse matrices in the triplet format use three arrays of equal sizes (accessible through the methods row_inds, col_inds, data), the first one storing the row indices of non-zero values, the second storing the corresponding column indices and the last array storing the corresponding scalar value. If a non-zero location is repeated in the arrays, the non-zero value is taken as the sum of the corresponding scalar entries.

This format is useful for iteratively building a sparse matrix, since the various non-zero entries can be specified in any order, or even partially as is common in physics with partial derivatives equations.

This format cannot be used for arithmetic operations. Arithmetic operations are more efficient in the compressed format. A matrix in the triplet format can be converted to the compressed format using the methods to_csc and to_csr.

The TriMatBase type is parameterized by the storage type for the row and column indices, IStorage, and by the storage type for the non-zero values DStorage. Convenient aliases are availaible to specify frequent variant: TriMat refers to a triplet matrix owning the storage of its indices and and values, TriMatView refers to a triplet matrix with slices to store its indices and values, while TriMatViewMut refers to a a triplet matrix using mutable slices.

Additionaly, the type aliases TriMatI, TriMatViewI and TriMatViewMutI can be used to choose an index type different from the default usize.

Implementations§

source§

impl<N, I: SpIndex> TriMatBase<Vec<I, Global>, Vec<N, Global>>

source

pub fn new(shape: (usize, usize)) -> Self

Create a new triplet matrix of shape (nb_rows, nb_cols)

source

pub fn with_capacity(shape: (usize, usize), cap: usize) -> Self

Create a new triplet matrix of shape (nb_rows, nb_cols), and pre-allocate cap elements on the backing storage

source

pub fn from_triplets( shape: (usize, usize), row_inds: Vec<I>, col_inds: Vec<I>, data: Vec<N> ) -> Self

Create a triplet matrix from its raw components. All arrays should have the same length.

Panics
  • if the arrays don’t have the same length
  • if either the row or column indices are out of bounds.
source

pub fn add_triplet(&mut self, row: usize, col: usize, val: N)

Append a non-zero triplet to this matrix.

source

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

Reserve cap additional non-zeros

source

pub fn reserve_exact(&mut self, cap: usize)

Reserve exactly cap non-zeros

source§

impl<N, I: SpIndex, IStorage, DStorage> TriMatBase<IStorage, DStorage>where IStorage: Deref<Target = [I]>, DStorage: Deref<Target = [N]>,

source

pub fn rows(&self) -> usize

The number of rows of the matrix

source

pub fn cols(&self) -> usize

The number of cols of the matrix

source

pub fn shape(&self) -> (usize, usize)

The shape of the matrix, as a (rows, cols) tuple

source

pub fn nnz(&self) -> usize

The number of non-zero entries

source

pub fn row_inds(&self) -> &[I]

The non-zero row indices

source

pub fn col_inds(&self) -> &[I]

The non-zero column indices

source

pub fn data(&self) -> &[N]

The non-zero values

source

pub fn find_locations(&self, row: usize, col: usize) -> Vec<TripletIndex>

Find all non-zero entries at the location given by row and col

source

pub fn transpose_view(&self) -> TriMatViewI<'_, N, I>

Get a transposed view of this matrix

source

pub fn triplet_iter(&self) -> TriMatIter<Iter<'_, I>, Iter<'_, I>, Iter<'_, N>>

Get an iterator over non-zero elements stored by this matrix

source

pub fn to_csc<Iptr: SpIndex>(&self) -> CsMatI<N, I, Iptr>where N: Clone + Add<Output = N>,

Create a CSC matrix from this triplet matrix

source

pub fn to_csr<Iptr: SpIndex>(&self) -> CsMatI<N, I, Iptr>where N: Clone + Add<Output = N>,

Create a CSR matrix from this triplet matrix

source

pub fn view(&self) -> TriMatViewI<'_, N, I>

source§

impl<'a, N, I: SpIndex> TriMatBase<&'a [I], &'a [N]>

source

pub fn triplet_iter_rbr( &self ) -> TriMatIter<Iter<'a, I>, Iter<'a, I>, Iter<'a, N>>

Get an iterator over non-zero elements stored by this matrix

Reborrowing version of triplet_iter().

source§

impl<N, I: SpIndex, IStorage, DStorage> TriMatBase<IStorage, DStorage>where IStorage: DerefMut<Target = [I]>, DStorage: DerefMut<Target = [N]>,

source

pub fn set_triplet( &mut self, TripletIndex: TripletIndex, row: usize, col: usize, val: N )

Replace a non-zero value at the given index. Indices can be obtained using find_locations.

source

pub fn view_mut(&mut self) -> TriMatViewMutI<'_, N, I>

Trait Implementations§

source§

impl<IStorage: Debug, DStorage: Debug> Debug for TriMatBase<IStorage, DStorage>

source§

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

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

impl<IStorage: Hash, DStorage: Hash> Hash for TriMatBase<IStorage, DStorage>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a, N, I, IS, DS> IntoIterator for &'a TriMatBase<IS, DS>where I: 'a + SpIndex, N: 'a, IS: Deref<Target = [I]>, DS: Deref<Target = [N]>,

§

type Item = (&'a N, (I, I))

The type of the elements being iterated over.
§

type IntoIter = TriMatIter<Iter<'a, I>, Iter<'a, I>, Iter<'a, N>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<IStorage: PartialEq, DStorage: PartialEq> PartialEq<TriMatBase<IStorage, DStorage>> for TriMatBase<IStorage, DStorage>

source§

fn eq(&self, other: &TriMatBase<IStorage, DStorage>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, N, I, IS, DS> SparseMat for &'a TriMatBase<IS, DS>where I: 'a + SpIndex, N: 'a, IS: Deref<Target = [I]>, DS: Deref<Target = [N]>,

source§

fn rows(&self) -> usize

The number of rows of this matrix
source§

fn cols(&self) -> usize

The number of columns of this matrix
source§

fn nnz(&self) -> usize

The number of nonzeros of this matrix
source§

impl<N, I, IS, DS> SparseMat for TriMatBase<IS, DS>where I: SpIndex, IS: Deref<Target = [I]>, DS: Deref<Target = [N]>,

source§

fn rows(&self) -> usize

The number of rows of this matrix
source§

fn cols(&self) -> usize

The number of columns of this matrix
source§

fn nnz(&self) -> usize

The number of nonzeros of this matrix
source§

impl<IStorage: Eq, DStorage: Eq> Eq for TriMatBase<IStorage, DStorage>

source§

impl<IStorage, DStorage> StructuralEq for TriMatBase<IStorage, DStorage>

source§

impl<IStorage, DStorage> StructuralPartialEq for TriMatBase<IStorage, DStorage>

Auto Trait Implementations§

§

impl<IStorage, DStorage> RefUnwindSafe for TriMatBase<IStorage, DStorage>where DStorage: RefUnwindSafe, IStorage: RefUnwindSafe,

§

impl<IStorage, DStorage> Send for TriMatBase<IStorage, DStorage>where DStorage: Send, IStorage: Send,

§

impl<IStorage, DStorage> Sync for TriMatBase<IStorage, DStorage>where DStorage: Sync, IStorage: Sync,

§

impl<IStorage, DStorage> Unpin for TriMatBase<IStorage, DStorage>where DStorage: Unpin, IStorage: Unpin,

§

impl<IStorage, DStorage> UnwindSafe for TriMatBase<IStorage, DStorage>where DStorage: UnwindSafe, IStorage: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<SS, SP> SupersetOf<SS> for SPwhere 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§

unsafe 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.