| 1 | /* multimin/simplex.c |
| 2 | * |
| 3 | * Copyright (C) 2007 Brian Gough |
| 4 | * Copyright (C) 2002 Tuomo Keskitalo, Ivo Alxneit |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or (at |
| 9 | * your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | - Originally written by Tuomo Keskitalo <[email protected]> |
| 23 | - Corrections to nmsimplex_iterate and other functions |
| 24 | by Ivo Alxneit <[email protected]> |
| 25 | - Additional help by Brian Gough <[email protected]> |
| 26 | */ |
| 27 | |
| 28 | /* The Simplex method of Nelder and Mead, |
| 29 | also known as the polytope search alogorithm. Ref: |
| 30 | Nelder, J.A., Mead, R., Computer Journal 7 (1965) pp. 308-313. |
| 31 | |
| 32 | This implementation uses n+1 corner points in the simplex. |
| 33 | */ |
| 34 | |
| 35 | #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/ |
| 36 | #include <stdlib.h> |
| 37 | #include "gsl_blas.h"cxx . . $ `<file>/external/gsl/gsl_blas.h`/ |
| 38 | #include "gsl_multimin.h"cxx . . $ `<file>/external/gsl/gsl_multimin.h`/ |
| 39 | |
| 40 | typedef structcxx . . $ $anonymous_type_a180c10398c56876_0# |
| 41 | { |
| 42 | gsl_matrixcxx . . $ gsl_matrix# *x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.; /* simplex corner points */ |
| 43 | gsl_vectorcxx . . $ gsl_vector# *y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1.; /* function value at corner points */ |
| 44 | gsl_vectorcxx . . $ gsl_vector# *ws1cxx . . $ $anonymous_type_a180c10398c56876_0#ws1.; /* workspace 1 for algorithm */ |
| 45 | gsl_vectorcxx . . $ gsl_vector# *ws2cxx . . $ $anonymous_type_a180c10398c56876_0#ws2.; /* workspace 2 for algorithm */ |
| 46 | } |
| 47 | nmsimplex_state_tcxx . . $ nmsimplex_state_t#; |
| 48 | |
| 49 | static double |
| 50 | nmsimplex_move_cornercxx . . $ nmsimplex_move_corner(e56a1a324aea26c7). (const double coeff, const nmsimplex_state_tcxx . . $ nmsimplex_state_t# * state, |
| 51 | size_t corner, gsl_vectorcxx . . $ gsl_vector# * xc, |
| 52 | const gsl_multimin_functioncxx . . $ gsl_multimin_function# * f) |
| 53 | { |
| 54 | /* moves a simplex corner scaled by coeff (negative value represents |
| 55 | mirroring by the middle point of the "other" corner points) |
| 56 | and gives new corner in xc and function value at xc as a |
| 57 | return value |
| 58 | */ |
| 59 | |
| 60 | gsl_matrixcxx . . $ gsl_matrix# *x1 = state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.; |
| 61 | |
| 62 | size_t i, j; |
| 63 | double newval, mp; |
| 64 | |
| 65 | for (j = 0; j < x1->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; j++) |
| 66 | { |
| 67 | mp = 0.0; |
| 68 | for (i = 0; i < x1->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; i++) |
| 69 | { |
| 70 | if (i != corner) |
| 71 | { |
| 72 | mp += (gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (x1, i, j)); |
| 73 | } |
| 74 | } |
| 75 | mp /= (double) (x1->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. - 1); |
| 76 | newval = mp - coeff * (mp - gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (x1, corner, j)); |
| 77 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (xc, j, newval); |
| 78 | } |
| 79 | |
| 80 | newval = GSL_MULTIMIN_FN_EVALcxx . . $ `external/gsl/gsl_multimin.h:56:9`! (f, xc); |
| 81 | |
| 82 | return newval; |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | nmsimplex_contract_by_bestcxx . . $ nmsimplex_contract_by_best(5f789993765135ee). (nmsimplex_state_tcxx . . $ nmsimplex_state_t# * state, size_t best, |
| 87 | gsl_vectorcxx . . $ gsl_vector# * xc, gsl_multimin_functioncxx . . $ gsl_multimin_function# * f) |
| 88 | { |
| 89 | |
| 90 | /* Function contracts the simplex in respect to |
| 91 | best valued corner. That is, all corners besides the |
| 92 | best corner are moved. */ |
| 93 | |
| 94 | /* the xc vector is simply work space here */ |
| 95 | |
| 96 | gsl_matrixcxx . . $ gsl_matrix# *x1 = state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.; |
| 97 | gsl_vectorcxx . . $ gsl_vector# *y1 = state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1.; |
| 98 | |
| 99 | size_t i, j; |
| 100 | double newval; |
| 101 | |
| 102 | int status = GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 103 | |
| 104 | for (i = 0; i < x1->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; i++) |
| 105 | { |
| 106 | if (i != best) |
| 107 | { |
| 108 | for (j = 0; j < x1->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; j++) |
| 109 | { |
| 110 | newval = 0.5 * (gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (x1, i, j) |
| 111 | + gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (x1, best, j)); |
| 112 | gsl_matrix_setcxx . . $ gsl_matrix_set(b56d954874bd7b13). (x1, i, j, newval); |
| 113 | } |
| 114 | |
| 115 | /* evaluate function in the new point */ |
| 116 | |
| 117 | gsl_matrix_get_rowcxx . . $ gsl_matrix_get_row(2e672276793e9a49). (xc, x1, i); |
| 118 | newval = GSL_MULTIMIN_FN_EVALcxx . . $ `external/gsl/gsl_multimin.h:56:9`! (f, xc); |
| 119 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (y1, i, newval); |
| 120 | |
| 121 | /* notify caller that we found at least one bad function value. |
| 122 | we finish the contraction (and do not abort) to allow the user |
| 123 | to handle the situation */ |
| 124 | |
| 125 | if(!gsl_finitecxx . . $ gsl_finite(620b9ac44d2573a6).(newval)) |
| 126 | { |
| 127 | status = GSL_EBADFUNCcxx . . $ GSL_EBADFUNC.; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return status; |
| 133 | } |
| 134 | |
| 135 | static int |
| 136 | nmsimplex_calc_centercxx . . $ nmsimplex_calc_center(19b4a28634ed64a7). (const nmsimplex_state_tcxx . . $ nmsimplex_state_t# * state, gsl_vectorcxx . . $ gsl_vector# * mp) |
| 137 | { |
| 138 | /* calculates the center of the simplex to mp */ |
| 139 | |
| 140 | gsl_matrixcxx . . $ gsl_matrix# *x1 = state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.; |
| 141 | |
| 142 | size_t i, j; |
| 143 | double val; |
| 144 | |
| 145 | for (j = 0; j < x1->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; j++) |
| 146 | { |
| 147 | val = 0.0; |
| 148 | for (i = 0; i < x1->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; i++) |
| 149 | { |
| 150 | val += gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (x1, i, j); |
| 151 | } |
| 152 | val /= x1->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 153 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (mp, j, val); |
| 154 | } |
| 155 | |
| 156 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 157 | } |
| 158 | |
| 159 | static double |
| 160 | nmsimplex_sizecxx . . $ nmsimplex_size(8b2f97d92134d62e). (nmsimplex_state_tcxx . . $ nmsimplex_state_t# * state) |
| 161 | { |
| 162 | /* calculates simplex size as average sum of length of vectors |
| 163 | from simplex center to corner points: |
| 164 | |
| 165 | ( sum ( || y - y_middlepoint || ) ) / n |
| 166 | */ |
| 167 | |
| 168 | gsl_vectorcxx . . $ gsl_vector# *s = state->ws1cxx . . $ $anonymous_type_a180c10398c56876_0#ws1.; |
| 169 | gsl_vectorcxx . . $ gsl_vector# *mp = state->ws2cxx . . $ $anonymous_type_a180c10398c56876_0#ws2.; |
| 170 | |
| 171 | gsl_matrixcxx . . $ gsl_matrix# *x1 = state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.; |
| 172 | size_t i; |
| 173 | |
| 174 | double ss = 0.0; |
| 175 | |
| 176 | /* Calculate middle point */ |
| 177 | nmsimplex_calc_centercxx . . $ nmsimplex_calc_center(19b4a28634ed64a7). (state, mp); |
| 178 | |
| 179 | for (i = 0; i < x1->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; i++) |
| 180 | { |
| 181 | gsl_matrix_get_rowcxx . . $ gsl_matrix_get_row(2e672276793e9a49). (s, x1, i); |
| 182 | gsl_blas_daxpycxx . . $ gsl_blas_daxpy(b9b02baa1ffb1e94). (-1.0, mp, s); |
| 183 | ss += gsl_blas_dnrm2cxx . . $ gsl_blas_dnrm2(1b712e435b5359e9). (s); |
| 184 | } |
| 185 | |
| 186 | return ss / (double) (x1->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.); |
| 187 | } |
| 188 | |
| 189 | static int |
| 190 | nmsimplex_alloccxx . . $ nmsimplex_alloc(c0a5f96701d3f89). (void *vstate, size_t n) |
| 191 | { |
| 192 | nmsimplex_state_tcxx . . $ nmsimplex_state_t# *state = (nmsimplex_state_tcxx . . $ nmsimplex_state_t# *) vstate; |
| 193 | |
| 194 | if (n == 0) |
| 195 | { |
| 196 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`!("invalid number of parameters specified", GSL_EINVAL); |
| 197 | } |
| 198 | |
| 199 | state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1. = gsl_matrix_alloccxx . . $ gsl_matrix_alloc(5a4d020b4836594). (n + 1, n); |
| 200 | |
| 201 | if (state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 202 | { |
| 203 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for x1", GSL_ENOMEM); |
| 204 | } |
| 205 | |
| 206 | state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1. = gsl_vector_alloccxx . . $ gsl_vector_alloc(71d9e095f27e8ea9). (n + 1); |
| 207 | |
| 208 | if (state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 209 | { |
| 210 | gsl_matrix_freecxx . . $ gsl_matrix_free(ab29a617cccd3636).(state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.); |
| 211 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for y", GSL_ENOMEM); |
| 212 | } |
| 213 | |
| 214 | state->ws1cxx . . $ $anonymous_type_a180c10398c56876_0#ws1. = gsl_vector_alloccxx . . $ gsl_vector_alloc(71d9e095f27e8ea9). (n); |
| 215 | |
| 216 | if (state->ws1cxx . . $ $anonymous_type_a180c10398c56876_0#ws1. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 217 | { |
| 218 | gsl_matrix_freecxx . . $ gsl_matrix_free(ab29a617cccd3636).(state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.); |
| 219 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259).(state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1.); |
| 220 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for ws1", GSL_ENOMEM); |
| 221 | } |
| 222 | |
| 223 | state->ws2cxx . . $ $anonymous_type_a180c10398c56876_0#ws2. = gsl_vector_alloccxx . . $ gsl_vector_alloc(71d9e095f27e8ea9). (n); |
| 224 | |
| 225 | if (state->ws2cxx . . $ $anonymous_type_a180c10398c56876_0#ws2. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 226 | { |
| 227 | gsl_matrix_freecxx . . $ gsl_matrix_free(ab29a617cccd3636).(state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.); |
| 228 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259).(state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1.); |
| 229 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259).(state->ws1cxx . . $ $anonymous_type_a180c10398c56876_0#ws1.); |
| 230 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for ws2", GSL_ENOMEM); |
| 231 | } |
| 232 | |
| 233 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 234 | } |
| 235 | |
| 236 | static int |
| 237 | nmsimplex_setcxx . . $ nmsimplex_set(b0aaf11e8c4927d2). (void *vstate, gsl_multimin_functioncxx . . $ gsl_multimin_function# * f, |
| 238 | const gsl_vectorcxx . . $ gsl_vector# * x, |
| 239 | double *size, const gsl_vectorcxx . . $ gsl_vector# * step_size) |
| 240 | { |
| 241 | int status; |
| 242 | size_t i; |
| 243 | double val; |
| 244 | |
| 245 | nmsimplex_state_tcxx . . $ nmsimplex_state_t# *state = (nmsimplex_state_tcxx . . $ nmsimplex_state_t# *) vstate; |
| 246 | |
| 247 | gsl_vectorcxx . . $ gsl_vector# *xtemp = state->ws1cxx . . $ $anonymous_type_a180c10398c56876_0#ws1.; |
| 248 | |
| 249 | if (xtemp->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 250 | { |
| 251 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`!("incompatible size of x", GSL_EINVAL); |
| 252 | } |
| 253 | |
| 254 | if (xtemp->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != step_size->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 255 | { |
| 256 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`!("incompatible size of step_size", GSL_EINVAL); |
| 257 | } |
| 258 | |
| 259 | /* first point is the original x0 */ |
| 260 | |
| 261 | val = GSL_MULTIMIN_FN_EVALcxx . . $ `external/gsl/gsl_multimin.h:56:9`! (f, x); |
| 262 | |
| 263 | if (!gsl_finitecxx . . $ gsl_finite(620b9ac44d2573a6).(val)) |
| 264 | { |
| 265 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`!("non-finite function value encountered", GSL_EBADFUNC); |
| 266 | } |
| 267 | |
| 268 | gsl_matrix_set_rowcxx . . $ gsl_matrix_set_row(cc0bbdd4e7d1c9ee). (state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1., 0, x); |
| 269 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1., 0, val); |
| 270 | |
| 271 | /* following points are initialized to x0 + step_size */ |
| 272 | |
| 273 | for (i = 0; i < x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.; i++) |
| 274 | { |
| 275 | status = gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (xtemp, x); |
| 276 | |
| 277 | if (status != 0) |
| 278 | { |
| 279 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("vector memcopy failed", GSL_EFAILED); |
| 280 | } |
| 281 | |
| 282 | val = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (xtemp, i) + gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (step_size, i); |
| 283 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (xtemp, i, val); |
| 284 | val = GSL_MULTIMIN_FN_EVALcxx . . $ `external/gsl/gsl_multimin.h:56:9`! (f, xtemp); |
| 285 | |
| 286 | if (!gsl_finitecxx . . $ gsl_finite(620b9ac44d2573a6).(val)) |
| 287 | { |
| 288 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`!("non-finite function value encountered", GSL_EBADFUNC); |
| 289 | } |
| 290 | |
| 291 | gsl_matrix_set_rowcxx . . $ gsl_matrix_set_row(cc0bbdd4e7d1c9ee). (state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1., i + 1, xtemp); |
| 292 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1., i + 1, val); |
| 293 | } |
| 294 | |
| 295 | /* Initialize simplex size */ |
| 296 | |
| 297 | *size = nmsimplex_sizecxx . . $ nmsimplex_size(8b2f97d92134d62e). (state); |
| 298 | |
| 299 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | nmsimplex_freecxx . . $ nmsimplex_free(5d22bdacc48458e8). (void *vstate) |
| 304 | { |
| 305 | nmsimplex_state_tcxx . . $ nmsimplex_state_t# *state = (nmsimplex_state_tcxx . . $ nmsimplex_state_t# *) vstate; |
| 306 | |
| 307 | gsl_matrix_freecxx . . $ gsl_matrix_free(ab29a617cccd3636). (state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.); |
| 308 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1.); |
| 309 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->ws1cxx . . $ $anonymous_type_a180c10398c56876_0#ws1.); |
| 310 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->ws2cxx . . $ $anonymous_type_a180c10398c56876_0#ws2.); |
| 311 | } |
| 312 | |
| 313 | static int |
| 314 | nmsimplex_iteratecxx . . $ nmsimplex_iterate(43ac7c8a4ab54416). (void *vstate, gsl_multimin_functioncxx . . $ gsl_multimin_function# * f, |
| 315 | gsl_vectorcxx . . $ gsl_vector# * x, double *size, double *fval) |
| 316 | { |
| 317 | |
| 318 | /* Simplex iteration tries to minimize function f value */ |
| 319 | /* Includes corrections from Ivo Alxneit <[email protected]> */ |
| 320 | |
| 321 | nmsimplex_state_tcxx . . $ nmsimplex_state_t# *state = (nmsimplex_state_tcxx . . $ nmsimplex_state_t# *) vstate; |
| 322 | |
| 323 | /* xc and xc2 vectors store tried corner point coordinates */ |
| 324 | |
| 325 | gsl_vectorcxx . . $ gsl_vector# *xc = state->ws1cxx . . $ $anonymous_type_a180c10398c56876_0#ws1.; |
| 326 | gsl_vectorcxx . . $ gsl_vector# *xc2 = state->ws2cxx . . $ $anonymous_type_a180c10398c56876_0#ws2.; |
| 327 | gsl_vectorcxx . . $ gsl_vector# *y1 = state->y1cxx . . $ $anonymous_type_a180c10398c56876_0#y1.; |
| 328 | gsl_matrixcxx . . $ gsl_matrix# *x1 = state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1.; |
| 329 | |
| 330 | size_t n = y1->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.; |
| 331 | size_t i; |
| 332 | size_t hi = 0, s_hi = 0, lo = 0; |
| 333 | double dhi, ds_hi, dlo; |
| 334 | int status; |
| 335 | double val, val2; |
| 336 | |
| 337 | |
| 338 | if (xc->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 339 | { |
| 340 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`!("incompatible size of x", GSL_EINVAL); |
| 341 | } |
| 342 | |
| 343 | /* get index of highest, second highest and lowest point */ |
| 344 | |
| 345 | dhi = ds_hi = dlo = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (y1, 0); |
| 346 | |
| 347 | for (i = 1; i < n; i++) |
| 348 | { |
| 349 | val = (gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (y1, i)); |
| 350 | if (val < dlo) |
| 351 | { |
| 352 | dlo = val; |
| 353 | lo = i; |
| 354 | } |
| 355 | else if (val > dhi) |
| 356 | { |
| 357 | ds_hi = dhi; |
| 358 | s_hi = hi; |
| 359 | dhi = val; |
| 360 | hi = i; |
| 361 | } |
| 362 | else if (val > ds_hi) |
| 363 | { |
| 364 | ds_hi = val; |
| 365 | s_hi = i; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* reflect the highest value */ |
| 370 | |
| 371 | val = nmsimplex_move_cornercxx . . $ nmsimplex_move_corner(e56a1a324aea26c7). (-1.0, state, hi, xc, f); |
| 372 | |
| 373 | if (gsl_finitecxx . . $ gsl_finite(620b9ac44d2573a6).(val) && val < gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (y1, lo)) |
| 374 | { |
| 375 | |
| 376 | /* reflected point becomes lowest point, try expansion */ |
| 377 | |
| 378 | val2 = nmsimplex_move_cornercxx . . $ nmsimplex_move_corner(e56a1a324aea26c7). (-2.0, state, hi, xc2, f); |
| 379 | |
| 380 | if (gsl_finitecxx . . $ gsl_finite(620b9ac44d2573a6).(val2) && val2 < gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (y1, lo)) |
| 381 | { |
| 382 | gsl_matrix_set_rowcxx . . $ gsl_matrix_set_row(cc0bbdd4e7d1c9ee). (x1, hi, xc2); |
| 383 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (y1, hi, val2); |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | gsl_matrix_set_rowcxx . . $ gsl_matrix_set_row(cc0bbdd4e7d1c9ee). (x1, hi, xc); |
| 388 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (y1, hi, val); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* reflection does not improve things enough |
| 393 | or |
| 394 | we got a non-finite (illegal) function value */ |
| 395 | |
| 396 | else if (!gsl_finitecxx . . $ gsl_finite(620b9ac44d2573a6).(val) || val > gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (y1, s_hi)) |
| 397 | { |
| 398 | if (gsl_finitecxx . . $ gsl_finite(620b9ac44d2573a6).(val) && val <= gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (y1, hi)) |
| 399 | { |
| 400 | |
| 401 | /* if trial point is better than highest point, replace |
| 402 | highest point */ |
| 403 | |
| 404 | gsl_matrix_set_rowcxx . . $ gsl_matrix_set_row(cc0bbdd4e7d1c9ee). (x1, hi, xc); |
| 405 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (y1, hi, val); |
| 406 | } |
| 407 | |
| 408 | /* try one dimensional contraction */ |
| 409 | |
| 410 | val2 = nmsimplex_move_cornercxx . . $ nmsimplex_move_corner(e56a1a324aea26c7). (0.5, state, hi, xc2, f); |
| 411 | |
| 412 | if (gsl_finitecxx . . $ gsl_finite(620b9ac44d2573a6).(val2) && val2 <= gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (y1, hi)) |
| 413 | { |
| 414 | gsl_matrix_set_rowcxx . . $ gsl_matrix_set_row(cc0bbdd4e7d1c9ee). (state->x1cxx . . $ $anonymous_type_a180c10398c56876_0#x1., hi, xc2); |
| 415 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (y1, hi, val2); |
| 416 | } |
| 417 | |
| 418 | else |
| 419 | { |
| 420 | |
| 421 | /* contract the whole simplex in respect to the best point */ |
| 422 | |
| 423 | status = nmsimplex_contract_by_bestcxx . . $ nmsimplex_contract_by_best(5f789993765135ee). (state, lo, xc, f); |
| 424 | if (status != GSL_SUCCESScxx . . $ GSL_SUCCESS.) |
| 425 | { |
| 426 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("nmsimplex_contract_by_best failed", GSL_EFAILED); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | |
| 433 | /* trial point is better than second highest point. |
| 434 | Replace highest point by it */ |
| 435 | |
| 436 | gsl_matrix_set_rowcxx . . $ gsl_matrix_set_row(cc0bbdd4e7d1c9ee). (x1, hi, xc); |
| 437 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (y1, hi, val); |
| 438 | } |
| 439 | |
| 440 | /* return lowest point of simplex as x */ |
| 441 | |
| 442 | lo = gsl_vector_min_indexcxx . . $ gsl_vector_min_index(5954d1cf5d86524e). (y1); |
| 443 | gsl_matrix_get_rowcxx . . $ gsl_matrix_get_row(2e672276793e9a49). (x, x1, lo); |
| 444 | *fval = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (y1, lo); |
| 445 | |
| 446 | /* Update simplex size */ |
| 447 | |
| 448 | *size = nmsimplex_sizecxx . . $ nmsimplex_size(8b2f97d92134d62e). (state); |
| 449 | |
| 450 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 451 | } |
| 452 | |
| 453 | static const gsl_multimin_fminimizer_typecxx . . $ gsl_multimin_fminimizer_type# nmsimplex_typecxx . . $ nmsimplex_type. = |
| 454 | { "nmsimplex", /* name */ |
| 455 | sizeof (nmsimplex_state_tcxx . . $ nmsimplex_state_t#), |
| 456 | &nmsimplex_alloccxx . . $ nmsimplex_alloc(c0a5f96701d3f89)., |
| 457 | &nmsimplex_setcxx . . $ nmsimplex_set(b0aaf11e8c4927d2)., |
| 458 | &nmsimplex_iteratecxx . . $ nmsimplex_iterate(43ac7c8a4ab54416)., |
| 459 | &nmsimplex_freecxx . . $ nmsimplex_free(5d22bdacc48458e8). |
| 460 | }; |
| 461 | |
| 462 | const gsl_multimin_fminimizer_typecxx . . $ gsl_multimin_fminimizer_type# |
| 463 | * gsl_multimin_fminimizer_nmsimplexcxx . . $ gsl_multimin_fminimizer_nmsimplex. = &nmsimplex_typecxx . . $ nmsimplex_type.; |