Logo ROOT  
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 <vector>
34#include <stdlib.h>
35#include <cassert>
36
37
38#include "Math/GSLQuasiRandom.h"
39#include "GSLQRngWrapper.h"
40
41//#include <iostream>
42
43namespace ROOT {
44namespace Math {
45
46
47
48
49
50 // default constructor (need to call set type later)
52 fQRng(0 )
53 { }
54
55 // constructor from external rng
56 // internal generator will be managed or not depending on
57 // how the GSLQRngWrapper is created
59 fQRng(new GSLQRngWrapper(*rng) )
60 {}
61
62 // copy constructor
64 fQRng(new GSLQRngWrapper(*eng.fQRng) )
65 {}
66
68 // destructor : call terminate if not yet called
69 if (fQRng) Terminate();
70 }
71
72 // assignment operator
74 if (this == &eng) return *this;
75 if (fQRng)
76 *fQRng = *eng.fQRng;
77 else
78 fQRng = new GSLQRngWrapper(*eng.fQRng);
79 return *this;
80 }
81
82
83 void GSLQuasiRandomEngine::Initialize(unsigned int dimension) {
84 // initialize the generator by allocating the GSL object
85 // if type was not passed create with default generator
86 if (!fQRng) fQRng = new GSLQRngWrapper();
87 fQRng->Allocate(dimension);
88 }
89
91 // terminate the generator by freeing the GSL object
92 if (!fQRng) return;
93 fQRng->Free();
94 delete fQRng;
95 fQRng = 0;
96 }
97
98
100 // generate next point in the quasi random sequence. The generate number x is 0 < x < 1
101 // with 0 and 1 excluded
102 // This method should be called only if dimension == 1
103 assert(fQRng->Dimension() == 1);
104 double x;
105 gsl_qrng_get(fQRng->Rng(), &x );
106 return x;
107 }
108
110 // generate next point in the quasi random sequence. The generate number x is 0 < x < 1
111 // with 0 and 1 excluded
112 int status = gsl_qrng_get(fQRng->Rng(), x );
113 return (status == 0);
114 }
115
116 bool GSLQuasiRandomEngine::Skip(unsigned int n) const {
117 // throw away the next n random numbers
118 std::vector<double> xtmp(fQRng->Dimension() );
119 int status = 0;
120 for (unsigned int i = 0; i < n; ++i ) {
121 status |= gsl_qrng_get(fQRng->Rng(), &xtmp[0] );
122 }
123 return status == 0;
124 }
125
126 bool GSLQuasiRandomEngine::GenerateArray(double * begin, double * end ) const {
127 // generate array of randoms betweeen 0 and 1. 0 is excluded
128 // specialization for double * (to be faster)
129 int status = 0;
130 for ( double * itr = begin; itr != end; itr+=fQRng->Dimension() ) {
131 status |= gsl_qrng_get(fQRng->Rng(), itr );
132 }
133 return status == 0;
134 }
135
136
137 std::string GSLQuasiRandomEngine::Name() const {
138 //////////////////////////////////////////////////////////////////////////
139
140 assert (fQRng != 0);
141 assert(fQRng->Rng() != 0);
142 const char * name = gsl_qrng_name( fQRng->Rng() );
143 if (!name) return std::string();
144 return std::string( name);
145 }
146
147 unsigned int GSLQuasiRandomEngine::Size() const {
148 //////////////////////////////////////////////////////////////////////////
149
150 assert (fQRng != 0);
151 return gsl_qrng_size( fQRng->Rng() );
152 }
153
154 unsigned int GSLQuasiRandomEngine::NDim() const {
155 //////////////////////////////////////////////////////////////////////////
156
157 assert (fQRng != 0);
158 return fQRng->Dimension();
159 }
160
161
162
163
164 //----------------------------------------------------
165 // generators
166 //----------------------------------------------------
167
168 //----------------------------------------------------
169 // generator based on Sobol sequence
171 {
172 SetType(new GSLQRngWrapper(gsl_qrng_sobol));
173 }
174
175
176 // generator based on Bratley-Fox,Niederreiter sequence
178 {
179 SetType(new GSLQRngWrapper(gsl_qrng_niederreiter_2) );
180 }
181
182
183
184
185
186} // namespace Math
187} // namespace ROOT
188
189
190
char name[80]
Definition: TGX11.cxx:109
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21