IntegratorOptions.cxx

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: IntegratorOptions.cxx 36806 2010-11-20 11:09:14Z moneta $ 
00002 // Author: L. Moneta Fri Aug 15 2008
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2008  LCG ROOT Math Team, CERN/PH-SFT                *
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>   // need to use c version of tolower defined here
00021 
00022 #include <map>
00023 
00024 namespace ROOT { 
00025    
00026 namespace Math { 
00027 
00028    // eventually could take values from /etc/system.rootrc
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;  // corresponds to 31 points
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 // some utility functions
00060 
00061 namespace IntegOptionsUtil { 
00062 
00063 
00064    // traits for the specific methods 1D - ND
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    //print  option values (not the default ones) 
00088    template <class OptionType>
00089    void Print(std::ostream & os,const OptionType & opt) {
00090       //print all the options
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    /// print default  options 
00105    template <class OptionType>
00106    void PrintDefault(const char * name, std::ostream & os) {
00107       //print default options
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 /// constructor (protected) to avoid user creating this class
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    // copy constructor 
00132    (*this) = opt; 
00133 }
00134 
00135 BaseIntegratorOptions & BaseIntegratorOptions::operator=(const BaseIntegratorOptions & opt) {  
00136    // assignment operator 
00137    if (this == &opt) return *this; // self assignment
00138    fWKSize = opt.fWKSize;
00139    fNCalls = opt.fNCalls;
00140    fAbsTolerance = opt.fAbsTolerance; 
00141    fRelTolerance = opt.fRelTolerance; 
00142    fIntegType = opt.fIntegType; 
00143 
00144 //    std::cout << " copy options for " << fIntegName << std::endl;
00145 //    std::cout << fExtraOptions << std::endl;
00146 //    if (fExtraOptions) fExtraOptions->Print(std::cout); 
00147 
00148 //    std::cout << opt.fExtraOptions << std::endl;
00149 //    if (opt.fExtraOptions) (opt.fExtraOptions)->Print(std::cout); 
00150 
00151 
00152 
00153    ClearExtra(); 
00154    if (opt.fExtraOptions)  fExtraOptions =  (opt.fExtraOptions)->Clone();
00155    return *this;
00156 }
00157 
00158 
00159 void BaseIntegratorOptions::ClearExtra() {  
00160    // delete extra options
00161    if (fExtraOptions) delete fExtraOptions; 
00162    fExtraOptions = 0; 
00163 }
00164 
00165 void BaseIntegratorOptions::SetExtraOptions(const IOptions & opt) {  
00166    // delete extra options
00167    ClearExtra(); 
00168    fExtraOptions = opt.Clone(); 
00169 }
00170 
00171 
00172 
00173 // one dim specific  methods
00174 
00175 // implementation of non-static methods 
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; // N.B. ownership of pointer is given to the class !
00187 
00188    // check  the default options if opts = 0
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    // set the default 1D integrator
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    //print all the options
00208    IntegOptionsUtil::Print(os, *this);
00209 }
00210 
00211 // one dim integrator options:  implementation for static methods 
00212 
00213 /// print default  options 
00214 void IntegratorOneDimOptions::PrintDefault(const char * name, std::ostream & os) {
00215    //print default options
00216    IntegOptionsUtil::PrintDefault<IntegratorOneDimOptions>(name,os);
00217 }
00218 
00219 
00220 
00221 void IntegratorOneDimOptions::SetDefaultIntegrator(const char * algo ) {   
00222    // set the default 1D integrator
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    // return default integrator name
00234    return  IntegratorOneDim::GetName((IntegratorOneDim::Type) IntegOneDim::gDefaultIntegrator);
00235 }
00236 
00237 IntegratorOneDim::Type  IntegratorOneDimOptions::DefaultIntegratorType() {
00238    // return default integrator type (enum) 
00239    return  (IntegratorOneDim::Type) IntegOneDim::gDefaultIntegrator;
00240 }
00241 
00242 
00243 void IntegratorOneDimOptions::SetDefaultAbsTolerance(double tol) {
00244    // set the default tolerance
00245    IntegOneDim::gDefaultAbsTolerance = tol; 
00246 }
00247 void IntegratorOneDimOptions::SetDefaultRelTolerance(double tol) {
00248    // set the default tolerance
00249    IntegOneDim::gDefaultRelTolerance = tol; 
00250 }
00251 
00252 void IntegratorOneDimOptions::SetDefaultWKSize(unsigned int size) {
00253    // set the default workspace size 
00254    IntegOneDim::gDefaultWKSize = size;
00255 }
00256 void IntegratorOneDimOptions::SetDefaultNPoints(unsigned int n) {
00257    // set the default number of points for the integration rule
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    // create default extra options for the given algorithm type 
00270    return GenAlgoOptions::Default(algo);
00271 }
00272 
00273 IOptions * IntegratorOneDimOptions::FindDefault(const char * algo) { 
00274    // find extra options for the given algorithm type 
00275    return GenAlgoOptions::FindDefault(algo);
00276 }
00277 
00278 //////////////////////////////////////////////////////
00279 //Multi-dim integration options implementation
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; // N.B. ownership of pointer is given to the class !
00292 
00293    // check  the default options if opts = 0
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    // set the default integrator
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    //print all the options
00312    IntegOptionsUtil::Print(os, *this);
00313 }
00314 
00315 // multi dim integrator options:  implementation for static methods 
00316 
00317 /// print default  options 
00318 void IntegratorMultiDimOptions::PrintDefault(const char * name, std::ostream & os) {
00319    //print default options
00320    IntegOptionsUtil::PrintDefault<IntegratorMultiDimOptions>(name,os);
00321 }
00322 
00323 
00324 void IntegratorMultiDimOptions::SetDefaultIntegrator(const char * algo ) {   
00325    // set the default integrator
00326    if (!algo) return; 
00327    IntegMultiDim::gDefaultIntegrator = (int) IntegratorMultiDim::GetType(algo);
00328 }
00329 
00330 
00331 std::string  IntegratorMultiDimOptions::DefaultIntegrator() {
00332    // return default integrator name 
00333    return  IntegratorMultiDim::GetName((IntegratorMultiDim::Type) IntegMultiDim::gDefaultIntegrator);
00334 }
00335 
00336 IntegratorMultiDim::Type  IntegratorMultiDimOptions::DefaultIntegratorType() {
00337    // return default integrator type (enum) 
00338    return  (IntegratorMultiDim::Type) IntegMultiDim::gDefaultIntegrator;
00339 }
00340 
00341 
00342 void IntegratorMultiDimOptions::SetDefaultAbsTolerance(double tol) {
00343    // set the default tolerance
00344    IntegMultiDim::gDefaultAbsTolerance = tol; 
00345 }
00346 
00347 void IntegratorMultiDimOptions::SetDefaultRelTolerance(double tol) {
00348    // set the default tolerance
00349    IntegMultiDim::gDefaultRelTolerance = tol; 
00350 }
00351 
00352 void IntegratorMultiDimOptions::SetDefaultWKSize(unsigned int size) {
00353    // set the default workspace size 
00354    IntegMultiDim::gDefaultWKSize = size;
00355 }
00356 void IntegratorMultiDimOptions::SetDefaultNCalls(unsigned int ncall) {
00357    // set the default (max) function calls
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    // create default extra options for the given algorithm type 
00370    return GenAlgoOptions::Default(algo);
00371 }
00372 
00373 IOptions * IntegratorMultiDimOptions::FindDefault(const char * algo) { 
00374    // create default extra options for the given algorithm type 
00375    return GenAlgoOptions::FindDefault(algo);
00376 }
00377 
00378 
00379 } // end namespace Math
00380 
00381 } // end namespace ROOT
00382 

Generated on Tue Jul 5 14:34:24 2011 for ROOT_528-00b_version by  doxygen 1.5.1