OpenNN  2.2
Open Neural Networks Library
numerical_integration.cpp
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 */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 // OpenNN includes
15 
16 #include "numerical_integration.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
24 
26 {
27 
28 }
29 
30 
31 // DESTRUCTOR
32 
34 
36 {
37 
38 }
39 
40 
41 // METHODS
42 
43 // const NumericalIntegrationMethod& get_numerical_integration_method(void) const method
44 
46 
48 {
50 }
51 
52 
53 // std::string write_numerical_integration_method(void) const method
54 
56 
58 {
60  {
61  case TrapezoidMethod:
62  {
63  return("TrapezoidMethod");
64  }
65  break;
66 
67  case SimpsonMethod:
68  {
69  return("SimpsonMethod");
70  }
71  break;
72 
73  default:
74  {
75  std::ostringstream buffer;
76 
77  buffer << "OpenNN Exception: NumericalIntegration class.\n"
78  << "std::string write_numerical_integration_method(void) const method.\n"
79  << "Unknown numerical integration method.\n";
80 
81  throw std::logic_error(buffer.str());
82  }
83  break;
84  }
85 }
86 
87 
88 // const bool& get_display(void) const method
89 
91 
92 const bool& NumericalIntegration::get_display(void) const
93 {
94  return(display);
95 }
96 
97 
98 // void set(const NumericalIntegration&) method
99 
102 
103 void NumericalIntegration::set(const NumericalIntegration& other_numerical_integration)
104 {
105  numerical_integration_method = other_numerical_integration.numerical_integration_method;
106 
107  display = other_numerical_integration.display;
108 }
109 
110 
111 // void set_numerical_integration_method(const NumericalIntegrationMethod&)
112 
115 
117 (const NumericalIntegration::NumericalIntegrationMethod& new_numerical_integration_method)
118 {
119  numerical_integration_method = new_numerical_integration_method;
120 }
121 
122 
123 // void set_numerical_integration_method(const std::string&) method
124 
128 
129 void NumericalIntegration::set_numerical_integration_method(const std::string& new_numerical_integration_method)
130 {
131  if(new_numerical_integration_method == "TrapezoidMethod")
132  {
133  numerical_integration_method = TrapezoidMethod;
134  }
135  else if(new_numerical_integration_method == "SimpsonMethod")
136  {
137  numerical_integration_method = SimpsonMethod;
138  }
139  else
140  {
141  std::ostringstream buffer;
142 
143  buffer << "OpenNN Exception: NumericalIntegration class.\n"
144  << "void set_numerical_integration_method(const std::string&) method.\n"
145  << "Unknown numerical integration method: " << new_numerical_integration_method << ".\n";
146 
147  throw std::logic_error(buffer.str());
148  }
149 }
150 
151 
152 // void set_display(const bool&) method
153 
156 
157 void NumericalIntegration::set_display(const bool& new_display)
158 {
159  display = new_display;
160 }
161 
162 
163 // void set_default(void) method
164 
170 
172 {
173  numerical_integration_method = SimpsonMethod;
174 
175  display = true;
176 }
177 
178 
179 // double calculate_trapezoid_integral(const Vector<double>&, const Vector<double>&) const method
180 
185 
187 {
188  // Number of integration points
189 
190  const size_t n = x.size();
191 
192  // Calculate integral
193 
194  double trapezoid_integral = 0;
195 
196  for(size_t i = 0; i < n-1; i++)
197  {
198  trapezoid_integral += 0.5*(x[i+1]-x[i])*(y[i+1]+y[i]);
199  }
200 
201  // Return integral value
202 
203  return(trapezoid_integral);
204 }
205 
206 
207 // double calculate_Simpson_integral(const Vector<double>&, const Vector<double>&) const method
208 
213 
215 {
216  double Simpson_integral = 0.0;
217 
218  // Number of integration points
219 
220  const size_t n = x.size();
221 
222  size_t m = 0;
223 
224  double a = 0.0;
225  double fa = 0.0;
226  double b = 0.0;
227  double fb = 0.0;
228  double c = 0.0;
229  double fc = 0.0;
230  double wa = 0.0;
231  double wb = 0.0;
232  double wc = 0.0;
233  double h = 0.0;
234 
235  double sum = 0.0;// evenSum = 0.0;
236 
237  if(n%2 != 0)
238  {
239  m=(n-1)/2;
240 
241  for(size_t i = 0 ; i < m ; i++ )
242  {
243  a = x[2*i];
244  b = x[2*i+1];
245  c = x[2*i+2];
246 
247  fa = y[2*i];
248  fb = y[2*i+1];
249  fc = y[2*i+2];
250 
251  wa = (c-a)/((a-b)*(a-c))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(b+c)+b*c);
252  wb = (c-a)/((b-a)*(b-c))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(a+c)+a*c);
253  wc = (c-a)/((c-a)*(c-b))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(a+b)+a*b);
254 
255  sum += wa*fa+wb*fb+wc*fc;
256  }
257  }
258  else
259  {
260  m=(n-2)/2;
261 
262  for(size_t i = 0; i < m; i++ )
263  {
264  a = x[2*i];
265  b = x[2*i+1];
266  c = x[2*i+2];
267 
268  fa = y[2*i];
269  fb = y[2*i+1];
270  fc = y[2*i+2];
271 
272  wa = (c-a)/((a-b)*(a-c))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(b+c)+b*c);
273  wb = (c-a)/((b-a)*(b-c))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(a+c)+a*c);
274  wc = (c-a)/((c-a)*(c-b))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(a+b)+a*b);
275 
276  sum += wa*fa+wb*fb+wc*fc;
277  }
278 
279  // Trapezoid
280 
281  h = x[n-1]-x[n-2];
282 
283  sum += h*(y[n-1]+y[n-2])/2.0;
284  }
285 
286  Simpson_integral = sum ;
287 
288  return(Simpson_integral);
289 }
290 
291 
292 // double calculate_integral(const Vector<double>&, const Vector<double>&) const method
293 
297 
299 {
301  {
302  case TrapezoidMethod:
303  {
304  return(calculate_trapezoid_integral(x, y));
305  }
306  break;
307 
308  case SimpsonMethod:
309  {
310  return(calculate_Simpson_integral(x, y));
311  }
312  break;
313 
314  default:
315  {
316  std::ostringstream buffer;
317 
318  buffer << "OpenNN Exception: NumericalIntegration class.\n"
319  << "double calculate_integral(const Vector<double>&, const Vector<double>&) const method.\n"
320  << "Unknown numerical integration method.\n";
321 
322  throw std::logic_error(buffer.str());
323  }
324  break;
325  }
326 
327 }
328 
329 // tinyxml2::XMLDocument* to_XML(void) const method
330 
332 
333 tinyxml2::XMLDocument* NumericalIntegration::to_XML(void) const
334 {
335  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
336 
337  tinyxml2::XMLElement* element = NULL;
338  tinyxml2::XMLText* text = NULL;
339 
340  std::ostringstream buffer;
341 
342  // Numerical integration
343 
344  tinyxml2::XMLElement* root_element = document->NewElement("NumericalIntegration");
345 
346  document->InsertFirstChild(root_element);
347 
348  // Numerical integration method
349 
350  element = document->NewElement("NumericalIntegrationMethod");
351  root_element->LinkEndChild(element);
352 
353  text = document->NewText(write_numerical_integration_method().c_str());
354  element->LinkEndChild(text);
355 
356  // Display
357 
358  element = document->NewElement("Display");
359  root_element->LinkEndChild(element);
360 
361  buffer.str("");
362  buffer << display;
363 
364  text = document->NewText(buffer.str().c_str());
365  element->LinkEndChild(text);
366 
367  return(document);
368 }
369 
370 
371 // void from_XML(const tinyxml2::XMLDocument&) method
372 
375 
376 void NumericalIntegration::from_XML(const tinyxml2::XMLDocument& document)
377 {
378  const tinyxml2::XMLElement* root_element = document.FirstChildElement("NumericalIntegration");
379 
380  if(!root_element)
381  {
382  std::ostringstream buffer;
383 
384  buffer << "OpenNN Exception: NumericalIntegration class.\n"
385  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
386  << "Numerical integration element is NULL.\n";
387 
388  throw std::logic_error(buffer.str());
389  }
390 
391  // Numerical integration method
392  {
393  const tinyxml2::XMLElement* element = root_element->FirstChildElement("NumericalIntegrationMethod");
394 
395  if(element)
396  {
397  const std::string new_numerical_integration_method = element->GetText();
398 
399  try
400  {
401  set_numerical_integration_method(new_numerical_integration_method);
402  }
403  catch(const std::logic_error& e)
404  {
405  std::cout << e.what() << std::endl;
406  }
407  }
408  }
409 
410  // Display
411  {
412  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Display");
413 
414  if(element)
415  {
416  const std::string new_display = element->GetText();
417 
418  try
419  {
420  set_display(new_display != "0");
421  }
422  catch(const std::logic_error& e)
423  {
424  std::cout << e.what() << std::endl;
425  }
426  }
427  }
428 }
429 
430 }
431 
432 
433 // OpenNN: Open Neural Networks Library.
434 // Copyright (c) 2005-2015 Roberto Lopez.
435 //
436 // This library is free software; you can redistribute it and/or
437 // modify it under the terms of the GNU Lesser General Public
438 // License as published by the Free Software Foundation; either
439 // version 2.1 of the License, or any later version.
440 //
441 // This library is distributed in the hope that it will be useful,
442 // but WITHOUT ANY WARRANTY; without even the implied warranty of
443 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
444 // Lesser General Public License for more details.
445 
446 // You should have received a copy of the GNU Lesser General Public
447 // License along with this library; if not, write to the Free Software
448 // 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.
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
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