00001
00005
00006
00007 #ifndef TBCI_MATHPLUS_H
00008 #define TBCI_MATHPLUS_H
00009
00010
00011
00012
00013 #include <math.h>
00014 #include "constants.h"
00015
00016 #ifndef MATH__
00017 # define MATH__
00018 #endif
00019
00020 #ifndef LONG_LONG
00021 # ifdef __GNUC__
00022 # define LONG_LONG long long
00023 # else
00024 # define LONG_LONG long
00025 # endif
00026 #endif
00027
00029 #if defined(__cplusplus) && defined(NAMESPACE_TBCI)
00030 NAMESPACE_TBCI
00031 #endif
00032
00034
00035 inline double bernoulli(const double x)
00036 {
00038 if (MATH__ fabs(x)<0.01) return ((-1.0/720.0*x*x+1.0/12.0)*x-0.5)*x +1.0;
00039 return (x/(MATH__ exp(x)-1));
00040 }
00041
00042
00043 #ifndef __USE_MISC
00044 inline double asinh(const double x)
00045 {
00046 if(-x == MATH__ sqrt(1+x*x)) return MATH__ log (1/(2 * MATH__ fabs(x)));
00047 else return MATH__ log(x + MATH__ sqrt(1+x*x) );
00048 }
00049 #endif
00050
00052 inline unsigned long binom (const unsigned long x, unsigned long y)
00053 {
00054 unsigned LONG_LONG e, d;
00055 unsigned long i;
00056 if (y > x) return 0;
00057 if (y == x || y == 0) return 1;
00058 if (y > x/2) y = x - y;
00059 e = x; d = y;
00060 { for (i = (x-1); i > (x-y); i--) e *= i; }
00061 { for (i = (y-1); i > 1; i--) d *= i; }
00062 return (e / d);
00063 }
00064
00066 inline unsigned long binomial (const unsigned long y, const unsigned long z)
00067 {
00068 unsigned LONG_LONG e, d;
00069 unsigned long i, yy, x = y + z;
00070 if (y == x || y == 0) return 1;
00071 if (y > x/2) yy = x - y; else yy = y;
00072 e = x; d = yy;
00073 { for (i = (x-1); i > (x-yy); i--) e *= i; }
00074 { for (i = (yy-1); i > 1; i--) d *= i; }
00075 return (e / d);
00076 }
00077
00078
00079 inline unsigned LONG_LONG fac (const unsigned char x)
00080 {
00081 unsigned char i;
00082 unsigned LONG_LONG f = 1;
00083 for (i = 2; i <= x; i++) f *= i;
00084 return f;
00085 }
00086
00087
00092 inline unsigned long trinomial (const unsigned long x, const unsigned long y, const unsigned long z)
00093 {
00094 return fac((unsigned char)(x+y+z)) / ( fac((unsigned char)x)
00095 *fac((unsigned char)y)
00096 *fac((unsigned char)z) );
00097 }
00098
00099
00101 inline long double fact (const double x)
00102 {
00103 long double corr, r = 1.0/x;
00104
00105
00106
00107 corr = 1.0 + r/12.00000034 + r/(x*287.97452028) - r/(x*x*371.045958768);
00108 return (MATH__ pow(x/E1,x) * MATH__ sqrt(x*(pi*2.0)) * corr);
00109 }
00110
00112 inline long double ldgamma (const double x)
00113 {
00114 long double res; double x0 = x - 1.0;
00115 if (x0 >= 42.0 && x0 <= 43.0) return fact (x0);
00116 x0 += 42.0 - MATH__ floor(x0);
00117 res = fact (x0);
00118 if ((x-x0) < 0.5) while ((x-x0) < 0.5) { res /= x0; x0 -= 1.0; }
00119 else while ((x-x0) > 1.5) { x0 += 1.0; res *= x0; }
00120 return res;
00121 }
00122
00124 inline double poisson (const double x, const double la)
00125 {
00126 return (MATH__ pow(la,x) * MATH__ exp(-la) / ldgamma(x+1.0));
00127 }
00128
00130 inline double chi_s (const double x, const double n)
00131 {
00132 double k = 1.0 / (MATH__ pow(2.0,n/2.0)*ldgamma(n/2.0));
00133 return k * MATH__ pow(x,(n-2.0)/2.0) * MATH__ exp (-x/2.0);
00134 }
00135
00137 inline double erfc3 (const double x, const double c, const double s)
00138 {
00139 double d = (x-c)/s;
00140 return MATH__ exp (-0.5*d*d) / (s * MATH__ sqrt(2.0*pi));
00141 }
00142
00143 #if defined(__cplusplus) && defined(NAMESPACE_TBCI)
00144 NAMESPACE_END
00145 #endif
00146 #endif