16 #include "RConfigure.h"
30 namespace IntegOneDim {
32 #ifdef R__HAS_MATHMORE
33 static int gDefaultIntegrator = IntegrationOneDim::kADAPTIVESINGULAR;
35 static int gDefaultIntegrator = IntegrationOneDim::kGAUSS;
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;
42 static unsigned int gDefaultNPoints = gDefaultNPointsGSLAdaptive;
47 namespace IntegMultiDim {
49 static int gDefaultIntegrator = IntegrationMultiDim::kADAPTIVE;
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;
63 namespace IntegOptionsUtil {
67 template<
class OptionType>
69 static int N() {
return 0; }
70 static int N(
const OptionType & ) {
return 0; }
71 static const char * DescriptionOfN() {
return 0; }
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)";}
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";}
90 template <
class OptionType>
91 void Print(std::ostream & os,
const OptionType & opt) {
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);
107 template <
class OptionType>
108 void PrintDefault(
const char * name, std::ostream & os) {
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);
125 BaseIntegratorOptions::BaseIntegratorOptions() :
127 fWKSize(0), fNCalls(0),
128 fAbsTolerance(0), fRelTolerance(0),
132 BaseIntegratorOptions::BaseIntegratorOptions(
const BaseIntegratorOptions & opt) : fExtraOptions(0) {
137 BaseIntegratorOptions & BaseIntegratorOptions::operator=(
const BaseIntegratorOptions & opt) {
139 if (
this == &opt)
return *
this;
140 fWKSize = opt.fWKSize;
141 fNCalls = opt.fNCalls;
142 fAbsTolerance = opt.fAbsTolerance;
143 fRelTolerance = opt.fRelTolerance;
144 fIntegType = opt.fIntegType;
156 if (opt.fExtraOptions) fExtraOptions = (opt.fExtraOptions)->Clone();
161 void BaseIntegratorOptions::ClearExtra() {
163 if (fExtraOptions)
delete fExtraOptions;
167 void BaseIntegratorOptions::SetExtraOptions(
const IOptions & opt) {
170 fExtraOptions = opt.Clone();
179 IntegratorOneDimOptions::IntegratorOneDimOptions(IOptions * opts):
180 BaseIntegratorOptions()
182 fWKSize = IntegOneDim::gDefaultWKSize;
183 fNCalls = IntegOneDim::gDefaultNPoints;
184 fAbsTolerance = IntegOneDim::gDefaultAbsTolerance;
185 fRelTolerance = IntegOneDim::gDefaultRelTolerance;
186 fIntegType = IntegOneDim::gDefaultIntegrator;
188 fExtraOptions = opts;
191 if (!fExtraOptions) {
192 std::string igname = DefaultIntegrator();
193 IOptions * gopts = FindDefault( igname.c_str() );
194 if (gopts) fExtraOptions = gopts->Clone();
198 void IntegratorOneDimOptions::SetIntegrator(
const char * algo ) {
201 fIntegType = (int) IntegratorOneDim::GetType(algo);
204 std::string IntegratorOneDimOptions::Integrator()
const {
205 return IntegratorOneDim::GetName((IntegratorOneDim::Type) fIntegType);
208 void IntegratorOneDimOptions::Print(std::ostream & os)
const {
210 IntegOptionsUtil::Print(os, *
this);
216 void IntegratorOneDimOptions::PrintDefault(
const char * name, std::ostream & os) {
218 IntegOptionsUtil::PrintDefault<IntegratorOneDimOptions>(name,os);
223 void IntegratorOneDimOptions::SetDefaultIntegrator(
const char * algo ) {
226 IntegrationOneDim::Type type = IntegratorOneDim::GetType(algo);
227 if (type == IntegrationOneDim::kDEFAULT)
return;
228 IntegOneDim::gDefaultIntegrator = (int) type;
229 if (IntegOneDim::gDefaultIntegrator == IntegrationOneDim::kLEGENDRE)
230 IntegOneDim::gDefaultNPoints = IntegOneDim::gDefaultNPointsLegendre;
231 if (IntegOneDim::gDefaultIntegrator == IntegrationOneDim::kADAPTIVE)
232 IntegOneDim::gDefaultNPoints = IntegOneDim::gDefaultNPointsGSLAdaptive;
236 std::string IntegratorOneDimOptions::DefaultIntegrator() {
238 return IntegratorOneDim::GetName((IntegratorOneDim::Type) IntegOneDim::gDefaultIntegrator);
241 IntegratorOneDim::Type IntegratorOneDimOptions::DefaultIntegratorType() {
243 return (IntegratorOneDim::Type) IntegOneDim::gDefaultIntegrator;
247 void IntegratorOneDimOptions::SetDefaultAbsTolerance(
double tol) {
249 IntegOneDim::gDefaultAbsTolerance = tol;
251 void IntegratorOneDimOptions::SetDefaultRelTolerance(
double tol) {
253 IntegOneDim::gDefaultRelTolerance = tol;
256 void IntegratorOneDimOptions::SetDefaultWKSize(
unsigned int size) {
258 IntegOneDim::gDefaultWKSize = size;
260 void IntegratorOneDimOptions::SetDefaultNPoints(
unsigned int n) {
262 IntegOneDim::gDefaultNPoints = n;
266 double IntegratorOneDimOptions::DefaultAbsTolerance() {
return IntegOneDim::gDefaultAbsTolerance; }
267 double IntegratorOneDimOptions::DefaultRelTolerance() {
return IntegOneDim::gDefaultRelTolerance; }
268 unsigned int IntegratorOneDimOptions::DefaultWKSize() {
return IntegOneDim::gDefaultWKSize; }
269 unsigned int IntegratorOneDimOptions::DefaultNPoints() {
return IntegOneDim::gDefaultNPoints; }
272 IOptions & IntegratorOneDimOptions::Default(
const char * algo) {
274 return GenAlgoOptions::Default(algo);
277 IOptions * IntegratorOneDimOptions::FindDefault(
const char * algo) {
279 return GenAlgoOptions::FindDefault(algo);
286 IntegratorMultiDimOptions::IntegratorMultiDimOptions(IOptions * opts):
287 BaseIntegratorOptions()
289 fWKSize = IntegMultiDim::gDefaultWKSize;
290 fNCalls = IntegMultiDim::gDefaultNCalls;
291 fAbsTolerance = IntegMultiDim::gDefaultAbsTolerance;
292 fRelTolerance = IntegMultiDim::gDefaultRelTolerance;
293 fIntegType = IntegMultiDim::gDefaultIntegrator;
295 fExtraOptions = opts;
298 if (!fExtraOptions) {
299 IOptions * gopts = FindDefault( DefaultIntegrator().c_str() );
300 if (gopts) fExtraOptions = gopts->Clone();
304 void IntegratorMultiDimOptions::SetIntegrator(
const char * algo ) {
307 fIntegType = (int) IntegratorMultiDim::GetType(algo);
310 std::string IntegratorMultiDimOptions::Integrator()
const {
311 return IntegratorMultiDim::GetName((IntegratorMultiDim::Type) fIntegType);
314 void IntegratorMultiDimOptions::Print(std::ostream & os)
const {
316 IntegOptionsUtil::Print(os, *
this);
322 void IntegratorMultiDimOptions::PrintDefault(
const char * name, std::ostream & os) {
324 IntegOptionsUtil::PrintDefault<IntegratorMultiDimOptions>(name,os);
328 void IntegratorMultiDimOptions::SetDefaultIntegrator(
const char * algo ) {
332 IntegrationMultiDim::Type type = IntegratorMultiDim::GetType(algo);
333 if (type == IntegrationMultiDim::kDEFAULT)
return;
334 IntegMultiDim::gDefaultIntegrator = (int) type;
338 std::string IntegratorMultiDimOptions::DefaultIntegrator() {
340 return IntegratorMultiDim::GetName((IntegratorMultiDim::Type) IntegMultiDim::gDefaultIntegrator);
343 IntegratorMultiDim::Type IntegratorMultiDimOptions::DefaultIntegratorType() {
345 return (IntegratorMultiDim::Type) IntegMultiDim::gDefaultIntegrator;
349 void IntegratorMultiDimOptions::SetDefaultAbsTolerance(
double tol) {
351 IntegMultiDim::gDefaultAbsTolerance = tol;
354 void IntegratorMultiDimOptions::SetDefaultRelTolerance(
double tol) {
356 IntegMultiDim::gDefaultRelTolerance = tol;
359 void IntegratorMultiDimOptions::SetDefaultWKSize(
unsigned int size) {
361 IntegMultiDim::gDefaultWKSize = size;
363 void IntegratorMultiDimOptions::SetDefaultNCalls(
unsigned int ncall) {
365 IntegMultiDim::gDefaultNCalls = ncall;
369 double IntegratorMultiDimOptions::DefaultAbsTolerance() {
return IntegMultiDim::gDefaultAbsTolerance; }
370 double IntegratorMultiDimOptions::DefaultRelTolerance() {
return IntegMultiDim::gDefaultRelTolerance; }
371 unsigned int IntegratorMultiDimOptions::DefaultWKSize() {
return IntegMultiDim::gDefaultWKSize; }
372 unsigned int IntegratorMultiDimOptions::DefaultNCalls() {
return IntegMultiDim::gDefaultNCalls; }
375 IOptions & IntegratorMultiDimOptions::Default(
const char * algo) {
377 return GenAlgoOptions::Default(algo);
380 IOptions * IntegratorMultiDimOptions::FindDefault(
const char * algo) {
382 return GenAlgoOptions::FindDefault(algo);