pub struct CsVecBase<IStorage, DStorage, N, I: SpIndex = usize>{ /* 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§
source§impl<N, I: SpIndex, DStorage, IStorage> CsVecBase<IStorage, DStorage, N, I>
impl<N, I: SpIndex, DStorage, IStorage> CsVecBase<IStorage, DStorage, N, I>
§Constructor methods
sourcepub fn new(n: usize, indices: IStorage, data: DStorage) -> Self
pub fn new(n: usize, indices: IStorage, data: DStorage) -> Self
Create a sparse vector
§Panics
- if
indices
anddata
lengths differ - if the vector contains out of bounds indices
- if indices are out of order
§Examples
// Creating a sparse owned vector
let owned = CsVec::new(10, vec![0, 4], vec![-4, 2]);
// Creating a sparse borrowing vector with `I = u16`
let borrow = CsVecViewI::new(10, &[0_u16, 4], &[-4, 2]);
// Creating a general sparse vector with different storage types
let mixed = CsVecBase::new(10, &[0_u64, 4] as &[_], vec![-4, 2]);
sourcepub fn try_new(
n: usize,
indices: IStorage,
data: DStorage,
) -> Result<Self, (IStorage, DStorage, StructureError)>
pub fn try_new( n: usize, indices: IStorage, data: DStorage, ) -> Result<Self, (IStorage, DStorage, StructureError)>
Try create a sparse vector from the given buffers
Will return the buffers along with the error if conversion is illegal
sourcepub unsafe fn new_uncheked(n: usize, indices: IStorage, data: DStorage) -> Self
pub unsafe fn new_uncheked(n: usize, indices: IStorage, data: DStorage) -> Self
Create a CsVec
without checking the structure
§Safety
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.
source§impl<N, I: SpIndex, DStorage, IStorage> CsVecBase<IStorage, DStorage, N, I>
impl<N, I: SpIndex, DStorage, IStorage> CsVecBase<IStorage, DStorage, N, I>
sourcepub fn new_from_unsorted(
n: usize,
indices: IStorage,
data: DStorage,
) -> Result<Self, (IStorage, DStorage, StructureError)>where
N: Clone,
pub fn new_from_unsorted(
n: usize,
indices: IStorage,
data: DStorage,
) -> Result<Self, (IStorage, DStorage, StructureError)>where
N: Clone,
Creates a sparse vector
Will sort indices and data if necessary
source§impl<N, I: SpIndex> CsVecBase<Vec<I>, Vec<N>, N, I>
impl<N, I: SpIndex> CsVecBase<Vec<I>, Vec<N>, N, I>
§Methods operating on owning sparse vectors
sourcepub fn empty(dim: usize) -> Self
pub fn empty(dim: usize) -> Self
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.
source§impl<N, I, IStorage, DStorage> CsVecBase<IStorage, DStorage, N, I>
impl<N, I, IStorage, DStorage> CsVecBase<IStorage, DStorage, N, I>
§Common methods of sparse vectors
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<(), StructureError>
pub fn check_structure(&self) -> Result<(), StructureError>
Check the sparse structure, namely that:
- indices are sorted
- all indices are less than
dims()
sourcepub fn to_other_types<I2>(&self) -> CsVecI<N, I2>
pub fn to_other_types<I2>(&self) -> CsVecI<N, I2>
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<Iptr: SpIndex>(
&self,
) -> CsMatBase<N, I, Array2<Iptr>, &'_ [I], &'_ [N], Iptr>
pub fn row_view<Iptr: SpIndex>( &self, ) -> CsMatBase<N, I, Array2<Iptr>, &'_ [I], &'_ [N], Iptr>
View this vector as a matrix with only one row.
sourcepub fn col_view<Iptr: SpIndex>(
&self,
) -> CsMatBase<N, I, Array2<Iptr>, &'_ [I], &'_ [N], Iptr>
pub fn col_view<Iptr: SpIndex>( &self, ) -> CsMatBase<N, I, Array2<Iptr>, &'_ [I], &'_ [N], Iptr>
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>(&'b self, rhs: T) -> Nwhere
N: 'b + MulAcc + Zero,
I: 'b,
T: IntoSparseVecIter<'b, N> + Copy,
<T as IntoSparseVecIter<'b, N>>::IterType: Iterator<Item = (usize, &'b N)>,
pub fn dot<'b, T>(&'b self, rhs: T) -> Nwhere
N: 'b + MulAcc + Zero,
I: 'b,
T: IntoSparseVecIter<'b, N> + Copy,
<T as IntoSparseVecIter<'b, N>>::IterType: Iterator<Item = (usize, &'b N)>,
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_acc<'b, T, M, Acc>(&'b self, rhs: T) -> Accwhere
Acc: 'b + MulAcc<N, M> + Zero,
M: 'b,
T: IntoSparseVecIter<'b, M> + Copy,
<T as IntoSparseVecIter<'b, M>>::IterType: Iterator<Item = (usize, &'b M)>,
pub fn dot_acc<'b, T, M, Acc>(&'b self, rhs: T) -> Accwhere
Acc: 'b + MulAcc<N, M> + Zero,
M: 'b,
T: IntoSparseVecIter<'b, M> + Copy,
<T as IntoSparseVecIter<'b, M>>::IterType: Iterator<Item = (usize, &'b M)>,
Sparse vector dot product into accumulator.
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).
The output type can be any type supporting MulAcc
.
sourcepub fn dot_dense<V>(&self, rhs: V) -> N
pub fn dot_dense<V>(&self, rhs: V) -> N
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.
sourcepub fn squared_l2_norm(&self) -> N
pub fn squared_l2_norm(&self) -> N
Compute the squared L2-norm.
sourcepub fn norm(&self, p: N) -> N
pub fn norm(&self, p: N) -> N
Compute the vector norm for the given order p.
The norm for vector v is defined as:
- If p = ∞: maxᵢ |vᵢ|
- If p = -∞: minᵢ |vᵢ|
- If p = 0: ∑ᵢ[vᵢ≠0]
- Otherwise: ᵖ√(∑ᵢ|vᵢ|ᵖ)
source§impl<'a, N, I, IStorage, DStorage> CsVecBase<IStorage, DStorage, N, I>
impl<'a, N, I, IStorage, DStorage> CsVecBase<IStorage, DStorage, N, I>
§Methods on sparse vectors with mutable access to their data
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)
pub fn map_inplace<F>(&mut self, f: F)
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.
Trait Implementations§
source§impl<N, I, IS1, DS1, IS2, DS2> AbsDiffEq<CsVecBase<IS2, DS2, N, I>> for CsVecBase<IS1, DS1, N, I>
impl<N, I, IS1, DS1, IS2, DS2> AbsDiffEq<CsVecBase<IS2, DS2, N, I>> for CsVecBase<IS1, DS1, N, I>
source§fn default_epsilon() -> N::Epsilon
fn default_epsilon() -> N::Epsilon
source§fn abs_diff_eq(
&self,
other: &CsVecBase<IS2, DS2, N, I>,
epsilon: N::Epsilon,
) -> bool
fn abs_diff_eq( &self, other: &CsVecBase<IS2, DS2, N, I>, epsilon: N::Epsilon, ) -> bool
source§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq
.source§impl<'a, 'b, Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Add<&'b CsVecBase<IS2, DS2, Rhs, I>> for &'a CsVecBase<IS1, DS1, Lhs, I>
impl<'a, 'b, Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Add<&'b CsVecBase<IS2, DS2, Rhs, I>> for &'a CsVecBase<IS1, DS1, Lhs, I>
source§impl<'a, Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Add<&'a CsVecBase<IS2, DS2, Rhs, I>> for CsVecBase<IS1, DS1, Lhs, I>
impl<'a, Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Add<&'a CsVecBase<IS2, DS2, Rhs, I>> for CsVecBase<IS1, DS1, Lhs, I>
source§impl<'a, Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Add<CsVecBase<IS2, DS2, Rhs, I>> for &'a CsVecBase<IS1, DS1, Lhs, I>
impl<'a, Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Add<CsVecBase<IS2, DS2, Rhs, I>> for &'a CsVecBase<IS1, DS1, Lhs, I>
source§impl<Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Add<CsVecBase<IS2, DS2, Rhs, I>> for CsVecBase<IS1, DS1, Lhs, I>
impl<Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Add<CsVecBase<IS2, DS2, Rhs, I>> for CsVecBase<IS1, DS1, Lhs, I>
source§impl<IStorage, DStorage, N: Clone, I: Clone + SpIndex> Clone for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N: Clone, I: Clone + SpIndex> Clone for CsVecBase<IStorage, DStorage, N, I>
source§impl<IStorage, DStorage, N: Debug, I: Debug + SpIndex> Debug for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N: Debug, I: Debug + SpIndex> Debug for CsVecBase<IStorage, DStorage, N, I>
source§impl<'de, IStorage, DStorage, N, I: SpIndex> Deserialize<'de> for CsVecBase<IStorage, DStorage, N, I>where
IStorage: Deref<Target = [I]> + Deserialize<'de>,
DStorage: Deref<Target = [N]> + Deserialize<'de>,
impl<'de, IStorage, DStorage, N, I: SpIndex> Deserialize<'de> for CsVecBase<IStorage, DStorage, N, I>where
IStorage: Deref<Target = [I]> + Deserialize<'de>,
DStorage: Deref<Target = [N]> + Deserialize<'de>,
source§fn 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>,
source§impl<N, I, IStorage, DStorage> DivAssign<N> for CsVecBase<IStorage, DStorage, N, I>
impl<N, I, IStorage, DStorage> DivAssign<N> for CsVecBase<IStorage, DStorage, N, I>
source§fn div_assign(&mut self, rhs: N)
fn div_assign(&mut self, rhs: N)
/=
operation. Read moresource§impl<IStorage, DStorage, N: Hash, I: Hash + SpIndex> Hash for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N: Hash, I: Hash + SpIndex> Hash for CsVecBase<IStorage, DStorage, N, I>
source§impl<'a, N: 'a, I, IS, DS> IntoSparseVecIter<'a, N> for &'a CsVecBase<IS, DS, N, I>
impl<'a, N: 'a, I, IS, DS> IntoSparseVecIter<'a, N> for &'a CsVecBase<IS, DS, N, I>
type IterType = VectorIterator<'a, N, I>
source§fn into_sparse_vec_iter(self) -> VectorIterator<'a, N, I> ⓘ
fn into_sparse_vec_iter(self) -> VectorIterator<'a, N, I> ⓘ
source§impl<'a, 'b, N, I, Iptr, IS1, DS1, IpS2, IS2, DS2> Mul<&'b CsMatBase<N, I, IpS2, IS2, DS2, Iptr>> for &'a CsVecBase<IS1, DS1, N, I>
impl<'a, 'b, N, I, Iptr, IS1, DS1, IpS2, IS2, DS2> Mul<&'b CsMatBase<N, I, IpS2, IS2, DS2, Iptr>> for &'a CsVecBase<IS1, DS1, N, I>
source§impl<'a, 'b, N, I, Iptr, IpS1, IS1, DS1, IS2, DS2> Mul<&'b CsVecBase<IS2, DS2, N, I>> for &'a CsMatBase<N, I, IpS1, IS1, DS1, Iptr>
impl<'a, 'b, N, I, Iptr, IpS1, IS1, DS1, IS2, DS2> Mul<&'b CsVecBase<IS2, DS2, N, I>> for &'a CsMatBase<N, I, IpS1, IS1, DS1, Iptr>
source§impl<N, I, IStorage, DStorage> MulAssign<N> for CsVecBase<IStorage, DStorage, N, I>
impl<N, I, IStorage, DStorage> MulAssign<N> for CsVecBase<IStorage, DStorage, N, I>
source§fn mul_assign(&mut self, rhs: N)
fn mul_assign(&mut self, rhs: N)
*=
operation. Read moresource§impl<IStorage, DStorage, N: PartialEq, I: PartialEq + SpIndex> PartialEq for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N: PartialEq, I: PartialEq + SpIndex> PartialEq for CsVecBase<IStorage, DStorage, N, I>
source§impl<N, I, IS1, DS1, IS2, DS2> RelativeEq<CsVecBase<IS2, DS2, N, I>> for CsVecBase<IS1, DS1, N, I>
impl<N, I, IS1, DS1, IS2, DS2> RelativeEq<CsVecBase<IS2, DS2, N, I>> for CsVecBase<IS1, DS1, N, I>
source§fn default_max_relative() -> N::Epsilon
fn default_max_relative() -> N::Epsilon
source§fn relative_eq(
&self,
other: &CsVecBase<IS2, DS2, N, I>,
epsilon: N::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_eq( &self, other: &CsVecBase<IS2, DS2, N, I>, epsilon: N::Epsilon, max_relative: Self::Epsilon, ) -> bool
source§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
RelativeEq::relative_eq
.source§impl<'a, 'b, Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Sub<&'b CsVecBase<IS2, DS2, Rhs, I>> for &'a CsVecBase<IS1, DS1, Lhs, I>
impl<'a, 'b, Lhs, Rhs, Res, I, IS1, DS1, IS2, DS2> Sub<&'b CsVecBase<IS2, DS2, Rhs, I>> for &'a CsVecBase<IS1, DS1, Lhs, I>
source§impl<N, I, IS1, DS1, IS2, DS2> UlpsEq<CsVecBase<IS2, DS2, N, I>> for CsVecBase<IS1, DS1, N, I>
impl<N, I, IS1, DS1, IS2, DS2> UlpsEq<CsVecBase<IS2, DS2, N, I>> for CsVecBase<IS1, DS1, N, I>
source§fn default_max_ulps() -> u32
fn default_max_ulps() -> u32
source§impl<N, I: SpIndex, IS: Deref<Target = [I]>, DS: Deref<Target = [N]>> VecDim<N> for CsVecBase<IS, DS, N, I>
impl<N, I: SpIndex, IS: Deref<Target = [I]>, DS: Deref<Target = [N]>> VecDim<N> for CsVecBase<IS, DS, N, I>
impl<IStorage, DStorage, N: Copy, I: Copy + SpIndex> Copy for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N: Eq, I: Eq + SpIndex> Eq for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N, I: SpIndex> StructuralPartialEq for CsVecBase<IStorage, DStorage, N, I>
Auto Trait Implementations§
impl<IStorage, DStorage, N, I> Freeze for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N, I> RefUnwindSafe for CsVecBase<IStorage, DStorage, N, I>where
IStorage: RefUnwindSafe,
DStorage: RefUnwindSafe,
impl<IStorage, DStorage, N, I> Send for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N, I> Sync for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N, I> Unpin for CsVecBase<IStorage, DStorage, N, I>
impl<IStorage, DStorage, N, I> UnwindSafe for CsVecBase<IStorage, DStorage, N, I>where
IStorage: UnwindSafe,
DStorage: 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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§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.