pub struct CsVecBase<IStorage, DStorage> { /* private fields */ }
Expand description
A sparse vector, storing the indices of its non-zero data.
A CsVec
represents a sparse vector by storing a sorted indices()
array
containing the locations of the non-zero values and a data()
array
containing the corresponding values. The format is compatible with CsMat
,
ie a CsVec
can represent the row of a CSR matrix without any copying.
Similar to CsMat
and TriMat
, the CsVecBase
type is parameterized
over the indexing storage backend IStorage
and the data storage backend
DStorage
. Type aliases are provided for common cases: CsVec
represents
a sparse vector owning its data, with Vec
s as storage backends;
CsVecView
represents a sparse vector borrowing its data, using slices
as storage backends; and CsVecViewMut
represents a sparse vector that
mutably borrows its data (but immutably borrows its indices).
Additionaly, the type aliases CsVecI
, CsVecViewI
, and
CsVecViewMutI
can be used to choose an index type different from the
default usize
.
Implementations
sourceimpl<N, I: SpIndex> CsVecBase<Vec<I>, Vec<N>>
impl<N, I: SpIndex> CsVecBase<Vec<I>, Vec<N>>
sourcepub fn new(n: usize, indices: Vec<I>, data: Vec<N>) -> CsVecI<N, I>where
N: Copy,
pub fn new(n: usize, indices: Vec<I>, data: Vec<N>) -> CsVecI<N, I>where
N: Copy,
Create an owning CsVec from vector data.
Panics
- if
indices
anddata
lengths differ - if the vector contains out of bounds indices
sourcepub fn empty(dim: usize) -> CsVecI<N, I>
pub fn empty(dim: usize) -> CsVecI<N, I>
Create an empty CsVec, which can be used for incremental construction
sourcepub fn append(&mut self, ind: usize, val: N)
pub fn append(&mut self, ind: usize, val: N)
Append an element to the sparse vector. Used for incremental building of the CsVec. The append should preserve the structure of the vector, ie the newly added index should be strictly greater than the last element of indices.
Panics
- Panics if
ind
is lower or equal to the last element ofself.indices()
- Panics if
ind
is greater thanself.dim()
sourcepub fn reserve_exact(&mut self, exact_size: usize)
pub fn reserve_exact(&mut self, exact_size: usize)
Reserve exactly exact_size
non-zero values.
sourceimpl<N, I, IStorage, DStorage> CsVecBase<IStorage, DStorage>where
I: SpIndex,
IStorage: Deref<Target = [I]>,
DStorage: Deref<Target = [N]>,
impl<N, I, IStorage, DStorage> CsVecBase<IStorage, DStorage>where
I: SpIndex,
IStorage: Deref<Target = [I]>,
DStorage: Deref<Target = [N]>,
sourcepub fn view(&self) -> CsVecViewI<'_, N, I>
pub fn view(&self) -> CsVecViewI<'_, N, I>
Get a view of this vector.
sourcepub fn iter(&self) -> VectorIterator<'_, N, I> ⓘ
pub fn iter(&self) -> VectorIterator<'_, N, I> ⓘ
Iterate over the non zero values.
Example
use sprs::CsVec;
let v = CsVec::new(5, vec![0, 2, 4], vec![1., 2., 3.]);
let mut iter = v.iter();
assert_eq!(iter.next(), Some((0, &1.)));
assert_eq!(iter.next(), Some((2, &2.)));
assert_eq!(iter.next(), Some((4, &3.)));
assert_eq!(iter.next(), None);
sourcepub fn into_raw_storage(self) -> (IStorage, DStorage)
pub fn into_raw_storage(self) -> (IStorage, DStorage)
Destruct the vector object and recycle its storage containers.
sourcepub fn check_structure(&self) -> Result<(), SprsError>
pub fn check_structure(&self) -> Result<(), SprsError>
Check the sparse structure, namely that:
- indices is sorted
- indices are lower than dims()
sourcepub fn to_other_types<I2>(&self) -> CsVecI<N, I2>where
N: Clone,
I2: SpIndex,
pub fn to_other_types<I2>(&self) -> CsVecI<N, I2>where
N: Clone,
I2: SpIndex,
Clone the vector with another integer type for its indices
Panics
If the indices cannot be represented by the requested integer type.
sourcepub fn row_view(&self) -> CsMatBase<N, I, Array2<I>, &'_ [I], &'_ [N]>
pub fn row_view(&self) -> CsMatBase<N, I, Array2<I>, &'_ [I], &'_ [N]>
View this vector as a matrix with only one row.
sourcepub fn col_view(&self) -> CsMatBase<N, I, Array2<I>, &'_ [I], &'_ [N]>
pub fn col_view(&self) -> CsMatBase<N, I, Array2<I>, &'_ [I], &'_ [N]>
View this vector as a matrix with only one column.
sourcepub fn get<'a>(&'a self, index: usize) -> Option<&'a N>where
I: 'a,
pub fn get<'a>(&'a self, index: usize) -> Option<&'a N>where
I: 'a,
Access element at given index, with logarithmic complexity
sourcepub fn nnz_index(&self, index: usize) -> Option<NnzIndex>
pub fn nnz_index(&self, index: usize) -> Option<NnzIndex>
Find the non-zero index of the requested dimension index, returning None if no non-zero is present at the requested location.
Looking for the NnzIndex is done with logarithmic complexity, but once it is available, the NnzIndex enables retrieving the data with O(1) complexity.
sourcepub fn dot<'b, T: IntoSparseVecIter<'b, N>>(&'b self, rhs: T) -> Nwhere
N: 'b + Num + Copy + Sum,
I: 'b,
<T as IntoSparseVecIter<'b, N>>::IterType: Iterator<Item = (usize, &'b N)>,
T: Copy,
pub fn dot<'b, T: IntoSparseVecIter<'b, N>>(&'b self, rhs: T) -> Nwhere
N: 'b + Num + Copy + Sum,
I: 'b,
<T as IntoSparseVecIter<'b, N>>::IterType: Iterator<Item = (usize, &'b N)>,
T: Copy,
Sparse vector dot product. The right-hand-side can be any type that can be interpreted as a sparse vector (hence sparse vectors, std vectors and slices, and ndarray’s dense vectors work).
However, even if dense vectors work, it is more performant to use
the dot_dense
.
Panics
If the dimension of the vectors do not match.
Example
use sprs::CsVec;
let v1 = CsVec::new(8, vec![1, 2, 4, 6], vec![1.; 4]);
let v2 = CsVec::new(8, vec![1, 3, 5, 7], vec![2.; 4]);
assert_eq!(2., v1.dot(&v2));
assert_eq!(4., v1.dot(&v1));
assert_eq!(16., v2.dot(&v2));
sourcepub fn dot_dense<T>(&self, rhs: T) -> Nwhere
T: DenseVector<N>,
N: Num + Copy + Sum,
pub fn dot_dense<T>(&self, rhs: T) -> Nwhere
T: DenseVector<N>,
N: Num + Copy + Sum,
Sparse-dense vector dot product. The right-hand-side can be any type that can be interpreted as a dense vector (hence std vectors and slices, and ndarray’s dense vectors work).
Since the dot
method can work with the same performance on
dot vectors, the main interest of this method is to enforce at
compile time that the rhs is dense.
Panics
If the dimension of the vectors do not match.
sourceimpl<'a, N, I, IStorage, DStorage> CsVecBase<IStorage, DStorage>where
N: 'a,
I: 'a + SpIndex,
IStorage: 'a + Deref<Target = [I]>,
DStorage: DerefMut<Target = [N]>,
impl<'a, N, I, IStorage, DStorage> CsVecBase<IStorage, DStorage>where
N: 'a,
I: 'a + SpIndex,
IStorage: 'a + Deref<Target = [I]>,
DStorage: DerefMut<Target = [N]>,
pub fn view_mut(&mut self) -> CsVecViewMutI<'_, N, I>
sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut N>
pub fn get_mut(&mut self, index: usize) -> Option<&mut N>
Access element at given index, with logarithmic complexity
sourcepub fn map_inplace<F>(&mut self, f: F)where
F: FnMut(&N) -> N,
pub fn map_inplace<F>(&mut self, f: F)where
F: FnMut(&N) -> N,
Apply a function to each non-zero element, mutating it
sourcepub fn iter_mut(&mut self) -> VectorIteratorMut<'_, N, I> ⓘ
pub fn iter_mut(&mut self) -> VectorIteratorMut<'_, N, I> ⓘ
Mutable iteration over the non-zero values of a sparse vector
Only the values can be changed, the sparse structure is kept.
sourceimpl<'a, N: 'a, I: 'a + SpIndex> CsVecBase<&'a [I], &'a [N]>
impl<'a, N: 'a, I: 'a + SpIndex> CsVecBase<&'a [I], &'a [N]>
sourcepub fn new_view(
n: usize,
indices: &'a [I],
data: &'a [N]
) -> Result<CsVecViewI<'a, N, I>, SprsError>
pub fn new_view(
n: usize,
indices: &'a [I],
data: &'a [N]
) -> Result<CsVecViewI<'a, N, I>, SprsError>
Create a borrowed CsVec over slice data.
sourcepub fn get_rbr(&self, index: usize) -> Option<&'a N>
pub fn get_rbr(&self, index: usize) -> Option<&'a N>
Access element at given index, with logarithmic complexity
Re-borrowing version of at()
.
sourcepub unsafe fn new_view_raw(
n: usize,
nnz: usize,
indices: *const I,
data: *const N
) -> CsVecViewI<'a, N, I>
pub unsafe fn new_view_raw(
n: usize,
nnz: usize,
indices: *const I,
data: *const N
) -> CsVecViewI<'a, N, I>
Create a borrowed CsVec over slice data without checking the structure This is unsafe because algorithms are free to assume that properties guaranteed by check_structure are enforced. For instance, non out-of-bounds indices can be relied upon to perform unchecked slice access.
sourceimpl<'a, N, I> CsVecBase<&'a [I], &'a mut [N]>where
N: 'a,
I: 'a + SpIndex,
impl<'a, N, I> CsVecBase<&'a [I], &'a mut [N]>where
N: 'a,
I: 'a + SpIndex,
sourcepub unsafe fn new_view_mut_raw(
n: usize,
nnz: usize,
indices: *const I,
data: *mut N
) -> CsVecViewMutI<'a, N, I>
pub unsafe fn new_view_mut_raw(
n: usize,
nnz: usize,
indices: *const I,
data: *mut N
) -> CsVecViewMutI<'a, N, I>
Create a borrowed CsVec over slice data without checking the structure This is unsafe because algorithms are free to assume that properties guaranteed by check_structure are enforced, and because the lifetime of the pointers is unconstrained. For instance, non out-of-bounds indices can be relied upon to perform unchecked slice access. For safety, lifetime of the resulting vector should match the lifetime of the input pointers.
Trait Implementations
sourceimpl<'a, N, I, IS1, DS1, IS2, DS2> Add<&'a CsVecBase<IS2, DS2>> for CsVecBase<IS1, DS1>where
N: Copy + Num,
I: SpIndex,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
impl<'a, N, I, IS1, DS1, IS2, DS2> Add<&'a CsVecBase<IS2, DS2>> for CsVecBase<IS1, DS1>where
N: Copy + Num,
I: SpIndex,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
sourceimpl<'a, 'b, N, I, IS1, DS1, IS2, DS2> Add<&'b CsVecBase<IS2, DS2>> for &'a CsVecBase<IS1, DS1>where
N: Copy + Num,
I: SpIndex,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
impl<'a, 'b, N, I, IS1, DS1, IS2, DS2> Add<&'b CsVecBase<IS2, DS2>> for &'a CsVecBase<IS1, DS1>where
N: Copy + Num,
I: SpIndex,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
sourceimpl<'a, N, I, IS1, DS1, IS2, DS2> Add<CsVecBase<IS2, DS2>> for &'a CsVecBase<IS1, DS1>where
N: Copy + Num,
I: SpIndex,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
impl<'a, N, I, IS1, DS1, IS2, DS2> Add<CsVecBase<IS2, DS2>> for &'a CsVecBase<IS1, DS1>where
N: Copy + Num,
I: SpIndex,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
sourceimpl<N, I, IS1, DS1, IS2, DS2> Add<CsVecBase<IS2, DS2>> for CsVecBase<IS1, DS1>where
N: Copy + Num,
I: SpIndex,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
impl<N, I, IS1, DS1, IS2, DS2> Add<CsVecBase<IS2, DS2>> for CsVecBase<IS1, DS1>where
N: Copy + Num,
I: SpIndex,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
sourceimpl<'de, IStorage, DStorage> Deserialize<'de> for CsVecBase<IStorage, DStorage>where
IStorage: Deserialize<'de>,
DStorage: Deserialize<'de>,
impl<'de, IStorage, DStorage> Deserialize<'de> for CsVecBase<IStorage, DStorage>where
IStorage: Deserialize<'de>,
DStorage: Deserialize<'de>,
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
sourceimpl<N, IS, DS> Index<NnzIndex> for CsVecBase<IS, DS>where
IS: Deref<Target = [usize]>,
DS: Deref<Target = [N]>,
impl<N, IS, DS> Index<NnzIndex> for CsVecBase<IS, DS>where
IS: Deref<Target = [usize]>,
DS: Deref<Target = [N]>,
sourceimpl<N, IS, DS> Index<usize> for CsVecBase<IS, DS>where
IS: Deref<Target = [usize]>,
DS: Deref<Target = [N]>,
impl<N, IS, DS> Index<usize> for CsVecBase<IS, DS>where
IS: Deref<Target = [usize]>,
DS: Deref<Target = [N]>,
sourceimpl<N, IS, DS> IndexMut<NnzIndex> for CsVecBase<IS, DS>where
IS: Deref<Target = [usize]>,
DS: DerefMut<Target = [N]>,
impl<N, IS, DS> IndexMut<NnzIndex> for CsVecBase<IS, DS>where
IS: Deref<Target = [usize]>,
DS: DerefMut<Target = [N]>,
sourceimpl<N, IS, DS> IndexMut<usize> for CsVecBase<IS, DS>where
IS: Deref<Target = [usize]>,
DS: DerefMut<Target = [N]>,
impl<N, IS, DS> IndexMut<usize> for CsVecBase<IS, DS>where
IS: Deref<Target = [usize]>,
DS: DerefMut<Target = [N]>,
sourceimpl<'a, N: 'a, I: 'a, IS, DS> IntoSparseVecIter<'a, N> for &'a CsVecBase<IS, DS>where
I: SpIndex,
IS: Deref<Target = [I]>,
DS: Deref<Target = [N]>,
impl<'a, N: 'a, I: 'a, IS, DS> IntoSparseVecIter<'a, N> for &'a CsVecBase<IS, DS>where
I: SpIndex,
IS: Deref<Target = [I]>,
DS: Deref<Target = [N]>,
type IterType = VectorIterator<'a, N, I>
sourcefn into_sparse_vec_iter(self) -> VectorIterator<'a, N, I> ⓘ
fn into_sparse_vec_iter(self) -> VectorIterator<'a, N, I> ⓘ
sourceimpl<'a, 'b, N, I, IS1, DS1, IpS2, IS2, DS2> Mul<&'b CsMatBase<N, I, IpS2, IS2, DS2>> for &'a CsVecBase<IS1, DS1>where
N: 'a + Copy + Num + Default,
I: 'a + SpIndex,
IS1: 'a + Deref<Target = [I]>,
DS1: 'a + Deref<Target = [N]>,
IpS2: 'b + Deref<Target = [I]>,
IS2: 'b + Deref<Target = [I]>,
DS2: 'b + Deref<Target = [N]>,
impl<'a, 'b, N, I, IS1, DS1, IpS2, IS2, DS2> Mul<&'b CsMatBase<N, I, IpS2, IS2, DS2>> for &'a CsVecBase<IS1, DS1>where
N: 'a + Copy + Num + Default,
I: 'a + SpIndex,
IS1: 'a + Deref<Target = [I]>,
DS1: 'a + Deref<Target = [N]>,
IpS2: 'b + Deref<Target = [I]>,
IS2: 'b + Deref<Target = [I]>,
DS2: 'b + Deref<Target = [N]>,
sourceimpl<'a, 'b, N, I, IpS1, IS1, DS1, IS2, DS2> Mul<&'b CsVecBase<IS2, DS2>> for &'a CsMatBase<N, I, IpS1, IS1, DS1>where
N: Copy + Num + Default + Sum,
I: SpIndex,
IpS1: Deref<Target = [I]>,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
impl<'a, 'b, N, I, IpS1, IS1, DS1, IS2, DS2> Mul<&'b CsVecBase<IS2, DS2>> for &'a CsMatBase<N, I, IpS1, IS1, DS1>where
N: Copy + Num + Default + Sum,
I: SpIndex,
IpS1: Deref<Target = [I]>,
IS1: Deref<Target = [I]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [I]>,
DS2: Deref<Target = [N]>,
sourceimpl<IStorage: PartialEq, DStorage: PartialEq> PartialEq<CsVecBase<IStorage, DStorage>> for CsVecBase<IStorage, DStorage>
impl<IStorage: PartialEq, DStorage: PartialEq> PartialEq<CsVecBase<IStorage, DStorage>> for CsVecBase<IStorage, DStorage>
sourceimpl<IStorage, DStorage> Serialize for CsVecBase<IStorage, DStorage>where
IStorage: Serialize,
DStorage: Serialize,
impl<IStorage, DStorage> Serialize for CsVecBase<IStorage, DStorage>where
IStorage: Serialize,
DStorage: Serialize,
sourceimpl<'a, 'b, N, IS1, DS1, IS2, DS2> Sub<&'b CsVecBase<IS2, DS2>> for &'a CsVecBase<IS1, DS1>where
N: Copy + Num,
IS1: Deref<Target = [usize]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [usize]>,
DS2: Deref<Target = [N]>,
impl<'a, 'b, N, IS1, DS1, IS2, DS2> Sub<&'b CsVecBase<IS2, DS2>> for &'a CsVecBase<IS1, DS1>where
N: Copy + Num,
IS1: Deref<Target = [usize]>,
DS1: Deref<Target = [N]>,
IS2: Deref<Target = [usize]>,
DS2: Deref<Target = [N]>,
impl<IStorage, DStorage> StructuralPartialEq for CsVecBase<IStorage, DStorage>
Auto Trait Implementations
impl<IStorage, DStorage> RefUnwindSafe for CsVecBase<IStorage, DStorage>where
DStorage: RefUnwindSafe,
IStorage: RefUnwindSafe,
impl<IStorage, DStorage> Send for CsVecBase<IStorage, DStorage>where
DStorage: Send,
IStorage: Send,
impl<IStorage, DStorage> Sync for CsVecBase<IStorage, DStorage>where
DStorage: Sync,
IStorage: Sync,
impl<IStorage, DStorage> Unpin for CsVecBase<IStorage, DStorage>where
DStorage: Unpin,
IStorage: Unpin,
impl<IStorage, DStorage> UnwindSafe for CsVecBase<IStorage, DStorage>where
DStorage: UnwindSafe,
IStorage: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
sourcefn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moresourcefn 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).sourceunsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.sourcefn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.