| 1 | /* multifit/fsolver.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 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include "gsl_errno.h" |
| 24 | #include "gsl_multifit_nlin.h" |
| 25 | |
| 26 | gsl_multifit_fsolver * |
| 27 | gsl_multifit_fsolver_alloc (const gsl_multifit_fsolver_type * T, |
| 28 | size_t n, size_t p) |
| 29 | { |
| 30 | int status; |
| 31 | |
| 32 | gsl_multifit_fsolver * s; |
| 33 | |
| 34 | if (n < p) |
| 35 | { |
| 36 | GSL_ERROR_VAL ("insufficient data points, n < p", GSL_EINVAL, 0); |
| 37 | } |
| 38 | |
| 39 | s = (gsl_multifit_fsolver *) malloc (sizeof (gsl_multifit_fsolver)); |
| 40 | |
| 41 | if (s == 0) |
| 42 | { |
| 43 | GSL_ERROR_VAL ("failed to allocate space for multifit solver struct", |
| 44 | GSL_ENOMEM, 0); |
| 45 | } |
| 46 | |
| 47 | s->x = gsl_vector_calloc (p); |
| 48 | |
| 49 | if (s->x == 0) |
| 50 | { |
| 51 | free (s); |
| 52 | GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0); |
| 53 | } |
| 54 | |
| 55 | s->f = gsl_vector_calloc (n); |
| 56 | |
| 57 | if (s->f == 0) |
| 58 | { |
| 59 | gsl_vector_free (s->x); |
| 60 | free (s); |
| 61 | GSL_ERROR_VAL ("failed to allocate space for f", GSL_ENOMEM, 0); |
| 62 | } |
| 63 | |
| 64 | s->dx = gsl_vector_calloc (p); |
| 65 | |
| 66 | if (s->dx == 0) |
| 67 | { |
| 68 | gsl_vector_free (s->x); |
| 69 | gsl_vector_free (s->f); |
| 70 | free (s); |
| 71 | GSL_ERROR_VAL ("failed to allocate space for dx", GSL_ENOMEM, 0); |
| 72 | } |
| 73 | |
| 74 | s->state = malloc (T->size); |
| 75 | |
| 76 | if (s->state == 0) |
| 77 | { |
| 78 | gsl_vector_free (s->dx); |
| 79 | gsl_vector_free (s->x); |
| 80 | gsl_vector_free (s->f); |
| 81 | free (s); /* exception in constructor, avoid memory leak */ |
| 82 | |
| 83 | GSL_ERROR_VAL ("failed to allocate space for multifit solver state", |
| 84 | GSL_ENOMEM, 0); |
| 85 | } |
| 86 | |
| 87 | s->type = T ; |
| 88 | |
| 89 | status = (s->type->alloc)(s->state, n, p); |
| 90 | |
| 91 | if (status != GSL_SUCCESS) |
| 92 | { |
| 93 | (s->type->free)(s->state); |
| 94 | free (s->state); |
| 95 | gsl_vector_free (s->dx); |
| 96 | gsl_vector_free (s->x); |
| 97 | gsl_vector_free (s->f); |
| 98 | free (s); /* exception in constructor, avoid memory leak */ |
| 99 | |
| 100 | GSL_ERROR_VAL ("failed to set solver", status, 0); |
| 101 | } |
| 102 | |
| 103 | s->function = NULL; |
| 104 | |
| 105 | return s; |
| 106 | } |
| 107 | |
| 108 | int |
| 109 | gsl_multifit_fsolver_set (gsl_multifit_fsolver * s, |
| 110 | gsl_multifit_function * f, |
| 111 | const gsl_vector * x) |
| 112 | { |
| 113 | if (s->f->size != f->n) |
| 114 | { |
| 115 | GSL_ERROR ("function size does not match solver", GSL_EBADLEN); |
| 116 | } |
| 117 | |
| 118 | if (s->x->size != x->size) |
| 119 | { |
| 120 | GSL_ERROR ("vector length does not match solver", GSL_EBADLEN); |
| 121 | } |
| 122 | |
| 123 | s->function = f; |
| 124 | gsl_vector_memcpy(s->x,x); |
| 125 | |
| 126 | return (s->type->set) (s->state, s->function, s->x, s->f, s->dx); |
| 127 | } |
| 128 | |
| 129 | int |
| 130 | gsl_multifit_fsolver_iterate (gsl_multifit_fsolver * s) |
| 131 | { |
| 132 | return (s->type->iterate) (s->state, s->function, s->x, s->f, s->dx); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | gsl_multifit_fsolver_free (gsl_multifit_fsolver * s) |
| 137 | { |
| 138 | (s->type->free) (s->state); |
| 139 | free (s->state); |
| 140 | gsl_vector_free (s->dx); |
| 141 | gsl_vector_free (s->x); |
| 142 | gsl_vector_free (s->f); |
| 143 | free (s); |
| 144 | } |
| 145 | |
| 146 | const char * |
| 147 | gsl_multifit_fsolver_name (const gsl_multifit_fsolver * s) |
| 148 | { |
| 149 | return s->type->name; |
| 150 | } |
| 151 | |
| 152 | gsl_vector * |
| 153 | gsl_multifit_fsolver_position (const gsl_multifit_fsolver * s) |
| 154 | { |
| 155 | return s->x; |
| 156 | } |