Public Member Functions | Public Attributes | Protected Member Functions | List of all members
o2scl::deriv_gsl< func_t, fp_t > Class Template Reference

Numerical differentiation (GSL) More...

#include <deriv_gsl.h>

Inheritance diagram for o2scl::deriv_gsl< func_t, fp_t >:
o2scl::deriv_base< funct, double >

Detailed Description

template<class func_t = funct, class fp_t = double>
class o2scl::deriv_gsl< func_t, fp_t >

This class computes the numerical derivative of a function. The stepsize h should be specified before use. If similar functions are being differentiated in succession, the user may be able to increase the speed of later derivatives by setting the new stepsize equal to the optimized stepsize from the previous differentiation, by setting h to h_opt.

The results will be incorrect for sufficiently difficult functions or if the step size is not properly chosen.

Some successive derivative computations can be made more efficient by using the optimized stepsize in deriv_gsl::h_opt , which is set by the most recent last derivative computation.

If the function returns a non-finite value, or if func_max is greater than zero and the absolute value of the function is larger than func_max, then this class attempts to decrease the step size by a factor of 10 in order to compute the derivative. The class gives up after 20 reductions of the step size.

If h is negative or zero, the initial step size is chosen to be $ 10^{-4} |x| $ or if $x=0$, then the initial step size is chosen to be $ 10^{-4} $ .

Setting deriv_base::verbose to a number greater than zero results in output for each call to central_deriv() which looks like:

deriv_gsl: 
step: 1.000000e-04
abscissas: 4.999500e-01 4.999000e-01 5.000500e-01 5.001000e-01
ordinates: 4.793377e-01 4.793816e-01 4.794694e-01 4.795132e-01
res: 8.775825e-01 trc: 1.462163e-09 rnd: 7.361543e-12

where the last line contains the result (res), the truncation error (trc) and the rounding error (rnd). If deriv_base::verbose is greater than 1, a keypress is required after each iteration.

If the function always returns a finite value, then computing first derivatives requires either 1 or 2 calls to central_deriv() and thus either 4 or 8 function calls.

Note
Second and third derivatives are computed by naive nested applications of the formula for the first derivative. No uncertainty for these derivatives is provided.

An example demonstrating the usage of this class is given in examples/ex_deriv.cpp and the Differentiation example .

Idea for Future:
Include the forward and backward GSL derivatives. These would be useful for EOS classes which run in to trouble for negative densities.

Definition at line 124 of file deriv_gsl.h.

Public Member Functions

virtual int deriv_err (fp_t x, func_t &func, fp_t &dfdx, fp_t &err)
 Calculate the first derivative of func w.r.t. x and uncertainty.
 
virtual const char * type ()
 Return string denoting type ("deriv_gsl")
 
- Public Member Functions inherited from o2scl::deriv_base< funct, double >
virtual double deriv (double x, funct &func)
 Calculate the first derivative of func w.r.t. x. More...
 
virtual double deriv2 (double x, funct &func)
 Calculate the second derivative of func w.r.t. x.
 
virtual double deriv3 (double x, funct &func)
 Calculate the third derivative of func w.r.t. x.
 
virtual double get_err ()
 Get uncertainty of last calculation.
 
virtual int deriv_err (double x, funct &func, double &dfdx, double &err)=0
 Calculate the first derivative of func w.r.t. x and the uncertainty.
 
virtual int deriv2_err (double x, funct &func, double &d2fdx2, double &err)
 Calculate the second derivative of func w.r.t. x and the uncertainty.
 
virtual int deriv3_err (double x, funct &func, double &d3fdx3, double &err)
 Calculate the third derivative of func w.r.t. x and the uncertainty.
 

Public Attributes

fp_t h
 Initial stepsize. More...
 
fp_t func_max
 Maximum absolute value of function, or a negative value for no maximum (default -1)
 
fp_t h_opt
 The last value of the optimized stepsize. More...
 
- Public Attributes inherited from o2scl::deriv_base< funct, double >
bool err_nonconv
 If true, call the error handler if the routine does not "converge".
 
int verbose
 Output control.
 

Protected Member Functions

template<class func2_t >
int deriv_tlate (fp_t x, func2_t &func, fp_t &dfdx, fp_t &err)
 Internal template version of the derivative function.
 
virtual int deriv_err_int (fp_t x, typename deriv_base< func_t, fp_t >::internal_func_t &func, fp_t &dfdx, fp_t &err)
 Internal version of calc_err() for second and third derivatives.
 
template<class func2_t >
int central_deriv (fp_t x, fp_t hh, fp_t &result, fp_t &abserr_round, fp_t &abserr_trunc, func2_t &func)
 Compute derivative using 5-point rule. More...
 
- Protected Member Functions inherited from o2scl::deriv_base< funct, double >
virtual double deriv_int (double x, internal_func_t &func)
 Calculate the first derivative of func w.r.t. x. More...
 
virtual int deriv_err_int (double x, internal_func_t &func, double &dfdx, double &err)=0
 Calculate the first derivative of func w.r.t. x and the uncertainty. More...
 
double derivfun (double x, funct *fp)
 The function for the second derivative.
 
double derivfun2 (double x, funct *fp)
 The function for the third derivative.
 

Additional Inherited Members

- Protected Types inherited from o2scl::deriv_base< funct, double >
typedef std::function< double(double)> internal_func_t
 
- Protected Attributes inherited from o2scl::deriv_base< funct, double >
bool from_deriv
 Avoids infinite loops in case the user calls the base class version.
 
double derr
 The uncertainity in the most recent derivative computation.
 

Member Function Documentation

◆ central_deriv()

template<class func_t = funct, class fp_t = double>
template<class func2_t >
int o2scl::deriv_gsl< func_t, fp_t >::central_deriv ( fp_t  x,
fp_t  hh,
fp_t &  result,
fp_t &  abserr_round,
fp_t &  abserr_trunc,
func2_t &  func 
)
inlineprotected

Compute the derivative using the 5-point rule (x-h, x-h/2, x, x+h/2, x+h) and the error using the difference between the 5-point and the 3-point rule (x-h,x,x+h). Note that the central point is not used for either.

This must be a class template because it is used by both deriv_err() and deriv_err_int().

Definition at line 268 of file deriv_gsl.h.

Member Data Documentation

◆ h

template<class func_t = funct, class fp_t = double>
fp_t o2scl::deriv_gsl< func_t, fp_t >::h

This should be specified before a call to deriv() or deriv_err(). If it is less than or equal to zero, then $ x 10^{-4} $ will used, or if x is zero, then $ 10^{-4} $ will be used.

Definition at line 144 of file deriv_gsl.h.

◆ h_opt

template<class func_t = funct, class fp_t = double>
fp_t o2scl::deriv_gsl< func_t, fp_t >::h_opt

This is initialized to zero in the constructor and set by deriv_err() to the most recent value of the optimized stepsize.

Definition at line 156 of file deriv_gsl.h.


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

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).