hankel.hpp 6.8 KB

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