toms748_solve.hpp 19 KB

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