test_bessel_j_prime.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright (c) 2013 Anton Bikineev
  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/math/constants/constants.hpp>
  12. #include <boost/type_traits/is_floating_point.hpp>
  13. #include <boost/array.hpp>
  14. #include "functor.hpp"
  15. #include "handle_test_result.hpp"
  16. #include "table_type.hpp"
  17. #ifndef SC_
  18. # define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  19. #endif
  20. template <class Real, class T>
  21. void do_test_cyl_bessel_j_prime(const T& data, const char* type_name, const char* test_name)
  22. {
  23. #if !(defined(ERROR_REPORTING_MODE) && !defined(BESSEL_JP_FUNCTION_TO_TEST))
  24. typedef Real value_type;
  25. typedef value_type (*pg)(value_type, value_type);
  26. #ifdef BESSEL_JP_FUNCTION_TO_TEST
  27. pg funcp = BESSEL_JP_FUNCTION_TO_TEST;
  28. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  29. pg funcp = boost::math::cyl_bessel_j_prime<value_type, value_type>;
  30. #else
  31. pg funcp = boost::math::cyl_bessel_j_prime;
  32. #endif
  33. boost::math::tools::test_result<value_type> result;
  34. std::cout << "Testing " << test_name << " with type " << type_name
  35. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  36. //
  37. // test cyl_bessel_j against data:
  38. //
  39. result = boost::math::tools::test_hetero<Real>(
  40. data,
  41. bind_func<Real>(funcp, 0, 1),
  42. extract_result<Real>(2));
  43. handle_test_result(result, data[result.worst()], result.worst(), type_name, "cyl_bessel_j_prime", test_name);
  44. std::cout << std::endl;
  45. #endif
  46. }
  47. template <class T>
  48. T cyl_bessel_j_prime_int_wrapper(T v, T x)
  49. {
  50. #ifdef BESSEL_JPN_FUNCTION_TO_TEST
  51. return static_cast<T>(BESSEL_JPN_FUNCTION_TO_TEST(boost::math::itrunc(v), x));
  52. #else
  53. return static_cast<T>(boost::math::cyl_bessel_j_prime(boost::math::itrunc(v), x));
  54. #endif
  55. }
  56. template <class Real, class T>
  57. void do_test_cyl_bessel_j_prime_int(const T& data, const char* type_name, const char* test_name)
  58. {
  59. #if !(defined(ERROR_REPORTING_MODE) && !defined(BESSEL_JPN_FUNCTION_TO_TEST))
  60. typedef Real value_type;
  61. typedef value_type (*pg)(value_type, value_type);
  62. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  63. pg funcp = cyl_bessel_j_prime_int_wrapper<value_type>;
  64. #else
  65. pg funcp = cyl_bessel_j_prime_int_wrapper;
  66. #endif
  67. boost::math::tools::test_result<value_type> result;
  68. std::cout << "Testing " << test_name << " with type " << type_name
  69. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  70. //
  71. // test cyl_bessel_j against data:
  72. //
  73. result = boost::math::tools::test_hetero<Real>(
  74. data,
  75. bind_func<Real>(funcp, 0, 1),
  76. extract_result<Real>(2));
  77. handle_test_result(result, data[result.worst()], result.worst(), type_name, "cyl_bessel_j_prime (integer orders)", test_name);
  78. std::cout << std::endl;
  79. #endif
  80. }
  81. template <class Real, class T>
  82. void do_test_sph_bessel_j_prime(const T& data, const char* type_name, const char* test_name)
  83. {
  84. #if !(defined(ERROR_REPORTING_MODE) && !defined(BESSEL_JPS_FUNCTION_TO_TEST))
  85. typedef Real value_type;
  86. typedef value_type (*pg)(unsigned, value_type);
  87. #ifdef BESSEL_JPS_FUNCTION_TO_TEST
  88. pg funcp = BESSEL_JPS_FUNCTION_TO_TEST;
  89. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  90. pg funcp = boost::math::sph_bessel_prime<value_type>;
  91. #else
  92. pg funcp = boost::math::sph_bessel_prime;
  93. #endif
  94. boost::math::tools::test_result<value_type> result;
  95. std::cout << "Testing " << test_name << " with type " << type_name
  96. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  97. //
  98. // test sph_bessel against data:
  99. //
  100. result = boost::math::tools::test_hetero<Real>(
  101. data,
  102. bind_func_int1<Real>(funcp, 0, 1),
  103. extract_result<Real>(2));
  104. handle_test_result(result, data[result.worst()], result.worst(), type_name, "sph_bessel_prime", test_name);
  105. std::cout << std::endl;
  106. #endif
  107. }
  108. template <class T>
  109. void test_bessel_prime(T, const char* name)
  110. {
  111. //
  112. // The actual test data is rather verbose, so it's in a separate file
  113. //
  114. // The contents are as follows, each row of data contains
  115. // three items, input value a, input value b and J'(a, b):
  116. //
  117. // function values calculated on http://functions.wolfram.com/
  118. static const boost::array<boost::array<typename table_type<T>::type, 3>, 8> j0_data = {{
  119. {{ SC_(0.0), SC_(0.0), SC_(0.0) }},
  120. {{ SC_(0.0), SC_(1.0), SC_(-0.440050585744933515959682203718914913127) }},
  121. {{ SC_(0.0), SC_(-2.0), SC_(0.576724807756873387202448242269137086920) }},
  122. {{ SC_(0.0), SC_(4.0), SC_(0.06604332802354913614318542080327502873) }},
  123. {{ SC_(0.0), SC_(-8.0), SC_(0.2346363468539146243812766515904546115488) }},
  124. {{ SC_(0.0), SC_(1e-05), SC_(-0.499999999993750000000026041666666612413194e-5) }},
  125. {{ SC_(0.0), SC_(1e-10), SC_(-0.499999999999999999999375000000000000000000e-10) }},
  126. {{ SC_(0.0), SC_(-1e+01), SC_(0.0434727461688614366697487680258592883062724) }},
  127. }};
  128. static const boost::array<boost::array<T, 3>, 6> j0_tricky = {{
  129. // Big numbers make the accuracy of std::sin the limiting factor:
  130. {{ SC_(0.0), SC_(1e+03), SC_(-0.00472831190708952391757607190121691628542) }},
  131. {{ SC_(0.0), SC_(1e+05), SC_(-0.0018467575628825677163621239671142157437) }},
  132. // test at the regular Bessel roots:
  133. {{ SC_(0.0), T(2521642)/(1024 * 1024), SC_(-0.519147572225778564548541576612898453392794) }},
  134. {{ SC_(0.0), T(5788221)/(1024 * 1024), SC_(0.34026483151709114336072749629487476476084) }},
  135. {{ SC_(0.0), T(9074091)/(1024 * 1024), SC_(-0.271452311894657014854145327490965399410) }},
  136. {{ SC_(0.0), T(12364320)/(1024 * 1024), SC_(0.2324598316641066033541448467171088144257742) }}
  137. }};
  138. static const boost::array<boost::array<typename table_type<T>::type, 3>, 8> j1_data = {{
  139. {{ SC_(1.0), SC_(0.0), SC_(0.5) }},
  140. {{ SC_(1.0), SC_(1.0), SC_(0.325147100813033035490035322383748307781902) }},
  141. {{ SC_(1.0), SC_(-2.0), SC_(-0.064471624737201025549396666484619917634997) }},
  142. {{ SC_(1.0), SC_(4.0), SC_(-0.38063897785796008825079441325087928479376) }},
  143. {{ SC_(1.0), SC_(-8.0), SC_(0.1423212637808145780432098264031651746248) }},
  144. {{ SC_(1.0), SC_(1e-05), SC_(0.499999999981250000000130208333332953559028) }},
  145. {{ SC_(1.0), SC_(1e-10), SC_(0.499999999999999999998125000000000000000001) }},
  146. {{ SC_(1.0), SC_(-1e+01), SC_(-0.250283039068234478864735739287914682660226) }},
  147. }};
  148. static const boost::array<boost::array<T, 3>, 5> j1_tricky = {{
  149. // Big numbers make the accuracy of std::sin the limiting factor:
  150. {{ SC_(1.0), SC_(1e+03), SC_(0.024781957840513085037413155043792491869881) }},
  151. {{ SC_(1.0), SC_(1e+05), SC_(-0.0017192195838116010182477650983128728897) }},
  152. // test at the regular Bessel roots:
  153. {{ SC_(1.0), T(4017834)/(1024*1024), SC_(-0.4027594878673806944036073218740057200193405151367) }},
  154. {{ SC_(1.0), T(7356375)/(1024*1024), SC_(0.3001157854852247730548242543591186404228210449219) }},
  155. {{ SC_(1.0), T(10667654)/(1024*1024), SC_(-0.2497048893045206718888096020236844196915626525879) }},
  156. }};
  157. static const boost::array<boost::array<typename table_type<T>::type, 3>, 17> jn_data = {{
  158. {{ SC_(-1.0), SC_(1.25), SC_(-0.2374074770153809244011000600949046202003956) }},
  159. {{ SC_(2.0), SC_(0.0), SC_(0.0) }},
  160. {{ SC_(-2.0), SC_(0.0), SC_(0.0) }},
  161. {{ SC_(2.0), SC_(1e-02), SC_(0.00249995833352864539930612069540679799606337) }},
  162. {{ SC_(5.0), SC_(10.0), SC_(-0.102571922008611714904101858221407144485) }},
  163. {{ SC_(5.0), SC_(-10.0), SC_(-0.102571922008611714904101858221407144485) }},
  164. {{ SC_(-5.0), SC_(1e+06), SC_(-0.0003310524513007044105585859534523271988) }},
  165. {{ SC_(5.0), SC_(1e+06), SC_(0.0003310524513007044105585859534523271988) }},
  166. {{ SC_(-5.0), SC_(-1.0), SC_(-0.001227850313053782886909720690402218190791576) }},
  167. {{ SC_(10.0), SC_(10.0), SC_(0.08436957863176118824849051273337698304165) }},
  168. {{ SC_(10.0), SC_(-10.0), SC_(-0.08436957863176118824849051273337698304165) }},
  169. {{ SC_(10.0), SC_(-5.0), SC_(-0.00258467784485473925206548854676116157568106) }},
  170. {{ SC_(-10.0), SC_(1e+06), SC_(-0.0007259518037193243350387875733893635962) }},
  171. {{ SC_(10.0), SC_(1e+06), SC_(-0.0007259518037193243350387875733893635962) }},
  172. {{ SC_(1e+02), SC_(8e+01), SC_(3.5036060582489177538508950593467499997755e-06) }},
  173. {{ SC_(1e+03), SC_(1e+05), SC_(-0.0021724469777608393409850758227465776486) }},
  174. {{ SC_(10.0), SC_(1e-100), SC_(2.69114445546737213403880070546737213403880070547e-909) }},
  175. }};
  176. do_test_cyl_bessel_j_prime<T>(j0_data, name, "Bessel J0': Mathworld Data");
  177. do_test_cyl_bessel_j_prime<T>(j0_tricky, name, "Bessel J0': Mathworld Data (Tricky cases)");
  178. do_test_cyl_bessel_j_prime<T>(j1_data, name, "Bessel J1': Mathworld Data");
  179. do_test_cyl_bessel_j_prime<T>(j1_tricky, name, "Bessel J1': Mathworld Data (tricky cases)");
  180. do_test_cyl_bessel_j_prime<T>(jn_data, name, "Bessel JN': Mathworld Data");
  181. do_test_cyl_bessel_j_prime_int<T>(j0_data, name, "Bessel J0': Mathworld Data (Integer Version)");
  182. do_test_cyl_bessel_j_prime_int<T>(j0_tricky, name, "Bessel J0': Mathworld Data (Tricky cases) (Integer Version)");
  183. do_test_cyl_bessel_j_prime_int<T>(j1_data, name, "Bessel J1': Mathworld Data (Integer Version)");
  184. do_test_cyl_bessel_j_prime_int<T>(j1_tricky, name, "Bessel J1': Mathworld Data (tricky cases) (Integer Version)");
  185. do_test_cyl_bessel_j_prime_int<T>(jn_data, name, "Bessel JN': Mathworld Data (Integer Version)");
  186. static const boost::array<boost::array<T, 3>, 21> jv_data = {{
  187. {{ T(22.5), T(0), SC_(0.0) }},
  188. {{ T(2457)/1024, T(1)/1024, SC_(9.35477929043111040277363766198320562099360690e-6) }},
  189. {{ SC_(5.5), T(3217)/1024, SC_(0.042165579369684463582791278988393873) }},
  190. {{ SC_(-5.5), T(3217)/1024, SC_(3.361570113176257957139775812778503494) }},
  191. {{ SC_(-5.5), SC_(1e+04), SC_(0.007593311396019034252155600098309836289) }},
  192. {{ SC_(5.5), SC_(1e+04), SC_(-0.00245022241637437956702428797044365092) }},
  193. {{ SC_(5.5), SC_(1e+06), SC_(-0.000279242826717266554062248256927185394) }},
  194. {{ SC_(5.125), SC_(1e+06), SC_(0.0001830632695189459708211614700642271) }},
  195. {{ SC_(5.875), SC_(1e+06), SC_(-0.0006474276718101871487286860109203539) }},
  196. {{ SC_(0.5), SC_(101.0), SC_(0.070640819172197226936337703929857171981702865) }},
  197. {{ SC_(-5.5), SC_(1e+04), SC_(0.007593311396019034252155600098309836289) }},
  198. {{ SC_(-5.5), SC_(1e+06), SC_(-0.0007474243882060190346457525218941411076) }},
  199. {{ SC_(-0.5), SC_(101.0), SC_(-0.036238035321276062532981494694583591262302408) }},
  200. {{ T(-10486074) / (1024*1024), T(1)/512, SC_(-7.0724447469115535625316241941528969321944e35) }},
  201. {{ T(-10486074) / (1024*1024), SC_(15.0), SC_(-0.15994088796049823354364759206656917967697690) }},
  202. {{ T(10486074) / (1024*1024), SC_(1e+02), SC_(-0.05778764167290516644655950658602424434253) }},
  203. {{ T(10486074) / (1024*1024), SC_(2e+04), SC_(-0.00091101010794789360775314125410690740803) }},
  204. {{ T(-10486074) / (1024*1024), SC_(1e+02), SC_(-0.057736130385111563671838499496767877709471701) }},
  205. {{ SC_(1.5), T(8034)/1024, SC_(0.2783550354042687982259490073096357) }},
  206. {{ SC_(8.5), boost::math::constants::pi<T>() * 4, SC_(-0.194590144622675911618596506265006877277074) }},
  207. {{ SC_(-8.5), boost::math::constants::pi<T>() * 4, SC_(-0.014516314554743677558496402742690038592728) }},
  208. }};
  209. do_test_cyl_bessel_j_prime<T>(jv_data, name, "Bessel J': Mathworld Data");
  210. static const boost::array<boost::array<T, 3>, 4> jv_large_data = {{
  211. #if LDBL_MAX_10_EXP > 308
  212. {{ SC_(-0.5), static_cast<T>(std::ldexp(0.5, -683)), SC_(-2.8687031947358902542073388638943588627056993e308) }},
  213. #else
  214. {{ SC_(-0.5), static_cast<T>(std::ldexp(0.5, -450)), SC_(-1.7688953183288445554095310240218576026580197125814e203) }},
  215. #endif
  216. {{ SC_(256.0), SC_(512.0), SC_(0.032286467266411904239327492993951594201583145) }},
  217. {{ SC_(-256.0), SC_(8.0), SC_(4.6974301387555891979202431551474684165419e-352) }},
  218. {{ SC_(-2.5), SC_(4.0), SC_(-0.3580070651681080294136741901878543615958139) }},
  219. }};
  220. if(jv_large_data[0][1] != 0)
  221. do_test_cyl_bessel_j_prime<T>(jv_large_data, name, "Bessel J': Mathworld Data (large values)");
  222. #include "bessel_j_prime_int_data.ipp"
  223. do_test_cyl_bessel_j_prime<T>(bessel_j_prime_int_data, name, "Bessel JN': Random Data");
  224. #include "bessel_j_prime_data.ipp"
  225. do_test_cyl_bessel_j_prime<T>(bessel_j_prime_data, name, "Bessel J': Random Data");
  226. #include "bessel_j_prime_large_data.ipp"
  227. do_test_cyl_bessel_j_prime<T>(bessel_j_prime_large_data, name, "Bessel J': Random Data (Tricky large values)");
  228. #include "sph_bessel_prime_data.ipp"
  229. do_test_sph_bessel_j_prime<T>(sph_bessel_prime_data, name, "Bessel j': Random Data");
  230. //
  231. // Some special cases:
  232. //
  233. BOOST_CHECK_EQUAL(boost::math::cyl_bessel_j_prime(T(1), T(0)), T(0.5));
  234. BOOST_CHECK_EQUAL(boost::math::cyl_bessel_j_prime(T(-1), T(0)), T(-0.5));
  235. BOOST_CHECK_EQUAL(boost::math::cyl_bessel_j_prime(T(2), T(0)), T(0));
  236. //
  237. // Special cases that are errors:
  238. //
  239. BOOST_MATH_CHECK_THROW(boost::math::sph_bessel_prime(1, T(0)), std::domain_error);
  240. BOOST_MATH_CHECK_THROW(boost::math::sph_bessel_prime(100000, T(0)), std::domain_error);
  241. BOOST_MATH_CHECK_THROW(boost::math::cyl_bessel_j_prime(T(-2.5), T(0)), std::domain_error);
  242. BOOST_MATH_CHECK_THROW(boost::math::cyl_bessel_j_prime(T(-2.5), T(-2)), std::domain_error);
  243. BOOST_MATH_CHECK_THROW(boost::math::cyl_bessel_j_prime(T(2.5), T(-2)), std::domain_error);
  244. }