ROOT  6.06/09
Reference Guide
GSLMCIntegrator.cxx
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: Magdalena Slawinska 08/2007
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2007 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 // implementation file for class GSLMCIntegrator
26 // Author: Magdalena Slawinska
27 //
28 //
29 
30 #include "Math/IFunctionfwd.h"
31 #include "Math/IFunction.h"
32 #include "Math/Error.h"
33 #include <vector>
34 
36 
37 #include "Math/GSLMCIntegrator.h"
39 #include "GSLRngWrapper.h"
40 
41 #include <algorithm>
42 #include <functional>
43 #include <ctype.h> // need to use c version of tolower defined here
44 
45 
46 #include "gsl/gsl_monte_vegas.h"
47 #include "gsl/gsl_monte_miser.h"
48 #include "gsl/gsl_monte_plain.h"
49 
50 
51 
52 namespace ROOT {
53 namespace Math {
54 
55 
56 
57 // constructors
58 
59 // GSLMCIntegrator::GSLMCIntegrator():
60 // fResult(0),fError(0),fStatus(-1),
61 // fWorkspace(0),
62 // fFunction(0)
63 // {
64 // // constructor of GSL MCIntegrator.Vegas MC is set as default integration type
65 // //set random number generator
66 // fRng = new GSLRngWrapper();
67 // fRng->Allocate();
68 // // use the default options
69 // ROOT::Math::IntegratorMultiDimOptions opts; // this create the default options
70 // SetOptions(opts);
71 // }
72 
73 
74 GSLMCIntegrator::GSLMCIntegrator(MCIntegration::Type type, double absTol, double relTol, unsigned int calls):
75  fType(type),
76  fDim(0),
77  fCalls((calls > 0) ? calls : IntegratorMultiDimOptions::DefaultNCalls()),
78  fAbsTol((absTol >= 0) ? absTol : IntegratorMultiDimOptions::DefaultAbsTolerance() ),
79  fRelTol((relTol >= 0) ? relTol : IntegratorMultiDimOptions::DefaultRelTolerance() ),
80  fResult(0),fError(0),fStatus(-1),
81  fWorkspace(0),
82  fFunction(0)
83 {
84  // constructor of GSL MCIntegrator using enumeration as type
85  SetType(type);
86  //set random number generator
87  fRng = new GSLRngWrapper();
88  fRng->Allocate();
89  // use the default options for the needed extra parameters
90  // use the default options for the needed extra parameters
93  if (opts != 0) SetParameters( VegasParameters(*opts) );
94  }
95  else if (fType == MCIntegration::kMISER) {
97  if (opts != 0) SetParameters( MiserParameters(*opts) );
98  }
99 
100 }
101 
102 GSLMCIntegrator::GSLMCIntegrator(const char * type, double absTol, double relTol, unsigned int calls):
103  fDim(0),
104  fCalls(calls),
105  fAbsTol(absTol),
106  fRelTol(relTol),
107  fResult(0),fError(0),fStatus(-1),
108  fWorkspace(0),
109  fFunction(0)
110 {
111  // constructor of GSL MCIntegrator. Vegas MC is set as default integration type if type == 0
112  SetTypeName(type);
113 
114  //set random number generator
115  fRng = new GSLRngWrapper();
116  fRng->Allocate();
117  // use the default options for the needed extra parameters
118  if (fType == MCIntegration::kVEGAS) {
120  if (opts != 0) SetParameters( VegasParameters(*opts) );
121  }
122  else if (fType == MCIntegration::kMISER) {
124  if (opts != 0) SetParameters( MiserParameters(*opts) );
125  }
126 
127 }
128 
129 
130 
132 {
133  // delete workspace
134  if (fWorkspace) delete fWorkspace;
135  if (fRng != 0) delete fRng;
136  if (fFunction != 0) delete fFunction;
137  fRng = 0;
138 
139 }
140 
141 
142 // disable copy ctrs
143 
144 
147 {}
148 
150 
151 
152 
153 
154 
156 {
157  // method to set the a generic integration function
160  fDim = f.NDim();
161 }
162 
163 void GSLMCIntegrator::SetFunction( GSLMonteFuncPointer f, unsigned int dim, void * p )
164 {
165  // method to set the a generic integration function
168  fFunction->SetParams ( p );
169  fDim = dim;
170 }
171 
172 
173 
174 double GSLMCIntegrator::Integral(const double* a, const double* b)
175 {
176  // evaluate the Integral of a over the defined interval (a[],b[])
177  assert(fRng != 0);
178  gsl_rng* fr = fRng->Rng();
179  assert(fr != 0);
180  if (!CheckFunction()) return 0;
181 
182  // initialize by creating the right WS
183  // (if dimension and type are different than previous calculation)
184  DoInitialize();
185 
187  {
189  assert(ws != 0);
190  fStatus = gsl_monte_vegas_integrate( fFunction->GetFunc(), (double *) a, (double*) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
191  }
192  else if (fType == MCIntegration::kMISER)
193  {
195  assert(ws != 0);
196  fStatus = gsl_monte_miser_integrate( fFunction->GetFunc(), (double *) a, (double *) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
197  }
198  else if (fType == MCIntegration::kPLAIN)
199  {
201  assert(ws != 0);
202  fStatus = gsl_monte_plain_integrate( fFunction->GetFunc(), (double *) a, (double *) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
203  }
204  /**/
205  else
206  {
207 
208  fResult = 0;
209  fError = 0;
210  fStatus = -1;
211  std::cerr << "GSLIntegrator - Error: Unknown integration type" << std::endl;
212  throw std::exception();
213  }
214 
215  return fResult;
216 
217 }
218 
219 
220 double GSLMCIntegrator::Integral(const GSLMonteFuncPointer & f, unsigned int dim, double* a, double* b, void * p )
221 {
222  // evaluate the Integral for a function f over the defined interval (a[],b[])
223  SetFunction(f,dim,p);
224  return Integral(a,b);
225 }
226 
227 
228 /* to be added later
229  double GSLMCIntegrator::Integral(GSLMonteFuncPointer f, void * p, double* a, double* b)
230  {
231 
232  }
233 
234 */
235 //MCIntegration::Type GSLMCIntegrator::MCType() const {return fType;}
236 
237 /**
238  return the Result of the last Integral calculation
239 */
240 double GSLMCIntegrator::Result() const { return fResult; }
241 
242 /**
243  return the estimate of the absolute Error of the last Integral calculation
244 */
245 double GSLMCIntegrator::Error() const { return fError; }
246 
247 /**
248  return the Error Status of the last Integral calculation
249 */
250 int GSLMCIntegrator::Status() const { return fStatus; }
251 
252 
253 // setter for control Parameters (getters are not needed so far )
254 
255 /**
256  set the desired relative Error
257 */
258 void GSLMCIntegrator::SetRelTolerance(double relTol){ this->fRelTol = relTol; }
259 
260 /**
261  set the desired absolute Error
262 */
264 
266 
268 {
269  // create workspace according to the type
270  fType=type;
271  if (fWorkspace != 0) {
272  if (type == fWorkspace->Type() ) return;
273  delete fWorkspace; // delete because is a different type
274  fWorkspace = 0;
275  }
276  //create Workspace according to type
277  if (type == MCIntegration::kPLAIN) {
279  }
280  else if (type == MCIntegration::kMISER) {
282  }
283  else {
284  // default: use VEGAS
285  if (type != MCIntegration::kVEGAS) {
286  MATH_WARN_MSG("GSLMCIntegration","Invalid integration type : use Vegas as default");
288  }
290  }
291 }
292 
294 {
295  // set the integration type using a string
296  std::string typeName = (type!=0) ? type : "VEGAS";
297  if (type == 0) MATH_INFO_MSG("GSLMCIntegration::SetTypeName","use default Vegas integrator method");
298  std::transform(typeName.begin(), typeName.end(), typeName.begin(), (int(*)(int)) toupper );
299 
300  MCIntegration::Type integType = MCIntegration::kVEGAS; // default
301 
302  if (typeName == "PLAIN") {
303  integType = MCIntegration::kPLAIN;
304  }
305  else if (typeName == "MISER") {
306  integType = MCIntegration::kMISER;
307  }
308  else if (typeName != "VEGAS") {
309  MATH_WARN_MSG("GSLMCIntegration::SetTypeName","Invalid integration type : use Vegas as default");
310  }
311 
312  // create the fWorkspace object
313  if (integType != fType) SetType(integType);
314 }
315 
316 
318 {
319  // set integration mode for VEGAS method
321  {
323  assert(ws != 0);
324  if(mode == MCIntegration::kIMPORTANCE) ws->GetWS()->mode = GSL_VEGAS_MODE_IMPORTANCE;
325  else if(mode == MCIntegration::kSTRATIFIED) ws->GetWS()->mode = GSL_VEGAS_MODE_STRATIFIED;
326  else if(mode == MCIntegration::kIMPORTANCE_ONLY) ws->GetWS()->mode = GSL_VEGAS_MODE_IMPORTANCE_ONLY;
327  }
328 
329  else std::cerr << "Mode not matching integration type";
330 }
331 
333 {
334  // set integration options
335  SetTypeName(opt.Integrator().c_str() );
336  SetAbsTolerance( opt.AbsTolerance() );
337  SetRelTolerance( opt.RelTolerance() );
338  fCalls = opt.NCalls();
339 
340  //std::cout << fType << " " << MCIntegration::kVEGAS << std::endl;
341 
342  // specific options
343  ROOT::Math::IOptions * extraOpt = opt.ExtraOptions();
344  if (extraOpt) {
345  if (fType == MCIntegration::kVEGAS ) {
346  VegasParameters p(*extraOpt);
347  SetParameters(p);
348  }
349  else if (fType == MCIntegration::kMISER ) {
350  MiserParameters p(fDim); // if possible pass dimension
351  p = (*extraOpt);
352  SetParameters(p);
353  }
354  else {
355  MATH_WARN_MSG("GSLMCIntegrator::SetOptions","Invalid options set for the chosen integration type - ignore them");
356  }
357  }
358 }
359 
360 
362 {
363  // set method parameters
365  {
367  assert(ws != 0);
368  ws->SetParameters(p);
369  }
370  else
371  MATH_ERROR_MSG("GSLIntegrator::SetParameters"," Parameters not matching integration type");
372 }
373 
375 {
376  // set method parameters
378  {
380  assert(ws != 0);
381  ws->SetParameters(p);
382  }
383  else
384  MATH_ERROR_MSG("GSLIntegrator::SetParameters"," Parameters not matching integration type");
385 }
386 
387 
389 {
390  // initialize by setting integration type
391 
392  if (fWorkspace == 0) return;
393  if (fDim == fWorkspace->NDim() && fType == fWorkspace->Type() )
394  return; // can use previously existing ws
395 
396  // otherwise clear workspace
397  fWorkspace->Clear();
398  // and create a new one
399  fWorkspace->Init(fDim);
400 }
401 
402 
403 
404 //----------- methods specific for VEGAS
405 
407 {
408  // returns the error sigma from the last iteration of the VEGAS algorithm
410  {
412  assert (ws != 0);
413  return ws->GetWS()->sigma;
414  }
415  else
416  {
417  std::cerr << "Parameter not mathcing integration type";
418  return 0;
419  }
420 
421 }
422 
423 
424 /**
425 */
427 {
428  // returns chi-squared per degree of freedom for the estimate of the integral
430  {
432  assert(ws != 0);
433  return ws->GetWS()->chisq;
434  }
435  else
436  {
437  std::cerr << "Parameter not mathcing integration type";
438  return 0;
439  }
440 }
441 
442 
443 
445 {
446  // internal method to check validity of GSL function pointer
447 
448  if (fFunction && fFunction->GetFunc() ) return true;
449  MATH_ERROR_MSG("GSLMCIntegrator::CheckFunction","Function has not been specified");
450  return false;
451 }
452 
453 const char * GSLMCIntegrator::GetTypeName() const {
454  if (fType == MCIntegration::kVEGAS) return "VEGAS";
455  if (fType == MCIntegration::kMISER) return "MISER";
456  if (fType == MCIntegration::kPLAIN) return "PLAIN";
457  return "UNDEFINED";
458 }
459 
461  IOptions * extraOpts = ExtraOptions();
465  opt.SetNCalls(fCalls);
466  opt.SetWKSize(0);
467  opt.SetIntegrator(GetTypeName() );
468  return opt;
469 }
470 
472  if (!fWorkspace) return 0;
473  return fWorkspace->Options();
474 }
475 
476 
477 } // namespace Math
478 } // namespace ROOT
479 
480 
481 
ROOT::Math::IntegratorMultiDimOptions Options() const
get the option used for the integration
double Result() const
return the type of the integration used
GSLMCIntegrator(MCIntegration::Type type=MCIntegration::kVEGAS, double absTol=-1, double relTol=-1, unsigned int calls=0)
constructor of GSL MCIntegrator using all the default options
const double absTol
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
GSLMonteFunctionWrapper * fFunction
double Integral(const GSLMonteFuncPointer &f, unsigned int dim, double *a, double *b, void *p=0)
evaluate the Integral of a function f over the defined hypercube (a,b)
Type
enumeration specifying the integration types.
void SetWKSize(unsigned int size)
set workspace size
#define assert(cond)
Definition: unittest.h:542
void SetFuncPointer(GSLMonteFuncPointer f)
void SetNCalls(unsigned int calls)
set maximum number of function calls
structures collecting parameters for MISER multidimensional integration
Definition: MCParameters.h:76
TArc * a
Definition: textangle.C:12
void SetFunction(const IMultiGenFunction &f)
method to set the a generic integration function
#define MATH_WARN_MSG(loc, str)
Definition: Error.h:47
structures collecting parameters for VEGAS multidimensional integration FOr implementation of default...
Definition: MCParameters.h:45
double Error() const
return the estimate of the absolute Error of the last Integral calculation
void SetRelTolerance(double relTolerance)
set the desired relative Error
double ChiSqr()
returns chi-squared per degree of freedom for the estimate of the integral in the Vegas algorithm ...
IOptions * ExtraOptions() const
return extra options
const char * GetTypeName() const
return the name
virtual ~GSLMCIntegrator()
destructor
GSLMCIntegrationWorkspace * fWorkspace
GSLRngWrapper class to wrap gsl_rng structure.
Definition: GSLRngWrapper.h:25
void SetParameters(const struct VegasParameters &p)
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
#define MATH_INFO_MSG(loc, str)
Definition: Error.h:44
Numerical multi dimensional integration options.
void SetFunction(const FuncType &f)
Fill gsl function structure from a C++ Function class.
ROOT::R::TRInterface & r
Definition: Object.C:4
Interface (abstract) class for multi numerical integration It must be implemented by the concrete Int...
void SetIntegrator(const char *name)
set multi-dim integrator name
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
MCIntegration::Type fType
void SetParameters(const VegasParameters &p)
set default parameters for VEGAS method
unsigned int NCalls() const
maximum number of function calls
void SetOptions(const ROOT::Math::IntegratorMultiDimOptions &opt)
set the integration options
PyObject * fType
void SetType(MCIntegration::Type type)
set integration method
void SetTypeName(const char *typeName)
set integration method using a name instead of an enumeration
double f(double x)
GSLMCIntegrator & operator=(const GSLMCIntegrator &)
static ROOT::Math::IOptions * FindDefault(const char *name)
double RelTolerance() const
absolute tolerance
std::string Integrator() const
name of multi-dim integrator
int type
Definition: TGX11.cxx:120
int Status() const
return the Error Status of the last Integral calculation
double Sigma()
set parameters for PLAIN method
ROOT::Math::IOptions * ExtraOptions() const
get the specific options (for Vegas or Miser) in term of string- name
virtual ROOT::Math::IOptions * Options() const =0
retrieve option pointer corresponding to parameters create a new object to be managed by the user ...
Namespace for new Math classes and functions.
double(* GSLMonteFuncPointer)(double *, size_t, void *)
Class for adapting any multi-dimension C++ functor class to C function pointers used by GSL MonteCarl...
void SetGenerator(GSLRngWrapper *r)
set random number generator
void SetMode(MCIntegration::Mode mode)
set integration mode for VEGAS method The possible MODE are : MCIntegration::kIMPORTANCE (default) : ...
Generic interface for defining configuration options of a numerical algorithm.
Definition: IOptions.h:32
wrapper to a multi-dim function withtout derivatives for Monte Carlo multi-dimensional integration al...
virtual MCIntegration::Type Type() const =0
void SetAbsTolerance(double absTolerance)
set the desired absolute Error
virtual void Clear()
free the workspace deleting the GSL pointer
double AbsTolerance() const
non-static methods for retrivieng options
virtual bool Init(size_t dim)=0
initialize the workspace creating the GSL pointer if it is not there
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
void SetRelTolerance(double tol)
set the relative tolerance
void SetAbsTolerance(double tol)
non-static methods for setting options