roots.qbk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. [section:roots_deriv Root Finding With Derivatives: Newton-Raphson, Halley & Schr'''ö'''der]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/tools/roots.hpp>
  5. ``
  6. namespace boost { namespace math {
  7. namespace tools { // Note namespace boost::math::tools.
  8. // Newton-Raphson
  9. template <class F, class T>
  10. T newton_raphson_iterate(F f, T guess, T min, T max, int digits);
  11. template <class F, class T>
  12. T newton_raphson_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter);
  13. // Halley
  14. template <class F, class T>
  15. T halley_iterate(F f, T guess, T min, T max, int digits);
  16. template <class F, class T>
  17. T halley_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter);
  18. // Schr'''&#xf6;'''der
  19. template <class F, class T>
  20. T schroder_iterate(F f, T guess, T min, T max, int digits);
  21. template <class F, class T>
  22. T schroder_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter);
  23. template <class F, class Complex>
  24. Complex complex_newton(F f, Complex guess, int max_iterations = std::numeric_limits<typename Complex::value_type>::digits);
  25. template<class T>
  26. auto quadratic_roots(T const & a, T const & b, T const & c);
  27. }}} // namespaces boost::math::tools.
  28. [h4 Description]
  29. These functions all perform iterative root-finding [*using derivatives]:
  30. * `newton_raphson_iterate` performs second-order __newton.
  31. * `halley_iterate` and `schroder_iterate` perform third-order
  32. __halley and __schroder iteration.
  33. * `complex_newton` performs Newton's method on complex-analytic functions.
  34. * `solve_quadratic` solves quadratic equations using various tricks to keep catastrophic cancellation from occurring in computation of the discriminant.
  35. [variablelist Parameters of the real-valued root finding functions
  36. [[F f] [Type F must be a callable function object (or C++ lambda) that accepts one parameter and
  37. returns a __tuple_type:
  38. For second-order iterative method ([@http://en.wikipedia.org/wiki/Newton_Raphson Newton Raphson])
  39. the `tuple` should have [*two] elements containing the evaluation
  40. of the function and its first derivative.
  41. For the third-order methods
  42. ([@http://en.wikipedia.org/wiki/Halley%27s_method Halley] and
  43. Schr'''&#xf6;'''der)
  44. the `tuple` should have [*three] elements containing the evaluation of
  45. the function and its first and second derivatives.]]
  46. [[T guess] [The initial starting value. A good guess is crucial to quick convergence!]]
  47. [[T min] [The minimum possible value for the result, this is used as an initial lower bracket.]]
  48. [[T max] [The maximum possible value for the result, this is used as an initial upper bracket.]]
  49. [[int digits] [The desired number of binary digits precision.]]
  50. [[uintmax_t& max_iter] [An optional maximum number of iterations to perform. On exit, this is updated to the actual number of iterations performed.]]
  51. ]
  52. When using these functions you should note that:
  53. * Default `max_iter = (std::numeric_limits<boost::uintmax_t>::max)()` is effectively 'iterate for ever'.
  54. * They may be very sensitive to the initial guess, typically they converge very rapidly
  55. if the initial guess has two or three decimal digits correct. However convergence
  56. can be no better than __bisect, or in some rare cases, even worse than __bisect if the
  57. initial guess is a long way from the correct value and the derivatives are close to zero.
  58. * These functions include special cases to handle zero first (and second where appropriate)
  59. derivatives, and fall back to __bisect in this case. However, it is helpful
  60. if functor F is defined to return an arbitrarily small value ['of the correct sign] rather
  61. than zero.
  62. * The functions will raise an __evaluation_error if arguments `min` and `max` are the wrong way around
  63. or if they converge to a local minima.
  64. * If the derivative at the current best guess for the result is infinite (or
  65. very close to being infinite) then these functions may terminate prematurely.
  66. A large first derivative leads to a very small next step, triggering the termination
  67. condition. Derivative based iteration may not be appropriate in such cases.
  68. * If the function is 'Really Well Behaved' (is monotonic and has only one root)
  69. the bracket bounds ['min] and ['max] may as well be set to the widest limits
  70. like zero and `numeric_limits<T>::max()`.
  71. *But if the function more complex and may have more than one root or a pole,
  72. the choice of bounds is protection against jumping out to seek the 'wrong' root.
  73. * These functions fall back to __bisect if the next computed step would take the
  74. next value out of bounds. The bounds are updated after each step to ensure this leads
  75. to convergence. However, a good initial guess backed up by asymptotically-tight
  76. bounds will improve performance no end - rather than relying on __bisection.
  77. * The value of ['digits] is crucial to good performance of these functions,
  78. if it is set too high then at best you will get one extra (unnecessary)
  79. iteration, and at worst the last few steps will proceed by __bisection.
  80. Remember that the returned value can never be more accurate than ['f(x)] can be
  81. evaluated, and that if ['f(x)] suffers from cancellation errors as it
  82. tends to zero then the computed steps will be effectively random. The
  83. value of ['digits] should be set so that iteration terminates before this point:
  84. remember that for second and third order methods the number of correct
  85. digits in the result is increasing quite
  86. substantially with each iteration, ['digits] should be set by experiment so that the final
  87. iteration just takes the next value into the zone where ['f(x)] becomes inaccurate.
  88. A good starting point for ['digits] would be 0.6*D for Newton and 0.4*D for Halley or Shr'''&#xf6;'''der
  89. iteration, where D is `std::numeric_limits<T>::digits`.
  90. * If you need some diagnostic output to see what is going on, you can
  91. `#define BOOST_MATH_INSTRUMENT` before the `#include <boost/math/tools/roots.hpp>`,
  92. and also ensure that display of all the significant digits with
  93. ` cout.precision(std::numeric_limits<double>::digits10)`:
  94. or even possibly significant digits with
  95. ` cout.precision(std::numeric_limits<double>::max_digits10)`:
  96. but be warned, this may produce copious output!
  97. * Finally: you may well be able to do better than these functions by hand-coding
  98. the heuristics used so that they are tailored to a specific function. You may also
  99. be able to compute the ratio of derivatives used by these methods more efficiently
  100. than computing the derivatives themselves. As ever, algebraic simplification can
  101. be a big win.
  102. [h4:newton Newton Raphson Method]
  103. Given an initial guess ['x0] the subsequent values are computed using:
  104. [equation roots1]
  105. Out-of-bounds steps revert to __bisection of the current bounds.
  106. Under ideal conditions, the number of correct digits doubles with each iteration.
  107. [h4:halley Halley's Method]
  108. Given an initial guess ['x0] the subsequent values are computed using:
  109. [equation roots2]
  110. Over-compensation by the second derivative (one which would proceed
  111. in the wrong direction) causes the method to
  112. revert to a Newton-Raphson step.
  113. Out of bounds steps revert to bisection of the current bounds.
  114. Under ideal conditions, the number of correct digits trebles with each iteration.
  115. [h4:schroder Schr'''&#xf6;'''der's Method]
  116. Given an initial guess x0 the subsequent values are computed using:
  117. [equation roots3]
  118. Over-compensation by the second derivative (one which would proceed
  119. in the wrong direction) causes the method to
  120. revert to a Newton-Raphson step. Likewise a Newton step is used
  121. whenever that Newton step would change the next value by more than 10%.
  122. Out of bounds steps revert to __bisection_wikipedia of the current bounds.
  123. Under ideal conditions, the number of correct digits trebles with each iteration.
  124. This is Schr'''&#xf6;'''der's general result (equation 18 from [@http://drum.lib.umd.edu/handle/1903/577 Stewart, G. W.
  125. "On Infinitely Many Algorithms for Solving Equations." English translation of Schr'''&#xf6;'''der's original paper.
  126. College Park, MD: University of Maryland, Institute for Advanced Computer Studies, Department of Computer Science, 1993].)
  127. This method guarantees at least quadratic convergence (the same as Newton's method), and is known to work well in the presence of multiple roots:
  128. something that neither Newton nor Halley can do.
  129. The complex Newton method works slightly differently than the rest of the methods:
  130. Since there is no way to bracket roots in the complex plane,
  131. the `min` and `max` arguments are not accepted.
  132. Failure to reach a root is communicated by returning `nan`s.
  133. Remember that if a function has many roots,
  134. then which root the complex Newton's method converges to is essentially impossible to predict a priori; see the Newton's fractal for more information.
  135. Finally, the derivative of /f/ must be continuous at the root or else non-roots can be found; see [@https://math.stackexchange.com/questions/3017766/constructing-newton-iteration-converging-to-non-root here] for an example.
  136. An example usage of `complex_newton` is given in `examples/daubechies_coefficients.cpp`.
  137. [h4 Quadratics]
  138. To solve a quadratic /ax/[super 2] + /bx/ + /c/ = 0, we may use
  139. auto [x0, x1] = boost::math::tools::quadratic_roots(a, b, c);
  140. If the roots are real, they are arranged so that `x0` \u2264 `x1`.
  141. If the roots are complex and the inputs are real, `x0` and `x1` are both `std::numeric_limits<Real>::quiet_NaN()`.
  142. In this case we must cast `a`, `b` and `c` to a complex type to extract the complex roots.
  143. If `a`, `b` and `c` are integral, then the roots are of type double.
  144. The routine is much faster if the fused-multiply-add instruction is available on your architecture.
  145. If the fma is not available, the function resorts to slow emulation.
  146. Finally, speed is improved if you compile for your particular architecture.
  147. For instance, if you compile without any architecture flags, then the `std::fma` call compiles down to `call _fma`,
  148. which dynamically chooses to emulate or execute the `vfmadd132sd` instruction based on the capabilities of the architecture.
  149. If instead, you compile with (say) `-march=native` then no dynamic choice is made:
  150. The `vfmadd132sd` instruction is always executed if available and emulation is used if not.
  151. [h4 Examples]
  152. See __root_finding_examples.
  153. [endsect] [/section:roots_deriv Root Finding With Derivatives]
  154. [/
  155. Copyright 2006, 2010, 2012 John Maddock and Paul A. Bristow.
  156. Distributed under the Boost Software License, Version 1.0.
  157. (See accompanying file LICENSE_1_0.txt or copy at
  158. http://www.boost.org/LICENSE_1_0.txt).
  159. ]