thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
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
14namespace ThePEG {
15
18namespace Math {
19
24struct MathType {};
25
28double exp1m(double x);
29
32double log1m(double);
33
35double powi(double x, int p);
36
38inline 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
43inline 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
50inline 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
55inline 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
61inline 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
73inline 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
79template <typename FloatType>
80inline double relativeError(FloatType x, FloatType y) {
81 return ( x == y ? 0.0 : double((x - y)/(abs(x) + abs(y))) );
82}
83
85template <typename T>
86inline T absmin(const T & x, const T & y) {
87 return abs(x) < abs(y)? x: y;
88}
89
91template <typename T>
92inline T absmax(const T & x, const T & y) {
93 return abs(x) > abs(y)? x: y;
94}
95
99template <typename T, typename U>
100inline T sign(T x, U y) {
101 return y > U()? abs(x): -abs(x);
102}
103
109template <int N, bool Inv>
110struct Power: public MathType {};
111
115template <int N>
116struct Power<N,false> {
118 static double pow(double x) { return x*Power<N-1,false>::pow(x); }
119};
120
124template <int N>
125struct Power<N,true> {
127 static double pow(double x) { return Power<N+1,true>::pow(x)/x; }
128};
129
133template <>
134struct Power<0,true> {
136 static double pow(double) { return 1.0; }
137};
138
142template <>
143struct Power<0,false> {
145 static double pow(double) { return 1.0; }
146};
148
151template <int N>
152inline double Pow(double x) { return Power<N, (N < 0)>::pow(x); }
153
157namespace Functions {
158
160template <int N>
161struct 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
186template <>
187inline double PowX<1>::generate(double x0, double x1, double R) {
188 return std::sqrt(x0*x0 + R*(x1*x1 - x0*x0));
189}
190
194template <>
195inline double PowX<0>::generate(double x0, double x1, double R) {
196 return x0 + R*(x1 - x0);
197}
198
202template<>
203inline double PowX<-1>::primitive(double x) {
204 return log(x);
205}
206
210template <>
211inline double PowX<-1>::integrate(double x0, double x1) {
212 return log(x1/x0);
213}
214
218template <>
219inline double PowX<-1>::generate(double x0, double x1, double R) {
220 return x0*pow(x1/x0, R);
221}
222
226template <>
227inline double PowX<-2>::generate(double x0, double x1, double R) {
228 return x0*x1/(x1 - R*(x1 - x0));
229}
230
234template <>
235inline 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
245template <int N>
246struct 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
267struct 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
289struct 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
311template <int N, int D>
312struct 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 */
T absmax(const T &x, const T &y)
Return x if |x|>|y|, else return y.
Definition: Maths.h:92
double relativeError(FloatType x, FloatType y)
Returns (x - y)/(|x| + |y|).
Definition: Maths.h:80
double exp1m(double x)
Return , with highest possible precision for .
double powi(double x, int p)
Return x rased to the integer power p, using recursion.
double pGenerate(double p, double xl, double xu, double rnd)
Generate an x between xl and xu distributed as .
Definition: Maths.h:55
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
double pIntegrate(double p, double xl, double xu)
Return the integral of between xl and xu.
Definition: Maths.h:38
T sign(T x, U y)
Transfer the sign of the second argument to the first.
Definition: Maths.h:100
double Pow(double x)
Templated function to calculate integer powers known at compile-time.
Definition: Maths.h:152
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
T absmin(const T &x, const T &y)
Return x if |x|<|y|, else return y.
Definition: Maths.h:86
double log1m(double)
Return , with highest possible precision for .
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
Class corresponding to functions of the form .
Definition: Maths.h:289
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:297
static double primitive(double x)
The primitive function.
Definition: Maths.h:292
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
Class corresponding to functions of the form with integer N and D.
Definition: Maths.h:312
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:321
static double primitive(double x)
The primitive function.
Definition: Maths.h:315
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
Class corresponding to functions of the form .
Definition: Maths.h:267
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:275
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
static double primitive(double x)
The primitive function.
Definition: Maths.h:270
Class corresponding to functions of the form with integer N.
Definition: Maths.h:246
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
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:254
Class corresponding to functions of the form with integer N.
Definition: Maths.h:161
static double integrate(double x0, double x1)
Integrate function in a given interval.
Definition: Maths.h:169
static double primitive(double x)
The primitive function.
Definition: Maths.h:164
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
MathType is an empty non-polymorphic base class for all mathematical function types.
Definition: Maths.h:24
static double pow(double)
Member for the power.
Definition: Maths.h:145
static double pow(double)
Member for the power.
Definition: Maths.h:136
static double pow(double x)
Member for the power.
Definition: Maths.h:118
static double pow(double x)
Member for the power.
Definition: Maths.h:127
Templated class for calculating integer powers.
Definition: Maths.h:110