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

#include <itkCompensatedSummation.h>

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

Detailed Description

template<class 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, http://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

typedef CompensatedSummation<double> CompensatedSummationType;
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.

Public Types

typedef NumericTraits
< FloatType >::AccumulateType 
AccumulateType
 
typedef TFloat FloatType
 
typedef CompensatedSummation Self
 

Public Member Functions

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

Private Member Functions

 typedef (itk::Concept::IsFloatingPoint< TFloat >) OnlyDefinedForFloatingPointTypes
 

Private Attributes

AccumulateType m_Compensation
 
AccumulateType m_Sum
 

Member Typedef Documentation

Type used for the sum and compensation.

Definition at line 73 of file itkCompensatedSummation.h.

template<class TFloat >
typedef TFloat itk::CompensatedSummation< TFloat >::FloatType

Type of the input elements.

Definition at line 70 of file itkCompensatedSummation.h.

template<class TFloat >
typedef CompensatedSummation itk::CompensatedSummation< TFloat >::Self

Standard class typedefs.

Definition at line 76 of file itkCompensatedSummation.h.

Constructor & Destructor Documentation

template<class TFloat >
itk::CompensatedSummation< TFloat >::CompensatedSummation ( )

Constructor.

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

Copy constructor.

Member Function Documentation

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

Add an element to the sum.

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

Get the sum.

template<class 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.

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

Add an element to the sum.

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

Subtract an element from the sum.

template<class 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.

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

Assignment operator.

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

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

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

Reset the sum and compensation to zero.

template<class TFloat >
itk::CompensatedSummation< TFloat >::typedef ( itk::Concept::IsFloatingPoint< TFloat >  )
private

This class requires OnlyDefinedForFloatingPointTypes in the form of ( itk::Concept::IsFloatingPoint< TFloat > )

Member Data Documentation

template<class TFloat >
AccumulateType itk::CompensatedSummation< TFloat >::m_Compensation
private

Definition at line 112 of file itkCompensatedSummation.h.

template<class TFloat >
AccumulateType itk::CompensatedSummation< TFloat >::m_Sum
private

Definition at line 111 of file itkCompensatedSummation.h.


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