From e0737c56c1e384e05064a0c6e1ac46979371041b Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Wed, 19 Dec 2018 22:58:32 +0100 Subject: New upstream version 2.0.0+repack --- .../doc/html/math_toolkit/roots/bad_guess.html | 778 +++ .../doc/html/math_toolkit/roots/bad_roots.html | 127 + .../doc/html/math_toolkit/roots/brent_minima.html | 612 +++ .../doc/html/math_toolkit/roots/polynomials.html | 312 ++ .../math/doc/html/math_toolkit/roots/rational.html | 232 + .../html/math_toolkit/roots/root_comparison.html | 55 + .../roots/root_comparison/cbrt_comparison.html | 1588 ++++++ .../roots/root_comparison/elliptic_comparison.html | 1870 +++++++ .../roots/root_comparison/root_n_comparison.html | 5286 ++++++++++++++++++++ .../math_toolkit/roots/root_finding_examples.html | 90 + .../roots/root_finding_examples/5th_root_eg.html | 169 + .../roots/root_finding_examples/cbrt_eg.html | 486 ++ .../roots/root_finding_examples/elliptic_eg.html | 272 + .../roots/root_finding_examples/lambda.html | 78 + .../root_finding_examples/multiprecision_root.html | 291 ++ .../roots/root_finding_examples/nth_root.html | 184 + .../doc/html/math_toolkit/roots/roots_deriv.html | 315 ++ .../doc/html/math_toolkit/roots/roots_noderiv.html | 222 + .../math_toolkit/roots/roots_noderiv/TOMS748.html | 193 + .../math_toolkit/roots/roots_noderiv/bisect.html | 152 + .../roots/roots_noderiv/bracket_solve.html | 186 + .../math_toolkit/roots/roots_noderiv/brent.html | 54 + .../roots/roots_noderiv/implementation.html | 63 + .../roots/roots_noderiv/root_termination.html | 102 + 24 files changed, 13717 insertions(+) create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/bad_guess.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/bad_roots.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/brent_minima.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/polynomials.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/rational.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_comparison.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_comparison/cbrt_comparison.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_comparison/elliptic_comparison.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_comparison/root_n_comparison.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/5th_root_eg.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/cbrt_eg.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/elliptic_eg.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/lambda.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/multiprecision_root.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/nth_root.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/roots_deriv.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/TOMS748.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/bisect.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/bracket_solve.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/brent.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/implementation.html create mode 100644 boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/root_termination.html (limited to 'boost/libs/math/doc/html/math_toolkit/roots') diff --git a/boost/libs/math/doc/html/math_toolkit/roots/bad_guess.html b/boost/libs/math/doc/html/math_toolkit/roots/bad_guess.html new file mode 100644 index 00000000..25e524f4 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/bad_guess.html @@ -0,0 +1,778 @@ + + + +The Effect of a Poor Initial Guess + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ It's instructive to take our "toy" example algorithms, and use + deliberately bad initial guesses to see how the various root finding algorithms + fair. We'll start with the cubed root, and using the cube root of 500 as + the test case: +

+
+++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

+ Initial Guess= +

+
+

+ -500% (≈1.323) +

+
+

+ -100% (≈3.97) +

+
+

+ -50% (≈3.96) +

+
+

+ -20% (≈6.35) +

+
+

+ -10% (≈7.14) +

+
+

+ -5% (≈7.54) +

+
+

+ 5% (≈8.33) +

+
+

+ 10% (≈8.73) +

+
+

+ 20% (≈9.52) +

+
+

+ 50% (≈11.91) +

+
+

+ 100% (≈15.87) +

+
+

+ 500 (≈47.6) +

+
+

+ bracket_and_solve_root +

+
+

+ 12 +

+
+

+ 8 +

+
+

+ 8 +

+
+

+ 10 +

+
+

+ 11 +

+
+

+ 11 +

+
+

+ 11 +

+
+

+ 11 +

+
+

+ 11 +

+
+

+ 11 +

+
+

+ 7 +

+
+

+ 13 +

+
+

+ newton_iterate +

+
+

+ 12 +

+
+

+ 7 +

+
+

+ 7 +

+
+

+ 5 +

+
+

+ 5 +

+
+

+ 4 +

+
+

+ 4 +

+
+

+ 5 +

+
+

+ 5 +

+
+

+ 6 +

+
+

+ 7 +

+
+

+ 9 +

+
+

+ halley_iterate +

+
+

+ 7 +

+
+

+ 4 +

+
+

+ 4 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 4 +

+
+

+ 4 +

+
+

+ 6 +

+
+

+ schroder_iterate +

+
+

+ 11 +

+
+

+ 6 +

+
+

+ 6 +

+
+

+ 4 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 4 +

+
+

+ 5 +

+
+

+ 5 +

+
+

+ 8 +

+
+

+ As you can see bracket_and_solve_root + is relatively insensitive to starting location - as long as you don't start + many orders of magnitude away from the root it will take roughly the same + number of steps to bracket the root and solve it. On the other hand the derivative-based + methods are slow to start, but once they have some digits correct they increase + precision exceptionally fast: they are therefore quite sensitive to the initial + starting location. +

+

+ The next table shows the number of iterations required to find the second + radius of an ellipse with first radius 50 and arc-length 500: +

+
+++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

+ Initial Guess= +

+
+

+ -500% (≈20.6) +

+
+

+ -100% (≈61.81) +

+
+

+ -50% (≈61.81) +

+
+

+ -20% (≈98.9) +

+
+

+ -10% (≈111.3) +

+
+

+ -5% (≈117.4) +

+
+

+ 5% (≈129.8) +

+
+

+ 10% (≈136) +

+
+

+ 20% (≈148.3) +

+
+

+ 50% (≈185.4) +

+
+

+ 100% (≈247.2) +

+
+

+ 500 (≈741.7) +

+
+

+ bracket_and_solve_root +

+
+

+ 11 +

+
+

+ 5 +

+
+

+ 5 +

+
+

+ 8 +

+
+

+ 8 +

+
+

+ 7 +

+
+

+ 7 +

+
+

+ 8 +

+
+

+ 9 +

+
+

+ 8 +

+
+

+ 6 +

+
+

+ 10 +

+
+

+ newton_iterate +

+
+

+ 4 +

+
+

+ 4 +

+
+

+ 4 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 4 +

+
+

+ 4 +

+
+

+ 4 +

+
+

+ halley_iterate +

+
+

+ 4 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 2 +

+
+

+ 2 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ schroder_iterate +

+
+

+ 4 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 2 +

+
+

+ 2 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ 3 +

+
+

+ Interestingly this function is much more resistant to a poor initial guess + when using derivatives. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/bad_roots.html b/boost/libs/math/doc/html/math_toolkit/roots/bad_roots.html new file mode 100644 index 00000000..4c463afb --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/bad_roots.html @@ -0,0 +1,127 @@ + + + +Examples Where Root Finding Goes Wrong + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ There are many reasons why root root finding can fail, here are just a few + of the more common examples: +

+

+ + Local + Minima +

+

+ If you start in the wrong place, such as z0 here: +

+

+ +

+

+ Then almost any root-finding algorithm will descend into a local minima rather + than find the root. +

+

+ + Flatlining +

+

+ In this example, we're starting from a location (z0) where the first derivative + is essentially zero: +

+

+ +

+

+ In this situation the next iteration will shoot off to infinity (assuming + we're using derivatives that is). Our code guards against this by insisting + that the root is always bracketed, and then never stepping outside those + bounds. In a case like this, no root finding algorithm can do better than + bisecting until the root is found. +

+

+ Note that there is no scale on the graph, we have seen examples of this situation + occur in practice even when several decimal places of the initial + guess z0 are correct. +

+

+ This is really a special case of a more common situation where root finding + with derivatives is divergent. Consider starting at + z0 in this case: +

+

+ +

+

+ An initial Newton step would take you further from the root than you started, + as will all subsequent steps. +

+

+ + Micro-stepping + / Non-convergence +

+

+ Consider starting at z0 in this situation: +

+

+ +

+

+ The first derivative is essentially infinite, and the second close to zero + (and so offers no correction if we use it), as a result we take a very small + first step. In the worst case situation, the first step is so small - perhaps + even so small that subtracting from z0 has no effect at the current working + precision - that our algorithm will assume we are at the root already and + terminate. Otherwise we will take lot's of very small steps which never converge + on the root: our algorithms will protect against that by reverting to bisection. +

+

+ An example of this situation would be trying to find the root of e-1/z2 - + this function has a single root at z = 0, but for z0 < + 0 neither Newton nor Halley steps will ever converge on the root, + and for z0 > 0 the steps are actually divergent. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/brent_minima.html b/boost/libs/math/doc/html/math_toolkit/roots/brent_minima.html new file mode 100644 index 00000000..672ded34 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/brent_minima.html @@ -0,0 +1,612 @@ + + + +Locating Function Minima using Brent's algorithm + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +
+ + Synopsis +
+
#include <boost/math/tools/minima.hpp>
+
+
template <class F, class T>
+std::pair<T, T> brent_find_minima(F f, T min, T max, int bits);
+
+template <class F, class T>
+std::pair<T, T> brent_find_minima(F f, T min, T max, int bits, boost::uintmax_t& max_iter);
+
+
+ + Description +
+

+ These two functions locate the minima of the continuous function f + using Brent's + method: specifically it uses quadratic interpolation to locate the + minima, or if that fails, falls back to a golden-section + search. +

+

+ Parameters +

+
+

+
+
f
+

+ The function to minimise: a function object (functor) that should be + smooth over the range [min, max], with no maxima + occurring in that interval. +

+
min
+

+ The lower endpoint of the range in which to search for the minima. +

+
max
+

+ The upper endpoint of the range in which to search for the minima. +

+
bits
+

+ The number of bits precision to which the minima should be found.
+ Note that in principle, the minima can not be located to greater accuracy + than the square root of machine epsilon (for 64-bit double, sqrt(1e-16)≅1e-8), + therefore the value of bits will be ignored if + it's greater than half the number of bits in the mantissa of T. +

+
max_iter
+

+ The maximum number of iterations to use in the algorithm, if not provided + the algorithm will just keep on going until the minima is found. +

+
+
+

+ Returns: +

+

+ A pair of type T containing + the value of the abscissa at the minima and the value of f(x) + at the minima. +

+
+ + + + + +
[Tip]Tip
+

+ Defining BOOST_MATH_INSTRUMENT will show some parameters, for example: +

+
Type T is double
+bits = 24, maximum 26
+tolerance = 1.19209289550781e-007
+seeking minimum in range min-4 to 1.33333333333333
+maximum iterations 18446744073709551615
+10 iterations.
+
+
+
+ + Brent + Minimisation Example +
+

+ As a demonstration, we replicate this Wikipedia + example minimising the function y= (x+3)(x-1)2. +

+

+ It is obvious from the equation and the plot that there is a minimum at exactly + one and the value of the function at one is exactly zero. +

+
+ + + + + +
[Tip]Tip

+ This observation shows that an analytical or Closed-form + expression solution always beats brute-force hands-down for both + speed and precision. +

+

+ +

+

+ First an include is needed: +

+
#include <boost/math/tools/minima.hpp>
+
+

+ This function is encoded in C++ as function object (functor) using double precision thus: +

+
struct funcdouble
+{
+  double operator()(double const& x)
+  { //
+    return (x + 3) * (x - 1) * (x - 1); // (x + 3)(x - 1)^2
+  }
+};
+
+

+ The Brent function is conveniently accessed through a using + statement (noting sub-namespace ::tools). +

+

+ The search minimum and maximum are chosen as -4 to 4/3 (as in the Wikipedia + example). +

+
+ + + + + +
[Tip]Tip

+ S A Stage (reference 6) reports that the Brent algorithm is slow + to start, but fast to converge, so choosing a tight min-max + range is good. +

+

+ For simplicity, we set the precision parameter bits + to std::numeric_limits<double>::digits, which is effectively the maximum + possible i.e. std::numeric_limits<double>::digits/2. Nor do we provide a maximum iterations + parameter max_iter, (perhaps + unwidely), so the function will iterate until it finds a minimum. +

+
int bits = std::numeric_limits<double>::digits;
+
+std::pair<double, double> r = brent_find_minima(funcdouble(), -4., 4. / 3, bits);
+
+std::cout.precision(std::numeric_limits<double>::digits10);
+std::cout << "x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second << std::endl;
+// x at minimum = 1.00000000112345, f(1.00000000112345) = 5.04852568272458e-018
+
+

+ The resulting std::pair + contains the minimum close to one and the minimum value close to zero. +

+
x at minimum = 1.00000000112345, f(1.00000000112345) = 5.04852568272458e-018
+
+

+ The differences from the expected one and zero + are less than the uncertainty (for double) + 1.5e-008 calculated from sqrt(std::numeric_limits<double>::digits) == 53. +

+

+ We can use it like this to check that the two values are close-enough to + those expected, +

+
 using boost::math::fpc::is_close_to;
+ using boost::math::fpc::is_small;
+
+double uncertainty = sqrt(std::numeric_limits<double>::digits);
+is_close_to(1., r.first, uncertainty);
+is_small(r.second, uncertainty);
+
+x == 1 (compared to uncertainty 0.00034527) is true
+f(x) == 0 (compared to uncertainty 0.00034527) is true
+
+

+ It is possible to make this comparison more generally with a templated function, + returning true when this criterion + is met, for example: +

+
//
+template <class T = double>
+bool close(T expect, T got, T tolerance)
+{
+  using boost::math::fpc::is_close_to;
+  using boost::math::fpc::is_small;
+
+  if (is_small<T>(expect, tolerance))
+  {
+    return is_small<T>(got, tolerance);
+  }
+  else
+  {
+    return is_close_to<T>(expect, got, tolerance);
+  }
+}
+
+

+ In practical applications, we might want to know how many iterations, and + maybe to limit iterations and perhaps to trade some loss of precision for + speed, for example: +

+
const boost::uintmax_t maxit = 20;
+boost::uintmax_t it = maxit;
+r = brent_find_minima(funcdouble(), -4., 4. / 3, bits, it);
+std::cout << "x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second
+<< " after " << it << " iterations. " << std::endl;
+
+

+ limits to a maximum of 20 iterations (a reasonable estimate for this application, + even for higher precision shown later). +

+

+ The parameter it is updated + to return the actual number of iterations (so it may be useful to also keep + a record of the limit in maxit). +

+

+ It is neat to avoid showing insignificant digits by computing the number + of decimal digits to display. +

+
std::streamsize prec = static_cast<int>(2 + sqrt(bits));  // Number of significant decimal digits.
+std::cout << "Showing " << bits << " bits precision with " << prec
+  << " decimal digits from tolerance " << sqrt(std::numeric_limits<double>::epsilon())
+  << std::endl;
+std::streamsize precision = std::cout.precision(prec); // Save.
+
+std::cout << "x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second
+  << " after " << it << " iterations. " << std::endl;
+
+
Showing 53 bits precision with 9 decimal digits from tolerance 1.49011611938477e-008
+x at minimum = 1, f(1) = 5.04852568e-018
+
+

+ We can also half the number of precision bits from 52 to 26. +

+
bits /= 2; // Half digits precision (effective maximum).
+double epsilon_2 = boost::math::pow<-(std::numeric_limits<double>::digits/2 - 1), double>(2);
+
+std::cout << "Showing " << bits << " bits precision with " << prec
+  << " decimal digits from tolerance " << sqrt(epsilon_2)
+  << std::endl;
+std::streamsize precision = std::cout.precision(prec); // Save.
+
+boost::uintmax_t it = maxit;
+r = brent_find_minima(funcdouble(), -4., 4. / 3, bits, it);
+std::cout << "x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second << std::endl;
+std::cout << it << " iterations. " << std::endl;
+
+

+ showing no change in the result and no change in the number of iterations, + as expected. +

+

+ It is only if we reduce the precision to a quarter, specifying only 13 precision + bits +

+
bits /= 2; // Quarter precision.
+double epsilon_4 = boost::math::pow<-(std::numeric_limits<double>::digits / 4 - 1), double>(2);
+
+std::cout << "Showing " << bits << " bits precision with " << prec
+  << " decimal digits from tolerance " << sqrt(epsilon_4)
+  << std::endl;
+std::streamsize precision = std::cout.precision(prec); // Save.
+
+boost::uintmax_t it = maxit;
+r = brent_find_minima(funcdouble(), -4., 4. / 3, bits, it);
+std::cout << "x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second
+<< ", after " << it << " iterations. " << std::endl;
+
+

+ that we reduce the number of iterations from 10 to 7 and the result significantly + differing from one and zero. +

+
Showing 13 bits precision with 9 decimal digits from tolerance 0.015625
+x at minimum = 0.9999776, f(0.9999776) = 2.0069572e-009 after  7 iterations.
+
+
+ + Templating + on floating-point type +
+

+ If we want to switch the floating-point type, then the functor must be revised. + Since the functor is stateless, the easiest option is to simply make operator() + a template member function: +

+
struct func
+{
+  template <class T>
+  T operator()(T const& x)
+  { //
+    return (x + 3) * (x - 1) * (x - 1); //
+  }
+};
+
+

+ The brent_find_minima function + can now be used in template form. +

+
std::cout.precision(std::numeric_limits<long double>::digits10);
+long double bracket_min = -4.;
+long double bracket_max = 4. / 3;
+int bits = std::numeric_limits<long double>::digits;
+const boost::uintmax_t maxit = 20;
+boost::uintmax_t it = maxit;
+
+std::pair<long double, long double> r = brent_find_minima(func(), bracket_min, bracket_max, bits, it);
+std::cout << "x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second
+  << ", after " << it << " iterations. " << std::endl;
+
+

+ The form shown uses the floating-point type long + double by deduction, but it is also + possible to be more explicit, for example: +

+
std::pair<long double, long double> r = brent_find_minima<func, long double>
+(func(), bracket_min, bracket_max, bits, it);
+
+

+ In order to show the use of multiprecision below, it may be convenient to + write a templated function to use this. +

+
template <class T>
+void show_minima()
+{
+  using boost::math::tools::brent_find_minima;
+  try
+  { // Always use try'n'catch blocks with Boost.Math to get any error messages.
+
+    int bits = std::numeric_limits<T>::digits/2; // Maximum is digits/2;
+    std::streamsize prec = static_cast<int>(2 + sqrt(bits));  // Number of significant decimal digits.
+    std::streamsize precision = std::cout.precision(prec); // Save.
+
+    std::cout << "\n\nFor type  " << typeid(T).name()
+      << ",\n  epsilon = " << std::numeric_limits<T>::epsilon()
+      // << ", precision of " << bits << " bits"
+      << ",\n  the maximum theoretical precision from Brent minimization is " << sqrt(std::numeric_limits<T>::epsilon())
+      << "\n  Displaying to std::numeric_limits<T>::digits10 " << prec << " significant decimal digits."
+      << std::endl;
+
+    const boost::uintmax_t maxit = 20;
+    boost::uintmax_t it = maxit;
+    // Construct using string, not double, avoids loss of precision.
+    //T bracket_min = static_cast<T>("-4");
+    //T bracket_max = static_cast<T>("1.3333333333333333333333333333333333333333333333333");
+
+    //  Construction from double may cause loss of precision for multiprecision types like cpp_bin_float.
+    // but brackets values are good enough for using Brent minimization.
+    T bracket_min = static_cast<T>(-4);
+    T bracket_max = static_cast<T>(1.3333333333333333333333333333333333333333333333333);
+
+    std::pair<T, T> r = brent_find_minima<func, T>(func(), bracket_min, bracket_max, bits, it);
+
+    std::cout << "  x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second;
+    if (it < maxit)
+    {
+      std::cout << ",\n  met " << bits << " bits precision" << ", after " << it << " iterations." << std::endl;
+    }
+    else
+    {
+      std::cout << ",\n  did NOT meet " << bits << " bits precision" << " after " << it << " iterations!" << std::endl;
+    }
+    // Check that result is that expected (compared to theoretical uncertainty).
+    T uncertainty = sqrt(std::numeric_limits<T>::epsilon());
+    //std::cout << std::boolalpha << "x == 1 (compared to uncertainty " << uncertainty << ") is " << close(static_cast<T>(1), r.first, uncertainty) << std::endl;
+    //std::cout << std::boolalpha << "f(x) == (0 compared to uncertainty " << uncertainty << ") is " << close(static_cast<T>(0), r.second, uncertainty) << std::endl;
+    // Problems with this using multiprecision with expression template on?
+    std::cout.precision(precision);  // Restore.
+  }
+  catch (const std::exception& e)
+  { // Always useful to include try & catch blocks because default policies
+    // are to throw exceptions on arguments that cause errors like underflow, overflow.
+    // Lacking try & catch blocks, the program will abort without a message below,
+    // which may give some helpful clues as to the cause of the exception.
+    std::cout <<
+      "\n""Message from thrown exception was:\n   " << e.what() << std::endl;
+  }
+} // void show_minima()
+
+

+ We can use this with all built-in floating-point types, for example +

+
show_minima<float>();
+show_minima<double>();
+show_minima<long double>();
+
+

+ and, on platforms that provide it, a 128-bit + quad type. (See float128). +

+

+ For this optional include, the build should define the macro BOOST_HAVE_QUADMATH: +

+
#ifdef BOOST_HAVE_QUADMATH  // Define only if GCC or Intel and have quadmath.lib or .dll library available.
+  using boost::multiprecision::float128;
+#endif
+
+

+ or +

+
// #ifndef _MSC_VER
+#ifdef BOOST_HAVE_QUADMATH  // Define only if GCC or Intel and have quadmath.lib or .dll library available.
+  show_minima<float128>(); // Needs quadmath_snprintf, sqrtQ, fabsq that are in in quadmath library.
+#endif
+
+
+ + Multiprecision +
+

+ If a higher precision than double + (or long double + if that is more precise) is required, then this is easily achieved using + Boost.Multiprecision + with some includes from +

+
#include <boost/multiprecision/cpp_dec_float.hpp> // For decimal boost::multiprecision::cpp_dec_float_50.
+#include <boost/multiprecision/cpp_bin_float.hpp> // For binary boost::multiprecision::cpp_bin_float_50;
+
+

+ and some typdefs. +

+
using boost::multiprecision::cpp_bin_float_50; // binary.
+
+typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<50>,
+  boost::multiprecision::et_on>
+  cpp_bin_float_50_et_on;  // et_on is default so is same as cpp_bin_float_50.
+
+typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<50>,
+  boost::multiprecision::et_off>
+  cpp_bin_float_50_et_off;
+
+using boost::multiprecision::cpp_dec_float_50; // decimal.
+
+typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50>,
+  boost::multiprecision::et_on> // et_on is default so is same as cpp_dec_float_50.
+  cpp_dec_float_50_et_on;
+
+typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50>,
+  boost::multiprecision::et_off>
+  cpp_dec_float_50_et_off;
+
+

+ Using thus +

+
std::cout.precision(std::numeric_limits<cpp_bin_float_50>::digits10);
+
+cpp_bin_float_50 fpv("-1.2345");
+cpp_bin_float_50 absv;
+
+absv = fpv < static_cast<cpp_bin_float_50>(0) ? -fpv : fpv;
+std::cout << fpv << ' ' << absv << std::endl;
+
+
+int bits = std::numeric_limits<cpp_bin_float_50>::digits / 2 - 2;
+
+cpp_bin_float_50 bracket_min = static_cast<cpp_bin_float_50>("-4");
+cpp_bin_float_50 bracket_max = static_cast<cpp_bin_float_50>("1.3333333333333333333333333333333333333333333333333");
+
+std::cout << bracket_min << " " << bracket_max << std::endl;
+const boost::uintmax_t maxit = 20;
+boost::uintmax_t it = maxit;
+std::pair<cpp_bin_float_50, cpp_bin_float_50> r = brent_find_minima(func(), bracket_min, bracket_max, bits, it);
+
+std::cout << "x at minimum = " << r.first << ", f(" << r.first << ") = " << r.second
+// x at minimum = 1, f(1) = 5.04853e-018
+  << ", after " << it << " iterations. " << std::endl;
+
+close(static_cast<cpp_bin_float_50>(1), r.first, sqrt(std::numeric_limits<cpp_bin_float_50>::epsilon()));
+
+

+ and with our show function +

+
show_minima<cpp_bin_float_50_et_on>(); //
+
+
For type  class boost::multiprecision::number<class boost::multiprecision::backends::cpp_bin_float<50, 10, void, int, 0, 0>, 1>,
+  epsilon = 5.3455294202e-51,
+  the maximum theoretical precision from Brent minimization is 7.311312755e-26
+  Displaying to std::numeric_limits<T>::digits10 11 significant decimal digits.
+  x at minimum = 1, f(1) = 5.6273022713e-58,
+  met 84 bits precision, after 14 iterations.
+
+
For type  class boost::multiprecision::number<class boost::multiprecision::backends::cpp_bin_float<50, 10, void, int, 0, 0>, 1>,
+
+
+ + + + + +
[Tip]Tip

+ One can usually rely on template argument deduction to avoid specifying + the verbose multiprecision types, but great care in needed with the type + of the values provided to avoid confusing the compiler. +

+
+ + + + + +
[Tip]Tip

+ Using std::cout.precision(std::numeric_limits<T>::digits10); + or std::cout.precision(std::numeric_limits<T>::max_digits10); + during debugging may be wise because it gives some warning if construction + of multiprecision values involves unintended conversion from double by showing trailing zero or random + digits after max_digits10, + that is 17 for double, digit + 18... may be just noise. +

+

+ The complete example code is at brent_minimise_example.cpp. +

+
+ + Implementation +
+

+ This is a reasonably faithful implementation of Brent's algorithm. +

+
+ + References +
+
    +
  1. + Brent, R.P. 1973, Algorithms for Minimization without Derivatives, (Englewood + Cliffs, NJ: Prentice-Hall), Chapter 5. +
  2. +
  3. + Numerical Recipes in C, The Art of Scientific Computing, Second Edition, + William H. Press, Saul A. Teukolsky, William T. Vetterling, and Brian + P. Flannery. Cambridge University Press. 1988, 1992. +
  4. +
  5. + An algorithm with guaranteed convergence for finding a zero of a function, + R. P. Brent, The Computer Journal, Vol 44, 1971. +
  6. +
  7. + Brent's method + in Wikipedia. +
  8. +
  9. + Z. Zhang, An Improvement to the Brent's Method, IJEA, vol. 2, pp. 2 to + 26, May 31, 2011. http://www.cscjournals.org/manuscript/Journals/IJEA/volume2/Issue1/IJEA-7.pdf +
  10. +
  11. + Steven A. Stage, Comments on An Improvement to the Brent's Method (and + comparison of various algorithms) http://www.cscjournals.org/manuscript/Journals/IJEA/volume4/Issue1/IJEA-33.pdf + Stage concludes that Brent's algorithm is slow to start, but fast to + finish convergence, and has good accuracy. +
  12. +
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/polynomials.html b/boost/libs/math/doc/html/math_toolkit/roots/polynomials.html new file mode 100644 index 00000000..4b988cc2 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/polynomials.html @@ -0,0 +1,312 @@ + + + +Polynomials + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +
+ + Synopsis +
+
#include <boost/math/tools/polynomial.hpp>
+
+
namespace boost { namespace math {
+namespace tools {
+
+template <class T>
+class polynomial
+{
+public:
+   // typedefs:
+   typedef typename std::vector<T>::value_type value_type;
+   typedef typename std::vector<T>::size_type  size_type;
+
+   // construct:
+   polynomial(){}
+   template <class U>
+   polynomial(const U* data, unsigned order);
+   template <class I>
+   polynomial(I first, I last);
+   template <class U>
+   explicit polynomial(const U& point);
+   polynomial(std::initializer_list<T> l); // C++11
+
+   // access:
+   size_type size()const;
+   size_type degree()const;
+   value_type& operator[](size_type i);
+   const value_type& operator[](size_type i)const;
+   std::vector<T> const& data() const;
+   std::vector<T>& data();
+   explicit operator bool() const;
+
+   // modify:
+   void set_zero();
+
+   // operators:
+   template <class U>
+   polynomial& operator +=(const U& value);
+   template <class U>
+   polynomial& operator -=(const U& value);
+   template <class U>
+   polynomial& operator *=(const U& value);
+   template <class U>
+   polynomial& operator /=(const U& value);
+   template <class U>
+   polynomial& operator %=(const U& value);
+   template <class U>
+   polynomial& operator +=(const polynomial<U>& value);
+   template <class U>
+   polynomial& operator -=(const polynomial<U>& value);
+   template <class U>
+   polynomial& operator *=(const polynomial<U>& value);
+   template <class U>
+   polynomial& operator /=(const polynomial<U>& value);
+   template <class U>
+   polynomial& operator %=(const polynomial<U>& value);
+};
+
+template <class T>
+polynomial<T> operator + (const polynomial<T>& a, const polynomial<T>& b);
+template <class T>
+polynomial<T> operator - (const polynomial<T>& a, const polynomial<T>& b);
+template <class T>
+polynomial<T> operator * (const polynomial<T>& a, const polynomial<T>& b);
+template <class T>
+polynomial<T> operator / (const polynomial<T>& a, const polynomial<T>& b);
+template <class T>
+polynomial<T> operator % (const polynomial<T>& a, const polynomial<T>& b);
+
+template <class T, class U>
+polynomial<T> operator + (const polynomial<T>& a, const U& b);
+template <class T, class U>
+polynomial<T> operator - (const polynomial<T>& a, const U& b);
+template <class T, class U>
+polynomial<T> operator * (const polynomial<T>& a, const U& b);
+template <class T, class U>
+polynomial<T> operator / (const polynomial<T>& a, const U& b);
+template <class T, class U>
+polynomial<T> operator % (const polynomial<T>& a, const U& b);
+
+template <class U, class T>
+polynomial<T> operator + (const U& a, const polynomial<T>& b);
+template <class U, class T>
+polynomial<T> operator - (const U& a, const polynomial<T>& b);
+template <class U, class T>
+polynomial<T> operator * (const U& a, const polynomial<T>& b);
+
+template <class T>
+polynomial<T> operator - (const polynomial<T>& a);
+
+template <class T>
+polynomial<T> operator >>= (const U& a);
+template <class T>
+polynomial<T> operator >> (polynomial<T> const &a, const U& b);
+template <class T>
+polynomial<T> operator <<= (const U& a);
+template <class T>
+polynomial<T> operator << (polynomial<T> const &a, const U& b);
+
+template <class T>
+bool operator == (const polynomial<T> &a, const polynomial<T> &b);
+template <class T>
+bool operator != (const polynomial<T> &a, const polynomial<T> &b);
+
+template <class T>
+polynomial<T> pow(polynomial<T> base, int exp);
+
+template <class charT, class traits, class T>
+std::basic_ostream<charT, traits>& operator <<
+   (std::basic_ostream<charT, traits>& os, const polynomial<T>& poly);
+
+template <typename T>
+std::pair< polynomial<T>, polynomial<T> >
+quotient_remainder(const polynomial<T>& a, const polynomial<T>& b);
+
+} //    namespace tools
+}} //    namespace boost { namespace math
+
+
+ + Description +
+

+ This is a somewhat trivial class for polynomial manipulation. +

+

+ See +

+
    +
  • + Polynomial + and +
  • +
  • + Donald E. Knuth, The Art of Computer Programming: Volume 2, Third edition, + (1998) Chapter 4.6.1, Algorithm D: Division of polynomials over a field. +
  • +
+

+ Implementation is currently of the "naive" variety, with 𝑶(N2) multiplication, + for example. This class should not be used in high-performance computing + environments: it is intended for the simple manipulation of small polynomials, + typically generated for special function approximation. +

+

+ It does has division for polynomials over a field + (here floating point, complex, etc) and over a unique factorization domain + (integers). Division of polynomials over a field is compatible with Euclidean GCD. +

+

+ Advanced manipulations: the FFT, factorisation etc are not currently provided. + Submissions for these are of course welcome :-) +

+
+ + Polynomial + Arithmetic Examples +
+

+ First include the essential polynomial header (and others) to make the example: +

+
#include <boost/math/tools/polynomial.hpp>
+
+

+ and some using statements are convenient: +

+
using std::string;
+using std::exception;
+using std::cout;
+using std::abs;
+using std::pair;
+
+using namespace boost::math;
+using namespace boost::math::tools; // for polynomial
+using boost::lexical_cast;
+
+

+ Store the coefficients in a convenient way to access them, then create some + polynomials using construction from an iterator range, and finally output + in a 'pretty' formula format. +

+
+ + + + + +
[Tip]Tip

+ Although we might conventionally write a polynomial from left to right + in descending order of degree, Boost.Math stores in ascending + order of degree. +

+
Read/write for humans:    3x^3 - 4x^2 - 6x + 10
+Boost polynomial storage: [ 10, -6, -4, 3 ]
+
+
boost::array<double, 4> const d3a = {{10, -6, -4, 3}};
+polynomial<double> const a(d3a.begin(), d3a.end());
+
+// With C++11 and later, you can also use initializer_list construction.
+polynomial<double> const b{{-2.0, 1.0}};
+
+// formula_format() converts from Boost storage to human notation.
+cout << "a = " << formula_format(a)
+<< "\nb = " << formula_format(b) << "\n\n";
+
+

+ for output: +

+
a = 3x^3 - 4x^2 - 6x + 10
+b = x - 2
+
+
// Now we can do arithmetic with the usual infix operators: + - * / and %.
+polynomial<double> s = a + b;
+cout << "a + b = " << formula_format(s) << "\n";
+polynomial<double> d = a - b;
+cout << "a - b = " << formula_format(d) << "\n";
+polynomial<double> p = a * b;
+cout << "a * b = " << formula_format(p) << "\n";
+polynomial<double> q = a / b;
+cout << "a / b = " << formula_format(q) << "\n";
+polynomial<double> r = a % b;
+cout << "a % b = " << formula_format(r) << "\n";
+
+

+ for output +

+
a + b = 3x^3 - 4x^2 - 5x + 8
+a - b = 3x^3 - 4x^2 - 7x + 12
+a * b = 3x^4 - 10x^3 + 2x^2 + 22x - 20
+a / b = 3x^2 + 2x - 2
+a % b = 6
+
+
+ + Division, + Quotient and Remainder +
+

+ Division is a special case where you can calculate two for the price of one. +

+

+ Actually, quotient and remainder are always calculated together due to the + nature of the algorithm: the infix operators return one result and throw + the other away. +

+

+ If you are doing a lot of division and want both the quotient and remainder, + then you don't want to do twice the work necessary. +

+

+ In that case you can call the underlying function, quotient_remainder, + to get both results together as a pair. +

+
  pair< polynomial<double>, polynomial<double> > result;
+  result = quotient_remainder(a, b);
+// Reassure ourselves that the result is the same.
+  BOOST_ASSERT(result.first == q);
+  BOOST_ASSERT(result.second == r);
+
+

+ The source code is at polynomial_arithmetic.cpp +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/rational.html b/boost/libs/math/doc/html/math_toolkit/roots/rational.html new file mode 100644 index 00000000..453e1f00 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/rational.html @@ -0,0 +1,232 @@ + + + +Polynomial and Rational Function Evaluation + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +
+ + Synopsis +
+
#include <boost/math/tools/rational.hpp>
+
+
// Polynomials:
+template <std::size_t N, class T, class V>
+V evaluate_polynomial(const T(&poly)[N], const V& val);
+
+template <std::size_t N, class T, class V>
+V evaluate_polynomial(const boost::array<T,N>& poly, const V& val);
+
+template <class T, class U>
+U evaluate_polynomial(const T* poly, U z, std::size_t count);
+
+// Even polynomials:
+template <std::size_t N, class T, class V>
+V evaluate_even_polynomial(const T(&poly)[N], const V& z);
+
+template <std::size_t N, class T, class V>
+V evaluate_even_polynomial(const boost::array<T,N>& poly, const V& z);
+
+template <class T, class U>
+U evaluate_even_polynomial(const T* poly, U z, std::size_t count);
+
+// Odd polynomials
+template <std::size_t N, class T, class V>
+V evaluate_odd_polynomial(const T(&a)[N], const V& z);
+
+template <std::size_t N, class T, class V>
+V evaluate_odd_polynomial(const boost::array<T,N>& a, const V& z);
+
+template <class T, class U>
+U evaluate_odd_polynomial(const T* poly, U z, std::size_t count);
+
+// Rational Functions:
+template <std::size_t N, class T, class V>
+V evaluate_rational(const T(&a)[N], const T(&b)[N], const V& z);
+
+template <std::size_t N, class T, class V>
+V evaluate_rational(const boost::array<T,N>& a, const boost::array<T,N>& b, const V& z);
+
+template <class T, class U, class V>
+V evaluate_rational(const T* num, const U* denom, V z, unsigned count);
+
+
+ + Description +
+

+ Each of the functions come in three variants: a pair of overloaded functions + where the order of the polynomial or rational function is evaluated at compile + time, and an overload that accepts a runtime variable for the size of the + coefficient array. Generally speaking, compile time evaluation of the array + size results in better type safety, is less prone to programmer errors, and + may result in better optimised code. The polynomial evaluation functions + in particular, are specialised for various array sizes, allowing for loop + unrolling, and one hopes, optimal inline expansion. +

+
template <std::size_t N, class T, class V>
+V evaluate_polynomial(const T(&poly)[N], const V& val);
+
+template <std::size_t N, class T, class V>
+V evaluate_polynomial(const boost::array<T,N>& poly, const V& val);
+
+template <class T, class U>
+U evaluate_polynomial(const T* poly, U z, std::size_t count);
+
+

+ Evaluates the polynomial + described by the coefficients stored in poly. +

+

+ If the size of the array is specified at runtime, then the polynomial most + have order count-1 with count coefficients. + Otherwise it has order N-1 with N + coefficients. +

+

+ Coefficients should be stored such that the coefficients for the xi terms + are in poly[i]. +

+

+ The types of the coefficients and of variable z may + differ as long as *poly is convertible to type U. + This allows, for example, for the coefficient table to be a table of integers + if this is appropriate. +

+
template <std::size_t N, class T, class V>
+V evaluate_even_polynomial(const T(&poly)[N], const V& z);
+
+template <std::size_t N, class T, class V>
+V evaluate_even_polynomial(const boost::array<T,N>& poly, const V& z);
+
+template <class T, class U>
+U evaluate_even_polynomial(const T* poly, U z, std::size_t count);
+
+

+ As above, but evaluates an even polynomial: one where all the powers of + z are even numbers. Equivalent to calling evaluate_polynomial(poly, z*z, count). +

+
template <std::size_t N, class T, class V>
+V evaluate_odd_polynomial(const T(&a)[N], const V& z);
+
+template <std::size_t N, class T, class V>
+V evaluate_odd_polynomial(const boost::array<T,N>& a, const V& z);
+
+template <class T, class U>
+U evaluate_odd_polynomial(const T* poly, U z, std::size_t count);
+
+

+ As above but evaluates a polynomial where all the powers are odd numbers. + Equivalent to evaluate_polynomial(poly+1, z*z, + count-1) * + z + poly[0]. +

+
template <std::size_t N, class T, class U, class V>
+V evaluate_rational(const T(&num)[N], const U(&denom)[N], const V& z);
+
+template <std::size_t N, class T, class U, class V>
+V evaluate_rational(const boost::array<T,N>& num, const boost::array<U,N>& denom, const V& z);
+
+template <class T, class U, class V>
+V evaluate_rational(const T* num, const U* denom, V z, unsigned count);
+
+

+ Evaluates the rational function (the ratio of two polynomials) described + by the coefficients stored in num and demom. +

+

+ If the size of the array is specified at runtime then both polynomials most + have order count-1 with count coefficients. + Otherwise both polynomials have order N-1 with N + coefficients. +

+

+ Array num describes the numerator, and demon + the denominator. +

+

+ Coefficients should be stored such that the coefficients for the xi terms + are in num[i] and denom[i]. +

+

+ The types of the coefficients and of variable v may + differ as long as *num and *denom + are convertible to type V. This allows, for example, + for one or both of the coefficient tables to be a table of integers if this + is appropriate. +

+

+ These functions are designed to safely evaluate the result, even when the + value z is very large. As such they do not take advantage + of compile time array sizes to make any optimisations. These functions are + best reserved for situations where z may be large: if + you can be sure that numerical overflow will not occur then polynomial evaluation + with compile-time array sizes may offer slightly better performance. +

+
+ + Implementation +
+

+ Polynomials are evaluated by Horners + method. If the array size is known at compile time then the functions + dispatch to size-specific implementations that unroll the evaluation loop. +

+

+ Rational evaluation is by Horners + method: with the two polynomials being evaluated in parallel to make + the most of the processors floating-point pipeline. If v + is greater than one, then the polynomials are evaluated in reverse order + as polynomials in 1/v: this avoids unnecessary numerical + overflow when the coefficients are large. +

+

+ Both the polynomial and rational function evaluation algorithms can be tuned + using various configuration macros to provide optimal performance for a particular + combination of compiler and platform. This includes support for second-order + Horner's methods. The various options are documented + here. However, the performance benefits to be gained from these are + marginal on most current hardware, consequently it's best to run the performance test application + before changing the default settings. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_comparison.html b/boost/libs/math/doc/html/math_toolkit/roots/root_comparison.html new file mode 100644 index 00000000..64fd73c3 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_comparison.html @@ -0,0 +1,55 @@ + + + +Comparison of Root Finding Algorithms + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ + +
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/cbrt_comparison.html b/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/cbrt_comparison.html new file mode 100644 index 00000000..76f0bf86 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/cbrt_comparison.html @@ -0,0 +1,1588 @@ + + + +Comparison of Cube Root Finding Algorithms + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ In the table below, the cube root of 28 was computed for three fundamental + types floating-point types, and one Boost.Multiprecision + type cpp_bin_float + using 50 decimal digit precision, using four algorithms. +

+

+ The 'exact' answer was computed using a 100 decimal digit type: +

+
cpp_bin_float_100 full_answer ("3.036588971875662519420809578505669635581453977248111123242141654169177268411884961770250390838097895");
+
+

+ Times were measured using Boost.Timer + using class cpu_timer. +

+
    +
  • + Its is the number of iterations taken to find + the root. +
  • +
  • + Times is the CPU time-taken in arbitrary units. +
  • +
  • + Norm is a normalized time, in comparison to the + quickest algorithm (with value 1.00). +
  • +
  • + Dis is the distance from the nearest representation + of the 'exact' root in bits. Distance from the 'exact' answer is measured + by using function Boost.Math + float_distance. One or two bits distance means that all results + are effectively 'correct'. Zero means 'exact' - the nearest representable + value for the floating-point type. +
  • +
+

+ The cube-root function is a simple function, and is a contrived example + for root-finding. It does allow us to investigate some of the factors controlling + efficiency that may be extrapolated to more complex functions. +

+

+ The program used was root_finding_algorithms.cpp. + 100000 evaluations of each floating-point type and algorithm were used + and the CPU times were judged from repeat runs to have an uncertainty of + 10 %. Comparing MSVC for double + and long double + (which are identical on this patform) may give a guide to uncertainty of + timing. +

+

+ The requested precision was set as follows: +

+
++++ + + + + + + + + + + + + + + + + + + + + + + +
+

+ Function +

+
+

+ Precision Requested +

+
+

+ TOMS748 +

+
+

+ numeric_limits<T>::digits - 2 +

+
+

+ Newton +

+
+

+ floor(numeric_limits<T>::digits * 0.6) +

+
+

+ Halley +

+
+

+ floor(numeric_limits<T>::digits * 0.4) +

+
+

+ Schröder +

+
+

+ floor(numeric_limits<T>::digits * 0.4) +

+
+
    +
  • + The C++ Standard cube root function std::cbrt + is only defined for built-in or fundamental types, so cannot be used + with any User-Defined floating-point types like Boost.Multiprecision. + This, and that the cube function is so impeccably-behaved, allows the + implementer to use many tricks to achieve a fast computation. On some + platforms,std::cbrt appeared several times as quick + as the more general boost::math::cbrt, + on other platforms / compiler options boost::math::cbrt + is noticeably faster. In general, the results are highly dependent + on the code-generation / processor architecture selection compiler + options used. One can assume that the standard library will have been + compiled with options nearly optimal for the platform + it was installed on, where as the user has more choice over the options + used for Boost.Math. Pick something too general/conservative and performance + suffers, while selecting options that make use of the latest instruction + set opcodes speed's things up noticeably. +
  • +
  • + Two compilers in optimise mode were compared: GCC 4.9.1 using Netbeans + IDS and Microsoft Visual Studio 2013 (Update 1) on the same hardware. + The number of iterations seemed consistent, but the relative run-times + surprisingly different. +
  • +
  • + boost::math::cbrt allows use with any + user-defined floating-point type, conveniently Boost.Multiprecision. + It too can take some advantage of the good-behaviour of the cube function, + compared to the more general implementation in the nth root-finding + examples. For example, it uses a polynomial approximation to generate + a better guess than dividing the exponent by three, and can avoid the + complex checks in Newton-Raphson + iteration required to prevent the search going wildly off-track. + For a known precision, it may also be possible to fix the number of + iterations, allowing inlining and loop unrolling. It also algebraically + simplifies the Halley steps leading to a big reduction in the number + of floating point operations required compared to a "black box" + implementation that calculates the derivatives seperately and then + combines them in the Halley code. Typically, it was found that computation + using type double took + a few times longer when using the various root-finding algorithms directly + rather than the hand coded/optimized cbrt + routine. +
  • +
  • + The importance of getting a good guess can be seen by the iteration + count for the multiprecision case: here we "cheat" a little + and use the cube-root calculated to double precision as the initial + guess. The limitation of this tactic is that the range of possible + (exponent) values may be less than the multiprecision type. +
  • +
  • + For fundamental + types, there was little to choose between the three derivative + methods, but for cpp_bin_float, + Newton-Raphson + iteration was twice as fast. Note that the cube-root is an extreme + test case as the cost of calling the functor is so cheap that the runtimes + are largely dominated by the complexity of the iteration code. +
  • +
  • + Compiling with optimisation halved computation times, and any differences + between algorithms became nearly negligible. The optimisation speed-up + of the TOMS + Algorithm 748: enclosing zeros of continuous functions was + especially noticable. +
  • +
  • + Using a multiprecision type like cpp_bin_float_50 + for a precision of 50 decimal digits took a lot longer, as expected + because most computation uses software rather than 64-bit floating-point + hardware. Speeds are often more than 50 times slower. +
  • +
  • + Using cpp_bin_float_50, + TOMS Algorithm + 748: enclosing zeros of continuous functions was much slower + showing the benefit of using derivatives. Newton-Raphson + iteration was found to be twice as quick as either of the second-derivative + methods: this is an extreme case though, the function and its derivatives + are so cheap to compute that we're really measuring the complexity + of the boilerplate root-finding code. +
  • +
  • + For multiprecision types only one or two extra iterations + are needed to get the remaining 35 digits, whatever the algorithm used. + (The time taken was of course much greater for these types). +
  • +
  • + Using a 100 decimal-digit type only doubled the time and required only + a very few more iterations, so the cost of extra precision is mainly + the underlying cost of computing more digits, not in the way the algorithm + works. This confirms previous observations using NTL + A Library for doing Number Theory high-precision types. +
  • +
+
+ + Program + root_finding_algorithms.cpp, Microsoft Visual C++ version 12.0, Dinkumware + standard library version 610, Win32, x64
1000000 evaluations of each + of 5 root_finding algorithms.
+
+
+

Table 12.1. Cube root(28) for float, double, long double and cpp_bin_float_50

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algorithm +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ cbrt +

+
+

+ 0 +

+
+

+ 46875 +

+
+

+ 1.0 +

+
+

+ 0 +

+
+ +

+ 0 +

+
+

+ 46875 +

+
+

+ 1.0 +

+
+

+ 1 +

+
+ +

+ 0 +

+
+

+ 46875 +

+
+

+ 1.0 +

+
+

+ 1 +

+
+ +

+ 0 +

+
+

+ 4906250 +

+
+

+ 1.1 +

+
+

+ 0 +

+
+
+

+ TOMS748 +

+
+

+ 8 +

+
+

+ 234375 +

+
+

+ 5.0 +

+
+

+ -1 +

+
+ +

+ 11 +

+
+

+ 437500 +

+
+

+ 9.3 +

+
+

+ 2 +

+
+ +

+ 11 +

+
+

+ 437500 +

+
+

+ 9.3 +

+
+

+ 2 +

+
+ +

+ 7 +

+
+

+ 66218750 +

+
+

+ 15. +

+
+

+ -2 +

+
+
+

+ Newton +

+
+

+ 5 +

+
+

+ 109375 +

+
+

+ 2.3 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 125000 +

+
+

+ 2.7 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 140625 +

+
+

+ 3.0 +

+
+

+ 0 +

+
+ +

+ 2 +

+
+

+ 4531250 +

+
+

+ 1.0 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 3 +

+
+

+ 125000 +

+
+

+ 2.7 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 156250 +

+
+

+ 3.3 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 156250 +

+
+

+ 3.3 +

+
+

+ 0 +

+
+ +

+ 2 +

+
+

+ 10625000 +

+
+

+ 2.3 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 4 +

+
+

+ 140625 +

+
+

+ 3.0 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 187500 +

+
+

+ 4.0 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 203125 +

+
+

+ 4.3 +

+
+

+ 0 +

+
+ +

+ 2 +

+
+

+ 13109375 +

+
+

+ 2.9 +

+
+

+ 0 +

+
+
+
+
+ + Program + root_finding_algorithms.cpp, GNU C++ version 4.9.2, GNU libstdc++ version + 20141030, Win32, x64
1000000 evaluations of each of 5 root_finding + algorithms.
+
+
+

Table 12.2. Cube root(28) for float, double, long double and cpp_bin_float_50

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algorithm +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ cbrt +

+
+

+ 0 +

+
+

+ 46875 +

+
+

+ 1.0 +

+
+

+ 0 +

+
+ +

+ 0 +

+
+

+ 46875 +

+
+

+ 1.0 +

+
+

+ 0 +

+
+ +

+ 0 +

+
+

+ 46875 +

+
+

+ 1.0 +

+
+

+ 0 +

+
+ +

+ 0 +

+
+

+ 3500000 +

+
+

+ 1.1 +

+
+

+ 0 +

+
+
+

+ TOMS748 +

+
+

+ 8 +

+
+

+ 187500 +

+
+

+ 4.0 +

+
+

+ -1 +

+
+ +

+ 11 +

+
+

+ 406250 +

+
+

+ 8.7 +

+
+

+ 2 +

+
+ +

+ 10 +

+
+

+ 609375 +

+
+

+ 13. +

+
+

+ -1 +

+
+ +

+ 7 +

+
+

+ 44531250 +

+
+

+ 14. +

+
+

+ -2 +

+
+
+

+ Newton +

+
+

+ 5 +

+
+

+ 93750 +

+
+

+ 2.0 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 109375 +

+
+

+ 2.3 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 171875 +

+
+

+ 3.7 +

+
+

+ 0 +

+
+ +

+ 2 +

+
+

+ 3140625 +

+
+

+ 1.0 +

+
+

+ -1 +

+
+
+

+ Halley +

+
+

+ 3 +

+
+

+ 93750 +

+
+

+ 2.0 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 125000 +

+
+

+ 2.7 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 218750 +

+
+

+ 4.7 +

+
+

+ 0 +

+
+ +

+ 2 +

+
+

+ 7171875 +

+
+

+ 2.3 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 4 +

+
+

+ 109375 +

+
+

+ 2.3 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 171875 +

+
+

+ 3.7 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 281250 +

+
+

+ 6.0 +

+
+

+ 0 +

+
+ +

+ 2 +

+
+

+ 8703125 +

+
+

+ 2.8 +

+
+

+ 0 +

+
+
+
+
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/elliptic_comparison.html b/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/elliptic_comparison.html new file mode 100644 index 00000000..63a413e7 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/elliptic_comparison.html @@ -0,0 +1,1870 @@ + + + +Comparison of Elliptic Integral Root Finding Algoritghms + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ A second example compares four root finding algorithms for locating the + second radius of an ellipse with first radius 28 and arc length 300, for + four floating-point types, float, + double, long + double and a Boost.Multiprecision + type cpp_bin_float_50. +

+

+ Which is to say we're solving: +

+
4xE(sqrt(1 - 282 / x2)) - 300 = 0
+

+ In each case the target accuracy was set using our "recomended" + accuracy limits (or at least limits that make a good starting point - which + is likely to give close to full accuracy without resorting to unnecessary + iterations). +

+
++++ + + + + + + + + + + + + + + + + + + + + + + +
+

+ Function +

+
+

+ Precision Requested +

+
+

+ TOMS748 +

+
+

+ numeric_limits<T>::digits - 2 +

+
+

+ Newton +

+
+

+ floor(numeric_limits<T>::digits * 0.6) +

+
+

+ Halley +

+
+

+ floor(numeric_limits<T>::digits * 0.4) +

+
+

+ Schröder +

+
+

+ floor(numeric_limits<T>::digits * 0.4) +

+
+

+ Tests used Microsoft Visual Studio 2013 (Update 1) and GCC 4.9.1 using + source code root_elliptic_finding.cpp. +

+

+ The timing uncertainty (especially using MSVC) is at least 5% of normalized + time 'Norm'. +

+

+ To pick out the 'best' and 'worst' algorithms are highlighted in blue and + red. More than one result can be 'best' when normalized times are indistinguishable + within the uncertainty. +

+
+ + Program + root_elliptic_finding.cpp, + Microsoft Visual C++ version 12.0, Dinkumware standard library version + 610, Win32 Compiled in optimise mode., _X86_SSE2 +
+
+

Table 12.12. root with radius 28 and arc length 300) for float, double, long + double and cpp_bin_float_50 types, using _X86_SSE2

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 5 +

+
+

+ 515 +

+
+

+ 1.43 +

+
+

+ -1 +

+
+ +

+ 9 +

+
+

+ 968 +

+
+

+ 1.82 +

+
+

+ 1 +

+
+ +

+ 9 +

+
+

+ 968 +

+
+

+ 1.82 +

+
+

+ 1 +

+
+ +

+ 11 +

+
+

+ 871875 +

+
+

+ 1.53 +

+
+

+ -3 +

+
+
+

+ Newton +

+
+

+ 3 +

+
+

+ 453 +

+
+

+ 1.26 +

+
+

+ -1 +

+
+ +

+ 4 +

+
+

+ 640 +

+
+

+ 1.21 +

+
+

+ 1 +

+
+ +

+ 4 +

+
+

+ 640 +

+
+

+ 1.21 +

+
+

+ 1 +

+
+ +

+ 5 +

+
+

+ 685937 +

+
+

+ 1.20 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 2 +

+
+

+ 359 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 531 +

+
+

+ 1.00 +

+
+

+ 3 +

+
+ +

+ 3 +

+
+

+ 531 +

+
+

+ 1.00 +

+
+

+ 3 +

+
+ +

+ 4 +

+
+

+ 570312 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 3 +

+
+

+ 484 +

+
+

+ 1.35 +

+
+

+ -1 +

+
+ +

+ 6 +

+
+

+ 1000 +

+
+

+ 1.88 +

+
+

+ 1 +

+
+ +

+ 6 +

+
+

+ 984 +

+
+

+ 1.85 +

+
+

+ 1 +

+
+ +

+ 5 +

+
+

+ 742187 +

+
+

+ 1.30 +

+
+

+ -2 +

+
+
+
+
+ + Program + root_elliptic_finding.cpp, + Microsoft Visual C++ version 12.0, Dinkumware standard library version + 610, Win32 Compiled in optimise mode., _X64_AVX +
+
+

Table 12.13. root with radius 28 and arc length 300) for float, double, long + double and cpp_bin_float_50 types, using _X64_AVX

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 5 +

+
+

+ 500 +

+
+

+ 1.33 +

+
+

+ -1 +

+
+ +

+ 9 +

+
+

+ 1046 +

+
+

+ 1.72 +

+
+

+ 1 +

+
+ +

+ 9 +

+
+

+ 1062 +

+
+

+ 1.70 +

+
+

+ 1 +

+
+ +

+ 11 +

+
+

+ 698437 +

+
+

+ 1.54 +

+
+

+ -3 +

+
+
+

+ Newton +

+
+

+ 3 +

+
+

+ 484 +

+
+

+ 1.29 +

+
+

+ -1 +

+
+ +

+ 4 +

+
+

+ 734 +

+
+

+ 1.21 +

+
+

+ 1 +

+
+ +

+ 4 +

+
+

+ 687 +

+
+

+ 1.10 +

+
+

+ 1 +

+
+ +

+ 5 +

+
+

+ 545312 +

+
+

+ 1.20 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 2 +

+
+

+ 375 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 609 +

+
+

+ 1.00 +

+
+

+ 3 +

+
+ +

+ 3 +

+
+

+ 625 +

+
+

+ 1.00 +

+
+

+ 3 +

+
+ +

+ 4 +

+
+

+ 453125 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 3 +

+
+

+ 546 +

+
+

+ 1.46 +

+
+

+ -1 +

+
+ +

+ 6 +

+
+

+ 1109 +

+
+

+ 1.82 +

+
+

+ 1 +

+
+ +

+ 6 +

+
+

+ 1187 +

+
+

+ 1.90 +

+
+

+ 1 +

+
+ +

+ 5 +

+
+

+ 564062 +

+
+

+ 1.24 +

+
+

+ -2 +

+
+
+
+
+ + Program + root_elliptic_finding.cpp, + GNU C++ version 4.9.2, GNU libstdc++ version 20141030, Win32 Compiled in + optimise mode., _X64_SSE2 +
+
+

Table 12.14. root with radius 28 and arc length 300) for float, double, long + double and cpp_bin_float_50 types, using _X64_SSE2

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 5 +

+
+

+ 328 +

+
+

+ 1.31 +

+
+

+ -1 +

+
+ +

+ 8 +

+
+

+ 875 +

+
+

+ 1.51 +

+
+

+ 0 +

+
+ +

+ 8 +

+
+

+ 1109 +

+
+

+ 1.69 +

+
+

+ 4 +

+
+ +

+ 11 +

+
+

+ 479687 +

+
+

+ 1.49 +

+
+

+ -3 +

+
+
+

+ Newton +

+
+

+ 3 +

+
+

+ 328 +

+
+

+ 1.31 +

+
+

+ -1 +

+
+ +

+ 4 +

+
+

+ 671 +

+
+

+ 1.16 +

+
+

+ 1 +

+
+ +

+ 4 +

+
+

+ 781 +

+
+

+ 1.19 +

+
+

+ 1 +

+
+ +

+ 5 +

+
+

+ 387500 +

+
+

+ 1.20 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 2 +

+
+

+ 250 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 578 +

+
+

+ 1.00 +

+
+

+ 1 +

+
+ +

+ 3 +

+
+

+ 656 +

+
+

+ 1.00 +

+
+

+ 7 +

+
+ +

+ 4 +

+
+

+ 321875 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 3 +

+
+

+ 375 +

+
+

+ 1.50 +

+
+

+ -1 +

+
+ +

+ 4 +

+
+

+ 734 +

+
+

+ 1.27 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 828 +

+
+

+ 1.26 +

+
+

+ 3 +

+
+ +

+ 5 +

+
+

+ 414062 +

+
+

+ 1.29 +

+
+

+ -2 +

+
+
+
+

+ Remarks: +

+
    +
  • + The function being solved is now moderately expensive to call, and + twice as expensive to call when obtaining the derivative than when + not. Consequently there is very little improvement in moving from a + derivative free method, to Newton iteration. However, once you've calculated + the first derivative the second comes almost for free, consequently + the third order methods (Halley) does much the best. +
  • +
  • + Of the two second order methods, Halley does best as would be expected: + the Schroder method offers better guarantees of quadratic + convergence, while Halley relies on a smooth function with a single + root to give cubic convergence. It's not entirely + clear why Schroder iteration often does worse than Newton. +
  • +
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/root_n_comparison.html b/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/root_n_comparison.html new file mode 100644 index 00000000..33cecd49 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_comparison/root_n_comparison.html @@ -0,0 +1,5286 @@ + + + +Comparison of Nth-root Finding Algorithms + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ A second example compares four generalized nth-root finding algorithms + for various n-th roots (5, 7 and 13) of a single value 28.0, for four floating-point + types, float, double, long + double and a Boost.Multiprecision + type cpp_bin_float_50. + In each case the target accuracy was set using our "recomended" + accuracy limits (or at least limits that make a good starting point - which + is likely to give close to full accuracy without resorting to unnecessary + iterations). +

+
++++ + + + + + + + + + + + + + + + + + + + + + + +
+

+ Function +

+
+

+ Precision Requested +

+
+

+ TOMS748 +

+
+

+ numeric_limits<T>::digits - 2 +

+
+

+ Newton +

+
+

+ floor(numeric_limits<T>::digits * 0.6) +

+
+

+ Halley +

+
+

+ floor(numeric_limits<T>::digits * 0.4) +

+
+

+ Schröder +

+
+

+ floor(numeric_limits<T>::digits * 0.4) +

+
+

+ Tests used Microsoft Visual Studio 2013 (Update 1) and GCC 4.9.1 using + source code root_n_finding_algorithms.cpp. +

+

+ The timing uncertainty (especially using MSVC) is at least 5% of normalized + time 'Norm'. +

+

+ To pick out the 'best' and 'worst' algorithms are highlighted in blue and + red. More than one result can be 'best' when normalized times are indistinguishable + within the uncertainty. +

+
+ + Program + root_n_finding_algorithms.cpp, Microsoft Visual C++ version 12.0, Dinkumware + standard library version 610, Win32 Compiled in optimise mode., _X86_SSE2 +
+

+ Fraction of full accuracy 1 +

+
+

Table 12.3. 5th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X86_SSE2

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 7 +

+
+

+ 320 +

+
+

+ 1.53 +

+
+

+ 0 +

+
+ +

+ 11 +

+
+

+ 576 +

+
+

+ 2.61 +

+
+

+ 1 +

+
+ +

+ 11 +

+
+

+ 557 +

+
+

+ 2.48 +

+
+

+ 1 +

+
+ +

+ 12 +

+
+

+ 119843 +

+
+

+ 7.52 +

+
+

+ 0 +

+
+
+

+ Newton +

+
+

+ 3 +

+
+

+ 209 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 221 +

+
+

+ 1.00 +

+
+

+ -1 +

+
+ +

+ 4 +

+
+

+ 225 +

+
+

+ 1.00 +

+
+

+ -1 +

+
+ +

+ 6 +

+
+

+ 15937 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 2 +

+
+

+ 214 +

+
+

+ 1.02 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 256 +

+
+

+ 1.16 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 243 +

+
+

+ 1.08 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 28437 +

+
+

+ 1.78 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 2 +

+
+

+ 218 +

+
+

+ 1.04 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 245 +

+
+

+ 1.11 +

+
+

+ -1 +

+
+ +

+ 3 +

+
+

+ 245 +

+
+

+ 1.09 +

+
+

+ -1 +

+
+ +

+ 4 +

+
+

+ 35625 +

+
+

+ 2.24 +

+
+

+ 0 +

+
+
+
+
+

Table 12.4. 7th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X86_SSE2

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 12 +

+
+

+ 493 +

+
+

+ 2.18 +

+
+

+ 1 +

+
+ +

+ 15 +

+
+

+ 762 +

+
+

+ 3.05 +

+
+

+ 2 +

+
+ +

+ 15 +

+
+

+ 765 +

+
+

+ 3.08 +

+
+

+ 2 +

+
+ +

+ 14 +

+
+

+ 157343 +

+
+

+ 7.09 +

+
+

+ 0 +

+
+
+

+ Newton +

+
+

+ 5 +

+
+

+ 226 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 250 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 248 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 8 +

+
+

+ 22187 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 4 +

+
+

+ 257 +

+
+

+ 1.14 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 293 +

+
+

+ 1.17 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 293 +

+
+

+ 1.18 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 44062 +

+
+

+ 1.99 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 5 +

+
+

+ 285 +

+
+

+ 1.26 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 317 +

+
+

+ 1.27 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 317 +

+
+

+ 1.28 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 61406 +

+
+

+ 2.77 +

+
+

+ 0 +

+
+
+
+
+

Table 12.5. 11th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X86_SSE2

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 12 +

+
+

+ 556 +

+
+

+ 2.24 +

+
+

+ -2 +

+
+ +

+ 14 +

+
+

+ 784 +

+
+

+ 2.94 +

+
+

+ 2 +

+
+ +

+ 14 +

+
+

+ 793 +

+
+

+ 2.94 +

+
+

+ 2 +

+
+ +

+ 17 +

+
+

+ 235781 +

+
+

+ 8.88 +

+
+

+ 2 +

+
+
+

+ Newton +

+
+

+ 6 +

+
+

+ 248 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 267 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 270 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 9 +

+
+

+ 26562 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 4 +

+
+

+ 254 +

+
+

+ 1.02 +

+
+

+ -1 +

+
+ +

+ 5 +

+
+

+ 290 +

+
+

+ 1.09 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 293 +

+
+

+ 1.09 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 46406 +

+
+

+ 1.75 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 6 +

+
+

+ 312 +

+
+

+ 1.26 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 351 +

+
+

+ 1.31 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 356 +

+
+

+ 1.32 +

+
+

+ 0 +

+
+ +

+ 8 +

+
+

+ 76250 +

+
+

+ 2.87 +

+
+

+ 0 +

+
+
+
+
+ + Program + root_n_finding_algorithms.cpp, Microsoft Visual C++ version 12.0, Dinkumware + standard library version 610, Win32 Compiled in optimise mode., _X64_AVX +
+

+ Fraction of full accuracy 1 +

+
+

Table 12.6. 5th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X64_AVX

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 7 +

+
+

+ 239 +

+
+

+ 1.50 +

+
+

+ 0 +

+
+ +

+ 11 +

+
+

+ 451 +

+
+

+ 2.53 +

+
+

+ 1 +

+
+ +

+ 11 +

+
+

+ 439 +

+
+

+ 2.49 +

+
+

+ 1 +

+
+ +

+ 12 +

+
+

+ 90312 +

+
+

+ 7.51 +

+
+

+ 0 +

+
+
+

+ Newton +

+
+

+ 3 +

+
+

+ 159 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 178 +

+
+

+ 1.00 +

+
+

+ -1 +

+
+ +

+ 4 +

+
+

+ 176 +

+
+

+ 1.00 +

+
+

+ -1 +

+
+ +

+ 6 +

+
+

+ 12031 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 2 +

+
+

+ 168 +

+
+

+ 1.06 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 203 +

+
+

+ 1.14 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 198 +

+
+

+ 1.13 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 20937 +

+
+

+ 1.74 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 2 +

+
+

+ 173 +

+
+

+ 1.09 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 206 +

+
+

+ 1.16 +

+
+

+ -1 +

+
+ +

+ 3 +

+
+

+ 203 +

+
+

+ 1.15 +

+
+

+ -1 +

+
+ +

+ 4 +

+
+

+ 26250 +

+
+

+ 2.18 +

+
+

+ 0 +

+
+
+
+
+

Table 12.7. 7th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X64_AVX

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 12 +

+
+

+ 385 +

+
+

+ 2.19 +

+
+

+ 1 +

+
+ +

+ 15 +

+
+

+ 635 +

+
+

+ 3.13 +

+
+

+ 2 +

+
+ +

+ 15 +

+
+

+ 621 +

+
+

+ 3.17 +

+
+

+ 2 +

+
+ +

+ 14 +

+
+

+ 114843 +

+
+

+ 6.81 +

+
+

+ 0 +

+
+
+

+ Newton +

+
+

+ 5 +

+
+

+ 176 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 203 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 196 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 8 +

+
+

+ 16875 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 4 +

+
+

+ 209 +

+
+

+ 1.19 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 254 +

+
+

+ 1.25 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 246 +

+
+

+ 1.26 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 32343 +

+
+

+ 1.92 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 5 +

+
+

+ 223 +

+
+

+ 1.27 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 273 +

+
+

+ 1.34 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 275 +

+
+

+ 1.40 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 45156 +

+
+

+ 2.68 +

+
+

+ 0 +

+
+
+
+
+

Table 12.8. 11th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X64_AVX

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 12 +

+
+

+ 467 +

+
+

+ 2.42 +

+
+

+ -2 +

+
+ +

+ 14 +

+
+

+ 648 +

+
+

+ 3.06 +

+
+

+ 2 +

+
+ +

+ 14 +

+
+

+ 640 +

+
+

+ 2.99 +

+
+

+ 2 +

+
+ +

+ 17 +

+
+

+ 170000 +

+
+

+ 8.85 +

+
+

+ 2 +

+
+
+

+ Newton +

+
+

+ 6 +

+
+

+ 193 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 212 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 214 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 9 +

+
+

+ 19218 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 4 +

+
+

+ 209 +

+
+

+ 1.08 +

+
+

+ -1 +

+
+ +

+ 5 +

+
+

+ 256 +

+
+

+ 1.21 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 250 +

+
+

+ 1.17 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 32656 +

+
+

+ 1.70 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 6 +

+
+

+ 248 +

+
+

+ 1.28 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 306 +

+
+

+ 1.44 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 298 +

+
+

+ 1.39 +

+
+

+ 0 +

+
+ +

+ 8 +

+
+

+ 53437 +

+
+

+ 2.78 +

+
+

+ 0 +

+
+
+
+
+ + Program + root_n_finding_algorithms.cpp, GNU C++ version 4.9.2, GNU libstdc++ version + 20141030, Win32 Compiled in optimise mode., _X64_SSE2 +
+

+ Fraction of full accuracy 1 +

+
+

Table 12.9. 5th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X64_SSE2

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 7 +

+
+

+ 193 +

+
+

+ 2.14 +

+
+

+ 0 +

+
+ +

+ 11 +

+
+

+ 432 +

+
+

+ 3.86 +

+
+

+ 1 +

+
+ +

+ 9 +

+
+

+ 579 +

+
+

+ 3.83 +

+
+

+ 0 +

+
+ +

+ 12 +

+
+

+ 59062 +

+
+

+ 7.56 +

+
+

+ 0 +

+
+
+

+ Newton +

+
+

+ 3 +

+
+

+ 90 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 112 +

+
+

+ 1.00 +

+
+

+ -1 +

+
+ +

+ 5 +

+
+

+ 151 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 7812 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 2 +

+
+

+ 98 +

+
+

+ 1.09 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 135 +

+
+

+ 1.21 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 201 +

+
+

+ 1.33 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 13750 +

+
+

+ 1.76 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 2 +

+
+

+ 112 +

+
+

+ 1.24 +

+
+

+ 0 +

+
+ +

+ 3 +

+
+

+ 142 +

+
+

+ 1.27 +

+
+

+ -1 +

+
+ +

+ 3 +

+
+

+ 206 +

+
+

+ 1.36 +

+
+

+ 0 +

+
+ +

+ 4 +

+
+

+ 17031 +

+
+

+ 2.18 +

+
+

+ 0 +

+
+
+
+
+

Table 12.10. 7th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X64_SSE2

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 12 +

+
+

+ 351 +

+
+

+ 1.97 +

+
+

+ 1 +

+
+ +

+ 15 +

+
+

+ 621 +

+
+

+ 3.18 +

+
+

+ 2 +

+
+ +

+ 13 +

+
+

+ 906 +

+
+

+ 3.61 +

+
+

+ 0 +

+
+ +

+ 14 +

+
+

+ 75468 +

+
+

+ 7.10 +

+
+

+ 0 +

+
+
+

+ Newton +

+
+

+ 5 +

+
+

+ 178 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 195 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 251 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 8 +

+
+

+ 10625 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 4 +

+
+

+ 196 +

+
+

+ 1.10 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 242 +

+
+

+ 1.24 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 345 +

+
+

+ 1.37 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 21093 +

+
+

+ 1.99 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 5 +

+
+

+ 225 +

+
+

+ 1.26 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 270 +

+
+

+ 1.38 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 384 +

+
+

+ 1.53 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 29062 +

+
+

+ 2.74 +

+
+

+ 0 +

+
+
+
+
+

Table 12.11. 11th root(28) for float, double, long double and cpp_bin_float_50 + types, using _X64_SSE2

+
+++++++++++++++++++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

+ float +

+
+ + + + +

+ double +

+
+ + + + +

+ long d +

+
+ + + + +

+ cpp50 +

+
+ +   
+

+ Algo +

+
+

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+ +

+ Its +

+
+

+ Times +

+
+

+ Norm +

+
+

+ Dis +

+
+
+

+ TOMS748 +

+
+

+ 12 +

+
+

+ 429 +

+
+

+ 2.22 +

+
+

+ -2 +

+
+ +

+ 14 +

+
+

+ 679 +

+
+

+ 3.02 +

+
+

+ 2 +

+
+ +

+ 14 +

+
+

+ 1098 +

+
+

+ 3.94 +

+
+

+ 1 +

+
+ +

+ 17 +

+
+

+ 114531 +

+
+

+ 8.83 +

+
+

+ 2 +

+
+
+

+ Newton +

+
+

+ 6 +

+
+

+ 193 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 225 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 279 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+ +

+ 9 +

+
+

+ 12968 +

+
+

+ 1.00 +

+
+

+ 0 +

+
+
+

+ Halley +

+
+

+ 4 +

+
+

+ 196 +

+
+

+ 1.02 +

+
+

+ -1 +

+
+ +

+ 5 +

+
+

+ 248 +

+
+

+ 1.10 +

+
+

+ 0 +

+
+ +

+ 5 +

+
+

+ 348 +

+
+

+ 1.25 +

+
+

+ 0 +

+
+ +

+ 6 +

+
+

+ 21718 +

+
+

+ 1.67 +

+
+

+ 0 +

+
+
+

+ Schröder +

+
+

+ 6 +

+
+

+ 254 +

+
+

+ 1.32 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 323 +

+
+

+ 1.44 +

+
+

+ 0 +

+
+ +

+ 7 +

+
+

+ 453 +

+
+

+ 1.62 +

+
+

+ 0 +

+
+ +

+ 8 +

+
+

+ 35625 +

+
+

+ 2.75 +

+
+

+ 0 +

+
+
+
+

+ Some tentative conclusions can be drawn from this limited exercise. +

+
    +
  • + Perhaps surprisingly, there is little difference between the various + algorithms for fundamental + types floating-point types. Using the first derivatives (Newton-Raphson iteration) + is usually the best, but while the improvement over the no-derivative + TOMS Algorithm + 748: enclosing zeros of continuous functions is considerable + in number of iterations, but little in execution time. This reflects + the fact that the function we are finding the root for is trivial to + evaluate, so runtimetimes are dominated by the time taken by the boilerplate + code in each method. +
  • +
  • + The extra cost of evaluating the second derivatives (Halley + or Schröder) + is usually too much for any net benefit: as with the cube root, these + functors are so cheap to evaluate that the runtime is largely dominated + by the complexity of the root finding method. +
  • +
  • + For a Boost.Multiprecision + floating-point type, the Newton-Raphson + iteration is a clear winner with a several-fold gain over TOMS Algorithm 748: + enclosing zeros of continuous functions, and again no improvement + from the second-derivative algorithms. +
  • +
  • + The run-time of 50 decimal-digit Boost.Multiprecision + is about 30-fold greater than double. +
  • +
  • + The column 'dis' showing the number of bits distance from the correct + result. The Newton-Raphson algorithm shows a bit or two better accuracy + than TOMS + Algorithm 748: enclosing zeros of continuous functions. +
  • +
  • + The goodness of the 'guess' is especially crucial for Boost.Multiprecision. + Separate experiments show that evaluating the 'guess' using double allows convergence to the final + exact result in one or two iterations. So in this contrived example, + crudely dividing the exponent by N for a 'guess', it would be far better + to use a pow<double> + or , if more precise pow<long double>, + function to estimate a 'guess'. The limitation of this tactic is that + the range of possible (exponent) values may be less than the multiprecision + type. +
  • +
  • + Using floating-point extension SSE2 + instructions made a modest ten-percent speedup. +
  • +
  • + Using MSVC, there was some improvement using 64-bit, markedly for + Boost.Multiprecision. +
  • +
  • + The GCC compiler 4.9.1 using 64-bit was at least five-folder faster + that 32-bit, apparently reflecting better optimization. +
  • +
+

+ Clearly, your mileage will vary, but in + summary, Newton-Raphson + iteration seems the first choice of algorithm, and effort to find + a good 'guess' the first speed-up target, especially for Boost.Multiprecision. + And of course, compiler optimisation is crucial for speed. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples.html b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples.html new file mode 100644 index 00000000..0028c77b --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples.html @@ -0,0 +1,90 @@ + + + +Examples of Root-Finding (with and without derivatives) + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ + +

+ The examples demonstrate how to use the various tools for root + finding. +

+

+ We start with the simple cube root function cbrt + ( C++ standard function name cbrt) + showing root finding without + derivatives. +

+

+ We then show how use of derivatives can improve the speed of convergence. +

+

+ (But these examples are only a demonstration and do not try to make the ultimate + improvements of an 'industrial-strength' implementation, for example, of + boost::math::cbrt, mainly by using a better computed + initial 'guess' at cbrt.hpp). +

+

+ Then we show how a higher root (fifth + root) 5√ can be computed, and in root_finding_n_example.cpp + a generic method for the nth + root that constructs the derivatives at compile-time. +

+

+ These methods should be applicable to other functions that can be differentiated + easily. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/5th_root_eg.html b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/5th_root_eg.html new file mode 100644 index 00000000..080062dc --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/5th_root_eg.html @@ -0,0 +1,169 @@ + + + +Computing the Fifth Root + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ Let's now suppose we want to find the fifth root + of a number a. +

+

+ The equation we want to solve is : +

+

+   f(x) = x5 -a +

+

+ If your differentiation is a little rusty (or you are faced with an function + whose complexity makes differentiation daunting), then you can get help, + for example, from the invaluable WolframAlpha + site. +

+

+ For example, entering the commmand: differentiate + x ^ + 5 +

+

+ or the Wolfram Language command: D[x ^ + 5, x] +

+

+ gives the output: d/dx(x + ^ 5) = 5 + x ^ + 4 +

+

+ and to get the second differential, enter: second + differentiate x + ^ 5 +

+

+ or the Wolfram Language command: D[x ^ + 5, { x, + 2 }] +

+

+ to get the output: d ^ + 2 / dx ^ 2(x + ^ 5) = 20 + x ^ + 3 +

+

+ To get a reference value, we can enter: fifth root 3126 +

+

+ or: N[3126 ^ (1 / 5), 50] +

+

+ to get a result with a precision of 50 decimal digits: +

+

+ 5.0003199590478625588206333405631053401128722314376 +

+

+ (We could also get a reference value using multiprecision + root). +

+

+ The 1st and 2nd derivatives of x5 are: +

+

+   f'(x) = 5x4 +

+

+   f''(x) = 20x3 +

+

+ Using these expressions for the derivatives, the functor is: +

+
template <class T>
+struct fifth_functor_2deriv
+{
+  // Functor returning both 1st and 2nd derivatives.
+  fifth_functor_2deriv(T const& to_find_root_of) : a(to_find_root_of)
+  { /* Constructor stores value a to find root of, for example: */ }
+
+  std::tuple<T, T, T> operator()(T const& x)
+  {
+    // Return both f(x) and f'(x) and f''(x).
+    T fx = boost::math::pow<5>(x) - a;    // Difference (estimate x^3 - value).
+    T dx = 5 * boost::math::pow<4>(x);    // 1st derivative = 5x^4.
+    T d2x = 20 * boost::math::pow<3>(x);  // 2nd derivative = 20 x^3
+    return std::make_tuple(fx, dx, d2x);  // 'return' fx, dx and d2x.
+  }
+private:
+  T a;                                    // to be 'fifth_rooted'.
+}; // struct fifth_functor_2deriv
+
+

+ Our fifth-root function is now: +

+
template <class T>
+T fifth_2deriv(T x)
+{
+  // return fifth root of x using 1st and 2nd derivatives and Halley.
+  using namespace std;                  // Help ADL of std functions.
+  using namespace boost::math::tools;   // for halley_iterate.
+
+  int exponent;
+  frexp(x, &exponent);                  // Get exponent of z (ignore mantissa).
+  T guess = ldexp(1., exponent / 5);    // Rough guess is to divide the exponent by five.
+  T min = ldexp(0.5, exponent / 5);     // Minimum possible value is half our guess.
+  T max = ldexp(2., exponent / 5);      // Maximum possible value is twice our guess.
+  // Stop when slightly more than one of the digits are correct:
+  const int digits = static_cast<int>(std::numeric_limits<T>::digits * 0.4);
+  const boost::uintmax_t maxit = 50;
+  boost::uintmax_t it = maxit;
+  T result = halley_iterate(fifth_functor_2deriv<T>(x), guess, min, max, digits, it);
+  return result;
+}
+
+

+ Full code of this example is at root_finding_example.cpp + and root_finding_n_example.cpp. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/cbrt_eg.html b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/cbrt_eg.html new file mode 100644 index 00000000..61e5cae0 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/cbrt_eg.html @@ -0,0 +1,486 @@ + + + +Finding the Cubed Root With and Without Derivatives + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ First some #includes + that will be needed. +

+
#include <boost/math/tools/roots.hpp>
+//using boost::math::policies::policy;
+//using boost::math::tools::newton_raphson_iterate;
+//using boost::math::tools::halley_iterate; //
+//using boost::math::tools::eps_tolerance; // Binary functor for specified number of bits.
+//using boost::math::tools::bracket_and_solve_root;
+//using boost::math::tools::toms748_solve;
+
+#include <boost/math/special_functions/next.hpp> // For float_distance.
+#include <tuple> // for std::tuple and std::make_tuple.
+#include <boost/math/special_functions/cbrt.hpp> // For boost::math::cbrt.
+
+
+ + + + + +
[Tip]Tip

+ For clarity, using statements + are provided to list what functions are being used in this example: you + can, of course, partly or fully qualify the names in other ways. (For + your application, you may wish to extract some parts into header files, + but you should never use using + statements globally in header files). +

+

+ Let's suppose we want to find the root of a number a, + and to start, compute the cube root. +

+

+ So the equation we want to solve is: +

+

+    f(x) = x³ -a +

+

+ We will first solve this without using any information about the slope + or curvature of the cube root function. +

+

+ Fortunately, the cube-root function is 'Really Well Behaved' in that it + is monotonic and has only one root (we leave negative values 'as an exercise + for the student'). +

+

+ We then show how adding what we can know about this function, first just + the slope or 1st derivative f'(x), will speed homing + in on the solution. +

+

+ Lastly, we show how adding the curvature f''(x) too + will speed convergence even more. +

+

+ + Cube + root function without derivatives +

+

+ First we define a function object (functor): +

+
template <class T>
+struct cbrt_functor_noderiv
+{
+  //  cube root of x using only function - no derivatives.
+  cbrt_functor_noderiv(T const& to_find_root_of) : a(to_find_root_of)
+  { /* Constructor just stores value a to find root of. */ }
+  T operator()(T const& x)
+  {
+    T fx = x*x*x - a; // Difference (estimate x^3 - a).
+    return fx;
+  }
+private:
+  T a; // to be 'cube_rooted'.
+};
+
+

+ Implementing the cube-root function itself is fairly trivial now: the hardest + part is finding a good approximation to begin with. In this case we'll + just divide the exponent by three. (There are better but more complex guess + algorithms used in 'real life'.) +

+
template <class T>
+T cbrt_noderiv(T x)
+{
+  // return cube root of x using bracket_and_solve (no derivatives).
+  using namespace std;                          // Help ADL of std functions.
+  using namespace boost::math::tools;           // For bracket_and_solve_root.
+
+  int exponent;
+  frexp(x, &exponent);                          // Get exponent of z (ignore mantissa).
+  T guess = ldexp(1., exponent/3);              // Rough guess is to divide the exponent by three.
+  T factor = 2;                                 // How big steps to take when searching.
+
+  const boost::uintmax_t maxit = 20;            // Limit to maximum iterations.
+  boost::uintmax_t it = maxit;                  // Initally our chosen max iterations, but updated with actual.
+  bool is_rising = true;                        // So if result if guess^3 is too low, then try increasing guess.
+  int digits = std::numeric_limits<T>::digits;  // Maximum possible binary digits accuracy for type T.
+  // Some fraction of digits is used to control how accurate to try to make the result.
+  int get_digits = digits - 3;                  // We have to have a non-zero interval at each step, so
+                                                // maximum accuracy is digits - 1.  But we also have to
+                                                // allow for inaccuracy in f(x), otherwise the last few
+                                                // iterations just thrash around.
+  eps_tolerance<T> tol(get_digits);             // Set the tolerance.
+  std::pair<T, T> r = bracket_and_solve_root(cbrt_functor_noderiv<T>(x), guess, factor, is_rising, tol, it);
+  return r.first + (r.second - r.first)/2;      // Midway between brackets is our result, if necessary we could
+                                                // return the result as an interval here.
+}
+
+
+ + + + + +
[Note]Note
+

+ The final parameter specifying a maximum number of iterations is optional. + However, it defaults to boost::uintmax_t + maxit = + (std::numeric_limits<boost::uintmax_t>::max)(); which is 18446744073709551615 + and is more than anyone would wish to wait for! +

+

+ So it may be wise to chose some reasonable estimate of how many iterations + may be needed, In this case the function is so well behaved that we can + chose a low value of 20. +

+

+ Internally when Boost.Math uses these functions, it sets the maximum + iterations to policies::get_max_root_iterations<Policy>();. +

+
+

+ Should we have wished we can show how many iterations were used in bracket_and_solve_root (this information + is lost outside cbrt_noderiv), + for example with: +

+
if (it >= maxit)
+{
+  std::cout << "Unable to locate solution in " << maxit << " iterations:"
+    " Current best guess is between " << r.first << " and " << r.second << std::endl;
+}
+else
+{
+  std::cout << "Converged after " << it << " (from maximum of " << maxit << " iterations)." << std::endl;
+}
+
+

+ for output like +

+
Converged after 11 (from maximum of 20 iterations).
+
+

+ This snippet from main() in root_finding_example.cpp + shows how it can be used. +

+
try
+{
+  double threecubed = 27.;   // Value that has an *exactly representable* integer cube root.
+  double threecubedp1 = 28.; // Value whose cube root is *not* exactly representable.
+
+  std::cout << "cbrt(28) " << boost::math::cbrt(28.) << std::endl; // boost::math:: version of cbrt.
+  std::cout << "std::cbrt(28) " << std::cbrt(28.) << std::endl;    // std:: version of cbrt.
+  std::cout <<" cast double " << static_cast<double>(3.0365889718756625194208095785056696355814539772481111) << std::endl;
+
+  // Cube root using bracketing:
+  double r = cbrt_noderiv(threecubed);
+  std::cout << "cbrt_noderiv(" << threecubed << ") = " << r << std::endl;
+  r = cbrt_noderiv(threecubedp1);
+  std::cout << "cbrt_noderiv(" << threecubedp1 << ") = " << r << std::endl;
+
+
  cbrt_noderiv(27) = 3
+  cbrt_noderiv(28) = 3.0365889718756618
+
+

+ The result of bracket_and_solve_root + is a pair + of values that could be displayed. +

+

+ The number of bits separating them can be found using float_distance(r.first, r.second). The distance is zero (closest representable) + for 33 = 27 but float_distance(r.first, r.second) = 3 + for cube root of 28 with this function. The result (avoiding overflow) + is midway between these two values. +

+

+ + Cube + root function with 1st derivative (slope) +

+

+ We now solve the same problem, but using more information about the function, + to show how this can speed up finding the best estimate of the root. +

+

+ For the root function, the 1st differential (the slope of the tangent to + a curve at any point) is known. +

+

+ This algorithm is similar to this nth + root algorithm. +

+

+ If you need some reminders, then derivatives + of elementary functions may help. +

+

+ Using the rule that the derivative of xn for positive + n (actually all nonzero n) is n xn-1, allows us to + get the 1st differential as 3x2. +

+

+ To see how this extra information is used to find a root, view Newton-Raphson + iterations and the animation. +

+

+ We define a better functor cbrt_functor_deriv + that returns both the evaluation of the function to solve, along with its + first derivative: +

+

+ To 'return' two values, we use a std::pair + of floating-point values. +

+
template <class T>
+struct cbrt_functor_deriv
+{ // Functor also returning 1st derivative.
+  cbrt_functor_deriv(T const& to_find_root_of) : a(to_find_root_of)
+  { // Constructor stores value a to find root of,
+    // for example: calling cbrt_functor_deriv<T>(a) to use to get cube root of a.
+  }
+  std::pair<T, T> operator()(T const& x)
+  {
+    // Return both f(x) and f'(x).
+    T fx = x*x*x - a;                // Difference (estimate x^3 - value).
+    T dx =  3 * x*x;                 // 1st derivative = 3x^2.
+    return std::make_pair(fx, dx);   // 'return' both fx and dx.
+  }
+private:
+  T a;                               // Store value to be 'cube_rooted'.
+};
+
+

+ Our cube root function is now: +

+
template <class T>
+T cbrt_deriv(T x)
+{
+  // return cube root of x using 1st derivative and Newton_Raphson.
+  using namespace boost::math::tools;
+  int exponent;
+  frexp(x, &exponent);                                // Get exponent of z (ignore mantissa).
+  T guess = ldexp(1., exponent/3);                    // Rough guess is to divide the exponent by three.
+  T min = ldexp(0.5, exponent/3);                     // Minimum possible value is half our guess.
+  T max = ldexp(2., exponent/3);                      // Maximum possible value is twice our guess.
+  const int digits = std::numeric_limits<T>::digits;  // Maximum possible binary digits accuracy for type T.
+  int get_digits = static_cast<int>(digits * 0.6);    // Accuracy doubles with each step, so stop when we have
+                                                      // just over half the digits correct.
+  const boost::uintmax_t maxit = 20;
+  boost::uintmax_t it = maxit;
+  T result = newton_raphson_iterate(cbrt_functor_deriv<T>(x), guess, min, max, get_digits, it);
+  return result;
+}
+
+

+ The result of newton_raphson_iterate function + is a single value. +

+
+ + + + + +
[Tip]Tip

+ There is a compromise between accuracy and speed when chosing the value + of digits. It is tempting + to simply chose std::numeric_limits<T>::digits, but this may mean some inefficient + and unnecessary iterations as the function thrashes around trying to + locate the last bit. In theory, since the precision doubles with each + step it is sufficient to stop when half the bits are correct: as the + last step will have doubled that to full precision. Of course the function + has no way to tell if that is actually the case unless it does one more + step to be sure. In practice setting the precision to slightly more than + std::numeric_limits<T>::digits / + 2 is a good choice. +

+

+ Note that it is up to the caller of the function to check the iteration + count after the call to see if iteration stoped as a result of running + out of iterations rather than meeting the required precision. +

+

+ Using the test data in /test/test_cbrt.cpp + this found the cube root exact to the last digit in every case, and in + no more than 6 iterations at double precision. However, you will note that + a high precision was used in this example, exactly what was warned against + earlier on in these docs! In this particular case it is possible to compute + f(x) exactly and without undue cancellation error, + so a high limit is not too much of an issue. +

+

+ However, reducing the limit to std::numeric_limits<T>::digits + * 2 / 3 gave + full precision in all but one of the test cases (and that one was out by + just one bit). The maximum number of iterations remained 6, but in most + cases was reduced by one. +

+

+ Note also that the above code omits a probable optimization by computing + z² +and reusing it, omits error handling, and does not handle negative values + of z correctly. (These are left as the customary exercise for the reader!) +

+

+ The boost::math::cbrt function also includes these and + other improvements: most importantly it uses a much better initial guess + which reduces the iteration count to just 1 in almost all cases. +

+

+ + Cube + root with 1st & 2nd derivative (slope & curvature) +

+

+ Next we define yet another even better functor cbrt_functor_2deriv + that returns both the evaluation of the function to solve, along with its + first and second derivative: +

+

+   f''(x) = 6x +

+

+ using information about both slope and curvature to speed convergence. +

+

+ To 'return' three values, we use a tuple + of three floating-point values: +

+
template <class T>
+struct cbrt_functor_2deriv
+{
+  // Functor returning both 1st and 2nd derivatives.
+  cbrt_functor_2deriv(T const& to_find_root_of) : a(to_find_root_of)
+  { // Constructor stores value a to find root of, for example:
+    // calling cbrt_functor_2deriv<T>(x) to get cube root of x,
+  }
+  std::tuple<T, T, T> operator()(T const& x)
+  {
+    // Return both f(x) and f'(x) and f''(x).
+    T fx = x*x*x - a;                     // Difference (estimate x^3 - value).
+    T dx = 3 * x*x;                       // 1st derivative = 3x^2.
+    T d2x = 6 * x;                        // 2nd derivative = 6x.
+    return std::make_tuple(fx, dx, d2x);  // 'return' fx, dx and d2x.
+  }
+private:
+  T a; // to be 'cube_rooted'.
+};
+
+

+ Our cube root function is now: +

+
template <class T>
+T cbrt_2deriv(T x)
+{
+  // return cube root of x using 1st and 2nd derivatives and Halley.
+  //using namespace std;  // Help ADL of std functions.
+  using namespace boost::math::tools;
+  int exponent;
+  frexp(x, &exponent);                                // Get exponent of z (ignore mantissa).
+  T guess = ldexp(1., exponent/3);                    // Rough guess is to divide the exponent by three.
+  T min = ldexp(0.5, exponent/3);                     // Minimum possible value is half our guess.
+  T max = ldexp(2., exponent/3);                      // Maximum possible value is twice our guess.
+  const int digits = std::numeric_limits<T>::digits;  // Maximum possible binary digits accuracy for type T.
+  // digits used to control how accurate to try to make the result.
+  int get_digits = static_cast<int>(digits * 0.4);    // Accuracy triples with each step, so stop when just
+                                                      // over one third of the digits are correct.
+  boost::uintmax_t maxit = 20;
+  T result = halley_iterate(cbrt_functor_2deriv<T>(x), guess, min, max, get_digits, maxit);
+  return result;
+}
+
+

+ The function halley_iterate + also returns a single value, and the number of iterations will reveal if + it met the convergence criterion set by get_digits. +

+

+ The no-derivative method gives a result of +

+
cbrt_noderiv(28) = 3.0365889718756618
+
+

+ with a 3 bits distance between the bracketed values, whereas the derivative + methods both converge to a single value +

+
cbrt_2deriv(28) = 3.0365889718756627
+
+

+ which we can compare with the boost::math::cbrt +

+
cbrt(28)              = 3.0365889718756627
+
+

+ Note that the iterations are set to stop at just one-half of full precision, + and yet, even so, not one of the test cases had a single bit wrong. What's + more, the maximum number of iterations was now just 4. +

+

+ Just to complete the picture, we could have called schroder_iterate in the last example: + and in fact it makes no difference to the accuracy or number of iterations + in this particular case. However, the relative performance of these two + methods may vary depending upon the nature of f(x), + and the accuracy to which the initial guess can be computed. There appear + to be no generalisations that can be made except "try them and see". +

+

+ Finally, had we called cbrt + with NTL::RR set to + 1000 bit precision (about 300 decimal digits), then full precision can + be obtained with just 7 iterations. To put that in perspective, an increase + in precision by a factor of 20, has less than doubled the number of iterations. + That just goes to emphasise that most of the iterations are used up getting + the first few digits correct: after that these methods can churn out further + digits with remarkable efficiency. +

+

+ Or to put it another way: nothing beats a really good initial + guess! +

+

+ Full code of this example is at root_finding_example.cpp, +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/elliptic_eg.html b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/elliptic_eg.html new file mode 100644 index 00000000..7bd122f1 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/elliptic_eg.html @@ -0,0 +1,272 @@ + + + +A More complex example - Inverting the Elliptic Integrals + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ The arc length of an ellipse with radii a and b + is given by: +

+
L(a, b) = 4aE(k)
+

+ with: +

+
k = √(1 - b2/a2)
+

+ where E(k) is the complete elliptic integral of the + second kind - see ellint_2. +

+

+ Let's suppose we know the arc length and one radii, we can then calculate + the other radius by inverting the formula above. We'll begin by encoding + the above formula into a functor that our root-finding algorithms can call. +

+

+ Note that while not completely obvious from the formula above, the function + is completely symmetrical in the two radii - which can be interchanged + at will - in this case we need to make sure that a + >= b + so that we don't accidentally take the square root of a negative number: +

+
template <typename T = double>
+struct elliptic_root_functor_noderiv
+{ //  Nth root of x using only function - no derivatives.
+   elliptic_root_functor_noderiv(T const& arc, T const& radius) : m_arc(arc), m_radius(radius)
+   { // Constructor just stores value a to find root of.
+   }
+   T operator()(T const& x)
+   {
+      using std::sqrt;
+      // return the difference between required arc-length, and the calculated arc-length for an
+      // ellipse with radii m_radius and x:
+      T a = (std::max)(m_radius, x);
+      T b = (std::min)(m_radius, x);
+      T k = sqrt(1 - b * b / (a * a));
+      return 4 * a * boost::math::ellint_2(k) - m_arc;
+   }
+private:
+   T m_arc;     // length of arc.
+   T m_radius;  // one of the two radii of the ellipse
+}; // template <class T> struct elliptic_root_functor_noderiv
+
+

+ We'll also need a decent estimate to start searching from, the approximation: +

+
L(a, b) ≈ 4√(a2 + b2)
+

+ Is easily inverted to give us what we need, which using derivative-free + root finding leads to the algorithm: +

+
template <class T = double>
+T elliptic_root_noderiv(T radius, T arc)
+{ // return the other radius of an ellipse, given one radii and the arc-length
+   using namespace std;  // Help ADL of std functions.
+   using namespace boost::math::tools; // For bracket_and_solve_root.
+
+   T guess = sqrt(arc * arc / 16 - radius * radius);
+   T factor = 1.2;                     // How big steps to take when searching.
+
+   const boost::uintmax_t maxit = 50;  // Limit to maximum iterations.
+   boost::uintmax_t it = maxit;        // Initally our chosen max iterations, but updated with actual.
+   bool is_rising = true;              // arc-length increases if one radii increases, so function is rising
+   // Define a termination condition, stop when nearly all digits are correct, but allow for
+   // the fact that we are returning a range, and must have some inaccuracy in the elliptic integral:
+   eps_tolerance<T> tol(std::numeric_limits<T>::digits - 2);
+   // Call bracket_and_solve_root to find the solution, note that this is a rising function:
+   std::pair<T, T> r = bracket_and_solve_root(elliptic_root_functor_noderiv<T>(arc, radius), guess, factor, is_rising, tol, it);
+   // Result is midway between the endpoints of the range:
+   return r.first + (r.second - r.first) / 2;
+} // template <class T> T elliptic_root_noderiv(T x)
+
+

+ This function generally finds the root within 8-10 iterations, so given + that the runtime is completely dominated by the cost of calling the ellliptic + integral it would be nice to reduce that count somewhat. We'll try to do + that by using a derivative-based method; the derivatives of this function + are rather hard to work out by hand, but fortunately Wolfram + Alpha can do the grunt work for us to give: +

+
d/da L(a, b) = 4(a2E(k) - b2K(k)) / (a2 - b2)
+

+ Note that now we have two elliptic integral + calls to get the derivative, so our functor will be at least twice as expensive + to call as the derivative-free one above: we'll have to reduce the iteration + count quite substantially to make a difference! +

+

+ Here's the revised functor: +

+
template <class T = double>
+struct elliptic_root_functor_1deriv
+{ // Functor also returning 1st derviative.
+   BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
+
+   elliptic_root_functor_1deriv(T const& arc, T const& radius) : m_arc(arc), m_radius(radius)
+   { // Constructor just stores value a to find root of.
+   }
+   std::pair<T, T> operator()(T const& x)
+   {
+      using std::sqrt;
+      // Return the difference between required arc-length, and the calculated arc-length for an
+      // ellipse with radii m_radius and x, plus it's derivative.
+      // See http://www.wolframalpha.com/input/?i=d%2Fda+[4+*+a+*+EllipticE%281+-+b^2%2Fa^2%29]
+      // We require two elliptic integral calls, but from these we can calculate both
+      // the function and it's derivative:
+      T a = (std::max)(m_radius, x);
+      T b = (std::min)(m_radius, x);
+      T a2 = a * a;
+      T b2 = b * b;
+      T k = sqrt(1 - b2 / a2);
+      T Ek = boost::math::ellint_2(k);
+      T Kk = boost::math::ellint_1(k);
+      T fx = 4 * a * Ek - m_arc;
+      T dfx = 4 * (a2 * Ek - b2 * Kk) / (a2 - b2);
+      return std::make_pair(fx, dfx);
+   }
+private:
+   T m_arc;     // length of arc.
+   T m_radius;  // one of the two radii of the ellipse
+};  // struct elliptic_root__functor_1deriv
+
+

+ The root-finding code is now almost the same as before, but we'll make + use of Newton-iteration to get the result: +

+
template <class T = double>
+T elliptic_root_1deriv(T radius, T arc)
+{
+   using namespace std;  // Help ADL of std functions.
+   using namespace boost::math::tools; // For newton_raphson_iterate.
+
+   BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
+
+   T guess = sqrt(arc * arc / 16 - radius * radius);
+   T min = 0;   // Minimum possible value is zero.
+   T max = arc; // Maximum possible value is the arc length.
+
+   // Accuracy doubles at each step, so stop when just over half of the digits are
+   // correct, and rely on that step to polish off the remainder:
+   int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.6);
+   const boost::uintmax_t maxit = 20;
+   boost::uintmax_t it = maxit;
+   T result = newton_raphson_iterate(elliptic_root_functor_1deriv<T>(arc, radius), guess, min, max, get_digits, it);
+   return result;
+} // T elliptic_root_1_deriv  Newton-Raphson
+
+

+ The number of iterations required for double + precision is now usually around 4 - so we've slightly more than halved + the number of iterations, but made the functor twice as expensive to call! +

+

+ Interestingly though, the second derivative requires no more expensive + elliptic integral calls than the first does, in other words it comes essentially + "for free", in which case we might as well make use of it and + use Halley-iteration. This is quite a typical situation when inverting + special-functions. Here's the revised functor: +

+
template <class T = double>
+struct elliptic_root_functor_2deriv
+{ // Functor returning both 1st and 2nd derivatives.
+   BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
+
+   elliptic_root_functor_2deriv(T const& arc, T const& radius) : m_arc(arc), m_radius(radius) {}
+   std::tuple<T, T, T> operator()(T const& x)
+   {
+      using std::sqrt;
+      // Return the difference between required arc-length, and the calculated arc-length for an
+      // ellipse with radii m_radius and x, plus it's derivative.
+      // See http://www.wolframalpha.com/input/?i=d^2%2Fda^2+[4+*+a+*+EllipticE%281+-+b^2%2Fa^2%29]
+      // for the second derivative.
+      T a = (std::max)(m_radius, x);
+      T b = (std::min)(m_radius, x);
+      T a2 = a * a;
+      T b2 = b * b;
+      T k = sqrt(1 - b2 / a2);
+      T Ek = boost::math::ellint_2(k);
+      T Kk = boost::math::ellint_1(k);
+      T fx = 4 * a * Ek - m_arc;
+      T dfx = 4 * (a2 * Ek - b2 * Kk) / (a2 - b2);
+      T dfx2 = 4 * b2 * ((a2 + b2) * Kk - 2 * a2 * Ek) / (a * (a2 - b2) * (a2 - b2));
+      return std::make_tuple(fx, dfx, dfx2);
+   }
+private:
+   T m_arc;     // length of arc.
+   T m_radius;  // one of the two radii of the ellipse
+};
+
+

+ The actual root-finding code is almost the same as before, except we can + use Halley, rather than Newton iteration: +

+
template <class T = double>
+T elliptic_root_2deriv(T radius, T arc)
+{
+   using namespace std;                // Help ADL of std functions.
+   using namespace boost::math::tools; // For halley_iterate.
+
+   BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
+
+   T guess = sqrt(arc * arc / 16 - radius * radius);
+   T min = 0;                                   // Minimum possible value is zero.
+   T max = arc;                                 // radius can't be larger than the arc length.
+
+   // Accuracy triples at each step, so stop when just over one-third of the digits
+   // are correct, and the last iteration will polish off the remaining digits:
+   int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.4);
+   const boost::uintmax_t maxit = 20;
+   boost::uintmax_t it = maxit;
+   T result = halley_iterate(elliptic_root_functor_2deriv<T>(arc, radius), guess, min, max, get_digits, it);
+   return result;
+} // nth_2deriv Halley
+
+

+ While this function uses only slightly fewer iterations (typically around + 3) to find the root, compared to the original derivative-free method, we've + moved from 8-10 elliptic integral calls to 6. +

+

+ Full code of this example is at root_elliptic_finding.cpp. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/lambda.html b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/lambda.html new file mode 100644 index 00000000..62f952f4 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/lambda.html @@ -0,0 +1,78 @@ + + + +Using C++11 Lambda's + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ Since all the root finding functions accept a function-object, they can + be made to work (often in a lot less code) with C++11 lambda's. Here's + the much reduced code for our "toy" cube root function: +

+
template <class T>
+T cbrt_2deriv_lambda(T x)
+{
+   // return cube root of x using 1st and 2nd derivatives and Halley.
+   //using namespace std;  // Help ADL of std functions.
+   using namespace boost::math::tools;
+   int exponent;
+   frexp(x, &exponent);                                // Get exponent of z (ignore mantissa).
+   T guess = ldexp(1., exponent / 3);                    // Rough guess is to divide the exponent by three.
+   T min = ldexp(0.5, exponent / 3);                     // Minimum possible value is half our guess.
+   T max = ldexp(2., exponent / 3);                      // Maximum possible value is twice our guess.
+   const int digits = std::numeric_limits<T>::digits;  // Maximum possible binary digits accuracy for type T.
+   // digits used to control how accurate to try to make the result.
+   int get_digits = static_cast<int>(digits * 0.4);    // Accuracy triples with each step, so stop when just
+   // over one third of the digits are correct.
+   boost::uintmax_t maxit = 20;
+   T result = halley_iterate(
+      // lambda function:
+      [x](const T& g){ return std::make_tuple(g * g * g - x, 3 * g * g, 6 * g); },
+      guess, min, max, get_digits, maxit);
+   return result;
+}
+
+

+ Full code of this example is at root_finding_example.cpp, +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/multiprecision_root.html b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/multiprecision_root.html new file mode 100644 index 00000000..aad8bfd7 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/multiprecision_root.html @@ -0,0 +1,291 @@ + + + +Root-finding using Boost.Multiprecision + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ The apocryphally astute reader might, by now, be asking "How do we + know if this computes the 'right' answer?". +

+

+ For most values, there is, sadly, no 'right' answer. This is because values + can only rarely be exactly represented by C++ floating-point + types. What we do want is the 'best' representation - one that is the nearest + representable + value. (For more about how numbers are represented see Floating + point). +

+

+ Of course, we might start with finding an external reference source like + Wolfram Alpha, as above, + but this is not always possible. +

+

+ Another way to reassure is to compute 'reference' values at higher precision + with which to compare the results of our iterative computations using built-in + like double. They should agree + within the tolerance that was set. +

+

+ The result of static_casting + to double from a higher-precision + type like cpp_bin_float_50 + is guaranteed to be the nearest representable + double value. +

+

+ For example, the cube root functions in our example for cbrt(28.) + compute +

+

+ std::cbrt<double>(28.) = + 3.0365889718756627 +

+

+ WolframAlpha says 3.036588971875662519420809578505669635581453977248111123242141... +

+

+ static_cast<double>(3.03658897187566251942080957850) + = 3.0365889718756627 +

+

+ This example cbrt(28.) = + 3.0365889718756627 +

+
+ + + + + +
[Tip]Tip
+

+ To ensure that all potentially significant decimal digits are displayed + use std::numeric_limits<T>::max_digits10 (or if not available on + older platforms or compilers use 2+std::numeric_limits<double>::digits*3010/10000).
+

+

+ Ideally, values should agree to std::numeric-limits<T>::digits10 + decimal digits. +

+

+ This also means that a 'reference' value to be input + or static_cast should have + at least max_digits10 + decimal digits (17 for 64-bit double). +

+
+

+ If we wish to compute higher-precision values + then, on some platforms, we may be able to use long + double with a higher precision than + double to compare with the + very common double and/or + a more efficient built-in quad floating-point type like __float128. +

+

+ Almost all platforms can easily use Boost.Multiprecision, + for example, cpp_dec_float + or a binary type cpp_bin_float + types, to compute values at very much higher precision. +

+
+ + + + + +
[Note]Note

+ With multiprecision types, it is debatable whether to use the type T for computing the initial guesses. + Type double is like to be + accurate enough for the method used in these examples. This would limit + the exponent range of possible values to that of double. + There is also the cost of conversion to and from type T + to consider. In these examples, double + is used via typedef double + guess_type. +

+

+ Since the functors and functions used above are templated on the value + type, we can very simply use them with any of the Boost.Multiprecision + types. As a reminder, here's our toy cube root function using 2 derivatives + and C++11 lambda functions to find the root: +

+
template <class T>
+T cbrt_2deriv_lambda(T x)
+{
+   // return cube root of x using 1st and 2nd derivatives and Halley.
+   //using namespace std;  // Help ADL of std functions.
+   using namespace boost::math::tools;
+   int exponent;
+   frexp(x, &exponent);                                // Get exponent of z (ignore mantissa).
+   T guess = ldexp(1., exponent / 3);                    // Rough guess is to divide the exponent by three.
+   T min = ldexp(0.5, exponent / 3);                     // Minimum possible value is half our guess.
+   T max = ldexp(2., exponent / 3);                      // Maximum possible value is twice our guess.
+   const int digits = std::numeric_limits<T>::digits;  // Maximum possible binary digits accuracy for type T.
+   // digits used to control how accurate to try to make the result.
+   int get_digits = static_cast<int>(digits * 0.4);    // Accuracy triples with each step, so stop when just
+   // over one third of the digits are correct.
+   boost::uintmax_t maxit = 20;
+   T result = halley_iterate(
+      // lambda function:
+      [x](const T& g){ return std::make_tuple(g * g * g - x, 3 * g * g, 6 * g); },
+      guess, min, max, get_digits, maxit);
+   return result;
+}
+
+

+ Some examples below are 50 decimal digit decimal and binary types (and + on some platforms a much faster float128 + or quad_float type ) that + we can use with these includes: +

+
#include <boost/multiprecision/cpp_bin_float.hpp> // For cpp_bin_float_50.
+#include <boost/multiprecision/cpp_dec_float.hpp> // For cpp_dec_float_50.
+#ifndef _MSC_VER  // float128 is not yet supported by Microsoft compiler at 2013.
+#  include <boost/multiprecision/float128.hpp> // Requires libquadmath.
+#endif
+
+

+ Some using statements simplify their use: +

+
  using boost::multiprecision::cpp_dec_float_50; // decimal.
+  using boost::multiprecision::cpp_bin_float_50; // binary.
+#ifndef _MSC_VER  // Not supported by Microsoft compiler.
+  using boost::multiprecision::float128;
+#endif
+
+

+ They can be used thus: +

+
std::cout.precision(std::numeric_limits<cpp_dec_float_50>::digits10);
+
+cpp_dec_float_50 two = 2; // 
+cpp_dec_float_50  r = cbrt_2deriv(two);
+std::cout << "cbrt(" << two << ") = " << r << std::endl;
+
+r = cbrt_2deriv(2.); // Passing a double, so ADL will compute a double precision result.
+std::cout << "cbrt(" << two << ") = " << r << std::endl;
+// cbrt(2) = 1.2599210498948731906665443602832965552806854248047 'wrong' from digits 17 onwards!
+r = cbrt_2deriv(static_cast<cpp_dec_float_50>(2.)); // Passing a cpp_dec_float_50, 
+// so will compute a cpp_dec_float_50 precision result.
+std::cout << "cbrt(" << two << ") = " << r << std::endl;
+r = cbrt_2deriv<cpp_dec_float_50>(2.); // Explictly a cpp_dec_float_50, so will compute a cpp_dec_float_50 precision result.
+std::cout << "cbrt(" << two << ") = " << r << std::endl;
+// cpp_dec_float_50 1.2599210498948731647672106072782283505702514647015
+
+

+ A reference value computed by Wolfram + Alpha is +

+
N[2^(1/3), 50]  1.2599210498948731647672106072782283505702514647015
+
+

+ which agrees exactly. +

+

+ To show values to their full precision, + it is necessary to adjust the std::ostream + precision to suit the type, + for example: +

+
template <typename T>
+T show_cube_root(T value)
+{ // Demonstrate by printing the root using all definitely significant digits.
+  std::cout.precision(std::numeric_limits<T>::digits10);
+  T r = cbrt_2deriv(value);
+  std::cout << "value = " << value << ", cube root =" << r << std::endl;
+  return r;
+}
+
+
show_cube_root(2.);
+show_cube_root(2.L);
+show_cube_root(two);
+
+

+ which outputs: +

+
cbrt(2) = 1.2599210498948731647672106072782283505702514647015
+
+value = 2, cube root =1.25992104989487
+value = 2, cube root =1.25992104989487
+value = 2, cube root =1.2599210498948731647672106072782283505702514647015
+
+
+ + + + + +
[Tip]Tip
+

+ Be very careful about the floating-point + type T that is passed + to the root-finding function. Carelessly passing a integer by writing + cpp_dec_float_50 r + = cbrt_2deriv(2); + or show_cube_root(2); will + provoke many warnings and compile errors. +

+

+ Even show_cube_root(2.F); will + produce warnings because typedef + double guess_type + defines the type used to compute the guess and bracket values as double. +

+

+ Even more treacherous is passing a double + as in cpp_dec_float_50 r = cbrt_2deriv(2.); which + silently gives the 'wrong' result, computing a double + result and then converting to cpp_dec_float_50! All digits beyond + max_digits10 will be + incorrect. Making the cbrt + type explicit with cbrt_2deriv<cpp_dec_float_50>(2.); will give you the desired 50 decimal + digit precision result. +

+
+

+ Full code of this example is at root_finding_multiprecision_example.cpp. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/nth_root.html b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/nth_root.html new file mode 100644 index 00000000..b3ba887b --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/root_finding_examples/nth_root.html @@ -0,0 +1,184 @@ + + + +Generalizing to Compute the nth root + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ If desired, we can now further generalize to compute the nth + root by computing the derivatives at compile-time + using the rules for differentiation and boost::math::pow<N> where template parameter N is an integer and a compile time constant. + Our functor and function now have an additional template parameter N, for the root required. +

+
+ + + + + +
[Note]Note

+ Since the powers and derivatives are fixed at compile time, the resulting + code is as efficient as as if hand-coded as the cube and fifth-root examples + above. A good compiler should also optimise any repeated multiplications. +

+

+ Our nth root functor is +

+
template <int N, class T = double>
+struct nth_functor_2deriv
+{ // Functor returning both 1st and 2nd derivatives.
+  BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
+  BOOST_STATIC_ASSERT_MSG((N > 0) == true, "root N must be > 0!");
+
+  nth_functor_2deriv(T const& to_find_root_of) : a(to_find_root_of)
+  { /* Constructor stores value a to find root of, for example: */ }
+
+  // using boost::math::tuple; // to return three values.
+  std::tuple<T, T, T> operator()(T const& x)
+  {
+    // Return f(x), f'(x) and f''(x).
+    using boost::math::pow;
+    T fx = pow<N>(x) - a;                  // Difference (estimate x^n - a).
+    T dx = N * pow<N - 1>(x);              // 1st derivative f'(x).
+    T d2x = N * (N - 1) * pow<N - 2 >(x);  // 2nd derivative f''(x).
+
+    return std::make_tuple(fx, dx, d2x);   // 'return' fx, dx and d2x.
+  }
+private:
+  T a;                                     // to be 'nth_rooted'.
+};
+
+

+ and our nth root function is +

+
template <int N, class T = double>
+T nth_2deriv(T x)
+{ // return nth root of x using 1st and 2nd derivatives and Halley.
+
+  using namespace std;  // Help ADL of std functions.
+  using namespace boost::math::tools; // For halley_iterate.
+
+  BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
+  BOOST_STATIC_ASSERT_MSG((N > 0) == true, "root N must be > 0!");
+  BOOST_STATIC_ASSERT_MSG((N > 1000) == false, "root N is too big!");
+
+  typedef double guess_type; // double may restrict (exponent) range for a multiprecision T?
+
+  int exponent;
+  frexp(static_cast<guess_type>(x), &exponent);                 // Get exponent of z (ignore mantissa).
+  T guess = ldexp(static_cast<guess_type>(1.), exponent / N);   // Rough guess is to divide the exponent by n.
+  T min = ldexp(static_cast<guess_type>(1.) / 2, exponent / N); // Minimum possible value is half our guess.
+  T max = ldexp(static_cast<guess_type>(2.), exponent / N);     // Maximum possible value is twice our guess.
+
+  int digits = std::numeric_limits<T>::digits * 0.4;            // Accuracy triples with each step, so stop when
+                                                                // slightly more than one third of the digits are correct.
+  const boost::uintmax_t maxit = 20;
+  boost::uintmax_t it = maxit;
+  T result = halley_iterate(nth_functor_2deriv<N, T>(x), guess, min, max, digits, it);
+  return result;
+}
+
+
    show_nth_root<5, double>(2.);
+    show_nth_root<5, long double>(2.);
+#ifndef _MSC_VER  // float128 is not supported by Microsoft compiler 2013.
+    show_nth_root<5, float128>(2);
+#endif
+    show_nth_root<5, cpp_dec_float_50>(2); // dec
+    show_nth_root<5, cpp_bin_float_50>(2); // bin
+
+

+ produces an output similar to this +

+
 Using MSVC 2013
+
+nth Root finding Example.
+Type double value = 2, 5th root = 1.14869835499704
+Type long double value = 2, 5th root = 1.14869835499704
+Type class boost::multiprecision::number<class boost::multiprecision::backends::cpp_dec_float<50,int,void>,1> value = 2,
+  5th root = 1.1486983549970350067986269467779275894438508890978
+Type class boost::multiprecision::number<class boost::multiprecision::backends::cpp_bin_float<50,10,void,int,0,0>,0> value = 2,
+  5th root = 1.1486983549970350067986269467779275894438508890978
+
+
+ + + + + +
[Tip]Tip
+

+ Take care with the type passed to the function. It is best to pass a + double or greater-precision + floating-point type. +

+

+ Passing an integer value, for example, nth_2deriv<5>(2) will + be rejected, while nth_2deriv<5, + double>(2) converts + the integer to double. +

+

+ Avoid passing a float value + that will provoke warnings (actually spurious) from the compiler about + potential loss of data, as noted above. +

+
+
+ + + + + +
[Warning]Warning

+ Asking for unreasonable roots, for example, show_nth_root<1000000>(2.); may lead to Loss + of significance like Type + double value + = 2, 1000000th root + = 1.00000069314783. + Use of the the pow function + is more sensible for this unusual need. +

+

+ Full code of this example is at root_finding_n_example.cpp. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/roots_deriv.html b/boost/libs/math/doc/html/math_toolkit/roots/roots_deriv.html new file mode 100644 index 00000000..e98947e8 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/roots_deriv.html @@ -0,0 +1,315 @@ + + + +Root Finding With Derivatives: Newton-Raphson, Halley & Schröder + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +
+ + Synopsis +
+
#include <boost/math/tools/roots.hpp>
+
+
namespace boost { namespace math {
+namespace tools { // Note namespace boost::math::tools.
+// Newton-Raphson
+template <class F, class T>
+T newton_raphson_iterate(F f, T guess, T min, T max, int digits);
+
+template <class F, class T>
+T newton_raphson_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter);
+
+// Halley
+template <class F, class T>
+T halley_iterate(F f, T guess, T min, T max, int digits);
+
+template <class F, class T>
+T halley_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter);
+
+// Schr'''&#xf6;'''der
+template <class F, class T>
+T schroder_iterate(F f, T guess, T min, T max, int digits);
+
+template <class F, class T>
+T schroder_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter);
+
+}}} // namespaces boost::math::tools.
+
+
+ + Description +
+

+ These functions all perform iterative root-finding using + derivatives: +

+
+

+ The functions all take the same parameters: +

+
+

Parameters of the root finding functions

+
+
F f
+
+

+ Type F must be a callable function object that accepts one parameter + and returns a std::pair, + std::tuple, boost::tuple or boost::fusion::tuple: +

+

+ For second-order iterative method (Newton + Raphson) the tuple + should have two elements containing + the evaluation of the function and its first derivative. +

+

+ For the third-order methods (Halley + and Schröder) the tuple + should have three elements containing + the evaluation of the function and its first and second derivatives. +

+
+
T guess
+

+ The initial starting value. A good guess is crucial to quick convergence! +

+
T min
+

+ The minimum possible value for the result, this is used as an initial + lower bracket. +

+
T max
+

+ The maximum possible value for the result, this is used as an initial + upper bracket. +

+
int digits
+

+ The desired number of binary digits precision. +

+
uintmax_t& max_iter
+

+ An optional maximum number of iterations to perform. On exit, this + is updated to the actual number of iterations performed. +

+
+
+

+ When using these functions you should note that: +

+
    +
  • + Default max_iter = + (std::numeric_limits<boost::uintmax_t>::max)() is effectively 'iterate for ever'. +
  • +
  • + They may be very sensitive to the initial guess, typically they converge + very rapidly if the initial guess has two or three decimal digits correct. + However convergence can be no better than bisect, + or in some rare cases, even worse than bisect + if the initial guess is a long way from the correct value and the derivatives + are close to zero. +
  • +
  • + These functions include special cases to handle zero first (and second + where appropriate) derivatives, and fall back to bisect + in this case. However, it is helpful if functor F is defined to return + an arbitrarily small value of the correct sign rather + than zero. +
  • +
  • + If the derivative at the current best guess for the result is infinite + (or very close to being infinite) then these functions may terminate + prematurely. A large first derivative leads to a very small next step, + triggering the termination condition. Derivative based iteration may + not be appropriate in such cases. +
  • +
  • + If the function is 'Really Well Behaved' (is monotonic and has only one + root) the bracket bounds min and max + may as well be set to the widest limits like zero and numeric_limits<T>::max(). +
  • +
  • + But if the function more complex and may have more than one root or a + pole, the choice of bounds is protection against jumping out to seek + the 'wrong' root. +
  • +
  • + These functions fall back to bisect + if the next computed step would take the next value out of bounds. The + bounds are updated after each step to ensure this leads to convergence. + However, a good initial guess backed up by asymptotically-tight bounds + will improve performance no end - rather than relying on bisection. +
  • +
  • + The value of digits is crucial to good performance + of these functions, if it is set too high then at best you will get one + extra (unnecessary) iteration, and at worst the last few steps will proceed + by bisection. + Remember that the returned value can never be more accurate than f(x) + can be evaluated, and that if f(x) suffers from + cancellation errors as it tends to zero then the computed steps will + be effectively random. The value of digits should + be set so that iteration terminates before this point: remember that + for second and third order methods the number of correct digits in the + result is increasing quite substantially with each iteration, digits + should be set by experiment so that the final iteration just takes the + next value into the zone where f(x) becomes inaccurate. + A good starting point for digits would be 0.6*D + for Newton and 0.4*D for Halley or Shröder iteration, where D is std::numeric_limits<T>::digits. +
  • +
  • + If you need some diagnostic output to see what is going on, you can + #define BOOST_MATH_INSTRUMENT + before the #include <boost/math/tools/roots.hpp>, and also ensure that display of + all the significant digits with cout.precision(std::numeric_limits<double>::digits10): or even possibly significant digits + with cout.precision(std::numeric_limits<double>::max_digits10): + but be warned, this may produce copious output! +
  • +
  • + Finally: you may well be able to do better than these functions by hand-coding + the heuristics used so that they are tailored to a specific function. + You may also be able to compute the ratio of derivatives used by these + methods more efficiently than computing the derivatives themselves. As + ever, algebraic simplification can be a big win. +
  • +
+
+ + Newton + Raphson Method +
+

+ Given an initial guess x0 the subsequent values are + computed using: +

+

+ +

+

+ Out of bounds steps revert to bisection + of the current bounds. +

+

+ Under ideal conditions, the number of correct digits doubles with each iteration. +

+
+ + Halley's + Method +
+

+ Given an initial guess x0 the subsequent values are + computed using: +

+

+ +

+

+ Over-compensation by the second derivative (one which would proceed in the + wrong direction) causes the method to revert to a Newton-Raphson step. +

+

+ Out of bounds steps revert to bisection of the current bounds. +

+

+ Under ideal conditions, the number of correct digits trebles with each iteration. +

+
+ + Schröder's + Method +
+

+ Given an initial guess x0 the subsequent values are computed using: +

+

+ +

+

+ Over-compensation by the second derivative (one which would proceed in the + wrong direction) causes the method to revert to a Newton-Raphson step. Likewise + a Newton step is used whenever that Newton step would change the next value + by more than 10%. +

+

+ Out of bounds steps revert to bisection + of the current bounds. +

+

+ Under ideal conditions, the number of correct digits trebles with each iteration. +

+

+ This is Schröder's general result (equation 18 from Stewart, + G. W. "On Infinitely Many Algorithms for Solving Equations." English + translation of Schröder's original paper. College Park, MD: University of + Maryland, Institute for Advanced Computer Studies, Department of Computer + Science, 1993.) +

+

+ 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: something + that neither Newton nor Halley can do. +

+
+ + Examples +
+

+ See root-finding + examples. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv.html b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv.html new file mode 100644 index 00000000..519975b4 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv.html @@ -0,0 +1,222 @@ + + + +Root Finding Without Derivatives + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ + +
+ + Synopsis +
+
#include <boost/math/tools/roots.hpp>
+
+
namespace boost { namespace math {
+namespace tools { // Note namespace boost::math::tools.
+// Bisection
+template <class F, class T, class Tol>
+std::pair<T, T>
+   bisect(
+      F f,
+      T min,
+      T max,
+      Tol tol,
+      boost::uintmax_t& max_iter);
+
+template <class F, class T, class Tol>
+std::pair<T, T>
+   bisect(
+      F f,
+      T min,
+      T max,
+      Tol tol);
+
+template <class F, class T, class Tol, class Policy>
+std::pair<T, T>
+   bisect(
+      F f,
+      T min,
+      T max,
+      Tol tol,
+      boost::uintmax_t& max_iter,
+      const Policy&);
+
+// Bracket and Solve Root
+template <class F, class T, class Tol>
+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);
+
+template <class F, class T, class Tol, class Policy>
+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,
+      const Policy&);
+
+ // TOMS 748 algorithm
+template <class F, class T, class Tol>
+std::pair<T, T>
+   toms748_solve(
+      F f,
+      const T& a,
+      const T& b,
+      Tol tol,
+      boost::uintmax_t& max_iter);
+
+template <class F, class T, class Tol, class Policy>
+std::pair<T, T>
+   toms748_solve(
+      F f,
+      const T& a,
+      const T& b,
+      Tol tol,
+      boost::uintmax_t& max_iter,
+      const Policy&);
+
+template <class F, class T, class Tol>
+std::pair<T, T>
+   toms748_solve(
+      F f,
+      const T& a,
+      const T& b,
+      const T& fa,
+      const T& fb,
+      Tol tol,
+      boost::uintmax_t& max_iter);
+
+template <class F, class T, class Tol, class Policy>
+std::pair<T, T>
+   toms748_solve(
+      F f,
+      const T& a,
+      const T& b,
+      const T& fa,
+      const T& fb,
+      Tol tol,
+      boost::uintmax_t& max_iter,
+      const Policy&);
+
+// Termination conditions:
+template <class T>
+struct eps_tolerance;
+
+struct equal_floor;
+struct equal_ceil;
+struct equal_nearest_integer;
+
+}}} // boost::math::tools namespaces
+
+
+ + Description +
+

+ These functions solve the root of some function f(x) + - without the need for any derivatives of f(x). +

+

+ The bracket_and_solve_root + functions use TOMS + 748 algorithm by Alefeld, Potra and Shi that is asymptotically the + most efficient known, and has been shown to be optimal for a certain classes + of smooth functions. Variants with and without Policies + are provided. +

+

+ Alternatively, bisect + is a simple bisection + routine which can be useful in its own right in some situations, or alternatively + for narrowing down the range containing the root, prior to calling a more + advanced algorithm. +

+

+ All the algorithms in this section reduce the diameter of the enclosing interval + with the same asymptotic efficiency with which they locate the root. This + is in contrast to the derivative based methods which may never + significantly reduce the enclosing interval, even though they rapidly approach + the root. This is also in contrast to some other derivative-free methods + (for example, Brent's method described at Brent-Dekker) + which only reduces the enclosing interval on the final step. Therefore these + methods return a std::pair containing the enclosing interval + found, and accept a function object specifying the termination condition. +

+

+ Three function objects are provided for ready-made termination conditions: +

+
    +
  • + eps_tolerance causes termination when the relative + error in the enclosing interval is below a certain threshold. +
  • +
  • + equal_floor and equal_ceil + are useful for certain statistical applications where the result is known + to be an integer. +
  • +
  • + Other user-defined termination conditions are likely to be used only + rarely, but may be useful in some specific circumstances. +
  • +
+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/TOMS748.html b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/TOMS748.html new file mode 100644 index 00000000..d687f407 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/TOMS748.html @@ -0,0 +1,193 @@ + + + +Algorithm TOMS 748: Alefeld, Potra and Shi: Enclosing zeros of continuous functions + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +
template <class F, class T, class Tol>
+std::pair<T, T>
+   toms748_solve(
+      F f,
+      const T& a,
+      const T& b,
+      Tol tol,
+      boost::uintmax_t& max_iter);
+
+template <class F, class T, class Tol, class Policy>
+std::pair<T, T>
+   toms748_solve(
+      F f,
+      const T& a,
+      const T& b,
+      Tol tol,
+      boost::uintmax_t& max_iter,
+      const Policy&);
+
+template <class F, class T, class Tol>
+std::pair<T, T>
+   toms748_solve(
+      F f,
+      const T& a,
+      const T& b,
+      const T& fa,
+      const T& fb,
+      Tol tol,
+      boost::uintmax_t& max_iter);
+
+template <class F, class T, class Tol, class Policy>
+std::pair<T, T>
+   toms748_solve(
+      F f,
+      const T& a,
+      const T& b,
+      const T& fa,
+      const T& fb,
+      Tol tol,
+      boost::uintmax_t& max_iter,
+      const Policy&);
+
+

+ These functions implement TOMS Algorithm 748: it uses a mixture of cubic, + quadratic and linear (secant) interpolation to locate the root of f(x). + The two pairs of functions differ only by whether values for f(a) + and f(b) are already available. +

+

+ Generally speaking it is easier (and often more efficient) to use bracket and solve + rather than trying to bracket the root yourself as this function requires. +

+

+ This function is provided rather than Brent's + method as it is known to be more effient in many cases (it is asymptotically + the most efficient known, and has been shown to be optimal for a certain + classes of smooth functions). It also has the useful property of decreasing + the bracket size with each step, unlike Brent's method which only shrinks + the enclosing interval in the final step. This makes it particularly useful + when you need a result where the ends of the interval round to the same + integer: as often happens in statistical applications for example. In this + situation the function is able to exit after a much smaller number of iterations + than would otherwise be possible. +

+

+ The TOMS 748 algorithm + parameters are: +

+
+

+
+
f
+

+ A unary functor that is the function whose root is to be solved. + f(x) need not be uniformly increasing or decreasing on x + and may have multiple roots. However, the bounds given must bracket + a single root. +

+
a
+

+ The lower bound for the initial bracket of the root. +

+
b
+

+ The upper bound for the initial bracket of the root. It is a precondition + that a < b and that a + and b bracket the root to find so that f(a) + * f(b) < 0. +

+
fa
+

+ Optional: the value of f(a). +

+
fb
+

+ Optional: the value of f(b). +

+
tol
+

+ A binary functor that determines the termination condition for the + search for the root. tol is passed the current + brackets at each step, when it returns true, then the current brackets + are returned as the result. See also predefined + termination functors. +

+
max_iter
+

+ The maximum number of function invocations to perform in the search + for the root. On exit, max_iter is set to actual + number of function invocations used. +

+
+
+

+ The final Policy argument is optional and + can be used to control the behaviour of the function: how it handles errors, + what level of precision to use etc. Refer to the policy + documentation for more details. +

+

+ toms748_solve returns: + a pair of values r that bracket the root so that: +

+
f(r.first) * f(r.second) <= 0
+
+

+ and either +

+
tol(r.first, r.second) == true
+
+

+ or +

+
max_iter >= m
+
+

+ where m is the initial value of max_iter + passed to the function. +

+

+ In other words, it's up to the caller to verify whether termination occurred + as a result of exceeding max_iter function invocations + (easily done by checking the updated value of max_iter + against its previous value passed as parameter), rather than because the + termination condition tol was satisfied. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/bisect.html b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/bisect.html new file mode 100644 index 00000000..0e61f166 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/bisect.html @@ -0,0 +1,152 @@ + + + +Bisection + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +
template <class F, class T, class Tol>
+std::pair<T, T>
+   bisect(  // Unlimited iterations.
+      F f,
+      T min,
+      T max,
+      Tol tol);
+
+template <class F, class T, class Tol>
+std::pair<T, T>
+   bisect(  // Limited iterations.
+      F f,
+      T min,
+      T max,
+      Tol tol,
+      boost::uintmax_t& max_iter);
+
+template <class F, class T, class Tol, class Policy>
+std::pair<T, T>
+   bisect( // Specified policy.
+      F f,
+      T min,
+      T max,
+      Tol tol,
+      boost::uintmax_t& max_iter,
+      const Policy&);
+
+

+ These functions locate the root using bisection. +

+

+ bisect function arguments + are: +

+
+

+
+
f
+

+ A unary functor which is the function f(x) whose + root is to be found. +

+
min
+

+ The left bracket of the interval known to contain the root. +

+
max
+

+ The right bracket of the interval known to contain the root.
+ It is a precondition that min < max and + f(min)*f(max) <= 0, the function raises an + evaluation_error + if these preconditions are violated. The action taken on error is + controlled by the Policy template argument: + the default behavior is to throw a boost::math::evaluation_error. + If the Policy is changed to not throw + then it returns std::pair<T>(min, min). +

+
tol
+

+ A binary functor that specifies the termination condition: the function + will return the current brackets enclosing the root when tol(min, + max) becomes true. See also predefined + termination functors. +

+
max_iter
+

+ The maximum number of invocations of f(x) to + make while searching for the root. On exit, this is updated to the + actual number of invocations performed. +

+
+
+

+ The final Policy argument is optional and + can be used to control the behaviour of the function: how it handles errors, + what level of precision to use etc. Refer to the policy + documentation for more details. +

+

+ Returns: a pair of values r + that bracket the root so that: +

+
f(r.first) * f(r.second) <= 0
+
+

+ and either +

+
tol(r.first, r.second) == true
+
+

+ or +

+
max_iter >= m
+
+

+ where m is the initial value of max_iter + passed to the function. +

+

+ In other words, it's up to the caller to verify whether termination occurred + as a result of exceeding max_iter function invocations + (easily done by checking the updated value of max_iter + when the function returns), rather than because the termination condition + tol was satisfied. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/bracket_solve.html b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/bracket_solve.html new file mode 100644 index 00000000..7c11655c --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/bracket_solve.html @@ -0,0 +1,186 @@ + + + +Bracket and Solve Root + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +
template <class F, class T, class Tol>
+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);
+
+template <class F, class T, class Tol, class Policy>
+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,
+      const Policy&);
+
+

+ bracket_and_solve_root + is a convenience function that calls TOMS + 748 algorithm internally to find the root of f(x). + It is generally much easier to use this function rather than TOMS + 748 algorithm, since it does the hard work of bracketing the root + for you. It's bracketing routines are quite robust and will usually be + more foolproof than home-grown routines, unless the function can be analysed + to yield tight brackets. +

+

+ Note that this routine can only be used when: +

+
    +
  • + f(x) is monotonic in the half of the real axis + containing guess. +
  • +
  • + The value of the inital guess must have the same sign as the root: + the function will never cross the origin when + searching for the root. +
  • +
  • + The location of the root should be known at least approximately, if + the location of the root differs by many orders of magnitude from + guess then many iterations will be needed to bracket + the root in spite of the special heuristics used to guard against this + very situation. A typical example would be setting the initial guess + to 0.1, when the root is at 1e-300. +
  • +
+

+ The bracket_and_solve_root + parameters are: +

+
+

+
+
f
+

+ A unary functor that is the function whose root is to be solved. + f(x) must be uniformly increasing or decreasing + on x. +

+
guess
+

+ An initial approximation to the root. +

+
factor
+

+ A scaling factor that is used to bracket the root: the value guess + is multiplied (or divided as appropriate) by factor + until two values are found that bracket the root. A value such as + 2 is a typical choice for factor. In addition + factor will be multiplied by 2 every 32 iterations: + this is to guard against a really very bad initial guess, typically + these occur when it's known the result is very large or small, but + not the exact order of magnitude. +

+
rising
+

+ Set to true if f(x) is + rising on x and false if + f(x) is falling on x. This + value is used along with the result of f(guess) + to determine if guess is above or below the + root. +

+
tol
+

+ A binary functor that determines the termination condition for the + search for the root. tol is passed the current + brackets at each step, when it returns true then the current brackets + are returned as the pair result. See also predefined + termination functors. +

+
max_iter
+

+ The maximum number of function invocations to perform in the search + for the root. On exit is set to the actual number of invocations + performed. +

+
+
+

+ The final Policy argument is optional and + can be used to control the behaviour of the function: how it handles errors, + what level of precision to use etc. Refer to the policy + documentation for more details. +

+

+ Returns: a pair of values r + that bracket the root so that: +

+
f(r.first) * f(r.second) <= 0
+
+

+ and either +

+
tol(r.first, r.second) == true
+
+

+ or +

+
max_iter >= m
+
+

+ where m is the initial value of max_iter + passed to the function. +

+

+ In other words, it's up to the caller to verify whether termination occurred + as a result of exceeding max_iter function invocations + (easily done by checking the value of max_iter when + the function returns), rather than because the termination condition tol + was satisfied. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/brent.html b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/brent.html new file mode 100644 index 00000000..beb66e2c --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/brent.html @@ -0,0 +1,54 @@ + + + +Brent-Decker Algorithm + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ The Brent-Dekker + algorithm, although very well know, is not provided by this library + as TOMS 748 algorithm + or its slightly easier to use variant bracket + and solve are superior and provide equivalent functionality. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/implementation.html b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/implementation.html new file mode 100644 index 00000000..33d7ab6b --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/implementation.html @@ -0,0 +1,63 @@ + + + +Implementation + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +

+ The implementation of the bisection algorithm is extremely straightforward + and not detailed here. +

+

+ TOMS Algorithm + 748: enclosing zeros of continuous functions is described in detail + in: +

+

+ Algorithm 748: Enclosing Zeros of Continuous Functions, G. E. + Alefeld, F. A. Potra and Yixun Shi, ACM Transactions on Mathematica1 Software, + Vol. 21. No. 3. September 1995. Pages 327-344. +

+

+ The implementation here is a faithful translation of this paper into C++. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + diff --git a/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/root_termination.html b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/root_termination.html new file mode 100644 index 00000000..c0191104 --- /dev/null +++ b/boost/libs/math/doc/html/math_toolkit/roots/roots_noderiv/root_termination.html @@ -0,0 +1,102 @@ + + + +Termination Condition Functors + + + + + + + + + + + + + + + +
Boost C++ LibrariesHomeLibrariesPeopleFAQMore
+
+
+PrevUpHomeNext +
+
+ +
template <class T>
+struct eps_tolerance
+{
+   eps_tolerance();
+   eps_tolerance(int bits);
+   bool operator()(const T& a, const T& b)const;
+};
+
+

+ eps_tolerance is the usual + termination condition used with these root finding functions. Its operator() + will return true when the relative distance between a + and b is less than four times the machine epsilon + for T, or 21-bits, whichever is the larger. In other words, you set bits + to the number of bits of precision you want in the result. The minimal + tolerance of four times the machine epsilon of type T + is required to ensure that we get back a bracketing interval, since this + must clearly be at greater than one epsilon in size. While in theory a + maximum distance of twice machine epsilon is possible to achieve, in practice + this results in a great deal of "thrashing" given that the function + whose root is being found can only ever be accurate to 1 epsilon at best. +

+
struct equal_floor
+{
+   equal_floor();
+   template <class T> bool operator()(const T& a, const T& b)const;
+};
+
+

+ This termination condition is used when you want to find an integer result + that is the floor of the true root. It will terminate + as soon as both ends of the interval have the same floor. +

+
struct equal_ceil
+{
+   equal_ceil();
+   template <class T> bool operator()(const T& a, const T& b)const;
+};
+
+

+ This termination condition is used when you want to find an integer result + that is the ceil of the true root. It will terminate + as soon as both ends of the interval have the same ceil. +

+
struct equal_nearest_integer
+{
+   equal_nearest_integer();
+   template <class T> bool operator()(const T& a, const T& b)const;
+};
+
+

+ This termination condition is used when you want to find an integer result + that is the closest to the true root. It will terminate + as soon as both ends of the interval round to the same nearest integer. +

+
+ + + +
+
+
+PrevUpHomeNext +
+ + -- cgit v1.2.3