test_kn.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright John Maddock 2015.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifdef _MSC_VER
  6. # pragma warning (disable : 4224)
  7. #endif
  8. #include <boost/math/special_functions/bessel.hpp>
  9. #include <boost/array.hpp>
  10. #include <boost/lexical_cast.hpp>
  11. #include "../../test/table_type.hpp"
  12. #include "table_helper.hpp"
  13. #include "performance.hpp"
  14. #include <iostream>
  15. typedef double T;
  16. #define SC_(x) static_cast<double>(x)
  17. static const boost::array<boost::array<T, 3>, 9> k0_data = { {
  18. { { SC_(0.0), SC_(1.0), SC_(0.421024438240708333335627379212609036136219748226660472298970) } },
  19. { { SC_(0.0), SC_(2.0), SC_(0.113893872749533435652719574932481832998326624388808882892530) } },
  20. { { SC_(0.0), SC_(4.0), SC_(0.0111596760858530242697451959798334892250090238884743405382553) } },
  21. { { SC_(0.0), SC_(8.0), SC_(0.000146470705222815387096584408698677921967305368833759024089154) } },
  22. { { SC_(0.0), T(std::ldexp(1.0, -15)), SC_(10.5131392267382037062459525561594822400447325776672021972753) } },
  23. { { SC_(0.0), T(std::ldexp(1.0, -30)), SC_(20.9103469324567717360787328239372191382743831365906131108531) } },
  24. { { SC_(0.0), T(std::ldexp(1.0, -60)), SC_(41.7047623492551310138446473188663682295952219631968830346918) } },
  25. { { SC_(0.0), SC_(50.0), SC_(3.41016774978949551392067551235295223184502537762334808993276e-23) } },
  26. { { SC_(0.0), SC_(100.0), SC_(4.65662822917590201893900528948388635580753948544211387402671e-45) } },
  27. } };
  28. static const boost::array<boost::array<T, 3>, 9> k1_data = { {
  29. { { SC_(1.0), SC_(1.0), SC_(0.601907230197234574737540001535617339261586889968106456017768) } },
  30. { { SC_(1.0), SC_(2.0), SC_(0.139865881816522427284598807035411023887234584841515530384442) } },
  31. { { SC_(1.0), SC_(4.0), SC_(0.0124834988872684314703841799808060684838415849886258457917076) } },
  32. { { SC_(1.0), SC_(8.0), SC_(0.000155369211805001133916862450622474621117065122872616157079566) } },
  33. { { SC_(1.0), T(std::ldexp(1.0, -15)), SC_(32767.9998319528316432647441316539139725104728341577594326513) } },
  34. { { SC_(1.0), T(std::ldexp(1.0, -30)), SC_(1.07374182399999999003003028572687332810353799544215073362305e9) } },
  35. { { SC_(1.0), T(std::ldexp(1.0, -60)), SC_(1.15292150460684697599999999999999998169660198868126604634036e18) } },
  36. { { SC_(1.0), SC_(50.0), SC_(3.44410222671755561259185303591267155099677251348256880221927e-23) } },
  37. { { SC_(1.0), SC_(100.0), SC_(4.67985373563690928656254424202433530797494354694335352937465e-45) } },
  38. } };
  39. static const boost::array<boost::array<T, 3>, 9> kn_data = { {
  40. { { SC_(2.0), T(std::ldexp(1.0, -30)), SC_(2.30584300921369395150000000000000000234841952009593636868109e18) } },
  41. { { SC_(5.0), SC_(10.0), SC_(0.0000575418499853122792763740236992723196597629124356739596921536) } },
  42. { { SC_(-5.0), SC_(100.0), SC_(5.27325611329294989461777188449044716451716555009882448801072e-45) } },
  43. { { SC_(10.0), SC_(10.0), SC_(0.00161425530039067002345725193091329085443750382929208307802221) } },
  44. { { SC_(10.0), T(std::ldexp(1.0, -30)), SC_(3.78470202927236255215249281534478864916684072926050665209083e98) } },
  45. { { SC_(-10.0), SC_(1.0), SC_(1.80713289901029454691597861302340015908245782948536080022119e8) } },
  46. { { SC_(100.0), SC_(5.0), SC_(7.03986019306167654653386616796116726248616158936088056952477e115) } },
  47. { { SC_(100.0), SC_(80.0), SC_(8.39287107246490782848985384895907681748152272748337807033319e-12) } },
  48. { { SC_(-1000.0), SC_(700.0), SC_(6.51561979144735818903553852606383312984409361984128221539405e-31) } },
  49. } };
  50. int main()
  51. {
  52. #include "bessel_k_int_data.ipp"
  53. add_data(k0_data);
  54. add_data(k1_data);
  55. add_data(kn_data);
  56. add_data(bessel_k_int_data);
  57. unsigned data_total = data.size();
  58. screen_data([](const std::vector<double>& v){ return boost::math::cyl_bessel_k(static_cast<int>(v[0]), v[1]); }, [](const std::vector<double>& v){ return v[2]; });
  59. #if defined(TEST_LIBSTDCXX) && !defined(COMPILER_COMPARISON_TABLES)
  60. screen_data([](const std::vector<double>& v){ return std::tr1::cyl_bessel_k(static_cast<int>(v[0]), v[1]); }, [](const std::vector<double>& v){ return v[2]; });
  61. #endif
  62. #if defined(TEST_GSL) && !defined(COMPILER_COMPARISON_TABLES)
  63. screen_data([](const std::vector<double>& v){ return gsl_sf_bessel_Kn(static_cast<int>(v[0]), v[1]); }, [](const std::vector<double>& v){ return v[2]; });
  64. #endif
  65. #if defined(TEST_RMATH) && !defined(COMPILER_COMPARISON_TABLES)
  66. screen_data([](const std::vector<double>& v){ return bessel_k(v[1], static_cast<int>(v[0]), 1); }, [](const std::vector<double>& v){ return v[2]; });
  67. #endif
  68. unsigned data_used = data.size();
  69. std::string function = "cyl_bessel_k (integer order)[br](" + boost::lexical_cast<std::string>(data_used) + "/" + boost::lexical_cast<std::string>(data_total) + " tests selected)";
  70. std::string function_short = "cyl_bessel_k (integer order)";
  71. double time;
  72. time = exec_timed_test([](const std::vector<double>& v){ return boost::math::cyl_bessel_k(static_cast<int>(v[0]), v[1]); });
  73. std::cout << time << std::endl;
  74. #if !defined(COMPILER_COMPARISON_TABLES) && (defined(TEST_GSL) || defined(TEST_RMATH) || defined(TEST_LIBSTDCXX))
  75. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, boost_name());
  76. #endif
  77. report_execution_time(time, std::string("Compiler Comparison on ") + std::string(platform_name()), function_short, compiler_name() + std::string("[br]") + boost_name());
  78. //
  79. // Boost again, but with promotion to long double turned off:
  80. //
  81. #if !defined(COMPILER_COMPARISON_TABLES)
  82. if(sizeof(long double) != sizeof(double))
  83. {
  84. time = exec_timed_test([](const std::vector<double>& v){ return boost::math::cyl_bessel_k(static_cast<int>(v[0]), v[1], boost::math::policies::make_policy(boost::math::policies::promote_double<false>())); });
  85. std::cout << time << std::endl;
  86. #if !defined(COMPILER_COMPARISON_TABLES) && (defined(TEST_GSL) || defined(TEST_RMATH) || defined(TEST_LIBSTDCXX))
  87. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, boost_name() + "[br]promote_double<false>");
  88. #endif
  89. report_execution_time(time, std::string("Compiler Comparison on ") + std::string(platform_name()), function_short, compiler_name() + std::string("[br]") + boost_name() + "[br]promote_double<false>");
  90. }
  91. #endif
  92. #if defined(TEST_LIBSTDCXX) && !defined(COMPILER_COMPARISON_TABLES)
  93. time = exec_timed_test([](const std::vector<double>& v){ return std::tr1::cyl_bessel_k(static_cast<int>(v[0]), v[1]); });
  94. std::cout << time << std::endl;
  95. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, "tr1/cmath");
  96. #endif
  97. #if defined(TEST_GSL) && !defined(COMPILER_COMPARISON_TABLES)
  98. time = exec_timed_test([](const std::vector<double>& v){ return gsl_sf_bessel_Kn(static_cast<int>(v[0]), v[1]); });
  99. std::cout << time << std::endl;
  100. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, "GSL " GSL_VERSION);
  101. #endif
  102. #if defined(TEST_RMATH) && !defined(COMPILER_COMPARISON_TABLES)
  103. time = exec_timed_test([](const std::vector<double>& v){ return bessel_k(v[1], static_cast<int>(v[0]), 1); });
  104. std::cout << time << std::endl;
  105. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, "Rmath " R_VERSION_STRING);
  106. #endif
  107. return 0;
  108. }