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_multifit_nlin.h>
26 gsl_multifit_fsolver *
27 gsl_multifit_fsolver_alloc (const gsl_multifit_fsolver_type * T,
32 gsl_multifit_fsolver * s;
36 GSL_ERROR_VAL ("insufficient data points, n < p", GSL_EINVAL, 0);
39 s = (gsl_multifit_fsolver *) malloc (sizeof (gsl_multifit_fsolver));
43 GSL_ERROR_VAL ("failed to allocate space for multifit solver struct",
47 s->x = gsl_vector_calloc (p);
52 GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
55 s->f = gsl_vector_calloc (n);
59 gsl_vector_free (s->x);
61 GSL_ERROR_VAL ("failed to allocate space for f", GSL_ENOMEM, 0);
64 s->dx = gsl_vector_calloc (p);
68 gsl_vector_free (s->x);
69 gsl_vector_free (s->f);
71 GSL_ERROR_VAL ("failed to allocate space for dx", GSL_ENOMEM, 0);
74 s->state = malloc (T->size);
78 gsl_vector_free (s->dx);
79 gsl_vector_free (s->x);
80 gsl_vector_free (s->f);
81 free (s); /* exception in constructor, avoid memory leak */
83 GSL_ERROR_VAL ("failed to allocate space for multifit solver state",
89 status = (s->type->alloc)(s->state, n, p);
91 if (status != GSL_SUCCESS)
93 (s->type->free)(s->state);
95 gsl_vector_free (s->dx);
96 gsl_vector_free (s->x);
97 gsl_vector_free (s->f);
98 free (s); /* exception in constructor, avoid memory leak */
100 GSL_ERROR_VAL ("failed to set solver", status, 0);
109 gsl_multifit_fsolver_set (gsl_multifit_fsolver * s,
110 gsl_multifit_function * f,
111 const gsl_vector * x)
113 if (s->f->size != f->n)
115 GSL_ERROR ("function size does not match solver", GSL_EBADLEN);
118 if (s->x->size != x->size)
120 GSL_ERROR ("vector length does not match solver", GSL_EBADLEN);
124 gsl_vector_memcpy(s->x,x);
126 return (s->type->set) (s->state, s->function, s->x, s->f, s->dx);
130 gsl_multifit_fsolver_iterate (gsl_multifit_fsolver * s)
132 return (s->type->iterate) (s->state, s->function, s->x, s->f, s->dx);
136 gsl_multifit_fsolver_free (gsl_multifit_fsolver * s)
138 (s->type->free) (s->state);
140 gsl_vector_free (s->dx);
141 gsl_vector_free (s->x);
142 gsl_vector_free (s->f);
147 gsl_multifit_fsolver_name (const gsl_multifit_fsolver * s)
149 return s->type->name;
153 gsl_multifit_fsolver_position (const gsl_multifit_fsolver * s)