gsl_roots__brent.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* roots/brent.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, 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 root 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_roots.h"
  29. #include "gsl_roots__roots.h"
  30. typedef struct
  31. {
  32. double a, b, c, d, e;
  33. double fa, fb, fc;
  34. }
  35. brent_state_t;
  36. static int brent_init (void * vstate, gsl_function * f, double * root, double x_lower, double x_upper);
  37. static int brent_iterate (void * vstate, gsl_function * f, double * root, double * x_lower, double * x_upper);
  38. static int
  39. brent_init (void * vstate, gsl_function * f, double * root, double x_lower, double x_upper)
  40. {
  41. brent_state_t * state = (brent_state_t *) vstate;
  42. double f_lower, f_upper ;
  43. *root = 0.5 * (x_lower + x_upper) ;
  44. SAFE_FUNC_CALL (f, x_lower, &f_lower);
  45. SAFE_FUNC_CALL (f, x_upper, &f_upper);
  46. state->a = x_lower;
  47. state->fa = f_lower;
  48. state->b = x_upper;
  49. state->fb = f_upper;
  50. state->c = x_upper;
  51. state->fc = f_upper;
  52. state->d = x_upper - x_lower ;
  53. state->e = x_upper - x_lower ;
  54. if ((f_lower < 0.0 && f_upper < 0.0) || (f_lower > 0.0 && f_upper > 0.0))
  55. {
  56. GSL_ERROR ("endpoints do not straddle y=0", GSL_EINVAL);
  57. }
  58. return GSL_SUCCESS;
  59. }
  60. static int
  61. brent_iterate (void * vstate, gsl_function * f, double * root, double * x_lower, double * x_upper)
  62. {
  63. brent_state_t * state = (brent_state_t *) vstate;
  64. double tol, m;
  65. int ac_equal = 0;
  66. double a = state->a, b = state->b, c = state->c;
  67. double fa = state->fa, fb = state->fb, fc = state->fc;
  68. double d = state->d, e = state->e;
  69. if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0))
  70. {
  71. ac_equal = 1;
  72. c = a;
  73. fc = fa;
  74. d = b - a;
  75. e = b - a;
  76. }
  77. if (fabs (fc) < fabs (fb))
  78. {
  79. ac_equal = 1;
  80. a = b;
  81. b = c;
  82. c = a;
  83. fa = fb;
  84. fb = fc;
  85. fc = fa;
  86. }
  87. tol = 0.5 * GSL_DBL_EPSILON * fabs (b);
  88. m = 0.5 * (c - b);
  89. if (fb == 0)
  90. {
  91. *root = b;
  92. *x_lower = b;
  93. *x_upper = b;
  94. return GSL_SUCCESS;
  95. }
  96. if (fabs (m) <= tol)
  97. {
  98. *root = b;
  99. if (b < c)
  100. {
  101. *x_lower = b;
  102. *x_upper = c;
  103. }
  104. else
  105. {
  106. *x_lower = c;
  107. *x_upper = b;
  108. }
  109. return GSL_SUCCESS;
  110. }
  111. if (fabs (e) < tol || fabs (fa) <= fabs (fb))
  112. {
  113. d = m; /* use bisection */
  114. e = m;
  115. }
  116. else
  117. {
  118. double p, q, r; /* use inverse cubic interpolation */
  119. double s = fb / fa;
  120. if (ac_equal)
  121. {
  122. p = 2 * m * s;
  123. q = 1 - s;
  124. }
  125. else
  126. {
  127. q = fa / fc;
  128. r = fb / fc;
  129. p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
  130. q = (q - 1) * (r - 1) * (s - 1);
  131. }
  132. if (p > 0)
  133. {
  134. q = -q;
  135. }
  136. else
  137. {
  138. p = -p;
  139. }
  140. if (2 * p < GSL_MIN (3 * m * q - fabs (tol * q), fabs (e * q)))
  141. {
  142. e = d;
  143. d = p / q;
  144. }
  145. else
  146. {
  147. /* interpolation failed, fall back to bisection */
  148. d = m;
  149. e = m;
  150. }
  151. }
  152. a = b;
  153. fa = fb;
  154. if (fabs (d) > tol)
  155. {
  156. b += d;
  157. }
  158. else
  159. {
  160. b += (m > 0 ? +tol : -tol);
  161. }
  162. SAFE_FUNC_CALL (f, b, &fb);
  163. state->a = a ;
  164. state->b = b ;
  165. state->c = c ;
  166. state->d = d ;
  167. state->e = e ;
  168. state->fa = fa ;
  169. state->fb = fb ;
  170. state->fc = fc ;
  171. /* Update the best estimate of the root and bounds on each
  172. iteration */
  173. *root = b;
  174. if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0))
  175. {
  176. c = a;
  177. }
  178. if (b < c)
  179. {
  180. *x_lower = b;
  181. *x_upper = c;
  182. }
  183. else
  184. {
  185. *x_lower = c;
  186. *x_upper = b;
  187. }
  188. return GSL_SUCCESS ;
  189. }
  190. static const gsl_root_fsolver_type brent_type =
  191. {"brent", /* name */
  192. sizeof (brent_state_t),
  193. &brent_init,
  194. &brent_iterate};
  195. const gsl_root_fsolver_type * gsl_root_fsolver_brent = &brent_type;