test_hankel.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright John Maddock 2012
  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. #include <pch_light.hpp>
  6. #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
  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/concepts/real_concept.hpp>
  12. #include <boost/array.hpp>
  13. #include <boost/math/special_functions/hankel.hpp>
  14. #include <iostream>
  15. #include <iomanip>
  16. #ifdef _MSC_VER
  17. # pragma warning(disable : 4756 4127) // overflow in constant arithmetic
  18. // Constants are too big for float case, but this doesn't matter for test.
  19. #endif
  20. //
  21. // DESCRIPTION:
  22. // ~~~~~~~~~~~~
  23. //
  24. // This file tests the Hankel H1 and H2 functions.
  25. // These are basically just a few spot tests, since the underlying implementation
  26. // is the same as for the Bessel functions.
  27. //
  28. template <class T>
  29. void check_close(const std::complex<T>& a, const std::complex<T>& b)
  30. {
  31. T tol = boost::math::tools::epsilon<T>() * 3000;
  32. BOOST_CHECK_CLOSE_FRACTION(a.real(), b.real(), tol);
  33. BOOST_CHECK_CLOSE_FRACTION(a.imag(), b.imag(), tol);
  34. }
  35. template <class T>
  36. void test_hankel(T, const char* name)
  37. {
  38. std::cout << "Testing type " << name << std::endl;
  39. static const boost::array<boost::array<std::complex<T>, 4>, 16> data =
  40. {{
  41. // Values are v, z, J, and Y.
  42. // H1 and H2 are calculated from functions.wolfram.com.
  43. {{ 0, 1, static_cast<T>(0.765197686557966551449717526102663220909274289755325241861548L), static_cast<T>(0.0882569642156769579829267660235151628278175230906755467110438L) }},
  44. {{ 20, 15.5, static_cast<T>(0.0114685669049540880132573889173945340441319848974792913849131L), static_cast<T>(-2.23357703561803241011539677726233479433825678625142168545338L) }},
  45. {{ 202, 65, static_cast<T>(4.03123907894335908039069177339845754619082182624766366629474e-77L), static_cast<T>(-4.12853948338543234456930697819285129000267780751856135651604e73L) }},
  46. {{ 1.25, 2.25, static_cast<T>(0.548918751190427983177518806565451279306141559548962787802891L), static_cast<T>(-0.125900744882628421758627761371862848797979705547465002154794L) }},
  47. {{ -20, 15.5, static_cast<T>(0.0114685669049540880132573889173945340441319848974792913849131L), static_cast<T>(-2.23357703561803241011539677726233479433825678625142168545338L) }},
  48. {{ -20, -15.5, static_cast<T>(0.0114685669049540880132573889173945340441319848974792913849131L), std::complex<T>(static_cast<T>(-2.23357703561803241011539677726233479433825678625142168545338L), static_cast<T>(0.02293713380990817602651477783478906808826396979495858276983L))}},
  49. {{ 1.25, -1.5, std::complex<T>(static_cast<T>(-0.335713500965919366139805990226845134897000581426417882156072L), static_cast<T>(-0.335713500965919366139805990226845134897000581426417882156072L)), std::complex<T>(static_cast<T>(0.392747687664481978664420363126684555843062241632017696636768L), static_cast<T>(-1.064174689596320710944032343580374825637063404484853460948911L)) }},
  50. {{ -1.25, -1.5, std::complex<T>(static_cast<T>(0.515099846311769525023685454389374962206960751452481078650112L), static_cast<T>(-0.515099846311769525023685454389374962206960751452481078650112L)), std::complex<T>(static_cast<T>(-0.040329260174013212604062774398331485097569895404765001721544L), static_cast<T>(0.989870432449525837443308134380418439316351607500197155578680L)) }},
  51. {{ 4, 4, static_cast<T>(0.281129064961360106322277160229942806897088617059328870629222L), static_cast<T>(-0.488936768533842510615657398339913206218740182079627974737267L) }},
  52. {{ 4, -4, static_cast<T>(0.281129064961360106322277160229942806897088617059328870629222L), std::complex<T>(static_cast<T>(-0.488936768533842510615657398339913206218740182079627974737267L), static_cast<T>(0.562258129922720212644554320459885613794177234118657741258443L)) }},
  53. {{ -4, 4, static_cast<T>(0.281129064961360106322277160229942806897088617059328870629222L), static_cast<T>(-0.488936768533842510615657398339913206218740182079627974737267L) }},
  54. {{ -4, -4, static_cast<T>(0.281129064961360106322277160229942806897088617059328870629222), std::complex<T>(static_cast<T>(-0.488936768533842510615657398339913206218740182079627974737267), static_cast<T>(0.562258129922720212644554320459885613794177234118657741258443L)) }},
  55. {{ 3, 3, static_cast<T>(0.309062722255251643618260194946833149429135935993056794354475L), static_cast<T>(-0.538541616105031618004703905338594463807957863604859251481262L) }},
  56. {{ 3, -3, static_cast<T>(-0.309062722255251643618260194946833149429135935993056794354475L), std::complex<T>(static_cast<T>(0.538541616105031618004703905338594463807957863604859251481262L), static_cast<T>(-0.618125444510503287236520389893666298858271871986113588708949L)) }},
  57. {{ -3, 3, static_cast<T>(-0.309062722255251643618260194946833149429135935993056794354475L), static_cast<T>(0.538541616105031618004703905338594463807957863604859251481262L) }},
  58. {{ -3, -3, static_cast<T>(0.309062722255251643618260194946833149429135935993056794354475L), std::complex<T>(static_cast<T>(-0.538541616105031618004703905338594463807957863604859251481262L), static_cast<T>(0.618125444510503287236520389893666298858271871986113588708949L)) }},
  59. }};
  60. std::complex<T> im(0, 1);
  61. for(unsigned i = 0; i < data.size(); ++i)
  62. {
  63. if((i != 2) || (std::numeric_limits<T>::max_exponent10 > 80))
  64. {
  65. check_close(boost::math::cyl_hankel_1(data[i][0].real(), data[i][1].real()), data[i][2] + im * data[i][3]);
  66. check_close(boost::math::cyl_hankel_2(data[i][0].real(), data[i][1].real()), data[i][2] - im * data[i][3]);
  67. check_close(
  68. boost::math::cyl_hankel_1(data[i][0].real() + 0.5f, data[i][1].real()) * boost::math::constants::root_half_pi<T>() / sqrt(data[i][1]),
  69. boost::math::sph_hankel_1(data[i][0].real(), data[i][1].real()));
  70. check_close(
  71. boost::math::cyl_hankel_2(data[i][0].real() + 0.5f, data[i][1].real()) * boost::math::constants::root_half_pi<T>() / sqrt(data[i][1]),
  72. boost::math::sph_hankel_2(data[i][0].real(), data[i][1].real()));
  73. }
  74. }
  75. }
  76. //
  77. // Instantiate a few instances to check our error handling code can cope with std::complex:
  78. //
  79. typedef boost::math::policies::policy<
  80. boost::math::policies::overflow_error<boost::math::policies::throw_on_error>,
  81. boost::math::policies::denorm_error<boost::math::policies::throw_on_error>,
  82. boost::math::policies::underflow_error<boost::math::policies::throw_on_error>,
  83. boost::math::policies::domain_error<boost::math::policies::throw_on_error>,
  84. boost::math::policies::pole_error<boost::math::policies::throw_on_error>,
  85. boost::math::policies::rounding_error<boost::math::policies::throw_on_error>,
  86. boost::math::policies::evaluation_error<boost::math::policies::throw_on_error>,
  87. boost::math::policies::indeterminate_result_error<boost::math::policies::throw_on_error> > pol1;
  88. template std::complex<double> boost::math::cyl_hankel_1<double, double, pol1>(double, double, const pol1&);
  89. typedef boost::math::policies::policy<
  90. boost::math::policies::overflow_error<boost::math::policies::errno_on_error>,
  91. boost::math::policies::denorm_error<boost::math::policies::errno_on_error>,
  92. boost::math::policies::underflow_error<boost::math::policies::errno_on_error>,
  93. boost::math::policies::domain_error<boost::math::policies::errno_on_error>,
  94. boost::math::policies::pole_error<boost::math::policies::errno_on_error>,
  95. boost::math::policies::rounding_error<boost::math::policies::errno_on_error>,
  96. boost::math::policies::evaluation_error<boost::math::policies::errno_on_error>,
  97. boost::math::policies::indeterminate_result_error<boost::math::policies::errno_on_error> > pol2;
  98. template std::complex<double> boost::math::cyl_hankel_1<double, double, pol2>(double, double, const pol2&);
  99. typedef boost::math::policies::policy<
  100. boost::math::policies::overflow_error<boost::math::policies::ignore_error>,
  101. boost::math::policies::denorm_error<boost::math::policies::ignore_error>,
  102. boost::math::policies::underflow_error<boost::math::policies::ignore_error>,
  103. boost::math::policies::domain_error<boost::math::policies::ignore_error>,
  104. boost::math::policies::pole_error<boost::math::policies::ignore_error>,
  105. boost::math::policies::rounding_error<boost::math::policies::ignore_error>,
  106. boost::math::policies::evaluation_error<boost::math::policies::ignore_error>,
  107. boost::math::policies::indeterminate_result_error<boost::math::policies::ignore_error> > pol3;
  108. template std::complex<double> boost::math::cyl_hankel_1<double, double, pol3>(double, double, const pol3&);
  109. BOOST_AUTO_TEST_CASE( test_main )
  110. {
  111. #ifdef TEST_GSL
  112. gsl_set_error_handler_off();
  113. #endif
  114. BOOST_MATH_CONTROL_FP;
  115. #ifndef BOOST_MATH_BUGGY_LARGE_FLOAT_CONSTANTS
  116. test_hankel(0.1F, "float");
  117. #endif
  118. test_hankel(0.1, "double");
  119. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  120. test_hankel(0.1L, "long double");
  121. #else
  122. std::cout << "<note>The long double tests have been disabled on this platform "
  123. "either because the long double overloads of the usual math functions are "
  124. "not available at all, or because they are too inaccurate for these tests "
  125. "to pass.</note>" << std::endl;
  126. #endif
  127. }