00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include<iostream>
00019
00020
00021
00022 #include "numerical_differentiation.h"
00023
00024
00025
00026 #include "../../parsers/tinyxml/tinyxml.h"
00027
00028
00029 namespace OpenNN
00030 {
00031
00032
00033
00036
00037 NumericalDifferentiation::NumericalDifferentiation(void)
00038 {
00039 set_default();
00040 }
00041
00042
00043
00044
00046
00047 NumericalDifferentiation::NumericalDifferentiation(const NumericalDifferentiation& other_numerical_differentiation)
00048 {
00049 set(other_numerical_differentiation);
00050 }
00051
00052
00053
00054
00056
00057 NumericalDifferentiation::~NumericalDifferentiation(void)
00058 {
00059
00060 }
00061
00062
00063
00064
00065
00066
00070
00071 NumericalDifferentiation& NumericalDifferentiation::operator = (const NumericalDifferentiation& other_numerical_differentiation)
00072 {
00073 if(this != &other_numerical_differentiation)
00074 {
00075 numerical_differentiation_method = other_numerical_differentiation.numerical_differentiation_method;
00076
00077 precision_digits = other_numerical_differentiation.precision_digits;
00078
00079 display = other_numerical_differentiation.display;
00080 }
00081
00082 return(*this);
00083 }
00084
00085
00086
00087
00088
00089
00094
00095 bool NumericalDifferentiation::operator == (const NumericalDifferentiation& other_numerical_differentiation) const
00096 {
00097 if(numerical_differentiation_method == other_numerical_differentiation.numerical_differentiation_method
00098 && precision_digits == other_numerical_differentiation.precision_digits
00099 && display == other_numerical_differentiation.display)
00100 {
00101 return(true);
00102 }
00103 else
00104 {
00105 return(false);
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00115
00116 const NumericalDifferentiation::NumericalDifferentiationMethod& NumericalDifferentiation::get_numerical_differentiation_method(void) const
00117 {
00118 return(numerical_differentiation_method);
00119 }
00120
00121
00122
00123
00125
00126 std::string NumericalDifferentiation::write_numerical_differentiation_method(void) const
00127 {
00128 switch(numerical_differentiation_method)
00129 {
00130 case ForwardDifferences:
00131 {
00132 return("ForwardDifferences");
00133 }
00134 break;
00135
00136 case CentralDifferences:
00137 {
00138 return("CentralDifferences");
00139 }
00140 break;
00141
00142 default:
00143 {
00144 std::ostringstream buffer;
00145
00146 buffer << "OpenNN Exception: NumericalDifferentiation class.\n"
00147 << "std::string write_numerical_differentiation_method(void) const method.\n"
00148 << "Unknown numerical differentiation method.\n";
00149
00150 throw std::logic_error(buffer.str());
00151 }
00152 break;
00153 }
00154 }
00155
00156
00157
00158
00160
00161 const unsigned int& NumericalDifferentiation::get_precision_digits(void) const
00162 {
00163 return(precision_digits);
00164 }
00165
00166
00167
00168
00170
00171 const bool& NumericalDifferentiation::get_display(void) const
00172 {
00173 return(display);
00174 }
00175
00176
00177
00178
00181
00182 void NumericalDifferentiation::set(const NumericalDifferentiation& other_numerical_differentiation)
00183 {
00184 numerical_differentiation_method = other_numerical_differentiation.numerical_differentiation_method;
00185
00186 precision_digits = other_numerical_differentiation.precision_digits;
00187
00188 display = other_numerical_differentiation.display;
00189
00190 }
00191
00192
00193
00194
00197
00198 void NumericalDifferentiation::set_numerical_differentiation_method
00199 (const NumericalDifferentiation::NumericalDifferentiationMethod& new_numerical_differentiation_method)
00200 {
00201 numerical_differentiation_method = new_numerical_differentiation_method;
00202 }
00203
00204
00205
00206
00210
00211 void NumericalDifferentiation::set_numerical_differentiation_method(const std::string& new_numerical_differentiation_method)
00212 {
00213 if(new_numerical_differentiation_method == "ForwardDifferences")
00214 {
00215 numerical_differentiation_method = ForwardDifferences;
00216 }
00217 else if(new_numerical_differentiation_method == "CentralDifferences")
00218 {
00219 numerical_differentiation_method = CentralDifferences;
00220 }
00221 else
00222 {
00223 std::ostringstream buffer;
00224
00225 buffer << "OpenNN Exception: NumericalDifferentiation class.\n"
00226 << "void set_numerical_differentiation_method(const std::string&) method.\n"
00227 << "Unknown numerical differentiation method: " << new_numerical_differentiation_method << ".\n";
00228
00229 throw std::logic_error(buffer.str());
00230 }
00231 }
00232
00233
00234
00235
00238
00239 void NumericalDifferentiation::set_display(const bool& new_display)
00240 {
00241 display = new_display;
00242 }
00243
00244
00245
00246
00249
00250 void NumericalDifferentiation::set_precision_digits(const unsigned int& new_precision_digits)
00251 {
00252 precision_digits = new_precision_digits;
00253 }
00254
00255
00256
00257
00264
00265 void NumericalDifferentiation::set_default(void)
00266 {
00267 numerical_differentiation_method = CentralDifferences;
00268
00269 precision_digits = 6;
00270
00271 display = true;
00272 }
00273
00274
00275
00276
00279
00280 double NumericalDifferentiation::calculate_h(const double& x) const
00281 {
00282 const double eta = pow(10.0, (int)(-1*precision_digits));
00283
00284 return(sqrt(eta)*(1.0 + fabs(x)));
00285 }
00286
00287
00288
00289
00292
00293 Vector<double> NumericalDifferentiation::calculate_h(const Vector<double>& x) const
00294 {
00295 const double eta = pow(10.0, (int)(-1*precision_digits));
00296
00297 const unsigned int n = x.size();
00298
00299 Vector<double> h(n);
00300
00301 for(unsigned int i = 0; i < n; i++)
00302 {
00303 h[i] = sqrt(eta)*(1.0 + fabs(x[i]));
00304 }
00305
00306 return(h);
00307 }
00308
00309
00310
00311
00313
00314 TiXmlElement* NumericalDifferentiation::to_XML(void) const
00315 {
00316 std::ostringstream buffer;
00317
00318
00319
00320 TiXmlElement* numerical_differentiation_element = new TiXmlElement("NumericalDifferentiation");
00321 numerical_differentiation_element->SetAttribute("Version", 4);
00322
00323
00324 {
00325 TiXmlElement* element = new TiXmlElement("NumericalDifferentiationMethod");
00326 numerical_differentiation_element->LinkEndChild(element);
00327
00328 TiXmlText* text = new TiXmlText(write_numerical_differentiation_method().c_str());
00329 element->LinkEndChild(text);
00330 }
00331
00332
00333 {
00334 TiXmlElement* element = new TiXmlElement("PrecisionDigits");
00335 numerical_differentiation_element->LinkEndChild(element);
00336
00337 buffer.str("");
00338 buffer << precision_digits;
00339
00340 TiXmlText* text = new TiXmlText(buffer.str().c_str());
00341 element->LinkEndChild(text);
00342 }
00343
00344
00345 {
00346 TiXmlElement* element = new TiXmlElement("Display");
00347 numerical_differentiation_element->LinkEndChild(element);
00348
00349 buffer.str("");
00350 buffer << display;
00351
00352 TiXmlText* text = new TiXmlText(buffer.str().c_str());
00353 element->LinkEndChild(text);
00354 }
00355
00356 return(numerical_differentiation_element);
00357 }
00358
00359
00360
00361
00364
00365 void NumericalDifferentiation::from_XML(TiXmlElement* numerical_differentiation_element)
00366 {
00367 if(numerical_differentiation_element)
00368 {
00369
00370 {
00371 TiXmlElement* element = numerical_differentiation_element->FirstChildElement("NumericalDifferentiationMethod");
00372
00373 if(element)
00374 {
00375 std::string new_numerical_differentiation_method = element->GetText();
00376
00377 try
00378 {
00379 set_numerical_differentiation_method(new_numerical_differentiation_method);
00380 }
00381 catch(std::exception& e)
00382 {
00383 std::cout << e.what() << std::endl;
00384 }
00385 }
00386 }
00387
00388
00389 {
00390 TiXmlElement* element = numerical_differentiation_element->FirstChildElement("PrecisionDigits");
00391
00392 if(element)
00393 {
00394 const unsigned int new_precision_digits = atoi(element->GetText());
00395
00396 try
00397 {
00398 set_precision_digits(new_precision_digits);
00399 }
00400 catch(std::exception& e)
00401 {
00402 std::cout << e.what() << std::endl;
00403 }
00404 }
00405 }
00406
00407
00408 {
00409 TiXmlElement* element = numerical_differentiation_element->FirstChildElement("Display");
00410
00411 if(element)
00412 {
00413 std::string new_display = element->GetText();
00414
00415 try
00416 {
00417 set_display(new_display != "0");
00418 }
00419 catch(std::exception& e)
00420 {
00421 std::cout << e.what() << std::endl;
00422 }
00423 }
00424 }
00425 }
00426 }
00427
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445