Logo ROOT   6.13/01
Reference Guide
IntegratorOptions.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Fri Aug 15 2008
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2008 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 #include "Math/IntegratorOptions.h"
12 #include "Math/Integrator.h"
14 #include "Math/GenAlgoOptions.h"
15 
16 #include "RConfigure.h"
17 
18 #include <algorithm>
19 #include <functional>
20 #include <ctype.h> // need to use c version of tolower defined here
21 
22 #include <map>
23 
24 namespace ROOT {
25 
26 namespace Math {
27 
28  // eventually could take values from /etc/system.rootrc
29 
30 namespace IntegOneDim {
31 
32 #ifdef R__HAS_MATHMORE
34 #else
35  static int gDefaultIntegrator = IntegrationOneDim::kGAUSS;
36 #endif
37  static double gDefaultAbsTolerance = 1.E-09;
38  static double gDefaultRelTolerance = 1.E-09;
39  static unsigned int gDefaultWKSize = 1000;
40  static unsigned int gDefaultNPointsLegendre = 10;
41  static unsigned int gDefaultNPointsGSLAdaptive = 3; // corresponds to 31 points
43 
44 
45 }
46 
47 namespace IntegMultiDim {
48 
50  // by default do not use absolute tolerance in AdaptiveIntegration multidim.
51  // If an absolute tolerance is given integration of shar peaks often failed
52  static double gDefaultAbsTolerance = 0.0;
53  static double gDefaultRelTolerance = 1.E-09;
54  static unsigned int gDefaultWKSize = 100000;
55  static unsigned int gDefaultNCalls = 100000;
56 
57 
58 }
59 
60 
61 // some utility functions
62 
63 namespace IntegOptionsUtil {
64 
65 
66  // traits for the specific methods 1D - ND
67  template<class OptionType>
68  struct OptionTrait {
69  static int N() { return 0; }
70  static int N(const OptionType & ) { return 0; }
71  static const char * DescriptionOfN() {return 0; }
72  };
73  template<>
74  struct OptionTrait<IntegratorOneDimOptions> {
75  typedef IntegratorOneDimOptions OptType;
76  static int N() { return OptType::DefaultNPoints(); }
77  static int N(const OptType & opt) { return opt.NPoints(); }
78  static const char * DescriptionOfN() {return "Rule (Npoints)";}
79  };
80  template<>
81  struct OptionTrait<IntegratorMultiDimOptions> {
82  typedef IntegratorMultiDimOptions OptType;
83  static int N() { return OptType::DefaultNCalls(); }
84  static int N(const OptType & opt) { return opt.NCalls(); }
85  static const char * DescriptionOfN() {return "(max) function calls";}
86  };
87 
88 
89  //print option values (not the default ones)
90  template <class OptionType>
91  void Print(std::ostream & os,const OptionType & opt) {
92  //print all the options
93  os << std::setw(25) << "Integrator Type" << " : " << std::setw(15) << opt.Integrator() << std::endl;
94  os << std::setw(25) << "Absolute tolerance" << " : " << std::setw(15) << opt.AbsTolerance() << std::endl;
95  os << std::setw(25) << "Relative tolerance" << " : " << std::setw(15) << opt.RelTolerance() << std::endl;
96  os << std::setw(25) << "Workspace size" << " : " << std::setw(15) << opt.WKSize() << std::endl;
97  typedef OptionTrait<OptionType> OPT;
98  os << std::setw(25) << OPT::DescriptionOfN() << " : " << std::setw(15) << OPT::N(opt) << std::endl;
99  if (opt.ExtraOptions()) {
100  os << opt.Integrator() << " specific options :" << std::endl;
101  opt.ExtraOptions()->Print(os);
102  }
103  }
104 
105 
106  /// print default options
107  template <class OptionType>
108  void PrintDefault(const char * name, std::ostream & os) {
109  //print default options
110  std::string integName = (name != 0) ? name : OptionType::DefaultIntegrator();
111  os << "Default options for numerical integrator " << integName << " : " << std::endl;
112  os << std::setw(25) << "Absolute tolerance" << " : " << std::setw(15) << OptionType::DefaultAbsTolerance() << std::endl;
113  os << std::setw(25) << "Relative tolerance" << " : " <<std::setw(15) << OptionType::DefaultRelTolerance() << std::endl;
114  os << std::setw(25) << "Workspace size" << " : " << std::setw(15) << OptionType::DefaultWKSize() << std::endl;
115  typedef OptionTrait<OptionType> OPT;
116  os << std::setw(25) << OPT::DescriptionOfN() << " : " << std::setw(15) << OPT::N() << std::endl;
117  IOptions * opts = GenAlgoOptions::FindDefault(integName.c_str());
118  if (opts) opts->Print(os);
119  }
120 
121 }
122 
123 
124 /// constructor (protected) to avoid user creating this class
126  fIntegType(-1),
127  fWKSize(0), fNCalls(0),
128  fAbsTolerance(0), fRelTolerance(0),
129  fExtraOptions(0)
130 {}
131 
133  // copy constructor
134  (*this) = opt;
135 }
136 
138  // assignment operator
139  if (this == &opt) return *this; // self assignment
140  fWKSize = opt.fWKSize;
141  fNCalls = opt.fNCalls;
144  fIntegType = opt.fIntegType;
145 
146 // std::cout << " copy options for " << fIntegName << std::endl;
147 // std::cout << fExtraOptions << std::endl;
148 // if (fExtraOptions) fExtraOptions->Print(std::cout);
149 
150 // std::cout << opt.fExtraOptions << std::endl;
151 // if (opt.fExtraOptions) (opt.fExtraOptions)->Print(std::cout);
152 
153 
154 
155  ClearExtra();
156  if (opt.fExtraOptions) fExtraOptions = (opt.fExtraOptions)->Clone();
157  return *this;
158 }
159 
160 
162  // delete extra options
163  if (fExtraOptions) delete fExtraOptions;
164  fExtraOptions = 0;
165 }
166 
168  // delete extra options
169  ClearExtra();
170  fExtraOptions = opt.Clone();
171 }
172 
173 
174 
175 // one dim specific methods
176 
177 // implementation of non-static methods
178 
181 {
187 
188  fExtraOptions = opts; // N.B. ownership of pointer is given to the class !
189 
190  // check the default options if opts = 0
191  if (!fExtraOptions) {
192  std::string igname = DefaultIntegrator();
193  IOptions * gopts = FindDefault( igname.c_str() );
194  if (gopts) fExtraOptions = gopts->Clone();
195  }
196 }
197 
198 void IntegratorOneDimOptions::SetIntegrator(const char * algo ) {
199  // set the default 1D integrator
200  if (!algo) return;
202 }
203 
206 }
207 
208 void IntegratorOneDimOptions::Print(std::ostream & os) const {
209  //print all the options
210  IntegOptionsUtil::Print(os, *this);
211 }
212 
213 // one dim integrator options: implementation for static methods
214 
215 /// print default options
216 void IntegratorOneDimOptions::PrintDefault(const char * name, std::ostream & os) {
217  //print default options
218  IntegOptionsUtil::PrintDefault<IntegratorOneDimOptions>(name,os);
219 }
220 
221 
222 
224  // set the default 1D integrator
225  if (!algo) return;
227  if (type == IntegrationOneDim::kDEFAULT) return; // this is possible only when invalid name was specified
228  IntegOneDim::gDefaultIntegrator = (int) type;
233 }
234 
235 
237  // return default integrator name
239 }
240 
242  // return default integrator type (enum)
244 }
245 
246 
248  // set the default tolerance
250 }
252  // set the default tolerance
254 }
255 
257  // set the default workspace size
259 }
261  // set the default number of points for the integration rule
263 }
264 
265 
270 
271 
273  // create default extra options for the given algorithm type
274  return GenAlgoOptions::Default(algo);
275 }
276 
278  // find extra options for the given algorithm type
279  return GenAlgoOptions::FindDefault(algo);
280 }
281 
282 //////////////////////////////////////////////////////
283 //Multi-dim integration options implementation
284 /////////////////////////////////////////////////////////
285 
288 {
294 
295  fExtraOptions = opts; // N.B. ownership of pointer is given to the class !
296 
297  // check the default options if opts = 0
298  if (!fExtraOptions) {
299  IOptions * gopts = FindDefault( DefaultIntegrator().c_str() );
300  if (gopts) fExtraOptions = gopts->Clone();
301  }
302 }
303 
305  // set the default integrator
306  if (!algo) return;
308 }
309 
312 }
313 
314 void IntegratorMultiDimOptions::Print(std::ostream & os) const {
315  //print all the options
316  IntegOptionsUtil::Print(os, *this);
317 }
318 
319 // multi dim integrator options: implementation for static methods
320 
321 /// print default options
322 void IntegratorMultiDimOptions::PrintDefault(const char * name, std::ostream & os) {
323  //print default options
324  IntegOptionsUtil::PrintDefault<IntegratorMultiDimOptions>(name,os);
325 }
326 
327 
329  // set the default integrator
330  if (!algo) return;
331  // check if type is correct
333  if (type == IntegrationMultiDim::kDEFAULT) return; // this is possible only when invalid name was specified
335 }
336 
337 
339  // return default integrator name
341 }
342 
344  // return default integrator type (enum)
346 }
347 
348 
350  // set the default tolerance
352 }
353 
355  // set the default tolerance
357 }
358 
360  // set the default workspace size
362 }
364  // set the default (max) function calls
366 }
367 
368 
373 
374 
376  // create default extra options for the given algorithm type
377  return GenAlgoOptions::Default(algo);
378 }
379 
381  // create default extra options for the given algorithm type
382  return GenAlgoOptions::FindDefault(algo);
383 }
384 
385 
386 } // end namespace Math
387 
388 } // end namespace ROOT
389 
void Print(std::ostream &os=std::cout) const
print all the options
static ROOT::Math::IOptions * FindDefault(const char *name)
static IntegrationMultiDim::Type GetType(const char *name)
static function to get the enumeration from a string
Definition: Integrator.cxx:78
static unsigned int gDefaultWKSize
void SetExtraOptions(const IOptions &opt)
set extra options (in this case pointer is cloned)
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
static void PrintDefault(const char *name=0, std::ostream &os=std::cout)
print only the specified default options
Type
enumeration specifying the integration types.
static unsigned int gDefaultNPoints
static unsigned int gDefaultNPointsGSLAdaptive
virtual void Print(std::ostream &=std::cout) const
print options
Definition: IOptions.h:98
static void SetDefaultIntegrator(const char *name)
static void SetDefaultWKSize(unsigned int size)
static void SetDefaultIntegrator(const char *name)
void PrintDefault(const char *name, std::ostream &os)
print default options
static void PrintDefault(const char *name=0, std::ostream &os=std::cout)
print only the specified default options
static void SetDefaultAbsTolerance(double tol)
void Print(std::ostream &os=std::cout) const
print all the options
static unsigned int gDefaultNPointsLegendre
BaseIntegratorOptions & operator=(const BaseIntegratorOptions &opt)
assignment operators
IntegratorMultiDimOptions(IOptions *extraOpts=0)
static void SetDefaultNCalls(unsigned int ncall)
static unsigned int gDefaultNCalls
ROOT::Math::IOptions * fExtraOptions
static unsigned int gDefaultWKSize
#define N
IntegratorOneDimOptions(IOptions *extraOpts=0)
static IntegrationOneDim::Type DefaultIntegratorType()
static void SetDefaultRelTolerance(double tol)
static ROOT::Math::IOptions & Default(const char *name)
std::string Integrator() const
name of multi-dim integrator
static IntegrationOneDim::Type GetType(const char *name)
static function to get the enumeration from a string
Definition: Integrator.cxx:53
void SetIntegrator(const char *name)
set multi-dim integrator name
static std::string GetName(IntegrationMultiDim::Type)
static function to get a string from the enumeration
Definition: Integrator.cxx:90
virtual IOptions * Clone() const =0
Base class for Numerical integration options common in 1D and multi-dimension This is an internal cla...
static double gDefaultRelTolerance
static IntegrationMultiDim::Type DefaultIntegratorType()
Type
enumeration specifying the integration types.
static ROOT::Math::IOptions & Default(const char *name)
retrieve specific options - if not existing create a IOptions
void Print(std::ostream &os, const OptionType &opt)
static void SetDefaultRelTolerance(double tol)
static void SetDefaultNPoints(unsigned int n)
static ROOT::Math::IOptions * FindDefault(const char *name)
static std::string GetName(IntegrationOneDim::Type)
static function to get a string from the enumeration
Definition: Integrator.cxx:66
std::string Integrator() const
name of 1D integrator
Namespace for new Math classes and functions.
static IOptions & Default(const char *algoname)
Generic interface for defining configuration options of a numerical algorithm.
Definition: IOptions.h:30
static double gDefaultAbsTolerance
BaseIntegratorOptions()
protected constructor to avoid user creating this class
static IOptions * FindDefault(const char *algoname)
void SetIntegrator(const char *name)
set 1D integrator name
static void SetDefaultAbsTolerance(double tol)
static void SetDefaultWKSize(unsigned int size)