1 /* multimin/fdfminimizer.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.
21 #include <gsl/gsl_errno.h>
22 #include <gsl/gsl_multimin.h>
24 gsl_multimin_fdfminimizer *
25 gsl_multimin_fdfminimizer_alloc (const gsl_multimin_fdfminimizer_type * T,
30 gsl_multimin_fdfminimizer *s =
31 (gsl_multimin_fdfminimizer *) malloc (sizeof (gsl_multimin_fdfminimizer));
35 GSL_ERROR_VAL ("failed to allocate space for minimizer struct",
41 s->x = gsl_vector_calloc (n);
46 GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
49 s->gradient = gsl_vector_calloc (n);
53 gsl_vector_free (s->x);
55 GSL_ERROR_VAL ("failed to allocate space for gradient", GSL_ENOMEM, 0);
58 s->dx = gsl_vector_calloc (n);
62 gsl_vector_free (s->x);
63 gsl_vector_free (s->gradient);
65 GSL_ERROR_VAL ("failed to allocate space for dx", GSL_ENOMEM, 0);
68 s->state = malloc (T->size);
72 gsl_vector_free (s->x);
73 gsl_vector_free (s->gradient);
74 gsl_vector_free (s->dx);
76 GSL_ERROR_VAL ("failed to allocate space for minimizer state",
80 status = (T->alloc) (s->state, n);
82 if (status != GSL_SUCCESS)
85 gsl_vector_free (s->x);
86 gsl_vector_free (s->gradient);
87 gsl_vector_free (s->dx);
90 GSL_ERROR_VAL ("failed to initialize minimizer state", GSL_ENOMEM, 0);
97 gsl_multimin_fdfminimizer_set (gsl_multimin_fdfminimizer * s,
98 gsl_multimin_function_fdf * fdf,
100 double step_size, double tol)
102 if (s->x->size != fdf->n)
104 GSL_ERROR ("function incompatible with solver size", GSL_EBADLEN);
107 if (x->size != fdf->n)
109 GSL_ERROR ("vector length not compatible with function", GSL_EBADLEN);
114 gsl_vector_memcpy (s->x,x);
115 gsl_vector_set_zero (s->dx);
117 return (s->type->set) (s->state, s->fdf, s->x, &(s->f), s->gradient, step_size, tol);
121 gsl_multimin_fdfminimizer_free (gsl_multimin_fdfminimizer * s)
123 (s->type->free) (s->state);
125 gsl_vector_free (s->dx);
126 gsl_vector_free (s->gradient);
127 gsl_vector_free (s->x);
132 gsl_multimin_fdfminimizer_iterate (gsl_multimin_fdfminimizer * s)
134 return (s->type->iterate) (s->state, s->fdf, s->x, &(s->f), s->gradient, s->dx);
138 gsl_multimin_fdfminimizer_restart (gsl_multimin_fdfminimizer * s)
140 return (s->type->restart) (s->state);
144 gsl_multimin_fdfminimizer_name (const gsl_multimin_fdfminimizer * s)
146 return s->type->name;
151 gsl_multimin_fdfminimizer_x (const gsl_multimin_fdfminimizer * s)
157 gsl_multimin_fdfminimizer_dx (const gsl_multimin_fdfminimizer * s)
163 gsl_multimin_fdfminimizer_gradient (const gsl_multimin_fdfminimizer * s)
169 gsl_multimin_fdfminimizer_minimum (const gsl_multimin_fdfminimizer * s)