BCMath Namespace Reference

Some useful mathematic functions. More...

Functions

double ApproxBinomial (int n, int k, double p)
double ApproxLogFact (double x)
double LogApproxBinomial (int n, int k, double p)
double LogBinomFactor (int n, int k)
double LogBreitWignerNonRel (double x, double mean, double Gamma, bool norm=false)
double LogBreitWignerRel (double x, double mean, double Gamma)
double LogChi2 (double x, int n)
double LogFact (int n)
double LogGaus (double x, double mean=0, double sigma=1, bool norm=false)
double LogNoverK (int n, int k)
double LogPoisson (double x, double par)
double LogVoigtian (double x, double sigma, double gamma)
double Max (double x, double y)
int Max (int x, int y)
double Min (double x, double y)
int Min (int x, int y)
int Nint (double x)
double rms (int n, const double *a)

Detailed Description

Some useful mathematic functions.

Author:
Daniel Kollar
Kevin Kröninger
Jing Liu
Version:
1.0
Date:
08.2008 A namespace which encapsulates some mathematical functions necessary for BAT.

Function Documentation

double BCMath::ApproxBinomial ( int  n,
int  k,
double  p 
)

Calculates Binomial probability using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function.

Definition at line 59 of file BCMath.cxx.

00060 {
00061    return exp( BCMath::LogApproxBinomial(n, k, p) );
00062 }

double BCMath::ApproxLogFact ( double  x  ) 

Calculates natural logarithm of the n-factorial (n!) using Srinivasa Ramanujan approximation log(n!) = n*log(n) - n + log(n*(1.+4.*n*(1.+2.*n)))/6. + log(PI)/2. if n > 20. If n <= 20 it uses BCMath::LogFact to calculate it exactly.

Definition at line 110 of file BCMath.cxx.

00111 {
00112    if(x>BCMATH_NFACT_ALIMIT)
00113       return x*log(x) - x + log(x*(1.+4.*x*(1.+2.*x)))/6. + log(M_PI)/2.;
00114 
00115    else
00116       return BCMath::LogFact((int)x);
00117 }

double BCMath::LogApproxBinomial ( int  n,
int  k,
double  p 
)

Calculates natural logarithm of the Binomial probability using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function.

Definition at line 66 of file BCMath.cxx.

00067 {
00068    // check p
00069    if (p == 0)
00070       return -1e99;
00071 
00072    else if (p == 1)
00073       return 0;
00074 
00075    // switch parameters if n < k
00076    if(n<k)
00077    {
00078       int a=n;
00079       n=k;
00080       k=a;
00081    }
00082 
00083    return BCMath::LogBinomFactor(n,k) + (double)k*log(p) + (double)(n-k)*log(1.-p);
00084 }

double BCMath::LogBinomFactor ( int  n,
int  k 
)

Calculates natural logarithm of the Binomial factor "n over k" using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function. Even for large numbers the calculation is performed precisely, if n-k < 5

Definition at line 88 of file BCMath.cxx.

00089 {
00090    // switch parameters if n < k
00091    if(n<k)
00092    {
00093       int a=n;
00094       n=k;
00095       k=a;
00096    }
00097 
00098    if(n==k || k==0) return 0.;
00099    if(k==1 || k==n-1) return log((double)n);
00100 
00101    // if no approximation needed
00102    if(n<BCMATH_NFACT_ALIMIT || (n-k)<5) return BCMath::LogNoverK(n,k);
00103 
00104    // calculate final log(n over k) using approximations if necessary
00105    return BCMath::ApproxLogFact((double)n) - BCMath::ApproxLogFact((double)k) - BCMath::ApproxLogFact((double)(n-k));
00106 }

double BCMath::LogBreitWignerNonRel ( double  x,
double  mean,
double  Gamma,
bool  norm = false 
)

Calculates the logarithm of the non-relativistic Breit-Wigner distribution.

Definition at line 206 of file BCMath.cxx.

00207 {
00208    double bw = log(Gamma) - log( (x - mean) * (x - mean) + Gamma * Gamma / 4.0);
00209 
00210    if (norm)
00211       bw -= log(2. * M_PI);
00212 
00213    return bw;
00214 }

double BCMath::LogBreitWignerRel ( double  x,
double  mean,
double  Gamma 
)

Definition at line 218 of file BCMath.cxx.

00219 {
00220    return -log( (x*x - mean*mean) * (x*x - mean*mean) + mean*mean*Gamma*Gamma );
00221 }

double BCMath::LogChi2 ( double  x,
int  n 
)

Calculates the logarithm of chi square function: chi2(double x; size_t n)

Definition at line 225 of file BCMath.cxx.

00226 {
00227    if (x<0) {
00228       BCLog::OutWarning("BCMath::LogChi2 : parameter cannot be negative!");
00229       return -1e99;
00230    }
00231 
00232    if (x==0 && n==1) {
00233       BCLog::OutWarning("BCMath::LogChi2 : returned value is infinity!");
00234       return 1e99;
00235    }
00236 
00237    double nOver2 = ((double) n)/2.;
00238 
00239    return (nOver2-1.)*log(x) - x/2. - nOver2*log(2) - log(TMath::Gamma(nOver2));
00240 }

double BCMath::LogFact ( int  n  ) 

Calculates natural logarithm of the n-factorial (n!)

Definition at line 120 of file BCMath.cxx.

00121 {
00122    double ln = 0.;
00123 
00124    for(int i=1;i<=n;i++)
00125       ln += log((double)i);
00126 
00127    return ln;
00128 }

double BCMath::LogGaus ( double  x,
double  mean = 0,
double  sigma = 1,
bool  norm = false 
)

Calculate the natural logarithm of a gaussian function with mean and sigma. If norm=true (default is false) the result is multiplied by the normalization constant, i.e. divided by sqrt(2*Pi)*sigma.

Definition at line 19 of file BCMath.cxx.

00020 {
00021    // if we have a delta function, return fixed value
00022    if(sigma==0.)
00023       return 0;
00024 
00025    // if sigma is negative use absolute value
00026    if(sigma<0.)
00027       sigma *= -1.;
00028 
00029    double arg = (x-mean)/sigma;
00030    double result = -.5 * arg * arg;
00031 
00032    // check if we should add the normalization constant
00033    if(!norm)
00034       return result;
00035 
00036    // subtract the log of the denominator of the normalization constant
00037    // and return
00038    return result - log( sqrt( 2.* M_PI ) * sigma );
00039 }

double BCMath::LogNoverK ( int  n,
int  k 
)

Calculates natural logarithm of the Binomial factor "n over k".

Definition at line 132 of file BCMath.cxx.

00133 {
00134    // switch parameters if n < k
00135    if(n<k)
00136    {
00137       int a = n;
00138       n = k;
00139       k = a;
00140    }
00141 
00142    if(n==k || k==0) return 0.;
00143    if(k==1 || k==n-1) return log((double)n);
00144 
00145    int lmax = BCMath::Max(k,n-k);
00146    int lmin = BCMath::Min(k,n-k);
00147 
00148    double ln = 0.;
00149 
00150    for(int i=n;i>lmax;i--)
00151       ln += log((double)i);
00152    ln -= BCMath::LogFact(lmin);
00153 
00154    return ln;
00155 }

double BCMath::LogPoisson ( double  x,
double  par 
)

Calculate the natural logarithm of a poisson distribution.

Definition at line 43 of file BCMath.cxx.

00044 {
00045    if (par > 899)
00046       return BCMath::LogGaus(x,par,sqrt(par),true);
00047 
00048    if (x<0)
00049       return 0;
00050 
00051    if (x == 0.)
00052       return  -par;
00053 
00054    return x*log(par)-par-BCMath::ApproxLogFact(x);
00055 }

double BCMath::LogVoigtian ( double  x,
double  sigma,
double  gamma 
)

Calculates the logarithm of normalized voigtian function: voigtian(double x, double sigma, double gamma)

voigtian is a convolution of the following two functions: gaussian(x) = 1/(sqrt(2*pi)*sigma) * exp(x*x/(2*sigma*sigma) and lorentz(x) = (1/pi)*(gamma/2) / (x*x + (gamma/2)*(gamma/2))

it is singly peaked at x=0. The width of the peak is decided by sigma and gamma, so they should be positive.

Definition at line 244 of file BCMath.cxx.

00245 {
00246    if (sigma<=0 || gamma<=0) {
00247       BCLog::OutWarning("BCMath::LogVoigtian : widths are negative or zero!");
00248       return -1e99;
00249    }
00250 
00251    return log(TMath::Voigt(x,sigma,gamma));
00252 }

double BCMath::Max ( double  x,
double  y 
) [inline]

Definition at line 95 of file BCMath.h.

00096       { return x >= y ? x : y; }

int BCMath::Max ( int  x,
int  y 
) [inline]

Returns the "greater or equal" of two numbers

Definition at line 92 of file BCMath.h.

00093       { return x >= y ? x : y; }

double BCMath::Min ( double  x,
double  y 
) [inline]

Definition at line 104 of file BCMath.h.

00105    { return x <= y ? x : y; }

int BCMath::Min ( int  x,
int  y 
) [inline]

Returns the "less or equal" of two numbers

Definition at line 101 of file BCMath.h.

00102       { return x <= y ? x : y; }

int BCMath::Nint ( double  x  ) 

Returns the nearest integer of a double number.

Definition at line 159 of file BCMath.cxx.

00160 {
00161    // round to integer
00162    int i;
00163 
00164    if (x >= 0)
00165    {
00166       i = (int)(x + .5);
00167 
00168       if (x + .5 == (double)i && i & 1)
00169          i--;
00170    }
00171 
00172    else
00173    {
00174       i = int(x - 0.5);
00175 
00176       if (x - 0.5 == double(i) && i & 1)
00177          i++;
00178    }
00179 
00180    return i;
00181 }

double BCMath::rms ( int  n,
const double *  a 
)

Returns the rms of an array.

Definition at line 185 of file BCMath.cxx.

00186 {
00187    if (n <= 0 || !a)
00188       return 0;
00189 
00190    double sum = 0., sum2 = 0.;
00191 
00192    for (int i = 0; i < n; i++)
00193    {
00194       sum  += a[i];
00195       sum2 += a[i] * a[i];
00196    }
00197 
00198    double n1 = 1./(double)n;
00199    double mean = sum * n1;
00200 
00201    return sqrt(fabs(sum2 * n1 - mean * mean));
00202 }


Generated on Tue Oct 6 09:48:23 2009 for Bayesian Analysis Toolkit by  doxygen 1.6.1