OpenNN  2.2
Open Neural Networks Library
numerical_integration.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* N U M E R I C A L I N T E G R A T I O N C L A S S H E A D E R */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 #ifndef __NUMERICALINTEGRATION_H__
15 #define __NUMERICALINTEGRATION_H__
16 
17 // System includes
18 
19 #include<iostream>
20 
21 // OpenNN includes
22 
23 #include "vector.h"
24 
25 // TinyXml includes
26 
27 #include "../tinyxml2/tinyxml2.h"
28 
29 
30 namespace OpenNN
31 {
32 
35 
37 {
38  public:
39 
40  // CONSTRUCTOR
41 
42  explicit NumericalIntegration(void);
43 
44 
45  // DESTRUCTOR
46 
47  virtual ~NumericalIntegration(void);
48 
50 
51  enum NumericalIntegrationMethod{TrapezoidMethod, SimpsonMethod};
52 
53  // METHODS
54 
56  std::string write_numerical_integration_method(void) const;
57 
58  const bool& get_display(void) const;
59 
60  void set(const NumericalIntegration&);
61 
63  void set_numerical_integration_method(const std::string&);
64 
65  void set_display(const bool&);
66 
67  void set_default(void);
68 
69  // Integration of pairs of data (x,y)
70 
71  double calculate_trapezoid_integral(const Vector<double>&, const Vector<double>&) const;
72  double calculate_Simpson_integral(const Vector<double>&, const Vector<double>&) const;
73 
74  double calculate_integral(const Vector<double>&, const Vector<double>&) const;
75 
76  // Serialization methods
77 
78  tinyxml2::XMLDocument* to_XML(void) const;
79  void from_XML(const tinyxml2::XMLDocument&);
80 
81  // Integration of class member methods
82 
83  // template<class T> double calculate_trapezoid_integral
84  // (const T&, double (T::*f)(const double&) const, const double& a, const double& b, const size_t& n) method
85 
93 
94  template<class T>
95  static double calculate_trapezoid_integral(const T& t,
96  double (T::*f)(const double&) const , const double& a, const double& b, const size_t& n)
97  {
98  // Integration step
99 
100  const double h = (b-a)/(n-1.0);
101 
102  // Sum
103 
104  double sum = (t.*f)(a)/2.0;
105 
106  for(size_t i = 1; i < n-1; i++)
107  {
108  sum += (t.*f)(a + i*h);
109  }
110 
111  sum += (t.*f)(b)/2.0;
112 
113  // Trapezoidal rule
114 
115  return(h*sum);
116  }
117 
118 
119  // Vector<double> calculate_trapezoid_integral
120  // (const T&, Vector<double> (T::*f)(const double&) const, const double, const double, const size_t&) const method
121 
129 
130  template<class T>
131  static Vector<double> calculate_trapezoid_integral(const T& t, Vector<double> (T::*f)(const double&) const , const double& a, const double& b, const size_t& n)
132  {
133  // Integration step
134 
135  const double h = (b-a)/(n-1.0);
136 
137  // Sum
138 
139  Vector<double> sum = (t.*f)(a)/2.0;
140 
141  for(size_t i = 1; i < n-1; i++)
142  {
143  sum += (t.*f)(a + i*h);
144  }
145 
146  sum += (t.*f)(b)/2.0;
147 
148  // Trapezoidal rule
149 
150  return(sum*h);
151  }
152 
153 
154  // template<class T> double calculate_Simpson_integral
155  // (const T&, double (T::*f)(double), const double&, const double&, const size_t&) const method
156 
164 
165  template<class T>
166  static double calculate_Simpson_integral(const T& t, double (T::*f)(const double&) const , const double& a, const double& b, const size_t& n)
167  {
168  const double h = (b-a)/(n-1.0);
169 
170  double sum = (t.*f)(a)/3.0;
171 
172  for(size_t i = 1; i < n-1; i++)
173  {
174  if(i%2 != 0) // odd
175  {
176  sum += 4.0*(t.*f)(a + i*h)/3.0;
177  }
178  else // even
179  {
180  sum += 2.0*(t.*f)(a + i*h)/3.0;
181  }
182  }
183 
184  sum += (t.*f)(b)/3.0;
185 
186  return(h*sum);
187  }
188 
189 
190  // Vector<double> calculate_Simpson_integral
191  // (const T&, Vector<double> (T::*f)(const double&) const , const double&, const double&, const size_t&) const method
192 
200 
201  template<class T>
202  static Vector<double> calculate_Simpson_integral(const T& t, Vector<double> (T::*f)(const double&) const , const double& a, const double& b, const size_t& n)
203  {
204  const double h = (b-a)/(n-1.0);
205 
206  Vector<double> sum = (t.*f)(a)/3.0;
207 
208  for(size_t i = 1; i < n-1; i++)
209  {
210  if(i%2 != 0) // odd
211  {
212  sum += (t.*f)(a + i*h)*4.0/3.0;
213  }
214  else // even
215  {
216  sum += (t.*f)(a + i*h)*2.0/3.0;
217  }
218  }
219 
220  sum += (t.*f)(b)/3.0;
221 
222  // Simpson's rule
223 
224  return(sum*h);
225  }
226 
227 
228  // template<class T> double calculate_integral
229  // (const T&, double (T::*f)(const double&) const, const double&, const double&, const size_t&) const method
230 
237 
238  template<class T>
239  double calculate_integral(const T& t, double (T::*)(const double&) const , const double& a, const double& b, const size_t& n) const
240  {
242  {
243  case TrapezoidMethod:
244  {
245  return(calculate_trapezoid_integral(t, &T::f, a, b, n));
246  }
247  break;
248 
249  case SimpsonMethod:
250  {
251  return(calculate_Simpson_integral(t, &T::f, a, b, n));
252  }
253  break;
254 
255  default:
256  {
257  std::ostringstream buffer;
258 
259  buffer << "OpenNN Exception: NumericalIntegration class.\n"
260  << "double calculate_integral(const T&, double (T::*f)(const double&) const, const double&, const double&, const size_t&) const method.\n"
261  << "Unknown numerical integration method.\n";
262 
263  throw std::logic_error(buffer.str());
264  }
265  break;
266  }
267  }
268 
269 
270  // Vector<double> calculate_integral
271  // (const T&, Vector<double> (T::*f)(const double&) const, const double&, const double&, const size_t&) const method
272 
279 
280  template<class T>
281  Vector<double> calculate_integral(const T& t, Vector<double> (T::*)(const double&) const , const double& a, const double& b, const size_t& n) const
282  {
284  {
285  case TrapezoidMethod:
286  {
287  return(calculate_trapezoid_integral(t, &T::f, a, b, n));
288  }
289  break;
290 
291  case SimpsonMethod:
292  {
293  return(calculate_Simpson_integral(t, &T::f, a, b, n));
294  }
295  break;
296 
297  default:
298  {
299  std::ostringstream buffer;
300 
301  buffer << "OpenNN Exception: NumericalIntegration class.\n"
302  << "double calculate_integral(const T&, Vector<double> (T::*f)(const double&) const, const double&, const double&, const size_t&) const method.\n"
303  << "Unknown numerical integration method.\n";
304 
305  throw std::logic_error(buffer.str());
306  }
307  break;
308  }
309  }
310 
311 
312 private:
313 
315 
317 
319 
320  bool display;
321 
322 };
323 
324 }
325 
326 #endif
327 
328 
329 // OpenNN: Open Neural Networks Library.
330 // Copyright (c) 2005-2015 Roberto Lopez.
331 //
332 // This library is free software; you can redistribute it and/or
333 // modify it under the terms of the GNU Lesser General Public
334 // License as published by the Free Software Foundation; either
335 // version 2.1 of the License, or any later version.
336 //
337 // This library is distributed in the hope that it will be useful,
338 // but WITHOUT ANY WARRANTY; without even the implied warranty of
339 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
340 // Lesser General Public License for more details.
341 
342 // You should have received a copy of the GNU Lesser General Public
343 // License along with this library; if not, write to the Free Software
344 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
bool display
Flag for displaying warning messages from this class.
NumericalIntegrationMethod
Enumeration of available methods for numerical integration.
const NumericalIntegrationMethod & get_numerical_integration_method(void) const
Returns the method used for numerical integration (trapezoid method or Simpson's method).
NumericalIntegration(void)
Default constructor.
void from_XML(const tinyxml2::XMLDocument &)
const bool & get_display(void) const
Returns the flag used by this class for displaying or not displaying warnings.
tinyxml2::XMLDocument * to_XML(void) const
Serializes this numerical integration object into a XML document.
static Vector< double > calculate_Simpson_integral(const T &t, Vector< double >(T::*f)(const double &) const , const double &a, const double &b, const size_t &n)
double calculate_integral(const T &t, double(T::*)(const double &) const , const double &a, const double &b, const size_t &n) const
static double calculate_Simpson_integral(const T &t, double(T::*f)(const double &) const , const double &a, const double &b, const size_t &n)
Vector< double > calculate_integral(const T &t, Vector< double >(T::*)(const double &) const , const double &a, const double &b, const size_t &n) const
static Vector< double > calculate_trapezoid_integral(const T &t, Vector< double >(T::*f)(const double &) const , const double &a, const double &b, const size_t &n)
void set_numerical_integration_method(const NumericalIntegrationMethod &)
NumericalIntegrationMethod numerical_integration_method
Numerical integration method variable.
std::string write_numerical_integration_method(void) const
Returns a string with the name of the method to be used for numerical integration.
void set(const NumericalIntegration &)
double calculate_Simpson_integral(const Vector< double > &, const Vector< double > &) const
static double calculate_trapezoid_integral(const T &t, double(T::*f)(const double &) const , const double &a, const double &b, const size_t &n)
double calculate_trapezoid_integral(const Vector< double > &, const Vector< double > &) const
virtual ~NumericalIntegration(void)
Destructor.
double calculate_integral(const Vector< double > &, const Vector< double > &) const