gsl_min__golden.c 3.4 KB

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