Skip to main content

Accumulator

Trait Accumulator 

Source
pub trait Accumulator: Sized {
    type Input;
    type Output;

    // Required methods
    fn new() -> Self;
    fn push(&mut self, value: Self::Input);
    fn combine(&mut self, other: Self);
    fn result(self) -> Self::Output;

    // Provided methods
    fn new_sequential<I>(values: I) -> Self
       where I: IntoIterator<Item = Self::Input> { ... }
    fn new_parallel<I>(values: I) -> Self
       where Self: Send,
             I: IntoParallelIterator<Item = Self::Input> { ... }
}
Expand description

Base trait for incremental accumulation over a sequence of values.

An accumulator collects items one at a time via push and produces a final result via result. Partial accumulators can be merged with combine, enabling chunked and parallel reductions.

§Correctness requirement for combine

new() must be the identity element for combine: for any accumulator x, both combine(new(), x) and combine(x, new()) must produce an accumulator equivalent to x. This invariant is required by new_parallel.

Required Associated Types§

Source

type Input

The type of values pushed into the accumulator.

Source

type Output

The type of the final result produced by result.

Required Methods§

Source

fn new() -> Self

Creates a new, empty accumulator (the identity for combine).

Source

fn push(&mut self, value: Self::Input)

Adds a single value to the accumulator.

Source

fn combine(&mut self, other: Self)

Merges other into self.

After this call self represents the accumulation of all values pushed into either self or other. Used by new_parallel to merge per-chunk partial results.

Source

fn result(self) -> Self::Output

Consumes the accumulator and returns the final result.

Provided Methods§

Source

fn new_sequential<I>(values: I) -> Self
where I: IntoIterator<Item = Self::Input>,

Creates a new accumulator by sequentially adding all values from values.

This is the simplest way to build an accumulator from owned input data. For chunked or parallel reductions, prefer Accumulator::combine on partial accumulators.

§Example
use num_valid::algorithms::accumulators::*;

let values = vec![1.0, 1.0e100, 1.0, -1.0e100];

let neumaier = NeumaierSum::new_sequential(values);
let sum = neumaier.result();
println!("Sum: {}", sum);
assert_eq!(sum, 2.0);
Source

fn new_parallel<I>(values: I) -> Self
where Self: Send, I: IntoParallelIterator<Item = Self::Input>,

Builds an accumulator by processing a parallel iterator.

The iterator is partitioned into chunks by rayon; each chunk is folded into a partial accumulator with push, and the partial results are merged with combine.

Requires Self: Send.

Any additional Send constraints on iterator items are enforced by the concrete IntoParallelIterator implementation.

§Example
use num_valid::algorithms::accumulators::*;
use rayon::prelude::*;

let values = vec![1.0_f64, 2.0, 3.0, 4.0];
let sum = NaiveSum::new_parallel(values.into_par_iter()).result();
assert_eq!(sum, 10.0);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<RealType: RealScalar> Accumulator for MaxAccumulator<RealType>

Source§

type Input = RealType

Source§

type Output = RealType

Source§

impl<RealType: RealScalar> Accumulator for MinAccumulator<RealType>

Source§

type Input = RealType

Source§

type Output = RealType

Source§

impl<ScalarType: FpScalar> Accumulator for MaxAbsValueAccumulator<ScalarType>

Source§

type Input = ScalarType

Source§

type Output = NonNegativeRealScalar<<ScalarType as FpScalar>::RealType>

Source§

impl<ScalarType> Accumulator for NeumaierSum<ScalarType>
where ScalarType: Clone + Zero + for<'a> Add<&'a ScalarType, Output = ScalarType> + for<'a> AddAssign<&'a ScalarType> + for<'a> Mul<&'a ScalarType, Output = ScalarType> + for<'a> MulAssign<&'a ScalarType> + NeumaierAddable,

Source§

type Input = ScalarType

Source§

type Output = ScalarType

Source§

impl<T: FpScalar> Accumulator for NaiveSum<T>

Source§

type Input = T

Source§

type Output = T