Logo ROOT   6.08/07
Reference Guide
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 <time.h>
33 #include <stdlib.h>
34 #include <cassert>
35 
36 
37 #include "Math/GSLQuasiRandom.h"
38 #include "GSLQRngWrapper.h"
39 
40 //#include <iostream>
41 
42 namespace ROOT {
43 namespace Math {
44 
45 
46 
47 
48 
49  // default constructor (need to call set type later)
51  fQRng(0 )
52  { }
53 
54  // constructor from external rng
55  // internal generator will be managed or not depending on
56  // how the GSLQRngWrapper is created
58  fQRng(new GSLQRngWrapper(*rng) )
59  {}
60 
61  // copy constructor
63  fQRng(new GSLQRngWrapper(*eng.fQRng) )
64  {}
65 
67  // destructor : call terminate if not yet called
68  if (fQRng) Terminate();
69  }
70 
71  // assignment operator
73  if (this == &eng) return *this;
74  if (fQRng)
75  *fQRng = *eng.fQRng;
76  else
77  fQRng = new GSLQRngWrapper(*eng.fQRng);
78  return *this;
79  }
80 
81 
82  void GSLQuasiRandomEngine::Initialize(unsigned int dimension) {
83  // initialize the generator by allocating the GSL object
84  // if type was not passed create with default generator
85  if (!fQRng) fQRng = new GSLQRngWrapper();
86  fQRng->Allocate(dimension);
87  }
88 
90  // terminate the generator by freeing the GSL object
91  if (!fQRng) return;
92  fQRng->Free();
93  delete fQRng;
94  fQRng = 0;
95  }
96 
97 
99  // generate next point in the quasi random sequence. The generate number x is 0 < x < 1
100  // with 0 and 1 excluded
101  // This method should be called only if dimension == 1
102  assert(fQRng->Dimension() == 1);
103  double x;
104  gsl_qrng_get(fQRng->Rng(), &x );
105  return x;
106  }
107 
108  bool GSLQuasiRandomEngine::operator() (double * x) const {
109  // generate next point in the quasi random sequence. The generate number x is 0 < x < 1
110  // with 0 and 1 excluded
111  int status = gsl_qrng_get(fQRng->Rng(), x );
112  return (status == 0);
113  }
114 
115  bool GSLQuasiRandomEngine::Skip(unsigned int n) const {
116  // throw away the next n random numbers
117  std::vector<double> xtmp(fQRng->Dimension() );
118  int status = 0;
119  for (unsigned int i = 0; i < n; ++i ) {
120  status |= gsl_qrng_get(fQRng->Rng(), &xtmp[0] );
121  }
122  return status == 0;
123  }
124 
125  bool GSLQuasiRandomEngine::GenerateArray(double * begin, double * end ) const {
126  // generate array of randoms betweeen 0 and 1. 0 is excluded
127  // specialization for double * (to be faster)
128  int status = 0;
129  for ( double * itr = begin; itr != end; itr+=fQRng->Dimension() ) {
130  status |= gsl_qrng_get(fQRng->Rng(), itr );
131  }
132  return status == 0;
133  }
134 
135 
136  std::string GSLQuasiRandomEngine::Name() const {
137  //////////////////////////////////////////////////////////////////////////
138 
139  assert (fQRng != 0);
140  assert(fQRng->Rng() != 0);
141  const char * name = gsl_qrng_name( fQRng->Rng() );
142  if (!name) return std::string();
143  return std::string( name);
144  }
145 
146  unsigned int GSLQuasiRandomEngine::Size() const {
147  //////////////////////////////////////////////////////////////////////////
148 
149  assert (fQRng != 0);
150  return gsl_qrng_size( fQRng->Rng() );
151  }
152 
153  unsigned int GSLQuasiRandomEngine::NDim() const {
154  //////////////////////////////////////////////////////////////////////////
155 
156  assert (fQRng != 0);
157  return fQRng->Dimension();
158  }
159 
160 
161 
162 
163  //----------------------------------------------------
164  // generators
165  //----------------------------------------------------
166 
167  //----------------------------------------------------
168  // generator based on Sobol sequence
170  {
171  SetType(new GSLQRngWrapper(gsl_qrng_sobol));
172  }
173 
174 
175  // generator based on Bratley-Fox,Niederreiter sequence
177  {
178  SetType(new GSLQRngWrapper(gsl_qrng_niederreiter_2) );
179  }
180 
181 
182 
183 
184 
185 } // namespace Math
186 } // namespace ROOT
187 
188 
189 
unsigned int NDim() const
return the dimension of generator
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
void Allocate(unsigned int dimension)
GSLQuasiRandomEngine & operator=(const GSLQuasiRandomEngine &eng)
Assignment operator : make a deep copy of the contained GSL generator.
void SetType(GSLQRngWrapper *r)
internal method used by the derived class to set the type of generators
bool GenerateArray(double *begin, double *end) const
Generate an array of quasi random numbers The iterators points to the random numbers.
Double_t x[n]
Definition: legend1.C:17
void Initialize(unsigned int dimension)
initialize the generator giving the dimension of the sequence If no rng is present the default one ba...
unsigned int Size() const
return the state size of generator
virtual ~GSLQuasiRandomEngine()
call Terminate()
std::string Name() const
return name of generator
bool Skip(unsigned int n) const
Skip the next n random numbers.
unsigned int Dimension() const
void Terminate()
delete pointer to contained rng
Namespace for new Math classes and functions.
GSLQRngWrapper class to wrap gsl_qrng structure.
GSLQuasiRandomEngine Base class for all GSL quasi random engines, normally user instantiate the deriv...
GSLQuasiRandomEngine()
default constructor.
double operator()() const
Generate a random number between ]0,1[.
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109