1 /* multimin/fminimizer.c
3 * Copyright (C) 2002 Tuomo Keskitalo, Ivo Alxneit
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_fminimizer *
25 gsl_multimin_fminimizer_alloc (const gsl_multimin_fminimizer_type * T,
30 gsl_multimin_fminimizer *s =
31 (gsl_multimin_fminimizer *) malloc (sizeof (gsl_multimin_fminimizer));
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->state = malloc (T->size);
53 gsl_vector_free (s->x);
55 GSL_ERROR_VAL ("failed to allocate space for minimizer state",
59 status = (T->alloc) (s->state, n);
61 if (status != GSL_SUCCESS)
64 gsl_vector_free (s->x);
67 GSL_ERROR_VAL ("failed to initialize minimizer state", GSL_ENOMEM, 0);
74 gsl_multimin_fminimizer_set (gsl_multimin_fminimizer * s,
75 gsl_multimin_function * f,
77 const gsl_vector * step_size)
79 if (s->x->size != f->n)
81 GSL_ERROR ("function incompatible with solver size", GSL_EBADLEN);
84 if (x->size != f->n || step_size->size != f->n)
86 GSL_ERROR ("vector length not compatible with function", GSL_EBADLEN);
91 gsl_vector_memcpy (s->x,x);
93 return (s->type->set) (s->state, s->f, s->x, &(s->size), step_size);
97 gsl_multimin_fminimizer_free (gsl_multimin_fminimizer * s)
99 (s->type->free) (s->state);
101 gsl_vector_free (s->x);
106 gsl_multimin_fminimizer_iterate (gsl_multimin_fminimizer * s)
108 return (s->type->iterate) (s->state, s->f, s->x, &(s->size), &(s->fval));
112 gsl_multimin_fminimizer_name (const gsl_multimin_fminimizer * s)
114 return s->type->name;
119 gsl_multimin_fminimizer_x (const gsl_multimin_fminimizer * s)
125 gsl_multimin_fminimizer_minimum (const gsl_multimin_fminimizer * s)
131 gsl_multimin_fminimizer_size (const gsl_multimin_fminimizer * s)