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>
36 gsl_permutation * permutation;
40 static int newton_alloc (void * vstate, size_t n);
41 static int newton_set (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
42 static int newton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx);
43 static void newton_free (void * vstate);
46 newton_alloc (void * vstate, size_t n)
48 newton_state_t * state = (newton_state_t *) vstate;
52 m = gsl_matrix_calloc (n,n);
56 GSL_ERROR ("failed to allocate space for lu", GSL_ENOMEM);
61 p = gsl_permutation_calloc (n);
67 GSL_ERROR ("failed to allocate space for permutation", GSL_ENOMEM);
70 state->permutation = p ;
76 newton_set (void * vstate, gsl_multiroot_function_fdf * FDF, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
78 newton_state_t * state = (newton_state_t *) vstate;
80 size_t i, n = FDF->n ;
82 state = 0 ; /* avoid warnings about unused parameters */
84 GSL_MULTIROOT_FN_EVAL_F_DF (FDF, x, f, J);
86 for (i = 0; i < n; i++)
88 gsl_vector_set (dx, i, 0.0);
95 newton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
97 newton_state_t * state = (newton_state_t *) vstate;
105 gsl_matrix_memcpy (state->lu, J);
107 gsl_linalg_LU_decomp (state->lu, state->permutation, &signum);
109 gsl_linalg_LU_solve (state->lu, state->permutation, f, dx);
111 for (i = 0; i < n; i++)
113 double e = gsl_vector_get (dx, i);
114 double y = gsl_vector_get (x, i);
115 gsl_vector_set (dx, i, -e);
116 gsl_vector_set (x, i, y - e);
120 int status = GSL_MULTIROOT_FN_EVAL_F_DF (fdf, x, f, J);
122 if (status != GSL_SUCCESS)
133 newton_free (void * vstate)
135 newton_state_t * state = (newton_state_t *) vstate;
137 gsl_matrix_free(state->lu);
139 gsl_permutation_free(state->permutation);
143 static const gsl_multiroot_fdfsolver_type newton_type =
144 {"newton", /* name */
145 sizeof (newton_state_t),
151 const gsl_multiroot_fdfsolver_type * gsl_multiroot_fdfsolver_newton = &newton_type;