43 #ifndef O2SCL_DERIV_GSL_H
44 #define O2SCL_DERIV_GSL_H
54 #include <gsl/gsl_deriv.h>
55 #include <gsl/gsl_errno.h>
57 #include <o2scl/deriv.h>
58 #include <o2scl/funct.h>
60 #ifndef DOXYGEN_NO_O2NS
140 virtual int deriv_err(
double x, func_t &func,
double &dfdx,
double &err) {
141 return deriv_tlate<func_t>(x,func,dfdx,err);
145 virtual const char *
type() {
return "deriv_gsl"; }
147 #ifndef DOXYGEN_INTERNAL
154 double &dfdx,
double &err) {
157 if (x==0.0) hh=1.0e-4;
163 double r_0, round, trunc, error;
166 error = round + trunc;
168 if (round < trunc && (round > 0 && trunc > 0)) {
169 double r_opt, round_opt, trunc_opt, error_opt;
175 h_opt =
h * pow (round / (2.0 * trunc), 1.0 / 3.0);
177 error_opt = round_opt + trunc_opt;
182 if (error_opt < error && fabs (r_opt - r_0) < 4.0 * error) {
198 (
double x,
funct11 &func,
double &dfdx,
double &err) {
199 return deriv_tlate<>(x,func,dfdx,err);
212 template<
class func2_t>
214 double &abserr_round,
double &abserr_trunc,
217 double fm1, fp1, fmh, fph;
219 double eps=std::numeric_limits<double>::epsilon();
227 double r3 = 0.5 * (fp1 - fm1);
228 double r5 = (4.0 / 3.0) * (fph - fmh) - (1.0 / 3.0) * r3;
230 double e3 = (fabs (fp1) + fabs (fm1)) * eps;
231 double e5 = 2.0 * (fabs (fph) + fabs (fmh)) * eps + e3;
235 double dy=GSL_MAX(fabs(r3/hh),fabs(r5/hh))*fabs(x/hh)*eps;
245 abserr_trunc = fabs ((r5 - r3) / hh);
247 abserr_round = fabs (e5 / hh) + dy;
250 std::cout <<
"deriv_gsl: " << std::endl;
251 std::cout <<
"step: " << hh << std::endl;
252 std::cout <<
"abscissas: " << x-hh/2 <<
" " << x-hh <<
" "
253 << x+hh/2 <<
" " << x+hh << std::endl;
254 std::cout <<
"ordinates: " << fm1 <<
" " << fmh <<
" " << fph <<
" "
256 std::cout <<
"res: " << result <<
" trc: " << abserr_trunc
257 <<
" rnd: " << abserr_round << std::endl;
271 #ifndef DOXYGEN_NO_O2NS
double h_opt
The last value of the optimized stepsize.
int central_deriv(double x, double hh, double &result, double &abserr_round, double &abserr_trunc, func2_t &func)
Compute derivative using 5-point rule.
std::function< double(double)> funct11
One-dimensional function typedef.
virtual int deriv_err(double x, func_t &func, double &dfdx, double &err)
Calculate the first derivative of func w.r.t. x and uncertainty.
double h
Initial stepsize.
Numerical differentiation base [abstract base].
Numerical differentiation (GSL)
virtual const char * type()
Return string denoting type ("deriv_gsl")
int verbose
Output control.
int deriv_tlate(double x, func2_t &func, double &dfdx, double &err)
Internal template version of the derivative function.
virtual int deriv_err_int(double x, funct11 &func, double &dfdx, double &err)
Internal version of calc_err() for second and third derivatives.