BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ROOT::Math::KahanSum< T > Class Template Reference

Description

template<class T>
class ROOT::Math::KahanSum< T >

The Kahan compensate summation algorithm significantly reduces the numerical error in the total obtained by adding a sequence of finite precision floating point numbers. This is done by keeping a separate running compensation (a variable to accumulate small errors).
.

The intial values of the result and the correction are set to the default value of the type it hass been instantiated with.

Examples:

std::vector<double> numbers = {0.01, 0.001, 0.0001, 0.000001, 0.00000000001};
k.Add(numbers);
The Kahan compensate summation algorithm significantly reduces the numerical error in the total obtai...
Definition: Util.h:85
void Add(const T &x)
Single element accumulated addition.
Definition: Util.h:91
static T Accumulate(const Iterator begin, const Iterator end, const T &initialValue=T{})
Iterate over a datastructure referenced by a pointer and return the result of its accumulation....
Definition: Util.h:111

Definition at line 85 of file Util.h.

Collaboration diagram for ROOT::Math::KahanSum< T >:
[legend]

Public Member Functions

 KahanSum (const T &initialValue=T{})
 Constructor accepting a initial value for the summation as parameter. More...
 
template<class Iterator >
void Add (const Iterator begin, const Iterator end)
 Iterate over a datastructure referenced by a pointer and accumulate on the exising result. More...
 
void Add (const T &x)
 Single element accumulated addition. More...
 
Result ()
 Return the result. More...
 

Static Public Member Functions

template<class Iterator >
static T Accumulate (const Iterator begin, const Iterator end, const T &initialValue=T{})
 Iterate over a datastructure referenced by a pointer and return the result of its accumulation. Can take an initial value as third parameter. More...
 

Private Attributes

fCorrection {}
 
fSum {}
 

Constructor & Destructor Documentation

◆ KahanSum()

template<class T >
ROOT::Math::KahanSum< T >::KahanSum ( const T &  initialValue = T{})
inline

Constructor accepting a initial value for the summation as parameter.

Definition at line 88 of file Util.h.

88 {}) : fSum(initialValue) {}

Member Function Documentation

◆ Accumulate()

template<class T >
template<class Iterator >
static T ROOT::Math::KahanSum< T >::Accumulate ( const Iterator  begin,
const Iterator  end,
const T &  initialValue = T{} 
)
inlinestatic

Iterate over a datastructure referenced by a pointer and return the result of its accumulation. Can take an initial value as third parameter.

Definition at line 111 of file Util.h.

111  {})
112  {
113  static_assert(!std::is_same<decltype(*begin), T>::value,
114  "Iterator points to an element of the different type than the KahanSum class");
115  KahanSum init(initialValue);
116  init.Add(begin, end);
117  return init.fSum;
118  }
KahanSum(const T &initialValue=T{})
Constructor accepting a initial value for the summation as parameter.
Definition: Util.h:88

◆ Add() [1/2]

template<class T >
template<class Iterator >
void ROOT::Math::KahanSum< T >::Add ( const Iterator  begin,
const Iterator  end 
)
inline

Iterate over a datastructure referenced by a pointer and accumulate on the exising result.

Definition at line 101 of file Util.h.

102  {
103  static_assert(!std::is_same<decltype(*begin), T>::value,
104  "Iterator points to an element of the different type than the KahanSum class");
105  for (auto it = begin; it != end; it++) this->Add(*it);
106  }

References ROOT::Math::KahanSum< T >::Add().

Here is the call graph for this function:

◆ Add() [2/2]

template<class T >
void ROOT::Math::KahanSum< T >::Add ( const T &  x)
inline

Single element accumulated addition.

Definition at line 91 of file Util.h.

92  {
93  auto y = x - fCorrection;
94  auto t = fSum + y;
95  fCorrection = (t - fSum) - y;
96  fSum = t;
97  }

References ROOT::Math::KahanSum< T >::fCorrection, and ROOT::Math::KahanSum< T >::fSum.

Referenced by ROOT::Math::KahanSum< T >::Add().

◆ Result()

template<class T >
T ROOT::Math::KahanSum< T >::Result ( )
inline

Return the result.

Definition at line 121 of file Util.h.

121 { return fSum; }

References ROOT::Math::KahanSum< T >::fSum.

Member Data Documentation

◆ fCorrection

template<class T >
T ROOT::Math::KahanSum< T >::fCorrection {}
private

Definition at line 125 of file Util.h.

Referenced by ROOT::Math::KahanSum< T >::Add().

◆ fSum

template<class T >
T ROOT::Math::KahanSum< T >::fSum {}
private

Definition at line 124 of file Util.h.

Referenced by ROOT::Math::KahanSum< T >::Add(), and ROOT::Math::KahanSum< T >::Result().


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