TBCI Numerical high perf. C++ Library  2.8.0
mathplus.h
Go to the documentation of this file.
1 
5 // $Id: mathplus.h,v 1.16.2.5 2019/05/28 11:13:02 garloff Exp $
6 
7 #ifndef TBCI_MATHPLUS_H
8 #define TBCI_MATHPLUS_H
9 
10 /* These do NOT include basics.h and do NOT use namespace TBCI ! */
11 /* It should be ANSI C (not: C++) compliant */
12 
13 #include <math.h>
14 #include "tbci/constants.h"
15 
16 #ifndef MATH__
17 # define MATH__
18 #endif
19 
20 #ifndef LONG_LONG
21 # ifdef __GNUC__
22 # define LONG_LONG long long
23 # else
24 # define LONG_LONG long
25 # endif
26 #endif
27 
29 #if defined(__cplusplus) && defined(NAMESPACE_TBCI)
31 #endif
32 
34 
35 inline double bernoulli(const double x)
36 {
38  if (MATH__ fabs(x)<0.01) return ((-1.0/720.0*x*x+1.0/12.0)*x-0.5)*x +1.0;
39  return (x/(MATH__ exp(x)-1));
40 }
41 
42 
43 #ifndef __USE_MISC
44 inline double asinh(const double x)
45 {
46  if(-x == MATH__ sqrt(1+x*x)) return MATH__ log (1/(2 * MATH__ fabs(x)));
47  else return MATH__ log(x + MATH__ sqrt(1+x*x) );
48 }
49 #endif
50 
52 inline unsigned long binom (const unsigned long x, unsigned long y)
53 {
54  unsigned LONG_LONG e, d;
55  unsigned long i;
56  if (y > x) return 0;
57  if (y == x || y == 0) return 1;
58  if (y > x/2) y = x - y;
59  e = x; d = y;
60  { for (i = (x-1); i > (x-y); i--) e *= i; }
61  { for (i = (y-1); i > 1; i--) d *= i; }
62  return (e / d);
63 }
64 
66 inline unsigned long binomial (const unsigned long y, const unsigned long z)
67 {
68  unsigned LONG_LONG e, d;
69  unsigned long i, yy, x = y + z;
70  if (y == x || y == 0) return 1;
71  if (y > x/2) yy = x - y; else yy = y;
72  e = x; d = yy;
73  { for (i = (x-1); i > (x-yy); i--) e *= i; }
74  { for (i = (yy-1); i > 1; i--) d *= i; }
75  return (e / d);
76 }
77 
78 
79 inline unsigned LONG_LONG fac (const unsigned char x)
80 {
81  unsigned char i;
82  unsigned LONG_LONG f = 1;
83  for (i = 2; i <= x; i++) f *= i;
84  return f;
85 }
86 
87 
92 inline unsigned long trinomial (const unsigned long x, const unsigned long y, const unsigned long z)
93 {
94  return fac((unsigned char)(x+y+z)) / ( fac((unsigned char)x)
95  *fac((unsigned char)y)
96  *fac((unsigned char)z) );
97 }
98 
99 
101 inline long double fact (const double x)
102 {
103  long double corr, r = 1.0/x;
104  //Abramov/Stegun (51840/139 = 372.94964..)
105  //corr = 1.0 + r/12 + r/(x*288) - 139.0*r/(x*x*51840);
106  //optimized for [42;43]
107  corr = 1.0 + r/12.00000034 + r/(x*287.97452028) - r/(x*x*371.045958768);
108  return (MATH__ pow(x/E1,x) * MATH__ sqrt(x*(pi*2.0)) * corr);
109 }
110 
112 inline long double ldgamma (const double x)
113 {
114  long double res; double x0 = x - 1.0;
115  if (x0 >= 42.0 && x0 <= 43.0) return fact (x0);
116  x0 += 42.0 - MATH__ floor(x0); // ]42.0;43.0[
117  res = fact (x0);
118  if ((x-x0) < 0.5) while ((x-x0) < 0.5) { res /= x0; x0 -= 1.0; }
119  else while ((x-x0) > 1.5) { x0 += 1.0; res *= x0; }
120  return res;
121 }
122 
124 inline double poisson (const double x, const double la)
125 {
126  return (MATH__ pow(la,x) * MATH__ exp(-la) / ldgamma(x+1.0));
127 }
128 
130 inline double chi_s (const double x, const double n)
131 {
132  double k = 1.0 / (MATH__ pow(2.0,n/2.0)*ldgamma(n/2.0));
133  return k * MATH__ pow(x,(n-2.0)/2.0) * MATH__ exp (-x/2.0);
134 }
135 
137 inline double erfc3 (const double x, const double c, const double s)
138 {
139  double d = (x-c)/s;
140  return MATH__ exp (-0.5*d*d) / (s * MATH__ sqrt(2.0*pi));
141 }
142 
143 #if defined(__cplusplus) && defined(NAMESPACE_TBCI)
145 #endif
146 #endif /* TBCI_MATHPLUS_H */
unsigned long binom(const unsigned long x, unsigned long y)
x!/(y!(x-y)!)
Definition: mathplus.h:52
const Vector< T > const Vector< T > & x
Definition: LM_fit.h:97
unsigned long trinomial(const unsigned long x, const unsigned long y, const unsigned long z)
(x+y+z)!/(x!y!z!) Like for binomial, more efficient algos (or algos less prone to overflow) are possi...
Definition: mathplus.h:92
tm fac
Definition: f_matrix.h:1052
long double fact(const double x)
Definition: mathplus.h:101
unsigned long binomial(const unsigned long y, const unsigned long z)
(y+z)!/(y!z!)
Definition: mathplus.h:66
TBCI__ cplx< T > log(const TBCI__ cplx< T > &z)
Definition: cplx.h:771
return c
Definition: f_matrix.h:760
double fabs(const int a)
Definition: basics.h:1215
#define NAMESPACE_TBCI
Definition: basics.h:317
double erfc3(const double x, const double c, const double s)
Gauss normal distribution (x, center, sigma)
Definition: mathplus.h:137
const double E1
. Exponential base.
Definition: constants.h:71
double poisson(const double x, const double la)
poisson distrib
Definition: mathplus.h:124
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & z
Definition: LM_fit.h:172
const double pi
Definition: constants.h:38
double sqrt(const int a)
Definition: basics.h:1216
long double ldgamma(const double x)
the gamma fct
Definition: mathplus.h:112
TBCI__ cplx< T > exp(const TBCI__ cplx< T > &z)
Definition: cplx.h:756
int i
Definition: LM_fit.h:71
TBCI__ cplx< T > asinh(const TBCI__ cplx< T > &z)
Definition: cplx.h:811
double chi_s(const double x, const double n)
chi_square distrib
Definition: mathplus.h:130
#define NAMESPACE_END
Definition: basics.h:323
const Vector< T > Vector< T > Vector< T > Vector< T > & y
Definition: LM_fit.h:172
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int res
Definition: LM_fit.h:199
double bernoulli(const double x)
Despite being C compliant, we do use namespace stuff, if we can.
Definition: mathplus.h:35
#define LONG_LONG
Definition: mathplus.h:22
#define MATH__
Definition: mathplus.h:17
TBCI__ cplx< T > pow(const TBCI__ cplx< T > &z, const double n)
Definition: cplx.h:761