ITK  5.4.0
Insight Toolkit
Public Types | Public Member Functions | Private Attributes | List of all members
itk::CompensatedSummation< TFloat > Class Template Reference

#include <itkCompensatedSummation.h>

Detailed Description

template<typename TFloat>
class itk::CompensatedSummation< TFloat >

Perform more precise accumulation of floating point numbers.

The float and double datatypes only have finite precision. When performing a running sum of floats, the accumulated errors get progressively worse as the magnitude of the sum gets large relative to new elements.

From Wikipedia, https://en.wikipedia.org/wiki/Kahan_summation_algorithm

"In numerical analysis, the Kahan summation algorithm (also known as compensated summation) significantly reduces the numerical error in the total obtained by adding a sequence of finite precision floating point numbers, compared to the obvious approach. This is done by keeping a separate running compensation (a variable to accumulate small errors)."

For example, instead of

double sum = 0.0;
for( unsigned int i = 0; i < array.Size(); ++i )
{
sum += array.GetElement(i);
}

do

using CompensatedSummationType = CompensatedSummation<double>;
CompensatedSummationType compensatedSummation;
for( unsigned int i = 0; i < array.Size(); ++i )
{
compensatedSummation += array.GetElement(i);
}
double sum = compensatedSummation.GetSum();

Definition at line 66 of file itkCompensatedSummation.h.

+ Collaboration diagram for itk::CompensatedSummation< TFloat >:

Public Types

using AccumulateType = typename NumericTraits< FloatType >::AccumulateType
 
using FloatType = TFloat
 
using Self = CompensatedSummation
 

Public Member Functions

 CompensatedSummation (const Self &rhs)
 
const AccumulateTypeGetSum () const
 
 operator FloatType () const
 
Selfoperator-= (const FloatType &rhs)
 
Selfoperator= (const FloatType &rhs)
 
Selfoperator= (const Self &rhs)
 
void ResetToZero ()
 
 CompensatedSummation ()=default
 
 CompensatedSummation (FloatType value)
 
void AddElement (const FloatType &element)
 
Selfoperator+= (const FloatType &rhs)
 
Selfoperator+= (const Self &rhs)
 
Selfoperator*= (const FloatType &rhs)
 
Selfoperator/= (const FloatType &rhs)
 

Private Attributes

AccumulateType m_Compensation {}
 
AccumulateType m_Sum {}
 

Member Typedef Documentation

◆ AccumulateType

template<typename TFloat>
using itk::CompensatedSummation< TFloat >::AccumulateType = typename NumericTraits<FloatType>::AccumulateType

Type used for the sum and compensation.

Definition at line 73 of file itkCompensatedSummation.h.

◆ FloatType

template<typename TFloat>
using itk::CompensatedSummation< TFloat >::FloatType = TFloat

Type of the input elements.

Definition at line 70 of file itkCompensatedSummation.h.

◆ Self

template<typename TFloat>
using itk::CompensatedSummation< TFloat >::Self = CompensatedSummation

Standard class type aliases.

Definition at line 76 of file itkCompensatedSummation.h.

Constructor & Destructor Documentation

◆ CompensatedSummation() [1/3]

template<typename TFloat>
itk::CompensatedSummation< TFloat >::CompensatedSummation ( )
default

Constructors.

◆ CompensatedSummation() [2/3]

template<typename TFloat>
itk::CompensatedSummation< TFloat >::CompensatedSummation ( FloatType  value)

Constructors.

◆ CompensatedSummation() [3/3]

template<typename TFloat>
itk::CompensatedSummation< TFloat >::CompensatedSummation ( const Self rhs)

Copy constructor.

Member Function Documentation

◆ AddElement()

template<typename TFloat>
void itk::CompensatedSummation< TFloat >::AddElement ( const FloatType element)

Add an element to the sum.

◆ GetSum()

template<typename TFloat>
const AccumulateType& itk::CompensatedSummation< TFloat >::GetSum ( ) const

Get the sum.

◆ operator FloatType()

template<typename TFloat>
itk::CompensatedSummation< TFloat >::operator FloatType ( ) const
explicit

explicit conversion

◆ operator*=()

template<typename TFloat>
Self& itk::CompensatedSummation< TFloat >::operator*= ( const FloatType rhs)

Division and multiplication. These do not provide any numerical advantages relative to vanilla division and multiplication.

◆ operator+=() [1/2]

template<typename TFloat>
Self& itk::CompensatedSummation< TFloat >::operator+= ( const FloatType rhs)

Add an element to the sum.

◆ operator+=() [2/2]

template<typename TFloat>
Self& itk::CompensatedSummation< TFloat >::operator+= ( const Self rhs)

Add an element to the sum.

◆ operator-=()

template<typename TFloat>
Self& itk::CompensatedSummation< TFloat >::operator-= ( const FloatType rhs)

Subtract an element from the sum.

◆ operator/=()

template<typename TFloat>
Self& itk::CompensatedSummation< TFloat >::operator/= ( const FloatType rhs)

Division and multiplication. These do not provide any numerical advantages relative to vanilla division and multiplication.

◆ operator=() [1/2]

template<typename TFloat>
Self& itk::CompensatedSummation< TFloat >::operator= ( const FloatType rhs)

Reset the sum to the given value and the compensation to zero.

◆ operator=() [2/2]

template<typename TFloat>
Self& itk::CompensatedSummation< TFloat >::operator= ( const Self rhs)

Assignment operator.

◆ ResetToZero()

template<typename TFloat>
void itk::CompensatedSummation< TFloat >::ResetToZero ( )

Reset the sum and compensation to zero.

Member Data Documentation

◆ m_Compensation

template<typename TFloat>
AccumulateType itk::CompensatedSummation< TFloat >::m_Compensation {}
private

Definition at line 128 of file itkCompensatedSummation.h.

◆ m_Sum

template<typename TFloat>
AccumulateType itk::CompensatedSummation< TFloat >::m_Sum {}
private

Definition at line 127 of file itkCompensatedSummation.h.


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