30 #define GSL_SQRT_DBL_EPSILON 1.e-4
31 #define GSL_DBL_EPSILON 1.e-8
53 brent(
void *vstate,
double (*f) (),
double *x_minimum,
double *f_minimum,
54 double *x_lower,
double *f_lower,
double *x_upper,
double *f_upper)
58 const double x_left = *x_lower;
59 const double x_right = *x_upper;
61 const double z = *x_minimum;
65 const double v = state->
v;
66 const double w = state->
w;
67 const double f_v = state->
f_v;
68 const double f_w = state->
f_w;
69 const double f_z = *f_minimum;
71 const double golden = 0.3819660;
73 const double w_lower = (z - x_left);
74 const double w_upper = (x_right - z);
78 double p = 0,
q = 0,
r = 0;
80 const double midpoint = 0.5 * (x_left + x_right);
82 if (fabs(e) > tolerance) {
85 r = (z -
w) * (f_z - f_v);
86 q = (z - v) * (f_z - f_w);
87 p = (z - v) *
q - (z - w) *
r;
101 if (fabs(p) < fabs(0.5 *
q *
r) && p <
q * w_lower && p <
q * w_upper) {
102 double t2 = 2 * tolerance;
107 if ((u - x_left) < t2 || (x_right - z) < t2) {
108 d = (z < midpoint) ? tolerance : -tolerance;
112 e = (z < midpoint) ? x_right - z : -(z - x_left);
117 if (fabs(d) >= tolerance) {
121 u = z + ((d > 0) ? tolerance : -tolerance);
142 else if (f_u < f_z) {
160 else if (f_u <= f_w || w == z) {
167 else if (f_u <= f_v || v == z || v == w) {
184 double x_minimum = (x_upper + x_lower) / 2.;
191 const double golden = 0.3819660;
192 double v = x_lower + golden * (x_upper - x_lower);
196 f_lower = (*f) (x_lower);
197 f_upper = (*f) (x_upper);
198 f_minimum = (*f) (x_minimum);
213 for (i = 0; i < maxiter; i++) {
214 brent(&itstate, f, &x_minimum, &f_minimum, &x_lower, &f_lower,
#define GSL_SQRT_DBL_EPSILON
double brent_iterate(double(*f)(), double x_lower, double x_upper, int maxiter)