GammaLib  2.1.0.dev
 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-2023 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 adaptive_gauss_kronrod(const double& a, const double& b) const;
87  double gauss_kronrod(const double& a, const double& b) const;
88  std::string print(const GChatter& chatter = NORMAL) const;
89 
90 protected:
91  // Protected methods
92  void init_members(void);
93  void copy_members(const GIntegral& integral);
94  void free_members(void);
95  double polint(double* xa, double* ya, int n, double x, double *dy);
96  double adaptive_simpson_aux(const double& a, const double& b,
97  const double& eps, const double& S,
98  const double& fa, const double& fb,
99  const double& fc,
100  const int& bottom) const;
101  double adaptive_gauss_kronrod_aux(const double& a, const double& b,
102  const double& fa, const double& fb,
103  const double& is,
104  const double& toler) const;
105  double rescale_error(double err,
106  const double& result_abs,
107  const double& result_asc) const;
108 
109  // Protected data area
110  GFunction* m_kernel; //!< Pointer to function kernel
111  double m_eps; //!< Requested relative integration precision
112  int m_max_iter; //!< Maximum number of iterations
113  int m_fix_iter; //!< Fixed number of iterations
114  bool m_silent; //!< Suppress integration warnings in console
115 
116  // Integrator results
117  mutable int m_iter; //!< Number of iterations used
118  mutable int m_calls; //!< Number of function calls used
119  mutable bool m_isvalid; //!< Integration result valid (true=yes)
120  mutable bool m_has_abserr; //!< Has absolute integration error
121  mutable bool m_has_relerr; //!< Has relative integration error
122  mutable double m_abserr; //!< Absolute integration error
123  mutable double m_relerr; //!< Absolute integration error
124  mutable std::string m_message; //!< Status message (if result is invalid)
125  mutable bool m_terminate; //!< Signals termination of subdivision
126 };
127 
128 
129 /***********************************************************************//**
130  * @brief Return class name
131  *
132  * @return String containing the class name ("GIntegral").
133  ***************************************************************************/
134 inline
135 std::string GIntegral::classname(void) const
136 {
137  return ("GIntegral");
138 }
139 
140 
141 /***********************************************************************//**
142  * @brief Return number of iterations
143  *
144  * @return Number of iterations.
145  ***************************************************************************/
146 inline
147 const int& GIntegral::iter(void) const
148 {
149  return m_iter;
150 }
151 
152 
153 /***********************************************************************//**
154  * @brief Set maximum number of iterations
155  *
156  * @param[in] iter Maximum number of iterations.
157  ***************************************************************************/
158 inline
159 void GIntegral::max_iter(const int& iter)
160 {
161  m_max_iter = iter;
162  return;
163 }
164 
165 
166 /***********************************************************************//**
167  * @brief Return maximum number of iterations
168  *
169  * @return Maximum number of iterations.
170  ***************************************************************************/
171 inline
172 const int& GIntegral::max_iter(void) const
173 {
174  return m_max_iter;
175 }
176 
177 
178 /***********************************************************************//**
179  * @brief Set fixed number of iterations
180  *
181  * @param[in] iter Fixed number of iterations.
182  *
183  * If the fixed number of iterations is set, the integration algorithm will
184  * always performed the given number of iterations, irrespectively of the
185  * precision that is reached. This feature is relevant for computing
186  * numerical derivates from numerically integrated functions.
187  ***************************************************************************/
188 inline
189 void GIntegral::fixed_iter(const int& iter)
190 {
191  m_fix_iter = iter;
192  return;
193 }
194 
195 
196 /***********************************************************************//**
197  * @brief Return fixed number of iterations
198  *
199  * @return Fixed number of iterations.
200  ***************************************************************************/
201 inline
202 const int& GIntegral::fixed_iter(void) const
203 {
204  return m_fix_iter;
205 }
206 
207 
208 /***********************************************************************//**
209  * @brief Set relative precision
210  *
211  * @param[in] eps Relative precision.
212  ***************************************************************************/
213 inline
214 void GIntegral::eps(const double& eps)
215 {
216  m_eps = eps;
217  return;
218 }
219 
220 
221 /***********************************************************************//**
222  * @brief Get relative precision
223  *
224  * @return Relative precision.
225  ***************************************************************************/
226 inline
227 const double& GIntegral::eps(void) const
228 {
229  return m_eps;
230 }
231 
232 
233 /***********************************************************************//**
234  * @brief Get number of function calls
235  *
236  * @return Number of function calls.
237  ***************************************************************************/
238 inline
239 const int& GIntegral::calls(void) const
240 {
241  return m_calls;
242 }
243 
244 
245 /***********************************************************************//**
246  * @brief Set silence flag
247  *
248  * @param[in] silent Silence flag.
249  ***************************************************************************/
250 inline
251 void GIntegral::silent(const bool& silent)
252 {
253  m_silent = silent;
254  return;
255 }
256 
257 
258 /***********************************************************************//**
259  * @brief Get silence flag
260  *
261  * @return True is class is silent, false otherwise.
262  ***************************************************************************/
263 inline
264 const bool& GIntegral::silent(void) const
265 {
266  return m_silent;
267 }
268 
269 
270 /***********************************************************************//**
271  * @brief Set function kernel
272  *
273  * @param[in] kernel Function kernel.
274  *
275  * Sets the function kernel for which the integral should be determined.
276  ***************************************************************************/
277 inline
279 {
280  m_kernel = kernel;
281  return;
282 }
283 
284 
285 /***********************************************************************//**
286  * @brief Get function kernel
287  *
288  * @return Function kernel.
289  ***************************************************************************/
290 inline
291 const GFunction* GIntegral::kernel(void) const
292 {
293  return m_kernel;
294 }
295 
296 
297 /***********************************************************************//**
298  * @brief Signal if integration result is valid
299  *
300  * @return True is integration result is valid.
301  ***************************************************************************/
302 inline
303 const bool& GIntegral::is_valid(void) const
304 {
305  return m_isvalid;
306 }
307 
308 
309 /***********************************************************************//**
310  * @brief Return integration status message
311  *
312  * @return Integration status message.
313  ***************************************************************************/
314 inline
315 const std::string& GIntegral::message(void) const
316 {
317  return m_message;
318 }
319 
320 #endif /* GINTEGRAL_HPP */
double adaptive_gauss_kronrod_aux(const double &a, const double &b, const double &fa, const double &fb, const double &is, const double &toler) const
Adaptive Gauss-Lobatto-Kronrod integration helper.
Definition: GIntegral.cpp:1335
const double & eps(void) const
Get relative precision.
Definition: GIntegral.hpp:227
const int & fixed_iter(void) const
Return fixed number of iterations.
Definition: GIntegral.hpp:202
double romberg(std::vector< double > bounds, const int &order=5)
Perform Romberg integration.
Definition: GIntegral.cpp:382
int m_iter
Number of iterations used.
Definition: GIntegral.hpp:117
const bool & silent(void) const
Get silence flag.
Definition: GIntegral.hpp:264
void init_members(void)
Initialise class members.
Definition: GIntegral.cpp:1109
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:1188
double rescale_error(double err, const double &result_abs, const double &result_asc) const
Rescale errors for Gauss-Kronrod integration.
Definition: GIntegral.cpp:1405
std::string classname(void) const
Return class name.
Definition: GIntegral.hpp:135
int m_max_iter
Maximum number of iterations.
Definition: GIntegral.hpp:112
const std::string & message(void) const
Return integration status message.
Definition: GIntegral.hpp:315
GIntegral class interface definition.
Definition: GIntegral.hpp:46
double gauss_kronrod(const double &a, const double &b) const
Gauss-Kronrod integration.
Definition: GIntegral.cpp:860
int m_fix_iter
Fixed number of iterations.
Definition: GIntegral.hpp:113
GIntegral & operator=(const GIntegral &integral)
Assignment operator.
Definition: GIntegral.cpp:306
void clear(void)
Clear integral.
Definition: GIntegral.cpp:336
const int & iter(void) const
Return number of iterations.
Definition: GIntegral.hpp:147
bool m_isvalid
Integration result valid (true=yes)
Definition: GIntegral.hpp:119
GIntegral(void)
Void constructor.
Definition: GIntegral.cpp:232
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:586
bool m_has_abserr
Has absolute integration error.
Definition: GIntegral.hpp:120
std::string m_message
Status message (if result is invalid)
Definition: GIntegral.hpp:124
GChatter
Definition: GTypemaps.hpp:33
bool m_has_relerr
Has relative integration error.
Definition: GIntegral.hpp:121
const int & calls(void) const
Get number of function calls.
Definition: GIntegral.hpp:239
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:1267
const GFunction * kernel(void) const
Get function kernel.
Definition: GIntegral.hpp:291
double m_relerr
Absolute integration error.
Definition: GIntegral.hpp:123
void copy_members(const GIntegral &integral)
Copy class members.
Definition: GIntegral.cpp:1138
double m_eps
Requested relative integration precision.
Definition: GIntegral.hpp:111
bool m_silent
Suppress integration warnings in console.
Definition: GIntegral.hpp:114
std::string print(const GChatter &chatter=NORMAL) const
Print integral information.
Definition: GIntegral.cpp:1040
virtual ~GIntegral(void)
Destructor.
Definition: GIntegral.cpp:284
Single parameter function abstract base class.
Definition: GFunction.hpp:44
const int & max_iter(void) const
Return maximum number of iterations.
Definition: GIntegral.hpp:172
GFunction * m_kernel
Pointer to function kernel.
Definition: GIntegral.hpp:110
int m_calls
Number of function calls used.
Definition: GIntegral.hpp:118
bool m_terminate
Signals termination of subdivision.
Definition: GIntegral.hpp:125
double adaptive_gauss_kronrod(const double &a, const double &b) const
Adaptive Gauss-Lobatto-Kronrod integration.
Definition: GIntegral.cpp:770
const bool & is_valid(void) const
Signal if integration result is valid.
Definition: GIntegral.hpp:303
double m_abserr
Absolute integration error.
Definition: GIntegral.hpp:122
void free_members(void)
Delete class members.
Definition: GIntegral.cpp:1165
GIntegral * clone(void) const
Clone integral.
Definition: GIntegral.cpp:354
double adaptive_simpson(const double &a, const double &b) const
Adaptive Simpson&#39;s integration.
Definition: GIntegral.cpp:709