pub struct NeumaierSum<ScalarType> { /* private fields */ }
Expand description
Neumaier compensated sum of an iterable object of floating-point (real or scalar) numbers.
When summing floating-point values in a vector, the standard method of adding values sequentially can lead to precision loss, especially when there are a large number of elements or a mix of very large and very small values. This happens because floating-point arithmetic is not associative due to rounding errors. The most accurate algorithm to sum floating-point numbers typically avoids these precision problems.
Kahan summation is a popular algorithm that reduces numerical errors when adding a sequence of floating-point numbers. It keeps track of a running compensation for lost low-order bits during summation.
Neumaier summation is a slight modification of Kahan summation that can handle larger errors better. It uses an extra step to correct for the compensation term if the summation results in a larger round-off error than Kahan’s method can correct.
§Example
use num_valid::neumaier_compensated_sum::NeumaierSum;
let mut neumaier = NeumaierSum::<f64>::zero();
neumaier.add(1.0);
neumaier.add(1e100);
neumaier.add(1.0);
neumaier.add(-1e100);
let sum = neumaier.sum();
println!("Sum: {}", sum);
assert_eq!(sum, 2.0);
§References
Implementations§
Source§impl<ScalarType> NeumaierSum<ScalarType>
impl<ScalarType> NeumaierSum<ScalarType>
Sourcepub fn sum_before_compensation(&self) -> &ScalarType
pub fn sum_before_compensation(&self) -> &ScalarType
The sum before the compensation term is added.
Sourcepub fn compensation(&self) -> &ScalarType
pub fn compensation(&self) -> &ScalarType
The compensation term.
This is the correction term that is added to the sum_before_compensation
to correct for the loss of precision.
Source§impl<ScalarType> NeumaierSum<ScalarType>where
ScalarType: Clone + Zero + for<'a> Add<&'a ScalarType, Output = ScalarType> + NeumaierAddable,
impl<ScalarType> NeumaierSum<ScalarType>where
ScalarType: Clone + Zero + for<'a> Add<&'a ScalarType, Output = ScalarType> + NeumaierAddable,
Sourcepub fn new(value: ScalarType) -> Self
pub fn new(value: ScalarType) -> Self
Creates a new instance of the Neumaier compensated sum, initializing the
sum with the provided value
.
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates a new instance of the Neumaier compensated sum, initialized to zero.
Sourcepub fn add(&mut self, value: ScalarType)
pub fn add(&mut self, value: ScalarType)
Adds a value
to the sum. This method should be called for each value to be summed.
Sourcepub fn new_sequential<I>(values: I) -> Selfwhere
I: IntoIterator<Item = ScalarType>,
pub fn new_sequential<I>(values: I) -> Selfwhere
I: IntoIterator<Item = ScalarType>,
Creates a new instance of the NeumaierSum
object summing the values in the iterable object values
(sequential_version).
§Example
use num_valid::neumaier_compensated_sum::NeumaierSum;
let values = vec![1.0, 1.0e100, 1.0, -1.0e100];
let neumaier = NeumaierSum::new_sequential(values);
let sum = neumaier.sum();
println!("Sum: {}", sum);
assert_eq!(sum, 2.0);
Trait Implementations§
Source§impl<ScalarType: Clone> Clone for NeumaierSum<ScalarType>
impl<ScalarType: Clone> Clone for NeumaierSum<ScalarType>
Source§fn clone(&self) -> NeumaierSum<ScalarType>
fn clone(&self) -> NeumaierSum<ScalarType>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more