00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "Math/IntegratorOptions.h"
00012 #include "Math/Integrator.h"
00013 #include "Math/IntegratorMultiDim.h"
00014 #include "Math/GenAlgoOptions.h"
00015
00016 #include "RConfigure.h"
00017
00018 #include <algorithm>
00019 #include <functional>
00020 #include <ctype.h>
00021
00022 #include <map>
00023
00024 namespace ROOT {
00025
00026 namespace Math {
00027
00028
00029
00030 namespace IntegOneDim {
00031
00032 #ifdef R__HAS_MATHMORE
00033 static int gDefaultIntegrator = IntegrationOneDim::kADAPTIVESINGULAR;
00034 #else
00035 static int gDefaultIntegrator = IntegrationOneDim::kGAUSS;
00036 #endif
00037 static double gDefaultAbsTolerance = 1.E-06;
00038 static double gDefaultRelTolerance = 1.E-09;
00039 static unsigned int gDefaultWKSize = 1000;
00040 static unsigned int gDefaultNPointsLegendre = 10;
00041 static unsigned int gDefaultNPointsGSLAdaptive = 3;
00042 static unsigned int gDefaultNPoints = gDefaultNPointsGSLAdaptive;
00043
00044
00045 }
00046
00047 namespace IntegMultiDim {
00048
00049 static int gDefaultIntegrator = IntegrationMultiDim::kADAPTIVE;
00050 static double gDefaultAbsTolerance = 1.E-06;
00051 static double gDefaultRelTolerance = 1.E-09;
00052 static unsigned int gDefaultWKSize = 100000;
00053 static unsigned int gDefaultNCalls = 100000;
00054
00055
00056 }
00057
00058
00059
00060
00061 namespace IntegOptionsUtil {
00062
00063
00064
00065 template<class OptionType>
00066 struct OptionTrait {
00067 static int N() { return 0; }
00068 static int N(const OptionType & ) { return 0; }
00069 static const char * DescriptionOfN() {return 0; }
00070 };
00071 template<>
00072 struct OptionTrait<IntegratorOneDimOptions> {
00073 typedef IntegratorOneDimOptions OptType;
00074 static int N() { return OptType::DefaultNPoints(); }
00075 static int N(const OptType & opt) { return opt.NPoints(); }
00076 static const char * DescriptionOfN() {return "Rule (Npoints)";}
00077 };
00078 template<>
00079 struct OptionTrait<IntegratorMultiDimOptions> {
00080 typedef IntegratorMultiDimOptions OptType;
00081 static int N() { return OptType::DefaultNCalls(); }
00082 static int N(const OptType & opt) { return opt.NCalls(); }
00083 static const char * DescriptionOfN() {return "(max) function calls";}
00084 };
00085
00086
00087
00088 template <class OptionType>
00089 void Print(std::ostream & os,const OptionType & opt) {
00090
00091 os << std::setw(25) << "Integrator Type" << " : " << std::setw(15) << opt.Integrator() << std::endl;
00092 os << std::setw(25) << "Absolute tolerance" << " : " << std::setw(15) << opt.AbsTolerance() << std::endl;
00093 os << std::setw(25) << "Relative tolerance" << " : " << std::setw(15) << opt.RelTolerance() << std::endl;
00094 os << std::setw(25) << "Workspace size" << " : " << std::setw(15) << opt.WKSize() << std::endl;
00095 typedef OptionTrait<OptionType> OPT;
00096 os << std::setw(25) << OPT::DescriptionOfN() << " : " << std::setw(15) << OPT::N(opt) << std::endl;
00097 if (opt.ExtraOptions()) {
00098 os << opt.Integrator() << " specific options :" << std::endl;
00099 opt.ExtraOptions()->Print(os);
00100 }
00101 }
00102
00103
00104
00105 template <class OptionType>
00106 void PrintDefault(const char * name, std::ostream & os) {
00107
00108 std::string integName = (name != 0) ? name : OptionType::DefaultIntegrator();
00109 os << "Default options for numerical integrator " << integName << " : " << std::endl;
00110 os << std::setw(25) << "Absolute tolerance" << " : " << std::setw(15) << OptionType::DefaultAbsTolerance() << std::endl;
00111 os << std::setw(25) << "Relative tolerance" << " : " <<std::setw(15) << OptionType::DefaultRelTolerance() << std::endl;
00112 os << std::setw(25) << "Workspace size" << " : " << std::setw(15) << OptionType::DefaultWKSize() << std::endl;
00113 typedef OptionTrait<OptionType> OPT;
00114 os << std::setw(25) << OPT::DescriptionOfN() << " : " << std::setw(15) << OPT::N() << std::endl;
00115 IOptions * opts = GenAlgoOptions::FindDefault(integName.c_str());
00116 if (opts) opts->Print(os);
00117 }
00118
00119 }
00120
00121
00122
00123 BaseIntegratorOptions::BaseIntegratorOptions() :
00124 fIntegType(-1),
00125 fWKSize(0), fNCalls(0),
00126 fAbsTolerance(0), fRelTolerance(0),
00127 fExtraOptions(0)
00128 {}
00129
00130 BaseIntegratorOptions::BaseIntegratorOptions(const BaseIntegratorOptions & opt) : fExtraOptions(0) {
00131
00132 (*this) = opt;
00133 }
00134
00135 BaseIntegratorOptions & BaseIntegratorOptions::operator=(const BaseIntegratorOptions & opt) {
00136
00137 if (this == &opt) return *this;
00138 fWKSize = opt.fWKSize;
00139 fNCalls = opt.fNCalls;
00140 fAbsTolerance = opt.fAbsTolerance;
00141 fRelTolerance = opt.fRelTolerance;
00142 fIntegType = opt.fIntegType;
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 ClearExtra();
00154 if (opt.fExtraOptions) fExtraOptions = (opt.fExtraOptions)->Clone();
00155 return *this;
00156 }
00157
00158
00159 void BaseIntegratorOptions::ClearExtra() {
00160
00161 if (fExtraOptions) delete fExtraOptions;
00162 fExtraOptions = 0;
00163 }
00164
00165 void BaseIntegratorOptions::SetExtraOptions(const IOptions & opt) {
00166
00167 ClearExtra();
00168 fExtraOptions = opt.Clone();
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 IntegratorOneDimOptions::IntegratorOneDimOptions(IOptions * opts):
00178 BaseIntegratorOptions()
00179 {
00180 fWKSize = IntegOneDim::gDefaultWKSize;
00181 fNCalls = IntegOneDim::gDefaultNPoints;
00182 fAbsTolerance = IntegOneDim::gDefaultAbsTolerance;
00183 fRelTolerance = IntegOneDim::gDefaultRelTolerance;
00184 fIntegType = IntegOneDim::gDefaultIntegrator;
00185
00186 fExtraOptions = opts;
00187
00188
00189 if (!fExtraOptions) {
00190 std::string igname = DefaultIntegrator();
00191 IOptions * gopts = FindDefault( igname.c_str() );
00192 if (gopts) fExtraOptions = gopts->Clone();
00193 }
00194 }
00195
00196 void IntegratorOneDimOptions::SetIntegrator(const char * algo ) {
00197
00198 if (!algo) return;
00199 fIntegType = (int) IntegratorOneDim::GetType(algo);
00200 }
00201
00202 std::string IntegratorOneDimOptions::Integrator() const {
00203 return IntegratorOneDim::GetName((IntegratorOneDim::Type) fIntegType);
00204 }
00205
00206 void IntegratorOneDimOptions::Print(std::ostream & os) const {
00207
00208 IntegOptionsUtil::Print(os, *this);
00209 }
00210
00211
00212
00213
00214 void IntegratorOneDimOptions::PrintDefault(const char * name, std::ostream & os) {
00215
00216 IntegOptionsUtil::PrintDefault<IntegratorOneDimOptions>(name,os);
00217 }
00218
00219
00220
00221 void IntegratorOneDimOptions::SetDefaultIntegrator(const char * algo ) {
00222
00223 if (!algo) return;
00224 IntegOneDim::gDefaultIntegrator = (int) IntegratorOneDim::GetType(algo);
00225 if (IntegOneDim::gDefaultIntegrator == IntegrationOneDim::kLEGENDRE)
00226 IntegOneDim::gDefaultNPoints = IntegOneDim::gDefaultNPointsLegendre;
00227 if (IntegOneDim::gDefaultIntegrator == IntegrationOneDim::kADAPTIVE)
00228 IntegOneDim::gDefaultNPoints = IntegOneDim::gDefaultNPointsGSLAdaptive;
00229 }
00230
00231
00232 std::string IntegratorOneDimOptions::DefaultIntegrator() {
00233
00234 return IntegratorOneDim::GetName((IntegratorOneDim::Type) IntegOneDim::gDefaultIntegrator);
00235 }
00236
00237 IntegratorOneDim::Type IntegratorOneDimOptions::DefaultIntegratorType() {
00238
00239 return (IntegratorOneDim::Type) IntegOneDim::gDefaultIntegrator;
00240 }
00241
00242
00243 void IntegratorOneDimOptions::SetDefaultAbsTolerance(double tol) {
00244
00245 IntegOneDim::gDefaultAbsTolerance = tol;
00246 }
00247 void IntegratorOneDimOptions::SetDefaultRelTolerance(double tol) {
00248
00249 IntegOneDim::gDefaultRelTolerance = tol;
00250 }
00251
00252 void IntegratorOneDimOptions::SetDefaultWKSize(unsigned int size) {
00253
00254 IntegOneDim::gDefaultWKSize = size;
00255 }
00256 void IntegratorOneDimOptions::SetDefaultNPoints(unsigned int n) {
00257
00258 IntegOneDim::gDefaultNPoints = n;
00259 }
00260
00261
00262 double IntegratorOneDimOptions::DefaultAbsTolerance() { return IntegOneDim::gDefaultAbsTolerance; }
00263 double IntegratorOneDimOptions::DefaultRelTolerance() { return IntegOneDim::gDefaultRelTolerance; }
00264 unsigned int IntegratorOneDimOptions::DefaultWKSize() { return IntegOneDim::gDefaultWKSize; }
00265 unsigned int IntegratorOneDimOptions::DefaultNPoints() { return IntegOneDim::gDefaultNPoints; }
00266
00267
00268 IOptions & IntegratorOneDimOptions::Default(const char * algo) {
00269
00270 return GenAlgoOptions::Default(algo);
00271 }
00272
00273 IOptions * IntegratorOneDimOptions::FindDefault(const char * algo) {
00274
00275 return GenAlgoOptions::FindDefault(algo);
00276 }
00277
00278
00279
00280
00281
00282 IntegratorMultiDimOptions::IntegratorMultiDimOptions(IOptions * opts):
00283 BaseIntegratorOptions()
00284 {
00285 fWKSize = IntegMultiDim::gDefaultWKSize;
00286 fNCalls = IntegMultiDim::gDefaultNCalls;
00287 fAbsTolerance = IntegMultiDim::gDefaultAbsTolerance;
00288 fRelTolerance = IntegMultiDim::gDefaultRelTolerance;
00289 fIntegType = IntegMultiDim::gDefaultIntegrator;
00290
00291 fExtraOptions = opts;
00292
00293
00294 if (!fExtraOptions) {
00295 IOptions * gopts = FindDefault( DefaultIntegrator().c_str() );
00296 if (gopts) fExtraOptions = gopts->Clone();
00297 }
00298 }
00299
00300 void IntegratorMultiDimOptions::SetIntegrator(const char * algo ) {
00301
00302 if (!algo) return;
00303 fIntegType = (int) IntegratorMultiDim::GetType(algo);
00304 }
00305
00306 std::string IntegratorMultiDimOptions::Integrator() const {
00307 return IntegratorMultiDim::GetName((IntegratorMultiDim::Type) fIntegType);
00308 }
00309
00310 void IntegratorMultiDimOptions::Print(std::ostream & os) const {
00311
00312 IntegOptionsUtil::Print(os, *this);
00313 }
00314
00315
00316
00317
00318 void IntegratorMultiDimOptions::PrintDefault(const char * name, std::ostream & os) {
00319
00320 IntegOptionsUtil::PrintDefault<IntegratorMultiDimOptions>(name,os);
00321 }
00322
00323
00324 void IntegratorMultiDimOptions::SetDefaultIntegrator(const char * algo ) {
00325
00326 if (!algo) return;
00327 IntegMultiDim::gDefaultIntegrator = (int) IntegratorMultiDim::GetType(algo);
00328 }
00329
00330
00331 std::string IntegratorMultiDimOptions::DefaultIntegrator() {
00332
00333 return IntegratorMultiDim::GetName((IntegratorMultiDim::Type) IntegMultiDim::gDefaultIntegrator);
00334 }
00335
00336 IntegratorMultiDim::Type IntegratorMultiDimOptions::DefaultIntegratorType() {
00337
00338 return (IntegratorMultiDim::Type) IntegMultiDim::gDefaultIntegrator;
00339 }
00340
00341
00342 void IntegratorMultiDimOptions::SetDefaultAbsTolerance(double tol) {
00343
00344 IntegMultiDim::gDefaultAbsTolerance = tol;
00345 }
00346
00347 void IntegratorMultiDimOptions::SetDefaultRelTolerance(double tol) {
00348
00349 IntegMultiDim::gDefaultRelTolerance = tol;
00350 }
00351
00352 void IntegratorMultiDimOptions::SetDefaultWKSize(unsigned int size) {
00353
00354 IntegMultiDim::gDefaultWKSize = size;
00355 }
00356 void IntegratorMultiDimOptions::SetDefaultNCalls(unsigned int ncall) {
00357
00358 IntegMultiDim::gDefaultNCalls = ncall;
00359 }
00360
00361
00362 double IntegratorMultiDimOptions::DefaultAbsTolerance() { return IntegMultiDim::gDefaultAbsTolerance; }
00363 double IntegratorMultiDimOptions::DefaultRelTolerance() { return IntegMultiDim::gDefaultRelTolerance; }
00364 unsigned int IntegratorMultiDimOptions::DefaultWKSize() { return IntegMultiDim::gDefaultWKSize; }
00365 unsigned int IntegratorMultiDimOptions::DefaultNCalls() { return IntegMultiDim::gDefaultNCalls; }
00366
00367
00368 IOptions & IntegratorMultiDimOptions::Default(const char * algo) {
00369
00370 return GenAlgoOptions::Default(algo);
00371 }
00372
00373 IOptions * IntegratorMultiDimOptions::FindDefault(const char * algo) {
00374
00375 return GenAlgoOptions::FindDefault(algo);
00376 }
00377
00378
00379 }
00380
00381 }
00382