12 Double_t TNumDeriv::fgTiny=0;
13 Double_t TNumDeriv::fgEpsilon=0;
22 double dfdx,delta=fStep,rerr=0;
23 dfdx = numericalDerivative( add ,delta,rerr);
30 Double_t TNumDeriv::Tiny()
32 if (fgTiny)
return fgTiny;
33 for (
double d = 1; d ; d/=2) { fgTiny=d;}
38 Double_t TNumDeriv::Epsilon()
40 if (fgEpsilon)
return fgEpsilon;
41 for (
double d = 1; (d+1.)>1. ; d/=2) { fgEpsilon=d;}
45 double TNumDeriv::numericalDerivative(
double x,
double &delta,
double &error )
50 if(!h0) h0 = 5 * pow(2.0, -17);
52 const double maxErrorA = .0012;
53 const double maxErrorB = .0000026;
55 const double maxErrorC = .0003;
59 static const int nItersMax = 6;
61 double bestError = 1.0E30;
65 static const double valFactor = pow(2.0, -16);
66 static const double zero = 1e-10;
68 static const double w = 5.0/8;
69 static const double wi2 = 64.0/25.0;
70 static const double wi4 = wi2*wi2;
74 if (!finite(F0))
return 0;
75 double size0 = fabs(F0);
76 if (size0==0) size0 = pow(2.0, -53);
78 static const double adjustmentFactor[nItersMax] = {
86 for ( nIters = 0; nIters < nItersMax; ++nIters ) {
90 double h = h0 * adjustmentFactor[nIters];
95 if (!finite(Fr) || fOutLim)
continue;
97 if (!finite(Fl) || fOutLim)
continue;
99 double A1 = (Fr - Fl)/(2.0*h);
100 if (!finite(A1) || fabs(A1) < zero)
continue;
102 if (fabs(A1) > size) size = fabs(A1);
106 if (!finite(Fr) || fOutLim)
continue;
108 if (!finite(Fl) || fOutLim)
continue;
109 double A2 = (Fr-Fl)/(2.0*hh);
110 if (!finite(A2) || fabs(A2) < zero)
continue;
112 if (fabs(A2) > size) size = fabs(A2);
116 if (!finite(Fr) || fOutLim)
continue;
118 if (!finite(Fl) || fOutLim)
continue;
119 double A3 = (Fr-Fl)/(2.0*hh);
121 if (!finite(A3)|| fabs(A3) < zero)
continue;
122 if (fabs(A3) > size) size = fabs(A3);
124 if ( (fabs(A1-A2)/size > maxErrorA) || (fabs(A1-A3)/size > maxErrorA) ) {
130 double B1 = ( A2 * wi2 - A1 ) / ( wi2 - 1 );
131 if (!finite(B1))
continue;
132 double B2 = ( A3 * wi2 - A2 ) / ( wi2 - 1 );
133 if (!finite(B2))
continue;
134 if ( fabs(B1-B2)/size > maxErrorB ) {
140 double ans = ( B2 * wi4 - B1 ) / ( wi4 - 1 );
141 if (!finite(ans))
continue;
142 double err = fabs ( ans - B1 );
143 if ( err < bestError ) {
152 double val = (Fcn(x+hh) - Fcn(x-hh))/(2.0*hh);
153 if (!finite(val) || fOutLim)
continue;
154 if ( fabs(val-ans)/size > maxErrorC ) {