Skip to main content

SparseVec

Struct SparseVec 

Source
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>

Source

pub fn new(size: usize) -> Self

The zero vector of length size, in sparse mode with an empty pattern.

§Panics

If size > u32::MAX.

Source

pub fn from_dense(values: &[T]) -> Self

A vector equal to values, in sparse mode with the exact support as pattern (in increasing order).

Source

pub fn size(&self) -> usize

Length of the vector.

Source

pub fn count(&self) -> usize

Number of positions that may be nonzero: |P| in sparse mode, size in dense mode.

Source

pub fn density(&self) -> f64

Fraction count / size (zero for an empty vector).

Source

pub fn is_sparse(&self) -> bool

true iff the vector is in sparse mode (it keeps a pattern).

Source

pub fn pattern(&self) -> Option<&[u32]>

The pattern P in sparse mode (in insertion order), None in dense mode.

Source

pub fn values(&self) -> &[T]

The dense value array.

Source

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.

Source

pub fn get(&self, i: u32) -> T

Entry y_i.

Source

pub fn set(&mut self, i: u32, v: T)

Sets y_i ← v.

Source

pub fn add(&mut self, i: u32, delta: T)

Algorithm S: y_i ← y_i + δ.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn dot(&self, z: &[T]) -> T

Inner product yᵀ z with a dense vector z.

§Panics

If z.len() != size.

Source

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

Source

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.

Source

pub fn make_dense(&mut self)

Switches to dense mode, discarding the pattern (O(count)).

Source

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

Source

pub fn check_invariants(&self) -> Result<(), SparseError>

Checks invariants (V1)–(V3).

Trait Implementations§

Source§

impl<T: Clone + Real> Clone for SparseVec<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Real> Debug for SparseVec<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Forget for SparseVec<T>

§

impl<T> Freeze for SparseVec<T>
where Vec<T>: Freeze,

§

impl<T> RefUnwindSafe for SparseVec<T>
where Vec<T>: RefUnwindSafe,

§

impl<T> Send for SparseVec<T>
where Vec<T>: Send,

§

impl<T> Sync for SparseVec<T>
where Vec<T>: Sync,

§

impl<T> Unpin for SparseVec<T>
where Vec<T>: Unpin,

§

impl<T> UnsafeUnpin for SparseVec<T>
where Vec<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for SparseVec<T>
where Vec<T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.