toms748_solve.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_SOLVE_ROOT_HPP
  6. #define BOOST_MATH_TOOLS_SOLVE_ROOT_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/tools/precision.hpp>
  11. #include <boost/math/policies/error_handling.hpp>
  12. #include <boost/math/tools/config.hpp>
  13. #include <boost/math/special_functions/sign.hpp>
  14. #include <boost/cstdint.hpp>
  15. #include <limits>
  16. namespace boost{ namespace math{ namespace tools{
  17. template <class T>
  18. class eps_tolerance
  19. {
  20. public:
  21. eps_tolerance(unsigned bits)
  22. {
  23. BOOST_MATH_STD_USING
  24. eps = (std::max)(T(ldexp(1.0F, 1-bits)), T(4 * tools::epsilon<T>()));
  25. }
  26. bool operator()(const T& a, const T& b)
  27. {
  28. BOOST_MATH_STD_USING
  29. return fabs(a - b) <= (eps * (std::min)(fabs(a), fabs(b)));
  30. }
  31. private:
  32. T eps;
  33. };
  34. struct equal_floor
  35. {
  36. equal_floor(){}
  37. template <class T>
  38. bool operator()(const T& a, const T& b)
  39. {
  40. BOOST_MATH_STD_USING
  41. return floor(a) == floor(b);
  42. }
  43. };
  44. struct equal_ceil
  45. {
  46. equal_ceil(){}
  47. template <class T>
  48. bool operator()(const T& a, const T& b)
  49. {
  50. BOOST_MATH_STD_USING
  51. return ceil(a) == ceil(b);
  52. }
  53. };
  54. struct equal_nearest_integer
  55. {
  56. equal_nearest_integer(){}
  57. template <class T>
  58. bool operator()(const T& a, const T& b)
  59. {
  60. BOOST_MATH_STD_USING
  61. return floor(a + 0.5f) == floor(b + 0.5f);
  62. }
  63. };
  64. namespace detail{
  65. template <class F, class T>
  66. void bracket(F f, T& a, T& b, T c, T& fa, T& fb, T& d, T& fd)
  67. {
  68. //
  69. // Given a point c inside the existing enclosing interval
  70. // [a, b] sets a = c if f(c) == 0, otherwise finds the new
  71. // enclosing interval: either [a, c] or [c, b] and sets
  72. // d and fd to the point that has just been removed from
  73. // the interval. In other words d is the third best guess
  74. // to the root.
  75. //
  76. BOOST_MATH_STD_USING // For ADL of std math functions
  77. T tol = tools::epsilon<T>() * 2;
  78. //
  79. // If the interval [a,b] is very small, or if c is too close
  80. // to one end of the interval then we need to adjust the
  81. // location of c accordingly:
  82. //
  83. if((b - a) < 2 * tol * a)
  84. {
  85. c = a + (b - a) / 2;
  86. }
  87. else if(c <= a + fabs(a) * tol)
  88. {
  89. c = a + fabs(a) * tol;
  90. }
  91. else if(c >= b - fabs(b) * tol)
  92. {
  93. c = b - fabs(a) * tol;
  94. }
  95. //
  96. // OK, lets invoke f(c):
  97. //
  98. T fc = f(c);
  99. //
  100. // if we have a zero then we have an exact solution to the root:
  101. //
  102. if(fc == 0)
  103. {
  104. a = c;
  105. fa = 0;
  106. d = 0;
  107. fd = 0;
  108. return;
  109. }
  110. //
  111. // Non-zero fc, update the interval:
  112. //
  113. if(boost::math::sign(fa) * boost::math::sign(fc) < 0)
  114. {
  115. d = b;
  116. fd = fb;
  117. b = c;
  118. fb = fc;
  119. }
  120. else
  121. {
  122. d = a;
  123. fd = fa;
  124. a = c;
  125. fa= fc;
  126. }
  127. }
  128. template <class T>
  129. inline T safe_div(T num, T denom, T r)
  130. {
  131. //
  132. // return num / denom without overflow,
  133. // return r if overflow would occur.
  134. //
  135. BOOST_MATH_STD_USING // For ADL of std math functions
  136. if(fabs(denom) < 1)
  137. {
  138. if(fabs(denom * tools::max_value<T>()) <= fabs(num))
  139. return r;
  140. }
  141. return num / denom;
  142. }
  143. template <class T>
  144. inline T secant_interpolate(const T& a, const T& b, const T& fa, const T& fb)
  145. {
  146. //
  147. // Performs standard secant interpolation of [a,b] given
  148. // function evaluations f(a) and f(b). Performs a bisection
  149. // if secant interpolation would leave us very close to either
  150. // a or b. Rationale: we only call this function when at least
  151. // one other form of interpolation has already failed, so we know
  152. // that the function is unlikely to be smooth with a root very
  153. // close to a or b.
  154. //
  155. BOOST_MATH_STD_USING // For ADL of std math functions
  156. T tol = tools::epsilon<T>() * 5;
  157. T c = a - (fa / (fb - fa)) * (b - a);
  158. if((c <= a + fabs(a) * tol) || (c >= b - fabs(b) * tol))
  159. return (a + b) / 2;
  160. return c;
  161. }
  162. template <class T>
  163. T quadratic_interpolate(const T& a, const T& b, T const& d,
  164. const T& fa, const T& fb, T const& fd,
  165. unsigned count)
  166. {
  167. //
  168. // Performs quadratic interpolation to determine the next point,
  169. // takes count Newton steps to find the location of the
  170. // quadratic polynomial.
  171. //
  172. // Point d must lie outside of the interval [a,b], it is the third
  173. // best approximation to the root, after a and b.
  174. //
  175. // Note: this does not guarentee to find a root
  176. // inside [a, b], so we fall back to a secant step should
  177. // the result be out of range.
  178. //
  179. // Start by obtaining the coefficients of the quadratic polynomial:
  180. //
  181. T B = safe_div(T(fb - fa), T(b - a), tools::max_value<T>());
  182. T A = safe_div(T(fd - fb), T(d - b), tools::max_value<T>());
  183. A = safe_div(T(A - B), T(d - a), T(0));
  184. if(A == 0)
  185. {
  186. // failure to determine coefficients, try a secant step:
  187. return secant_interpolate(a, b, fa, fb);
  188. }
  189. //
  190. // Determine the starting point of the Newton steps:
  191. //
  192. T c;
  193. if(boost::math::sign(A) * boost::math::sign(fa) > 0)
  194. {
  195. c = a;
  196. }
  197. else
  198. {
  199. c = b;
  200. }
  201. //
  202. // Take the Newton steps:
  203. //
  204. for(unsigned i = 1; i <= count; ++i)
  205. {
  206. //c -= safe_div(B * c, (B + A * (2 * c - a - b)), 1 + c - a);
  207. c -= safe_div(T(fa+(B+A*(c-b))*(c-a)), T(B + A * (2 * c - a - b)), T(1 + c - a));
  208. }
  209. if((c <= a) || (c >= b))
  210. {
  211. // Oops, failure, try a secant step:
  212. c = secant_interpolate(a, b, fa, fb);
  213. }
  214. return c;
  215. }
  216. template <class T>
  217. T cubic_interpolate(const T& a, const T& b, const T& d,
  218. const T& e, const T& fa, const T& fb,
  219. const T& fd, const T& fe)
  220. {
  221. //
  222. // Uses inverse cubic interpolation of f(x) at points
  223. // [a,b,d,e] to obtain an approximate root of f(x).
  224. // Points d and e lie outside the interval [a,b]
  225. // and are the third and forth best approximations
  226. // to the root that we have found so far.
  227. //
  228. // Note: this does not guarentee to find a root
  229. // inside [a, b], so we fall back to quadratic
  230. // interpolation in case of an erroneous result.
  231. //
  232. BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b
  233. << " d = " << d << " e = " << e << " fa = " << fa << " fb = " << fb
  234. << " fd = " << fd << " fe = " << fe);
  235. T q11 = (d - e) * fd / (fe - fd);
  236. T q21 = (b - d) * fb / (fd - fb);
  237. T q31 = (a - b) * fa / (fb - fa);
  238. T d21 = (b - d) * fd / (fd - fb);
  239. T d31 = (a - b) * fb / (fb - fa);
  240. BOOST_MATH_INSTRUMENT_CODE(
  241. "q11 = " << q11 << " q21 = " << q21 << " q31 = " << q31
  242. << " d21 = " << d21 << " d31 = " << d31);
  243. T q22 = (d21 - q11) * fb / (fe - fb);
  244. T q32 = (d31 - q21) * fa / (fd - fa);
  245. T d32 = (d31 - q21) * fd / (fd - fa);
  246. T q33 = (d32 - q22) * fa / (fe - fa);
  247. T c = q31 + q32 + q33 + a;
  248. BOOST_MATH_INSTRUMENT_CODE(
  249. "q22 = " << q22 << " q32 = " << q32 << " d32 = " << d32
  250. << " q33 = " << q33 << " c = " << c);
  251. if((c <= a) || (c >= b))
  252. {
  253. // Out of bounds step, fall back to quadratic interpolation:
  254. c = quadratic_interpolate(a, b, d, fa, fb, fd, 3);
  255. BOOST_MATH_INSTRUMENT_CODE(
  256. "Out of bounds interpolation, falling back to quadratic interpolation. c = " << c);
  257. }
  258. return c;
  259. }
  260. } // namespace detail
  261. template <class F, class T, class Tol, class Policy>
  262. std::pair<T, T> toms748_solve(F f, const T& ax, const T& bx, const T& fax, const T& fbx, Tol tol, boost::uintmax_t& max_iter, const Policy& pol)
  263. {
  264. //
  265. // Main entry point and logic for Toms Algorithm 748
  266. // root finder.
  267. //
  268. BOOST_MATH_STD_USING // For ADL of std math functions
  269. static const char* function = "boost::math::tools::toms748_solve<%1%>";
  270. boost::uintmax_t count = max_iter;
  271. T a, b, fa, fb, c, u, fu, a0, b0, d, fd, e, fe;
  272. static const T mu = 0.5f;
  273. // initialise a, b and fa, fb:
  274. a = ax;
  275. b = bx;
  276. if(a >= b)
  277. policies::raise_domain_error(
  278. function,
  279. "Parameters a and b out of order: a=%1%", a, pol);
  280. fa = fax;
  281. fb = fbx;
  282. if(tol(a, b) || (fa == 0) || (fb == 0))
  283. {
  284. max_iter = 0;
  285. if(fa == 0)
  286. b = a;
  287. else if(fb == 0)
  288. a = b;
  289. return std::make_pair(a, b);
  290. }
  291. if(boost::math::sign(fa) * boost::math::sign(fb) > 0)
  292. policies::raise_domain_error(
  293. function,
  294. "Parameters a and b do not bracket the root: a=%1%", a, pol);
  295. // dummy value for fd, e and fe:
  296. fe = e = fd = 1e5F;
  297. if(fa != 0)
  298. {
  299. //
  300. // On the first step we take a secant step:
  301. //
  302. c = detail::secant_interpolate(a, b, fa, fb);
  303. detail::bracket(f, a, b, c, fa, fb, d, fd);
  304. --count;
  305. BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b);
  306. if(count && (fa != 0) && !tol(a, b))
  307. {
  308. //
  309. // On the second step we take a quadratic interpolation:
  310. //
  311. c = detail::quadratic_interpolate(a, b, d, fa, fb, fd, 2);
  312. e = d;
  313. fe = fd;
  314. detail::bracket(f, a, b, c, fa, fb, d, fd);
  315. --count;
  316. BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b);
  317. }
  318. }
  319. while(count && (fa != 0) && !tol(a, b))
  320. {
  321. // save our brackets:
  322. a0 = a;
  323. b0 = b;
  324. //
  325. // Starting with the third step taken
  326. // we can use either quadratic or cubic interpolation.
  327. // Cubic interpolation requires that all four function values
  328. // fa, fb, fd, and fe are distinct, should that not be the case
  329. // then variable prof will get set to true, and we'll end up
  330. // taking a quadratic step instead.
  331. //
  332. T min_diff = tools::min_value<T>() * 32;
  333. bool prof = (fabs(fa - fb) < min_diff) || (fabs(fa - fd) < min_diff) || (fabs(fa - fe) < min_diff) || (fabs(fb - fd) < min_diff) || (fabs(fb - fe) < min_diff) || (fabs(fd - fe) < min_diff);
  334. if(prof)
  335. {
  336. c = detail::quadratic_interpolate(a, b, d, fa, fb, fd, 2);
  337. BOOST_MATH_INSTRUMENT_CODE("Can't take cubic step!!!!");
  338. }
  339. else
  340. {
  341. c = detail::cubic_interpolate(a, b, d, e, fa, fb, fd, fe);
  342. }
  343. //
  344. // re-bracket, and check for termination:
  345. //
  346. e = d;
  347. fe = fd;
  348. detail::bracket(f, a, b, c, fa, fb, d, fd);
  349. if((0 == --count) || (fa == 0) || tol(a, b))
  350. break;
  351. BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b);
  352. //
  353. // Now another interpolated step:
  354. //
  355. prof = (fabs(fa - fb) < min_diff) || (fabs(fa - fd) < min_diff) || (fabs(fa - fe) < min_diff) || (fabs(fb - fd) < min_diff) || (fabs(fb - fe) < min_diff) || (fabs(fd - fe) < min_diff);
  356. if(prof)
  357. {
  358. c = detail::quadratic_interpolate(a, b, d, fa, fb, fd, 3);
  359. BOOST_MATH_INSTRUMENT_CODE("Can't take cubic step!!!!");
  360. }
  361. else
  362. {
  363. c = detail::cubic_interpolate(a, b, d, e, fa, fb, fd, fe);
  364. }
  365. //
  366. // Bracket again, and check termination condition, update e:
  367. //
  368. detail::bracket(f, a, b, c, fa, fb, d, fd);
  369. if((0 == --count) || (fa == 0) || tol(a, b))
  370. break;
  371. BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b);
  372. //
  373. // Now we take a double-length secant step:
  374. //
  375. if(fabs(fa) < fabs(fb))
  376. {
  377. u = a;
  378. fu = fa;
  379. }
  380. else
  381. {
  382. u = b;
  383. fu = fb;
  384. }
  385. c = u - 2 * (fu / (fb - fa)) * (b - a);
  386. if(fabs(c - u) > (b - a) / 2)
  387. {
  388. c = a + (b - a) / 2;
  389. }
  390. //
  391. // Bracket again, and check termination condition:
  392. //
  393. e = d;
  394. fe = fd;
  395. detail::bracket(f, a, b, c, fa, fb, d, fd);
  396. BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b);
  397. BOOST_MATH_INSTRUMENT_CODE(" tol = " << T((fabs(a) - fabs(b)) / fabs(a)));
  398. if((0 == --count) || (fa == 0) || tol(a, b))
  399. break;
  400. //
  401. // And finally... check to see if an additional bisection step is
  402. // to be taken, we do this if we're not converging fast enough:
  403. //
  404. if((b - a) < mu * (b0 - a0))
  405. continue;
  406. //
  407. // bracket again on a bisection:
  408. //
  409. e = d;
  410. fe = fd;
  411. detail::bracket(f, a, b, T(a + (b - a) / 2), fa, fb, d, fd);
  412. --count;
  413. BOOST_MATH_INSTRUMENT_CODE("Not converging: Taking a bisection!!!!");
  414. BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b);
  415. } // while loop
  416. max_iter -= count;
  417. if(fa == 0)
  418. {
  419. b = a;
  420. }
  421. else if(fb == 0)
  422. {
  423. a = b;
  424. }
  425. return std::make_pair(a, b);
  426. }
  427. template <class F, class T, class Tol>
  428. inline std::pair<T, T> toms748_solve(F f, const T& ax, const T& bx, const T& fax, const T& fbx, Tol tol, boost::uintmax_t& max_iter)
  429. {
  430. return toms748_solve(f, ax, bx, fax, fbx, tol, max_iter, policies::policy<>());
  431. }
  432. template <class F, class T, class Tol, class Policy>
  433. inline std::pair<T, T> toms748_solve(F f, const T& ax, const T& bx, Tol tol, boost::uintmax_t& max_iter, const Policy& pol)
  434. {
  435. max_iter -= 2;
  436. std::pair<T, T> r = toms748_solve(f, ax, bx, f(ax), f(bx), tol, max_iter, pol);
  437. max_iter += 2;
  438. return r;
  439. }
  440. template <class F, class T, class Tol>
  441. inline std::pair<T, T> toms748_solve(F f, const T& ax, const T& bx, Tol tol, boost::uintmax_t& max_iter)
  442. {
  443. return toms748_solve(f, ax, bx, tol, max_iter, policies::policy<>());
  444. }
  445. template <class F, class T, class Tol, class Policy>
  446. std::pair<T, T> bracket_and_solve_root(F f, const T& guess, T factor, bool rising, Tol tol, boost::uintmax_t& max_iter, const Policy& pol)
  447. {
  448. BOOST_MATH_STD_USING
  449. static const char* function = "boost::math::tools::bracket_and_solve_root<%1%>";
  450. //
  451. // Set up inital brackets:
  452. //
  453. T a = guess;
  454. T b = a;
  455. T fa = f(a);
  456. T fb = fa;
  457. //
  458. // Set up invocation count:
  459. //
  460. boost::uintmax_t count = max_iter - 1;
  461. if((fa < 0) == (guess < 0 ? !rising : rising))
  462. {
  463. //
  464. // Zero is to the right of b, so walk upwards
  465. // until we find it:
  466. //
  467. while((boost::math::sign)(fb) == (boost::math::sign)(fa))
  468. {
  469. if(count == 0)
  470. policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", b, pol);
  471. //
  472. // Heuristic: every 20 iterations we double the growth factor in case the
  473. // initial guess was *really* bad !
  474. //
  475. if((max_iter - count) % 20 == 0)
  476. factor *= 2;
  477. //
  478. // Now go ahead and move our guess by "factor":
  479. //
  480. a = b;
  481. fa = fb;
  482. b *= factor;
  483. fb = f(b);
  484. --count;
  485. BOOST_MATH_INSTRUMENT_CODE("a = " << a << " b = " << b << " fa = " << fa << " fb = " << fb << " count = " << count);
  486. }
  487. }
  488. else
  489. {
  490. //
  491. // Zero is to the left of a, so walk downwards
  492. // until we find it:
  493. //
  494. while((boost::math::sign)(fb) == (boost::math::sign)(fa))
  495. {
  496. if(fabs(a) < tools::min_value<T>())
  497. {
  498. // Escape route just in case the answer is zero!
  499. max_iter -= count;
  500. max_iter += 1;
  501. return a > 0 ? std::make_pair(T(0), T(a)) : std::make_pair(T(a), T(0));
  502. }
  503. if(count == 0)
  504. policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", a, pol);
  505. //
  506. // Heuristic: every 20 iterations we double the growth factor in case the
  507. // initial guess was *really* bad !
  508. //
  509. if((max_iter - count) % 20 == 0)
  510. factor *= 2;
  511. //
  512. // Now go ahead and move are guess by "factor":
  513. //
  514. b = a;
  515. fb = fa;
  516. a /= factor;
  517. fa = f(a);
  518. --count;
  519. BOOST_MATH_INSTRUMENT_CODE("a = " << a << " b = " << b << " fa = " << fa << " fb = " << fb << " count = " << count);
  520. }
  521. }
  522. max_iter -= count;
  523. max_iter += 1;
  524. std::pair<T, T> r = toms748_solve(
  525. f,
  526. (a < 0 ? b : a),
  527. (a < 0 ? a : b),
  528. (a < 0 ? fb : fa),
  529. (a < 0 ? fa : fb),
  530. tol,
  531. count,
  532. pol);
  533. max_iter += count;
  534. BOOST_MATH_INSTRUMENT_CODE("max_iter = " << max_iter << " count = " << count);
  535. return r;
  536. }
  537. template <class F, class T, class Tol>
  538. inline std::pair<T, T> bracket_and_solve_root(F f, const T& guess, const T& factor, bool rising, Tol tol, boost::uintmax_t& max_iter)
  539. {
  540. return bracket_and_solve_root(f, guess, factor, rising, tol, max_iter, policies::policy<>());
  541. }
  542. } // namespace tools
  543. } // namespace math
  544. } // namespace boost
  545. #endif // BOOST_MATH_TOOLS_SOLVE_ROOT_HPP