gsl_min__brent.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /* brent.c -- brent 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 d, e, v, w;
  33. double f_v, f_w;
  34. }
  35. brent_state_t;
  36. static int brent_init (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 brent_iterate (void *vstate, gsl_function * f, double *x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper);
  38. static int
  39. brent_init (void *vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper)
  40. {
  41. brent_state_t *state = (brent_state_t *) vstate;
  42. const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
  43. double v = x_lower + golden * (x_upper - x_lower);
  44. double w = v;
  45. double f_vw;
  46. x_minimum = 0 ; /* avoid warnings about unused varibles */
  47. f_minimum = 0 ;
  48. f_lower = 0 ;
  49. f_upper = 0 ;
  50. state->v = v;
  51. state->w = w;
  52. state->d = 0;
  53. state->e = 0;
  54. SAFE_FUNC_CALL (f, v, &f_vw);
  55. state->f_v = f_vw;
  56. state->f_w = f_vw;
  57. return GSL_SUCCESS;
  58. }
  59. static int
  60. brent_iterate (void *vstate, gsl_function * f, double *x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper)
  61. {
  62. brent_state_t *state = (brent_state_t *) vstate;
  63. const double x_left = *x_lower;
  64. const double x_right = *x_upper;
  65. const double z = *x_minimum;
  66. double d = state->e;
  67. double e = state->d;
  68. double u, f_u;
  69. const double v = state->v;
  70. const double w = state->w;
  71. const double f_v = state->f_v;
  72. const double f_w = state->f_w;
  73. const double f_z = *f_minimum;
  74. const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
  75. const double w_lower = (z - x_left);
  76. const double w_upper = (x_right - z);
  77. const double tolerance = GSL_SQRT_DBL_EPSILON * fabs (z);
  78. double p = 0, q = 0, r = 0;
  79. const double midpoint = 0.5 * (x_left + x_right);
  80. if (fabs (e) > tolerance)
  81. {
  82. /* fit parabola */
  83. r = (z - w) * (f_z - f_v);
  84. q = (z - v) * (f_z - f_w);
  85. p = (z - v) * q - (z - w) * r;
  86. q = 2 * (q - r);
  87. if (q > 0)
  88. {
  89. p = -p;
  90. }
  91. else
  92. {
  93. q = -q;
  94. }
  95. r = e;
  96. e = d;
  97. }
  98. if (fabs (p) < fabs (0.5 * q * r) && p < q * w_lower && p < q * w_upper)
  99. {
  100. double t2 = 2 * tolerance ;
  101. d = p / q;
  102. u = z + d;
  103. if ((u - x_left) < t2 || (x_right - u) < t2)
  104. {
  105. d = (z < midpoint) ? tolerance : -tolerance ;
  106. }
  107. }
  108. else
  109. {
  110. e = (z < midpoint) ? x_right - z : -(z - x_left) ;
  111. d = golden * e;
  112. }
  113. if (fabs (d) >= tolerance)
  114. {
  115. u = z + d;
  116. }
  117. else
  118. {
  119. u = z + ((d > 0) ? tolerance : -tolerance) ;
  120. }
  121. state->e = e;
  122. state->d = d;
  123. SAFE_FUNC_CALL(f, u, &f_u);
  124. if (f_u <= f_z)
  125. {
  126. if (u < z)
  127. {
  128. *x_upper = z;
  129. *f_upper = f_z;
  130. }
  131. else
  132. {
  133. *x_lower = z;
  134. *f_lower = f_z;
  135. }
  136. state->v = w;
  137. state->f_v = f_w;
  138. state->w = z;
  139. state->f_w = f_z;
  140. *x_minimum = u;
  141. *f_minimum = f_u;
  142. return GSL_SUCCESS;
  143. }
  144. else
  145. {
  146. if (u < z)
  147. {
  148. *x_lower = u;
  149. *f_lower = f_u;
  150. return GSL_SUCCESS;
  151. }
  152. else
  153. {
  154. *x_upper = u;
  155. *f_upper = f_u;
  156. return GSL_SUCCESS;
  157. }
  158. if (f_u <= f_w || w == z)
  159. {
  160. state->v = w;
  161. state->f_v = f_w;
  162. state->w = u;
  163. state->f_w = f_u;
  164. return GSL_SUCCESS;
  165. }
  166. else if (f_u <= f_v || v == z || v == w)
  167. {
  168. state->v = u;
  169. state->f_v = f_u;
  170. return GSL_SUCCESS;
  171. }
  172. }
  173. return GSL_FAILURE;
  174. }
  175. static const gsl_min_fminimizer_type brent_type =
  176. {"brent", /* name */
  177. sizeof (brent_state_t),
  178. &brent_init,
  179. &brent_iterate};
  180. const gsl_min_fminimizer_type *gsl_min_fminimizer_brent = &brent_type;