GammaLib 2.0.0
Loading...
Searching...
No Matches
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 _______________________________________________ */
36class 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 ***************************************************************************/
46class GIntegral : public GBase {
47
48public:
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
89protected:
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 ***************************************************************************/
128inline
129std::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 ***************************************************************************/
140inline
141const 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 ***************************************************************************/
152inline
153void GIntegral::max_iter(const int& iter)
154{
156 return;
157}
158
159
160/***********************************************************************//**
161 * @brief Return maximum number of iterations
162 *
163 * @return Maximum number of iterations.
164 ***************************************************************************/
165inline
166const 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 ***************************************************************************/
182inline
183void GIntegral::fixed_iter(const int& iter)
184{
186 return;
187}
188
189
190/***********************************************************************//**
191 * @brief Return fixed number of iterations
192 *
193 * @return Fixed number of iterations.
194 ***************************************************************************/
195inline
196const 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 ***************************************************************************/
207inline
208void 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 ***************************************************************************/
220inline
221const 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 ***************************************************************************/
232inline
233const 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 ***************************************************************************/
244inline
245void GIntegral::silent(const bool& silent)
246{
248 return;
249}
250
251
252/***********************************************************************//**
253 * @brief Get silence flag
254 *
255 * @return True is class is silent, false otherwise.
256 ***************************************************************************/
257inline
258const 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 ***************************************************************************/
271inline
273{
275 return;
276}
277
278
279/***********************************************************************//**
280 * @brief Get function kernel
281 *
282 * @return Function kernel.
283 ***************************************************************************/
284inline
285const 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 ***************************************************************************/
296inline
297const 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 ***************************************************************************/
308inline
309const std::string& GIntegral::message(void) const
310{
311 return m_message;
312}
313
314#endif /* GINTEGRAL_HPP */
Definition of interface for all GammaLib classes.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Single parameter function abstract base class.
Definition GFunction.hpp:44
GIntegral class interface definition.
Definition GIntegral.hpp:46
const bool & silent(void) const
Get silence flag.
const std::string & message(void) const
Return integration status message.
std::string m_message
Status message (if result is invalid)
int m_calls
Number of function calls used.
const double & eps(void) const
Get relative precision.
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's method.
const int & iter(void) const
Return number of iterations.
double romberg(std::vector< double > bounds, const int &order=5)
Perform Romberg integration.
double m_relerr
Absolute integration error.
void copy_members(const GIntegral &integral)
Copy class members.
double m_eps
Requested relative integration precision.
const int & fixed_iter(void) const
Return fixed number of iterations.
const int & calls(void) const
Get number of function calls.
bool m_has_abserr
Has absolute integration error.
bool m_has_relerr
Has relative integration error.
GIntegral * clone(void) const
Clone integral.
double trapzd(const double &a, const double &b, const int &n=1, double result=0.0)
Perform Trapezoidal integration.
void clear(void)
Clear integral.
std::string classname(void) const
Return class name.
bool m_silent
Suppress integration warnings in console.
const GFunction * kernel(void) const
Get function kernel.
int m_iter
Number of iterations used.
double gauss_kronrod(const double &a, const double &b) const
Gauss-Kronrod integration.
virtual ~GIntegral(void)
Destructor.
int m_max_iter
Maximum number of iterations.
void init_members(void)
Initialise class members.
double adaptive_simpson(const double &a, const double &b) const
Adaptive Simpson's integration.
GFunction * m_kernel
Pointer to function kernel.
std::string print(const GChatter &chatter=NORMAL) const
Print integral information.
GIntegral(void)
Void constructor.
void free_members(void)
Delete class members.
GIntegral & operator=(const GIntegral &integral)
Assignment operator.
const int & max_iter(void) const
Return maximum number of iterations.
double rescale_error(double err, const double &result_abs, const double &result_asc) const
Rescale errors for Gauss-Kronrod integration.
const bool & is_valid(void) const
Signal if integration result is valid.
double m_abserr
Absolute integration error.
bool m_isvalid
Integration result valid (true=yes)
double polint(double *xa, double *ya, int n, double x, double *dy)
Perform Polynomial interpolation.
int m_fix_iter
Fixed number of iterations.