List of all members | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
utl::BinomialCoefficients Class Reference

Calculates binomial coefficients and caches the results. More...

#include "utl/BinomialCoefficients.h"

Inheritance diagram for utl::BinomialCoefficients:
Inheritance graph
[legend]

Public Member Functions

 BinomialCoefficients (const int initialN=3)
 
double operator() (const int n, const int k)
 

Public Attributes

static BinomialCoefficients &return instance
 

Private Member Functions

void Generate (const int n)
 
double & GetUnchecked (const int n, const int k)
 

Private Attributes

std::vector< double > fCoefficients
 
int fMaxN
 

Detailed Description

Calculates binomial coefficients and caches the results.

Call to the class' operator(n, k) is equivalent to binomial coefficient ${n \choose k}$.

Due to rounding errors for the explicit formula with factorials (division of large numbers) this generator is using the Pascal triangle relations instead, i.e.

\[ {n \choose k} = {n-1 \choose k-1} + {n-1 \choose k}, \]

where only addition of moderate sized numbers can be maintained even for fairly large n.

Usage:

BinomialCoefficients& binomial = BinomialCoefficients::GetInstance();
const double b_4_2 = binomial(4, 2);
Author
Darko Veberic
Date
05 Sep 2009

Definition at line 38 of file BinomialCoefficients.h.

Constructor & Destructor Documentation

utl::BinomialCoefficients::BinomialCoefficients ( const int  initialN = 3)
inline

Definition at line 40 of file BinomialCoefficients.h.

References Generate().

Member Function Documentation

void BinomialCoefficients::Generate ( const int  n)
private

Definition at line 8 of file BinomialCoefficients.cc.

References fCoefficients, fMaxN, GetUnchecked(), and WARNING.

Referenced by BinomialCoefficients(), and operator()().

double& utl::BinomialCoefficients::GetUnchecked ( const int  n,
const int  k 
)
inlineprivate

Definition at line 54 of file BinomialCoefficients.h.

References fCoefficients.

Referenced by Generate(), and operator()().

double utl::BinomialCoefficients::operator() ( const int  n,
const int  k 
)
inline

Definition at line 44 of file BinomialCoefficients.h.

References fMaxN, Generate(), and GetUnchecked().

Member Data Documentation

std::vector<double> utl::BinomialCoefficients::fCoefficients
private

Definition at line 60 of file BinomialCoefficients.h.

Referenced by Generate(), and GetUnchecked().

int utl::BinomialCoefficients::fMaxN
private

Definition at line 59 of file BinomialCoefficients.h.

Referenced by Generate(), and operator()().

BinomialCoefficients & return utl::Singleton< BinomialCoefficients >::instance
inherited

Definition at line 44 of file Singleton.h.


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

, generated on Tue Sep 26 2023.