GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GDerivative Class Reference

Numerical derivatives class. More...

#include <GDerivative.hpp>

Inheritance diagram for GDerivative:
GBase

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...
 
GDerivativeoperator= (const GDerivative &dx)
 Assignment operator. More...
 
void clear (void)
 Clear derivative. More...
 
GDerivativeclone (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 GFunctionfunction (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 &degree=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

GFunctionm_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...
 

Detailed Description

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.

Constructor & Destructor Documentation

GDerivative::GDerivative ( void  )

Void constructor.

Definition at line 60 of file GDerivative.cpp.

References init_members().

Referenced by clone().

GDerivative::GDerivative ( GFunction func)
explicit

Function constructor.

Parameters
[in]funcFunction 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.

Parameters
[in]dxDerivative.

Definition at line 98 of file GDerivative.cpp.

References copy_members(), and init_members().

GDerivative::~GDerivative ( void  )
virtual

Destructor.

Definition at line 114 of file GDerivative.cpp.

References free_members().

Member Function Documentation

std::string GDerivative::classname ( void  ) const
inlinevirtual

Return class name.

Returns
String containing the class name ("GDerivative").

Implements GBase.

Definition at line 119 of file GDerivative.hpp.

void GDerivative::clear ( void  )
virtual

Clear derivative.

Implements GBase.

Definition at line 166 of file GDerivative.cpp.

References free_members(), and init_members().

GDerivative * GDerivative::clone ( void  ) const
virtual

Clone derivative.

Returns
Pointer to deep copy of derivative.

Implements GBase.

Definition at line 184 of file GDerivative.cpp.

References GDerivative().

void GDerivative::copy_members ( const GDerivative dx)
protected

Copy class members.

Parameters
[in]dxDerivative.

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.

Parameters
[in]xFunction value.
[in]hStep 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.

void GDerivative::eps ( const double &  eps)
inline

Set precision.

Parameters
[in]epsPrecision.

Definition at line 168 of file GDerivative.hpp.

References eps(), and m_eps.

const double & GDerivative::eps ( void  ) const
inline

Get precision.

Returns
Precision.

Definition at line 181 of file GDerivative.hpp.

References m_eps.

Referenced by eps(), minuit2(), and print().

void GDerivative::free_members ( void  )
protected

Delete class members.

Definition at line 835 of file GDerivative.cpp.

Referenced by clear(), operator=(), and ~GDerivative().

void GDerivative::function ( GFunction function)
inline

Set function.

Parameters
[in]functionFunction.

Sets the function for which the derivative should be determined.

Definition at line 245 of file GDerivative.hpp.

References m_func.

const GFunction * GDerivative::function ( void  ) const
inline

Get function.

Returns
Function.

Definition at line 258 of file GDerivative.hpp.

References m_func.

void GDerivative::init_members ( void  )
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=().

const int & GDerivative::iter ( void  ) const
inline

Return number of iterations.

Returns
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.

Parameters
[in]xFunction value.
[in]hStep 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.

void GDerivative::max_iter ( const int &  max_iter)
inline

Set maximum number of iterations.

Parameters
[in]max_iterMaximum number of iterations.

Definition at line 143 of file GDerivative.hpp.

References m_max_iter, and max_iter().

const int & GDerivative::max_iter ( void  ) const
inline

Return maximum number of iterations.

Returns
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.

Parameters
[in]xFunction value.
[out]errEstimated 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.

Parameters
[in]dxDerivative.
Returns
Derivative.

Definition at line 136 of file GDerivative.cpp.

References copy_members(), free_members(), and init_members().

std::string GDerivative::print ( const GChatter chatter = NORMAL) const
virtual

Print derivative information.

Parameters
[in]chatterChattiness (defaults to NORMAL).
Returns
String containing derivative information.

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.

Parameters
[in]xFunction value.
[in]hEstimated initial step size.
[out]errEstimated error in the derivative.
Exceptions
GException::invalid_argumentStep 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.

Parameters
[in]xFunction value.
[in]hStep 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.

void GDerivative::set_tiny ( void  )
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.

void GDerivative::silent ( const bool &  silent)
inline

Set silence flag.

Parameters
[in]silentSilence flag.

Definition at line 218 of file GDerivative.hpp.

References m_silent, and silent().

const bool & GDerivative::silent ( void  ) const
inline

Get silence flag.

Returns
True is class is silent, false otherwise.

Definition at line 231 of file GDerivative.hpp.

References m_silent.

Referenced by minuit2(), print(), silent(), and value().

double GDerivative::smooth_robust ( const double &  x,
const double &  h,
const int &  degree = 2,
const int &  length = 5 
)

Returns smooth noise-robust derivative.

Parameters
[in]xFunction value.
[in]hStep size.
[in]degreeDifferentiator degree (default: 2).
[in]lengthFilter 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().

void GDerivative::step_frac ( const double &  fraction)
inline

Set step fraction.

Parameters
[in]fractionStep fraction.

Definition at line 193 of file GDerivative.hpp.

References m_step_frac.

const double & GDerivative::step_frac ( void  ) const
inline

Get step fraction.

Returns
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.

Parameters
[in]xFunction value.
[in]stepInitial 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().

Member Data Documentation

double GDerivative::m_eps
protected

Derivative precision.

Definition at line 104 of file GDerivative.hpp.

Referenced by copy_members(), eps(), init_members(), and value().

GFunction* GDerivative::m_func
protected
int GDerivative::m_iter
protected

Number of iterations used.

Definition at line 108 of file GDerivative.hpp.

Referenced by copy_members(), init_members(), iter(), minuit2(), and value().

int GDerivative::m_max_iter
protected

Maximum number of iterations.

Definition at line 107 of file GDerivative.hpp.

Referenced by copy_members(), init_members(), max_iter(), and value().

bool GDerivative::m_silent
protected

Suppress warnings.

Definition at line 109 of file GDerivative.hpp.

Referenced by copy_members(), init_members(), and silent().

double GDerivative::m_step_frac
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().

double GDerivative::m_tiny
protected

Tiny number for minuit2.

Definition at line 106 of file GDerivative.hpp.

Referenced by copy_members(), init_members(), and set_tiny().


The documentation for this class was generated from the following files: