3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* brent.c -- brent minimum finding algorithm */
30 #include <gsl/gsl_math.h>
31 #include <gsl/gsl_errno.h>
32 #include <gsl/gsl_min.h>
43 static int brent_init (void *vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper);
44 static int brent_iterate (void *vstate, gsl_function * f, double *x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper);
47 brent_init (void *vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper)
49 brent_state_t *state = (brent_state_t *) vstate;
51 const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
53 double v = x_lower + golden * (x_upper - x_lower);
58 x_minimum = 0 ; /* avoid warnings about unused varibles */
69 SAFE_FUNC_CALL (f, v, &f_vw);
78 brent_iterate (void *vstate, gsl_function * f, double *x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper)
80 brent_state_t *state = (brent_state_t *) vstate;
82 const double x_left = *x_lower;
83 const double x_right = *x_upper;
85 const double z = *x_minimum;
89 const double v = state->v;
90 const double w = state->w;
91 const double f_v = state->f_v;
92 const double f_w = state->f_w;
93 const double f_z = *f_minimum;
95 const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
97 const double w_lower = (z - x_left);
98 const double w_upper = (x_right - z);
100 const double tolerance = GSL_SQRT_DBL_EPSILON * fabs (z);
102 double p = 0, q = 0, r = 0;
104 const double midpoint = 0.5 * (x_left + x_right);
106 if (fabs (e) > tolerance)
110 r = (z - w) * (f_z - f_v);
111 q = (z - v) * (f_z - f_w);
112 p = (z - v) * q - (z - w) * r;
128 if (fabs (p) < fabs (0.5 * q * r) && p < q * w_lower && p < q * w_upper)
130 double t2 = 2 * tolerance ;
135 if ((u - x_left) < t2 || (x_right - u) < t2)
137 d = (z < midpoint) ? tolerance : -tolerance ;
142 e = (z < midpoint) ? x_right - z : -(z - x_left) ;
147 if (fabs (d) >= tolerance)
153 u = z + ((d > 0) ? tolerance : -tolerance) ;
159 SAFE_FUNC_CALL(f, u, &f_u);
195 if (f_u <= f_w || w == z)
203 else if (f_u <= f_v || v == z || v == w)
215 static const gsl_min_fminimizer_type brent_type =
217 sizeof (brent_state_t),
221 const gsl_min_fminimizer_type *gsl_min_fminimizer_brent = &brent_type;