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.
28 #include <gsl/gsl_math.h>
29 #include <gsl/gsl_errno.h>
30 #include <gsl/gsl_multifit_nlin.h>
31 #include <gsl/gsl_blas.h>
32 #include <gsl/gsl_linalg.h>
33 #include <gsl/gsl_permutation.h>
56 gsl_permutation * perm;
60 static int lmder_alloc (void *vstate, size_t n, size_t p);
61 static int lmder_set (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
62 static int lmsder_set (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
63 static int set (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale);
64 static int lmder_iterate (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
65 static void lmder_free (void *vstate);
66 static int iterate (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale);
71 #include "lmiterate.c"
75 lmder_alloc (void *vstate, size_t n, size_t p)
77 lmder_state_t *state = (lmder_state_t *) vstate;
79 gsl_vector *tau, *diag, *qtf, *newton, *gradient, *x_trial, *f_trial,
80 *df, *sdiag, *rptdx, *w, *work1;
81 gsl_permutation *perm;
83 r = gsl_matrix_calloc (n, p);
87 GSL_ERROR ("failed to allocate space for r", GSL_ENOMEM);
92 tau = gsl_vector_calloc (GSL_MIN(n, p));
98 GSL_ERROR ("failed to allocate space for tau", GSL_ENOMEM);
103 diag = gsl_vector_calloc (p);
108 gsl_vector_free (tau);
110 GSL_ERROR ("failed to allocate space for diag", GSL_ENOMEM);
115 qtf = gsl_vector_calloc (n);
120 gsl_vector_free (tau);
121 gsl_vector_free (diag);
123 GSL_ERROR ("failed to allocate space for qtf", GSL_ENOMEM);
128 newton = gsl_vector_calloc (p);
133 gsl_vector_free (tau);
134 gsl_vector_free (diag);
135 gsl_vector_free (qtf);
137 GSL_ERROR ("failed to allocate space for newton", GSL_ENOMEM);
140 state->newton = newton;
142 gradient = gsl_vector_calloc (p);
147 gsl_vector_free (tau);
148 gsl_vector_free (diag);
149 gsl_vector_free (qtf);
150 gsl_vector_free (newton);
152 GSL_ERROR ("failed to allocate space for gradient", GSL_ENOMEM);
155 state->gradient = gradient;
157 x_trial = gsl_vector_calloc (p);
162 gsl_vector_free (tau);
163 gsl_vector_free (diag);
164 gsl_vector_free (qtf);
165 gsl_vector_free (newton);
166 gsl_vector_free (gradient);
168 GSL_ERROR ("failed to allocate space for x_trial", GSL_ENOMEM);
171 state->x_trial = x_trial;
173 f_trial = gsl_vector_calloc (n);
178 gsl_vector_free (tau);
179 gsl_vector_free (diag);
180 gsl_vector_free (qtf);
181 gsl_vector_free (newton);
182 gsl_vector_free (gradient);
183 gsl_vector_free (x_trial);
185 GSL_ERROR ("failed to allocate space for f_trial", GSL_ENOMEM);
188 state->f_trial = f_trial;
190 df = gsl_vector_calloc (n);
195 gsl_vector_free (tau);
196 gsl_vector_free (diag);
197 gsl_vector_free (qtf);
198 gsl_vector_free (newton);
199 gsl_vector_free (gradient);
200 gsl_vector_free (x_trial);
201 gsl_vector_free (f_trial);
203 GSL_ERROR ("failed to allocate space for df", GSL_ENOMEM);
208 sdiag = gsl_vector_calloc (p);
213 gsl_vector_free (tau);
214 gsl_vector_free (diag);
215 gsl_vector_free (qtf);
216 gsl_vector_free (newton);
217 gsl_vector_free (gradient);
218 gsl_vector_free (x_trial);
219 gsl_vector_free (f_trial);
220 gsl_vector_free (df);
222 GSL_ERROR ("failed to allocate space for sdiag", GSL_ENOMEM);
225 state->sdiag = sdiag;
228 rptdx = gsl_vector_calloc (n);
233 gsl_vector_free (tau);
234 gsl_vector_free (diag);
235 gsl_vector_free (qtf);
236 gsl_vector_free (newton);
237 gsl_vector_free (gradient);
238 gsl_vector_free (x_trial);
239 gsl_vector_free (f_trial);
240 gsl_vector_free (df);
241 gsl_vector_free (sdiag);
243 GSL_ERROR ("failed to allocate space for rptdx", GSL_ENOMEM);
246 state->rptdx = rptdx;
248 w = gsl_vector_calloc (n);
253 gsl_vector_free (tau);
254 gsl_vector_free (diag);
255 gsl_vector_free (qtf);
256 gsl_vector_free (newton);
257 gsl_vector_free (gradient);
258 gsl_vector_free (x_trial);
259 gsl_vector_free (f_trial);
260 gsl_vector_free (df);
261 gsl_vector_free (sdiag);
262 gsl_vector_free (rptdx);
264 GSL_ERROR ("failed to allocate space for w", GSL_ENOMEM);
269 work1 = gsl_vector_calloc (p);
274 gsl_vector_free (tau);
275 gsl_vector_free (diag);
276 gsl_vector_free (qtf);
277 gsl_vector_free (newton);
278 gsl_vector_free (gradient);
279 gsl_vector_free (x_trial);
280 gsl_vector_free (f_trial);
281 gsl_vector_free (df);
282 gsl_vector_free (sdiag);
283 gsl_vector_free (rptdx);
286 GSL_ERROR ("failed to allocate space for work1", GSL_ENOMEM);
289 state->work1 = work1;
291 perm = gsl_permutation_calloc (p);
296 gsl_vector_free (tau);
297 gsl_vector_free (diag);
298 gsl_vector_free (qtf);
299 gsl_vector_free (newton);
300 gsl_vector_free (gradient);
301 gsl_vector_free (x_trial);
302 gsl_vector_free (f_trial);
303 gsl_vector_free (df);
304 gsl_vector_free (sdiag);
305 gsl_vector_free (rptdx);
307 gsl_vector_free (work1);
309 GSL_ERROR ("failed to allocate space for perm", GSL_ENOMEM);
318 lmder_set (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
320 int status = set (vstate, fdf, x, f, J, dx, 0);
325 lmsder_set (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
327 int status = set (vstate, fdf, x, f, J, dx, 1);
332 lmder_iterate (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
334 int status = iterate (vstate, fdf, x, f, J, dx, 0);
339 lmsder_iterate (void *vstate, gsl_multifit_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
341 int status = iterate (vstate, fdf, x, f, J, dx, 1);
346 lmder_free (void *vstate)
348 lmder_state_t *state = (lmder_state_t *) vstate;
350 gsl_permutation_free (state->perm);
351 gsl_vector_free (state->work1);
352 gsl_vector_free (state->w);
353 gsl_vector_free (state->rptdx);
354 gsl_vector_free (state->sdiag);
355 gsl_vector_free (state->df);
356 gsl_vector_free (state->f_trial);
357 gsl_vector_free (state->x_trial);
358 gsl_vector_free (state->gradient);
359 gsl_vector_free (state->newton);
360 gsl_vector_free (state->qtf);
361 gsl_vector_free (state->diag);
362 gsl_vector_free (state->tau);
363 gsl_matrix_free (state->r);
366 static const gsl_multifit_fdfsolver_type lmder_type =
369 sizeof (lmder_state_t),
376 static const gsl_multifit_fdfsolver_type lmsder_type =
379 sizeof (lmder_state_t),
386 const gsl_multifit_fdfsolver_type *gsl_multifit_fdfsolver_lmder = &lmder_type;
387 const gsl_multifit_fdfsolver_type *gsl_multifit_fdfsolver_lmsder = &lmsder_type;