1 // Copyright Paul A. Bristow 2015
2
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 // Comparison of finding roots using TOMS748, Newton-Raphson, Schroder & Halley algorithms.
9
10 // Note that this file contains Quickbook mark-up as well as code
11 // and comments, don't change any of the special comment mark-ups!
12
13 // root_finding_algorithms.cpp
14
15 #include <boost/cstdlib.hpp>
16 #include <boost/config.hpp>
17 #include <boost/array.hpp>
18 #include <boost/type_traits/is_floating_point.hpp>
19 #include <boost/type_traits/is_fundamental.hpp>
20
21 #include "table_type.hpp"
22 // Copy of i:\modular-boost\libs\math\test\table_type.hpp
23 // #include "handle_test_result.hpp"
24 // Copy of i:\modular - boost\libs\math\test\handle_test_result.hpp
25
26 #include <boost/math/tools/roots.hpp>
27 //using boost::math::policies::policy;
28 //using boost::math::tools::newton_raphson_iterate;
29 //using boost::math::tools::halley_iterate; //
30 //using boost::math::tools::eps_tolerance; // Binary functor for specified number of bits.
31 //using boost::math::tools::bracket_and_solve_root;
32 //using boost::math::tools::toms748_solve;
33 //using boost::math::tools::schroder_iterate;
34
35 #include <boost/math/special_functions/next.hpp> // For float_distance.
36 #include <tuple> // for tuple and make_tuple.
37 #include <boost/math/special_functions/cbrt.hpp> // For boost::math::cbrt.
38
39 #include <boost/multiprecision/cpp_bin_float.hpp> // is binary.
40 //#include <boost/multiprecision/cpp_dec_float.hpp> // is decimal.
41 using boost::multiprecision::cpp_bin_float_100;
42 using boost::multiprecision::cpp_bin_float_50;
43
44 #include <boost/timer/timer.hpp>
45 #include <boost/system/error_code.hpp>
46 #include <boost/multiprecision/cpp_bin_float/io.hpp>
47 #include <boost/preprocessor/stringize.hpp>
48
49 // STL
50 #include <iostream>
51 #include <iomanip>
52 #include <string>
53 #include <vector>
54 #include <limits>
55 #include <fstream> // std::ofstream
56 #include <cmath>
57 #include <typeinfo> // for type name using typid(thingy).name();
58
59 #ifndef BOOST_ROOT
60 # define BOOST_ROOT i:/modular-boost/
61 #endif
62 // Need to find this
63
64 #ifdef __FILE__
65 std::string sourcefilename = __FILE__;
66 #endif
67
chop_last(std::string s)68 std::string chop_last(std::string s)
69 {
70 std::string::size_type pos = s.find_last_of("\\/");
71 if(pos != std::string::npos)
72 s.erase(pos);
73 else if(s.empty())
74 abort();
75 else
76 s.erase();
77 return s;
78 }
79
make_root()80 std::string make_root()
81 {
82 std::string result;
83 if(sourcefilename.find_first_of(":") != std::string::npos)
84 {
85 result = chop_last(sourcefilename); // lose filename part
86 result = chop_last(result); // lose /example/
87 result = chop_last(result); // lose /math/
88 result = chop_last(result); // lose /libs/
89 }
90 else
91 {
92 result = chop_last(sourcefilename); // lose filename part
93 if(result.empty())
94 result = ".";
95 result += "/../../..";
96 }
97 return result;
98 }
99
short_file_name(std::string s)100 std::string short_file_name(std::string s)
101 {
102 std::string::size_type pos = s.find_last_of("\\/");
103 if(pos != std::string::npos)
104 s.erase(0, pos + 1);
105 return s;
106 }
107
108 std::string boost_root = make_root();
109
110 #ifdef _MSC_VER
111 std::string filename = boost_root.append("/libs/math/doc/roots/root_comparison_tables_msvc.qbk");
112 #else // assume GCC
113 std::string filename = boost_root.append("/libs/math/doc/roots/root_comparison_tables_gcc.qbk");
114 #endif
115
116 std::ofstream fout (filename.c_str(), std::ios_base::out);
117
118 //std::array<std::string, 6> float_type_names =
119 //{
120 // "float", "double", "long double", "cpp_bin_128", "cpp_dec_50", "cpp_dec_100"
121 //};
122
123 std::vector<std::string> algo_names =
124 {
125 "cbrt", "TOMS748", "Newton", "Halley", "Schr'''ö'''der"
126 };
127
128 std::vector<int> max_digits10s;
129 std::vector<std::string> typenames; // Full computer generated type name.
130 std::vector<std::string> names; // short name.
131
132 uintmax_t iters; // Global as iterations is not returned by rooting function.
133
134 const int count = 1000000; // Number of iterations to average.
135
136 struct root_info
137 { // for a floating-point type, float, double ...
138 std::size_t max_digits10; // for type.
139 std::string full_typename; // for type from type_id.name().
140 std::string short_typename; // for type "float", "double", "cpp_bin_float_50" ....
141
142 std::size_t bin_digits; // binary in floating-point type numeric_limits<T>::digits;
143 int get_digits; // fraction of maximum possible accuracy required.
144 // = digits * digits_accuracy
145 // Vector of values for each algorithm, std::cbrt, boost::math::cbrt, TOMS748, Newton, Halley.
146 //std::vector< boost::int_least64_t> times; converted to int.
147 std::vector<int> times;
148 //boost::int_least64_t min_time = std::numeric_limits<boost::int_least64_t>::max(); // Used to normalize times (as int).
149 std::vector<double> normed_times;
150 boost::int_least64_t min_time = (std::numeric_limits<boost::int_least64_t>::max)(); // Used to normalize times.
151 std::vector<uintmax_t> iterations;
152 std::vector<long int> distances;
153 std::vector<cpp_bin_float_100> full_results;
154 }; // struct root_info
155
156 std::vector<root_info> root_infos; // One element for each type used.
157
158 int type_no = -1; // float = 0, double = 1, ... indexing root_infos.
159
build_test_name(const char * type_name,const char * test_name)160 inline std::string build_test_name(const char* type_name, const char* test_name)
161 {
162 std::string result(BOOST_COMPILER);
163 result += "|";
164 result += BOOST_STDLIB;
165 result += "|";
166 result += BOOST_PLATFORM;
167 result += "|";
168 result += type_name;
169 result += "|";
170 result += test_name;
171 #if defined(_DEBUG ) || !defined(NDEBUG)
172 result += "|";
173 result += " debug";
174 #else
175 result += "|";
176 result += " release";
177 #endif
178 result += "|";
179 return result;
180 }
181
182 // No derivatives - using TOMS748 internally.
183 template <class T>
184 struct cbrt_functor_noderiv
185 { // cube root of x using only function - no derivatives.
cbrt_functor_noderivcbrt_functor_noderiv186 cbrt_functor_noderiv(T const& to_find_root_of) : a(to_find_root_of)
187 { // Constructor just stores value a to find root of.
188 }
operator ()cbrt_functor_noderiv189 T operator()(T const& x)
190 {
191 T fx = x*x*x - a; // Difference (estimate x^3 - a).
192 return fx;
193 }
194 private:
195 T a; // to be 'cube_rooted'.
196 }; // template <class T> struct cbrt_functor_noderiv
197
198 template <class T>
cbrt_noderiv(T x)199 T cbrt_noderiv(T x)
200 { // return cube root of x using bracket_and_solve (using NO derivatives).
201 using namespace std; // Help ADL of std functions.
202 using namespace boost::math::tools; // For bracket_and_solve_root.
203
204 // Maybe guess should be double, or use enable_if to avoid warning about conversion double to float here?
205 T guess;
206 if (boost::is_fundamental<T>::value)
207 {
208 int exponent;
209 frexp(x, &exponent); // Get exponent of z (ignore mantissa).
210 guess = ldexp((T)1., exponent / 3); // Rough guess is to divide the exponent by three.
211 }
212 else
213 { // (boost::is_class<T>)
214 double dx = static_cast<double>(x);
215 guess = boost::math::cbrt<T>(dx); // Get guess using double.
216 }
217
218 T factor = 2; // How big steps to take when searching.
219
220 const boost::uintmax_t maxit = 50; // Limit to maximum iterations.
221 boost::uintmax_t it = maxit; // Initially our chosen max iterations, but updated with actual.
222 bool is_rising = true; // So if result if guess^3 is too low, then try increasing guess.
223 // Some fraction of digits is used to control how accurate to try to make the result.
224 int get_digits = static_cast<int>(std::numeric_limits<T>::digits - 2);
225
226 eps_tolerance<T> tol(get_digits); // Set the tolerance.
227 std::pair<T, T> r =
228 bracket_and_solve_root(cbrt_functor_noderiv<T>(x), guess, factor, is_rising, tol, it);
229 iters = it;
230 T result = r.first + (r.second - r.first) / 2; // Midway between brackets.
231 return result;
232 } // template <class T> T cbrt_noderiv(T x)
233
234
235 // Using 1st derivative only Newton-Raphson
236
237 template <class T>
238 struct cbrt_functor_deriv
239 { // Functor also returning 1st derivative.
cbrt_functor_derivcbrt_functor_deriv240 cbrt_functor_deriv(T const& to_find_root_of) : a(to_find_root_of)
241 { // Constructor stores value a to find root of,
242 // for example: calling cbrt_functor_deriv<T>(x) to use to get cube root of x.
243 }
operator ()cbrt_functor_deriv244 std::pair<T, T> operator()(T const& x)
245 { // Return both f(x) and f'(x).
246 T fx = x*x*x - a; // Difference (estimate x^3 - value).
247 T dx = 3 * x*x; // 1st derivative = 3x^2.
248 return std::make_pair(fx, dx); // 'return' both fx and dx.
249 }
250 private:
251 T a; // to be 'cube_rooted'.
252 };
253
254 template <class T>
cbrt_deriv(T x)255 T cbrt_deriv(T x)
256 { // return cube root of x using 1st derivative and Newton_Raphson.
257 using namespace boost::math::tools;
258 int exponent;
259 T guess;
260 if(boost::is_fundamental<T>::value)
261 {
262 frexp(x, &exponent); // Get exponent of z (ignore mantissa).
263 guess = ldexp(static_cast<T>(1), exponent / 3); // Rough guess is to divide the exponent by three.
264 }
265 else
266 guess = boost::math::cbrt(static_cast<double>(x));
267 T min = guess / 2; // Minimum possible value is half our guess.
268 T max = 2 * guess; // Maximum possible value is twice our guess.
269 int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.6);
270 const boost::uintmax_t maxit = 20;
271 boost::uintmax_t it = maxit;
272 T result = newton_raphson_iterate(cbrt_functor_deriv<T>(x), guess, min, max, get_digits, it);
273 iters = it;
274 return result;
275 }
276
277 // Using 1st and 2nd derivatives with Halley algorithm.
278
279 template <class T>
280 struct cbrt_functor_2deriv
281 { // Functor returning both 1st and 2nd derivatives.
cbrt_functor_2derivcbrt_functor_2deriv282 cbrt_functor_2deriv(T const& to_find_root_of) : a(to_find_root_of)
283 { // Constructor stores value a to find root of, for example:
284 // calling cbrt_functor_2deriv<T>(x) to get cube root of x,
285 }
operator ()cbrt_functor_2deriv286 std::tuple<T, T, T> operator()(T const& x)
287 { // Return both f(x) and f'(x) and f''(x).
288 T fx = x*x*x - a; // Difference (estimate x^3 - value).
289 T dx = 3 * x*x; // 1st derivative = 3x^2.
290 T d2x = 6 * x; // 2nd derivative = 6x.
291 return std::make_tuple(fx, dx, d2x); // 'return' fx, dx and d2x.
292 }
293 private:
294 T a; // to be 'cube_rooted'.
295 };
296
297 template <class T>
cbrt_2deriv(T x)298 T cbrt_2deriv(T x)
299 { // return cube root of x using 1st and 2nd derivatives and Halley.
300 //using namespace std; // Help ADL of std functions.
301 using namespace boost::math::tools;
302 int exponent;
303 T guess;
304 if(boost::is_fundamental<T>::value)
305 {
306 frexp(x, &exponent); // Get exponent of z (ignore mantissa).
307 guess = ldexp(static_cast<T>(1), exponent / 3); // Rough guess is to divide the exponent by three.
308 }
309 else
310 guess = boost::math::cbrt(static_cast<double>(x));
311 T min = guess / 2; // Minimum possible value is half our guess.
312 T max = 2 * guess; // Maximum possible value is twice our guess.
313 // digits used to control how accurate to try to make the result.
314 int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.4);
315 boost::uintmax_t maxit = 20;
316 boost::uintmax_t it = maxit;
317 T result = halley_iterate(cbrt_functor_2deriv<T>(x), guess, min, max, get_digits, it);
318 iters = it;
319 return result;
320 }
321
322 // Using 1st and 2nd derivatives using Schroder algorithm.
323
324 template <class T>
cbrt_2deriv_s(T x)325 T cbrt_2deriv_s(T x)
326 { // return cube root of x using 1st and 2nd derivatives and Schroder algorithm.
327 //using namespace std; // Help ADL of std functions.
328 using namespace boost::math::tools;
329 int exponent;
330 T guess;
331 if(boost::is_fundamental<T>::value)
332 {
333 frexp(x, &exponent); // Get exponent of z (ignore mantissa).
334 guess = ldexp(static_cast<T>(1), exponent / 3); // Rough guess is to divide the exponent by three.
335 }
336 else
337 guess = boost::math::cbrt(static_cast<double>(x));
338 T min = guess / 2; // Minimum possible value is half our guess.
339 T max = 2 * guess; // Maximum possible value is twice our guess.
340 // digits used to control how accurate to try to make the result.
341 int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.4);
342 const boost::uintmax_t maxit = 20;
343 boost::uintmax_t it = maxit;
344 T result = schroder_iterate(cbrt_functor_2deriv<T>(x), guess, min, max, get_digits, it);
345 iters = it;
346 return result;
347 } // template <class T> T cbrt_2deriv_s(T x)
348
349
350
351 template <typename T>
test_root(cpp_bin_float_100 big_value,cpp_bin_float_100 answer,const char * type_name)352 int test_root(cpp_bin_float_100 big_value, cpp_bin_float_100 answer, const char* type_name)
353 {
354 //T value = 28.; // integer (exactly representable as floating-point)
355 // whose cube root is *not* exactly representable.
356 // Wolfram Alpha command N[28 ^ (1 / 3), 100] computes cube root to 100 decimal digits.
357 // 3.036588971875662519420809578505669635581453977248111123242141654169177268411884961770250390838097895
358
359 std::size_t max_digits = 2 + std::numeric_limits<T>::digits * 3010 / 10000;
360 // For new versions use max_digits10
361 // std::cout.precision(std::numeric_limits<T>::max_digits10);
362 std::cout.precision(max_digits);
363 std::cout << std::showpoint << std::endl; // Trailing zeros too.
364
365 root_infos.push_back(root_info());
366 type_no++; // Another type.
367
368 root_infos[type_no].max_digits10 = max_digits;
369 root_infos[type_no].full_typename = typeid(T).name(); // Full typename.
370 root_infos[type_no].short_typename = type_name; // Short typename.
371
372 root_infos[type_no].bin_digits = std::numeric_limits<T>::digits;
373
374 root_infos[type_no].get_digits = std::numeric_limits<T>::digits;
375
376 T to_root = static_cast<T>(big_value);
377 T result; // root
378 T ans = static_cast<T>(answer);
379 int algo = 0; // Count of algorithms used.
380
381 using boost::timer::nanosecond_type;
382 using boost::timer::cpu_times;
383 using boost::timer::cpu_timer;
384
385 cpu_times now; // Holds wall, user and system times.
386 T sum = 0;
387
388 // std::cbrt is much the fastest, but not useful for this comparison because it only handles fundamental types.
389 // Using enable_if allows us to avoid a compile fail with multiprecision types, but still distorts the results too much.
390
391 //{
392 // algorithm_names.push_back("std::cbrt");
393 // cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
394 // ti.start();
395 // for (long i = 0; i < count; ++i)
396 // {
397 // stdcbrt(big_value);
398 // }
399 // now = ti.elapsed();
400 // int time = static_cast<int>(now.user / count);
401 // root_infos[type_no].times.push_back(time); // CPU time taken per root.
402 // if (time < root_infos[type_no].min_time)
403 // {
404 // root_infos[type_no].min_time = time;
405 // }
406 // ti.stop();
407 // long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
408 // root_infos[type_no].distances.push_back(distance);
409 // root_infos[type_no].iterations.push_back(0); // Not known.
410 // root_infos[type_no].full_results.push_back(result);
411 // algo++;
412 //}
413 //{
414 // //algorithm_names.push_back("boost::math::cbrt"); // .
415 // cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
416 // ti.start();
417 // for (long i = 0; i < count; ++i)
418 // {
419 // result = boost::math::cbrt(to_root); //
420 // }
421 // now = ti.elapsed();
422 // int time = static_cast<int>(now.user / count);
423 // root_infos[type_no].times.push_back(time); // CPU time taken.
424 // ti.stop();
425 // if (time < root_infos[type_no].min_time)
426 // {
427 // root_infos[type_no].min_time = time;
428 // }
429 // long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
430 // root_infos[type_no].distances.push_back(distance);
431 // root_infos[type_no].iterations.push_back(0); // Iterations not knowable.
432 // root_infos[type_no].full_results.push_back(result);
433 //}
434
435
436
437 {
438 //algorithm_names.push_back("boost::math::cbrt"); // .
439 result = 0;
440 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
441 ti.start();
442 for (long i = 0; i < count; ++i)
443 {
444 result = boost::math::cbrt(to_root); //
445 sum += result;
446 }
447 now = ti.elapsed();
448
449 long time = static_cast<long>(now.user/1000); // convert nanoseconds to microseconds (assuming this is resolution).
450 root_infos[type_no].times.push_back(time); // CPU time taken.
451 ti.stop();
452 if (time < root_infos[type_no].min_time)
453 {
454 root_infos[type_no].min_time = time;
455 }
456 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
457 root_infos[type_no].distances.push_back(distance);
458 root_infos[type_no].iterations.push_back(0); // Iterations not knowable.
459 root_infos[type_no].full_results.push_back(result);
460 }
461 {
462 //algorithm_names.push_back("TOMS748"); //
463 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
464 ti.start();
465 for (long i = 0; i < count; ++i)
466 {
467 result = cbrt_noderiv<T>(to_root); //
468 sum += result;
469 }
470 now = ti.elapsed();
471 // int time = static_cast<int>(now.user / count);
472 long time = static_cast<long>(now.user/1000);
473 root_infos[type_no].times.push_back(time); // CPU time taken.
474 if (time < root_infos[type_no].min_time)
475 {
476 root_infos[type_no].min_time = time;
477 }
478 ti.stop();
479 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
480 root_infos[type_no].distances.push_back(distance);
481 root_infos[type_no].iterations.push_back(iters); //
482 root_infos[type_no].full_results.push_back(result);
483 }
484 {
485 // algorithm_names.push_back("Newton"); // algorithm
486 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
487 ti.start();
488 for (long i = 0; i < count; ++i)
489 {
490 result = cbrt_deriv(to_root); //
491 sum += result;
492 }
493 now = ti.elapsed();
494 // int time = static_cast<int>(now.user / count);
495 long time = static_cast<long>(now.user/1000);
496 root_infos[type_no].times.push_back(time); // CPU time taken.
497 if (time < root_infos[type_no].min_time)
498 {
499 root_infos[type_no].min_time = time;
500 }
501
502 ti.stop();
503 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
504 root_infos[type_no].distances.push_back(distance);
505 root_infos[type_no].iterations.push_back(iters); //
506 root_infos[type_no].full_results.push_back(result);
507 }
508 {
509 //algorithm_names.push_back("Halley"); // algorithm
510 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
511 ti.start();
512 for (long i = 0; i < count; ++i)
513 {
514 result = cbrt_2deriv(to_root); //
515 sum += result;
516 }
517 now = ti.elapsed();
518 // int time = static_cast<int>(now.user / count);
519 long time = static_cast<long>(now.user/1000);
520 root_infos[type_no].times.push_back(time); // CPU time taken.
521 ti.stop();
522 if (time < root_infos[type_no].min_time)
523 {
524 root_infos[type_no].min_time = time;
525 }
526 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
527 root_infos[type_no].distances.push_back(distance);
528 root_infos[type_no].iterations.push_back(iters); //
529 root_infos[type_no].full_results.push_back(result);
530 }
531
532 {
533 // algorithm_names.push_back("Shroeder"); // algorithm
534 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
535 ti.start();
536 for (long i = 0; i < count; ++i)
537 {
538 result = cbrt_2deriv_s(to_root); //
539 sum += result;
540 }
541 now = ti.elapsed();
542 // int time = static_cast<int>(now.user / count);
543 long time = static_cast<long>(now.user/1000);
544 root_infos[type_no].times.push_back(time); // CPU time taken.
545 if (time < root_infos[type_no].min_time)
546 {
547 root_infos[type_no].min_time = time;
548 }
549 ti.stop();
550 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
551 root_infos[type_no].distances.push_back(distance);
552 root_infos[type_no].iterations.push_back(iters); //
553 root_infos[type_no].full_results.push_back(result);
554 }
555 for (size_t i = 0; i != root_infos[type_no].times.size(); i++)
556 { // Normalize times.
557 double normed_time = static_cast<double>(root_infos[type_no].times[i]);
558 normed_time /= root_infos[type_no].min_time;
559 root_infos[type_no].normed_times.push_back(normed_time);
560 }
561 algo++;
562 std::cout << "Accumulated sum was " << sum << std::endl;
563 return algo; // Count of how many algorithms used.
564 } // test_root
565
table_root_info(cpp_bin_float_100 full_value,cpp_bin_float_100 full_answer)566 void table_root_info(cpp_bin_float_100 full_value, cpp_bin_float_100 full_answer)
567 {
568 // Fill the elements.
569 test_root<float>(full_value, full_answer, "float");
570 test_root<double>(full_value, full_answer, "double");
571 test_root<long double>(full_value, full_answer, "long double");
572 test_root<cpp_bin_float_50>(full_value, full_answer, "cpp_bin_float_50");
573 //test_root<cpp_bin_float_100>(full_value, full_answer, "cpp_bin_float_100");
574
575 std::cout << root_infos.size() << " floating-point types tested:" << std::endl;
576 #ifndef NDEBUG
577 std::cout << "Compiled in debug mode." << std::endl;
578 #else
579 std::cout << "Compiled in optimise mode." << std::endl;
580 #endif
581
582
583 for (size_t tp = 0; tp != root_infos.size(); tp++)
584 { // For all types:
585
586 std::cout << std::endl;
587
588 std::cout << "Floating-point type = " << root_infos[tp].short_typename << std::endl;
589 std::cout << "Floating-point type = " << root_infos[tp].full_typename << std::endl;
590 std::cout << "Max_digits10 = " << root_infos[tp].max_digits10 << std::endl;
591 std::cout << "Binary digits = " << root_infos[tp].bin_digits << std::endl;
592 std::cout << "Accuracy digits = " << root_infos[tp].get_digits - 2 << ", " << static_cast<int>(root_infos[tp].get_digits * 0.6) << ", " << static_cast<int>(root_infos[tp].get_digits * 0.4) << std::endl;
593 std::cout << "min_time = " << root_infos[tp].min_time << std::endl;
594
595 std::cout << std::setprecision(root_infos[tp].max_digits10 ) << "Roots = ";
596 std::copy(root_infos[tp].full_results.begin(), root_infos[tp].full_results.end(), std::ostream_iterator<cpp_bin_float_100>(std::cout, " "));
597 std::cout << std::endl;
598
599 // Header row.
600 std::cout << "Algorithm " << "Iterations " << "Times " << "Norm_times " << "Distance" << std::endl;
601
602 // Row for all algorithms.
603 for (unsigned algo = 0; algo != algo_names.size(); algo++)
604 {
605 std::cout
606 << std::left << std::setw(20) << algo_names[algo] << " "
607 << std::setw(8) << std::setprecision(2) << root_infos[tp].iterations[algo] << " "
608 << std::setw(8) << std::setprecision(5) << root_infos[tp].times[algo] << " "
609 << std::setw(8) << std::setprecision(3) << root_infos[tp].normed_times[algo] << " "
610 << std::setw(8) << std::setprecision(2) << root_infos[tp].distances[algo]
611 << std::endl;
612 } // for algo
613 } // for tp
614
615 // Print info as Quickbook table.
616 #if 0
617 fout << "[table:cbrt_5 Info for float, double, long double and cpp_bin_float_50\n"
618 << "[[type name] [max_digits10] [binary digits] [required digits]]\n";// header.
619
620 for (size_t tp = 0; tp != root_infos.size(); tp++)
621 { // For all types:
622 fout << "["
623 << "[" << root_infos[tp].short_typename << "]"
624 << "[" << root_infos[tp].max_digits10 << "]" // max_digits10
625 << "[" << root_infos[tp].bin_digits << "]"// < "Binary digits
626 << "[" << root_infos[tp].get_digits << "]]\n"; // Accuracy digits.
627 } // tp
628 fout << "] [/table cbrt_5] \n" << std::endl;
629 #endif
630 // Prepare Quickbook table of floating-point types.
631 fout << "[table:cbrt_4 Cube root(28) for float, double, long double and cpp_bin_float_50\n"
632 << "[[][float][][][] [][double][][][] [][long d][][][] [][cpp50][][]]\n"
633 << "[[Algorithm]";
634 for (size_t tp = 0; tp != root_infos.size(); tp++)
635 { // For all types:
636 fout << "[Its]" << "[Times]" << "[Norm]" << "[Dis]" << "[ ]";
637 }
638 fout << "]" << std::endl;
639
640 // Row for all algorithms.
641 for (size_t algo = 0; algo != algo_names.size(); algo++)
642 {
643 fout << "[[" << std::left << std::setw(9) << algo_names[algo] << "]";
644 for (size_t tp = 0; tp != root_infos.size(); tp++)
645 { // For all types:
646
647 fout
648 << "[" << std::right << std::showpoint
649 << std::setw(3) << std::setprecision(2) << root_infos[tp].iterations[algo] << "]["
650 << std::setw(5) << std::setprecision(5) << root_infos[tp].times[algo] << "][";
651 if(fabs(root_infos[tp].normed_times[algo]) <= 1.05)
652 fout << "[role blue " << std::setw(3) << std::setprecision(2) << root_infos[tp].normed_times[algo] << "]";
653 else if(fabs(root_infos[tp].normed_times[algo]) > 4)
654 fout << "[role red " << std::setw(3) << std::setprecision(2) << root_infos[tp].normed_times[algo] << "]";
655 else
656 fout << std::setw(3) << std::setprecision(2) << root_infos[tp].normed_times[algo];
657 fout
658 << "]["
659 << std::setw(3) << std::setprecision(2) << root_infos[tp].distances[algo] << "][ ]";
660 } // tp
661 fout <<"]" << std::endl;
662 } // for algo
663 fout << "] [/end of table cbrt_4]\n";
664 } // void table_root_info
665
main()666 int main()
667 {
668 using namespace boost::multiprecision;
669 using namespace boost::math;
670
671 try
672 {
673 std::cout << "Tests run with " << BOOST_COMPILER << ", "
674 << BOOST_STDLIB << ", " << BOOST_PLATFORM << ", ";
675
676 if (fout.is_open())
677 {
678 std::cout << "\nOutput to " << filename << std::endl;
679 }
680 else
681 { // Failed to open.
682 std::cout << " Open file " << filename << " for output failed!" << std::endl;
683 std::cout << "error" << errno << std::endl;
684 return boost::exit_failure;
685 }
686
687 fout <<
688 "[/""\n"
689 "Copyright 2015 Paul A. Bristow.""\n"
690 "Copyright 2015 John Maddock.""\n"
691 "Distributed under the Boost Software License, Version 1.0.""\n"
692 "(See accompanying file LICENSE_1_0.txt or copy at""\n"
693 "http://www.boost.org/LICENSE_1_0.txt).""\n"
694 "]""\n"
695 << std::endl;
696 std::string debug_or_optimize;
697 #ifdef _DEBUG
698 #if (_DEBUG == 0)
699 debug_or_optimize = "Compiled in debug mode.";
700 #else
701 debug_or_optimize = "Compiled in optimise mode.";
702 #endif
703 #endif
704
705 // Print out the program/compiler/stdlib/platform names as a Quickbook comment:
706 fout << "\n[h5 Program " << short_file_name(sourcefilename) << ", "
707 << BOOST_COMPILER << ", "
708 << BOOST_STDLIB << ", "
709 << BOOST_PLATFORM << (sizeof(void*) == 8 ? ", x64" : ", x86")
710 << debug_or_optimize << "[br]"
711 << count << " evaluations of each of " << algo_names.size() << " root_finding algorithms."
712 << "]"
713 << std::endl;
714
715 std::cout << count << " evaluations of root_finding." << std::endl;
716
717 BOOST_MATH_CONTROL_FP;
718
719 cpp_bin_float_100 full_value("28");
720
721 cpp_bin_float_100 full_answer ("3.036588971875662519420809578505669635581453977248111123242141654169177268411884961770250390838097895");
722
723 std::copy(max_digits10s.begin(), max_digits10s.end(), std::ostream_iterator<int>(std::cout, " "));
724 std::cout << std::endl;
725
726 table_root_info(full_value, full_answer);
727
728
729 return boost::exit_success;
730 }
731 catch (std::exception const& ex)
732 {
733 std::cout << "exception thrown: " << ex.what() << std::endl;
734 return boost::exit_failure;
735 }
736 } // int main()
737
738 /*
739 debug
740
741 1> float, maxdigits10 = 9
742 1> 6 algorithms used.
743 1> Digits required = 24.0000000
744 1> find root of 28.0000000, expected answer = 3.03658897
745 1> Times 156 312 18750 4375 3437 3906
746 1> Iterations: 0 0 8 6 4 5
747 1> Distance: 0 0 -1 0 0 0
748 1> Roots: 3.03658891 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
749
750 release
751
752 1> float, maxdigits10 = 9
753 1> 6 algorithms used.
754 1> Digits required = 24.0000000
755 1> find root of 28.0000000, expected answer = 3.03658897
756 1> Times 0 312 6875 937 937 937
757 1> Iterations: 0 0 8 6 4 5
758 1> Distance: 0 0 -1 0 0 0
759 1> Roots: 3.03658891 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
760
761
762 1>
763 1> 5 algorithms used:
764 1> 10 algorithms used:
765 1> boost::math::cbrt TOMS748 Newton Halley Shroeder boost::math::cbrt TOMS748 Newton Halley Shroeder
766 1> 2 types compared.
767 1> Precision of full type = 102 decimal digits
768 1> Find root of 28.000000000000000,
769 1> Expected answer = 3.0365889718756625
770 1> typeid(T).name()float, maxdigits10 = 9
771 1> find root of 28.0000000, expected answer = 3.03658897
772 1>
773 1> Iterations: 0 8 6 4 5
774 1> Times 468 8437 4375 3593 4062
775 1> Min Time 468
776 1> Normalized Times 1.00 18.0 9.35 7.68 8.68
777 1> Distance: 0 -1 0 0 0
778 1> Roots: 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
779 1> ==================================================================
780 1> typeid(T).name()double, maxdigits10 = 17
781 1> find root of 28.000000000000000, expected answer = 3.0365889718756625
782 1>
783 1> Iterations: 0 11 7 5 6
784 1> Times 312 15000 4531 3906 4375
785 1> Min Time 312
786 1> Normalized Times 1.00 48.1 14.5 12.5 14.0
787 1> Distance: 1 2 0 0 0
788 1> Roots: 3.0365889718756622 3.0365889718756618 3.0365889718756627 3.0365889718756627 3.0365889718756627
789 1> ==================================================================
790
791
792 Release
793
794 1> 5 algorithms used:
795 1> 10 algorithms used:
796 1> boost::math::cbrt TOMS748 Newton Halley Shroeder boost::math::cbrt TOMS748 Newton Halley Shroeder
797 1> 2 types compared.
798 1> Precision of full type = 102 decimal digits
799 1> Find root of 28.000000000000000,
800 1> Expected answer = 3.0365889718756625
801 1> typeid(T).name()float, maxdigits10 = 9
802 1> find root of 28.0000000, expected answer = 3.03658897
803 1>
804 1> Iterations: 0 8 6 4 5
805 1> Times 312 781 937 937 937
806 1> Min Time 312
807 1> Normalized Times 1.00 2.50 3.00 3.00 3.00
808 1> Distance: 0 -1 0 0 0
809 1> Roots: 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
810 1> ==================================================================
811 1> typeid(T).name()double, maxdigits10 = 17
812 1> find root of 28.000000000000000, expected answer = 3.0365889718756625
813 1>
814 1> Iterations: 0 11 7 5 6
815 1> Times 312 1093 937 937 937
816 1> Min Time 312
817 1> Normalized Times 1.00 3.50 3.00 3.00 3.00
818 1> Distance: 1 2 0 0 0
819 1> Roots: 3.0365889718756622 3.0365889718756618 3.0365889718756627 3.0365889718756627 3.0365889718756627
820 1> ==================================================================
821
822
823
824 1> 5 algorithms used:
825 1> 15 algorithms used:
826 1> boost::math::cbrt TOMS748 Newton Halley Shroeder boost::math::cbrt TOMS748 Newton Halley Shroeder boost::math::cbrt TOMS748 Newton Halley Shroeder
827 1> 3 types compared.
828 1> Precision of full type = 102 decimal digits
829 1> Find root of 28.00000000000000000000000000000000000000000000000000,
830 1> Expected answer = 3.036588971875662519420809578505669635581453977248111
831 1> typeid(T).name()float, maxdigits10 = 9
832 1> find root of 28.0000000, expected answer = 3.03658897
833 1>
834 1> Iterations: 0 8 6 4 5
835 1> Times 156 781 937 1093 937
836 1> Min Time 156
837 1> Normalized Times 1.00 5.01 6.01 7.01 6.01
838 1> Distance: 0 -1 0 0 0
839 1> Roots: 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
840 1> ==================================================================
841 1> typeid(T).name()double, maxdigits10 = 17
842 1> find root of 28.000000000000000, expected answer = 3.0365889718756625
843 1>
844 1> Iterations: 0 11 7 5 6
845 1> Times 312 1093 937 937 937
846 1> Min Time 312
847 1> Normalized Times 1.00 3.50 3.00 3.00 3.00
848 1> Distance: 1 2 0 0 0
849 1> Roots: 3.0365889718756622 3.0365889718756618 3.0365889718756627 3.0365889718756627 3.0365889718756627
850 1> ==================================================================
851 1> typeid(T).name()class boost::multiprecision::number<class boost::multiprecision::backends::cpp_bin_float<50,10,void,int,0,0>,0>, maxdigits10 = 52
852 1> find root of 28.00000000000000000000000000000000000000000000000000, expected answer = 3.036588971875662519420809578505669635581453977248111
853 1>
854 1> Iterations: 0 13 9 6 7
855 1> Times 8750 177343 30312 52968 58125
856 1> Min Time 8750
857 1> Normalized Times 1.00 20.3 3.46 6.05 6.64
858 1> Distance: 0 0 -1 0 0
859 1> Roots: 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248117 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248106
860 1> ==================================================================
861
862 Reduce accuracy required to 0.5
863
864 1> 5 algorithms used:
865 1> 15 algorithms used:
866 1> boost::math::cbrt TOMS748 Newton Halley Shroeder
867 1> 3 floating_point types compared.
868 1> Precision of full type = 102 decimal digits
869 1> Find root of 28.00000000000000000000000000000000000000000000000000,
870 1> Expected answer = 3.036588971875662519420809578505669635581453977248111
871 1> typeid(T).name() = float, maxdigits10 = 9
872 1> Digits accuracy fraction required = 0.500000000
873 1> find root of 28.0000000, expected answer = 3.03658897
874 1>
875 1> Iterations: 0 8 5 3 4
876 1> Times 156 5937 1406 1250 1250
877 1> Min Time 156
878 1> Normalized Times 1.0 38. 9.0 8.0 8.0
879 1> Distance: 0 -1 0 0 0
880 1> Roots: 3.03658891 3.03658915 3.03658891 3.03658891 3.03658891
881 1> ==================================================================
882 1> typeid(T).name() = double, maxdigits10 = 17
883 1> Digits accuracy fraction required = 0.50000000000000000
884 1> find root of 28.000000000000000, expected answer = 3.0365889718756625
885 1>
886 1> Iterations: 0 8 6 4 5
887 1> Times 156 6250 1406 1406 1250
888 1> Min Time 156
889 1> Normalized Times 1.0 40. 9.0 9.0 8.0
890 1> Distance: 1 3695766 0 0 0
891 1> Roots: 3.0365889718756622 3.0365889702344129 3.0365889718756627 3.0365889718756627 3.0365889718756627
892 1> ==================================================================
893 1> typeid(T).name() = class boost::multiprecision::number<class boost::multiprecision::backends::cpp_bin_float<50,10,void,int,0,0>,0>, maxdigits10 = 52
894 1> Digits accuracy fraction required = 0.5000000000000000000000000000000000000000000000000000
895 1> find root of 28.00000000000000000000000000000000000000000000000000, expected answer = 3.036588971875662519420809578505669635581453977248111
896 1>
897 1> Iterations: 0 11 8 5 6
898 1> Times 11562 239843 34843 47500 47812
899 1> Min Time 11562
900 1> Normalized Times 1.0 21. 3.0 4.1 4.1
901 1> Distance: 0 0 -1 0 0
902 1> Roots: 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248117 3.036588971875662519420809578505669635581453977248106 3.036588971875662519420809578505669635581453977248106
903 1> ==================================================================
904
905
906
907 */
908