pub struct SparseVec<T: Real = f64> { /* private fields */ }Expand description
Work vector with a dense value array and an optional nonzero pattern.
See the module documentation for the representation and invariants.
Implementations§
Source§impl<T: Real> SparseVec<T>
impl<T: Real> SparseVec<T>
Sourcepub fn from_dense(values: &[T]) -> Self
pub fn from_dense(values: &[T]) -> Self
A vector equal to values, in sparse mode with the exact support as
pattern (in increasing order).
Sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Number of positions that may be nonzero: |P| in sparse mode, size
in dense mode.
Sourcepub fn pattern(&self) -> Option<&[u32]>
pub fn pattern(&self) -> Option<&[u32]>
The pattern P in sparse mode (in insertion order), None in dense
mode.
Sourcepub fn values_mut(&mut self) -> &mut [T]
pub fn values_mut(&mut self) -> &mut [T]
Mutable access to the dense value array. Since arbitrary writes
invalidate the pattern, this switches the vector to dense mode; call
SparseVec::rebuild_pattern afterwards to return to sparse mode.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Sets y ← 0 and returns to sparse mode with an empty pattern.
Costs O(count) when count ≤ 0.3·size, O(size) otherwise.
Sourcepub fn scatter(&mut self, alpha: T, idx: &[u32], val: &[T])
pub fn scatter(&mut self, alpha: T, idx: &[u32], val: &[T])
Scatter: y ← y + α Σ_k ν_k e_{ι_k} for the packed vector
(idx, val). Repeated indices accumulate.
§Panics
If the slices differ in length or an index is ≥ size.
Sourcepub fn axpy(&mut self, alpha: T, x: &SparseVec<T>)
pub fn axpy(&mut self, alpha: T, x: &SparseVec<T>)
y ← y + α x. Uses the pattern of x when it has one, and a dense
sweep of x otherwise; both paths update exactly the positions with
x_i ≠ 0, so the results are identical. The pattern of y is kept
if y is in sparse mode.
§Panics
If the sizes differ.
Sourcepub fn gather(&self, idx: &mut Vec<u32>, val: &mut Vec<T>)
pub fn gather(&self, idx: &mut Vec<u32>, val: &mut Vec<T>)
Gather: writes the nonzero entries as a packed vector into idx and
val (both cleared first), in pattern order in sparse mode and in
increasing index order in dense mode.
Sourcepub fn tidy(&mut self, drop_tol: T)
pub fn tidy(&mut self, drop_tol: T)
Drops negligible entries: every y_i with |y_i| ≤ drop_tol is set
to exactly zero and, in sparse mode, removed from the pattern (the
solver passes Tolerances::zero).
Sourcepub fn iter(&self) -> impl Iterator<Item = (u32, T)> + '_
pub fn iter(&self) -> impl Iterator<Item = (u32, T)> + '_
The entries (i, y_i) of the pattern in sparse mode (in insertion
order, explicit zeros included), of every position in dense mode.
Sourcepub fn make_dense(&mut self)
pub fn make_dense(&mut self)
Switches to dense mode, discarding the pattern (O(count)).
Sourcepub fn rebuild_pattern(&mut self)
pub fn rebuild_pattern(&mut self)
Switches to sparse mode with the pattern set to the exact support of
y, in increasing order (O(size)).
Sourcepub fn check_invariants(&self) -> Result<(), SparseError>
pub fn check_invariants(&self) -> Result<(), SparseError>
Checks invariants (V1)–(V3).