1 /* multiroots/hybridj.c
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_multiroots.h>
31 #include <gsl/gsl_linalg.h>
61 static int hybridj_alloc (void *vstate, size_t n);
62 static int hybridj_set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
63 static int hybridsj_set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
64 static int set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale);
65 static int hybridj_iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
66 static void hybridj_free (void *vstate);
67 static int iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale);
70 hybridj_alloc (void *vstate, size_t n)
72 hybridj_state_t *state = (hybridj_state_t *) vstate;
74 gsl_vector *tau, *diag, *qtf, *newton, *gradient, *x_trial, *f_trial,
75 *df, *qtdf, *rdx, *w, *v;
77 q = gsl_matrix_calloc (n, n);
81 GSL_ERROR ("failed to allocate space for q", GSL_ENOMEM);
86 r = gsl_matrix_calloc (n, n);
92 GSL_ERROR ("failed to allocate space for r", GSL_ENOMEM);
97 tau = gsl_vector_calloc (n);
104 GSL_ERROR ("failed to allocate space for tau", GSL_ENOMEM);
109 diag = gsl_vector_calloc (n);
115 gsl_vector_free (tau);
117 GSL_ERROR ("failed to allocate space for diag", GSL_ENOMEM);
122 qtf = gsl_vector_calloc (n);
128 gsl_vector_free (tau);
129 gsl_vector_free (diag);
131 GSL_ERROR ("failed to allocate space for qtf", GSL_ENOMEM);
136 newton = gsl_vector_calloc (n);
142 gsl_vector_free (tau);
143 gsl_vector_free (diag);
144 gsl_vector_free (qtf);
146 GSL_ERROR ("failed to allocate space for newton", GSL_ENOMEM);
149 state->newton = newton;
151 gradient = gsl_vector_calloc (n);
157 gsl_vector_free (tau);
158 gsl_vector_free (diag);
159 gsl_vector_free (qtf);
160 gsl_vector_free (newton);
162 GSL_ERROR ("failed to allocate space for gradient", GSL_ENOMEM);
165 state->gradient = gradient;
167 x_trial = gsl_vector_calloc (n);
173 gsl_vector_free (tau);
174 gsl_vector_free (diag);
175 gsl_vector_free (qtf);
176 gsl_vector_free (newton);
177 gsl_vector_free (gradient);
179 GSL_ERROR ("failed to allocate space for x_trial", GSL_ENOMEM);
182 state->x_trial = x_trial;
184 f_trial = gsl_vector_calloc (n);
190 gsl_vector_free (tau);
191 gsl_vector_free (diag);
192 gsl_vector_free (qtf);
193 gsl_vector_free (newton);
194 gsl_vector_free (gradient);
195 gsl_vector_free (x_trial);
197 GSL_ERROR ("failed to allocate space for f_trial", GSL_ENOMEM);
200 state->f_trial = f_trial;
202 df = gsl_vector_calloc (n);
208 gsl_vector_free (tau);
209 gsl_vector_free (diag);
210 gsl_vector_free (qtf);
211 gsl_vector_free (newton);
212 gsl_vector_free (gradient);
213 gsl_vector_free (x_trial);
214 gsl_vector_free (f_trial);
216 GSL_ERROR ("failed to allocate space for df", GSL_ENOMEM);
221 qtdf = gsl_vector_calloc (n);
227 gsl_vector_free (tau);
228 gsl_vector_free (diag);
229 gsl_vector_free (qtf);
230 gsl_vector_free (newton);
231 gsl_vector_free (gradient);
232 gsl_vector_free (x_trial);
233 gsl_vector_free (f_trial);
234 gsl_vector_free (df);
236 GSL_ERROR ("failed to allocate space for qtdf", GSL_ENOMEM);
242 rdx = gsl_vector_calloc (n);
248 gsl_vector_free (tau);
249 gsl_vector_free (diag);
250 gsl_vector_free (qtf);
251 gsl_vector_free (newton);
252 gsl_vector_free (gradient);
253 gsl_vector_free (x_trial);
254 gsl_vector_free (f_trial);
255 gsl_vector_free (df);
256 gsl_vector_free (qtdf);
258 GSL_ERROR ("failed to allocate space for rdx", GSL_ENOMEM);
263 w = gsl_vector_calloc (n);
269 gsl_vector_free (tau);
270 gsl_vector_free (diag);
271 gsl_vector_free (qtf);
272 gsl_vector_free (newton);
273 gsl_vector_free (gradient);
274 gsl_vector_free (x_trial);
275 gsl_vector_free (f_trial);
276 gsl_vector_free (df);
277 gsl_vector_free (qtdf);
278 gsl_vector_free (rdx);
280 GSL_ERROR ("failed to allocate space for w", GSL_ENOMEM);
285 v = gsl_vector_calloc (n);
291 gsl_vector_free (tau);
292 gsl_vector_free (diag);
293 gsl_vector_free (qtf);
294 gsl_vector_free (newton);
295 gsl_vector_free (gradient);
296 gsl_vector_free (x_trial);
297 gsl_vector_free (f_trial);
298 gsl_vector_free (df);
299 gsl_vector_free (qtdf);
300 gsl_vector_free (rdx);
303 GSL_ERROR ("failed to allocate space for v", GSL_ENOMEM);
312 hybridj_set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
314 int status = set (vstate, fdf, x, f, J, dx, 0);
319 hybridsj_set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
321 int status = set (vstate, fdf, x, f, J, dx, 1);
326 set (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale)
328 hybridj_state_t *state = (hybridj_state_t *) vstate;
330 gsl_matrix *q = state->q;
331 gsl_matrix *r = state->r;
332 gsl_vector *tau = state->tau;
333 gsl_vector *diag = state->diag;
335 GSL_MULTIROOT_FN_EVAL_F_DF (fdf, x, f, J);
338 state->fnorm = enorm (f);
344 gsl_vector_set_all (dx, 0.0);
346 /* Store column norms in diag */
349 compute_diag (J, diag);
351 gsl_vector_set_all (diag, 1.0);
353 /* Set delta to factor |D x| or to factor if |D x| is zero */
355 state->delta = compute_delta (diag, x);
357 /* Factorize J into QR decomposition */
359 gsl_linalg_QR_decomp (J, tau);
360 gsl_linalg_QR_unpack (J, tau, q, r);
366 hybridj_iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
368 int status = iterate (vstate, fdf, x, f, J, dx, 0);
373 hybridsj_iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
375 int status = iterate (vstate, fdf, x, f, J, dx, 1);
380 iterate (void *vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx, int scale)
382 hybridj_state_t *state = (hybridj_state_t *) vstate;
384 const double fnorm = state->fnorm;
386 gsl_matrix *q = state->q;
387 gsl_matrix *r = state->r;
388 gsl_vector *tau = state->tau;
389 gsl_vector *diag = state->diag;
390 gsl_vector *qtf = state->qtf;
391 gsl_vector *x_trial = state->x_trial;
392 gsl_vector *f_trial = state->f_trial;
393 gsl_vector *df = state->df;
394 gsl_vector *qtdf = state->qtdf;
395 gsl_vector *rdx = state->rdx;
396 gsl_vector *w = state->w;
397 gsl_vector *v = state->v;
399 double prered, actred;
400 double pnorm, fnorm1, fnorm1p;
402 double p1 = 0.1, p5 = 0.5, p001 = 0.001, p0001 = 0.0001;
404 /* Compute qtf = Q^T f */
406 compute_qtf (q, f, qtf);
408 /* Compute dogleg step */
410 dogleg (r, qtf, diag, state->delta, state->newton, state->gradient, dx);
412 /* Take a trial step */
414 compute_trial_step (x, dx, state->x_trial);
416 pnorm = scaled_enorm (diag, dx);
418 if (state->iter == 1)
420 if (pnorm < state->delta)
422 state->delta = pnorm;
426 /* Evaluate function at x + p */
429 int status = GSL_MULTIROOT_FN_EVAL_F (fdf, x_trial, f_trial);
431 if (status != GSL_SUCCESS)
437 /* Set df = f_trial - f */
439 compute_df (f_trial, f, df);
441 /* Compute the scaled actual reduction */
443 fnorm1 = enorm (f_trial);
445 actred = compute_actual_reduction (fnorm, fnorm1);
447 /* Compute rdx = R dx */
449 compute_rdx (r, dx, rdx);
451 /* Compute the scaled predicted reduction phi1p = |Q^T f + R dx| */
453 fnorm1p = enorm_sum (qtf, rdx);
455 prered = compute_predicted_reduction (fnorm, fnorm1p);
457 /* Compute the ratio of the actual to predicted reduction */
461 ratio = actred / prered;
468 /* Update the step bound */
481 if (ratio >= p5 || state->ncsuc > 1)
482 state->delta = GSL_MAX (state->delta, pnorm / p5);
483 if (fabs (ratio - 1) <= p1)
484 state->delta = pnorm / p5;
487 /* Test for successful iteration */
491 gsl_vector_memcpy (x, x_trial);
492 gsl_vector_memcpy (f, f_trial);
493 state->fnorm = fnorm1;
497 /* Determine the progress of the iteration */
506 if (state->ncfail == 2)
509 int status = GSL_MULTIROOT_FN_EVAL_DF (fdf, x, J);
511 if (status != GSL_SUCCESS)
519 if (state->iter == 1)
522 compute_diag (J, diag);
523 state->delta = compute_delta (diag, x);
528 update_diag (J, diag);
531 /* Factorize J into QR decomposition */
533 gsl_linalg_QR_decomp (J, tau);
534 gsl_linalg_QR_unpack (J, tau, q, r);
538 /* Compute qtdf = Q^T df, w = (Q^T df - R dx)/|dx|, v = D^2 dx/|dx| */
540 compute_qtf (q, df, qtdf);
542 compute_wv (qtdf, rdx, dx, diag, pnorm, w, v);
544 /* Rank-1 update of the jacobian Q'R' = Q(R + w v^T) */
546 gsl_linalg_QR_update (q, r, w, v);
548 /* No progress as measured by jacobian evaluations */
550 if (state->nslow2 == 5)
555 /* No progress as measured by function evaluations */
557 if (state->nslow1 == 10)
567 hybridj_free (void *vstate)
569 hybridj_state_t *state = (hybridj_state_t *) vstate;
571 gsl_vector_free (state->v);
572 gsl_vector_free (state->w);
573 gsl_vector_free (state->rdx);
574 gsl_vector_free (state->qtdf);
575 gsl_vector_free (state->df);
576 gsl_vector_free (state->f_trial);
577 gsl_vector_free (state->x_trial);
578 gsl_vector_free (state->gradient);
579 gsl_vector_free (state->newton);
580 gsl_vector_free (state->qtf);
581 gsl_vector_free (state->diag);
582 gsl_vector_free (state->tau);
583 gsl_matrix_free (state->r);
584 gsl_matrix_free (state->q);
587 static const gsl_multiroot_fdfsolver_type hybridj_type =
589 "hybridj", /* name */
590 sizeof (hybridj_state_t),
597 static const gsl_multiroot_fdfsolver_type hybridsj_type =
599 "hybridsj", /* name */
600 sizeof (hybridj_state_t),
607 const gsl_multiroot_fdfsolver_type *gsl_multiroot_fdfsolver_hybridj = &hybridj_type;
608 const gsl_multiroot_fdfsolver_type *gsl_multiroot_fdfsolver_hybridsj = &hybridsj_type;