GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
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 _______________________________________________ */
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 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
90protected:
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 ***************************************************************************/
134inline
135std::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 ***************************************************************************/
146inline
147const 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 ***************************************************************************/
158inline
159void GIntegral::max_iter(const int& iter)
160{
162 return;
163}
164
165
166/***********************************************************************//**
167 * @brief Return maximum number of iterations
168 *
169 * @return Maximum number of iterations.
170 ***************************************************************************/
171inline
172const 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 ***************************************************************************/
188inline
189void GIntegral::fixed_iter(const int& iter)
190{
192 return;
193}
194
195
196/***********************************************************************//**
197 * @brief Return fixed number of iterations
198 *
199 * @return Fixed number of iterations.
200 ***************************************************************************/
201inline
202const 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 ***************************************************************************/
213inline
214void 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 ***************************************************************************/
226inline
227const 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 ***************************************************************************/
238inline
239const 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 ***************************************************************************/
250inline
251void GIntegral::silent(const bool& silent)
252{
254 return;
255}
256
257
258/***********************************************************************//**
259 * @brief Get silence flag
260 *
261 * @return True is class is silent, false otherwise.
262 ***************************************************************************/
263inline
264const 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 ***************************************************************************/
277inline
279{
281 return;
282}
283
284
285/***********************************************************************//**
286 * @brief Get function kernel
287 *
288 * @return Function kernel.
289 ***************************************************************************/
290inline
291const 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 ***************************************************************************/
302inline
303const 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 ***************************************************************************/
314inline
315const std::string& GIntegral::message(void) const
316{
317 return m_message;
318}
319
320#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 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.
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_terminate
Signals termination of subdivision.
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.
double adaptive_gauss_kronrod(const double &a, const double &b) const
Adaptive Gauss-Lobatto-Kronrod integration.
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.