test_bessel_k.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // (C) Copyright John Maddock 2007.
  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. #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
  6. #include <boost/math/concepts/real_concept.hpp>
  7. #define BOOST_TEST_MAIN
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/test/tools/floating_point_comparison.hpp>
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/type_traits/is_floating_point.hpp>
  12. #include <boost/array.hpp>
  13. #include "functor.hpp"
  14. #include "handle_test_result.hpp"
  15. #include "table_type.hpp"
  16. #ifndef SC_
  17. # define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  18. #endif
  19. template <class T>
  20. T cyl_bessel_k_int_wrapper(T v, T x)
  21. {
  22. #ifdef BESSEL_KN_FUNCTION_TO_TEST
  23. return static_cast<T>(
  24. BESSEL_KN_FUNCTION_TO_TEST(
  25. boost::math::itrunc(v), x));
  26. #else
  27. return static_cast<T>(
  28. boost::math::cyl_bessel_k(
  29. boost::math::itrunc(v), x));
  30. #endif
  31. }
  32. template <class Real, class T>
  33. void do_test_cyl_bessel_k(const T& data, const char* type_name, const char* test_name)
  34. {
  35. #if !(defined(ERROR_REPORTING_MODE) && !defined(BESSEL_K_FUNCTION_TO_TEST))
  36. typedef Real value_type;
  37. typedef value_type (*pg)(value_type, value_type);
  38. #ifdef BESSEL_K_FUNCTION_TO_TEST
  39. pg funcp = BESSEL_K_FUNCTION_TO_TEST;
  40. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  41. pg funcp = boost::math::cyl_bessel_k<value_type, value_type>;
  42. #else
  43. pg funcp = boost::math::cyl_bessel_k;
  44. #endif
  45. boost::math::tools::test_result<value_type> result;
  46. std::cout << "Testing " << test_name << " with type " << type_name
  47. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  48. //
  49. // test cyl_bessel_k against data:
  50. //
  51. result = boost::math::tools::test_hetero<Real>(
  52. data,
  53. bind_func<Real>(funcp, 0, 1),
  54. extract_result<Real>(2));
  55. handle_test_result(result, data[result.worst()], result.worst(), type_name, "cyl_bessel_k", test_name);
  56. std::cout << std::endl;
  57. #endif
  58. }
  59. template <class Real, class T>
  60. void do_test_cyl_bessel_k_int(const T& data, const char* type_name, const char* test_name)
  61. {
  62. #if !(defined(ERROR_REPORTING_MODE) && !defined(BESSEL_KN_FUNCTION_TO_TEST))
  63. typedef Real value_type;
  64. typedef value_type (*pg)(value_type, value_type);
  65. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  66. pg funcp = cyl_bessel_k_int_wrapper<value_type>;
  67. #else
  68. pg funcp = cyl_bessel_k_int_wrapper;
  69. #endif
  70. boost::math::tools::test_result<value_type> result;
  71. std::cout << "Testing " << test_name << " with type " << type_name
  72. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  73. //
  74. // test cyl_bessel_k against data:
  75. //
  76. result = boost::math::tools::test_hetero<Real>(
  77. data,
  78. bind_func<Real>(funcp, 0, 1),
  79. extract_result<Real>(2));
  80. handle_test_result(result, data[result.worst()], result.worst(), type_name, "cyl_bessel_k (integer orders)", test_name);
  81. std::cout << std::endl;
  82. #endif
  83. }
  84. template <class T>
  85. void test_bessel(T, const char* name)
  86. {
  87. // function values calculated on http://functions.wolfram.com/
  88. static const boost::array<boost::array<typename table_type<T>::type, 3>, 9> k0_data = {{
  89. {{ SC_(0.0), SC_(1.0), SC_(0.421024438240708333335627379212609036136219748226660472298970) }},
  90. {{ SC_(0.0), SC_(2.0), SC_(0.113893872749533435652719574932481832998326624388808882892530) }},
  91. {{ SC_(0.0), SC_(4.0), SC_(0.0111596760858530242697451959798334892250090238884743405382553) }},
  92. {{ SC_(0.0), SC_(8.0), SC_(0.000146470705222815387096584408698677921967305368833759024089154) }},
  93. {{ SC_(0.0), SC_(0.000030517578125) /*T(std::ldexp(1.0, -15))*/, SC_(10.5131392267382037062459525561594822400447325776672021972753) }},
  94. {{ SC_(0.0), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(20.9103469324567717360787328239372191382743831365906131108531) }},
  95. {{ SC_(0.0), SC_(8.67361737988403547205962240695953369140625e-19) /*T(std::ldexp(1.0, -60))*/, SC_(41.7047623492551310138446473188663682295952219631968830346918) }},
  96. {{ SC_(0.0), SC_(50.0), SC_(3.41016774978949551392067551235295223184502537762334808993276e-23) }},
  97. {{ SC_(0.0), SC_(100.0), SC_(4.65662822917590201893900528948388635580753948544211387402671e-45) }},
  98. }};
  99. static const boost::array<boost::array<typename table_type<T>::type, 3>, 9> k1_data = {{
  100. {{ SC_(1.0), SC_(1.0), SC_(0.601907230197234574737540001535617339261586889968106456017768) }},
  101. {{ SC_(1.0), SC_(2.0), SC_(0.139865881816522427284598807035411023887234584841515530384442) }},
  102. {{ SC_(1.0), SC_(4.0), SC_(0.0124834988872684314703841799808060684838415849886258457917076) }},
  103. {{ SC_(1.0), SC_(8.0), SC_(0.000155369211805001133916862450622474621117065122872616157079566) }},
  104. {{ SC_(1.0), SC_(0.000030517578125) /*T(std::ldexp(1.0, -15))*/, SC_(32767.9998319528316432647441316539139725104728341577594326513) }},
  105. {{ SC_(1.0), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(1.07374182399999999003003028572687332810353799544215073362305e9) }},
  106. {{ SC_(1.0), SC_(8.67361737988403547205962240695953369140625e-19) /*T(std::ldexp(1.0, -60))*/, SC_(1.15292150460684697599999999999999998169660198868126604634036e18) }},
  107. {{ SC_(1.0), SC_(50.0), SC_(3.44410222671755561259185303591267155099677251348256880221927e-23) }},
  108. {{ SC_(1.0), SC_(100.0), SC_(4.67985373563690928656254424202433530797494354694335352937465e-45) }},
  109. }};
  110. static const boost::array<boost::array<typename table_type<T>::type, 3>, 9> kn_data = {{
  111. {{ SC_(2.0), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(2.30584300921369395150000000000000000234841952009593636868109e18) }},
  112. {{ SC_(5.0), SC_(10.0), SC_(0.0000575418499853122792763740236992723196597629124356739596921536) }},
  113. {{ SC_(-5.0), SC_(100.0), SC_(5.27325611329294989461777188449044716451716555009882448801072e-45) }},
  114. {{ SC_(10.0), SC_(10.0), SC_(0.00161425530039067002345725193091329085443750382929208307802221) }},
  115. {{ SC_(10.0), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(3.78470202927236255215249281534478864916684072926050665209083e98) }},
  116. {{ SC_(-10.0), SC_(1.0), SC_(1.80713289901029454691597861302340015908245782948536080022119e8) }},
  117. {{ SC_(100.0), SC_(5.0), SC_(7.03986019306167654653386616796116726248616158936088056952477e115) }},
  118. {{ SC_(100.0), SC_(80.0), SC_(8.39287107246490782848985384895907681748152272748337807033319e-12) }},
  119. {{ SC_(-1000.0), SC_(700.0), SC_(6.51561979144735818903553852606383312984409361984128221539405e-31) }},
  120. }};
  121. static const boost::array<boost::array<typename table_type<T>::type, 3>, 11> kv_data = {{
  122. {{ SC_(0.5), SC_(0.875), SC_(0.558532231646608646115729767013630967055657943463362504577189) }},
  123. {{ SC_(0.5), SC_(1.125), SC_(0.383621010650189547146769320487006220295290256657827220786527) }},
  124. {{ SC_(2.25), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(5.62397392719283271332307799146649700147907612095185712015604e20) }},
  125. {{ SC_(5.5), SC_(3.1416015625) /*3217/1024*/, SC_(1.30623288775012596319554857587765179889689223531159532808379) }},
  126. {{ SC_(-5.5), SC_(10.0), SC_(0.0000733045300798502164644836879577484533096239574909573072142667) }},
  127. {{ SC_(-5.5), SC_(100.0), SC_(5.41274555306792267322084448693957747924412508020839543293369e-45) }},
  128. {{ SC_(10.0), SC_(0.0009765625) /*1/1024*/, SC_(2.35522579263922076203415803966825431039900000000993410734978e38) }},
  129. {{ SC_(10.0), SC_(10.0), SC_(0.00161425530039067002345725193091329085443750382929208307802221) }},
  130. {{ SC_(141.3994140625) /*T(144793)/1024)*/, SC_(100.0), SC_(1.39565245860302528069481472855619216759142225046370312329416e-6) }},
  131. {{ SC_(141.3994140625) /*T(144793)/1024)*/, SC_(200.0), SC_(9.11950412043225432171915100042647230802198254567007382956336e-68) }},
  132. {{ SC_(-141.3994140625) /*T(-144793)/1024*/, SC_(50.0), SC_(1.30185229717525025165362673848737761549946548375142378172956e42) }},
  133. }};
  134. static const boost::array<boost::array<typename table_type<T>::type, 3>, 5> kv_large_data = {{
  135. // Bug report https://svn.boost.org/trac/boost/ticket/5560:
  136. {{ SC_(-1.0), SC_(3.729170365600103371645482657731466918688235767300203447135759166603139192535059152468087445200213901e-155) /*static_cast<T>(ldexp(0.5, -512))*/, SC_(2.68156158598851941991480499964116922549587316411847867554471e154) }},
  137. {{ SC_(1.0), SC_(3.729170365600103371645482657731466918688235767300203447135759166603139192535059152468087445200213901e-155) /*static_cast<T>(ldexp(0.5, -512))*/, SC_(2.68156158598851941991480499964116922549587316411847867554471e154) }},
  138. {{ SC_(-1.125), SC_(3.729170365600103371645482657731466918688235767300203447135759166603139192535059152468087445200213901e-155) /*static_cast<T>(ldexp(0.5, -512))*/, SC_(5.53984048006472105611199242328122729730752165907526178753978e173) }},
  139. {{ SC_(1.125), SC_(3.729170365600103371645482657731466918688235767300203447135759166603139192535059152468087445200213901e-155) /*static_cast<T>(ldexp(0.5, -512))*/, SC_(5.53984048006472105611199242328122729730752165907526178753978e173) }},
  140. {{ SC_(0.5), SC_(1.2458993688871959419388378518880931736878259938089494331010226962863582408064841833232475731084062642684629e-206) /*static_cast<T>(ldexp(0.5, -683))*/, SC_(1.12284149973980088540335945247019177715948513804063794284101e103) }},
  141. }};
  142. do_test_cyl_bessel_k<T>(k0_data, name, "Bessel K0: Mathworld Data");
  143. do_test_cyl_bessel_k<T>(k1_data, name, "Bessel K1: Mathworld Data");
  144. do_test_cyl_bessel_k<T>(kn_data, name, "Bessel Kn: Mathworld Data");
  145. do_test_cyl_bessel_k_int<T>(k0_data, name, "Bessel K0: Mathworld Data (Integer Version)");
  146. do_test_cyl_bessel_k_int<T>(k1_data, name, "Bessel K1: Mathworld Data (Integer Version)");
  147. do_test_cyl_bessel_k_int<T>(kn_data, name, "Bessel Kn: Mathworld Data (Integer Version)");
  148. do_test_cyl_bessel_k<T>(kv_data, name, "Bessel Kv: Mathworld Data");
  149. if(0 != static_cast<T>(ldexp(0.5, -512)))
  150. do_test_cyl_bessel_k<T>(kv_large_data, name, "Bessel Kv: Mathworld Data (large values)");
  151. #include "bessel_k_int_data.ipp"
  152. do_test_cyl_bessel_k<T>(bessel_k_int_data, name, "Bessel Kn: Random Data");
  153. #include "bessel_k_data.ipp"
  154. do_test_cyl_bessel_k<T>(bessel_k_data, name, "Bessel Kv: Random Data");
  155. }