1 /* multimin/steepest_descent.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Fabrice Rossi
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 /* steepest_descent.c -- the steepest descent algorithm */
22 /* Modified by Brian Gough to use single iteration structure */
25 #include <gsl/gsl_multimin.h>
26 #include <gsl/gsl_blas_types.h>
27 #include <gsl/gsl_blas.h>
37 steepest_descent_state_t;
40 steepest_descent_alloc (void *vstate, size_t n)
42 steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
44 state->x1 = gsl_vector_alloc (n);
46 if (state->x1 == NULL)
48 GSL_ERROR ("failed to allocate space for x1", GSL_ENOMEM);
51 state->g1 = gsl_vector_alloc (n);
53 if (state->g1 == NULL)
55 gsl_vector_free (state->x1);
56 GSL_ERROR ("failed to allocate space for g1", GSL_ENOMEM);
63 steepest_descent_set (void *vstate, gsl_multimin_function_fdf * fdf,
64 const gsl_vector * x, double *f,
65 gsl_vector * gradient, double step_size, double tol)
67 steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
69 GSL_MULTIMIN_FN_EVAL_F_DF (fdf, x, f, gradient);
71 state->step = step_size;
72 state->max_step = step_size;
80 steepest_descent_free (void *vstate)
82 steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
84 gsl_vector_free (state->x1);
85 gsl_vector_free (state->g1);
89 steepest_descent_restart (void *vstate)
91 steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
93 state->step = state->max_step;
99 steepest_descent_iterate (void *vstate, gsl_multimin_function_fdf * fdf,
100 gsl_vector * x, double *f,
101 gsl_vector * gradient, gsl_vector * dx)
103 steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
105 gsl_vector *x1 = state->x1;
106 gsl_vector *g1 = state->g1;
110 double step = state->step, tol = state->tol;
114 /* compute new trial point at x1= x - step * dir, where dir is the
115 normalized gradient */
117 double gnorm = gsl_blas_dnrm2 (gradient);
121 gsl_vector_set_zero (dx);
126 gsl_vector_set_zero (dx);
127 gsl_blas_daxpy (-step / gnorm, gradient, dx);
129 gsl_vector_memcpy (x1, x);
130 gsl_blas_daxpy (1.0, dx, x1);
132 /* evaluate function and gradient at new point x1 */
134 GSL_MULTIMIN_FN_EVAL_F_DF (fdf, x1, &f1, g1);
138 /* downhill step failed, reduce step-size and try again */
152 gsl_vector_memcpy (x, x1);
153 gsl_vector_memcpy (gradient, g1);
160 static const gsl_multimin_fdfminimizer_type steepest_descent_type =
161 { "steepest_descent", /* name */
162 sizeof (steepest_descent_state_t),
163 &steepest_descent_alloc,
164 &steepest_descent_set,
165 &steepest_descent_iterate,
166 &steepest_descent_restart,
167 &steepest_descent_free
170 const gsl_multimin_fdfminimizer_type
171 * gsl_multimin_fdfminimizer_steepest_descent = &steepest_descent_type;