#include <genesis/utils/math/compensated_sum.hpp>
Compensated summation algorithmm, such as Kahan, Neumaier, and Klein summation.
We implement the basic Kahan summation here, taking care of overly aggressive compiler optimizations, as well as several extensions of the algorithm, namely the Kahan Babushka Neumaier summation and the Kahan Babushka Klein summation. See https://en.wikipedia.org/wiki/Kahan_summation_algorithm
We use tag dispatch via the tempalte parameter to select the summation algorithm at compile time. Alternatively, we offer typedefs for all three of them, for simplicity. The three currently implemented algorithms can be selected as follows:
CompensatedSum<KahanSummation>
, or KahanSumCompensatedSum<NeumaierSummation>
, or NeumaierSumCompensatedSum<KleinSummation>
, KleinSumThen, use add(), or simply operator +=
and operator -=
to add values to the summation, and use get() or implicit conversion via operator double
to obtain the compensated sum.
By default, we use the Neumaier summation, which offers a compromise between accuracy and speed.
Definition at line 49 of file compensated_sum.hpp.
Public Member Functions | |
CompensatedSum ()=default | |
CompensatedSum (CompensatedSum &&)=default | |
CompensatedSum (CompensatedSum const &)=default | |
CompensatedSum (double value) | |
Constructor that initializes the sum to a given value . More... | |
template<class It > | |
CompensatedSum (It first, It last) | |
Construct a CompensatedSum, summing over a range of double . More... | |
CompensatedSum (std::initializer_list< double > list) | |
Construct a CompensatedSum, summing over an initializer list of values. More... | |
~CompensatedSum ()=default | |
void | add (double value) |
double | get () const |
operator double () const | |
Conversion to double . Return the current compensated sum. More... | |
void | operator+= (double value) |
Add a value to the sum. More... | |
void | operator-= (double value) |
Subtract a value from the sum. More... | |
CompensatedSum & | operator= (CompensatedSum &&)=default |
CompensatedSum & | operator= (CompensatedSum const &)=default |
CompensatedSum & | operator= (double value) |
Set the sum to the given value . More... | |
void | reset () |
|
default |
|
inline |
Constructor that initializes the sum to a given value
.
Definition at line 120 of file compensated_sum.hpp.
|
inline |
Construct a CompensatedSum, summing over a range of double
.
The given iterator pair first
to last
needs to dereference to values that are convertible to double
. Their sum is computed.
Definition at line 131 of file compensated_sum.hpp.
|
inline |
Construct a CompensatedSum, summing over an initializer list of values.
Definition at line 142 of file compensated_sum.hpp.
|
default |
|
default |
|
default |
|
inline |
Definition at line 208 of file compensated_sum.hpp.
|
inline |
Definition at line 221 of file compensated_sum.hpp.
|
inline |
Conversion to double
. Return the current compensated sum.
Definition at line 197 of file compensated_sum.hpp.
|
inline |
Add a value
to the sum.
Definition at line 164 of file compensated_sum.hpp.
|
inline |
Subtract a value
from the sum.
This is identical to addting the negative of the value
.
Definition at line 174 of file compensated_sum.hpp.
|
default |
|
default |
|
inline |
Set the sum to the given value
.
This will also reset the correction term, as we assume that assining a new value is meant to start a new summation.
Definition at line 185 of file compensated_sum.hpp.
|
inline |
Definition at line 213 of file compensated_sum.hpp.