| 1 | /* multimin/fminimizer.c |
| 2 | * |
| 3 | * Copyright (C) 2002 Tuomo Keskitalo, Ivo Alxneit |
| 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 "gsl_errno.h" |
| 22 | #include "gsl_multimin.h" |
| 23 | |
| 24 | gsl_multimin_fminimizer * |
| 25 | gsl_multimin_fminimizer_alloc (const gsl_multimin_fminimizer_type * T, |
| 26 | size_t n) |
| 27 | { |
| 28 | int status; |
| 29 | |
| 30 | gsl_multimin_fminimizer *s = |
| 31 | (gsl_multimin_fminimizer *) malloc (sizeof (gsl_multimin_fminimizer)); |
| 32 | |
| 33 | if (s == 0) |
| 34 | { |
| 35 | GSL_ERROR_VAL ("failed to allocate space for minimizer struct", |
| 36 | GSL_ENOMEM, 0); |
| 37 | } |
| 38 | |
| 39 | s->type = T; |
| 40 | |
| 41 | s->x = gsl_vector_calloc (n); |
| 42 | |
| 43 | if (s->x == 0) |
| 44 | { |
| 45 | free (s); |
| 46 | GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0); |
| 47 | } |
| 48 | |
| 49 | s->state = malloc (T->size); |
| 50 | |
| 51 | if (s->state == 0) |
| 52 | { |
| 53 | gsl_vector_free (s->x); |
| 54 | free (s); |
| 55 | GSL_ERROR_VAL ("failed to allocate space for minimizer state", |
| 56 | GSL_ENOMEM, 0); |
| 57 | } |
| 58 | |
| 59 | status = (T->alloc) (s->state, n); |
| 60 | |
| 61 | if (status != GSL_SUCCESS) |
| 62 | { |
| 63 | free (s->state); |
| 64 | gsl_vector_free (s->x); |
| 65 | free (s); |
| 66 | |
| 67 | GSL_ERROR_VAL ("failed to initialize minimizer state", GSL_ENOMEM, 0); |
| 68 | } |
| 69 | |
| 70 | return s; |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | gsl_multimin_fminimizer_set (gsl_multimin_fminimizer * s, |
| 75 | gsl_multimin_function * f, |
| 76 | const gsl_vector * x, |
| 77 | const gsl_vector * step_size) |
| 78 | { |
| 79 | if (s->x->size != f->n) |
| 80 | { |
| 81 | GSL_ERROR ("function incompatible with solver size", GSL_EBADLEN); |
| 82 | } |
| 83 | |
| 84 | if (x->size != f->n || step_size->size != f->n) |
| 85 | { |
| 86 | GSL_ERROR ("vector length not compatible with function", GSL_EBADLEN); |
| 87 | } |
| 88 | |
| 89 | s->f = f; |
| 90 | |
| 91 | gsl_vector_memcpy (s->x,x); |
| 92 | |
| 93 | return (s->type->set) (s->state, s->f, s->x, &(s->size), step_size); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | gsl_multimin_fminimizer_free (gsl_multimin_fminimizer * s) |
| 98 | { |
| 99 | (s->type->free) (s->state); |
| 100 | free (s->state); |
| 101 | gsl_vector_free (s->x); |
| 102 | free (s); |
| 103 | } |
| 104 | |
| 105 | int |
| 106 | gsl_multimin_fminimizer_iterate (gsl_multimin_fminimizer * s) |
| 107 | { |
| 108 | return (s->type->iterate) (s->state, s->f, s->x, &(s->size), &(s->fval)); |
| 109 | } |
| 110 | |
| 111 | const char * |
| 112 | gsl_multimin_fminimizer_name (const gsl_multimin_fminimizer * s) |
| 113 | { |
| 114 | return s->type->name; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | gsl_vector * |
| 119 | gsl_multimin_fminimizer_x (const gsl_multimin_fminimizer * s) |
| 120 | { |
| 121 | return s->x; |
| 122 | } |
| 123 | |
| 124 | double |
| 125 | gsl_multimin_fminimizer_minimum (const gsl_multimin_fminimizer * s) |
| 126 | { |
| 127 | return s->fval; |
| 128 | } |
| 129 | |
| 130 | double |
| 131 | gsl_multimin_fminimizer_size (const gsl_multimin_fminimizer * s) |
| 132 | { |
| 133 | return s->size; |
| 134 | } |