GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GIntegral.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GIntegral.hpp - Integration class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2020 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GIntegral.hpp
23  * @brief Integration class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GINTEGRAL_HPP
28 #define GINTEGRAL_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <vector>
33 #include "GBase.hpp"
34 
35 /* __ Forward declarations _______________________________________________ */
36 class GFunction;
37 
38 /***********************************************************************//**
39  * @class GIntegral
40  *
41  * @brief GIntegral class interface definition.
42  *
43  * This class allows to perform integration using various methods. The
44  * integrand is implemented by a derived class of GFunction.
45  ***************************************************************************/
46 class GIntegral : public GBase {
47 
48 public:
49 
50  // Constructors and destructors
51  explicit GIntegral(void);
52  explicit GIntegral(GFunction* kernel);
53  GIntegral(const GIntegral& integral);
54  virtual ~GIntegral(void);
55 
56  // Operators
57  GIntegral& operator=(const GIntegral& integral);
58 
59  // Methods
60  void clear(void);
61  GIntegral* clone(void) const;
62  std::string classname(void) const;
63  void max_iter(const int& iter);
64  const int& max_iter(void) const;
65  void fixed_iter(const int& iter);
66  const int& fixed_iter(void) const;
67  void eps(const double& eps);
68  const double& eps(void) const;
69  void silent(const bool& silent);
70  const bool& silent(void) const;
71  const int& iter(void) const;
72  const int& calls(void) const;
73  const bool& is_valid(void) const;
74  const std::string& message(void) const;
75  void kernel(GFunction* kernel);
76  const GFunction* kernel(void) const;
77  double romberg(std::vector<double> bounds,
78  const int& order = 5);
79  double romberg(const double& a, const double& b,
80  const int& order = 5);
81  double trapzd(const double& a,
82  const double& b,
83  const int& n = 1,
84  double result = 0.0);
85  double adaptive_simpson(const double& a, const double& b) const;
86  double gauss_kronrod(const double& a, const double& b) const;
87  std::string print(const GChatter& chatter = NORMAL) const;
88 
89 protected:
90  // Protected methods
91  void init_members(void);
92  void copy_members(const GIntegral& integral);
93  void free_members(void);
94  double polint(double* xa, double* ya, int n, double x, double *dy);
95  double adaptive_simpson_aux(const double& a, const double& b,
96  const double& eps, const double& S,
97  const double& fa, const double& fb,
98  const double& fc,
99  const int& bottom) const;
100  double rescale_error(double err,
101  const double& result_abs,
102  const double& result_asc) const;
103 
104  // Protected data area
105  GFunction* m_kernel; //!< Pointer to function kernel
106  double m_eps; //!< Requested relative integration precision
107  int m_max_iter; //!< Maximum number of iterations
108  int m_fix_iter; //!< Fixed number of iterations
109  bool m_silent; //!< Suppress integration warnings in console
110 
111  // Integrator results
112  mutable int m_iter; //!< Number of iterations used
113  mutable int m_calls; //!< Number of function calls used
114  mutable bool m_isvalid; //!< Integration result valid (true=yes)
115  mutable bool m_has_abserr; //!< Has absolute integration error
116  mutable bool m_has_relerr; //!< Has relative integration error
117  mutable double m_abserr; //!< Absolute integration error
118  mutable double m_relerr; //!< Absolute integration error
119  mutable std::string m_message; //!< Status message (if result is invalid)
120 };
121 
122 
123 /***********************************************************************//**
124  * @brief Return class name
125  *
126  * @return String containing the class name ("GIntegral").
127  ***************************************************************************/
128 inline
129 std::string GIntegral::classname(void) const
130 {
131  return ("GIntegral");
132 }
133 
134 
135 /***********************************************************************//**
136  * @brief Return number of iterations
137  *
138  * @return Number of iterations.
139  ***************************************************************************/
140 inline
141 const int& GIntegral::iter(void) const
142 {
143  return m_iter;
144 }
145 
146 
147 /***********************************************************************//**
148  * @brief Set maximum number of iterations
149  *
150  * @param[in] iter Maximum number of iterations.
151  ***************************************************************************/
152 inline
153 void GIntegral::max_iter(const int& iter)
154 {
155  m_max_iter = iter;
156  return;
157 }
158 
159 
160 /***********************************************************************//**
161  * @brief Return maximum number of iterations
162  *
163  * @return Maximum number of iterations.
164  ***************************************************************************/
165 inline
166 const int& GIntegral::max_iter(void) const
167 {
168  return m_max_iter;
169 }
170 
171 
172 /***********************************************************************//**
173  * @brief Set fixed number of iterations
174  *
175  * @param[in] iter Fixed number of iterations.
176  *
177  * If the fixed number of iterations is set, the integration algorithm will
178  * always performed the given number of iterations, irrespectively of the
179  * precision that is reached. This feature is relevant for computing
180  * numerical derivates from numerically integrated functions.
181  ***************************************************************************/
182 inline
183 void GIntegral::fixed_iter(const int& iter)
184 {
185  m_fix_iter = iter;
186  return;
187 }
188 
189 
190 /***********************************************************************//**
191  * @brief Return fixed number of iterations
192  *
193  * @return Fixed number of iterations.
194  ***************************************************************************/
195 inline
196 const int& GIntegral::fixed_iter(void) const
197 {
198  return m_fix_iter;
199 }
200 
201 
202 /***********************************************************************//**
203  * @brief Set relative precision
204  *
205  * @param[in] eps Relative precision.
206  ***************************************************************************/
207 inline
208 void GIntegral::eps(const double& eps)
209 {
210  m_eps = eps;
211  return;
212 }
213 
214 
215 /***********************************************************************//**
216  * @brief Get relative precision
217  *
218  * @return Relative precision.
219  ***************************************************************************/
220 inline
221 const double& GIntegral::eps(void) const
222 {
223  return m_eps;
224 }
225 
226 
227 /***********************************************************************//**
228  * @brief Get number of function calls
229  *
230  * @return Number of function calls.
231  ***************************************************************************/
232 inline
233 const int& GIntegral::calls(void) const
234 {
235  return m_calls;
236 }
237 
238 
239 /***********************************************************************//**
240  * @brief Set silence flag
241  *
242  * @param[in] silent Silence flag.
243  ***************************************************************************/
244 inline
245 void GIntegral::silent(const bool& silent)
246 {
247  m_silent = silent;
248  return;
249 }
250 
251 
252 /***********************************************************************//**
253  * @brief Get silence flag
254  *
255  * @return True is class is silent, false otherwise.
256  ***************************************************************************/
257 inline
258 const bool& GIntegral::silent(void) const
259 {
260  return m_silent;
261 }
262 
263 
264 /***********************************************************************//**
265  * @brief Set function kernel
266  *
267  * @param[in] kernel Function kernel.
268  *
269  * Sets the function kernel for which the integral should be determined.
270  ***************************************************************************/
271 inline
273 {
274  m_kernel = kernel;
275  return;
276 }
277 
278 
279 /***********************************************************************//**
280  * @brief Get function kernel
281  *
282  * @return Function kernel.
283  ***************************************************************************/
284 inline
285 const GFunction* GIntegral::kernel(void) const
286 {
287  return m_kernel;
288 }
289 
290 
291 /***********************************************************************//**
292  * @brief Signal if integration result is valid
293  *
294  * @return True is integration result is valid.
295  ***************************************************************************/
296 inline
297 const bool& GIntegral::is_valid(void) const
298 {
299  return m_isvalid;
300 }
301 
302 
303 /***********************************************************************//**
304  * @brief Return integration status message
305  *
306  * @return Integration status message.
307  ***************************************************************************/
308 inline
309 const std::string& GIntegral::message(void) const
310 {
311  return m_message;
312 }
313 
314 #endif /* GINTEGRAL_HPP */
const double & eps(void) const
Get relative precision.
Definition: GIntegral.hpp:221
const int & fixed_iter(void) const
Return fixed number of iterations.
Definition: GIntegral.hpp:196
double romberg(std::vector< double > bounds, const int &order=5)
Perform Romberg integration.
Definition: GIntegral.cpp:381
int m_iter
Number of iterations used.
Definition: GIntegral.hpp:112
const bool & silent(void) const
Get silence flag.
Definition: GIntegral.hpp:258
void init_members(void)
Initialise class members.
Definition: GIntegral.cpp:1015
Definition of interface for all GammaLib classes.
double polint(double *xa, double *ya, int n, double x, double *dy)
Perform Polynomial interpolation.
Definition: GIntegral.cpp:1094
double rescale_error(double err, const double &result_abs, const double &result_asc) const
Rescale errors for Gauss-Kronrod integration.
Definition: GIntegral.cpp:1236
std::string classname(void) const
Return class name.
Definition: GIntegral.hpp:129
int m_max_iter
Maximum number of iterations.
Definition: GIntegral.hpp:107
const std::string & message(void) const
Return integration status message.
Definition: GIntegral.hpp:309
GIntegral class interface definition.
Definition: GIntegral.hpp:46
double gauss_kronrod(const double &a, const double &b) const
Gauss-Kronrod integration.
Definition: GIntegral.cpp:766
int m_fix_iter
Fixed number of iterations.
Definition: GIntegral.hpp:108
GIntegral & operator=(const GIntegral &integral)
Assignment operator.
Definition: GIntegral.cpp:305
void clear(void)
Clear integral.
Definition: GIntegral.cpp:335
const int & iter(void) const
Return number of iterations.
Definition: GIntegral.hpp:141
bool m_isvalid
Integration result valid (true=yes)
Definition: GIntegral.hpp:114
GIntegral(void)
Void constructor.
Definition: GIntegral.cpp:231
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
double trapzd(const double &a, const double &b, const int &n=1, double result=0.0)
Perform Trapezoidal integration.
Definition: GIntegral.cpp:585
bool m_has_abserr
Has absolute integration error.
Definition: GIntegral.hpp:115
std::string m_message
Status message (if result is invalid)
Definition: GIntegral.hpp:119
GChatter
Definition: GTypemaps.hpp:33
bool m_has_relerr
Has relative integration error.
Definition: GIntegral.hpp:116
const int & calls(void) const
Get number of function calls.
Definition: GIntegral.hpp:233
double adaptive_simpson_aux(const double &a, const double &b, const double &eps, const double &S, const double &fa, const double &fb, const double &fc, const int &bottom) const
Auxiliary function for adaptive Simpson&#39;s method.
Definition: GIntegral.cpp:1173
const GFunction * kernel(void) const
Get function kernel.
Definition: GIntegral.hpp:285
double m_relerr
Absolute integration error.
Definition: GIntegral.hpp:118
void copy_members(const GIntegral &integral)
Copy class members.
Definition: GIntegral.cpp:1044
double m_eps
Requested relative integration precision.
Definition: GIntegral.hpp:106
bool m_silent
Suppress integration warnings in console.
Definition: GIntegral.hpp:109
std::string print(const GChatter &chatter=NORMAL) const
Print integral information.
Definition: GIntegral.cpp:946
virtual ~GIntegral(void)
Destructor.
Definition: GIntegral.cpp:283
Single parameter function abstract base class.
Definition: GFunction.hpp:44
const int & max_iter(void) const
Return maximum number of iterations.
Definition: GIntegral.hpp:166
GFunction * m_kernel
Pointer to function kernel.
Definition: GIntegral.hpp:105
int m_calls
Number of function calls used.
Definition: GIntegral.hpp:113
const bool & is_valid(void) const
Signal if integration result is valid.
Definition: GIntegral.hpp:297
double m_abserr
Absolute integration error.
Definition: GIntegral.hpp:117
void free_members(void)
Delete class members.
Definition: GIntegral.cpp:1071
GIntegral * clone(void) const
Clone integral.
Definition: GIntegral.cpp:353
double adaptive_simpson(const double &a, const double &b) const
Adaptive Simpson&#39;s integration.
Definition: GIntegral.cpp:708