thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
Maths.h
1 // -*- C++ -*-
2 //
3 // Maths.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2019 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef ThePEG_Math_H
10 #define ThePEG_Math_H
11 
12 #include <cmath>
13 
14 namespace ThePEG {
15 
18 namespace Math {
19 
24 struct MathType {};
25 
28 double exp1m(double x);
29 
32 double log1m(double);
33 
35 double powi(double x, int p);
36 
38 inline double pIntegrate(double p, double xl, double xu) {
39  return p == -1.0? log(xu/xl): (pow(xu, p + 1.0) - pow(xl, p + 1.0))/(p + 1.0);
40 }
41 
43 inline double pIntegrate(int p, double xl, double xu) {
44  return p == -1? log(xu/xl): (powi(xu, p + 1) - powi(xl, p + 1))/double(p + 1);
45 }
46 
50 inline double pXIntegrate(double e, double xl, double dx) {
51  return e == 0.0? log1m(-dx/xl): -pow(xl, e)*exp1m(e*log1m(-dx/xl))/e;
52 }
53 
55 inline double pGenerate(double p, double xl, double xu, double rnd) {
56  return p == -1.0? xl*pow(xu/xl, rnd):
57  pow((1.0 - rnd)*pow(xl, p + 1.0) + rnd*pow(xu, p + 1.0), 1.0/(1.0 + p));
58 }
59 
61 inline double pGenerate(int p, double xl, double xu, double rnd) {
62  return p == -1? xl*pow(xu/xl, rnd):
63  pow((1.0 - rnd)*powi(xl, p + 1) + rnd*powi(xu, p + 1), 1.0/double(1 + p));
64 }
65 
73 inline double pXGenerate(double e, double xl, double dx, double rnd) {
74  return e == 0.0? -xl*exp1m(rnd*log1m(-dx/xl)):
75  -exp1m(log1m(rnd*exp1m(e*log1m(-dx/xl)))/e)*xl;
76 }
77 
79 template <typename FloatType>
80 inline double relativeError(FloatType x, FloatType y) {
81  return ( x == y ? 0.0 : double((x - y)/(abs(x) + abs(y))) );
82 }
83 
85 template <typename T>
86 inline T absmin(const T & x, const T & y) {
87  return abs(x) < abs(y)? x: y;
88 }
89 
91 template <typename T>
92 inline T absmax(const T & x, const T & y) {
93  return abs(x) > abs(y)? x: y;
94 }
95 
99 template <typename T, typename U>
100 inline T sign(T x, U y) {
101  return y > U()? abs(x): -abs(x);
102 }
103 
109 template <int N, bool Inv>
110 struct Power: public MathType {};
111 
115 template <int N>
116 struct Power<N,false> {
118  static double pow(double x) { return x*Power<N-1,false>::pow(x); }
119 };
120 
124 template <int N>
125 struct Power<N,true> {
127  static double pow(double x) { return Power<N+1,true>::pow(x)/x; }
128 };
129 
133 template <>
134 struct Power<0,true> {
136  static double pow(double) { return 1.0; }
137 };
138 
142 template <>
143 struct Power<0,false> {
145  static double pow(double) { return 1.0; }
146 };
148 
151 template <int N>
152 inline double Pow(double x) { return Power<N, (N < 0)>::pow(x); }
153 
157 namespace Functions {
158 
160 template <int N>
161 struct PowX: public MathType {
162 
164  static double primitive(double x) {
165  return Pow<N+1>(x)/double(N+1);
166  }
167 
169  static double integrate(double x0, double x1) {
170  return primitive(x1) - primitive(x0);
171  }
172 
175  static double generate(double x0, double x1, double R) {
176  return pow(primitive(x0) + R*integrate(x0, x1), 1.0/double(N+1));
177  }
178 
179 };
180 
186 template <>
187 inline double PowX<1>::generate(double x0, double x1, double R) {
188  return std::sqrt(x0*x0 + R*(x1*x1 - x0*x0));
189 }
190 
194 template <>
195 inline double PowX<0>::generate(double x0, double x1, double R) {
196  return x0 + R*(x1 - x0);
197 }
198 
202 template<>
203 inline double PowX<-1>::primitive(double x) {
204  return log(x);
205 }
206 
210 template <>
211 inline double PowX<-1>::integrate(double x0, double x1) {
212  return log(x1/x0);
213 }
214 
218 template <>
219 inline double PowX<-1>::generate(double x0, double x1, double R) {
220  return x0*pow(x1/x0, R);
221 }
222 
226 template <>
227 inline double PowX<-2>::generate(double x0, double x1, double R) {
228  return x0*x1/(x1 - R*(x1 - x0));
229 }
230 
234 template <>
235 inline double PowX<-3>::generate(double x0, double x1, double R) {
236  return x0*x1/std::sqrt(x1*x1 - R*(x1*x1 - x0*x0));
237 }
238 
245 template <int N>
246 struct Pow1mX: public MathType {
247 
249  static double primitive(double x) {
250  return -PowX<N>::primitive(1.0 - x);
251  }
252 
254  static double integrate(double x0, double x1) {
255  return PowX<N>::integrate(1.0 - x1, 1.0 - x0);
256  }
257 
260  static double generate(double x0, double x1, double R) {
261  return 1.0 - PowX<N>::generate(1.0 - x1, 1.0 - x0, R);
262  }
263 
264 };
265 
267 struct InvX1mX: public MathType {
268 
270  static double primitive(double x) {
271  return log(x/(1.0 - x));
272  }
273 
275  static double integrate(double x0, double x1) {
276  return log(x1*(1.0 - x0)/(x0*(1.0 - x1)));
277  }
278 
281  static double generate(double x0, double x1, double R) {
282  double r = pow(x1*(1.0 - x0)/(x0*(1.0 - x1)), R)*x0/(1.0 - x0);
283  return r/(1.0 + r);
284  }
285 
286 };
287 
289 struct ExpX: public MathType {
290 
292  static double primitive(double x) {
293  return exp(x);
294  }
295 
297  static double integrate(double x0, double x1) {
298  return exp(x1) - exp(x0);
299  }
300 
303  static double generate(double x0, double x1, double R) {
304  return log(exp(x0) + R*(exp(x1) - exp(x0)));
305  }
306 
307 };
308 
311 template <int N, int D>
312 struct FracPowX: public MathType {
313 
315  static double primitive(double x) {
316  double r = double(N)/double(D) + 1.0;
317  return pow(x, r)/r;
318  }
319 
321  static double integrate(double x0, double x1) {
322  return primitive(x1) - primitive(x0);
323  }
324 
327  static double generate(double x0, double x1, double R) {
328  double r = double(N)/double(D) + 1.0;
329  return pow(primitive(x0) + R*integrate(x0, x1), 1.0/r);
330  }
331 
332 };
333 
334 }
335 
336 }
337 
338 }
339 
340 #endif /* ThePEG_Math_H */
Class corresponding to functions of the form with integer N.
Definition: Maths.h:161
static double generate(double x0, double x1, double R)
Sample a distribution in a given interval given a flat random number R in the interval ]0...
Definition: Maths.h:281
Class corresponding to functions of the form with integer N.
Definition: Maths.h:246
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:297
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:254
static double primitive(double x)
The primitive function.
Definition: Maths.h:270
Class corresponding to functions of the form .
Definition: Maths.h:267
double pIntegrate(double p, double xl, double xu)
Return the integral of between xl and xu.
Definition: Maths.h:38
static double pow(double x)
Member for the power.
Definition: Maths.h:118
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
static double pow(double)
Member for the power.
Definition: Maths.h:136
double exp1m(double x)
Return , with highest possible precision for .
static double pow(double x)
Member for the power.
Definition: Maths.h:127
T absmin(const T &x, const T &y)
Return x if |x|<|y|, else return y.
Definition: Maths.h:86
double pXIntegrate(double e, double xl, double dx)
Return the integral of between xl and xl+dx with highest possible precision for and/or ...
Definition: Maths.h:50
double log1m(double)
Return , with highest possible precision for .
Templated class for calculating integer powers.
Definition: Maths.h:110
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:275
static double primitive(double x)
The primitive function.
Definition: Maths.h:164
Class corresponding to functions of the form with integer N and D.
Definition: Maths.h:312
static double primitive(double x)
The primitive function.
Definition: Maths.h:292
double relativeError(FloatType x, FloatType y)
Returns (x - y)/(|x| + |y|).
Definition: Maths.h:80
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:321
double powi(double x, int p)
Return x rased to the integer power p, using recursion.
double pXGenerate(double e, double xl, double dx, double rnd)
Generate an x between xl and xl + dx distributed as with highest possible precision for and/or * ...
Definition: Maths.h:73
static double pow(double)
Member for the power.
Definition: Maths.h:145
double pGenerate(double p, double xl, double xu, double rnd)
Generate an x between xl and xu distributed as .
Definition: Maths.h:55
Class corresponding to functions of the form .
Definition: Maths.h:289
static double primitive(double x)
The primitive function.
Definition: Maths.h:249
static double generate(double x0, double x1, double R)
Sample a distribution in a given interval given a flat random number R in the interval ]0...
Definition: Maths.h:260
MathType is an empty non-polymorphic base class for all mathematical function types.
Definition: Maths.h:24
static double primitive(double x)
The primitive function.
Definition: Maths.h:315
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:169
static double generate(double x0, double x1, double R)
Sample a distribution in a given interval given a flat random number R in the interval ]0...
Definition: Maths.h:303
T sign(T x, U y)
Transfer the sign of the second argument to the first.
Definition: Maths.h:100
static double generate(double x0, double x1, double R)
Sample a distribution in a given interval given a flat random number R in the interval ]0...
Definition: Maths.h:327
double Pow(double x)
Templated function to calculate integer powers known at compile-time.
Definition: Maths.h:152
static double generate(double x0, double x1, double R)
Sample a distribution in a given interval given a flat random number R in the interval ]0...
Definition: Maths.h:175
T absmax(const T &x, const T &y)
Return x if |x|>|y|, else return y.
Definition: Maths.h:92