roots.hpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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_NEWTON_SOLVER_HPP
  6. #define BOOST_MATH_TOOLS_NEWTON_SOLVER_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/tools/complex.hpp> // test for multiprecision types.
  11. #include <iostream>
  12. #include <utility>
  13. #include <boost/config/no_tr1/cmath.hpp>
  14. #include <stdexcept>
  15. #include <boost/math/tools/config.hpp>
  16. #include <boost/cstdint.hpp>
  17. #include <boost/assert.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #ifdef BOOST_MSVC
  20. #pragma warning(push)
  21. #pragma warning(disable: 4512)
  22. #endif
  23. #include <boost/math/tools/tuple.hpp>
  24. #ifdef BOOST_MSVC
  25. #pragma warning(pop)
  26. #endif
  27. #include <boost/math/special_functions/sign.hpp>
  28. #include <boost/math/special_functions/next.hpp>
  29. #include <boost/math/tools/toms748_solve.hpp>
  30. #include <boost/math/policies/error_handling.hpp>
  31. namespace boost {
  32. namespace math {
  33. namespace tools {
  34. namespace detail {
  35. namespace dummy {
  36. template<int n, class T>
  37. typename T::value_type get(const T&) BOOST_MATH_NOEXCEPT(T);
  38. }
  39. template <class Tuple, class T>
  40. void unpack_tuple(const Tuple& t, T& a, T& b) BOOST_MATH_NOEXCEPT(T)
  41. {
  42. using dummy::get;
  43. // Use ADL to find the right overload for get:
  44. a = get<0>(t);
  45. b = get<1>(t);
  46. }
  47. template <class Tuple, class T>
  48. void unpack_tuple(const Tuple& t, T& a, T& b, T& c) BOOST_MATH_NOEXCEPT(T)
  49. {
  50. using dummy::get;
  51. // Use ADL to find the right overload for get:
  52. a = get<0>(t);
  53. b = get<1>(t);
  54. c = get<2>(t);
  55. }
  56. template <class Tuple, class T>
  57. inline void unpack_0(const Tuple& t, T& val) BOOST_MATH_NOEXCEPT(T)
  58. {
  59. using dummy::get;
  60. // Rely on ADL to find the correct overload of get:
  61. val = get<0>(t);
  62. }
  63. template <class T, class U, class V>
  64. inline void unpack_tuple(const std::pair<T, U>& p, V& a, V& b) BOOST_MATH_NOEXCEPT(T)
  65. {
  66. a = p.first;
  67. b = p.second;
  68. }
  69. template <class T, class U, class V>
  70. inline void unpack_0(const std::pair<T, U>& p, V& a) BOOST_MATH_NOEXCEPT(T)
  71. {
  72. a = p.first;
  73. }
  74. template <class F, class T>
  75. void handle_zero_derivative(F f,
  76. T& last_f0,
  77. const T& f0,
  78. T& delta,
  79. T& result,
  80. T& guess,
  81. const T& min,
  82. const T& max) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  83. {
  84. if (last_f0 == 0)
  85. {
  86. // this must be the first iteration, pretend that we had a
  87. // previous one at either min or max:
  88. if (result == min)
  89. {
  90. guess = max;
  91. }
  92. else
  93. {
  94. guess = min;
  95. }
  96. unpack_0(f(guess), last_f0);
  97. delta = guess - result;
  98. }
  99. if (sign(last_f0) * sign(f0) < 0)
  100. {
  101. // we've crossed over so move in opposite direction to last step:
  102. if (delta < 0)
  103. {
  104. delta = (result - min) / 2;
  105. }
  106. else
  107. {
  108. delta = (result - max) / 2;
  109. }
  110. }
  111. else
  112. {
  113. // move in same direction as last step:
  114. if (delta < 0)
  115. {
  116. delta = (result - max) / 2;
  117. }
  118. else
  119. {
  120. delta = (result - min) / 2;
  121. }
  122. }
  123. }
  124. } // namespace
  125. template <class F, class T, class Tol, class Policy>
  126. std::pair<T, T> bisect(F f, T min, T max, Tol tol, boost::uintmax_t& max_iter, const Policy& pol) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<Policy>::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  127. {
  128. T fmin = f(min);
  129. T fmax = f(max);
  130. if (fmin == 0)
  131. {
  132. max_iter = 2;
  133. return std::make_pair(min, min);
  134. }
  135. if (fmax == 0)
  136. {
  137. max_iter = 2;
  138. return std::make_pair(max, max);
  139. }
  140. //
  141. // Error checking:
  142. //
  143. static const char* function = "boost::math::tools::bisect<%1%>";
  144. if (min >= max)
  145. {
  146. return boost::math::detail::pair_from_single(policies::raise_evaluation_error(function,
  147. "Arguments in wrong order in boost::math::tools::bisect (first arg=%1%)", min, pol));
  148. }
  149. if (fmin * fmax >= 0)
  150. {
  151. return boost::math::detail::pair_from_single(policies::raise_evaluation_error(function,
  152. "No change of sign in boost::math::tools::bisect, either there is no root to find, or there are multiple roots in the interval (f(min) = %1%).", fmin, pol));
  153. }
  154. //
  155. // Three function invocations so far:
  156. //
  157. boost::uintmax_t count = max_iter;
  158. if (count < 3)
  159. count = 0;
  160. else
  161. count -= 3;
  162. while (count && (0 == tol(min, max)))
  163. {
  164. T mid = (min + max) / 2;
  165. T fmid = f(mid);
  166. if ((mid == max) || (mid == min))
  167. break;
  168. if (fmid == 0)
  169. {
  170. min = max = mid;
  171. break;
  172. }
  173. else if (sign(fmid) * sign(fmin) < 0)
  174. {
  175. max = mid;
  176. }
  177. else
  178. {
  179. min = mid;
  180. fmin = fmid;
  181. }
  182. --count;
  183. }
  184. max_iter -= count;
  185. #ifdef BOOST_MATH_INSTRUMENT
  186. std::cout << "Bisection iteration, final count = " << max_iter << std::endl;
  187. static boost::uintmax_t max_count = 0;
  188. if (max_iter > max_count)
  189. {
  190. max_count = max_iter;
  191. std::cout << "Maximum iterations: " << max_iter << std::endl;
  192. }
  193. #endif
  194. return std::make_pair(min, max);
  195. }
  196. template <class F, class T, class Tol>
  197. inline std::pair<T, T> bisect(F f, T min, T max, Tol tol, boost::uintmax_t& max_iter) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  198. {
  199. return bisect(f, min, max, tol, max_iter, policies::policy<>());
  200. }
  201. template <class F, class T, class Tol>
  202. inline std::pair<T, T> bisect(F f, T min, T max, Tol tol) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  203. {
  204. boost::uintmax_t m = (std::numeric_limits<boost::uintmax_t>::max)();
  205. return bisect(f, min, max, tol, m, policies::policy<>());
  206. }
  207. template <class F, class T>
  208. T newton_raphson_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  209. {
  210. BOOST_MATH_STD_USING
  211. static const char* function = "boost::math::tools::newton_raphson_iterate<%1%>";
  212. if (min >= max)
  213. {
  214. return policies::raise_evaluation_error(function, "Range arguments in wrong order in boost::math::tools::newton_raphson_iterate(first arg=%1%)", min, boost::math::policies::policy<>());
  215. }
  216. T f0(0), f1, last_f0(0);
  217. T result = guess;
  218. T factor = static_cast<T>(ldexp(1.0, 1 - digits));
  219. T delta = tools::max_value<T>();
  220. T delta1 = tools::max_value<T>();
  221. T delta2 = tools::max_value<T>();
  222. //
  223. // We use these to sanity check that we do actually bracket a root,
  224. // we update these to the function value when we update the endpoints
  225. // of the range. Then, provided at some point we update both endpoints
  226. // checking that max_range_f * min_range_f <= 0 verifies there is a root
  227. // to be found somewhere. Note that if there is no root, and we approach
  228. // a local minima, then the derivative will go to zero, and hence the next
  229. // step will jump out of bounds (or at least past the minima), so this
  230. // check *should* happen in pathological cases.
  231. //
  232. T max_range_f = 0;
  233. T min_range_f = 0;
  234. boost::uintmax_t count(max_iter);
  235. #ifdef BOOST_MATH_INSTRUMENT
  236. std::cout << "Newton_raphson_iterate, guess = " << guess << ", min = " << min << ", max = " << max
  237. << ", digits = " << digits << ", max_iter = " << max_iter << std::endl;
  238. #endif
  239. do {
  240. last_f0 = f0;
  241. delta2 = delta1;
  242. delta1 = delta;
  243. detail::unpack_tuple(f(result), f0, f1);
  244. --count;
  245. if (0 == f0)
  246. break;
  247. if (f1 == 0)
  248. {
  249. // Oops zero derivative!!!
  250. #ifdef BOOST_MATH_INSTRUMENT
  251. std::cout << "Newton iteration, zero derivative found!" << std::endl;
  252. #endif
  253. detail::handle_zero_derivative(f, last_f0, f0, delta, result, guess, min, max);
  254. }
  255. else
  256. {
  257. delta = f0 / f1;
  258. }
  259. #ifdef BOOST_MATH_INSTRUMENT
  260. std::cout << "Newton iteration " << max_iter - count << ", delta = " << delta << std::endl;
  261. #endif
  262. if (fabs(delta * 2) > fabs(delta2))
  263. {
  264. // Last two steps haven't converged.
  265. T shift = (delta > 0) ? (result - min) / 2 : (result - max) / 2;
  266. if ((result != 0) && (fabs(shift) > fabs(result)))
  267. {
  268. delta = sign(delta) * fabs(result) * 1.1f; // Protect against huge jumps!
  269. //delta = sign(delta) * result; // Protect against huge jumps! Failed for negative result. https://github.com/boostorg/math/issues/216
  270. }
  271. else
  272. delta = shift;
  273. // reset delta1/2 so we don't take this branch next time round:
  274. delta1 = 3 * delta;
  275. delta2 = 3 * delta;
  276. }
  277. guess = result;
  278. result -= delta;
  279. if (result <= min)
  280. {
  281. delta = 0.5F * (guess - min);
  282. result = guess - delta;
  283. if ((result == min) || (result == max))
  284. break;
  285. }
  286. else if (result >= max)
  287. {
  288. delta = 0.5F * (guess - max);
  289. result = guess - delta;
  290. if ((result == min) || (result == max))
  291. break;
  292. }
  293. // Update brackets:
  294. if (delta > 0)
  295. {
  296. max = guess;
  297. max_range_f = f0;
  298. }
  299. else
  300. {
  301. min = guess;
  302. min_range_f = f0;
  303. }
  304. //
  305. // Sanity check that we bracket the root:
  306. //
  307. if (max_range_f * min_range_f > 0)
  308. {
  309. return policies::raise_evaluation_error(function, "There appears to be no root to be found in boost::math::tools::newton_raphson_iterate, perhaps we have a local minima near current best guess of %1%", guess, boost::math::policies::policy<>());
  310. }
  311. }while(count && (fabs(result * factor) < fabs(delta)));
  312. max_iter -= count;
  313. #ifdef BOOST_MATH_INSTRUMENT
  314. std::cout << "Newton Raphson final iteration count = " << max_iter << std::endl;
  315. static boost::uintmax_t max_count = 0;
  316. if (max_iter > max_count)
  317. {
  318. max_count = max_iter;
  319. // std::cout << "Maximum iterations: " << max_iter << std::endl;
  320. // Puzzled what this tells us, so commented out for now?
  321. }
  322. #endif
  323. return result;
  324. }
  325. template <class F, class T>
  326. inline T newton_raphson_iterate(F f, T guess, T min, T max, int digits) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  327. {
  328. boost::uintmax_t m = (std::numeric_limits<boost::uintmax_t>::max)();
  329. return newton_raphson_iterate(f, guess, min, max, digits, m);
  330. }
  331. namespace detail {
  332. struct halley_step
  333. {
  334. template <class T>
  335. static T step(const T& /*x*/, const T& f0, const T& f1, const T& f2) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T))
  336. {
  337. using std::fabs;
  338. T denom = 2 * f0;
  339. T num = 2 * f1 - f0 * (f2 / f1);
  340. T delta;
  341. BOOST_MATH_INSTRUMENT_VARIABLE(denom);
  342. BOOST_MATH_INSTRUMENT_VARIABLE(num);
  343. if ((fabs(num) < 1) && (fabs(denom) >= fabs(num) * tools::max_value<T>()))
  344. {
  345. // possible overflow, use Newton step:
  346. delta = f0 / f1;
  347. }
  348. else
  349. delta = denom / num;
  350. return delta;
  351. }
  352. };
  353. template <class F, class T>
  354. T bracket_root_towards_min(F f, T guess, const T& f0, T& min, T& max, boost::uintmax_t& count) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())));
  355. template <class F, class T>
  356. T bracket_root_towards_max(F f, T guess, const T& f0, T& min, T& max, boost::uintmax_t& count) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  357. {
  358. using std::fabs;
  359. //
  360. // Move guess towards max until we bracket the root, updating min and max as we go:
  361. //
  362. T guess0 = guess;
  363. T multiplier = 2;
  364. T f_current = f0;
  365. if (fabs(min) < fabs(max))
  366. {
  367. while (--count && ((f_current < 0) == (f0 < 0)))
  368. {
  369. min = guess;
  370. guess *= multiplier;
  371. if (guess > max)
  372. {
  373. guess = max;
  374. f_current = -f_current; // There must be a change of sign!
  375. break;
  376. }
  377. multiplier *= 2;
  378. unpack_0(f(guess), f_current);
  379. }
  380. }
  381. else
  382. {
  383. //
  384. // If min and max are negative we have to divide to head towards max:
  385. //
  386. while (--count && ((f_current < 0) == (f0 < 0)))
  387. {
  388. min = guess;
  389. guess /= multiplier;
  390. if (guess > max)
  391. {
  392. guess = max;
  393. f_current = -f_current; // There must be a change of sign!
  394. break;
  395. }
  396. multiplier *= 2;
  397. unpack_0(f(guess), f_current);
  398. }
  399. }
  400. if (count)
  401. {
  402. max = guess;
  403. if (multiplier > 16)
  404. return (guess0 - guess) + bracket_root_towards_min(f, guess, f_current, min, max, count);
  405. }
  406. return guess0 - (max + min) / 2;
  407. }
  408. template <class F, class T>
  409. T bracket_root_towards_min(F f, T guess, const T& f0, T& min, T& max, boost::uintmax_t& count) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  410. {
  411. using std::fabs;
  412. //
  413. // Move guess towards min until we bracket the root, updating min and max as we go:
  414. //
  415. T guess0 = guess;
  416. T multiplier = 2;
  417. T f_current = f0;
  418. if (fabs(min) < fabs(max))
  419. {
  420. while (--count && ((f_current < 0) == (f0 < 0)))
  421. {
  422. max = guess;
  423. guess /= multiplier;
  424. if (guess < min)
  425. {
  426. guess = min;
  427. f_current = -f_current; // There must be a change of sign!
  428. break;
  429. }
  430. multiplier *= 2;
  431. unpack_0(f(guess), f_current);
  432. }
  433. }
  434. else
  435. {
  436. //
  437. // If min and max are negative we have to multiply to head towards min:
  438. //
  439. while (--count && ((f_current < 0) == (f0 < 0)))
  440. {
  441. max = guess;
  442. guess *= multiplier;
  443. if (guess < min)
  444. {
  445. guess = min;
  446. f_current = -f_current; // There must be a change of sign!
  447. break;
  448. }
  449. multiplier *= 2;
  450. unpack_0(f(guess), f_current);
  451. }
  452. }
  453. if (count)
  454. {
  455. min = guess;
  456. if (multiplier > 16)
  457. return (guess0 - guess) + bracket_root_towards_max(f, guess, f_current, min, max, count);
  458. }
  459. return guess0 - (max + min) / 2;
  460. }
  461. template <class Stepper, class F, class T>
  462. T second_order_root_finder(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  463. {
  464. BOOST_MATH_STD_USING
  465. #ifdef BOOST_MATH_INSTRUMENT
  466. std::cout << "Second order root iteration, guess = " << guess << ", min = " << min << ", max = " << max
  467. << ", digits = " << digits << ", max_iter = " << max_iter << std::endl;
  468. #endif
  469. static const char* function = "boost::math::tools::halley_iterate<%1%>";
  470. if (min >= max)
  471. {
  472. return policies::raise_evaluation_error(function, "Range arguments in wrong order in boost::math::tools::halley_iterate(first arg=%1%)", min, boost::math::policies::policy<>());
  473. }
  474. T f0(0), f1, f2;
  475. T result = guess;
  476. T factor = ldexp(static_cast<T>(1.0), 1 - digits);
  477. T delta = (std::max)(T(10000000 * guess), T(10000000)); // arbitarily large delta
  478. T last_f0 = 0;
  479. T delta1 = delta;
  480. T delta2 = delta;
  481. bool out_of_bounds_sentry = false;
  482. #ifdef BOOST_MATH_INSTRUMENT
  483. std::cout << "Second order root iteration, limit = " << factor << std::endl;
  484. #endif
  485. //
  486. // We use these to sanity check that we do actually bracket a root,
  487. // we update these to the function value when we update the endpoints
  488. // of the range. Then, provided at some point we update both endpoints
  489. // checking that max_range_f * min_range_f <= 0 verifies there is a root
  490. // to be found somewhere. Note that if there is no root, and we approach
  491. // a local minima, then the derivative will go to zero, and hence the next
  492. // step will jump out of bounds (or at least past the minima), so this
  493. // check *should* happen in pathological cases.
  494. //
  495. T max_range_f = 0;
  496. T min_range_f = 0;
  497. boost::uintmax_t count(max_iter);
  498. do {
  499. last_f0 = f0;
  500. delta2 = delta1;
  501. delta1 = delta;
  502. detail::unpack_tuple(f(result), f0, f1, f2);
  503. --count;
  504. BOOST_MATH_INSTRUMENT_VARIABLE(f0);
  505. BOOST_MATH_INSTRUMENT_VARIABLE(f1);
  506. BOOST_MATH_INSTRUMENT_VARIABLE(f2);
  507. if (0 == f0)
  508. break;
  509. if (f1 == 0)
  510. {
  511. // Oops zero derivative!!!
  512. #ifdef BOOST_MATH_INSTRUMENT
  513. std::cout << "Second order root iteration, zero derivative found!" << std::endl;
  514. #endif
  515. detail::handle_zero_derivative(f, last_f0, f0, delta, result, guess, min, max);
  516. }
  517. else
  518. {
  519. if (f2 != 0)
  520. {
  521. delta = Stepper::step(result, f0, f1, f2);
  522. if (delta * f1 / f0 < 0)
  523. {
  524. // Oh dear, we have a problem as Newton and Halley steps
  525. // disagree about which way we should move. Probably
  526. // there is cancelation error in the calculation of the
  527. // Halley step, or else the derivatives are so small
  528. // that their values are basically trash. We will move
  529. // in the direction indicated by a Newton step, but
  530. // by no more than twice the current guess value, otherwise
  531. // we can jump way out of bounds if we're not careful.
  532. // See https://svn.boost.org/trac/boost/ticket/8314.
  533. delta = f0 / f1;
  534. if (fabs(delta) > 2 * fabs(guess))
  535. delta = (delta < 0 ? -1 : 1) * 2 * fabs(guess);
  536. }
  537. }
  538. else
  539. delta = f0 / f1;
  540. }
  541. #ifdef BOOST_MATH_INSTRUMENT
  542. std::cout << "Second order root iteration, delta = " << delta << std::endl;
  543. #endif
  544. T convergence = fabs(delta / delta2);
  545. if ((convergence > 0.8) && (convergence < 2))
  546. {
  547. // last two steps haven't converged.
  548. delta = (delta > 0) ? (result - min) / 2 : (result - max) / 2;
  549. if ((result != 0) && (fabs(delta) > result))
  550. delta = sign(delta) * fabs(result) * 0.9f; // protect against huge jumps!
  551. // reset delta2 so that this branch will *not* be taken on the
  552. // next iteration:
  553. delta2 = delta * 3;
  554. delta1 = delta * 3;
  555. BOOST_MATH_INSTRUMENT_VARIABLE(delta);
  556. }
  557. guess = result;
  558. result -= delta;
  559. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  560. // check for out of bounds step:
  561. if (result < min)
  562. {
  563. T diff = ((fabs(min) < 1) && (fabs(result) > 1) && (tools::max_value<T>() / fabs(result) < fabs(min)))
  564. ? T(1000)
  565. : (fabs(min) < 1) && (fabs(tools::max_value<T>() * min) < fabs(result))
  566. ? ((min < 0) != (result < 0)) ? -tools::max_value<T>() : tools::max_value<T>() : T(result / min);
  567. if (fabs(diff) < 1)
  568. diff = 1 / diff;
  569. if (!out_of_bounds_sentry && (diff > 0) && (diff < 3))
  570. {
  571. // Only a small out of bounds step, lets assume that the result
  572. // is probably approximately at min:
  573. delta = 0.99f * (guess - min);
  574. result = guess - delta;
  575. out_of_bounds_sentry = true; // only take this branch once!
  576. }
  577. else
  578. {
  579. if (fabs(float_distance(min, max)) < 2)
  580. {
  581. result = guess = (min + max) / 2;
  582. break;
  583. }
  584. delta = bracket_root_towards_min(f, guess, f0, min, max, count);
  585. result = guess - delta;
  586. guess = min;
  587. continue;
  588. }
  589. }
  590. else if (result > max)
  591. {
  592. T diff = ((fabs(max) < 1) && (fabs(result) > 1) && (tools::max_value<T>() / fabs(result) < fabs(max))) ? T(1000) : T(result / max);
  593. if (fabs(diff) < 1)
  594. diff = 1 / diff;
  595. if (!out_of_bounds_sentry && (diff > 0) && (diff < 3))
  596. {
  597. // Only a small out of bounds step, lets assume that the result
  598. // is probably approximately at min:
  599. delta = 0.99f * (guess - max);
  600. result = guess - delta;
  601. out_of_bounds_sentry = true; // only take this branch once!
  602. }
  603. else
  604. {
  605. if (fabs(float_distance(min, max)) < 2)
  606. {
  607. result = guess = (min + max) / 2;
  608. break;
  609. }
  610. delta = bracket_root_towards_max(f, guess, f0, min, max, count);
  611. result = guess - delta;
  612. guess = min;
  613. continue;
  614. }
  615. }
  616. // update brackets:
  617. if (delta > 0)
  618. {
  619. max = guess;
  620. max_range_f = f0;
  621. }
  622. else
  623. {
  624. min = guess;
  625. min_range_f = f0;
  626. }
  627. //
  628. // Sanity check that we bracket the root:
  629. //
  630. if (max_range_f * min_range_f > 0)
  631. {
  632. return policies::raise_evaluation_error(function, "There appears to be no root to be found in boost::math::tools::newton_raphson_iterate, perhaps we have a local minima near current best guess of %1%", guess, boost::math::policies::policy<>());
  633. }
  634. } while(count && (fabs(result * factor) < fabs(delta)));
  635. max_iter -= count;
  636. #ifdef BOOST_MATH_INSTRUMENT
  637. std::cout << "Second order root finder, final iteration count = " << max_iter << std::endl;
  638. #endif
  639. return result;
  640. }
  641. } // T second_order_root_finder
  642. template <class F, class T>
  643. T halley_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  644. {
  645. return detail::second_order_root_finder<detail::halley_step>(f, guess, min, max, digits, max_iter);
  646. }
  647. template <class F, class T>
  648. inline T halley_iterate(F f, T guess, T min, T max, int digits) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  649. {
  650. boost::uintmax_t m = (std::numeric_limits<boost::uintmax_t>::max)();
  651. return halley_iterate(f, guess, min, max, digits, m);
  652. }
  653. namespace detail {
  654. struct schroder_stepper
  655. {
  656. template <class T>
  657. static T step(const T& x, const T& f0, const T& f1, const T& f2) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T))
  658. {
  659. using std::fabs;
  660. T ratio = f0 / f1;
  661. T delta;
  662. if ((x != 0) && (fabs(ratio / x) < 0.1))
  663. {
  664. delta = ratio + (f2 / (2 * f1)) * ratio * ratio;
  665. // check second derivative doesn't over compensate:
  666. if (delta * ratio < 0)
  667. delta = ratio;
  668. }
  669. else
  670. delta = ratio; // fall back to Newton iteration.
  671. return delta;
  672. }
  673. };
  674. }
  675. template <class F, class T>
  676. T schroder_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  677. {
  678. return detail::second_order_root_finder<detail::schroder_stepper>(f, guess, min, max, digits, max_iter);
  679. }
  680. template <class F, class T>
  681. inline T schroder_iterate(F f, T guess, T min, T max, int digits) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  682. {
  683. boost::uintmax_t m = (std::numeric_limits<boost::uintmax_t>::max)();
  684. return schroder_iterate(f, guess, min, max, digits, m);
  685. }
  686. //
  687. // These two are the old spelling of this function, retained for backwards compatibity just in case:
  688. //
  689. template <class F, class T>
  690. T schroeder_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  691. {
  692. return detail::second_order_root_finder<detail::schroder_stepper>(f, guess, min, max, digits, max_iter);
  693. }
  694. template <class F, class T>
  695. inline T schroeder_iterate(F f, T guess, T min, T max, int digits) BOOST_NOEXCEPT_IF(policies::is_noexcept_error_policy<policies::policy<> >::value&& BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
  696. {
  697. boost::uintmax_t m = (std::numeric_limits<boost::uintmax_t>::max)();
  698. return schroder_iterate(f, guess, min, max, digits, m);
  699. }
  700. #ifndef BOOST_NO_CXX11_AUTO_DECLARATIONS
  701. /*
  702. * Why do we set the default maximum number of iterations to the number of digits in the type?
  703. * Because for double roots, the number of digits increases linearly with the number of iterations,
  704. * so this default should recover full precision even in this somewhat pathological case.
  705. * For isolated roots, the problem is so rapidly convergent that this doesn't matter at all.
  706. */
  707. template<class Complex, class F>
  708. Complex complex_newton(F g, Complex guess, int max_iterations = std::numeric_limits<typename Complex::value_type>::digits)
  709. {
  710. typedef typename Complex::value_type Real;
  711. using std::norm;
  712. using std::abs;
  713. using std::max;
  714. // z0, z1, and z2 cannot be the same, in case we immediately need to resort to Muller's Method:
  715. Complex z0 = guess + Complex(1, 0);
  716. Complex z1 = guess + Complex(0, 1);
  717. Complex z2 = guess;
  718. do {
  719. auto pair = g(z2);
  720. if (norm(pair.second) == 0)
  721. {
  722. // Muller's method. Notation follows Numerical Recipes, 9.5.2:
  723. Complex q = (z2 - z1) / (z1 - z0);
  724. auto P0 = g(z0);
  725. auto P1 = g(z1);
  726. Complex qp1 = static_cast<Complex>(1) + q;
  727. Complex A = q * (pair.first - qp1 * P1.first + q * P0.first);
  728. Complex B = (static_cast<Complex>(2) * q + static_cast<Complex>(1)) * pair.first - qp1 * qp1 * P1.first + q * q * P0.first;
  729. Complex C = qp1 * pair.first;
  730. Complex rad = sqrt(B * B - static_cast<Complex>(4) * A * C);
  731. Complex denom1 = B + rad;
  732. Complex denom2 = B - rad;
  733. Complex correction = (z1 - z2) * static_cast<Complex>(2) * C;
  734. if (norm(denom1) > norm(denom2))
  735. {
  736. correction /= denom1;
  737. }
  738. else
  739. {
  740. correction /= denom2;
  741. }
  742. z0 = z1;
  743. z1 = z2;
  744. z2 = z2 + correction;
  745. }
  746. else
  747. {
  748. z0 = z1;
  749. z1 = z2;
  750. z2 = z2 - (pair.first / pair.second);
  751. }
  752. // See: https://math.stackexchange.com/questions/3017766/constructing-newton-iteration-converging-to-non-root
  753. // If f' is continuous, then convergence of x_n -> x* implies f(x*) = 0.
  754. // This condition approximates this convergence condition by requiring three consecutive iterates to be clustered.
  755. Real tol = (max)(abs(z2) * std::numeric_limits<Real>::epsilon(), std::numeric_limits<Real>::epsilon());
  756. bool real_close = abs(z0.real() - z1.real()) < tol && abs(z0.real() - z2.real()) < tol && abs(z1.real() - z2.real()) < tol;
  757. bool imag_close = abs(z0.imag() - z1.imag()) < tol && abs(z0.imag() - z2.imag()) < tol && abs(z1.imag() - z2.imag()) < tol;
  758. if (real_close && imag_close)
  759. {
  760. return z2;
  761. }
  762. } while (max_iterations--);
  763. // The idea is that if we can get abs(f) < eps, we should, but if we go through all these iterations
  764. // and abs(f) < sqrt(eps), then roundoff error simply does not allow that we can evaluate f to < eps
  765. // This is somewhat awkward as it isn't scale invariant, but using the Daubechies coefficient example code,
  766. // I found this condition generates correct roots, whereas the scale invariant condition discussed here:
  767. // https://scicomp.stackexchange.com/questions/30597/defining-a-condition-number-and-termination-criteria-for-newtons-method
  768. // allows nonroots to be passed off as roots.
  769. auto pair = g(z2);
  770. if (abs(pair.first) < sqrt(std::numeric_limits<Real>::epsilon()))
  771. {
  772. return z2;
  773. }
  774. return { std::numeric_limits<Real>::quiet_NaN(),
  775. std::numeric_limits<Real>::quiet_NaN() };
  776. }
  777. #endif
  778. #if !defined(BOOST_NO_CXX17_IF_CONSTEXPR)
  779. // https://stackoverflow.com/questions/48979861/numerically-stable-method-for-solving-quadratic-equations/50065711
  780. namespace detail
  781. {
  782. #if defined(BOOST_GNU_STDLIB) && !defined(_GLIBCXX_USE_C99_MATH_TR1)
  783. float fma_workaround(float f) { return ::fmaf(f); }
  784. double fma_workaround(double f) { return ::fma(f); }
  785. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  786. long double fma_workaround(long double f) { return ::fmal(f); }
  787. #endif
  788. #endif
  789. template<class T>
  790. inline T discriminant(T const& a, T const& b, T const& c)
  791. {
  792. T w = 4 * a * c;
  793. #if defined(BOOST_GNU_STDLIB) && !defined(_GLIBCXX_USE_C99_MATH_TR1)
  794. T e = fma_workaround(-c, 4 * a, w);
  795. T f = fma_workaround(b, b, -w);
  796. #else
  797. T e = std::fma(-c, 4 * a, w);
  798. T f = std::fma(b, b, -w);
  799. #endif
  800. return f + e;
  801. }
  802. template<class T>
  803. std::pair<T, T> quadratic_roots_imp(T const& a, T const& b, T const& c)
  804. {
  805. using std::copysign;
  806. using std::sqrt;
  807. if constexpr (std::is_floating_point<T>::value)
  808. {
  809. T nan = std::numeric_limits<T>::quiet_NaN();
  810. if (a == 0)
  811. {
  812. if (b == 0 && c != 0)
  813. {
  814. return std::pair<T, T>(nan, nan);
  815. }
  816. else if (b == 0 && c == 0)
  817. {
  818. return std::pair<T, T>(0, 0);
  819. }
  820. return std::pair<T, T>(-c / b, -c / b);
  821. }
  822. if (b == 0)
  823. {
  824. T x0_sq = -c / a;
  825. if (x0_sq < 0) {
  826. return std::pair<T, T>(nan, nan);
  827. }
  828. T x0 = sqrt(x0_sq);
  829. return std::pair<T, T>(-x0, x0);
  830. }
  831. T discriminant = detail::discriminant(a, b, c);
  832. // Is there a sane way to flush very small negative values to zero?
  833. // If there is I don't know of it.
  834. if (discriminant < 0)
  835. {
  836. return std::pair<T, T>(nan, nan);
  837. }
  838. T q = -(b + copysign(sqrt(discriminant), b)) / T(2);
  839. T x0 = q / a;
  840. T x1 = c / q;
  841. if (x0 < x1)
  842. {
  843. return std::pair<T, T>(x0, x1);
  844. }
  845. return std::pair<T, T>(x1, x0);
  846. }
  847. else if constexpr (boost::math::tools::is_complex_type<T>::value)
  848. {
  849. typename T::value_type nan = std::numeric_limits<typename T::value_type>::quiet_NaN();
  850. if (a.real() == 0 && a.imag() == 0)
  851. {
  852. using std::norm;
  853. if (b.real() == 0 && b.imag() && norm(c) != 0)
  854. {
  855. return std::pair<T, T>({ nan, nan }, { nan, nan });
  856. }
  857. else if (b.real() == 0 && b.imag() && c.real() == 0 && c.imag() == 0)
  858. {
  859. return std::pair<T, T>({ 0,0 }, { 0,0 });
  860. }
  861. return std::pair<T, T>(-c / b, -c / b);
  862. }
  863. if (b.real() == 0 && b.imag() == 0)
  864. {
  865. T x0_sq = -c / a;
  866. T x0 = sqrt(x0_sq);
  867. return std::pair<T, T>(-x0, x0);
  868. }
  869. // There's no fma for complex types:
  870. T discriminant = b * b - T(4) * a * c;
  871. T q = -(b + sqrt(discriminant)) / T(2);
  872. return std::pair<T, T>(q / a, c / q);
  873. }
  874. else // Most likely the type is a boost.multiprecision.
  875. { //There is no fma for multiprecision, and in addition it doesn't seem to be useful, so revert to the naive computation.
  876. T nan = std::numeric_limits<T>::quiet_NaN();
  877. if (a == 0)
  878. {
  879. if (b == 0 && c != 0)
  880. {
  881. return std::pair<T, T>(nan, nan);
  882. }
  883. else if (b == 0 && c == 0)
  884. {
  885. return std::pair<T, T>(0, 0);
  886. }
  887. return std::pair<T, T>(-c / b, -c / b);
  888. }
  889. if (b == 0)
  890. {
  891. T x0_sq = -c / a;
  892. if (x0_sq < 0) {
  893. return std::pair<T, T>(nan, nan);
  894. }
  895. T x0 = sqrt(x0_sq);
  896. return std::pair<T, T>(-x0, x0);
  897. }
  898. T discriminant = b * b - 4 * a * c;
  899. if (discriminant < 0)
  900. {
  901. return std::pair<T, T>(nan, nan);
  902. }
  903. T q = -(b + copysign(sqrt(discriminant), b)) / T(2);
  904. T x0 = q / a;
  905. T x1 = c / q;
  906. if (x0 < x1)
  907. {
  908. return std::pair<T, T>(x0, x1);
  909. }
  910. return std::pair<T, T>(x1, x0);
  911. }
  912. }
  913. } // namespace detail
  914. template<class T1, class T2 = T1, class T3 = T1>
  915. inline std::pair<typename tools::promote_args<T1, T2, T3>::type, typename tools::promote_args<T1, T2, T3>::type> quadratic_roots(T1 const& a, T2 const& b, T3 const& c)
  916. {
  917. typedef typename tools::promote_args<T1, T2, T3>::type value_type;
  918. return detail::quadratic_roots_imp(static_cast<value_type>(a), static_cast<value_type>(b), static_cast<value_type>(c));
  919. }
  920. #endif
  921. } // namespace tools
  922. } // namespace math
  923. } // namespace boost
  924. #endif // BOOST_MATH_TOOLS_NEWTON_SOLVER_HPP