gsl_min__bracketing.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* min/bracketing.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. /* bracketing.c -- find an initial bracketing interval for a function to minimize */
  20. #include "gsl__config.h"
  21. #include "gsl_math.h"
  22. #include "gsl_errno.h"
  23. #include "gsl_min.h"
  24. #include "gsl_machine.h"
  25. #include "gsl_min__min.h"
  26. int
  27. gsl_min_find_bracket(gsl_function *f,double *x_minimum,double * f_minimum,
  28. double * x_lower, double * f_lower,
  29. double * x_upper, double * f_upper,
  30. size_t eval_max)
  31. {
  32. /* The three following variables must be declared volatile to avoid storage
  33. in extended precision registers available on some architecture. The code
  34. relies on the ability to compare double values. As the values will be
  35. store in regular memory, the extended precision will then be lost and
  36. values that are different in extended precision might have equal
  37. representation in double precision. This behavior might break the
  38. algorithm.
  39. */
  40. volatile double f_left = *f_lower;
  41. volatile double f_right = *f_upper;
  42. volatile double f_center;
  43. double x_left = *x_lower;
  44. double x_right= *x_upper;
  45. double x_center;
  46. const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
  47. size_t nb_eval = 0;
  48. if (f_right >= f_left)
  49. {
  50. x_center = (x_right - x_left) * golden + x_left;
  51. nb_eval++;
  52. SAFE_FUNC_CALL (f, x_center, &f_center);
  53. }
  54. else
  55. {
  56. x_center = x_right ;
  57. f_center = f_right ;
  58. x_right = (x_center - x_left) / golden + x_left;
  59. nb_eval++;
  60. SAFE_FUNC_CALL (f, x_right, &f_right);
  61. }
  62. do
  63. {
  64. if (f_center < f_left )
  65. {
  66. if (f_center < f_right)
  67. {
  68. *x_lower = x_left;
  69. *x_upper = x_right;
  70. *x_minimum = x_center;
  71. *f_lower = f_left;
  72. *f_upper = f_right;
  73. *f_minimum = f_center;
  74. /* gsl_ieee_printf_double (&f_left);
  75. printf(" ");
  76. gsl_ieee_printf_double (&f_center);
  77. printf("\n");*/
  78. return GSL_SUCCESS;
  79. }
  80. else if (f_center > f_right)
  81. {
  82. x_left = x_center;
  83. f_left = f_center;
  84. x_center = x_right;
  85. f_center = f_right;
  86. x_right = (x_center - x_left) / golden + x_left;
  87. nb_eval++;
  88. SAFE_FUNC_CALL (f, x_right, &f_right);
  89. }
  90. else /* f_center == f_right */
  91. {
  92. x_right = x_center;
  93. f_right = f_center;
  94. x_center = (x_right - x_left) * golden + x_left;
  95. nb_eval++;
  96. SAFE_FUNC_CALL (f, x_center, &f_center);
  97. }
  98. }
  99. else /* f_center >= f_left */
  100. {
  101. x_right = x_center;
  102. f_right = f_center;
  103. x_center = (x_right - x_left) * golden + x_left;
  104. nb_eval++;
  105. SAFE_FUNC_CALL (f, x_center, &f_center);
  106. }
  107. }
  108. while (nb_eval < eval_max
  109. && (x_right - x_left) > GSL_SQRT_DBL_EPSILON * ( (x_right + x_left) * 0.5 ) + GSL_SQRT_DBL_EPSILON);
  110. *x_lower = x_left;
  111. *x_upper = x_right;
  112. *x_minimum = x_center;
  113. *f_lower = f_left;
  114. *f_upper = f_right;
  115. *f_minimum = f_center;
  116. return GSL_FAILURE;
  117. }