36 #define G_VALUE "GDerivative::value(double&, double&)"
37 #define G_RIDDER "GDerivative::ridder(double&, double&, double*)"
38 #define G_MINUIT2 "GDerivative::minuit2(double&, double*)"
39 #define G_SMOOTH_ROBUST "GDerivative::smooth_robust(double&, double&, int&,"\
47 #define G_VALUE_DEBUG 0
48 #define G_MINUIT_DEBUG 0
208 const double min_h = 1.0e-6;
225 double err = DBL_MAX;
232 result =
ridder(x, h, &err);
236 std::cout <<
"GDerivative::value(";
237 std::cout <<
"iter=" <<
m_iter;
238 std::cout <<
", x=" << x;
239 std::cout <<
", dy/dx=" << result;
240 std::cout <<
", h=" << h;
241 std::cout <<
", err=" << err;
242 std::cout <<
")" << std::endl;
258 std::string msg =
"Derivative uncertainty "+
gammalib::str(err)+
294 const double con = 1.4;
295 const double con2 = con * con;
296 const double big = DBL_MAX;
297 const double safe = 2.0;
321 for (
int i = 1; i < ntab; ++i) {
332 for (
int j = 1; j <= i; ++j) {
335 a(j,i) = ( a(j-1,i) * fac - a(j-1,i-1) ) / (fac - 1.0);
339 double err1 =
std::abs(a(j,i) - a(j-1,i));
340 double err2 =
std::abs(a(j,i) - a(j-1,i-1));
341 double errt = (err1 > err2) ? err1 : err2;
350 if (
std::abs( a(i,i) - a(i-1,i-1) ) >= safe*(*err)) {
398 double fcnup = 0.01 * fcnmin;
399 double dfmin = 8.0 * eps2 * (
std::abs(fcnmin) + fcnup);
400 double vrysml = 8.0 * eps *
eps;
404 double step_tol = 0.1;
405 double grad_tol = 0.02;
410 double gstep = 0.001;
413 double grd = 0.5 * (fs1 - fs2) / gstep;
414 double g2 = (fs1 + fs2 - 2.0*fcnmin) / gstep / gstep;
418 std::cout <<
"GDerivative::minuit(";
419 std::cout <<
"iter=0";
420 std::cout <<
", x=" << x;
421 std::cout <<
", grd=" << grd;
422 std::cout <<
", step=" << gstep;
423 std::cout <<
")" << std::endl;
427 double epspri = eps2 +
std::abs(grd*eps2);
430 double step_change = 0.0;
437 double optstp =
std::sqrt(dfmin/(std::abs(g2)+epspri));
440 step =
std::max(optstp, std::abs(0.1*gstep));
441 double stpmax = 10.0 *
std::abs(gstep);
442 double stpmin =
std::max(vrysml, 8.0 * std::abs(eps2*x));
443 if (step > stpmax) step = stpmax;
444 if (step < stpmin) step = stpmin;
447 step_change =
std::abs((step-stepb4)/step);
448 if (step_change < step_tol) {
462 grd = 0.5 * (fs1 - fs2) / step;
463 g2 = (fs1 + fs2 - 2.0*fcnmin) / step / step;
470 std::cout <<
"GDerivative::minuit(";
471 std::cout <<
"iter=" <<
m_iter;
472 std::cout <<
", x=" << x;
473 std::cout <<
", grd=" << grd;
474 std::cout <<
", step=" << step;
475 std::cout <<
", err=" << *err;
476 std::cout <<
")" << std::endl;
480 if (*err < grad_tol) {
488 if (*err >= grad_tol) {
489 std::string msg =
"Derivative uncertainty "+
gammalib::str(*err)+
519 double derivative = 0.5 * (fs1 - fs2) / h;
541 double derivative = (fs1 - fs2) / h;
563 double derivative = (fs1 - fs2) / h;
588 const int& degree,
const int& length)
591 double derivative = 0.0;
606 derivative = (2.0*(fp1-fm1) + (fp2-fm2))/(8.0*h);
619 derivative = (5.0*(fp1-fm1) + 4.0*(fp2-fm2) + (fp3-fm3))/(32.0*h);
635 derivative = (14.0*(fp1-fm1) + 14.0*(fp2-fm2) + 6.0*(fp3-fm3) +
636 (fp4-fm4))/(128.0*h);
655 derivative = (42.0*(fp1-fm1) + 48.0*(fp2-fm2) + 27.0*(fp3-fm3) +
656 8.0*(fp4-fm4) + (fp5-fm5))/(512.0*h);
660 std::string msg =
"Specified filter length "+
662 "specify 5,7,9 or 11 as filter length.";
681 derivative = (39.0*(fp1-fm1) + 12.0*(fp2-fm2) - 5.0*(fp3-fm3)) /
698 derivative = (27.0*(fp1-fm1) + 16.0*(fp2-fm2) - (fp3-fm3) -
699 2.0*(fp4-fm4)) / (96.0*h);
718 derivative = (322.0*(fp1-fm1) + 256.0*(fp2-fm2) + 39.0*(fp3-fm3) -
719 32.0*(fp4-fm4) - 11.0*(fp5-fm5)) / (1536.0*h);
723 std::string msg =
"Specified filter length "+
725 "specify 7,9 or 11 as filter length.";
734 " not implemented. Please specify 2 as degree.";
759 result.append(
"=== GDerivative ===");
773 result.append(
"in standard output");
854 const double one = 1.0;
857 for (
int i = 0; i < 100; ++i) {
859 double epsp1 = one + epstry;
860 double epsbak = tiny(epsp1);
861 if (epsbak < epstry) {
888 double result = eps - one();
double right_difference(const double &x, const double &h)
Returns gradient computed from right-sided function difference.
double m_step_frac
Value fraction to use for initial step.
int m_max_iter
Maximum number of iterations.
double value(const double &x, const double &step=0.0)
Returns derivative.
const double & step_frac(void) const
Get step fraction.
void warning(const std::string &origin, const std::string &message)
Emits warning.
double one(void) const
Return one.
GVector abs(const GVector &vector)
Computes absolute of vector elements.
Generic matrix class definition.
double operator()(double eps) const
Evaluate minimal difference between two floating points.
double m_eps
Derivative precision.
double difference(const double &x, const double &h)
Returns gradient computed from symmetric function difference.
double ridder(const double &x, const double &h, double *err)
Returns derivative by Ridders' method.
GDerivative * clone(void) const
Clone derivative.
GDerivative & operator=(const GDerivative &dx)
Assignment operator.
GFunction * m_func
Pointer to function.
const int & max_iter(void) const
Return maximum number of iterations.
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
void init_members(void)
Initialise class members.
double m_tiny
Tiny number for minuit2.
const double & eps(void) const
Get precision.
double smooth_robust(const double &x, const double &h, const int °ree=2, const int &length=5)
Returns smooth noise-robust derivative.
virtual double eval(const double &x)=0
const bool & silent(void) const
Get silence flag.
void set_tiny(void)
Compute tiny number for Minuit2.
double max(const GVector &vector)
Computes maximum vector element.
GDerivative(void)
Void constructor.
std::string print(const GChatter &chatter=NORMAL) const
Print derivative information.
double left_difference(const double &x, const double &h)
Returns gradient computed from left-sided function difference.
void clear(void)
Clear derivative.
Numerical derivatives class.
GDerivative class interface definition.
Single parameter function abstract base class.
virtual ~GDerivative(void)
Destructor.
bool m_silent
Suppress warnings.
void copy_members(const GDerivative &dx)
Copy class members.
Generic matrix class definition.
int m_iter
Number of iterations used.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
void free_members(void)
Delete class members.
double minuit2(const double &x, double *err)
Returns derivative using Minuit2 algorithm.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.