Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
IntegratorMultiDim.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: M. Slawinska 08/2007
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2007 , LCG ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header source file for class IntegratorMultiDim
12 
13 
14 #ifndef ROOT_Math_IntegratorMultiDim
15 #define ROOT_Math_IntegratorMultiDim
16 
17 
18 #include "Math/IFunctionfwd.h"
19 
21 
22 #include "Math/IntegratorOptions.h"
23 
24 #include "Math/VirtualIntegrator.h"
25 
26 #ifndef __CINT__
27 
28 #include "Math/WrappedFunction.h"
29 
30 #endif
31 
32 #include <memory>
33 
34 namespace ROOT {
35 namespace Math {
36 
37 //___________________________________________________________________________________________
38 /**
39  User class for performing multidimensional integration
40 
41  By default uses adaptive multi-dimensional integration using the algorithm from Genz Mallik
42  implemented in the class ROOT::Math::AdaptiveIntegratorMultiDim otherwise it can uses via the
43  plug-in manager the MC integration class (ROOT::Math::GSLMCIntegration) from MathMore.
44 
45  @ingroup Integration
46 
47 
48  */
49 
50 class IntegratorMultiDim {
51 
52 public:
53 
54  typedef IntegrationMultiDim::Type Type; // for the enumerations defining the types
55 
56 
57  /** Generic constructor of multi dimensional Integrator. By default uses the Adaptive integration method
58 
59  @param type integration type (adaptive, MC methods, etc..)
60  @param absTol desired absolute Error
61  @param relTol desired relative Error
62  @param size maximum number of sub-intervals
63 
64  In case no parameter values are passed the default ones used in IntegratorMultiDimOptions are used
65  */
66  explicit
67  IntegratorMultiDim(IntegrationMultiDim::Type type = IntegrationMultiDim::kDEFAULT, double absTol = -1, double relTol = -1, unsigned int ncall = 0) :
68  fIntegrator(0)
69  {
70  fIntegrator = CreateIntegrator(type, absTol, relTol, ncall);
71  }
72 
73  /** Generic Constructor of multi dimensional Integrator passing a function. By default uses the adaptive integration method
74 
75  @param f integration function (multi-dim interface)
76  @param type integration type (adaptive, MC methods, etc..)
77  @param absTol desired absolute Error
78  @param relTol desired relative Error
79  @param ncall number of function calls (apply only to MC integratioon methods)
80  */
81  explicit
82  IntegratorMultiDim(const IMultiGenFunction &f, IntegrationMultiDim::Type type = IntegrationMultiDim::kDEFAULT, double absTol = -1, double relTol = -1, unsigned int ncall = 0) :
83  fIntegrator(0)
84  {
85  fIntegrator = CreateIntegrator(type, absTol, relTol, ncall);
86  SetFunction(f);
87  }
88 
89  // remove template constructor since is ambigous
90 
91  /** Template Constructor of multi dimensional Integrator passing a generic function. By default uses the adaptive integration method
92 
93  @param f integration function (generic function implementin operator()(const double *)
94  @param dim function dimension
95  @param type integration type (adaptive, MC methods, etc..)
96  @param absTol desired absolute Error
97  @param relTol desired relative Error
98  @param ncall number of function calls (apply only to MC integratioon methods)
99  */
100 // this is ambigous
101 // template<class Function>
102 // IntegratorMultiDim(Function & f, unsigned int dim, IntegrationMultiDim::Type type = IntegrationMultiDim::kADAPTIVE, double absTol = 1.E-9, double relTol = 1E-6, unsigned int ncall = 100000) {
103 // fIntegrator = CreateIntegrator(type, absTol, relTol, ncall);
104 // SetFunction(f, dim);
105 // }
106 
107  /// destructor
108  virtual ~IntegratorMultiDim() {
109  if (fIntegrator) delete fIntegrator;
110  }
111 
112 
113  // disable copy constructur and assignment operator
114 
115 private:
116  IntegratorMultiDim(const IntegratorMultiDim &) : fIntegrator(0), fFunc(nullptr) {}
117  IntegratorMultiDim & operator=(const IntegratorMultiDim &) { return *this; }
118 
119 public:
120 
121 
122  /**
123  evaluate the integral with the previously given function between xmin[] and xmax[]
124  */
125  double Integral(const double* xmin, const double * xmax) {
126  return fIntegrator == 0 ? 0 : fIntegrator->Integral(xmin,xmax);
127  }
128 
129  /// evaluate the integral passing a new function
130  double Integral(const IMultiGenFunction &f, const double* xmin, const double * xmax) {
131  SetFunction(f);
132  return Integral(xmin,xmax);
133  }
134 
135  /// evaluate the integral passing a new generic function
136  template<class Function>
137  double Integral(Function & f , unsigned int dim, const double* xmin, const double * xmax) {
138  SetFunction<Function>(f,dim);
139  return Integral(xmin, xmax);
140  }
141 
142 
143  /**
144  set integration function using a generic function implementing the operator()(double *x)
145  The dimension of the function is in this case required
146  */
147  template <class Function>
148  void SetFunction(Function & f, unsigned int dim) {
149  fFunc.reset(new WrappedMultiFunction<Function &> (f, dim) );
150  fIntegrator->SetFunction(*fFunc);
151  }
152 
153  // set the function without cloning it
154  void SetFunction(const IMultiGenFunction &f) {
155  if (fIntegrator) fIntegrator->SetFunction(f);
156  }
157 
158  /// return result of last integration
159  double Result() const { return fIntegrator == 0 ? 0 : fIntegrator->Result(); }
160 
161  /// return integration error
162  double Error() const { return fIntegrator == 0 ? 0 : fIntegrator->Error(); }
163 
164  /// return the Error Status of the last Integral calculation
165  int Status() const { return fIntegrator == 0 ? -1 : fIntegrator->Status(); }
166 
167  // return number of function evaluations in calculating the integral
168  //unsigned int NEval() const { return fNEval; }
169 
170  /// set the relative tolerance
171  void SetRelTolerance(double relTol) { if (fIntegrator) fIntegrator->SetRelTolerance(relTol); }
172 
173  /// set absolute tolerance
174  void SetAbsTolerance(double absTol) { if (fIntegrator) fIntegrator->SetAbsTolerance(absTol); }
175 
176  /// set the options
177  void SetOptions(const ROOT::Math::IntegratorMultiDimOptions & opt) { if (fIntegrator) fIntegrator->SetOptions(opt); }
178 
179  /// retrieve the options
180  ROOT::Math::IntegratorMultiDimOptions Options() const { return (fIntegrator) ? fIntegrator->Options() : IntegratorMultiDimOptions(); }
181 
182  /// return a pointer to integrator object
183  VirtualIntegratorMultiDim * GetIntegrator() { return fIntegrator; }
184 
185  /// return name of integrator
186  std::string Name() const { return (fIntegrator) ? Options().Integrator() : std::string(""); }
187 
188  /// static function to get the enumeration from a string
189  static IntegrationMultiDim::Type GetType(const char * name);
190 
191  /// static function to get a string from the enumeration
192  static std::string GetName(IntegrationMultiDim::Type);
193 
194 protected:
195 
196  VirtualIntegratorMultiDim * CreateIntegrator(IntegrationMultiDim::Type type , double absTol, double relTol, unsigned int ncall);
197 
198  private:
199 
200  VirtualIntegratorMultiDim * fIntegrator; // pointer to multi-dimensional integrator base class
201  std::unique_ptr<IMultiGenFunction> fFunc; // pointer to owned function
202 
203 
204 };
205 
206 }//namespace Math
207 }//namespace ROOT
208 
209 #endif /* ROOT_Math_IntegratorMultiDim */