toms748_solve.hpp 18 KB

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