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