GammaLib
2.1.0.dev
|
Numerical derivatives class. More...
#include <GDerivative.hpp>
Classes | |
class | tiny |
Public Member Functions | |
GDerivative (void) | |
Void constructor. More... | |
GDerivative (GFunction *func) | |
Function constructor. More... | |
GDerivative (const GDerivative &dx) | |
Copy constructor. More... | |
virtual | ~GDerivative (void) |
Destructor. More... | |
GDerivative & | operator= (const GDerivative &dx) |
Assignment operator. More... | |
void | clear (void) |
Clear derivative. More... | |
GDerivative * | clone (void) const |
Clone derivative. More... | |
std::string | classname (void) const |
Return class name. More... | |
void | max_iter (const int &max_iter) |
Set maximum number of iterations. More... | |
void | eps (const double &eps) |
Set precision. More... | |
void | step_frac (const double &fraction) |
Set step fraction. More... | |
void | silent (const bool &silent) |
Set silence flag. More... | |
const int & | iter (void) const |
Return number of iterations. More... | |
const int & | max_iter (void) const |
Return maximum number of iterations. More... | |
const double & | eps (void) const |
Get precision. More... | |
const double & | step_frac (void) const |
Get step fraction. More... | |
const bool & | silent (void) const |
Get silence flag. More... | |
void | function (GFunction *func) |
Set function. More... | |
const GFunction * | function (void) const |
Get function. More... | |
double | value (const double &x, const double &step=0.0) |
Returns derivative. More... | |
double | ridder (const double &x, const double &h, double *err) |
Returns derivative by Ridders' method. More... | |
double | minuit2 (const double &x, double *err) |
Returns derivative using Minuit2 algorithm. More... | |
double | difference (const double &x, const double &h) |
Returns gradient computed from symmetric function difference. More... | |
double | left_difference (const double &x, const double &h) |
Returns gradient computed from left-sided function difference. More... | |
double | right_difference (const double &x, const double &h) |
Returns gradient computed from right-sided function difference. More... | |
double | smooth_robust (const double &x, const double &h, const int °ree=2, const int &length=5) |
Returns smooth noise-robust derivative. More... | |
std::string | print (const GChatter &chatter=NORMAL) const |
Print derivative information. More... | |
Public Member Functions inherited from GBase | |
virtual | ~GBase (void) |
Destructor. More... | |
Protected Member Functions | |
void | init_members (void) |
Initialise class members. More... | |
void | copy_members (const GDerivative &dx) |
Copy class members. More... | |
void | free_members (void) |
Delete class members. More... | |
void | set_tiny (void) |
Compute tiny number for Minuit2. More... | |
Protected Attributes | |
GFunction * | m_func |
Pointer to function. More... | |
double | m_eps |
Derivative precision. More... | |
double | m_step_frac |
Value fraction to use for initial step. More... | |
double | m_tiny |
Tiny number for minuit2. More... | |
int | m_max_iter |
Maximum number of iterations. More... | |
int | m_iter |
Number of iterations used. More... | |
bool | m_silent |
Suppress warnings. More... | |
Numerical derivatives class.
This class allows to compute numerical derivatives using various methods. The function to be derived is implemented by the abstract GFunction class.
Definition at line 45 of file GDerivative.hpp.
GDerivative::GDerivative | ( | void | ) |
Void constructor.
Definition at line 60 of file GDerivative.cpp.
References init_members().
Referenced by clone().
|
explicit |
Function constructor.
[in] | func | Function pointer. |
Construct class instance from single parameter function. Note that the function is not copied but just a pointer to it is stored. The function should thus not be destroyed before computation of derivatives is finished.
Definition at line 80 of file GDerivative.cpp.
References init_members().
GDerivative::GDerivative | ( | const GDerivative & | dx | ) |
Copy constructor.
[in] | dx | Derivative. |
Definition at line 98 of file GDerivative.cpp.
References copy_members(), and init_members().
|
virtual |
|
inlinevirtual |
Return class name.
Implements GBase.
Definition at line 119 of file GDerivative.hpp.
|
virtual |
Clear derivative.
Implements GBase.
Definition at line 166 of file GDerivative.cpp.
References free_members(), and init_members().
|
virtual |
Clone derivative.
Implements GBase.
Definition at line 184 of file GDerivative.cpp.
References GDerivative().
|
protected |
Copy class members.
[in] | dx | Derivative. |
Definition at line 816 of file GDerivative.cpp.
References m_eps, m_func, m_iter, m_max_iter, m_silent, m_step_frac, and m_tiny.
Referenced by GDerivative(), and operator=().
double GDerivative::difference | ( | const double & | x, |
const double & | h | ||
) |
Returns gradient computed from symmetric function difference.
[in] | x | Function value. |
[in] | h | Step size. |
This is the most simple and dumb gradient computation method we can think of. It does a reasonable good job if the problem is well controlled, and in particular, if the step size is known in advance.
Definition at line 514 of file GDerivative.cpp.
References GFunction::eval(), and m_func.
|
inline |
|
inline |
|
protected |
Delete class members.
Definition at line 835 of file GDerivative.cpp.
Referenced by clear(), operator=(), and ~GDerivative().
|
inline |
Set function.
[in] | function | Function. |
Sets the function for which the derivative should be determined.
Definition at line 245 of file GDerivative.hpp.
References m_func.
|
inline |
|
protected |
Initialise class members.
Definition at line 792 of file GDerivative.cpp.
References m_eps, m_func, m_iter, m_max_iter, m_silent, m_step_frac, and m_tiny.
Referenced by clear(), GDerivative(), and operator=().
|
inline |
Return number of iterations.
Definition at line 131 of file GDerivative.hpp.
References m_iter.
double GDerivative::left_difference | ( | const double & | x, |
const double & | h | ||
) |
Returns gradient computed from left-sided function difference.
[in] | x | Function value. |
[in] | h | Step size. |
This is the most simple and dumb gradient computation method we can think of. It does a reasonable good job if the problem is well controlled, and in particular, if the step size is known in advance.
Definition at line 536 of file GDerivative.cpp.
References GFunction::eval(), and m_func.
|
inline |
Set maximum number of iterations.
[in] | max_iter | Maximum number of iterations. |
Definition at line 143 of file GDerivative.hpp.
References m_max_iter, and max_iter().
|
inline |
Return maximum number of iterations.
Definition at line 156 of file GDerivative.hpp.
References m_max_iter.
Referenced by max_iter(), and print().
double GDerivative::minuit2 | ( | const double & | x, |
double * | err | ||
) |
Returns derivative using Minuit2 algorithm.
[in] | x | Function value. |
[out] | err | Estimated error in the derivative. |
This method has been inspired by corresponding code in the Minuit2 package. Please check the following files in Minuit2 for more information:
Numerical2PGradientCalculator.cxx for the main code MnStrategy.cxx for the definition of algorithm parameters MnMachinePrecision.h for the definition of eps and eps2 InitialGradientCalculator.cxx for the Minuit2 way of estimating initial gradients.
The exact value of fcnup has little impact on the results.
Definition at line 380 of file GDerivative.cpp.
References abs(), eps(), GFunction::eval(), G_MINUIT2, m_func, m_iter, max(), silent(), sqrt(), gammalib::str(), and gammalib::warning().
GDerivative & GDerivative::operator= | ( | const GDerivative & | dx | ) |
Assignment operator.
[in] | dx | Derivative. |
Definition at line 136 of file GDerivative.cpp.
References copy_members(), free_members(), and init_members().
Print derivative information.
[in] | chatter | Chattiness (defaults to NORMAL). |
Implements GBase.
Definition at line 750 of file GDerivative.cpp.
References eps(), max_iter(), gammalib::parformat(), SILENT, silent(), step_frac(), and gammalib::str().
double GDerivative::ridder | ( | const double & | x, |
const double & | h, | ||
double * | err | ||
) |
Returns derivative by Ridders' method.
[in] | x | Function value. |
[in] | h | Estimated initial step size. |
[out] | err | Estimated error in the derivative. |
GException::invalid_argument | Step size of 0 has been specified. |
Returns the derivative of a function at point x by Ridders's method of polynomial extrapolation. The value h is input as an estimated initial stepsize; it needs not to be small, but rather should be an increment in x over which the function changes substantially.
This method has been adopted from Numerical Recipes, 3rd edition, page 321f.
Definition at line 290 of file GDerivative.cpp.
References abs(), GFunction::eval(), G_RIDDER, and m_func.
Referenced by value().
double GDerivative::right_difference | ( | const double & | x, |
const double & | h | ||
) |
Returns gradient computed from right-sided function difference.
[in] | x | Function value. |
[in] | h | Step size. |
This is the most simple and dumb gradient computation method we can think of. It does a reasonable good job if the problem is well controlled, and in particular, if the step size is known in advance.
Definition at line 558 of file GDerivative.cpp.
References GFunction::eval(), and m_func.
|
protected |
Compute tiny number for Minuit2.
On fermi gives m_tiny = 8.88178e-16 for i=51.
Definition at line 847 of file GDerivative.cpp.
References m_tiny.
|
inline |
Set silence flag.
[in] | silent | Silence flag. |
Definition at line 218 of file GDerivative.hpp.
|
inline |
double GDerivative::smooth_robust | ( | const double & | x, |
const double & | h, | ||
const int & | degree = 2 , |
||
const int & | length = 5 |
||
) |
Returns smooth noise-robust derivative.
[in] | x | Function value. |
[in] | h | Step size. |
[in] | degree | Differentiator degree (default: 2). |
[in] | length | Filter length (default: 5). |
Compute a smooth noise-robust numerical derivative using the method proposed by Pavel Holoborodko (http://www.holoborodko.com/pavel/numerical-methods/) Formulae for the following filter lengths and degrees have been implemented:
degree=2 length=5,7,9,11 degree=4 length=7,9,11
Definition at line 587 of file GDerivative.cpp.
References GFunction::eval(), G_SMOOTH_ROBUST, m_func, and gammalib::str().
|
inline |
Set step fraction.
[in] | fraction | Step fraction. |
Definition at line 193 of file GDerivative.hpp.
References m_step_frac.
|
inline |
Get step fraction.
Definition at line 206 of file GDerivative.hpp.
References m_step_frac.
Referenced by print().
double GDerivative::value | ( | const double & | x, |
const double & | step = 0.0 |
||
) |
Returns derivative.
[in] | x | Function value. |
[in] | step | Initial step size (default: automatic) |
Computes derivative using the Ridders' method. If the specified initial step size is non-zero, the method starts with this step size and iteratively decreases the step size until the error becomes less than a threshold defined with the eps() method (by default the threshold is set to 1e-6). If the initial step size is zero, the method takes \(h={\tt m\_step\_frac} |x|\) as initial step size. The maximum number of iterations is controlled by the max_iter() method (by default, the maximum is set to 5).
Definition at line 205 of file GDerivative.cpp.
References abs(), G_VALUE, m_eps, m_iter, m_max_iter, m_step_frac, ridder(), silent(), gammalib::str(), and gammalib::warning().
|
protected |
Derivative precision.
Definition at line 104 of file GDerivative.hpp.
Referenced by copy_members(), eps(), init_members(), and value().
|
protected |
Pointer to function.
Definition at line 103 of file GDerivative.hpp.
Referenced by copy_members(), difference(), function(), init_members(), left_difference(), minuit2(), ridder(), right_difference(), and smooth_robust().
|
protected |
Number of iterations used.
Definition at line 108 of file GDerivative.hpp.
Referenced by copy_members(), init_members(), iter(), minuit2(), and value().
|
protected |
Maximum number of iterations.
Definition at line 107 of file GDerivative.hpp.
Referenced by copy_members(), init_members(), max_iter(), and value().
|
protected |
Suppress warnings.
Definition at line 109 of file GDerivative.hpp.
Referenced by copy_members(), init_members(), and silent().
|
protected |
Value fraction to use for initial step.
Definition at line 105 of file GDerivative.hpp.
Referenced by copy_members(), init_members(), step_frac(), and value().
|
protected |
Tiny number for minuit2.
Definition at line 106 of file GDerivative.hpp.
Referenced by copy_members(), init_members(), and set_tiny().