Struct sprs::vec::CsVecBase

source ·
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 Vecs 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

Create an owning CsVec from vector data.

Panics
  • if indices and data lengths differ
  • if the vector contains out of bounds indices

Create an empty CsVec, which can be used for incremental construction

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 of self.indices()
  • Panics if ind is greater than self.dim()

Reserve size additional non-zero values.

Reserve exactly exact_size non-zero values.

Clear the underlying storage

Get a view of this vector.

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);

The underlying indices.

The underlying non zero values.

Destruct the vector object and recycle its storage containers.

The dimension of this vector.

The non zero count of this vector.

Check the sparse structure, namely that:

  • indices is sorted
  • indices are lower than dims()

Allocate a new vector equal to this one.

Clone the vector with another integer type for its indices

Panics

If the indices cannot be represented by the requested integer type.

View this vector as a matrix with only one row.

View this vector as a matrix with only one column.

Access element at given index, with logarithmic complexity

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.

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));

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.

Fill a dense vector with our values

Transform this vector into a set of (index, value) tuples

Apply a function to each non-zero element, yielding a new matrix with the same sparsity structure.

Access element at given index, with logarithmic complexity

Apply a function to each non-zero element, mutating it

Mutable iteration over the non-zero values of a sparse vector

Only the values can be changed, the sparse structure is kept.

Create a borrowed CsVec over slice data.

Access element at given index, with logarithmic complexity

Re-borrowing version of at().

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.

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

The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
The dimension of the vector
Transform self into an iterator that yields (usize, &N) tuples where the usize is the index of the value in the sparse vector. The indices should be sorted. Read more
Indicator to check whether the vector is actually dense
Random access to an element in the vector. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Serialize this value into the given Serde serializer. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The dimension of the vector

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Checks if self is actually part of its subset T (and can be converted to it).
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
The inclusion map: converts self to the equivalent element of its superset.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.