| 1 |
/* min/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_min.h" |
| 25 |
|
| 26 |
#include "gsl_min__min.h" |
| 27 |
|
| 28 |
static int |
| 29 |
compute_f_values (gsl_function * f, double x_minimum, double * f_minimum, |
| 30 |
double x_lower, double * f_lower, |
| 31 |
double x_upper, double * f_upper); |
| 32 |
|
| 33 |
|
| 34 |
static int |
| 35 |
compute_f_values (gsl_function * f, double x_minimum, double * f_minimum, |
| 36 |
double x_lower, double * f_lower, |
| 37 |
double x_upper, double * f_upper) |
| 38 |
{ |
| 39 |
SAFE_FUNC_CALL(f, x_lower, f_lower); |
| 40 |
SAFE_FUNC_CALL(f, x_upper, f_upper); |
| 41 |
SAFE_FUNC_CALL(f, x_minimum, f_minimum); |
| 42 |
|
| 43 |
return GSL_SUCCESS; |
| 44 |
} |
| 45 |
|
| 46 |
int |
| 47 |
gsl_min_fminimizer_set (gsl_min_fminimizer * s, |
| 48 |
gsl_function * f, |
| 49 |
double x_minimum, double x_lower, double x_upper) |
| 50 |
{ |
| 51 |
int status ; |
| 52 |
|
| 53 |
double f_minimum, f_lower, f_upper; |
| 54 |
|
| 55 |
status = compute_f_values (f, x_minimum, &f_minimum, |
| 56 |
x_lower, &f_lower, |
| 57 |
x_upper, &f_upper); |
| 58 |
|
| 59 |
if (status != GSL_SUCCESS) |
| 60 |
{ |
| 61 |
return status ; |
| 62 |
} |
| 63 |
|
| 64 |
status = gsl_min_fminimizer_set_with_values (s, f, x_minimum, f_minimum, |
| 65 |
x_lower, f_lower, |
| 66 |
x_upper, f_upper); |
| 67 |
return status; |
| 68 |
} |
| 69 |
|
| 70 |
gsl_min_fminimizer * |
| 71 |
gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T) |
| 72 |
{ |
| 73 |
gsl_min_fminimizer * s = |
| 74 |
(gsl_min_fminimizer *) malloc (sizeof (gsl_min_fminimizer)); |
| 75 |
|
| 76 |
if (s == 0) |
| 77 |
{ |
| 78 |
GSL_ERROR_VAL ("failed to allocate space for minimizer struct", |
| 79 |
GSL_ENOMEM, 0); |
| 80 |
}; |
| 81 |
|
| 82 |
s->state = malloc (T->size); |
| 83 |
|
| 84 |
if (s->state == 0) |
| 85 |
{ |
| 86 |
free (s); /* exception in constructor, avoid memory leak */ |
| 87 |
|
| 88 |
GSL_ERROR_VAL ("failed to allocate space for minimizer state", |
| 89 |
GSL_ENOMEM, 0); |
| 90 |
}; |
| 91 |
|
| 92 |
s->type = T ; |
| 93 |
s->function = NULL; |
| 94 |
|
| 95 |
return s; |
| 96 |
} |
| 97 |
|
| 98 |
int |
| 99 |
gsl_min_fminimizer_set_with_values (gsl_min_fminimizer * s, gsl_function * f, |
| 100 |
double x_minimum, double f_minimum, |
| 101 |
double x_lower, double f_lower, |
| 102 |
double x_upper, double f_upper) |
| 103 |
{ |
| 104 |
s->function = f; |
| 105 |
s->x_minimum = x_minimum; |
| 106 |
s->x_lower = x_lower; |
| 107 |
s->x_upper = x_upper; |
| 108 |
|
| 109 |
if (x_lower > x_upper) |
| 110 |
{ |
| 111 |
GSL_ERROR ("invalid interval (lower > upper)", GSL_EINVAL); |
| 112 |
} |
| 113 |
|
| 114 |
if (x_minimum >= x_upper || x_minimum <= x_lower) |
| 115 |
{ |
| 116 |
GSL_ERROR ("x_minimum must lie inside interval (lower < x < upper)", |
| 117 |
GSL_EINVAL); |
| 118 |
} |
| 119 |
|
| 120 |
s->f_lower = f_lower; |
| 121 |
s->f_upper = f_upper; |
| 122 |
s->f_minimum = f_minimum; |
| 123 |
|
| 124 |
if (f_minimum >= f_lower || f_minimum >= f_upper) |
| 125 |
{ |
| 126 |
GSL_ERROR ("endpoints do not enclose a minimum", GSL_EINVAL); |
| 127 |
} |
| 128 |
|
| 129 |
return (s->type->set) (s->state, s->function, |
| 130 |
x_minimum, f_minimum, |
| 131 |
x_lower, f_lower, |
| 132 |
x_upper, f_upper); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
int |
| 137 |
gsl_min_fminimizer_iterate (gsl_min_fminimizer * s) |
| 138 |
{ |
| 139 |
return (s->type->iterate) (s->state, s->function, |
| 140 |
&(s->x_minimum), &(s->f_minimum), |
| 141 |
&(s->x_lower), &(s->f_lower), |
| 142 |
&(s->x_upper), &(s->f_upper)); |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
gsl_min_fminimizer_free (gsl_min_fminimizer * s) |
| 147 |
{ |
| 148 |
free (s->state); |
| 149 |
free (s); |
| 150 |
} |
| 151 |
|
| 152 |
const char * |
| 153 |
gsl_min_fminimizer_name (const gsl_min_fminimizer * s) |
| 154 |
{ |
| 155 |
return s->type->name; |
| 156 |
} |
| 157 |
|
| 158 |
/* Deprecated, use x_minimum instead */ |
| 159 |
double |
| 160 |
gsl_min_fminimizer_minimum (const gsl_min_fminimizer * s) |
| 161 |
{ |
| 162 |
return s->x_minimum; |
| 163 |
} |
| 164 |
|
| 165 |
double |
| 166 |
gsl_min_fminimizer_x_minimum (const gsl_min_fminimizer * s) |
| 167 |
{ |
| 168 |
return s->x_minimum; |
| 169 |
} |
| 170 |
|
| 171 |
double |
| 172 |
gsl_min_fminimizer_x_lower (const gsl_min_fminimizer * s) |
| 173 |
{ |
| 174 |
return s->x_lower; |
| 175 |
} |
| 176 |
|
| 177 |
double |
| 178 |
gsl_min_fminimizer_x_upper (const gsl_min_fminimizer * s) |
| 179 |
{ |
| 180 |
return s->x_upper; |
| 181 |
} |
| 182 |
|
| 183 |
double |
| 184 |
gsl_min_fminimizer_f_minimum (const gsl_min_fminimizer * s) |
| 185 |
{ |
| 186 |
return s->f_minimum; |
| 187 |
} |
| 188 |
|
| 189 |
double |
| 190 |
gsl_min_fminimizer_f_lower (const gsl_min_fminimizer * s) |
| 191 |
{ |
| 192 |
return s->f_lower; |
| 193 |
} |
| 194 |
|
| 195 |
double |
| 196 |
gsl_min_fminimizer_f_upper (const gsl_min_fminimizer * s) |
| 197 |
{ |
| 198 |
return s->f_upper; |
| 199 |
} |
| 200 |
|