test_expint.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007, 2009
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/math/concepts/real_concept.hpp>
  7. #include <boost/math/special_functions/math_fwd.hpp>
  8. #define BOOST_TEST_MAIN
  9. #include <boost/test/unit_test.hpp>
  10. #include <boost/test/tools/floating_point_comparison.hpp>
  11. #include <boost/math/tools/stats.hpp>
  12. #include <boost/math/tools/test.hpp>
  13. #include <boost/math/constants/constants.hpp>
  14. #include <boost/type_traits/is_floating_point.hpp>
  15. #include <boost/array.hpp>
  16. #include "functor.hpp"
  17. #include "handle_test_result.hpp"
  18. #include "table_type.hpp"
  19. #ifndef SC_
  20. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  21. #endif
  22. template <class T>
  23. T expint_wrapper(T n, T z)
  24. {
  25. #ifdef EN_FUNCTION_TO_TEST
  26. return EN_FUNCTION_TO_TEST(
  27. boost::math::itrunc(n), z);
  28. #else
  29. return boost::math::expint(
  30. boost::math::itrunc(n), z);
  31. #endif
  32. }
  33. template <class Real, class T>
  34. void do_test_expint(const T& data, const char* type_name, const char* test_name)
  35. {
  36. #if !(defined(ERROR_REPORTING_MODE) && !defined(EN_FUNCTION_TO_TEST))
  37. //
  38. // test En(T) against data:
  39. //
  40. using namespace std;
  41. typedef Real value_type;
  42. std::cout << test_name << " with type " << type_name << std::endl;
  43. typedef value_type (*pg)(value_type, value_type);
  44. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  45. pg funcp = expint_wrapper<value_type>;
  46. #else
  47. pg funcp = expint_wrapper;
  48. #endif
  49. boost::math::tools::test_result<value_type> result;
  50. //
  51. // test expint against data:
  52. //
  53. result = boost::math::tools::test_hetero<Real>(
  54. data,
  55. bind_func<Real>(funcp, 0, 1),
  56. extract_result<Real>(2));
  57. handle_test_result(result, data[result.worst()], result.worst(), type_name, "expint (En)", test_name);
  58. std::cout << std::endl;
  59. #endif
  60. }
  61. template <class Real, class T>
  62. void do_test_expint_Ei(const T& data, const char* type_name, const char* test_name)
  63. {
  64. #if !(defined(ERROR_REPORTING_MODE) && !defined(EI_FUNCTION_TO_TEST))
  65. //
  66. // test Ei(T) against data:
  67. //
  68. using namespace std;
  69. typedef Real value_type;
  70. std::cout << test_name << " with type " << type_name << std::endl;
  71. typedef value_type (*pg)(value_type);
  72. #ifdef EI_FUNCTION_TO_TEST
  73. pg funcp = EI_FUNCTION_TO_TEST;
  74. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  75. pg funcp = boost::math::expint<value_type>;
  76. #else
  77. pg funcp = boost::math::expint;
  78. #endif
  79. boost::math::tools::test_result<value_type> result;
  80. //
  81. // test expint against data:
  82. //
  83. result = boost::math::tools::test_hetero<Real>(
  84. data,
  85. bind_func<Real>(funcp, 0),
  86. extract_result<Real>(1));
  87. handle_test_result(result, data[result.worst()], result.worst(), type_name, "expint (Ei)", test_name);
  88. #endif
  89. }
  90. template <class T>
  91. void test_expint(T, const char* name)
  92. {
  93. //
  94. // The actual test data is rather verbose, so it's in a separate file
  95. //
  96. #include "expint_data.ipp"
  97. do_test_expint<T>(expint_data, name, "Exponential Integral En");
  98. #include "expint_small_data.ipp"
  99. do_test_expint<T>(expint_small_data, name, "Exponential Integral En: small z values");
  100. #include "expint_1_data.ipp"
  101. do_test_expint<T>(expint_1_data, name, "Exponential Integral E1");
  102. #include "expinti_data.ipp"
  103. do_test_expint_Ei<T>(expinti_data, name, "Exponential Integral Ei");
  104. if(boost::math::tools::log_max_value<T>() > 100)
  105. {
  106. #include "expinti_data_double.ipp"
  107. do_test_expint_Ei<T>(expinti_data_double, name, "Exponential Integral Ei: double exponent range");
  108. }
  109. #if (defined(LDBL_MAX_10_EXP) && (LDBL_MAX_10_EXP > 2000)) || defined(TEST_UDT)
  110. if(boost::math::tools::log_max_value<T>() > 1000)
  111. {
  112. #include "expinti_data_long.ipp"
  113. do_test_expint_Ei<T>(expinti_data_long, name, "Exponential Integral Ei: long exponent range");
  114. }
  115. #endif
  116. }
  117. template <class T>
  118. void test_spots(T, const char* t)
  119. {
  120. std::cout << "Testing basic sanity checks for type " << t << std::endl;
  121. //
  122. // Basic sanity checks, tolerance is 100 epsilon
  123. // expressed as a percentage:
  124. //
  125. T tolerance = boost::math::tools::epsilon<T>() * 100 *
  126. (boost::is_floating_point<T>::value ? 500 : 500);
  127. //
  128. // En:
  129. //
  130. BOOST_CHECK_CLOSE(::boost::math::expint(0, static_cast<T>(1)/1024), static_cast<T>(1023.0004881223430781283448725609773366468629307172L), tolerance);
  131. BOOST_CHECK_CLOSE(::boost::math::expint(0, static_cast<T>(0.125F)), static_cast<T>(7.0599752206767632229191371458324058897760385999252L), tolerance);
  132. BOOST_CHECK_CLOSE(::boost::math::expint(0, static_cast<T>(0.5F)), static_cast<T>(1.2130613194252668472075990699823609068838362709744L), tolerance);
  133. BOOST_CHECK_CLOSE(::boost::math::expint(0, static_cast<T>(1.5F)), static_cast<T>(0.14875344009895321928885364717600834756144775290739L), tolerance);
  134. BOOST_CHECK_CLOSE(::boost::math::expint(0, static_cast<T>(4.5F)), static_cast<T>(0.0024686658973871792213651409526512283936753927790603L), tolerance);
  135. BOOST_CHECK_CLOSE(::boost::math::expint(0, static_cast<T>(50.0F)), static_cast<T>(3.8574996959278355660346856330540251495056653024605e-24L), tolerance);
  136. BOOST_CHECK_CLOSE(::boost::math::expint(1, static_cast<T>(1)/1024), static_cast<T>(6.3552324648310718026144555193580322129376300855378L), tolerance);
  137. BOOST_CHECK_CLOSE(::boost::math::expint(1, static_cast<T>(0.125F)), static_cast<T>(1.6234256405841687914563069246244088736331060573721L), tolerance);
  138. BOOST_CHECK_CLOSE(::boost::math::expint(1, static_cast<T>(0.5F)), static_cast<T>(0.55977359477616081174679593931508523522684689031635L), tolerance);
  139. BOOST_CHECK_CLOSE(::boost::math::expint(1, static_cast<T>(1.5F)), static_cast<T>(0.10001958240663265190190933991166697826173000614035L), tolerance);
  140. BOOST_CHECK_CLOSE(::boost::math::expint(1, static_cast<T>(4.5F)), static_cast<T>(0.0020734007547146144328855938695797884889319725701443L), tolerance);
  141. BOOST_CHECK_CLOSE(::boost::math::expint(1, static_cast<T>(50.0F)), static_cast<T>(3.7832640295504590186989678540212857803028931862511e-24L), tolerance);
  142. BOOST_CHECK_CLOSE(::boost::math::expint(2, static_cast<T>(1)/1024), static_cast<T>(0.99281763247803906867747111039220635198625517639812L), tolerance);
  143. BOOST_CHECK_CLOSE(::boost::math::expint(2, static_cast<T>(0.125F)), static_cast<T>(0.67956869751157430393285377765099962701786656781914L), tolerance);
  144. BOOST_CHECK_CLOSE(::boost::math::expint(2, static_cast<T>(0.5F)), static_cast<T>(0.32664386232455301773040156533363783582849469032901L), tolerance);
  145. BOOST_CHECK_CLOSE(::boost::math::expint(2, static_cast<T>(1.5F)), static_cast<T>(0.073100786538480851080416460896512053949576620150553L), tolerance);
  146. BOOST_CHECK_CLOSE(::boost::math::expint(2, static_cast<T>(4.5F)), static_cast<T>(0.0017786931420265415481579618738214795713453909401220L), tolerance);
  147. BOOST_CHECK_CLOSE(::boost::math::expint(2, static_cast<T>(50.0F)), static_cast<T>(3.7117833188688273667858889516369684601386058104706e-24L), tolerance);
  148. BOOST_CHECK_CLOSE(::boost::math::expint(5, static_cast<T>(1)/1024), static_cast<T>(0.24967471743034509414923673526350536071348482068601L), tolerance);
  149. BOOST_CHECK_CLOSE(::boost::math::expint(5, static_cast<T>(0.125F)), static_cast<T>(0.21195078838966585733668853784460504343264488743527L), tolerance);
  150. BOOST_CHECK_CLOSE(::boost::math::expint(5, static_cast<T>(0.5F)), static_cast<T>(0.13097731169586484777931864012654136046214618229236L), tolerance);
  151. BOOST_CHECK_CLOSE(::boost::math::expint(5, static_cast<T>(1.5F)), static_cast<T>(0.038529924425495155395971538166055731456940831714065L), tolerance);
  152. BOOST_CHECK_CLOSE(::boost::math::expint(5, static_cast<T>(4.5F)), static_cast<T>(0.0012311157382296328534406162790653865883418172939975L), tolerance);
  153. BOOST_CHECK_CLOSE(::boost::math::expint(5, static_cast<T>(50.0F)), static_cast<T>(3.5124400631332394056901125740903320085753990490695e-24L), tolerance);
  154. BOOST_CHECK_CLOSE(::boost::math::expint(22, static_cast<T>(1)/1024), static_cast<T>(0.047570244582119027573000641518739337055625638422014L), tolerance);
  155. BOOST_CHECK_CLOSE(::boost::math::expint(22, static_cast<T>(0.125F)), static_cast<T>(0.041762730174712898120606793100375713866392079558609L), tolerance);
  156. BOOST_CHECK_CLOSE(::boost::math::expint(22, static_cast<T>(0.5F)), static_cast<T>(0.028178840877963713109230354192609362941651630844684L), tolerance);
  157. BOOST_CHECK_CLOSE(::boost::math::expint(22, static_cast<T>(1.5F)), static_cast<T>(0.0098864453561701486668317763826728620676449196215016L), tolerance);
  158. BOOST_CHECK_CLOSE(::boost::math::expint(22, static_cast<T>(4.5F)), static_cast<T>(0.00043257793497205419001613830279995985617548506505159L), tolerance);
  159. BOOST_CHECK_CLOSE(::boost::math::expint(22, static_cast<T>(50.0F)), static_cast<T>(2.6900194251201629500599598206345018300567305625080e-24L), tolerance);
  160. //
  161. // Ei:
  162. //
  163. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(1)/1024), static_cast<T>(-6.35327933972759151358547423727042905862963067106751711596065L), tolerance);
  164. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(0.125)), static_cast<T>(-1.37320852494298333781545045921206470808223543321810480716122L), tolerance);
  165. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(0.5)), static_cast<T>(0.454219904863173579920523812662802365281405554352642045162818L), tolerance);
  166. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(1)), static_cast<T>(1.89511781635593675546652093433163426901706058173270759164623L), tolerance);
  167. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(50.5)), static_cast<T>(1.72763195602911805201155668940185673806099654090456049881069e20L), tolerance);
  168. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(-1)/1024), static_cast<T>(-6.35523246483107180261445551935803221293763008553775821607264L), tolerance);
  169. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(-0.125)), static_cast<T>(-1.62342564058416879145630692462440887363310605737209536579267L), tolerance);
  170. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(-0.5)), static_cast<T>(-0.559773594776160811746795939315085235226846890316353515248293L), tolerance);
  171. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(-1)), static_cast<T>(-0.219383934395520273677163775460121649031047293406908207577979L), tolerance);
  172. BOOST_CHECK_CLOSE(::boost::math::expint(static_cast<T>(-50.5)), static_cast<T>(-2.27237132932219350440719707268817831250090574830769670186618e-24L), tolerance);
  173. }