A library for working with phylogenetic and population genetic data.
v0.32.0
CompensatedSum< SummationAlgorithm > Class Template Reference

#include <genesis/utils/math/compensated_sum.hpp>

Detailed Description

template<typename SummationAlgorithm = NeumaierSum>
class genesis::utils::CompensatedSum< SummationAlgorithm >

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:

Then, 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...
 
CompensatedSumoperator= (CompensatedSum &&)=default
 
CompensatedSumoperator= (CompensatedSum const &)=default
 
CompensatedSumoperator= (double value)
 Set the sum to the given value. More...
 
void reset ()
 

Constructor & Destructor Documentation

◆ CompensatedSum() [1/6]

CompensatedSum ( )
default

◆ CompensatedSum() [2/6]

CompensatedSum ( double  value)
inline

Constructor that initializes the sum to a given value.

Definition at line 120 of file compensated_sum.hpp.

◆ CompensatedSum() [3/6]

CompensatedSum ( It  first,
It  last 
)
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.

◆ CompensatedSum() [4/6]

CompensatedSum ( std::initializer_list< double >  list)
inline

Construct a CompensatedSum, summing over an initializer list of values.

Definition at line 142 of file compensated_sum.hpp.

◆ ~CompensatedSum()

~CompensatedSum ( )
default

◆ CompensatedSum() [5/6]

CompensatedSum ( CompensatedSum< SummationAlgorithm > const &  )
default

◆ CompensatedSum() [6/6]

CompensatedSum ( CompensatedSum< SummationAlgorithm > &&  )
default

Member Function Documentation

◆ add()

void add ( double  value)
inline

Definition at line 208 of file compensated_sum.hpp.

◆ get()

double get ( ) const
inline

Definition at line 221 of file compensated_sum.hpp.

◆ operator double()

operator double ( ) const
inline

Conversion to double. Return the current compensated sum.

Definition at line 197 of file compensated_sum.hpp.

◆ operator+=()

void operator+= ( double  value)
inline

Add a value to the sum.

Definition at line 164 of file compensated_sum.hpp.

◆ operator-=()

void operator-= ( double  value)
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.

◆ operator=() [1/3]

CompensatedSum& operator= ( CompensatedSum< SummationAlgorithm > &&  )
default

◆ operator=() [2/3]

CompensatedSum& operator= ( CompensatedSum< SummationAlgorithm > const &  )
default

◆ operator=() [3/3]

CompensatedSum& operator= ( double  value)
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.

◆ reset()

void reset ( )
inline

Definition at line 213 of file compensated_sum.hpp.


The documentation for this class was generated from the following file: