GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GIntegrals.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GIntegrals.hpp - Integration class for set of functions *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 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 GIntegrals.hpp
23  * @brief Integration class for set of functions interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GINTEGRALS_HPP
28 #define GINTEGRALS_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <vector>
33 #include "GBase.hpp"
34 #include "GVector.hpp"
35 
36 /* __ Forward declarations _______________________________________________ */
37 class GFunctions;
38 
39 /***********************************************************************//**
40  * @class GIntegrals
41  *
42  * @brief Integration class for set of functions
43  *
44  * This class allows to perform integration of a set of functions. The
45  * integrand is implemented by a derived class of GFunctions.
46  ***************************************************************************/
47 class GIntegrals : public GBase {
48 
49 public:
50 
51  // Constructors and destructors
52  explicit GIntegrals(void);
53  explicit GIntegrals(GFunctions* kernels);
54  GIntegrals(const GIntegrals& integral);
55  virtual ~GIntegrals(void);
56 
57  // Operators
58  GIntegrals& operator=(const GIntegrals& integral);
59 
60  // Methods
61  void clear(void);
62  GIntegrals* clone(void) const;
63  std::string classname(void) const;
64  void max_iter(const int& iter);
65  const int& max_iter(void) const;
66  void fixed_iter(const int& iter);
67  const int& fixed_iter(void) const;
68  void eps(const double& eps);
69  const double& eps(void) const;
70  void silent(const bool& silent);
71  const bool& silent(void) const;
72  const int& iter(void) const;
73  const int& calls(void) const;
74  const bool& is_valid(void) const;
75  const std::string& message(void) const;
76  void kernels(GFunctions* kernels);
77  const GFunctions* kernels(void) const;
78  GVector romberg(std::vector<double> bounds,
79  const int& order = 5);
80  GVector romberg(const double& a,
81  const double& b,
82  const int& order = 5);
83  GVector trapzd(const double& a,
84  const double& b,
85  const int& n,
86  const GVector& previous_result);
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 GIntegrals& integral);
93  void free_members(void);
94  GVector polint(const double* xa,
95  const GVector* ya,
96  const int& order,
97  const double& x,
98  GVector* dy);
99 
100  // Protected data area
101  GFunctions* m_kernels; //!< Pointer to function kernels
102  double m_eps; //!< Requested relative integration precision
103  int m_max_iter; //!< Maximum number of iterations
104  int m_fix_iter; //!< Fixed number of iterations
105  bool m_silent; //!< Suppress integration warnings in console
106 
107  // Integrator results
108  mutable int m_iter; //!< Number of iterations used
109  mutable int m_calls; //!< Number of function calls used
110  mutable bool m_isvalid; //!< Integration result valid (true=yes)
111  mutable bool m_has_abserr; //!< Has absolute integration errors
112  mutable bool m_has_relerr; //!< Has relative integration errors
113  mutable GVector m_abserr; //!< Absolute integration errors
114  mutable GVector m_relerr; //!< Absolute integration errors
115  mutable std::string m_message; //!< Status message (if result is invalid)
116 };
117 
118 
119 /***********************************************************************//**
120  * @brief Return class name
121  *
122  * @return String containing the class name ("GIntegrals").
123  ***************************************************************************/
124 inline
125 std::string GIntegrals::classname(void) const
126 {
127  return ("GIntegrals");
128 }
129 
130 
131 /***********************************************************************//**
132  * @brief Return number of iterations
133  *
134  * @return Number of iterations.
135  ***************************************************************************/
136 inline
137 const int& GIntegrals::iter(void) const
138 {
139  return m_iter;
140 }
141 
142 
143 /***********************************************************************//**
144  * @brief Set maximum number of iterations
145  *
146  * @param[in] iter Maximum number of iterations.
147  ***************************************************************************/
148 inline
149 void GIntegrals::max_iter(const int& iter)
150 {
151  m_max_iter = iter;
152  return;
153 }
154 
155 
156 /***********************************************************************//**
157  * @brief Return maximum number of iterations
158  *
159  * @return Maximum number of iterations.
160  ***************************************************************************/
161 inline
162 const int& GIntegrals::max_iter(void) const
163 {
164  return m_max_iter;
165 }
166 
167 
168 /***********************************************************************//**
169  * @brief Set fixed number of iterations
170  *
171  * @param[in] iter Fixed number of iterations.
172  *
173  * If the fixed number of iterations is set, the integration algorithm will
174  * always performed the given number of iterations, irrespectively of the
175  * precision that is reached. This feature is relevant for computing
176  * numerical derivates from numerically integrated functions.
177  ***************************************************************************/
178 inline
179 void GIntegrals::fixed_iter(const int& iter)
180 {
181  m_fix_iter = iter;
182  return;
183 }
184 
185 
186 /***********************************************************************//**
187  * @brief Return fixed number of iterations
188  *
189  * @return Fixed number of iterations.
190  ***************************************************************************/
191 inline
192 const int& GIntegrals::fixed_iter(void) const
193 {
194  return m_fix_iter;
195 }
196 
197 
198 /***********************************************************************//**
199  * @brief Set relative precision
200  *
201  * @param[in] eps Relative precision.
202  ***************************************************************************/
203 inline
204 void GIntegrals::eps(const double& eps)
205 {
206  m_eps = eps;
207  return;
208 }
209 
210 
211 /***********************************************************************//**
212  * @brief Get relative precision
213  *
214  * @return Relative precision.
215  ***************************************************************************/
216 inline
217 const double& GIntegrals::eps(void) const
218 {
219  return m_eps;
220 }
221 
222 
223 /***********************************************************************//**
224  * @brief Get number of function calls
225  *
226  * @return Number of function calls.
227  ***************************************************************************/
228 inline
229 const int& GIntegrals::calls(void) const
230 {
231  return m_calls;
232 }
233 
234 
235 /***********************************************************************//**
236  * @brief Set silence flag
237  *
238  * @param[in] silent Silence flag.
239  ***************************************************************************/
240 inline
241 void GIntegrals::silent(const bool& silent)
242 {
243  m_silent = silent;
244  return;
245 }
246 
247 
248 /***********************************************************************//**
249  * @brief Get silence flag
250  *
251  * @return True is class is silent, false otherwise.
252  ***************************************************************************/
253 inline
254 const bool& GIntegrals::silent(void) const
255 {
256  return m_silent;
257 }
258 
259 
260 /***********************************************************************//**
261  * @brief Set function kernels
262  *
263  * @param[in] kernels Function kernels.
264  *
265  * Sets the function kernels for which the integral should be determined.
266  ***************************************************************************/
267 inline
269 {
270  m_kernels = kernels;
271  return;
272 }
273 
274 
275 /***********************************************************************//**
276  * @brief Get function kernels
277  *
278  * @return Function kernels.
279  ***************************************************************************/
280 inline
281 const GFunctions* GIntegrals::kernels(void) const
282 {
283  return m_kernels;
284 }
285 
286 
287 /***********************************************************************//**
288  * @brief Signal if integration result is valid
289  *
290  * @return True is integration result is valid.
291  ***************************************************************************/
292 inline
293 const bool& GIntegrals::is_valid(void) const
294 {
295  return m_isvalid;
296 }
297 
298 
299 /***********************************************************************//**
300  * @brief Return integration status message
301  *
302  * @return Integration status message.
303  ***************************************************************************/
304 inline
305 const std::string& GIntegrals::message(void) const
306 {
307  return m_message;
308 }
309 
310 #endif /* GINTEGRALS_HPP */
const bool & silent(void) const
Get silence flag.
Definition: GIntegrals.hpp:254
GIntegrals(void)
Void constructor.
Definition: GIntegrals.cpp:62
virtual ~GIntegrals(void)
Destructor.
Definition: GIntegrals.cpp:114
void init_members(void)
Initialise class members.
Definition: GIntegrals.cpp:641
int m_iter
Number of iterations used.
Definition: GIntegrals.hpp:108
int m_fix_iter
Fixed number of iterations.
Definition: GIntegrals.hpp:104
void copy_members(const GIntegrals &integral)
Copy class members.
Definition: GIntegrals.cpp:670
Definition of interface for all GammaLib classes.
GIntegrals & operator=(const GIntegrals &integral)
Assignment operator.
Definition: GIntegrals.cpp:136
const GFunctions * kernels(void) const
Get function kernels.
Definition: GIntegrals.hpp:281
bool m_isvalid
Integration result valid (true=yes)
Definition: GIntegrals.hpp:110
void clear(void)
Clear integral.
Definition: GIntegrals.cpp:166
int m_max_iter
Maximum number of iterations.
Definition: GIntegrals.hpp:103
double m_eps
Requested relative integration precision.
Definition: GIntegrals.hpp:102
bool m_has_relerr
Has relative integration errors.
Definition: GIntegrals.hpp:112
bool m_silent
Suppress integration warnings in console.
Definition: GIntegrals.hpp:105
std::string print(const GChatter &chatter=NORMAL) const
Print integral information.
Definition: GIntegrals.cpp:572
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
const int & calls(void) const
Get number of function calls.
Definition: GIntegrals.hpp:229
GFunctions * m_kernels
Pointer to function kernels.
Definition: GIntegrals.hpp:101
const bool & is_valid(void) const
Signal if integration result is valid.
Definition: GIntegrals.hpp:293
GIntegrals * clone(void) const
Clone integral.
Definition: GIntegrals.cpp:184
int m_calls
Number of function calls used.
Definition: GIntegrals.hpp:109
void free_members(void)
Delete class members.
Definition: GIntegrals.cpp:697
GChatter
Definition: GTypemaps.hpp:33
GVector m_relerr
Absolute integration errors.
Definition: GIntegrals.hpp:114
std::string m_message
Status message (if result is invalid)
Definition: GIntegrals.hpp:115
Vector class interface definition.
const int & fixed_iter(void) const
Return fixed number of iterations.
Definition: GIntegrals.hpp:192
const int & iter(void) const
Return number of iterations.
Definition: GIntegrals.hpp:137
bool m_has_abserr
Has absolute integration errors.
Definition: GIntegrals.hpp:111
const int & max_iter(void) const
Return maximum number of iterations.
Definition: GIntegrals.hpp:162
GVector romberg(std::vector< double > bounds, const int &order=5)
Perform Romberg integration.
Definition: GIntegrals.cpp:210
GVector trapzd(const double &a, const double &b, const int &n, const GVector &previous_result)
Perform Trapezoidal integration for a set of functions.
Definition: GIntegrals.cpp:450
Vector class.
Definition: GVector.hpp:46
GVector polint(const double *xa, const GVector *ya, const int &order, const double &x, GVector *dy)
Perform Polynomial interpolation.
Definition: GIntegrals.cpp:718
Integration class for set of functions.
Definition: GIntegrals.hpp:47
GVector m_abserr
Absolute integration errors.
Definition: GIntegrals.hpp:113
std::string classname(void) const
Return class name.
Definition: GIntegrals.hpp:125
const double & eps(void) const
Get relative precision.
Definition: GIntegrals.hpp:217
const std::string & message(void) const
Return integration status message.
Definition: GIntegrals.hpp:305
Single parameter functions abstract base class.
Definition: GFunctions.hpp:50