CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
RandStudentT.cc
Go to the documentation of this file.
1//
2// -*- C++ -*-
3//
4// -----------------------------------------------------------------------
5// HEP Random
6// --- RandStudentT ---
7// class implementation file
8// -----------------------------------------------------------------------
9
10// =======================================================================
11// John Marraffino - Created: 12th May 1998
12// G.Cosmo - Fixed minor bug on inline definition for shoot()
13// methods : 20th Aug 1998
14// M Fischler - put and get to/from streams 12/13/04
15// M Fischler - put/get to/from streams uses pairs of ulongs when
16// + storing doubles avoid problems with precision
17// 4/14/05
18// =======================================================================
19
20#include <float.h>
21#include "CLHEP/Random/defs.h"
22#include "CLHEP/Random/RandStudentT.h"
23#include "CLHEP/Random/DoubConv.h"
24
25#include <cmath> // for std::log() std::exp()
26#include <iostream>
27#include <string>
28#include <vector>
29
30namespace CLHEP {
31
32std::string RandStudentT::name() const {return "RandStudentT";}
33HepRandomEngine & RandStudentT::engine() {return *localEngine;}
34
36{
37}
38
40 return fire( defaultA );
41}
42
43double RandStudentT::operator()( double a ) {
44 return fire( a );
45}
46
47double RandStudentT::shoot( double a ) {
48/******************************************************************
49 * *
50 * Student-t Distribution - Polar Method *
51 * *
52 ******************************************************************
53 * *
54 * The polar method of Box/Muller for generating Normal variates *
55 * is adapted to the Student-t distribution. The two generated *
56 * variates are not independent and the expected no. of uniforms *
57 * per variate is 2.5464. *
58 * *
59 ******************************************************************
60 * *
61 * FUNCTION : - tpol samples a random number from the *
62 * Student-t distribution with parameter a > 0. *
63 * REFERENCE : - R.W. Bailey (1994): Polar generation of random *
64 * variates with the t-distribution, Mathematics *
65 * of Computation 62, 779-781. *
66 * SUBPROGRAM : - ... (0,1)-Uniform generator *
67 * *
68 * *
69 * Implemented by F. Niederl, 1994 *
70 ******************************************************************/
71
72 double u,v,w;
73
74// Check for valid input value
75
76 if( a < 0.0) return (DBL_MAX);
77
78 do
79 {
80 u = 2.0 * HepRandom::getTheEngine()->flat() - 1.0;
81 v = 2.0 * HepRandom::getTheEngine()->flat() - 1.0;
82 }
83 while ((w = u * u + v * v) > 1.0);
84
85 return(u * std::sqrt( a * ( std::exp(- 2.0 / a * std::log(w)) - 1.0) / w));
86}
87
88void RandStudentT::shootArray( const int size, double* vect,
89 double a )
90{
91 for( double* v = vect; v != vect + size; ++v )
92 *v = shoot(a);
93}
94
96 const int size, double* vect,
97 double a )
98{
99 for( double* v = vect; v != vect + size; ++v )
100 *v = shoot(anEngine,a);
101}
102
103double RandStudentT::fire( double a ) {
104
105 double u,v,w;
106
107 do
108 {
109 u = 2.0 * localEngine->flat() - 1.0;
110 v = 2.0 * localEngine->flat() - 1.0;
111 }
112 while ((w = u * u + v * v) > 1.0);
113
114 return(u * std::sqrt( a * ( std::exp(- 2.0 / a * std::log(w)) - 1.0) / w));
115}
116
117void RandStudentT::fireArray( const int size, double* vect)
118{
119 for( double* v = vect; v != vect + size; ++v )
120 *v = fire(defaultA);
121}
122
123void RandStudentT::fireArray( const int size, double* vect,
124 double a )
125{
126 for( double* v = vect; v != vect + size; ++v )
127 *v = fire(a);
128}
129
130double RandStudentT::shoot( HepRandomEngine *anEngine, double a ) {
131
132 double u,v,w;
133
134 do
135 {
136 u = 2.0 * anEngine->flat() - 1.0;
137 v = 2.0 * anEngine->flat() - 1.0;
138 }
139 while ((w = u * u + v * v) > 1.0);
140
141 return(u * std::sqrt( a * ( std::exp(- 2.0 / a * std::log(w)) - 1.0) / w));
142}
143
144std::ostream & RandStudentT::put ( std::ostream & os ) const {
145 long pr=os.precision(20);
146 std::vector<unsigned long> t(2);
147 os << " " << name() << "\n";
148 os << "Uvec" << "\n";
149 t = DoubConv::dto2longs(defaultA);
150 os << defaultA << " " << t[0] << " " << t[1] << "\n";
151 os.precision(pr);
152 return os;
153#ifdef REMOVED
154 long pr=os.precision(20);
155 os << " " << name() << "\n";
156 os << defaultA << "\n";
157 os.precision(pr);
158 return os;
159#endif
160}
161
162std::istream & RandStudentT::get ( std::istream & is ) {
163 std::string inName;
164 is >> inName;
165 if (inName != name()) {
166 is.clear(std::ios::badbit | is.rdstate());
167 std::cerr << "Mismatch when expecting to read state of a "
168 << name() << " distribution\n"
169 << "Name found was " << inName
170 << "\nistream is left in the badbit state\n";
171 return is;
172 }
173 if (possibleKeywordInput(is, "Uvec", defaultA)) {
174 std::vector<unsigned long> t(2);
175 is >> defaultA >> t[0] >> t[1]; defaultA = DoubConv::longs2double(t);
176 return is;
177 }
178 // is >> defaultA encompassed by possibleKeywordInput
179 return is;
180}
181
182} // namespace CLHEP
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94
virtual double flat()=0
static HepRandomEngine * getTheEngine()
Definition: Random.cc:270
static void shootArray(const int size, double *vect, double a=1.0)
Definition: RandStudentT.cc:88
virtual ~RandStudentT()
Definition: RandStudentT.cc:35
std::string name() const
Definition: RandStudentT.cc:32
void fireArray(const int size, double *vect)
static double shoot()
HepRandomEngine & engine()
Definition: RandStudentT.cc:33
std::istream & get(std::istream &is)
std::ostream & put(std::ostream &os) const
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168