hankel.hpp 7.6 KB

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