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 | 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 | 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) |
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 58 of file BCMath.cxx.
00059 { 00060 return exp( BCMath::LogApproxBinomial(n, k, p) ); 00061 }
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 109 of file BCMath.cxx.
00110 { 00111 if(x>BCMATH_NFACT_ALIMIT) 00112 return x*log(x) - x + log(x*(1.+4.*x*(1.+2.*x)))/6. + log(M_PI)/2.; 00113 00114 else 00115 return BCMath::LogFact((int)x); 00116 }
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 65 of file BCMath.cxx.
00066 { 00067 // check p 00068 if (p == 0) 00069 return -1e99; 00070 00071 else if (p == 1) 00072 return 0; 00073 00074 // switch parameters if n < k 00075 if(n<k) 00076 { 00077 int a=n; 00078 n=k; 00079 k=a; 00080 } 00081 00082 return BCMath::LogBinomFactor(n,k) + (double)k*log(p) + (double)(n-k)*log(1.-p); 00083 }
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 87 of file BCMath.cxx.
00088 { 00089 // switch parameters if n < k 00090 if(n<k) 00091 { 00092 int a=n; 00093 n=k; 00094 k=a; 00095 } 00096 00097 if(n==k || k==0) return 0.; 00098 if(k==1 || k==n-1) return log((double)n); 00099 00100 // if no approximation needed 00101 if(n<BCMATH_NFACT_ALIMIT || (n-k)<5) return BCMath::LogNoverK(n,k); 00102 00103 // calculate final log(n over k) using approximations if necessary 00104 return BCMath::ApproxLogFact((double)n) - BCMath::ApproxLogFact((double)k) - BCMath::ApproxLogFact((double)(n-k)); 00105 }
double BCMath::LogBreitWignerNonRel | ( | double | x, | |
double | mean, | |||
double | Gamma, | |||
bool | norm = false | |||
) |
Definition at line 205 of file BCMath.cxx.
00206 { 00207 double bw = log(Gamma) - log( (x - mean) * (x - mean) + Gamma * Gamma / 4.0); 00208 00209 if (norm) 00210 bw -= log(2. * M_PI); 00211 00212 return bw; 00213 }
double BCMath::LogBreitWignerRel | ( | double | x, | |
double | mean, | |||
double | Gamma | |||
) |
double BCMath::LogFact | ( | int | n | ) |
Calculates natural logarithm of the n-factorial (n!)
Definition at line 119 of file BCMath.cxx.
00120 { 00121 double ln = 0.; 00122 00123 for(int i=1;i<=n;i++) 00124 ln += log((double)i); 00125 00126 return ln; 00127 }
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 18 of file BCMath.cxx.
00019 { 00020 // if we have a delta function, return fixed value 00021 if(sigma==0.) 00022 return 0; 00023 00024 // if sigma is negative use absolute value 00025 if(sigma<0.) 00026 sigma *= -1.; 00027 00028 double arg = (x-mean)/sigma; 00029 double result = -.5 * arg * arg; 00030 00031 // check if we should add the normalization constant 00032 if(!norm) 00033 return result; 00034 00035 // subtract the log of the denominator of the normalization constant 00036 // and return 00037 return result - log( sqrt( 2.* M_PI ) * sigma ); 00038 }
double BCMath::LogNoverK | ( | int | n, | |
int | k | |||
) |
Calculates natural logarithm of the Binomial factor "n over k".
Definition at line 131 of file BCMath.cxx.
00132 { 00133 // switch parameters if n < k 00134 if(n<k) 00135 { 00136 int a = n; 00137 n = k; 00138 k = a; 00139 } 00140 00141 if(n==k || k==0) return 0.; 00142 if(k==1 || k==n-1) return log((double)n); 00143 00144 int lmax = BCMath::Max(k,n-k); 00145 int lmin = BCMath::Min(k,n-k); 00146 00147 double ln = 0.; 00148 00149 for(int i=n;i>lmax;i--) 00150 ln += log((double)i); 00151 ln -= BCMath::LogFact(lmin); 00152 00153 return ln; 00154 }
double BCMath::LogPoisson | ( | double | x, | |
double | par | |||
) |
Calculate the natural logarithm of a poisson distribution.
Definition at line 42 of file BCMath.cxx.
00043 { 00044 if (par > 899) 00045 return BCMath::LogGaus(x,par,sqrt(par),true); 00046 00047 if (x<0) 00048 return 0; 00049 00050 if (x == 0.) 00051 return -par; 00052 00053 return x*log(par)-par-BCMath::ApproxLogFact(x); 00054 }
double BCMath::Max | ( | double | x, | |
double | y | |||
) | [inline] |
int BCMath::Max | ( | int | x, | |
int | y | |||
) | [inline] |
double BCMath::Min | ( | double | x, | |
double | y | |||
) | [inline] |
int BCMath::Min | ( | int | x, | |
int | y | |||
) | [inline] |
int BCMath::Nint | ( | double | x | ) |
Returns the nearest integer of a double number.
Definition at line 158 of file BCMath.cxx.
00159 { 00160 // round to integer 00161 int i; 00162 00163 if (x >= 0) 00164 { 00165 i = (int)(x + .5); 00166 00167 if (x + .5 == (double)i && i & 1) 00168 i--; 00169 } 00170 00171 else 00172 { 00173 i = int(x - 0.5); 00174 00175 if (x - 0.5 == double(i) && i & 1) 00176 i++; 00177 } 00178 00179 return i; 00180 }
double BCMath::rms | ( | int | n, | |
const double * | a | |||
) |
Returns the rms of an array.
Definition at line 184 of file BCMath.cxx.
00185 { 00186 if (n <= 0 || !a) 00187 return 0; 00188 00189 double sum = 0., sum2 = 0.; 00190 00191 for (int i = 0; i < n; i++) 00192 { 00193 sum += a[i]; 00194 sum2 += a[i] * a[i]; 00195 } 00196 00197 double n1 = 1./(double)n; 00198 double mean = sum * n1; 00199 00200 return sqrt(fabs(sum2 * n1 - mean * mean)); 00201 }