root_finding_algorithms.cpp 33 KB

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