14 #ifndef __NUMERICALINTEGRATION_H__
15 #define __NUMERICALINTEGRATION_H__
27 #include "../tinyxml2/tinyxml2.h"
78 tinyxml2::XMLDocument*
to_XML(
void)
const;
79 void from_XML(
const tinyxml2::XMLDocument&);
96 double (T::*f)(
const double&)
const ,
const double& a,
const double& b,
const size_t& n)
100 const double h = (b-a)/(n-1.0);
104 double sum = (t.*f)(a)/2.0;
106 for(
size_t i = 1; i < n-1; i++)
108 sum += (t.*f)(a + i*h);
111 sum += (t.*f)(b)/2.0;
135 const double h = (b-a)/(n-1.0);
141 for(
size_t i = 1; i < n-1; i++)
143 sum += (t.*f)(a + i*h);
146 sum += (t.*f)(b)/2.0;
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)
168 const double h = (b-a)/(n-1.0);
170 double sum = (t.*f)(a)/3.0;
172 for(
size_t i = 1; i < n-1; i++)
176 sum += 4.0*(t.*f)(a + i*h)/3.0;
180 sum += 2.0*(t.*f)(a + i*h)/3.0;
184 sum += (t.*f)(b)/3.0;
204 const double h = (b-a)/(n-1.0);
208 for(
size_t i = 1; i < n-1; i++)
212 sum += (t.*f)(a + i*h)*4.0/3.0;
216 sum += (t.*f)(a + i*h)*2.0/3.0;
220 sum += (t.*f)(b)/3.0;
239 double calculate_integral(
const T& t,
double (T::*)(
const double&)
const ,
const double& a,
const double& b,
const size_t& n)
const
243 case TrapezoidMethod:
257 std::ostringstream buffer;
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";
263 throw std::logic_error(buffer.str());
285 case TrapezoidMethod:
299 std::ostringstream buffer;
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";
305 throw std::logic_error(buffer.str());
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 &)
void set_display(const bool &)
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