hankel.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright John Maddock 2012.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_HANKEL_HPP
  7. #define BOOST_MATH_HANKEL_HPP
  8. #include <boost/math/special_functions/bessel.hpp>
  9. namespace boost{ namespace math{
  10. namespace detail{
  11. template <class T, class Policy>
  12. std::complex<T> hankel_imp(T v, T x, const bessel_no_int_tag&, const Policy& pol, int sign)
  13. {
  14. BOOST_MATH_STD_USING
  15. static const char* function = "boost::math::cyl_hankel_1<%1%>(%1%,%1%)";
  16. if(x < 0)
  17. {
  18. bool isint_v = floor(v) == v;
  19. T j, y;
  20. bessel_jy(v, -x, &j, &y, need_j | need_y, pol);
  21. std::complex<T> cx(x), cv(v);
  22. std::complex<T> j_result, y_result;
  23. if(isint_v)
  24. {
  25. int s = (iround(v) & 1) ? -1 : 1;
  26. j_result = j * s;
  27. y_result = T(s) * (y - (2 / constants::pi<T>()) * (log(-x) - log(cx)) * j);
  28. }
  29. else
  30. {
  31. j_result = pow(cx, v) * pow(-cx, -v) * j;
  32. T p1 = pow(-x, v);
  33. std::complex<T> p2 = pow(cx, v);
  34. y_result = p1 * y / p2
  35. + (p2 / p1 - p1 / p2) * j / tan(constants::pi<T>() * v);
  36. }
  37. // multiply y_result by i:
  38. y_result = std::complex<T>(-sign * y_result.imag(), sign * y_result.real());
  39. return j_result + y_result;
  40. }
  41. if(x == 0)
  42. {
  43. if(v == 0)
  44. {
  45. // J is 1, Y is -INF
  46. return std::complex<T>(1, sign * -policies::raise_overflow_error<T>(function, 0, pol));
  47. }
  48. else
  49. {
  50. // At least one of J and Y is complex infinity:
  51. return std::complex<T>(policies::raise_overflow_error<T>(function, 0, pol), sign * policies::raise_overflow_error<T>(function, 0, pol));
  52. }
  53. }
  54. T j, y;
  55. bessel_jy(v, x, &j, &y, need_j | need_y, pol);
  56. return std::complex<T>(j, sign * y);
  57. }
  58. template <class T, class Policy>
  59. std::complex<T> hankel_imp(int v, T x, const bessel_int_tag&, const Policy& pol, int sign);
  60. template <class T, class Policy>
  61. inline std::complex<T> hankel_imp(T v, T x, const bessel_maybe_int_tag&, const Policy& pol, int sign)
  62. {
  63. BOOST_MATH_STD_USING // ADL of std names.
  64. int ival = detail::iconv(v, pol);
  65. if(0 == v - ival)
  66. {
  67. return hankel_imp(ival, x, bessel_int_tag(), pol, sign);
  68. }
  69. return hankel_imp(v, x, bessel_no_int_tag(), pol, sign);
  70. }
  71. template <class T, class Policy>
  72. inline std::complex<T> hankel_imp(int v, T x, const bessel_int_tag&, const Policy& pol, int sign)
  73. {
  74. BOOST_MATH_STD_USING
  75. if((std::abs(v) < 200) && (x > 0))
  76. return std::complex<T>(bessel_jn(v, x, pol), sign * bessel_yn(v, x, pol));
  77. return hankel_imp(static_cast<T>(v), x, bessel_no_int_tag(), pol, sign);
  78. }
  79. template <class T, class Policy>
  80. inline std::complex<T> sph_hankel_imp(T v, T x, const Policy& pol, int sign)
  81. {
  82. BOOST_MATH_STD_USING
  83. return constants::root_half_pi<T>() * hankel_imp(v + 0.5f, x, bessel_no_int_tag(), pol, sign) / sqrt(std::complex<T>(x));
  84. }
  85. } // namespace detail
  86. template <class T1, class T2, class Policy>
  87. inline std::complex<typename detail::bessel_traits<T1, T2, Policy>::result_type> cyl_hankel_1(T1 v, T2 x, const Policy& pol)
  88. {
  89. BOOST_FPU_EXCEPTION_GUARD
  90. typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
  91. typedef typename detail::bessel_traits<T1, T2, Policy>::optimisation_tag tag_type;
  92. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  93. return policies::checked_narrowing_cast<std::complex<result_type>, Policy>(detail::hankel_imp<value_type>(v, static_cast<value_type>(x), tag_type(), pol, 1), "boost::math::cyl_hankel_1<%1%>(%1%,%1%)");
  94. }
  95. template <class T1, class T2>
  96. inline std::complex<typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type> cyl_hankel_1(T1 v, T2 x)
  97. {
  98. return cyl_hankel_1(v, x, policies::policy<>());
  99. }
  100. template <class T1, class T2, class Policy>
  101. inline std::complex<typename detail::bessel_traits<T1, T2, Policy>::result_type> cyl_hankel_2(T1 v, T2 x, const Policy& pol)
  102. {
  103. BOOST_FPU_EXCEPTION_GUARD
  104. typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
  105. typedef typename detail::bessel_traits<T1, T2, Policy>::optimisation_tag tag_type;
  106. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  107. return policies::checked_narrowing_cast<std::complex<result_type>, Policy>(detail::hankel_imp<value_type>(v, static_cast<value_type>(x), tag_type(), pol, -1), "boost::math::cyl_hankel_1<%1%>(%1%,%1%)");
  108. }
  109. template <class T1, class T2>
  110. inline std::complex<typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type> cyl_hankel_2(T1 v, T2 x)
  111. {
  112. return cyl_hankel_2(v, x, policies::policy<>());
  113. }
  114. template <class T1, class T2, class Policy>
  115. inline std::complex<typename detail::bessel_traits<T1, T2, Policy>::result_type> sph_hankel_1(T1 v, T2 x, const Policy&)
  116. {
  117. BOOST_FPU_EXCEPTION_GUARD
  118. typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
  119. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  120. typedef typename policies::normalise<
  121. Policy,
  122. policies::promote_float<false>,
  123. policies::promote_double<false>,
  124. policies::discrete_quantile<>,
  125. policies::assert_undefined<> >::type forwarding_policy;
  126. return policies::checked_narrowing_cast<std::complex<result_type>, Policy>(detail::sph_hankel_imp<value_type>(static_cast<value_type>(v), static_cast<value_type>(x), forwarding_policy(), 1), "boost::math::sph_hankel_1<%1%>(%1%,%1%)");
  127. }
  128. template <class T1, class T2>
  129. inline std::complex<typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type> sph_hankel_1(T1 v, T2 x)
  130. {
  131. return sph_hankel_1(v, x, policies::policy<>());
  132. }
  133. template <class T1, class T2, class Policy>
  134. inline std::complex<typename detail::bessel_traits<T1, T2, Policy>::result_type> sph_hankel_2(T1 v, T2 x, const Policy&)
  135. {
  136. BOOST_FPU_EXCEPTION_GUARD
  137. typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
  138. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  139. typedef typename policies::normalise<
  140. Policy,
  141. policies::promote_float<false>,
  142. policies::promote_double<false>,
  143. policies::discrete_quantile<>,
  144. policies::assert_undefined<> >::type forwarding_policy;
  145. return policies::checked_narrowing_cast<std::complex<result_type>, Policy>(detail::sph_hankel_imp<value_type>(static_cast<value_type>(v), static_cast<value_type>(x), forwarding_policy(), -1), "boost::math::sph_hankel_1<%1%>(%1%,%1%)");
  146. }
  147. template <class T1, class T2>
  148. inline std::complex<typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type> sph_hankel_2(T1 v, T2 x)
  149. {
  150. return sph_hankel_2(v, x, policies::policy<>());
  151. }
  152. }} // namespaces
  153. #endif // BOOST_MATH_HANKEL_HPP