1 /* multiroots/fsolver.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.
23 #include <gsl/gsl_errno.h>
24 #include <gsl/gsl_multiroots.h>
26 gsl_multiroot_fsolver *
27 gsl_multiroot_fsolver_alloc (const gsl_multiroot_fsolver_type * T,
32 gsl_multiroot_fsolver * s;
34 s = (gsl_multiroot_fsolver *) malloc (sizeof (gsl_multiroot_fsolver));
38 GSL_ERROR_VAL ("failed to allocate space for multiroot solver struct",
42 s->x = gsl_vector_calloc (n);
47 GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
50 s->f = gsl_vector_calloc (n);
54 gsl_vector_free (s->x);
56 GSL_ERROR_VAL ("failed to allocate space for f", GSL_ENOMEM, 0);
59 s->dx = gsl_vector_calloc (n);
63 gsl_vector_free (s->x);
64 gsl_vector_free (s->f);
66 GSL_ERROR_VAL ("failed to allocate space for dx", GSL_ENOMEM, 0);
69 s->state = malloc (T->size);
73 gsl_vector_free (s->dx);
74 gsl_vector_free (s->x);
75 gsl_vector_free (s->f);
76 free (s); /* exception in constructor, avoid memory leak */
78 GSL_ERROR_VAL ("failed to allocate space for multiroot solver state",
84 status = (s->type->alloc)(s->state, n);
86 if (status != GSL_SUCCESS)
88 (s->type->free)(s->state);
90 gsl_vector_free (s->dx);
91 gsl_vector_free (s->x);
92 gsl_vector_free (s->f);
93 free (s); /* exception in constructor, avoid memory leak */
95 GSL_ERROR_VAL ("failed to set solver", status, 0);
104 gsl_multiroot_fsolver_set (gsl_multiroot_fsolver * s,
105 gsl_multiroot_function * f,
106 const gsl_vector * x)
108 if (s->x->size != f->n)
110 GSL_ERROR ("function incompatible with solver size", GSL_EBADLEN);
115 GSL_ERROR ("vector length not compatible with function", GSL_EBADLEN);
119 gsl_vector_memcpy(s->x,x);
121 return (s->type->set) (s->state, s->function, s->x, s->f, s->dx);
125 gsl_multiroot_fsolver_iterate (gsl_multiroot_fsolver * s)
127 return (s->type->iterate) (s->state, s->function, s->x, s->f, s->dx);
131 gsl_multiroot_fsolver_free (gsl_multiroot_fsolver * s)
133 (s->type->free) (s->state);
135 gsl_vector_free (s->dx);
136 gsl_vector_free (s->x);
137 gsl_vector_free (s->f);
142 gsl_multiroot_fsolver_name (const gsl_multiroot_fsolver * s)
144 return s->type->name;
148 gsl_multiroot_fsolver_root (const gsl_multiroot_fsolver * s)
154 gsl_multiroot_fsolver_dx (const gsl_multiroot_fsolver * s)
160 gsl_multiroot_fsolver_f (const gsl_multiroot_fsolver * s)