Debian Codegraph @

external/gsl/gsl_multiroots__newton.c

raw
1 /* multiroots/newton.c
2 *
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include "gsl__config.h"
21
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <math.h>
26 #include <float.h>
27
28 #include "gsl_math.h"
29 #include "gsl_errno.h"
30 #include "gsl_multiroots.h"
31 #include "gsl_linalg.h"
32
33 typedef struct
34 {
35 gsl_matrix * lu;
36 gsl_permutation * permutation;
37 }
38 newton_state_t;
39
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);
44
45 static int
46 newton_alloc (void * vstate, size_t n)
47 {
48 newton_state_t * state = (newton_state_t *) vstate;
49 gsl_permutation * p;
50 gsl_matrix * m;
51
52 m = gsl_matrix_calloc (n,n);
53
54 if (m == 0)
55 {
56 GSL_ERROR ("failed to allocate space for lu", GSL_ENOMEM);
57 }
58
59 state->lu = m ;
60
61 p = gsl_permutation_calloc (n);
62
63 if (p == 0)
64 {
65 gsl_matrix_free(m);
66
67 GSL_ERROR ("failed to allocate space for permutation", GSL_ENOMEM);
68 }
69
70 state->permutation = p ;
71
72 return GSL_SUCCESS;
73 }
74
75 static int
76 newton_set (void * vstate, gsl_multiroot_function_fdf * FDF, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
77 {
78 size_t i, n = FDF->n ;
79
80 GSL_MULTIROOT_FN_EVAL_F_DF (FDF, x, f, J);
81
82 for (i = 0; i < n; i++)
83 {
84 gsl_vector_set (dx, i, 0.0);
85 }
86
87 return GSL_SUCCESS;
88 }
89
90 static int
91 newton_iterate (void * vstate, gsl_multiroot_function_fdf * fdf, gsl_vector * x, gsl_vector * f, gsl_matrix * J, gsl_vector * dx)
92 {
93 newton_state_t * state = (newton_state_t *) vstate;
94
95 int signum;
96
97 size_t i;
98
99 size_t n = fdf->n ;
100
101 gsl_matrix_memcpy (state->lu, J);
102
103 gsl_linalg_LU_decomp (state->lu, state->permutation, &signum);
104
105 gsl_linalg_LU_solve (state->lu, state->permutation, f, dx);
106
107 for (i = 0; i < n; i++)
108 {
109 double e = gsl_vector_get (dx, i);
110 double y = gsl_vector_get (x, i);
111 gsl_vector_set (dx, i, -e);
112 gsl_vector_set (x, i, y - e);
113 }
114
115 {
116 int status = GSL_MULTIROOT_FN_EVAL_F_DF (fdf, x, f, J);
117
118 if (status != GSL_SUCCESS)
119 {
120 return GSL_EBADFUNC;
121 }
122 }
123
124 return GSL_SUCCESS;
125 }
126
127
128 static void
129 newton_free (void * vstate)
130 {
131 newton_state_t * state = (newton_state_t *) vstate;
132
133 gsl_matrix_free(state->lu);
134
135 gsl_permutation_free(state->permutation);
136 }
137
138
139 static const gsl_multiroot_fdfsolver_type newton_type =
140 {"newton", /* name */
141 sizeof (newton_state_t),
142 &newton_alloc,
143 &newton_set,
144 &newton_iterate,
145 &newton_free};
146
147 const gsl_multiroot_fdfsolver_type * gsl_multiroot_fdfsolver_newton = &newton_type;