ROOT  6.06/09
Reference Guide
GSLIntegrator.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Authors: L. Moneta, A. Zsenei 08/2005
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License *
10  * as published by the Free Software Foundation; either version 2 *
11  * of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library (see file COPYING); if not, write *
20  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21  * 330, Boston, MA 02111-1307 USA, or contact the author. *
22  * *
23  **********************************************************************/
24 
25 // Header file for class GSLIntegrator
26 //
27 // Created by: Lorenzo Moneta at Thu Nov 11 14:22:32 2004
28 //
29 // Last update: Thu Nov 11 14:22:32 2004
30 //
31 #ifndef ROOT_Math_GSLIntegrator
32 #define ROOT_Math_GSLIntegrator
33 
34 
35 #ifndef ROOT_Math_VirtualIntegrator
36 #include "Math/VirtualIntegrator.h"
37 #endif
38 
39 #ifndef ROOT_Math_IntegrationTypes
40 #include "Math/IntegrationTypes.h"
41 #endif
42 
43 #ifndef ROOT_Math_IFunctionfwd
44 #include "Math/IFunctionfwd.h"
45 #endif
46 
47 
48 
49 
50 #ifndef ROOT_Math_GSLFunctionAdapter
52 #endif
53 
54 #include <vector>
55 
56 
57 
58 namespace ROOT {
59 namespace Math {
60 
61 
62 
63  class GSLIntegrationWorkspace;
64  class GSLFunctionWrapper;
65 
66  //_________________________________________________________________________
67  /**
68 
69  Class for performing numerical integration of a function in one dimension.
70  It uses the numerical integration algorithms of GSL, which reimplements the
71  algorithms used in the QUADPACK, a numerical integration package written in Fortran.
72 
73  Various types of adaptive and non-adaptive integration are supported. These include
74  integration over infinite and semi-infinite ranges and singular integrals.
75 
76  The integration type is selected using the Integration::type enumeration
77  in the class constructor.
78  The default type is adaptive integration with singularity
79  (ADAPTIVESINGULAR or QAGS in the QUADPACK convention) applying a Gauss-Kronrod 21-point integration rule.
80  In the case of ADAPTIVE type, the integration rule can also be specified via the
81  Integration::GKRule. The default rule is 31 points.
82 
83  In the case of integration over infinite and semi-infinite ranges, the type used is always
84  ADAPTIVESINGULAR applying a transformation from the original interval into (0,1).
85 
86  The ADAPTIVESINGULAR type is the most sophicticated type. When performances are
87  important, it is then recommened to use the NONADAPTIVE type in case of smooth functions or
88  ADAPTIVE with a lower Gauss-Kronrod rule.
89 
90  For detailed description on GSL integration algorithms see the
91  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Numerical-Integration.html">GSL Manual</A>.
92 
93 
94  @ingroup Integration
95  */
96 
97 
99 
100  public:
101 
102 
103 
104  // constructors
105 
106 
107  /** Default constructor of GSL Integrator for Adaptive Singular integration
108 
109  @param absTol desired absolute Error
110  @param relTol desired relative Error
111  @param size maximum number of sub-intervals
112  */
113 
114  GSLIntegrator(double absTol = 1.E-9, double relTol = 1E-6, size_t size = 1000);
115 
116 
117 
118 
119  /** constructor of GSL Integrator. In the case of Adaptive integration the Gauss-Krond rule of 31 points is used
120 
121  @param type type of integration. The possible types are defined in the Integration::Type enumeration
122  @param absTol desired absolute Error
123  @param relTol desired relative Error
124  @param size maximum number of sub-intervals
125  */
126 
127 
128  GSLIntegrator(const Integration::Type type, double absTol = 1.E-9, double relTol = 1E-6, size_t size = 1000);
129 
130 
131  /**
132  generic constructor for GSL Integrator
133 
134  @param type type of integration. The possible types are defined in the Integration::Type enumeration
135  @param rule Gauss-Kronrod rule. It is used only for ADAPTIVE::Integration types. The possible rules are defined in the Integration::GKRule enumeration
136  @param absTol desired absolute Error
137  @param relTol desired relative Error
138  @param size maximum number of sub-intervals
139 
140  */
141 
142  GSLIntegrator(const Integration::Type type, const Integration::GKRule rule, double absTol = 1.E-9, double relTol = 1E-6, size_t size = 1000);
143 
144 
145  /** constructor of GSL Integrator. In the case of Adaptive integration the Gauss-Krond rule of 31 points is used
146  This is used by the plug-in manager (need a char * instead of enumerations)
147 
148  @param type type of integration. The possible types are defined in the Integration::Type enumeration
149  @param rule Gauss-Kronrod rule (from 1 to 6)
150  @param absTol desired absolute Error
151  @param relTol desired relative Error
152  @param size maximum number of sub-intervals
153  */
154  GSLIntegrator(const char * type, int rule, double absTol, double relTol, size_t size );
155 
156  virtual ~GSLIntegrator();
157  //~GSLIntegrator();
158 
159  // disable copy ctrs
160  private:
161 
162  GSLIntegrator(const GSLIntegrator &);
164 
165  public:
166 
167 
168  // template methods for generic functors
169 
170  /**
171  method to set the a generic integration function
172 
173  @param f integration function. The function type must implement the assigment operator, <em> double operator() ( double x ) </em>
174 
175  */
176 
177 
178  void SetFunction(const IGenFunction &f);
179 
180  /**
181  Set function from a GSL pointer function type
182  */
183  void SetFunction( GSLFuncPointer f, void * p = 0);
184 
185  // methods using IGenFunction
186 
187  /**
188  evaluate the Integral of a function f over the defined interval (a,b)
189  @param f integration function. The function type must implement the mathlib::IGenFunction interface
190  @param a lower value of the integration interval
191  @param b upper value of the integration interval
192  */
193 
194  double Integral(const IGenFunction & f, double a, double b);
195 
196 
197  /**
198  evaluate the Integral of a function f over the infinite interval (-inf,+inf)
199  @param f integration function. The function type must implement the mathlib::IGenFunction interface
200  */
201 
202  double Integral(const IGenFunction & f);
203 
204  /**
205  evaluate the Cauchy principal value of the integral of a previously defined function f over
206  the defined interval (a,b) with a singularity at c
207  @param a lower interval value
208  @param b lower interval value
209  @param c singular value of f
210  */
211  double IntegralCauchy(double a, double b, double c);
212 
213  /**
214  evaluate the Cauchy principal value of the integral of a function f over the defined interval (a,b)
215  with a singularity at c
216  @param f integration function. The function type must implement the mathlib::IGenFunction interface
217  @param a lower interval value
218  @param b lower interval value
219  @param c singular value of f
220  */
221  double IntegralCauchy(const IGenFunction & f, double a, double b, double c);
222 
223  /**
224  evaluate the Integral of a function f over the semi-infinite interval (a,+inf)
225  @param f integration function. The function type must implement the mathlib::IGenFunction interface
226  @param a lower value of the integration interval
227 
228  */
229  double IntegralUp(const IGenFunction & f, double a );
230 
231  /**
232  evaluate the Integral of a function f over the over the semi-infinite interval (-inf,b)
233  @param f integration function. The function type must implement the mathlib::IGenFunction interface
234  @param b upper value of the integration interval
235  */
236  double IntegralLow(const IGenFunction & f, double b );
237 
238  /**
239  evaluate the Integral of a function f with known singular points over the defined Integral (a,b)
240  @param f integration function. The function type must implement the mathlib::IGenFunction interface
241  @param pts vector containing both the function singular points and the lower/upper edges of the interval. The vector must have as first element the lower edge of the integration Integral ( \a a) and last element the upper value.
242 
243  */
244  double Integral(const IGenFunction & f, const std::vector<double> & pts );
245 
246  // evaluate using cached function
247 
248  /**
249  evaluate the Integral over the defined interval (a,b) using the function previously set with GSLIntegrator::SetFunction method
250  @param a lower value of the integration interval
251  @param b upper value of the integration interval
252  */
253 
254  double Integral(double a, double b);
255 
256 
257  /**
258  evaluate the Integral over the infinite interval (-inf,+inf) using the function previously set with GSLIntegrator::SetFunction method.
259  */
260  double Integral( );
261 
262  /**
263  evaluate the Integral of a function f over the semi-infinite interval (a,+inf) using the function previously set with GSLIntegrator::SetFunction method.
264  @param a lower value of the integration interval
265  */
266  double IntegralUp(double a );
267 
268  /**
269  evaluate the Integral of a function f over the over the semi-infinite interval (-inf,b) using the function previously set with GSLIntegrator::SetFunction method.
270  @param b upper value of the integration interval
271  */
272  double IntegralLow( double b );
273 
274  /**
275  evaluate the Integral over the defined interval (a,b) using the function previously set with GSLIntegrator::SetFunction method. The function has known singular points.
276  @param pts vector containing both the function singular points and the lower/upper edges of the interval. The vector must have as first element the lower edge of the integration Integral ( \a a) and last element the upper value.
277 
278  */
279  double Integral( const std::vector<double> & pts);
280 
281  // evaluate using free function pointer (same GSL signature)
282 
283  /**
284  signature for function pointers used by GSL
285  */
286  //typedef double ( * GSLFuncPointer ) ( double, void * );
287 
288  /**
289  evaluate the Integral of of a function f over the defined interval (a,b) passing a free function pointer
290  The integration function must be a free function and have a signature consistent with GSL functions:
291 
292  <em>double my_function ( double x, void * p ) { ...... } </em>
293 
294  This method is the most efficient since no internal adapter to GSL function is created.
295  @param f pointer to the integration function
296  @param p pointer to the Parameters of the function
297  @param a lower value of the integration interval
298  @param b upper value of the integration interval
299 
300  */
301  double Integral(GSLFuncPointer f, void * p, double a, double b);
302 
303  /**
304  evaluate the Integral of a function f over the infinite interval (-inf,+inf) passing a free function pointer
305  */
306  double Integral(GSLFuncPointer f, void * p);
307 
308  /**
309  evaluate the Integral of a function f over the semi-infinite interval (a,+inf) passing a free function pointer
310  */
311  double IntegralUp(GSLFuncPointer f, void * p, double a);
312 
313  /**
314  evaluate the Integral of a function f over the over the semi-infinite interval (-inf,b) passing a free function pointer
315  */
316  double IntegralLow(GSLFuncPointer f, void * p, double b);
317 
318  /**
319  evaluate the Integral of a function f with knows singular points over the over a defined interval passing a free function pointer
320  */
321  double Integral(GSLFuncPointer f, void * p, const std::vector<double> & pts);
322 
323  /**
324  return the Result of the last Integral calculation
325  */
326  double Result() const;
327 
328  /**
329  return the estimate of the absolute Error of the last Integral calculation
330  */
331  double Error() const;
332 
333  /**
334  return the Error Status of the last Integral calculation
335  */
336  int Status() const;
337 
338  /**
339  return number of function evaluations in calculating the integral
340  */
341  int NEval() const { return fNEval; }
342 
343  // setter for control Parameters (getters are not needed so far )
344 
345  /**
346  set the desired relative Error
347  */
348  void SetRelTolerance(double relTolerance);
349 
350 
351  /**
352  set the desired absolute Error
353  */
354  void SetAbsTolerance(double absTolerance);
355 
356  /**
357  set the integration rule (Gauss-Kronrod rule).
358  The possible rules are defined in the Integration::GKRule enumeration.
359  The integration rule can be modified only for ADAPTIVE type integrations
360  */
362 
363  /// set the options
364  virtual void SetOptions(const ROOT::Math::IntegratorOneDimOptions & opt);
365 
366  /// get the option used for the integration
368 
369  /// get type name
371 
372  /**
373  return the name
374  */
375  const char * GetTypeName() const;
376 
377 
378  protected:
379 
380  // internal method to check validity of GSL function pointer
381  bool CheckFunction();
382 
383 
384  private:
385 
388  double fAbsTol;
389  double fRelTol;
390  size_t fSize;
392 
393  // cache Error, Result and Status of integration
394 
395  double fResult;
396  double fError;
397  int fStatus;
398  int fNEval;
399 
400  // GSLIntegrationAlgorithm * fAlgorithm;
401 
404 
405  };
406 
407 
408 
409 
410 
411 } // namespace Math
412 } // namespace ROOT
413 
414 
415 #endif /* ROOT_Math_GSLIntegrator */
double Result() const
return the Result of the last Integral calculation
GKRule
enumeration specifying the Gauss-KronRod integration rule for ADAPTIVE integration type ...
Interface (abstract) class for 1D numerical integration It must be implemented by the concrate Integr...
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
const double absTol
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
double IntegralUp(const IGenFunction &f, double a)
evaluate the Integral of a function f over the semi-infinite interval (a,+inf)
void SetIntegrationRule(Integration::GKRule)
set the integration rule (Gauss-Kronrod rule).
virtual ROOT::Math::IntegratorOneDimOptions Options() const
get the option used for the integration
TArc * a
Definition: textangle.C:12
GSLFunctionWrapper * fFunction
const char * GetTypeName() const
return the name
virtual void SetOptions(const ROOT::Math::IntegratorOneDimOptions &opt)
set the options
Class for performing numerical integration of a function in one dimension.
Definition: GSLIntegrator.h:98
Integration::Type fType
double IntegralLow(const IGenFunction &f, double b)
evaluate the Integral of a function f over the over the semi-infinite interval (-inf,b)
double(* GSLFuncPointer)(double, void *)
Function pointer corresponding to gsl_function signature.
int Status() const
return the Error Status of the last Integral calculation
double Integral()
evaluate the Integral over the infinite interval (-inf,+inf) using the function previously set with G...
int NEval() const
return number of function evaluations in calculating the integral
Numerical one dimensional integration options.
void SetFunction(const IGenFunction &f)
method to set the a generic integration function
Double_t E()
Definition: TMath.h:54
Type
enumeration specifying the integration types.
IntegrationOneDim::Type GetType() const
get type name
GSLIntegrator(double absTol=1.E-9, double relTol=1E-6, size_t size=1000)
Default constructor of GSL Integrator for Adaptive Singular integration.
void SetRelTolerance(double relTolerance)
set the desired relative Error
double f(double x)
int type
Definition: TGX11.cxx:120
GSLIntegrationWorkspace * fWorkspace
double IntegralCauchy(double a, double b, double c)
evaluate the Cauchy principal value of the integral of a previously defined function f over the defin...
Namespace for new Math classes and functions.
void SetAbsTolerance(double absTolerance)
set the desired absolute Error
GSLIntegrator & operator=(const GSLIntegrator &)
Wrapper class to the gsl_function C structure.
Integration::GKRule fRule
double Error() const
return the estimate of the absolute Error of the last Integral calculation