1 /* multimin/fminimizer.c
2 *
3 * Copyright (C) 2002, 2009 Tuomo Keskitalo, Ivo Alxneit
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include <config.h>
21 #include <gsl/gsl_errno.h>
22 #include <gsl/gsl_multimin.h>
23
24 gsl_multimin_fminimizer *
gsl_multimin_fminimizer_alloc(const gsl_multimin_fminimizer_type * T,size_t n)25 gsl_multimin_fminimizer_alloc (const gsl_multimin_fminimizer_type * T,
26 size_t n)
27 {
28 int status;
29
30 gsl_multimin_fminimizer *s =
31 (gsl_multimin_fminimizer *) malloc (sizeof (gsl_multimin_fminimizer));
32
33 if (s == 0)
34 {
35 GSL_ERROR_VAL ("failed to allocate space for minimizer struct",
36 GSL_ENOMEM, 0);
37 }
38
39 s->type = T;
40
41 s->x = gsl_vector_calloc (n);
42
43 if (s->x == 0)
44 {
45 free (s);
46 GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
47 }
48
49 s->state = malloc (T->size);
50
51 if (s->state == 0)
52 {
53 gsl_vector_free (s->x);
54 free (s);
55 GSL_ERROR_VAL ("failed to allocate space for minimizer state",
56 GSL_ENOMEM, 0);
57 }
58
59 status = (T->alloc) (s->state, n);
60
61 if (status != GSL_SUCCESS)
62 {
63 free (s->state);
64 gsl_vector_free (s->x);
65 free (s);
66
67 GSL_ERROR_VAL ("failed to initialize minimizer state", GSL_ENOMEM, 0);
68 }
69
70 return s;
71 }
72
73 int
gsl_multimin_fminimizer_set(gsl_multimin_fminimizer * s,gsl_multimin_function * f,const gsl_vector * x,const gsl_vector * step_size)74 gsl_multimin_fminimizer_set (gsl_multimin_fminimizer * s,
75 gsl_multimin_function * f,
76 const gsl_vector * x,
77 const gsl_vector * step_size)
78 {
79 if (s->x->size != f->n)
80 {
81 GSL_ERROR ("function incompatible with solver size", GSL_EBADLEN);
82 }
83
84 if (x->size != f->n || step_size->size != f->n)
85 {
86 GSL_ERROR ("vector length not compatible with function", GSL_EBADLEN);
87 }
88
89 s->f = f;
90
91 gsl_vector_memcpy (s->x,x);
92
93 return (s->type->set) (s->state, s->f, s->x, &(s->size), step_size);
94 }
95
96 void
gsl_multimin_fminimizer_free(gsl_multimin_fminimizer * s)97 gsl_multimin_fminimizer_free (gsl_multimin_fminimizer * s)
98 {
99 RETURN_IF_NULL (s);
100 (s->type->free) (s->state);
101 free (s->state);
102 gsl_vector_free (s->x);
103 free (s);
104 }
105
106 int
gsl_multimin_fminimizer_iterate(gsl_multimin_fminimizer * s)107 gsl_multimin_fminimizer_iterate (gsl_multimin_fminimizer * s)
108 {
109 return (s->type->iterate) (s->state, s->f, s->x, &(s->size), &(s->fval));
110 }
111
112 const char *
gsl_multimin_fminimizer_name(const gsl_multimin_fminimizer * s)113 gsl_multimin_fminimizer_name (const gsl_multimin_fminimizer * s)
114 {
115 return s->type->name;
116 }
117
118
119 gsl_vector *
gsl_multimin_fminimizer_x(const gsl_multimin_fminimizer * s)120 gsl_multimin_fminimizer_x (const gsl_multimin_fminimizer * s)
121 {
122 return s->x;
123 }
124
125 double
gsl_multimin_fminimizer_minimum(const gsl_multimin_fminimizer * s)126 gsl_multimin_fminimizer_minimum (const gsl_multimin_fminimizer * s)
127 {
128 return s->fval;
129 }
130
131 double
gsl_multimin_fminimizer_size(const gsl_multimin_fminimizer * s)132 gsl_multimin_fminimizer_size (const gsl_multimin_fminimizer * s)
133 {
134 return s->size;
135 }
136