16 #include "numerical_integration.h"
63 return(
"TrapezoidMethod");
69 return(
"SimpsonMethod");
75 std::ostringstream buffer;
77 buffer <<
"OpenNN Exception: NumericalIntegration class.\n"
78 <<
"std::string write_numerical_integration_method(void) const method.\n"
79 <<
"Unknown numerical integration method.\n";
81 throw std::logic_error(buffer.str());
119 numerical_integration_method = new_numerical_integration_method;
131 if(new_numerical_integration_method ==
"TrapezoidMethod")
135 else if(new_numerical_integration_method ==
"SimpsonMethod")
141 std::ostringstream buffer;
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";
147 throw std::logic_error(buffer.str());
190 const size_t n = x.size();
194 double trapezoid_integral = 0;
196 for(
size_t i = 0; i < n-1; i++)
198 trapezoid_integral += 0.5*(x[i+1]-x[i])*(y[i+1]+y[i]);
203 return(trapezoid_integral);
216 double Simpson_integral = 0.0;
220 const size_t n = x.size();
241 for(
size_t i = 0 ; i < m ; i++ )
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);
255 sum += wa*fa+wb*fb+wc*fc;
262 for(
size_t i = 0; i < m; i++ )
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);
276 sum += wa*fa+wb*fb+wc*fc;
283 sum += h*(y[n-1]+y[n-2])/2.0;
286 Simpson_integral = sum ;
288 return(Simpson_integral);
302 case TrapezoidMethod:
316 std::ostringstream buffer;
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";
322 throw std::logic_error(buffer.str());
335 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
337 tinyxml2::XMLElement* element = NULL;
338 tinyxml2::XMLText* text = NULL;
340 std::ostringstream buffer;
344 tinyxml2::XMLElement* root_element = document->NewElement(
"NumericalIntegration");
346 document->InsertFirstChild(root_element);
350 element = document->NewElement(
"NumericalIntegrationMethod");
351 root_element->LinkEndChild(element);
354 element->LinkEndChild(text);
358 element = document->NewElement(
"Display");
359 root_element->LinkEndChild(element);
364 text = document->NewText(buffer.str().c_str());
365 element->LinkEndChild(text);
378 const tinyxml2::XMLElement* root_element = document.FirstChildElement(
"NumericalIntegration");
382 std::ostringstream buffer;
384 buffer <<
"OpenNN Exception: NumericalIntegration class.\n"
385 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
386 <<
"Numerical integration element is NULL.\n";
388 throw std::logic_error(buffer.str());
393 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"NumericalIntegrationMethod");
397 const std::string new_numerical_integration_method = element->GetText();
403 catch(
const std::logic_error& e)
405 std::cout << e.what() << std::endl;
412 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"Display");
416 const std::string new_display = element->GetText();
422 catch(
const std::logic_error& e)
424 std::cout << e.what() << std::endl;
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 &)
void set_display(const bool &)
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