1 /* multiroots/dnewton.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>
33 /* Newton method using a finite difference approximation to the jacobian.
34 The derivatives are estimated using a step size of
36 h_i = sqrt(DBL_EPSILON) * x_i
44 gsl_permutation * permutation;
48 static int dnewton_alloc (void * vstate, size_t n);
49 static int dnewton_set (void * vstate, gsl_multiroot_function * function, gsl_vector * x, gsl_vector * f, gsl_vector * dx);
50 static int dnewton_iterate (void * vstate, gsl_multiroot_function * function, gsl_vector * x, gsl_vector * f, gsl_vector * dx);
51 static void dnewton_free (void * vstate);
54 dnewton_alloc (void * vstate, size_t n)
56 dnewton_state_t * state = (dnewton_state_t *) vstate;
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 J = gsl_matrix_calloc (n,n);
84 gsl_permutation_free(p);
87 GSL_ERROR ("failed to allocate space for d", GSL_ENOMEM);
96 dnewton_set (void * vstate, gsl_multiroot_function * function, gsl_vector * x, gsl_vector * f, gsl_vector * dx)
98 dnewton_state_t * state = (dnewton_state_t *) vstate;
99 size_t i, n = function->n ;
102 status = GSL_MULTIROOT_FN_EVAL (function, x, f);
106 status = gsl_multiroot_fdjacobian (function, x, f, GSL_SQRT_DBL_EPSILON,
111 for (i = 0; i < n; i++)
113 gsl_vector_set (dx, i, 0.0);
120 dnewton_iterate (void * vstate, gsl_multiroot_function * function, gsl_vector * x, gsl_vector * f, gsl_vector * dx)
122 dnewton_state_t * state = (dnewton_state_t *) vstate;
128 size_t n = function->n ;
130 gsl_matrix_memcpy (state->lu, state->J);
133 int status = gsl_linalg_LU_decomp (state->lu, state->permutation, &signum);
139 int status = gsl_linalg_LU_solve (state->lu, state->permutation, f, dx);
144 for (i = 0; i < n; i++)
146 double e = gsl_vector_get (dx, i);
147 double y = gsl_vector_get (x, i);
148 gsl_vector_set (dx, i, -e);
149 gsl_vector_set (x, i, y - e);
153 int status = GSL_MULTIROOT_FN_EVAL (function, x, f);
155 if (status != GSL_SUCCESS)
161 gsl_multiroot_fdjacobian (function, x, f, GSL_SQRT_DBL_EPSILON, state->J);
168 dnewton_free (void * vstate)
170 dnewton_state_t * state = (dnewton_state_t *) vstate;
172 gsl_matrix_free(state->J);
173 gsl_matrix_free(state->lu);
174 gsl_permutation_free(state->permutation);
178 static const gsl_multiroot_fsolver_type dnewton_type =
179 {"dnewton", /* name */
180 sizeof (dnewton_state_t),
186 const gsl_multiroot_fsolver_type * gsl_multiroot_fsolver_dnewton = &dnewton_type;