| 1 | /* min/golden.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 | |
| 21 | /* goldensection.c -- goldensection minimum finding algorithm */ |
| 22 | |
| 23 | #include "gsl__config.h" |
| 24 | |
| 25 | #include <stddef.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <math.h> |
| 29 | #include <float.h> |
| 30 | |
| 31 | #include "gsl_math.h" |
| 32 | #include "gsl_errno.h" |
| 33 | #include "gsl_min.h" |
| 34 | |
| 35 | #include "gsl_min__min.h" |
| 36 | |
| 37 | typedef struct |
| 38 | { |
| 39 | double dummy; |
| 40 | } |
| 41 | goldensection_state_t; |
| 42 | |
| 43 | static int goldensection_init (void * vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper); |
| 44 | static int goldensection_iterate (void * vstate, gsl_function * f, double * x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper); |
| 45 | |
| 46 | static int |
| 47 | goldensection_init (void * vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper) |
| 48 | { |
| 49 | |
| 50 | /* no initialization required, prevent warnings about unused variables */ |
| 51 | |
| 52 | f = 0; |
| 53 | x_minimum = 0; |
| 54 | f_minimum = 0; |
| 55 | x_lower = 0; |
| 56 | f_lower = 0; |
| 57 | x_upper = 0; |
| 58 | f_upper = 0; |
| 59 | |
| 60 | return GSL_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | static int |
| 64 | goldensection_iterate (void * vstate, gsl_function * f, double * x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper) |
| 65 | { |
| 66 | const double x_center = *x_minimum ; |
| 67 | const double x_left = *x_lower ; |
| 68 | const double x_right = *x_upper ; |
| 69 | |
| 70 | const double f_min = *f_minimum; |
| 71 | |
| 72 | const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */ |
| 73 | |
| 74 | const double w_lower = (x_center - x_left); |
| 75 | const double w_upper = (x_right - x_center); |
| 76 | |
| 77 | double x_new, f_new; |
| 78 | |
| 79 | x_new = x_center + golden * ((w_upper > w_lower) ? w_upper : -w_lower) ; |
| 80 | |
| 81 | SAFE_FUNC_CALL (f, x_new, &f_new); |
| 82 | |
| 83 | if (f_new < f_min) |
| 84 | { |
| 85 | *x_minimum = x_new ; |
| 86 | *f_minimum = f_new ; |
| 87 | return GSL_SUCCESS; |
| 88 | } |
| 89 | else if (x_new < x_center && f_new > f_min) |
| 90 | { |
| 91 | *x_lower = x_new ; |
| 92 | *f_lower = f_new ; |
| 93 | return GSL_SUCCESS; |
| 94 | } |
| 95 | else if (x_new > x_center && f_new > f_min) |
| 96 | { |
| 97 | *x_upper = x_new ; |
| 98 | *f_upper = f_new ; |
| 99 | return GSL_SUCCESS; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | return GSL_FAILURE; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static const gsl_min_fminimizer_type goldensection_type = |
| 109 | {"goldensection", /* name */ |
| 110 | sizeof (goldensection_state_t), |
| 111 | &goldensection_init, |
| 112 | &goldensection_iterate}; |
| 113 | |
| 114 | const gsl_min_fminimizer_type * gsl_min_fminimizer_goldensection = &goldensection_type; |