29 #define SQRT_EPSILON sqrt(std::numeric_limits< C_FLOAT64 >::epsilon())
42 const double r = (3. - sqrt(5.0)) / 2;
45 if (tol <= 0)
return 1;
49 v = a + r * (b - a); fv = (*pF)(v);
53 for (iter = 0; iter < maxiter; iter++)
56 double middle_range = (a + b) / 2;
61 if (fabs(x - middle_range) + range / 2 <= 2*tol_act)
68 new_step = r * (x < middle_range ? b - x : a - x);
73 if (fabs(x - w) >= tol_act)
79 t = (x - w) * (fx - fv);
80 q = (x - v) * (fx - fw);
81 p = (x - v) * q - (x - w) * t;
89 if (fabs(p) < fabs(new_step*q) &&
90 p > q*(a - x + 2*tol_act) &&
91 p < q*(b - x - 2*tol_act))
100 if (fabs(new_step) < tol_act)
102 if (new_step > (
double)0)
110 double t = x + new_step;
111 double ft = (*pF)(t);
121 fv = fw; fw = fx; fx = ft;
130 if (ft <= fw || w == x)
135 else if (ft <= fv || v == x || v == w)
144 *min = x; *fmin = fx;
int FminBrent(double a, double b, FDescent *pF, double *min, double *fmin, double tol, int maxiter)