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>>
impl<N, I: SpIndex> TriMatBase<Vec<I, Global>, Vec<N, Global>>
sourcepub fn new(shape: (usize, usize)) -> Self
pub fn new(shape: (usize, usize)) -> Self
Create a new triplet matrix of shape (nb_rows, nb_cols)
sourcepub fn with_capacity(shape: (usize, usize), cap: usize) -> Self
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
sourcepub fn from_triplets(
shape: (usize, usize),
row_inds: Vec<I>,
col_inds: Vec<I>,
data: Vec<N>
) -> Self
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.
sourcepub fn add_triplet(&mut self, row: usize, col: usize, val: N)
pub fn add_triplet(&mut self, row: usize, col: usize, val: N)
Append a non-zero triplet to this matrix.
sourcepub fn reserve_exact(&mut self, cap: usize)
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]>,
impl<N, I: SpIndex, IStorage, DStorage> TriMatBase<IStorage, DStorage>where IStorage: Deref<Target = [I]>, DStorage: Deref<Target = [N]>,
sourcepub fn find_locations(&self, row: usize, col: usize) -> Vec<TripletIndex>
pub fn find_locations(&self, row: usize, col: usize) -> Vec<TripletIndex>
Find all non-zero entries at the location given by row
and col
sourcepub fn transpose_view(&self) -> TriMatViewI<'_, N, I>
pub fn transpose_view(&self) -> TriMatViewI<'_, N, I>
Get a transposed view of this matrix
sourcepub fn triplet_iter(&self) -> TriMatIter<Iter<'_, I>, Iter<'_, I>, Iter<'_, N>> ⓘ
pub fn triplet_iter(&self) -> TriMatIter<Iter<'_, I>, Iter<'_, I>, Iter<'_, N>> ⓘ
Get an iterator over non-zero elements stored by this matrix
sourcepub fn to_csc<Iptr: SpIndex>(&self) -> CsMatI<N, I, Iptr>where
N: Clone + Add<Output = N>,
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
sourcepub fn to_csr<Iptr: SpIndex>(&self) -> CsMatI<N, I, Iptr>where
N: Clone + Add<Output = N>,
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
pub fn view(&self) -> TriMatViewI<'_, N, I>
source§impl<'a, N, I: SpIndex> TriMatBase<&'a [I], &'a [N]>
impl<'a, N, I: SpIndex> TriMatBase<&'a [I], &'a [N]>
sourcepub fn triplet_iter_rbr(
&self
) -> TriMatIter<Iter<'a, I>, Iter<'a, I>, Iter<'a, N>> ⓘ
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]>,
impl<N, I: SpIndex, IStorage, DStorage> TriMatBase<IStorage, DStorage>where IStorage: DerefMut<Target = [I]>, DStorage: DerefMut<Target = [N]>,
sourcepub fn set_triplet(
&mut self,
TripletIndex: TripletIndex,
row: usize,
col: usize,
val: N
)
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
.
pub fn view_mut(&mut self) -> TriMatViewMutI<'_, N, I>
Trait Implementations§
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]>,
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]>,
source§impl<IStorage: PartialEq, DStorage: PartialEq> PartialEq<TriMatBase<IStorage, DStorage>> for TriMatBase<IStorage, DStorage>
impl<IStorage: PartialEq, DStorage: PartialEq> PartialEq<TriMatBase<IStorage, DStorage>> for TriMatBase<IStorage, DStorage>
source§fn eq(&self, other: &TriMatBase<IStorage, DStorage>) -> bool
fn eq(&self, other: &TriMatBase<IStorage, DStorage>) -> bool
self
and other
values to be equal, and is used
by ==
.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]>,
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§impl<N, I, IS, DS> SparseMat for TriMatBase<IS, DS>where
I: SpIndex,
IS: Deref<Target = [I]>,
DS: Deref<Target = [N]>,
impl<N, I, IS, DS> SparseMat for TriMatBase<IS, DS>where I: SpIndex, IS: Deref<Target = [I]>, DS: Deref<Target = [N]>,
impl<IStorage: Eq, DStorage: Eq> Eq for TriMatBase<IStorage, DStorage>
impl<IStorage, DStorage> StructuralEq for TriMatBase<IStorage, DStorage>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).source§unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.