]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/root_n_finding_algorithms.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / math / example / root_n_finding_algorithms.cpp
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, Halley & Schroder algorithms.
9 // root_n_finding_algorithms.cpp Generalised for nth root version.
10
11 // http://en.wikipedia.org/wiki/Cube_root
12
13 // Note that this file contains Quickbook mark-up as well as code
14 // and comments, don't change any of the special comment mark-ups!
15 // This program also writes files in Quickbook tables mark-up format.
16
17 #include <boost/cstdlib.hpp>
18 #include <boost/config.hpp>
19 #include <boost/array.hpp>
20 #include <boost/type_traits/is_floating_point.hpp>
21 #include <boost/math/concepts/real_concept.hpp>
22 #include <boost/math/tools/roots.hpp>
23
24 //using boost::math::policies::policy;
25 //using boost::math::tools::eps_tolerance; // Binary functor for specified number of bits.
26 //using boost::math::tools::bracket_and_solve_root;
27 //using boost::math::tools::toms748_solve;
28 //using boost::math::tools::halley_iterate;
29 //using boost::math::tools::newton_raphson_iterate;
30 //using boost::math::tools::schroder_iterate;
31
32 #include <boost/math/special_functions/next.hpp> // For float_distance.
33 #include <boost/math/special_functions/pow.hpp> // For pow<N>.
34 #include <boost/math/tools/tuple.hpp> // for tuple and make_tuple.
35
36 #include <boost/multiprecision/cpp_bin_float.hpp> // is binary.
37 using boost::multiprecision::cpp_bin_float_100;
38 using boost::multiprecision::cpp_bin_float_50;
39
40 #include <boost/timer/timer.hpp>
41 #include <boost/system/error_code.hpp>
42 #include <boost/preprocessor/stringize.hpp>
43
44 // STL
45 #include <iostream>
46 #include <iomanip>
47 #include <string>
48 #include <vector>
49 #include <limits>
50 #include <fstream> // std::ofstream
51 #include <cmath>
52 #include <typeinfo> // for type name using typid(thingy).name();
53
54 #ifdef __FILE__
55 std::string sourcefilename = __FILE__;
56 #else
57 std::string sourcefilename("");
58 #endif
59
60 std::string chop_last(std::string s)
61 {
62 std::string::size_type pos = s.find_last_of("\\/");
63 if(pos != std::string::npos)
64 s.erase(pos);
65 else if(s.empty())
66 abort();
67 else
68 s.erase();
69 return s;
70 }
71
72 std::string make_root()
73 {
74 std::string result;
75 if(sourcefilename.find_first_of(":") != std::string::npos)
76 {
77 result = chop_last(sourcefilename); // lose filename part
78 result = chop_last(result); // lose /example/
79 result = chop_last(result); // lose /math/
80 result = chop_last(result); // lose /libs/
81 }
82 else
83 {
84 result = chop_last(sourcefilename); // lose filename part
85 if(result.empty())
86 result = ".";
87 result += "/../../..";
88 }
89 return result;
90 }
91
92 std::string short_file_name(std::string s)
93 {
94 std::string::size_type pos = s.find_last_of("\\/");
95 if(pos != std::string::npos)
96 s.erase(0, pos + 1);
97 return s;
98 }
99
100 std::string boost_root = make_root();
101
102
103 std::string fp_hardware; // Any hardware features like SEE or AVX
104
105 const std::string roots_name = "libs/math/doc/roots/";
106
107 const std::string full_roots_name(boost_root + "/libs/math/doc/roots/");
108
109 const std::size_t nooftypes = 4;
110 const std::size_t noofalgos = 4;
111
112 double digits_accuracy = 1.0; // 1 == maximum possible accuracy.
113
114 std::stringstream ss;
115
116 std::ofstream fout;
117
118 std::vector<std::string> algo_names =
119 {
120 "TOMS748", "Newton", "Halley", "Schr'''&#xf6;'''der"
121 };
122
123 std::vector<std::string> names =
124 {
125 "float", "double", "long double", "cpp_bin_float50"
126 };
127
128 uintmax_t iters; // Global as value of iterations is not returned.
129
130 struct root_info
131 { // for a floating-point type, float, double ...
132 std::size_t max_digits10; // for type.
133 std::string full_typename; // for type from type_id.name().
134 std::string short_typename; // for type "float", "double", "cpp_bin_float_50" ....
135 std::size_t bin_digits; // binary in floating-point type numeric_limits<T>::digits;
136 int get_digits; // fraction of maximum possible accuracy required.
137 // = digits * digits_accuracy
138 // Vector of values (4) for each algorithm, TOMS748, Newton, Halley & Schroder.
139 //std::vector< boost::int_least64_t> times; converted to int.
140 std::vector<int> times; // arbirary units (ticks).
141 //boost::int_least64_t min_time = std::numeric_limits<boost::int_least64_t>::max(); // Used to normalize times (as int).
142 std::vector<double> normed_times;
143 int min_time = (std::numeric_limits<int>::max)(); // Used to normalize times.
144 std::vector<uintmax_t> iterations;
145 std::vector<long int> distances;
146 std::vector<cpp_bin_float_100> full_results;
147 }; // struct root_info
148
149 std::vector<root_info> root_infos; // One element for each floating-point type used.
150
151 inline std::string build_test_name(const char* type_name, const char* test_name)
152 {
153 std::string result(BOOST_COMPILER);
154 result += "|";
155 result += BOOST_STDLIB;
156 result += "|";
157 result += BOOST_PLATFORM;
158 result += "|";
159 result += type_name;
160 result += "|";
161 result += test_name;
162 #if defined(_DEBUG) || !defined(NDEBUG)
163 result += "|";
164 result += " debug";
165 #else
166 result += "|";
167 result += " release";
168 #endif
169 result += "|";
170 return result;
171 } // std::string build_test_name
172
173 // Algorithms //////////////////////////////////////////////
174
175 // No derivatives - using TOMS748 internally.
176
177 template <int N, typename T = double>
178 struct nth_root_functor_noderiv
179 { // Nth root of x using only function - no derivatives.
180 nth_root_functor_noderiv(T const& to_find_root_of) : a(to_find_root_of)
181 { // Constructor just stores value a to find root of.
182 }
183 T operator()(T const& x)
184 {
185 using boost::math::pow;
186 T fx = pow<N>(x) -a; // Difference (estimate x^n - a).
187 return fx;
188 }
189 private:
190 T a; // to be 'cube_rooted'.
191 }; // template <int N, class T> struct nth_root_functor_noderiv
192
193 template <int N, class T = double>
194 T nth_root_noderiv(T x)
195 { // return Nth root of x using bracket_and_solve (using NO derivatives).
196 using namespace std; // Help ADL of std functions.
197 using namespace boost::math::tools; // For bracket_and_solve_root.
198
199 typedef double guess_type;
200
201 int exponent;
202 frexp(static_cast<guess_type>(x), &exponent); // Get exponent of z (ignore mantissa).
203 T guess = static_cast<T>(ldexp(static_cast<guess_type>(1.), exponent / N)); // Rough guess is to divide the exponent by n.
204 //T min = static_cast<T>(ldexp(static_cast<guess_type>(1.) / 2, exponent / N)); // Minimum possible value is half our guess.
205 //T max = static_cast<T>(ldexp(static_cast<guess_type>(2.), exponent / N)); // Maximum possible value is twice our guess.
206
207 T factor = 2; // How big steps to take when searching.
208
209 const boost::uintmax_t maxit = 50; // Limit to maximum iterations.
210 boost::uintmax_t it = maxit; // Initally our chosen max iterations, but updated with actual.
211 bool is_rising = true; // So if result if guess^3 is too low, then try increasing guess.
212 // Some fraction of digits is used to control how accurate to try to make the result.
213 int get_digits = std::numeric_limits<T>::digits - 2;
214 eps_tolerance<T> tol(get_digits); // Set the tolerance.
215 std::pair<T, T> r;
216 r = bracket_and_solve_root(nth_root_functor_noderiv<N, T>(x), guess, factor, is_rising, tol, it);
217 iters = it;
218 T result = r.first + (r.second - r.first) / 2; // Midway between brackets.
219 return result;
220 } // template <class T> T nth_root_noderiv(T x)
221
222 // Using 1st derivative only Newton-Raphson
223
224 template <int N, class T = double>
225 struct nth_root_functor_1deriv
226 { // Functor also returning 1st derviative.
227 BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
228 BOOST_STATIC_ASSERT_MSG((N > 0) == true, "root N must be > 0!");
229
230 nth_root_functor_1deriv(T const& to_find_root_of) : a(to_find_root_of)
231 { // Constructor stores value a to find root of, for example:
232 }
233 std::pair<T, T> operator()(T const& x)
234 { // Return both f(x) and f'(x).
235 using boost::math::pow; // // Compile-time integral power.
236 T p = pow<N - 1>(x);
237 return std::make_pair(p * x - a, N * p); // 'return' both fx and dx.
238 }
239
240 private:
241 T a; // to be 'nth_rooted'.
242 }; // struct nthroot__functor_1deriv
243
244 template <int N, class T = double>
245 T nth_root_1deriv(T x)
246 { // return nth root of x using 1st derivative and Newton_Raphson.
247 using namespace std; // Help ADL of std functions.
248 using namespace boost::math::tools; // For newton_raphson_iterate.
249
250 BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
251 BOOST_STATIC_ASSERT_MSG((N > 0) == true, "root N must be > 0!");
252 BOOST_STATIC_ASSERT_MSG((N > 1000) == false, "root N is too big!");
253
254 typedef double guess_type;
255
256 int exponent;
257 frexp(static_cast<guess_type>(x), &exponent); // Get exponent of z (ignore mantissa).
258 T guess = static_cast<T>(ldexp(static_cast<guess_type>(1.), exponent / N)); // Rough guess is to divide the exponent by n.
259 T min = static_cast<T>(ldexp(static_cast<guess_type>(1.) / 2, exponent / N)); // Minimum possible value is half our guess.
260 T max = static_cast<T>(ldexp(static_cast<guess_type>(2.), exponent / N)); // Maximum possible value is twice our guess.
261
262 int digits = std::numeric_limits<T>::digits; // Maximum possible binary digits accuracy for type T.
263 int get_digits = static_cast<int>(digits * 0.6);
264 const boost::uintmax_t maxit = 20;
265 boost::uintmax_t it = maxit;
266 T result = newton_raphson_iterate(nth_root_functor_1deriv<N, T>(x), guess, min, max, get_digits, it);
267 iters = it;
268 return result;
269 } // T nth_root_1_deriv Newton-Raphson
270
271 // Using 1st and 2nd derivatives with Halley algorithm.
272
273 template <int N, class T = double>
274 struct nth_root_functor_2deriv
275 { // Functor returning both 1st and 2nd derivatives.
276 BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
277 BOOST_STATIC_ASSERT_MSG((N > 0) == true, "root N must be > 0!");
278
279 nth_root_functor_2deriv(T const& to_find_root_of) : a(to_find_root_of)
280 { // Constructor stores value a to find root of, for example:
281 }
282
283 // using boost::math::tuple; // to return three values.
284 std::tuple<T, T, T> operator()(T const& x)
285 { // Return f(x), f'(x) and f''(x).
286 using boost::math::pow; // Compile-time integral power.
287 T p = pow<N - 2>(x);
288
289 return std::make_tuple(p * x * x - a, p * x * N, p * N * (N - 1)); // 'return' fx, dx and d2x.
290 }
291 private:
292 T a; // to be 'nth_rooted'.
293 };
294
295 template <int N, class T = double>
296 T nth_root_2deriv(T x)
297 { // return nth root of x using 1st and 2nd derivatives and Halley.
298
299 using namespace std; // Help ADL of std functions.
300 using namespace boost::math::tools; // For halley_iterate.
301
302 BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
303 BOOST_STATIC_ASSERT_MSG((N > 0) == true, "root N must be > 0!");
304 BOOST_STATIC_ASSERT_MSG((N > 1000) == false, "root N is too big!");
305
306 typedef double guess_type;
307
308 int exponent;
309 frexp(static_cast<guess_type>(x), &exponent); // Get exponent of z (ignore mantissa).
310 T guess = static_cast<T>(ldexp(static_cast<guess_type>(1.), exponent / N)); // Rough guess is to divide the exponent by n.
311 T min = static_cast<T>(ldexp(static_cast<guess_type>(1.) / 2, exponent / N)); // Minimum possible value is half our guess.
312 T max = static_cast<T>(ldexp(static_cast<guess_type>(2.), exponent / N)); // Maximum possible value is twice our guess.
313
314 int digits = std::numeric_limits<T>::digits; // Maximum possible binary digits accuracy for type T.
315 int get_digits = static_cast<int>(digits * 0.4);
316 const boost::uintmax_t maxit = 20;
317 boost::uintmax_t it = maxit;
318 T result = halley_iterate(nth_root_functor_2deriv<N, T>(x), guess, min, max, get_digits, it);
319 iters = it;
320
321 return result;
322 } // nth_2deriv Halley
323
324 template <int N, class T = double>
325 T nth_root_2deriv_s(T x)
326 { // return nth root of x using 1st and 2nd derivatives and Schroder.
327
328 using namespace std; // Help ADL of std functions.
329 using namespace boost::math::tools; // For schroder_iterate.
330
331 BOOST_STATIC_ASSERT_MSG(boost::is_integral<T>::value == false, "Only floating-point type types can be used!");
332 BOOST_STATIC_ASSERT_MSG((N > 0) == true, "root N must be > 0!");
333 BOOST_STATIC_ASSERT_MSG((N > 1000) == false, "root N is too big!");
334
335 typedef double guess_type;
336
337 int exponent;
338 frexp(static_cast<guess_type>(x), &exponent); // Get exponent of z (ignore mantissa).
339 T guess = static_cast<T>(ldexp(static_cast<guess_type>(1.), exponent / N)); // Rough guess is to divide the exponent by n.
340 T min = static_cast<T>(ldexp(static_cast<guess_type>(1.) / 2, exponent / N)); // Minimum possible value is half our guess.
341 T max = static_cast<T>(ldexp(static_cast<guess_type>(2.), exponent / N)); // Maximum possible value is twice our guess.
342
343 int get_digits = static_cast<int>(std::numeric_limits<T>::digits * 0.4);
344 const boost::uintmax_t maxit = 20;
345 boost::uintmax_t it = maxit;
346 T result = schroder_iterate(nth_root_functor_2deriv<N, T>(x), guess, min, max, get_digits, it);
347 iters = it;
348
349 return result;
350 } // T nth_root_2deriv_s Schroder
351
352 //////////////////////////////////////////////////////// end of algorithms - perhaps in a separate .hpp?
353
354 //! Print 4 floating-point types info: max_digits10, digits and required accuracy digits as a Quickbook table.
355 int table_type_info(double digits_accuracy)
356 {
357 std::string qbk_name = full_roots_name; // Prefix by boost_root file.
358
359 qbk_name += "type_info_table";
360 std::stringstream ss;
361 ss.precision(3);
362 ss << "_" << digits_accuracy * 100;
363 qbk_name += ss.str();
364
365 #ifdef _MSC_VER
366 qbk_name += "_msvc.qbk";
367 #else // assume GCC
368 qbk_name += "_gcc.qbk";
369 #endif
370
371 // Example: type_info_table_100_msvc.qbk
372 fout.open(qbk_name, std::ios_base::out);
373
374 if (fout.is_open())
375 {
376 std::cout << "Output type table to " << qbk_name << std::endl;
377 }
378 else
379 { // Failed to open.
380 std::cout << " Open file " << qbk_name << " for output failed!" << std::endl;
381 std::cout << "errno " << errno << std::endl;
382 return errno;
383 }
384
385 fout <<
386 "[/"
387 << qbk_name
388 << "\n"
389 "Copyright 2015 Paul A. Bristow.""\n"
390 "Copyright 2015 John Maddock.""\n"
391 "Distributed under the Boost Software License, Version 1.0.""\n"
392 "(See accompanying file LICENSE_1_0.txt or copy at""\n"
393 "http://www.boost.org/LICENSE_1_0.txt).""\n"
394 "]""\n"
395 << std::endl;
396
397 fout << "[h6 Fraction of maximum possible bits of accuracy required is " << digits_accuracy << ".]\n" << std::endl;
398
399 std::string table_id("type_info");
400 table_id += ss.str(); // Fraction digits accuracy.
401
402 #ifdef _MSC_VER
403 table_id += "_msvc";
404 #else // assume GCC
405 table_id += "_gcc";
406 #endif
407
408 fout << "[table:" << table_id << " Digits for float, double, long double and cpp_bin_float_50\n"
409 << "[[type name] [max_digits10] [binary digits] [required digits]]\n";// header.
410
411 // For all fout types:
412
413 fout << "[[" << "float" << "]"
414 << "[" << std::numeric_limits<float>::max_digits10 << "]" // max_digits10
415 << "[" << std::numeric_limits<float>::digits << "]"// < "Binary digits
416 << "[" << static_cast<int>(std::numeric_limits<float>::digits * digits_accuracy) << "]]\n"; // Accuracy digits.
417
418 fout << "[[" << "float" << "]"
419 << "[" << std::numeric_limits<double>::max_digits10 << "]" // max_digits10
420 << "[" << std::numeric_limits<double>::digits << "]"// < "Binary digits
421 << "[" << static_cast<int>(std::numeric_limits<double>::digits * digits_accuracy) << "]]\n"; // Accuracy digits.
422
423 fout << "[[" << "long double" << "]"
424 << "[" << std::numeric_limits<long double>::max_digits10 << "]" // max_digits10
425 << "[" << std::numeric_limits<long double>::digits << "]"// < "Binary digits
426 << "[" << static_cast<int>(std::numeric_limits<long double>::digits * digits_accuracy) << "]]\n"; // Accuracy digits.
427
428 fout << "[[" << "cpp_bin_float_50" << "]"
429 << "[" << std::numeric_limits<cpp_bin_float_50>::max_digits10 << "]" // max_digits10
430 << "[" << std::numeric_limits<cpp_bin_float_50>::digits << "]"// < "Binary digits
431 << "[" << static_cast<int>(std::numeric_limits<cpp_bin_float_50>::digits * digits_accuracy) << "]]\n"; // Accuracy digits.
432
433 fout << "] [/table table_id_msvc] \n" << std::endl; // End of table.
434
435 fout.close();
436 return 0;
437 } // type_table
438
439 //! Evaluate root N timing for each algorithm, and for one floating-point type T.
440 template <int N, typename T>
441 int test_root(cpp_bin_float_100 big_value, cpp_bin_float_100 answer, const char* type_name, std::size_t type_no)
442 {
443 std::size_t max_digits = 2 + std::numeric_limits<T>::digits * 3010 / 10000;
444 // For new versions use max_digits10
445 // std::cout.precision(std::numeric_limits<T>::max_digits10);
446 std::cout.precision(max_digits);
447 std::cout << std::showpoint << std::endl; // Show trailing zeros too.
448
449 root_infos.push_back(root_info());
450
451 root_infos[type_no].max_digits10 = max_digits;
452 root_infos[type_no].full_typename = typeid(T).name(); // Full typename.
453 root_infos[type_no].short_typename = type_name; // Short typename.
454 root_infos[type_no].bin_digits = std::numeric_limits<T>::digits;
455 root_infos[type_no].get_digits = static_cast<int>(std::numeric_limits<T>::digits * digits_accuracy);
456
457 T to_root = static_cast<T>(big_value);
458
459 T result; // root
460 T sum = 0;
461 T ans = static_cast<T>(answer);
462
463 using boost::timer::nanosecond_type;
464 using boost::timer::cpu_times;
465 using boost::timer::cpu_timer;
466
467 int eval_count = boost::is_floating_point<T>::value ? 10000000 : 100000; // To give a sufficiently stable timing for the fast built-in types,
468 //int eval_count = 1000000; // To give a sufficiently stable timing for the fast built-in types,
469 // This takes an inconveniently long time for multiprecision cpp_bin_float_50 etc types.
470
471 cpu_times now; // Holds wall, user and system times.
472
473 { // Evaluate times etc for each algorithm.
474 //algorithm_names.push_back("TOMS748"); //
475 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
476 ti.start();
477 for (long i = 0; i < eval_count; ++i)
478 {
479 result = nth_root_noderiv<N, T>(to_root); //
480 sum += result;
481 }
482 now = ti.elapsed();
483 int time = static_cast<int>(now.user / eval_count);
484 root_infos[type_no].times.push_back(time); // CPU time taken.
485 if (time < root_infos[type_no].min_time)
486 {
487 root_infos[type_no].min_time = time;
488 }
489 ti.stop();
490 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
491 root_infos[type_no].distances.push_back(distance);
492 root_infos[type_no].iterations.push_back(iters); //
493 root_infos[type_no].full_results.push_back(result);
494 }
495 {
496 // algorithm_names.push_back("Newton"); // algorithm
497 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
498 ti.start();
499 for (long i = 0; i < eval_count; ++i)
500 {
501 result = nth_root_1deriv<N, T>(to_root); //
502 sum += result;
503 }
504 now = ti.elapsed();
505 int time = static_cast<int>(now.user / eval_count);
506 root_infos[type_no].times.push_back(time); // CPU time taken.
507 if (time < root_infos[type_no].min_time)
508 {
509 root_infos[type_no].min_time = time;
510 }
511
512 ti.stop();
513 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
514 root_infos[type_no].distances.push_back(distance);
515 root_infos[type_no].iterations.push_back(iters); //
516 root_infos[type_no].full_results.push_back(result);
517 }
518 {
519 //algorithm_names.push_back("Halley"); // algorithm
520 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
521 ti.start();
522 for (long i = 0; i < eval_count; ++i)
523 {
524 result = nth_root_2deriv<N>(to_root); //
525 sum += result;
526 }
527 now = ti.elapsed();
528 int time = static_cast<int>(now.user / eval_count);
529 root_infos[type_no].times.push_back(time); // CPU time taken.
530 ti.stop();
531 if (time < root_infos[type_no].min_time)
532 {
533 root_infos[type_no].min_time = time;
534 }
535 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
536 root_infos[type_no].distances.push_back(distance);
537 root_infos[type_no].iterations.push_back(iters); //
538 root_infos[type_no].full_results.push_back(result);
539 }
540 {
541 // algorithm_names.push_back("Schroder"); // algorithm
542 cpu_timer ti; // Can start, pause, resume and stop, and read elapsed.
543 ti.start();
544 for (long i = 0; i < eval_count; ++i)
545 {
546 result = nth_root_2deriv_s<N>(to_root); //
547 sum += result;
548 }
549 now = ti.elapsed();
550 int time = static_cast<int>(now.user / eval_count);
551 root_infos[type_no].times.push_back(time); // CPU time taken.
552 if (time < root_infos[type_no].min_time)
553 {
554 root_infos[type_no].min_time = time;
555 }
556 ti.stop();
557 long int distance = static_cast<int>(boost::math::float_distance<T>(result, ans));
558 root_infos[type_no].distances.push_back(distance);
559 root_infos[type_no].iterations.push_back(iters); //
560 root_infos[type_no].full_results.push_back(result);
561 }
562 for (size_t i = 0; i != root_infos[type_no].times.size(); i++) // For each time.
563 { // Normalize times.
564 root_infos[type_no].normed_times.push_back(static_cast<double>(root_infos[type_no].times[i]) / root_infos[type_no].min_time);
565 }
566
567 std::cout << "Accumulated result was: " << sum << std::endl;
568
569 return 4; // eval_count of how many algorithms used.
570 } // test_root
571
572 /*! Fill array of times, interations, etc for Nth root for all 4 types,
573 and write a table of results in Quickbook format.
574 */
575 template <int N>
576 void table_root_info(cpp_bin_float_100 full_value)
577 {
578 using std::abs;
579 std::cout << nooftypes << " floating-point types tested:" << std::endl;
580 #if defined(_DEBUG) || !defined(NDEBUG)
581 std::cout << "Compiled in debug mode." << std::endl;
582 #else
583 std::cout << "Compiled in optimise mode." << std::endl;
584 #endif
585 std::cout << "FP hardware " << fp_hardware << std::endl;
586 // Compute the 'right' answer for root N at 100 decimal digits.
587 cpp_bin_float_100 full_answer = nth_root_noderiv<N, cpp_bin_float_100>(full_value);
588
589 root_infos.clear(); // Erase any previous data.
590 // Fill the elements of the array for each floating-point type.
591
592 test_root<N, float>(full_value, full_answer, "float", 0);
593 test_root<N, double>(full_value, full_answer, "double", 1);
594 test_root<N, long double>(full_value, full_answer, "long double", 2);
595 test_root<N, cpp_bin_float_50>(full_value, full_answer, "cpp_bin_float_50", 3);
596
597 // Use info from 4 floating point types to
598
599 // Prepare Quickbook table for a single root
600 // with columns of times, iterations, distances repeated for various floating-point types,
601 // and 4 rows for each algorithm.
602
603 std::stringstream table_info;
604 table_info.precision(3);
605 table_info << "[table:root_" << N << " " << N << "th root(" << static_cast<float>(full_value) << ") for float, double, long double and cpp_bin_float_50 types";
606 if (fp_hardware != "")
607 {
608 table_info << ", using " << fp_hardware;
609 }
610 table_info << std::endl;
611
612 fout << table_info.str()
613 << "[[][float][][][] [][double][][][] [][long d][][][] [][cpp50][][]]\n"
614 << "[[Algo ]";
615 for (size_t tp = 0; tp != nooftypes; tp++)
616 { // For all types:
617 fout << "[Its]" << "[Times]" << "[Norm]" << "[Dis]" << "[ ]";
618 }
619 fout << "]" << std::endl;
620
621 // Row for all algorithms.
622 for (std::size_t algo = 0; algo != noofalgos; algo++)
623 {
624 fout << "[[" << std::left << std::setw(9) << algo_names[algo] << "]";
625 for (size_t tp = 0; tp != nooftypes; tp++)
626 { // For all types:
627 fout
628 << "[" << std::right << std::showpoint
629 << std::setw(3) << std::setprecision(2) << root_infos[tp].iterations[algo] << "]["
630 << std::setw(5) << std::setprecision(5) << root_infos[tp].times[algo] << "][";
631 fout << std::setw(3) << std::setprecision(3);
632 double normed_time = root_infos[tp].normed_times[algo];
633 if (abs(normed_time - 1.00) <= 0.05)
634 { // At or near the best time, so show as blue.
635 fout << "[role blue " << normed_time << "]";
636 }
637 else if (abs(normed_time) > 4.)
638 { // markedly poor so show as red.
639 fout << "[role red " << normed_time << "]";
640 }
641 else
642 { // Not the best, so normal black.
643 fout << normed_time;
644 }
645 fout << "]["
646 << std::setw(3) << std::setprecision(2) << root_infos[tp].distances[algo] << "][ ]";
647 } // tp
648 fout << "]" << std::endl;
649 } // for algo
650 fout << "] [/end of table root]\n";
651 } // void table_root_info
652
653 /*! Output program header, table of type info, and tables for 4 algorithms and 4 floating-point types,
654 for Nth root required digits_accuracy.
655 */
656
657 int roots_tables(cpp_bin_float_100 full_value, double digits_accuracy)
658 {
659 ::digits_accuracy = digits_accuracy;
660 // Save globally so that it is available to root-finding algorithms. Ugly :-(
661
662 #if defined(_DEBUG) || !defined(NDEBUG)
663 std::string debug_or_optimize("Compiled in debug mode.");
664 #else
665 std::string debug_or_optimize("Compiled in optimise mode.");
666 #endif
667
668 // Create filename for roots_table
669 std::string qbk_name = full_roots_name;
670 qbk_name += "roots_table";
671
672 std::stringstream ss;
673 ss.precision(3);
674 // ss << "_" << N // now put all the tables in one .qbk file?
675 ss << "_" << digits_accuracy * 100
676 << std::flush;
677 // Assume only save optimize mode runs, so don't add any _DEBUG info.
678 qbk_name += ss.str();
679
680 #ifdef _MSC_VER
681 qbk_name += "_msvc";
682 #else // assume GCC
683 qbk_name += "_gcc";
684 #endif
685 if (fp_hardware != "")
686 {
687 qbk_name += fp_hardware;
688 }
689 qbk_name += ".qbk";
690
691 fout.open(qbk_name, std::ios_base::out);
692
693 if (fout.is_open())
694 {
695 std::cout << "Output root table to " << qbk_name << std::endl;
696 }
697 else
698 { // Failed to open.
699 std::cout << " Open file " << qbk_name << " for output failed!" << std::endl;
700 std::cout << "errno " << errno << std::endl;
701 return errno;
702 }
703
704 fout <<
705 "[/"
706 << qbk_name
707 << "\n"
708 "Copyright 2015 Paul A. Bristow.""\n"
709 "Copyright 2015 John Maddock.""\n"
710 "Distributed under the Boost Software License, Version 1.0.""\n"
711 "(See accompanying file LICENSE_1_0.txt or copy at""\n"
712 "http://www.boost.org/LICENSE_1_0.txt).""\n"
713 "]""\n"
714 << std::endl;
715
716 // Print out the program/compiler/stdlib/platform names as a Quickbook comment:
717 fout << "\n[h6 Program " << sourcefilename << ",\n "
718 << BOOST_COMPILER << ", "
719 << BOOST_STDLIB << ", "
720 << BOOST_PLATFORM << "\n"
721 << debug_or_optimize
722 << ((fp_hardware != "") ? ", " + fp_hardware : "")
723 << "]" // [h6 close].
724 << std::endl;
725
726 fout << "Fraction of full accuracy " << digits_accuracy << std::endl;
727
728 table_root_info<5>(full_value);
729 table_root_info<7>(full_value);
730 table_root_info<11>(full_value);
731
732 fout.close();
733
734 // table_type_info(digits_accuracy);
735
736 return 0;
737 } // roots_tables
738
739
740 int main()
741 {
742 using namespace boost::multiprecision;
743 using namespace boost::math;
744
745
746 try
747 {
748 std::cout << "Tests run with " << BOOST_COMPILER << ", "
749 << BOOST_STDLIB << ", " << BOOST_PLATFORM << ", ";
750
751 // How to: Configure Visual C++ Projects to Target 64-Bit Platforms
752 // https://msdn.microsoft.com/en-us/library/9yb4317s.aspx
753
754 #ifdef _M_X64 // Defined for compilations that target x64 processors.
755 std::cout << "X64 " << std::endl;
756 fp_hardware += "_X64";
757 #else
758 # ifdef _M_IX86
759 std::cout << "X32 " << std::endl;
760 fp_hardware += "_X86";
761 # endif
762 #endif
763
764 #ifdef _M_AMD64
765 std::cout << "AMD64 " << std::endl;
766 // fp_hardware += "_AMD64";
767 #endif
768
769 // https://msdn.microsoft.com/en-us/library/7t5yh4fd.aspx
770 // /arch (x86) options /arch:[IA32|SSE|SSE2|AVX|AVX2]
771 // default is to use SSE and SSE2 instructions by default.
772 // https://msdn.microsoft.com/en-us/library/jj620901.aspx
773 // /arch (x64) options /arch:AVX and /arch:AVX2
774
775 // MSVC doesn't bother to set these SSE macros!
776 // http://stackoverflow.com/questions/18563978/sse-sse2-is-enabled-control-in-visual-studio
777 // https://msdn.microsoft.com/en-us/library/b0084kay.aspx predefined macros.
778
779 // But some of these macros are *not* defined by MSVC,
780 // unlike AVX (but *are* defined by GCC and Clang).
781 // So the macro code above does define them.
782 #if (defined(_M_AMD64) || defined (_M_X64))
783 #ifndef _M_X64
784 # define _M_X64
785 #endif
786 #ifndef __SSE2__
787 # define __SSE2__
788 #endif
789 #else
790 # ifdef _M_IX86_FP // Expands to an integer literal value indicating which /arch compiler option was used:
791 std::cout << "Floating-point _M_IX86_FP = " << _M_IX86_FP << std::endl;
792 # if (_M_IX86_FP == 2) // 2 if /arch:SSE2, /arch:AVX or /arch:AVX2
793 # define __SSE2__ // x32
794 # elif (_M_IX86_FP == 1) // 1 if /arch:SSE was used.
795 # define __SSE__ // x32
796 # elif (_M_IX86_FP == 0) // 0 if /arch:IA32 was used.
797 # define _X32 // No special FP instructions.
798 # endif
799 # endif
800 #endif
801 // Set the fp_hardware that is used in the .qbk filename.
802 #ifdef __AVX2__
803 std::cout << "Floating-point AVX2 " << std::endl;
804 fp_hardware += "_AVX2";
805 # else
806 # ifdef __AVX__
807 std::cout << "Floating-point AVX " << std::endl;
808 fp_hardware += "_AVX";
809 # else
810 # ifdef __SSE2__
811 std::cout << "Floating-point SSE2 " << std::endl;
812 fp_hardware += "_SSE2";
813 # else
814 # ifdef __SSE__
815 std::cout << "Floating-point SSE " << std::endl;
816 fp_hardware += "_SSE";
817 # endif
818 # endif
819 # endif
820 # endif
821
822 #ifdef _M_IX86
823 std::cout << "Floating-point X86 _M_IX86 = " << _M_IX86 << std::endl;
824 // https://msdn.microsoft.com/en-us/library/aa273918%28v=vs.60%29.aspx#_predir_table_1..3
825 // 600 = Pentium Pro
826 #endif
827
828 #ifdef _MSC_FULL_VER
829 std::cout << "Floating-point _MSC_FULL_VER " << _MSC_FULL_VER << std::endl;
830 #endif
831
832 #ifdef __MSVC_RUNTIME_CHECKS
833 std::cout << "Runtime __MSVC_RUNTIME_CHECKS " << std::endl;
834 #endif
835
836 BOOST_MATH_CONTROL_FP;
837
838 cpp_bin_float_100 full_value("28.");
839 // Compute full answer to more than precision of tests.
840 //T value = 28.; // integer (exactly representable as floating-point)
841 // whose cube root is *not* exactly representable.
842 // Wolfram Alpha command N[28 ^ (1 / 3), 100] computes cube root to 100 decimal digits.
843 // 3.036588971875662519420809578505669635581453977248111123242141654169177268411884961770250390838097895
844
845 std::cout.precision(100);
846 std::cout << "value " << full_value << std::endl;
847 // std::cout << ",\n""answer = " << full_answer << std::endl;
848 std::cout.precision(6);
849 // cbrt cpp_bin_float_100 full_answer("3.036588971875662519420809578505669635581453977248111123242141654169177268411884961770250390838097895");
850
851 // Output the table of types, maxdigits10 and digits and required digits for some accuracies.
852
853 // Output tables for some roots at full accuracy.
854 roots_tables(full_value, 1.);
855
856 // Output tables for some roots at less accuracy.
857 //roots_tables(full_value, 0.75);
858
859 return boost::exit_success;
860 }
861 catch (std::exception const& ex)
862 {
863 std::cout << "exception thrown: " << ex.what() << std::endl;
864 return boost::exit_failure;
865 }
866 } // int main()
867
868 /*
869
870 */