Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
GSLQuasiRandom.cxx
Go to the documentation of this file.
1// @(#)root/mathmore:$Id$
2// Authors: L. Moneta, A. Zsenei 08/2005
3
4 /**********************************************************************
5 * *
6 * Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU General Public License *
10 * as published by the Free Software Foundation; either version 2 *
11 * of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this library (see file COPYING); if not, write *
20 * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21 * 330, Boston, MA 02111-1307 USA, or contact the author. *
22 * *
23 **********************************************************************/
24//
25//
26// Created by: moneta at Sun Nov 21 :26:03 2004
27//
28// Last update: Sun Nov 21 16:26:03 2004
29//
30
31// need to be included later
32#include <ctime>
33#include <vector>
34#include <cstdlib>
35#include <cassert>
36
37#include "Math/GSLQuasiRandom.h"
38#include "GSLQRngWrapper.h"
39
40
41namespace ROOT {
42namespace Math {
43
44
45
46
47
48 // default constructor (need to call set type later)
50 fQRng(0 )
51 { }
52
53 // constructor from external rng
54 // internal generator will be managed or not depending on
55 // how the GSLQRngWrapper is created
57 fQRng(new GSLQRngWrapper(*rng) )
58 {}
59
60 // copy constructor
62 fQRng(new GSLQRngWrapper(*eng.fQRng) )
63 {}
64
66 // destructor : call terminate if not yet called
67 if (fQRng) Terminate();
68 }
69
70 // assignment operator
72 if (this == &eng) return *this;
73 if (fQRng)
74 *fQRng = *eng.fQRng;
75 else
76 fQRng = new GSLQRngWrapper(*eng.fQRng);
77 return *this;
78 }
79
80
81 void GSLQuasiRandomEngine::Initialize(unsigned int dimension) {
82 // initialize the generator by allocating the GSL object
83 // if type was not passed create with default generator
84 if (!fQRng) fQRng = new GSLQRngWrapper();
85 fQRng->Allocate(dimension);
86 }
87
89 // terminate the generator by freeing the GSL object
90 if (!fQRng) return;
91 fQRng->Free();
92 delete fQRng;
93 fQRng = 0;
94 }
95
96
98 // generate next point in the quasi random sequence. The generate number x is 0 < x < 1
99 // with 0 and 1 excluded
100 // This method should be called only if dimension == 1
101 assert(fQRng->Dimension() == 1);
102 double x;
103 gsl_qrng_get(fQRng->Rng(), &x );
104 return x;
105 }
106
108 // generate next point in the quasi random sequence. The generate number x is 0 < x < 1
109 // with 0 and 1 excluded
110 int status = gsl_qrng_get(fQRng->Rng(), x );
111 return (status == 0);
112 }
113
114 bool GSLQuasiRandomEngine::Skip(unsigned int n) const {
115 // throw away the next n random numbers
116 std::vector<double> xtmp(fQRng->Dimension() );
117 int status = 0;
118 for (unsigned int i = 0; i < n; ++i ) {
119 status |= gsl_qrng_get(fQRng->Rng(), &xtmp[0] );
120 }
121 return status == 0;
122 }
123
124 bool GSLQuasiRandomEngine::GenerateArray(double * begin, double * end ) const {
125 // generate array of randoms betweeen 0 and 1. 0 is excluded
126 // specialization for double * (to be faster)
127 int status = 0;
128 for ( double * itr = begin; itr != end; itr+=fQRng->Dimension() ) {
129 status |= gsl_qrng_get(fQRng->Rng(), itr );
130 }
131 return status == 0;
132 }
133
134
135 std::string GSLQuasiRandomEngine::Name() const {
136 //////////////////////////////////////////////////////////////////////////
137
138 assert (fQRng != 0);
139 assert(fQRng->Rng() != 0);
140 const char * name = gsl_qrng_name( fQRng->Rng() );
141 if (!name) return std::string();
142 return std::string( name);
143 }
144
145 unsigned int GSLQuasiRandomEngine::Size() const {
146 //////////////////////////////////////////////////////////////////////////
147
148 assert (fQRng != 0);
149 return gsl_qrng_size( fQRng->Rng() );
150 }
151
152 unsigned int GSLQuasiRandomEngine::NDim() const {
153 //////////////////////////////////////////////////////////////////////////
154
155 assert (fQRng != 0);
156 return fQRng->Dimension();
157 }
158
159
160
161
162 //----------------------------------------------------
163 // generators
164 //----------------------------------------------------
165
166 //----------------------------------------------------
167 // generator based on Sobol sequence
169 {
170 SetType(new GSLQRngWrapper(gsl_qrng_sobol));
171 }
172
173
174 // generator based on Bratley-Fox,Niederreiter sequence
176 {
177 SetType(new GSLQRngWrapper(gsl_qrng_niederreiter_2) );
178 }
179
180
181
182
183
184} // namespace Math
185} // namespace ROOT
186
187
188
char name[80]
Definition TGX11.cxx:110
GSLQRngWrapper class to wrap gsl_qrng structure.
void Allocate(unsigned int dimension)
unsigned int Dimension() const
GSLQuasiRandomEngine Base class for all GSL quasi random engines, normally user instantiate the deriv...
std::string Name() const
return name of generator
void SetType(GSLQRngWrapper *r)
internal method used by the derived class to set the type of generators
GSLQuasiRandomEngine & operator=(const GSLQuasiRandomEngine &eng)
Assignment operator : make a deep copy of the contained GSL generator.
void Initialize(unsigned int dimension)
initialize the generator giving the dimension of the sequence If no rng is present the default one ba...
bool GenerateArray(double *begin, double *end) const
Generate an array of quasi random numbers The iterators points to the random numbers.
virtual ~GSLQuasiRandomEngine()
call Terminate()
void Terminate()
delete pointer to contained rng
GSLQuasiRandomEngine()
default constructor.
unsigned int NDim() const
return the dimension of generator
unsigned int Size() const
return the state size of generator
double operator()() const
Generate a random number between ]0,1[.
bool Skip(unsigned int n) const
Skip the next n random numbers.
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.