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.
23 #include <gsl/gsl_errno.h>
24 #include <gsl/gsl_min.h>
29 compute_f_values (gsl_function * f, double x_minimum, double * f_minimum,
30 double x_lower, double * f_lower,
31 double x_upper, double * f_upper);
35 compute_f_values (gsl_function * f, double x_minimum, double * f_minimum,
36 double x_lower, double * f_lower,
37 double x_upper, double * f_upper)
39 SAFE_FUNC_CALL(f, x_lower, f_lower);
40 SAFE_FUNC_CALL(f, x_upper, f_upper);
41 SAFE_FUNC_CALL(f, x_minimum, f_minimum);
47 gsl_min_fminimizer_set (gsl_min_fminimizer * s,
49 double x_minimum, double x_lower, double x_upper)
53 double f_minimum, f_lower, f_upper;
55 status = compute_f_values (f, x_minimum, &f_minimum,
59 if (status != GSL_SUCCESS)
64 status = gsl_min_fminimizer_set_with_values (s, f, x_minimum, f_minimum,
71 gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T)
73 gsl_min_fminimizer * s =
74 (gsl_min_fminimizer *) malloc (sizeof (gsl_min_fminimizer));
78 GSL_ERROR_VAL ("failed to allocate space for minimizer struct",
82 s->state = malloc (T->size);
86 free (s); /* exception in constructor, avoid memory leak */
88 GSL_ERROR_VAL ("failed to allocate space for minimizer state",
99 gsl_min_fminimizer_set_with_values (gsl_min_fminimizer * s, gsl_function * f,
100 double x_minimum, double f_minimum,
101 double x_lower, double f_lower,
102 double x_upper, double f_upper)
105 s->x_minimum = x_minimum;
106 s->x_lower = x_lower;
107 s->x_upper = x_upper;
109 if (x_lower > x_upper)
111 GSL_ERROR ("invalid interval (lower > upper)", GSL_EINVAL);
114 if (x_minimum >= x_upper || x_minimum <= x_lower)
116 GSL_ERROR ("x_minimum must lie inside interval (lower < x < upper)",
120 s->f_lower = f_lower;
121 s->f_upper = f_upper;
122 s->f_minimum = f_minimum;
124 if (f_minimum >= f_lower || f_minimum >= f_upper)
126 GSL_ERROR ("endpoints do not enclose a minimum", GSL_EINVAL);
129 return (s->type->set) (s->state, s->function,
130 x_minimum, f_minimum,
137 gsl_min_fminimizer_iterate (gsl_min_fminimizer * s)
139 return (s->type->iterate) (s->state, s->function,
140 &(s->x_minimum), &(s->f_minimum),
141 &(s->x_lower), &(s->f_lower),
142 &(s->x_upper), &(s->f_upper));
146 gsl_min_fminimizer_free (gsl_min_fminimizer * s)
153 gsl_min_fminimizer_name (const gsl_min_fminimizer * s)
155 return s->type->name;
158 /* Deprecated, use x_minimum instead */
160 gsl_min_fminimizer_minimum (const gsl_min_fminimizer * s)
166 gsl_min_fminimizer_x_minimum (const gsl_min_fminimizer * s)
172 gsl_min_fminimizer_x_lower (const gsl_min_fminimizer * s)
178 gsl_min_fminimizer_x_upper (const gsl_min_fminimizer * s)
184 gsl_min_fminimizer_f_minimum (const gsl_min_fminimizer * s)
190 gsl_min_fminimizer_f_lower (const gsl_min_fminimizer * s)
196 gsl_min_fminimizer_f_upper (const gsl_min_fminimizer * s)