brent.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* min/brent.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
  4. *
  5. * Taken from 'GSL - The GNU Scientific Library':
  6. * "One dimensional Minimization"
  7. * http://sources.redhat.com/gsl/
  8. * modified by Stefano Menegon 2004
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <math.h>
  27. #include <float.h>
  28. #define GSL_SQRT_DBL_EPSILON 1.e-4
  29. #define GSL_DBL_EPSILON 1.e-8
  30. /*
  31. #define SAFE_FUNC_CALL(f, x, yp) \
  32. do { \
  33. *yp = GSL_FN_EVAL(f,x); \
  34. if (!finite(*yp)) \
  35. fprintf(stderr,"function not continuous\n");\
  36. } while (0)
  37. */
  38. typedef struct
  39. {
  40. double d, e, v, w;
  41. double f_v, f_w;
  42. }
  43. brent_state_t;
  44. static int
  45. brent(void *vstate, double (*f) (), double *x_minimum, double *f_minimum,
  46. double *x_lower, double *f_lower, double *x_upper, double *f_upper)
  47. {
  48. brent_state_t *state = (brent_state_t *) vstate;
  49. const double x_left = *x_lower;
  50. const double x_right = *x_upper;
  51. const double z = *x_minimum;
  52. double d = state->e;
  53. double e = state->d;
  54. double u, f_u;
  55. const double v = state->v;
  56. const double w = state->w;
  57. const double f_v = state->f_v;
  58. const double f_w = state->f_w;
  59. const double f_z = *f_minimum;
  60. const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
  61. const double w_lower = (z - x_left);
  62. const double w_upper = (x_right - z);
  63. const double tolerance = GSL_SQRT_DBL_EPSILON * fabs(z);
  64. double p = 0, q = 0, r = 0;
  65. const double midpoint = 0.5 * (x_left + x_right);
  66. if (fabs(e) > tolerance) {
  67. /* fit parabola */
  68. r = (z - w) * (f_z - f_v);
  69. q = (z - v) * (f_z - f_w);
  70. p = (z - v) * q - (z - w) * r;
  71. q = 2 * (q - r);
  72. if (q > 0) {
  73. p = -p;
  74. }
  75. else {
  76. q = -q;
  77. }
  78. r = e;
  79. e = d;
  80. }
  81. if (fabs(p) < fabs(0.5 * q * r) && p < q * w_lower && p < q * w_upper) {
  82. double t2 = 2 * tolerance;
  83. d = p / q;
  84. u = z + d;
  85. if ((u - x_left) < t2 || (x_right - z) < t2) {
  86. d = (z < midpoint) ? tolerance : -tolerance;
  87. }
  88. }
  89. else {
  90. e = (z < midpoint) ? x_right - z : -(z - x_left);
  91. d = golden * e;
  92. }
  93. if (fabs(d) >= tolerance) {
  94. u = z + d;
  95. }
  96. else {
  97. u = z + ((d > 0) ? tolerance : -tolerance);
  98. }
  99. state->e = e;
  100. state->d = d;
  101. /* SAFE_FUNC_CALL(f, u, &f_u); */
  102. f_u = (*f) (u);
  103. if (f_u > f_z) {
  104. if (u < z) {
  105. *x_lower = u;
  106. *f_lower = f_u;
  107. return 0;
  108. }
  109. else {
  110. *x_upper = u;
  111. *f_upper = f_u;
  112. return 0;
  113. }
  114. }
  115. else if (f_u < f_z) {
  116. if (u < z) {
  117. *x_upper = z;
  118. *f_upper = f_z;
  119. }
  120. else {
  121. *x_lower = z;
  122. *f_lower = f_z;
  123. }
  124. state->v = w;
  125. state->f_v = f_w;
  126. state->w = z;
  127. state->f_w = f_z;
  128. *x_minimum = u;
  129. *f_minimum = f_u;
  130. return 0;
  131. }
  132. else if (f_u <= f_w || w == z) {
  133. state->v = w;
  134. state->f_v = f_w;
  135. state->w = u;
  136. state->f_w = f_u;
  137. return 0;
  138. }
  139. else if (f_u <= f_v || v == z || v == w) {
  140. state->v = u;
  141. state->f_v = f_u;
  142. return 0;
  143. }
  144. else {
  145. return -1;
  146. }
  147. }
  148. /* Code modified by Stefano Menegon 1st of February 2004 */
  149. double brent_iterate(double (*f) (), double x_lower, double x_upper,
  150. int maxiter)
  151. {
  152. int i;
  153. double x_minimum = (x_upper + x_lower) / 2.;
  154. double f_minimum;
  155. double f_lower;
  156. double f_upper;
  157. /* stato iterazione */
  158. brent_state_t itstate;
  159. const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
  160. double v = x_lower + golden * (x_upper - x_lower);
  161. double w = v;
  162. double f_vw;
  163. f_lower = (*f) (x_lower);
  164. f_upper = (*f) (x_upper);
  165. f_minimum = (*f) (x_minimum);
  166. itstate.v = v;
  167. itstate.w = w;
  168. itstate.d = 0.;
  169. itstate.e = 0.;
  170. /* SAFE_FUNC_CALL (f, v, &f_vw); */
  171. f_vw = (*f) (v);
  172. itstate.f_v = f_vw;
  173. itstate.f_w = f_vw;
  174. for (i = 0; i < maxiter; i++) {
  175. brent(&itstate, f, &x_minimum, &f_minimum, &x_lower, &f_lower,
  176. &x_upper, &f_upper);
  177. if (fabs(f_upper - f_lower) < GSL_DBL_EPSILON * fabs(f_minimum)) {
  178. return x_minimum;
  179. }
  180. }
  181. return x_minimum;
  182. }