| 1 | /* multimin/steepest_descent.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000 Fabrice Rossi |
| 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 | /* steepest_descent.c -- the steepest descent algorithm */ |
| 21 | |
| 22 | /* Modified by Brian Gough to use single iteration structure */ |
| 23 | |
| 24 | #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/ |
| 25 | #include "gsl_multimin.h"cxx . . $ `<file>/external/gsl/gsl_multimin.h`/ |
| 26 | #include "gsl_blas_types.h"cxx . . $ `<file>/external/gsl/gsl_blas_types.h`/ |
| 27 | #include "gsl_blas.h"cxx . . $ `<file>/external/gsl/gsl_blas.h`/ |
| 28 | |
| 29 | typedef structcxx . . $ $anonymous_type_a6cf42a6e768792a_0# |
| 30 | { |
| 31 | double stepcxx . . $ $anonymous_type_a6cf42a6e768792a_0#step.; |
| 32 | double max_stepcxx . . $ $anonymous_type_a6cf42a6e768792a_0#max_step.; |
| 33 | double tolcxx . . $ $anonymous_type_a6cf42a6e768792a_0#tol.; |
| 34 | gsl_vectorcxx . . $ gsl_vector# *x1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#x1.; |
| 35 | gsl_vectorcxx . . $ gsl_vector# *g1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#g1.; |
| 36 | } |
| 37 | steepest_descent_state_tcxx . . $ steepest_descent_state_t#; |
| 38 | |
| 39 | static int |
| 40 | steepest_descent_alloccxx . . $ steepest_descent_alloc(c0a5f96701d3f89). (void *vstate, size_t n) |
| 41 | { |
| 42 | steepest_descent_state_tcxx . . $ steepest_descent_state_t# *state = (steepest_descent_state_tcxx . . $ steepest_descent_state_t# *) vstate; |
| 43 | |
| 44 | state->x1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#x1. = gsl_vector_alloccxx . . $ gsl_vector_alloc(71d9e095f27e8ea9). (n); |
| 45 | |
| 46 | if (state->x1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#x1. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 47 | { |
| 48 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for x1", GSL_ENOMEM); |
| 49 | } |
| 50 | |
| 51 | state->g1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#g1. = gsl_vector_alloccxx . . $ gsl_vector_alloc(71d9e095f27e8ea9). (n); |
| 52 | |
| 53 | if (state->g1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#g1. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 54 | { |
| 55 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#x1.); |
| 56 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for g1", GSL_ENOMEM); |
| 57 | } |
| 58 | |
| 59 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 60 | } |
| 61 | |
| 62 | static int |
| 63 | steepest_descent_setcxx . . $ steepest_descent_set(aff3d705de048e1c). (void *vstate, gsl_multimin_function_fdfcxx . . $ gsl_multimin_function_fdf# * fdf, |
| 64 | const gsl_vectorcxx . . $ gsl_vector# * x, double *f, |
| 65 | gsl_vectorcxx . . $ gsl_vector# * gradient, double step_size, double tol) |
| 66 | { |
| 67 | steepest_descent_state_tcxx . . $ steepest_descent_state_t# *state = (steepest_descent_state_tcxx . . $ steepest_descent_state_t# *) vstate; |
| 68 | |
| 69 | GSL_MULTIMIN_FN_EVAL_F_DFcxx . . $ `external/gsl/gsl_multimin.h:73:9`! (fdf, x, f, gradient); |
| 70 | |
| 71 | state->stepcxx . . $ $anonymous_type_a6cf42a6e768792a_0#step. = step_size; |
| 72 | state->max_stepcxx . . $ $anonymous_type_a6cf42a6e768792a_0#max_step. = step_size; |
| 73 | state->tolcxx . . $ $anonymous_type_a6cf42a6e768792a_0#tol. = tol; |
| 74 | |
| 75 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static void |
| 80 | steepest_descent_freecxx . . $ steepest_descent_free(5d22bdacc48458e8). (void *vstate) |
| 81 | { |
| 82 | steepest_descent_state_tcxx . . $ steepest_descent_state_t# *state = (steepest_descent_state_tcxx . . $ steepest_descent_state_t# *) vstate; |
| 83 | |
| 84 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#x1.); |
| 85 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->g1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#g1.); |
| 86 | } |
| 87 | |
| 88 | static int |
| 89 | steepest_descent_restartcxx . . $ steepest_descent_restart(dedf1e9b1c614a07). (void *vstate) |
| 90 | { |
| 91 | steepest_descent_state_tcxx . . $ steepest_descent_state_t# *state = (steepest_descent_state_tcxx . . $ steepest_descent_state_t# *) vstate; |
| 92 | |
| 93 | state->stepcxx . . $ $anonymous_type_a6cf42a6e768792a_0#step. = state->max_stepcxx . . $ $anonymous_type_a6cf42a6e768792a_0#max_step.; |
| 94 | |
| 95 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 96 | } |
| 97 | |
| 98 | static int |
| 99 | steepest_descent_iteratecxx . . $ steepest_descent_iterate(26454239738d3d2d). (void *vstate, gsl_multimin_function_fdfcxx . . $ gsl_multimin_function_fdf# * fdf, |
| 100 | gsl_vectorcxx . . $ gsl_vector# * x, double *f, |
| 101 | gsl_vectorcxx . . $ gsl_vector# * gradient, gsl_vectorcxx . . $ gsl_vector# * dx) |
| 102 | { |
| 103 | steepest_descent_state_tcxx . . $ steepest_descent_state_t# *state = (steepest_descent_state_tcxx . . $ steepest_descent_state_t# *) vstate; |
| 104 | |
| 105 | gsl_vectorcxx . . $ gsl_vector# *x1 = state->x1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#x1.; |
| 106 | gsl_vectorcxx . . $ gsl_vector# *g1 = state->g1cxx . . $ $anonymous_type_a6cf42a6e768792a_0#g1.; |
| 107 | |
| 108 | double f0 = *f; |
| 109 | double f1; |
| 110 | double step = state->stepcxx . . $ $anonymous_type_a6cf42a6e768792a_0#step., tol = state->tolcxx . . $ $anonymous_type_a6cf42a6e768792a_0#tol.; |
| 111 | |
| 112 | int failed = 0; |
| 113 | |
| 114 | /* compute new trial point at x1= x - step * dir, where dir is the |
| 115 | normalized gradient */ |
| 116 | |
| 117 | double gnorm = gsl_blas_dnrm2cxx . . $ gsl_blas_dnrm2(1b712e435b5359e9). (gradient); |
| 118 | |
| 119 | if (gnorm == 0.0) |
| 120 | { |
| 121 | gsl_vector_set_zerocxx . . $ gsl_vector_set_zero(5d65852ca0326259). (dx); |
| 122 | return GSL_ENOPROGcxx . . $ GSL_ENOPROG.; |
| 123 | } |
| 124 | |
| 125 | trial: |
| 126 | gsl_vector_set_zerocxx . . $ gsl_vector_set_zero(5d65852ca0326259). (dx); |
| 127 | gsl_blas_daxpycxx . . $ gsl_blas_daxpy(b9b02baa1ffb1e94). (-step / gnorm, gradient, dx); |
| 128 | |
| 129 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (x1, x); |
| 130 | gsl_blas_daxpycxx . . $ gsl_blas_daxpy(b9b02baa1ffb1e94). (1.0, dx, x1); |
| 131 | |
| 132 | /* evaluate function and gradient at new point x1 */ |
| 133 | |
| 134 | GSL_MULTIMIN_FN_EVAL_F_DFcxx . . $ `external/gsl/gsl_multimin.h:73:9`! (fdf, x1, &f1, g1); |
| 135 | |
| 136 | if (f1 > f0) |
| 137 | { |
| 138 | /* downhill step failed, reduce step-size and try again */ |
| 139 | |
| 140 | failed = 1; |
| 141 | step *= tol; |
| 142 | goto trial; |
| 143 | } |
| 144 | |
| 145 | if (failed) |
| 146 | step *= tol; |
| 147 | else |
| 148 | step *= 2.0; |
| 149 | |
| 150 | state->stepcxx . . $ $anonymous_type_a6cf42a6e768792a_0#step. = step; |
| 151 | |
| 152 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (x, x1); |
| 153 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (gradient, g1); |
| 154 | |
| 155 | *f = f1; |
| 156 | |
| 157 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 158 | } |
| 159 | |
| 160 | static const gsl_multimin_fdfminimizer_typecxx . . $ gsl_multimin_fdfminimizer_type# steepest_descent_typecxx . . $ steepest_descent_type. = |
| 161 | { "steepest_descent", /* name */ |
| 162 | sizeof (steepest_descent_state_tcxx . . $ steepest_descent_state_t#), |
| 163 | &steepest_descent_alloccxx . . $ steepest_descent_alloc(c0a5f96701d3f89)., |
| 164 | &steepest_descent_setcxx . . $ steepest_descent_set(aff3d705de048e1c)., |
| 165 | &steepest_descent_iteratecxx . . $ steepest_descent_iterate(26454239738d3d2d)., |
| 166 | &steepest_descent_restartcxx . . $ steepest_descent_restart(dedf1e9b1c614a07)., |
| 167 | &steepest_descent_freecxx . . $ steepest_descent_free(5d22bdacc48458e8). |
| 168 | }; |
| 169 | |
| 170 | const gsl_multimin_fdfminimizer_typecxx . . $ gsl_multimin_fdfminimizer_type# |
| 171 | * gsl_multimin_fdfminimizer_steepest_descentcxx . . $ gsl_multimin_fdfminimizer_steepest_descent. = &steepest_descent_typecxx . . $ steepest_descent_type.; |