test_tgamma.cpp 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/gamma.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. int main()
  16. {
  17. typedef double T;
  18. #define SC_(x) static_cast<double>(x)
  19. # include "test_gamma_data.ipp"
  20. add_data(factorials);
  21. add_data(near_0);
  22. add_data(near_1);
  23. add_data(near_2);
  24. add_data(near_m10);
  25. add_data(near_m55);
  26. unsigned data_total = data.size();
  27. screen_data([](const std::vector<double>& v){ return boost::math::tgamma(v[0]); }, [](const std::vector<double>& v){ return v[1]; });
  28. #if defined(TEST_C99) && !defined(COMPILER_COMPARISON_TABLES)
  29. screen_data([](const std::vector<double>& v){ return ::tgamma(v[0]); }, [](const std::vector<double>& v){ return v[1]; });
  30. #endif
  31. #if defined(TEST_LIBSTDCXX) && !defined(COMPILER_COMPARISON_TABLES)
  32. screen_data([](const std::vector<double>& v){ return std::tr1::tgamma(v[0]); }, [](const std::vector<double>& v){ return v[1]; });
  33. #endif
  34. #if defined(TEST_GSL) && !defined(COMPILER_COMPARISON_TABLES)
  35. screen_data([](const std::vector<double>& v){ return gsl_sf_gamma(v[0]); }, [](const std::vector<double>& v){ return v[1]; });
  36. #endif
  37. #if defined(TEST_RMATH) && !defined(COMPILER_COMPARISON_TABLES)
  38. screen_data([](const std::vector<double>& v){ return gammafn(v[0]); }, [](const std::vector<double>& v){ return v[1]; });
  39. #endif
  40. unsigned data_used = data.size();
  41. std::string function = "tgamma[br](" + boost::lexical_cast<std::string>(data_used) + "/" + boost::lexical_cast<std::string>(data_total) + " tests selected)";
  42. std::string function_short = "tgamma";
  43. double time = exec_timed_test([](const std::vector<double>& v){ return boost::math::tgamma(v[0]); });
  44. std::cout << time << std::endl;
  45. #if !defined(COMPILER_COMPARISON_TABLES) && (defined(TEST_GSL) || defined(TEST_RMATH) || defined(TEST_C99) || defined(TEST_LIBSTDCXX))
  46. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, boost_name());
  47. #endif
  48. report_execution_time(time, std::string("Compiler Comparison on ") + std::string(platform_name()), function_short, compiler_name() + std::string("[br]") + boost_name());
  49. //
  50. // Boost again, but with promotion to long double turned off:
  51. //
  52. #if !defined(COMPILER_COMPARISON_TABLES)
  53. if(sizeof(long double) != sizeof(double))
  54. {
  55. double time = exec_timed_test([](const std::vector<double>& v){ return boost::math::tgamma(v[0], boost::math::policies::make_policy(boost::math::policies::promote_double<false>())); });
  56. std::cout << time << std::endl;
  57. #if !defined(COMPILER_COMPARISON_TABLES) && (defined(TEST_GSL) || defined(TEST_RMATH) || defined(TEST_C99) || defined(TEST_LIBSTDCXX))
  58. 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>");
  59. #endif
  60. 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>");
  61. }
  62. #endif
  63. #if defined(TEST_C99) && !defined(COMPILER_COMPARISON_TABLES)
  64. time = exec_timed_test([](const std::vector<double>& v){ return ::tgamma(v[0]); });
  65. std::cout << time << std::endl;
  66. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, "math.h");
  67. #endif
  68. #if defined(TEST_LIBSTDCXX) && !defined(COMPILER_COMPARISON_TABLES)
  69. time = exec_timed_test([](const std::vector<double>& v){ return std::tr1::tgamma(v[0]); });
  70. std::cout << time << std::endl;
  71. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, "tr1/cmath");
  72. #endif
  73. #if defined(TEST_GSL) && !defined(COMPILER_COMPARISON_TABLES)
  74. time = exec_timed_test([](const std::vector<double>& v){ return gsl_sf_gamma(v[0]); });
  75. std::cout << time << std::endl;
  76. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, "GSL " GSL_VERSION);
  77. #endif
  78. #if defined(TEST_RMATH) && !defined(COMPILER_COMPARISON_TABLES)
  79. time = exec_timed_test([](const std::vector<double>& v){ return gammafn(v[0]); });
  80. std::cout << time << std::endl;
  81. report_execution_time(time, std::string("Library Comparison with ") + std::string(compiler_name()) + std::string(" on ") + platform_name(), function, "Rmath " R_VERSION_STRING);
  82. #endif
  83. return 0;
  84. }