18#include "CLHEP/Random/defs.h"
19#include "CLHEP/Random/RandGamma.h"
20#include "CLHEP/Random/DoubConv.h"
21#include "CLHEP/Utility/thread_local.h"
37 return genGamma( anEngine, k, lambda );
42 return genGamma( anEngine, k, lambda );
46 return genGamma( localEngine.get(), k, lambda );
50 double k,
double lambda )
52 for(
double* v = vect; v != vect + size; ++v )
57 const int size,
double* vect,
58 double k,
double lambda )
60 for(
double* v = vect; v != vect + size; ++v )
61 *v =
shoot(anEngine,k,lambda);
66 for(
double* v = vect; v != vect + size; ++v )
67 *v =
fire(defaultK,defaultLambda);
71 double k,
double lambda )
73 for(
double* v = vect; v != vect + size; ++v )
78 double a,
double lambda ) {
84 double aa = -1.0, aaa = -1.0, b{0.}, c{0.}, d{0.}, e{0.}, r{0.}, s{0.}, si{0.}, ss{0.}, q0{0.};
85 constexpr double q1 = 0.0416666664, q2 = 0.0208333723, q3 = 0.0079849875,
86 q4 = 0.0015746717, q5 = -0.0003349403, q6 = 0.0003340332,
87 q7 = 0.0006053049, q8 = -0.0004701849, q9 = 0.0001710320,
88 a1 = 0.333333333, a2 = -0.249999949, a3 = 0.199999867,
89 a4 =-0.166677482, a5 = 0.142873973, a6 =-0.124385581,
90 a7 = 0.110368310, a8 = -0.112750886, a9 = 0.104089866,
91 e1 = 1.000000000, e2 = 0.499999994, e3 = 0.166666848,
92 e4 = 0.041664508, e5 = 0.008345522, e6 = 0.001353826,
95double gds{0.},p{0.},q{0.},t{0.},sign_u{0.},u{0.},v{0.},w{0.},x{0.};
96double v1{0.},v2{0.},v12{0.};
100 if( a <= 0.0 )
return (-1.0);
101 if( lambda <= 0.0 )
return (-1.0);
105 b = 1.0 + 0.36788794412 * a;
108 p = b * anEngine->
flat();
111 gds = std::exp(std::log(p) / a);
112 if (std::log(anEngine->
flat()) <= -gds)
return(gds/lambda);
116 gds = - std::log ((b - p) / a);
117 if (std::log(anEngine->
flat()) <= ((a - 1.0) * std::log(gds)))
return(gds/lambda);
128 d = 5.656854249 - 12.0 * s;
132 v1 = 2.0 * anEngine->
flat() - 1.0;
133 v2 = 2.0 * anEngine->
flat() - 1.0;
135 }
while ( v12 > 1.0 );
136 t = v1*std::sqrt(-2.0*std::log(v12)/v12);
139 if (t >= 0.0)
return(gds/lambda);
141 u = anEngine->
flat();
142 if (d * u <= t * t * t)
return(gds/lambda);
148 q0 = ((((((((q9 * r + q8) * r + q7) * r + q6) * r + q5) * r + q4) *
149 r + q3) * r + q2) * r + q1) * r;
160 b = 1.654 + 0.0076 * ss;
161 si = 1.68 / s + 0.275;
162 c = 0.062 / s + 0.024;
167 b = 0.463 + s - 0.178 * ss;
169 c = 0.195 / s - 0.079 + 0.016 * s;
175 if (std::fabs(v) > 0.25)
177 q = q0 - s * t + 0.25 * t * t + (ss + ss) * std::log(1.0 + v);
181 q = q0 + 0.5 * t * t * ((((((((a9 * v + a8) * v + a7) * v + a6) *
182 v + a5) * v + a4) * v + a3) * v + a2) * v + a1) * v;
184 if (std::log(1.0 - u) <= q)
return(gds/lambda);
191 e = -std::log(anEngine->
flat());
192 u = anEngine->
flat();
194 sign_u = (u > 0)? 1.0 : -1.0;
195 t = b + (e * si) * sign_u;
197 while (t <= -0.71874483771719);
199 if (std::fabs(v) > 0.25)
201 q = q0 - s * t + 0.25 * t * t + (ss + ss) * std::log(1.0 + v);
205 q = q0 + 0.5 * t * t * ((((((((a9 * v + a8) * v + a7) * v + a6) *
206 v + a5) * v + a4) * v + a3) * v + a2) * v + a1) * v;
208 if (q <= 0.0)
continue;
211 w = std::exp(q) - 1.0;
215 w = ((((((e7 * q + e6) * q + e5) * q + e4) * q + e3) * q + e2) *
218 if ( c * u * sign_u <= w * std::exp(e - 0.5 * t * t))
228 long pr=os.precision(20);
229 std::vector<unsigned long> t(2);
230 os <<
" " <<
name() <<
"\n";
231 os <<
"Uvec" <<
"\n";
233 os << defaultK <<
" " << t[0] <<
" " << t[1] <<
"\n";
235 os << defaultLambda <<
" " << t[0] <<
" " << t[1] <<
"\n";
239 long pr=os.precision(20);
240 os <<
" " <<
name() <<
"\n";
241 os << defaultK <<
" " << defaultLambda <<
"\n";
250 if (inName !=
name()) {
251 is.clear(std::ios::badbit | is.rdstate());
252 std::cerr <<
"Mismatch when expecting to read state of a "
253 <<
name() <<
" distribution\n"
254 <<
"Name found was " << inName
255 <<
"\nistream is left in the badbit state\n";
259 std::vector<unsigned long> t(2);
static double longs2double(const std::vector< unsigned long > &v)
static std::vector< unsigned long > dto2longs(double d)
static HepRandomEngine * getTheEngine()
HepRandomEngine & engine()
static void shootArray(const int size, double *vect, double k=1.0, double lambda=1.0)
std::ostream & put(std::ostream &os) const
void fireArray(const int size, double *vect)
std::istream & get(std::istream &is)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)