/usr/include/ThePEG/Utilities/Maths.h is in libthepeg-dev 1.8.0-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | // -*- C++ -*-
//
// Maths.h is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef ThePEG_Math_H
#define ThePEG_Math_H
#include <cmath>
namespace ThePEG {
/** The Math namespace includes the declaration of some useful
* mathematical functions. */
namespace Math {
/**
* MathType is an empty non-polymorphic base class for all
* mathematical function types.
*/
struct MathType {};
/** The gamma function */
double gamma(double);
/** The log of the gamma function */
double lngamma(double);
/** Return \f${\rm atanh}(x)\f$ */
double atanh(double);
/** Return \f$1-e^x\f$, with highest possible precision for
* \f$x\rightarrow 0\f$. */
double exp1m(double x);
/** Return \f$1\log(1-x)\f$, with highest possible precision for
* \f$x\rightarrow 0\f$. */
double log1m(double);
/** Return x rased to the integer power p, using recursion. */
double powi(double x, int p);
/** Return the integral of \f$x^p dx\f$ between xl and xu. */
inline double pIntegrate(double p, double xl, double xu) {
return p == -1.0? log(xu/xl): (pow(xu, p + 1.0) - pow(xl, p + 1.0))/(p + 1.0);
}
/** Return the integral of \f$x^p dx\f$ between xl and xu. */
inline double pIntegrate(int p, double xl, double xu) {
return p == -1? log(xu/xl): (powi(xu, p + 1) - powi(xl, p + 1))/double(p + 1);
}
/** Return the integral of \f$x^{e-1} dx\f$ between xl and xl+dx with
* highest possible precision for \f$dx\rightarrow 0\f$ and/or
* \f$e\rightarrow 0\f$. */
inline double pXIntegrate(double e, double xl, double dx) {
return e == 0.0? log1m(-dx/xl): -pow(xl, e)*exp1m(e*log1m(-dx/xl))/e;
}
/** Generate an x between xl and xu distributed as \f$x^p\f$. */
inline double pGenerate(double p, double xl, double xu, double rnd) {
return p == -1.0? xl*pow(xu/xl, rnd):
pow((1.0 - rnd)*pow(xl, p + 1.0) + rnd*pow(xu, p + 1.0), 1.0/(1.0 + p));
}
/** Generate an x between xl and xu distributed as \f$x^p\f$. */
inline double pGenerate(int p, double xl, double xu, double rnd) {
return p == -1? xl*pow(xu/xl, rnd):
pow((1.0 - rnd)*powi(xl, p + 1) + rnd*powi(xu, p + 1), 1.0/double(1 + p));
}
/** Generate an x between xl and xl + dx distributed as \f$x^{e-1}\f$
* with highest possible precision for\f$dx\rightarrow 0\f$ and/or *
* \f$e\rightarrow 0\f$.
* @param e the parameter defining the power in \f$x^{e-1}\f$.
* @param xl the lower bound of the generation interval.
* @param dx the interval.
* @param rnd a flat random number in the interval ]0,1[. */
inline double pXGenerate(double e, double xl, double dx, double rnd) {
return e == 0.0? -xl*exp1m(rnd*log1m(-dx/xl)):
-exp1m(log1m(rnd*exp1m(e*log1m(-dx/xl)))/e)*xl;
}
/** Returns (x - y)/(|x| + |y|). */
template <typename FloatType>
inline double relativeError(FloatType x, FloatType y) {
return ( x == y ? 0.0 : double((x - y)/(abs(x) + abs(y))) );
}
/** Return x if |x|<|y|, else return y. */
template <typename T>
inline T absmin(const T & x, const T & y) {
return abs(x) < abs(y)? x: y;
}
/** Return x if |x|>|y|, else return y. */
template <typename T>
inline T absmax(const T & x, const T & y) {
return abs(x) > abs(y)? x: y;
}
/** Transfer the sign of the second argument to the first.
* @return \f$|x|\f$ if \f$y>0\f$ otherwise return \f$-|x|\f$.
*/
template <typename T, typename U>
inline T sign(T x, U y) {
return y > U()? abs(x): -abs(x);
}
/** Templated class for calculating integer powers. */
//@{
/**
* Struct for powers
*/
template <int N, bool Inv>
struct Power: public MathType {};
/**
* Struct for powers
*/
template <int N>
struct Power<N,false> {
/** Member for the power*/
static double pow(double x) { return x*Power<N-1,false>::pow(x); }
};
/**
* Struct for powers
*/
template <int N>
struct Power<N,true> {
/** Member for the power*/
static double pow(double x) { return Power<N+1,true>::pow(x)/x; }
};
/**
* Struct for powers
*/
template <>
struct Power<0,true> {
/** Member for the power*/
static double pow(double) { return 1.0; }
};
/**
* Struct for powers
*/
template <>
struct Power<0,false> {
/** Member for the power*/
static double pow(double) { return 1.0; }
};
//@}
/** Templated function to calculate integer powers known at
* compile-time. */
template <int N>
inline double Pow(double x) { return Power<N, (N < 0)>::pow(x); }
/** This namespace introduces some useful function classes with known
* primitive and inverse primitive functions. Useful to sample
* corresponding distributions.*/
namespace Functions {
/** Class corresponding to functions of the form \f$x^N\f$ with integer N. */
template <int N>
struct PowX: public MathType {
/** The primitive function. */
static double primitive(double x) {
return Pow<N+1>(x)/double(N+1);
}
/** Integrate function in a given interval. */
static double integrate(double x0, double x1) {
return primitive(x1) - primitive(x0);
}
/** Sample a distribution in a given interval given a flat random
* number R in the interval ]0,1[. */
static double generate(double x0, double x1, double R) {
return pow(primitive(x0) + R*integrate(x0, x1), 1.0/double(N+1));
}
};
/** @cond TRAITSPECIALIZATIONS */
/**
* Template for generating according to a specific power
*/
template <>
inline double PowX<1>::generate(double x0, double x1, double R) {
return std::sqrt(x0*x0 + R*(x1*x1 - x0*x0));
}
/**
* Template for generating according to a specific power
*/
template <>
inline double PowX<0>::generate(double x0, double x1, double R) {
return x0 + R*(x1 - x0);
}
/**
* Template for generating according to a specific power
*/
template<>
inline double PowX<-1>::primitive(double x) {
return log(x);
}
/**
* Template for generating according to a specific power
*/
template <>
inline double PowX<-1>::integrate(double x0, double x1) {
return log(x1/x0);
}
/**
* Template for generating according to a specific power
*/
template <>
inline double PowX<-1>::generate(double x0, double x1, double R) {
return x0*pow(x1/x0, R);
}
/**
* Template for generating according to a specific power
*/
template <>
inline double PowX<-2>::generate(double x0, double x1, double R) {
return x0*x1/(x1 - R*(x1 - x0));
}
/**
* Template for generating according to a specific power
*/
template <>
inline double PowX<-3>::generate(double x0, double x1, double R) {
return x0*x1/std::sqrt(x1*x1 - R*(x1*x1 - x0*x0));
}
/** @endcond */
/** Class corresponding to functions of the form \f$(1-x)^N\f$
* with integer N. */
template <int N>
struct Pow1mX: public MathType {
/** The primitive function. */
static double primitive(double x) {
return -PowX<N>::primitive(1.0 - x);
}
/** Integrate function in a given interval. */
static double integrate(double x0, double x1) {
return PowX<N>::integrate(1.0 - x1, 1.0 - x0);
}
/** Sample a distribution in a given interval given a flat random
* number R in the interval ]0,1[. */
static double generate(double x0, double x1, double R) {
return 1.0 - PowX<N>::generate(1.0 - x1, 1.0 - x0, R);
}
};
/** Class corresponding to functions of the form \f$1/(x(1-x))\f$ */
struct InvX1mX: public MathType {
/** The primitive function. */
static double primitive(double x) {
return log(x/(1.0 - x));
}
/** Integrate function in a given interval. */
static double integrate(double x0, double x1) {
return log(x1*(1.0 - x0)/(x0*(1.0 - x1)));
}
/** Sample a distribution in a given interval given a flat random
* number R in the interval ]0,1[. */
static double generate(double x0, double x1, double R) {
double r = pow(x1*(1.0 - x0)/(x0*(1.0 - x1)), R)*x0/(1.0 - x0);
return r/(1.0 + r);
}
};
/** Class corresponding to functions of the form \f$e^x\f$ */
struct ExpX: public MathType {
/** The primitive function. */
static double primitive(double x) {
return exp(x);
}
/** Integrate function in a given interval. */
static double integrate(double x0, double x1) {
return exp(x1) - exp(x0);
}
/** Sample a distribution in a given interval given a flat random
* number R in the interval ]0,1[. */
static double generate(double x0, double x1, double R) {
return log(exp(x0) + R*(exp(x1) - exp(x0)));
}
};
/** Class corresponding to functions of the form \f$x^{N/D}\f$
* with integer N and D. */
template <int N, int D>
struct FracPowX: public MathType {
/** The primitive function. */
static double primitive(double x) {
double r = double(N)/double(D) + 1.0;
return pow(x, r)/r;
}
/** Integrate function in a given interval. */
static double integrate(double x0, double x1) {
return primitive(x1) - primitive(x0);
}
/** Sample a distribution in a given interval given a flat random
* number R in the interval ]0,1[. */
static double generate(double x0, double x1, double R) {
double r = double(N)/double(D) + 1.0;
return pow(primitive(x0) + R*integrate(x0, x1), 1.0/r);
}
};
}
}
}
#endif /* ThePEG_Math_H */
|