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>
62 static int hybrid_alloc (void *vstate, size_t n);
63 static int hybrid_set (void *vstate, gsl_multiroot_function * func,
64 gsl_vector * x, gsl_vector * f, gsl_vector * dx);
65 static int hybrids_set (void *vstate, gsl_multiroot_function * func,
66 gsl_vector * x, gsl_vector * f, gsl_vector * dx);
67 static int set (void *vstate, gsl_multiroot_function * func, gsl_vector * x,
68 gsl_vector * f, gsl_vector * dx, int scale);
69 static int hybrid_iterate (void *vstate, gsl_multiroot_function * func,
70 gsl_vector * x, gsl_vector * f, gsl_vector * dx);
71 static void hybrid_free (void *vstate);
72 static int iterate (void *vstate, gsl_multiroot_function * func,
73 gsl_vector * x, gsl_vector * f, gsl_vector * dx,
77 hybrid_alloc (void *vstate, size_t n)
79 hybrid_state_t *state = (hybrid_state_t *) vstate;
80 gsl_matrix *J, *q, *r;
81 gsl_vector *tau, *diag, *qtf, *newton, *gradient, *x_trial, *f_trial,
82 *df, *qtdf, *rdx, *w, *v;
84 J = gsl_matrix_calloc (n, n);
88 GSL_ERROR ("failed to allocate space for J", GSL_ENOMEM);
93 q = gsl_matrix_calloc (n, n);
99 GSL_ERROR ("failed to allocate space for q", GSL_ENOMEM);
104 r = gsl_matrix_calloc (n, n);
111 GSL_ERROR ("failed to allocate space for r", GSL_ENOMEM);
116 tau = gsl_vector_calloc (n);
124 GSL_ERROR ("failed to allocate space for tau", GSL_ENOMEM);
129 diag = gsl_vector_calloc (n);
136 gsl_vector_free (tau);
138 GSL_ERROR ("failed to allocate space for diag", GSL_ENOMEM);
143 qtf = gsl_vector_calloc (n);
150 gsl_vector_free (tau);
151 gsl_vector_free (diag);
153 GSL_ERROR ("failed to allocate space for qtf", GSL_ENOMEM);
158 newton = gsl_vector_calloc (n);
165 gsl_vector_free (tau);
166 gsl_vector_free (diag);
167 gsl_vector_free (qtf);
169 GSL_ERROR ("failed to allocate space for newton", GSL_ENOMEM);
172 state->newton = newton;
174 gradient = gsl_vector_calloc (n);
181 gsl_vector_free (tau);
182 gsl_vector_free (diag);
183 gsl_vector_free (qtf);
184 gsl_vector_free (newton);
186 GSL_ERROR ("failed to allocate space for gradient", GSL_ENOMEM);
189 state->gradient = gradient;
191 x_trial = gsl_vector_calloc (n);
198 gsl_vector_free (tau);
199 gsl_vector_free (diag);
200 gsl_vector_free (qtf);
201 gsl_vector_free (newton);
202 gsl_vector_free (gradient);
204 GSL_ERROR ("failed to allocate space for x_trial", GSL_ENOMEM);
207 state->x_trial = x_trial;
209 f_trial = gsl_vector_calloc (n);
216 gsl_vector_free (tau);
217 gsl_vector_free (diag);
218 gsl_vector_free (qtf);
219 gsl_vector_free (newton);
220 gsl_vector_free (gradient);
221 gsl_vector_free (x_trial);
223 GSL_ERROR ("failed to allocate space for f_trial", GSL_ENOMEM);
226 state->f_trial = f_trial;
228 df = gsl_vector_calloc (n);
235 gsl_vector_free (tau);
236 gsl_vector_free (diag);
237 gsl_vector_free (qtf);
238 gsl_vector_free (newton);
239 gsl_vector_free (gradient);
240 gsl_vector_free (x_trial);
241 gsl_vector_free (f_trial);
243 GSL_ERROR ("failed to allocate space for df", GSL_ENOMEM);
248 qtdf = gsl_vector_calloc (n);
255 gsl_vector_free (tau);
256 gsl_vector_free (diag);
257 gsl_vector_free (qtf);
258 gsl_vector_free (newton);
259 gsl_vector_free (gradient);
260 gsl_vector_free (x_trial);
261 gsl_vector_free (f_trial);
262 gsl_vector_free (df);
264 GSL_ERROR ("failed to allocate space for qtdf", GSL_ENOMEM);
270 rdx = gsl_vector_calloc (n);
277 gsl_vector_free (tau);
278 gsl_vector_free (diag);
279 gsl_vector_free (qtf);
280 gsl_vector_free (newton);
281 gsl_vector_free (gradient);
282 gsl_vector_free (x_trial);
283 gsl_vector_free (f_trial);
284 gsl_vector_free (df);
285 gsl_vector_free (qtdf);
287 GSL_ERROR ("failed to allocate space for rdx", GSL_ENOMEM);
292 w = gsl_vector_calloc (n);
299 gsl_vector_free (tau);
300 gsl_vector_free (diag);
301 gsl_vector_free (qtf);
302 gsl_vector_free (newton);
303 gsl_vector_free (gradient);
304 gsl_vector_free (x_trial);
305 gsl_vector_free (f_trial);
306 gsl_vector_free (df);
307 gsl_vector_free (qtdf);
308 gsl_vector_free (rdx);
310 GSL_ERROR ("failed to allocate space for w", GSL_ENOMEM);
315 v = gsl_vector_calloc (n);
322 gsl_vector_free (tau);
323 gsl_vector_free (diag);
324 gsl_vector_free (qtf);
325 gsl_vector_free (newton);
326 gsl_vector_free (gradient);
327 gsl_vector_free (x_trial);
328 gsl_vector_free (f_trial);
329 gsl_vector_free (df);
330 gsl_vector_free (qtdf);
331 gsl_vector_free (rdx);
334 GSL_ERROR ("failed to allocate space for v", GSL_ENOMEM);
343 hybrid_set (void *vstate, gsl_multiroot_function * func, gsl_vector * x,
344 gsl_vector * f, gsl_vector * dx)
346 int status = set (vstate, func, x, f, dx, 0);
351 hybrids_set (void *vstate, gsl_multiroot_function * func, gsl_vector * x,
352 gsl_vector * f, gsl_vector * dx)
354 int status = set (vstate, func, x, f, dx, 1);
359 set (void *vstate, gsl_multiroot_function * func, gsl_vector * x,
360 gsl_vector * f, gsl_vector * dx, int scale)
362 hybrid_state_t *state = (hybrid_state_t *) vstate;
364 gsl_matrix *J = state->J;
365 gsl_matrix *q = state->q;
366 gsl_matrix *r = state->r;
367 gsl_vector *tau = state->tau;
368 gsl_vector *diag = state->diag;
372 status = GSL_MULTIROOT_FN_EVAL (func, x, f);
379 status = gsl_multiroot_fdjacobian (func, x, f, GSL_SQRT_DBL_EPSILON, J);
387 state->fnorm = enorm (f);
393 gsl_vector_set_all (dx, 0.0);
395 /* Store column norms in diag */
398 compute_diag (J, diag);
400 gsl_vector_set_all (diag, 1.0);
402 /* Set delta to factor |D x| or to factor if |D x| is zero */
404 state->delta = compute_delta (diag, x);
406 /* Factorize J into QR decomposition */
408 status = gsl_linalg_QR_decomp (J, tau);
415 status = gsl_linalg_QR_unpack (J, tau, q, r);
421 hybrid_iterate (void *vstate, gsl_multiroot_function * func, gsl_vector * x,
422 gsl_vector * f, gsl_vector * dx)
424 int status = iterate (vstate, func, x, f, dx, 0);
429 hybrids_iterate (void *vstate, gsl_multiroot_function * func, gsl_vector * x,
430 gsl_vector * f, gsl_vector * dx)
432 int status = iterate (vstate, func, x, f, dx, 1);
437 iterate (void *vstate, gsl_multiroot_function * func, gsl_vector * x,
438 gsl_vector * f, gsl_vector * dx, int scale)
440 hybrid_state_t *state = (hybrid_state_t *) vstate;
442 const double fnorm = state->fnorm;
444 gsl_matrix *J = state->J;
445 gsl_matrix *q = state->q;
446 gsl_matrix *r = state->r;
447 gsl_vector *tau = state->tau;
448 gsl_vector *diag = state->diag;
449 gsl_vector *qtf = state->qtf;
450 gsl_vector *x_trial = state->x_trial;
451 gsl_vector *f_trial = state->f_trial;
452 gsl_vector *df = state->df;
453 gsl_vector *qtdf = state->qtdf;
454 gsl_vector *rdx = state->rdx;
455 gsl_vector *w = state->w;
456 gsl_vector *v = state->v;
458 double prered, actred;
459 double pnorm, fnorm1, fnorm1p;
461 double p1 = 0.1, p5 = 0.5, p001 = 0.001, p0001 = 0.0001;
463 /* Compute qtf = Q^T f */
465 compute_qtf (q, f, qtf);
467 /* Compute dogleg step */
469 dogleg (r, qtf, diag, state->delta, state->newton, state->gradient, dx);
471 /* Take a trial step */
473 compute_trial_step (x, dx, state->x_trial);
475 pnorm = scaled_enorm (diag, dx);
477 if (state->iter == 1)
479 if (pnorm < state->delta)
481 state->delta = pnorm;
485 /* Evaluate function at x + p */
488 int status = GSL_MULTIROOT_FN_EVAL (func, x_trial, f_trial);
490 if (status != GSL_SUCCESS)
496 /* Set df = f_trial - f */
498 compute_df (f_trial, f, df);
500 /* Compute the scaled actual reduction */
502 fnorm1 = enorm (f_trial);
504 actred = compute_actual_reduction (fnorm, fnorm1);
506 /* Compute rdx = R dx */
508 compute_rdx (r, dx, rdx);
510 /* Compute the scaled predicted reduction phi1p = |Q^T f + R dx| */
512 fnorm1p = enorm_sum (qtf, rdx);
514 prered = compute_predicted_reduction (fnorm, fnorm1p);
516 /* Compute the ratio of the actual to predicted reduction */
520 ratio = actred / prered;
527 /* Update the step bound */
540 if (ratio >= p5 || state->ncsuc > 1)
541 state->delta = GSL_MAX (state->delta, pnorm / p5);
542 if (fabs (ratio - 1) <= p1)
543 state->delta = pnorm / p5;
546 /* Test for successful iteration */
550 gsl_vector_memcpy (x, x_trial);
551 gsl_vector_memcpy (f, f_trial);
552 state->fnorm = fnorm1;
556 /* Determine the progress of the iteration */
565 if (state->ncfail == 2)
567 gsl_multiroot_fdjacobian (func, x, f, GSL_SQRT_DBL_EPSILON, J);
571 if (state->iter == 1)
574 compute_diag (J, diag);
575 state->delta = compute_delta (diag, x);
580 update_diag (J, diag);
583 /* Factorize J into QR decomposition */
585 gsl_linalg_QR_decomp (J, tau);
586 gsl_linalg_QR_unpack (J, tau, q, r);
591 /* Compute qtdf = Q^T df, w = (Q^T df - R dx)/|dx|, v = D^2 dx/|dx| */
593 compute_qtf (q, df, qtdf);
595 compute_wv (qtdf, rdx, dx, diag, pnorm, w, v);
597 /* Rank-1 update of the jacobian Q'R' = Q(R + w v^T) */
599 gsl_linalg_QR_update (q, r, w, v);
601 /* No progress as measured by jacobian evaluations */
603 if (state->nslow2 == 5)
608 /* No progress as measured by function evaluations */
610 if (state->nslow1 == 10)
620 hybrid_free (void *vstate)
622 hybrid_state_t *state = (hybrid_state_t *) vstate;
624 gsl_vector_free (state->v);
625 gsl_vector_free (state->w);
626 gsl_vector_free (state->rdx);
627 gsl_vector_free (state->qtdf);
628 gsl_vector_free (state->df);
629 gsl_vector_free (state->f_trial);
630 gsl_vector_free (state->x_trial);
631 gsl_vector_free (state->gradient);
632 gsl_vector_free (state->newton);
633 gsl_vector_free (state->qtf);
634 gsl_vector_free (state->diag);
635 gsl_vector_free (state->tau);
636 gsl_matrix_free (state->r);
637 gsl_matrix_free (state->q);
638 gsl_matrix_free (state->J);
641 static const gsl_multiroot_fsolver_type hybrid_type = {
643 sizeof (hybrid_state_t),
650 static const gsl_multiroot_fsolver_type hybrids_type = {
651 "hybrids", /* name */
652 sizeof (hybrid_state_t),
659 const gsl_multiroot_fsolver_type *gsl_multiroot_fsolver_hybrid = &hybrid_type;
660 const gsl_multiroot_fsolver_type *gsl_multiroot_fsolver_hybrids =