1 /* multiroots/gnewton.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>
35 /* Simple globally convergent Newton method (rejects uphill steps) */
43 gsl_permutation * permutation;
47 static int gnewton_alloc (void * vstate, size_t n);
48 static int gnewton_set (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
49 static int gnewton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
50 static void gnewton_free (void * vstate);
53 gnewton_alloc (void * vstate, size_t n)
55 gnewton_state_t * state = (gnewton_state_t *) vstate;
56 gsl_vector * d, * x_trial ;
60 m = gsl_matrix_calloc (n,n);
64 GSL_ERROR ("failed to allocate space for lu", GSL_ENOMEM);
69 p = gsl_permutation_calloc (n);
75 GSL_ERROR ("failed to allocate space for permutation", GSL_ENOMEM);
78 state->permutation = p ;
80 d = gsl_vector_calloc (n);
84 gsl_permutation_free(p);
87 GSL_ERROR ("failed to allocate space for d", GSL_ENOMEM);
92 x_trial = gsl_vector_calloc (n);
97 gsl_permutation_free(p);
100 GSL_ERROR ("failed to allocate space for x_trial", GSL_ENOMEM);
103 state->x_trial = x_trial;
110 gnewton_set (void * vstate, gsl_multiroot_function_fdf * FDF, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
112 gnewton_state_t * state = (gnewton_state_t *) vstate;
113 size_t i, n = FDF->n ;
115 GSL_MULTIROOT_FN_EVAL_F_DF (FDF, x, f, J);
117 for (i = 0; i < n; i++)
119 gsl_vector_set (dx, i, 0.0);
122 state->phi = enorm(f);
128 gnewton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
130 gnewton_state_t * state = (gnewton_state_t *) vstate;
133 double t, phi0, phi1;
139 gsl_matrix_memcpy (state->lu, J);
141 gsl_linalg_LU_decomp (state->lu, state->permutation, &signum);
143 gsl_linalg_LU_solve (state->lu, state->permutation, f, state->d);
151 for (i = 0; i < n; i++)
153 double di = gsl_vector_get (state->d, i);
154 double xi = gsl_vector_get (x, i);
155 gsl_vector_set (state->x_trial, i, xi - t*di);
159 int status = GSL_MULTIROOT_FN_EVAL_F (fdf, state->x_trial, f);
161 if (status != GSL_SUCCESS)
169 if (phi1 > phi0 && t > GSL_DBL_EPSILON)
171 /* full step goes uphill, take a reduced step instead */
173 double theta = phi1 / phi0;
174 double u = (sqrt(1.0 + 6.0 * theta) - 1.0) / (3.0 * theta);
181 /* copy x_trial into x */
183 gsl_vector_memcpy (x, state->x_trial);
185 for (i = 0; i < n; i++)
187 double di = gsl_vector_get (state->d, i);
188 gsl_vector_set (dx, i, -t*di);
192 int status = GSL_MULTIROOT_FN_EVAL_DF (fdf, x, J);
194 if (status != GSL_SUCCESS)
207 gnewton_free (void * vstate)
209 gnewton_state_t * state = (gnewton_state_t *) vstate;
211 gsl_vector_free(state->d);
212 gsl_vector_free(state->x_trial);
213 gsl_matrix_free(state->lu);
214 gsl_permutation_free(state->permutation);
218 static const gsl_multiroot_fdfsolver_type gnewton_type =
219 {"gnewton", /* name */
220 sizeof (gnewton_state_t),
226 const gsl_multiroot_fdfsolver_type * gsl_multiroot_fdfsolver_gnewton = &gnewton_type;