| 1 | /* min/brent.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 | /* brent.c -- brent minimum finding algorithm */ |
| 21 | |
| 22 | #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/ |
| 23 | |
| 24 | #include <stddef.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <math.h> |
| 28 | #include <float.h> |
| 29 | |
| 30 | #include "gsl_math.h"cxx . . $ `<file>/external/gsl/gsl_math.h`/ |
| 31 | #include "gsl_errno.h"cxx . . $ `<file>/external/gsl/gsl_errno.h`/ |
| 32 | #include "gsl_min.h"cxx . . $ `<file>/external/gsl/gsl_min.h`/ |
| 33 | |
| 34 | #include "gsl_min__min.h"cxx . . $ `<file>/external/gsl/gsl_min__min.h`/ |
| 35 | |
| 36 | typedef structcxx . . $ $anonymous_type_8dacf4e988e5e24_0# |
| 37 | { |
| 38 | double dcxx . . $ $anonymous_type_8dacf4e988e5e24_0#d., ecxx . . $ $anonymous_type_8dacf4e988e5e24_0#e., vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#v., wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#w.; |
| 39 | double f_vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_v., f_wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_w.; |
| 40 | } |
| 41 | brent_state_tcxx . . $ brent_state_t#; |
| 42 | |
| 43 | static int brent_initcxx . . $ brent_init(b9567197d72e2d52). (void *vstate, gsl_functioncxx . . $ gsl_function# * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper); |
| 44 | static int brent_iteratecxx . . $ brent_iterate(cfc338ff494cac36). (void *vstate, gsl_functioncxx . . $ 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 | brent_initcxx . . $ brent_init(b9567197d72e2d52). (void *vstate, gsl_functioncxx . . $ gsl_function# * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper) |
| 48 | { |
| 49 | brent_state_tcxx . . $ brent_state_t# *state = (brent_state_tcxx . . $ brent_state_t# *) vstate; |
| 50 | |
| 51 | const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */ |
| 52 | |
| 53 | double v = x_lower + golden * (x_upper - x_lower); |
| 54 | double w = v; |
| 55 | |
| 56 | double f_vw; |
| 57 | |
| 58 | x_minimum = 0 ; /* avoid warnings about unused varibles */ |
| 59 | f_minimum = 0 ; |
| 60 | f_lower = 0 ; |
| 61 | f_upper = 0 ; |
| 62 | |
| 63 | state->vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#v. = v; |
| 64 | state->wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#w. = w; |
| 65 | |
| 66 | state->dcxx . . $ $anonymous_type_8dacf4e988e5e24_0#d. = 0; |
| 67 | state->ecxx . . $ $anonymous_type_8dacf4e988e5e24_0#e. = 0; |
| 68 | |
| 69 | SAFE_FUNC_CALLcxx . . $ `external/gsl/gsl_min__min.h:20:9`! (f, v, &f_vw); |
| 70 | |
| 71 | state->f_vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_v. = f_vw; |
| 72 | state->f_wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_w. = f_vw; |
| 73 | |
| 74 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 75 | } |
| 76 | |
| 77 | static int |
| 78 | brent_iteratecxx . . $ brent_iterate(cfc338ff494cac36). (void *vstate, gsl_functioncxx . . $ gsl_function# * f, double *x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper) |
| 79 | { |
| 80 | brent_state_tcxx . . $ brent_state_t# *state = (brent_state_tcxx . . $ brent_state_t# *) vstate; |
| 81 | |
| 82 | const double x_left = *x_lower; |
| 83 | const double x_right = *x_upper; |
| 84 | |
| 85 | const double z = *x_minimum; |
| 86 | double d = state->ecxx . . $ $anonymous_type_8dacf4e988e5e24_0#e.; |
| 87 | double e = state->dcxx . . $ $anonymous_type_8dacf4e988e5e24_0#d.; |
| 88 | double u, f_u; |
| 89 | const double v = state->vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#v.; |
| 90 | const double w = state->wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#w.; |
| 91 | const double f_v = state->f_vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_v.; |
| 92 | const double f_w = state->f_wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_w.; |
| 93 | const double f_z = *f_minimum; |
| 94 | |
| 95 | const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */ |
| 96 | |
| 97 | const double w_lower = (z - x_left); |
| 98 | const double w_upper = (x_right - z); |
| 99 | |
| 100 | const double tolerance = GSL_SQRT_DBL_EPSILONcxx . . $ `external/gsl/gsl_machine.h:18:9`! * fabs (z); |
| 101 | |
| 102 | double p = 0, q = 0, r = 0; |
| 103 | |
| 104 | const double midpoint = 0.5 * (x_left + x_right); |
| 105 | |
| 106 | if (fabs (e) > tolerance) |
| 107 | { |
| 108 | /* fit parabola */ |
| 109 | |
| 110 | r = (z - w) * (f_z - f_v); |
| 111 | q = (z - v) * (f_z - f_w); |
| 112 | p = (z - v) * q - (z - w) * r; |
| 113 | q = 2 * (q - r); |
| 114 | |
| 115 | if (q > 0) |
| 116 | { |
| 117 | p = -p; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | q = -q; |
| 122 | } |
| 123 | |
| 124 | r = e; |
| 125 | e = d; |
| 126 | } |
| 127 | |
| 128 | if (fabs (p) < fabs (0.5 * q * r) && p < q * w_lower && p < q * w_upper) |
| 129 | { |
| 130 | double t2 = 2 * tolerance ; |
| 131 | |
| 132 | d = p / q; |
| 133 | u = z + d; |
| 134 | |
| 135 | if ((u - x_left) < t2 || (x_right - u) < t2) |
| 136 | { |
| 137 | d = (z < midpoint) ? tolerance : -tolerance ; |
| 138 | } |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | e = (z < midpoint) ? x_right - z : -(z - x_left) ; |
| 143 | d = golden * e; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | if (fabs (d) >= tolerance) |
| 148 | { |
| 149 | u = z + d; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | u = z + ((d > 0) ? tolerance : -tolerance) ; |
| 154 | } |
| 155 | |
| 156 | state->ecxx . . $ $anonymous_type_8dacf4e988e5e24_0#e. = e; |
| 157 | state->dcxx . . $ $anonymous_type_8dacf4e988e5e24_0#d. = d; |
| 158 | |
| 159 | SAFE_FUNC_CALLcxx . . $ `external/gsl/gsl_min__min.h:20:9`!(f, u, &f_u); |
| 160 | |
| 161 | if (f_u <= f_z) |
| 162 | { |
| 163 | if (u < z) |
| 164 | { |
| 165 | *x_upper = z; |
| 166 | *f_upper = f_z; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | *x_lower = z; |
| 171 | *f_lower = f_z; |
| 172 | } |
| 173 | |
| 174 | state->vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#v. = w; |
| 175 | state->f_vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_v. = f_w; |
| 176 | state->wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#w. = z; |
| 177 | state->f_wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_w. = f_z; |
| 178 | *x_minimum = u; |
| 179 | *f_minimum = f_u; |
| 180 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | if (u < z) |
| 185 | { |
| 186 | *x_lower = u; |
| 187 | *f_lower = f_u; |
| 188 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | *x_upper = u; |
| 193 | *f_upper = f_u; |
| 194 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 195 | } |
| 196 | |
| 197 | if (f_u <= f_w || w == z) |
| 198 | { |
| 199 | state->vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#v. = w; |
| 200 | state->f_vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_v. = f_w; |
| 201 | state->wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#w. = u; |
| 202 | state->f_wcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_w. = f_u; |
| 203 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 204 | } |
| 205 | else if (f_u <= f_v || v == z || v == w) |
| 206 | { |
| 207 | state->vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#v. = u; |
| 208 | state->f_vcxx . . $ $anonymous_type_8dacf4e988e5e24_0#f_v. = f_u; |
| 209 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return GSL_FAILUREcxx . . $ GSL_FAILURE.; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | static const gsl_min_fminimizer_typecxx . . $ gsl_min_fminimizer_type# brent_typecxx . . $ brent_type. = |
| 218 | {"brent", /* name */ |
| 219 | sizeof (brent_state_tcxx . . $ brent_state_t#), |
| 220 | &brent_initcxx . . $ brent_init(b9567197d72e2d52)., |
| 221 | &brent_iteratecxx . . $ brent_iterate(cfc338ff494cac36).}; |
| 222 | |
| 223 | const gsl_min_fminimizer_typecxx . . $ gsl_min_fminimizer_type# *gsl_min_fminimizer_brentcxx . . $ gsl_min_fminimizer_brent. = &brent_typecxx . . $ brent_type.; |