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)
7 #ifndef BOOST_MATH_HANKEL_HPP
8 #define BOOST_MATH_HANKEL_HPP
10 #include <boost/math/special_functions/bessel.hpp>
12 namespace boost{ namespace math{
16 template <class T, class Policy>
17 std::complex<T> hankel_imp(T v, T x, const bessel_no_int_tag&, const Policy& pol, int sign)
20 static const char* function = "boost::math::cyl_hankel_1<%1%>(%1%,%1%)";
24 bool isint_v = floor(v) == v;
26 bessel_jy(v, -x, &j, &y, need_j | need_y, pol);
27 std::complex<T> cx(x), cv(v);
28 std::complex<T> j_result, y_result;
31 int s = (iround(v) & 1) ? -1 : 1;
33 y_result = T(s) * (y - (2 / constants::pi<T>()) * (log(-x) - log(cx)) * j);
37 j_result = pow(cx, v) * pow(-cx, -v) * j;
39 std::complex<T> p2 = pow(cx, v);
40 y_result = p1 * y / p2
41 + (p2 / p1 - p1 / p2) * j / tan(constants::pi<T>() * v);
43 // multiply y_result by i:
44 y_result = std::complex<T>(-sign * y_result.imag(), sign * y_result.real());
45 return j_result + y_result;
53 return std::complex<T>(1, sign * -policies::raise_overflow_error<T>(function, 0, pol));
57 // At least one of J and Y is complex infinity:
58 return std::complex<T>(policies::raise_overflow_error<T>(function, 0, pol), sign * policies::raise_overflow_error<T>(function, 0, pol));
63 bessel_jy(v, x, &j, &y, need_j | need_y, pol);
64 return std::complex<T>(j, sign * y);
67 template <class T, class Policy>
68 std::complex<T> hankel_imp(int v, T x, const bessel_int_tag&, const Policy& pol, int sign);
70 template <class T, class Policy>
71 inline std::complex<T> hankel_imp(T v, T x, const bessel_maybe_int_tag&, const Policy& pol, int sign)
73 BOOST_MATH_STD_USING // ADL of std names.
74 int ival = detail::iconv(v, pol);
77 return hankel_imp(ival, x, bessel_int_tag(), pol, sign);
79 return hankel_imp(v, x, bessel_no_int_tag(), pol, sign);
82 template <class T, class Policy>
83 inline std::complex<T> hankel_imp(int v, T x, const bessel_int_tag&, const Policy& pol, int sign)
86 if((std::abs(v) < 200) && (x > 0))
87 return std::complex<T>(bessel_jn(v, x, pol), sign * bessel_yn(v, x, pol));
88 return hankel_imp(static_cast<T>(v), x, bessel_no_int_tag(), pol, sign);
91 template <class T, class Policy>
92 inline std::complex<T> sph_hankel_imp(T v, T x, const Policy& pol, int sign)
95 return constants::root_half_pi<T>() * hankel_imp(v + 0.5f, x, bessel_no_int_tag(), pol, sign) / sqrt(std::complex<T>(x));
100 template <class T1, class T2, class Policy>
101 inline std::complex<typename detail::bessel_traits<T1, T2, Policy>::result_type> cyl_hankel_1(T1 v, T2 x, const Policy& pol)
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%)");
110 template <class T1, class T2>
111 inline std::complex<typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type> cyl_hankel_1(T1 v, T2 x)
113 return cyl_hankel_1(v, x, policies::policy<>());
116 template <class T1, class T2, class Policy>
117 inline std::complex<typename detail::bessel_traits<T1, T2, Policy>::result_type> cyl_hankel_2(T1 v, T2 x, const Policy& pol)
119 BOOST_FPU_EXCEPTION_GUARD
120 typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
121 typedef typename detail::bessel_traits<T1, T2, Policy>::optimisation_tag tag_type;
122 typedef typename policies::evaluation<result_type, Policy>::type value_type;
123 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%)");
126 template <class T1, class T2>
127 inline std::complex<typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type> cyl_hankel_2(T1 v, T2 x)
129 return cyl_hankel_2(v, x, policies::policy<>());
132 template <class T1, class T2, class Policy>
133 inline std::complex<typename detail::bessel_traits<T1, T2, Policy>::result_type> sph_hankel_1(T1 v, T2 x, const Policy&)
135 BOOST_FPU_EXCEPTION_GUARD
136 typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
137 typedef typename policies::evaluation<result_type, Policy>::type value_type;
138 typedef typename policies::normalise<
140 policies::promote_float<false>,
141 policies::promote_double<false>,
142 policies::discrete_quantile<>,
143 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%)");
148 template <class T1, class T2>
149 inline std::complex<typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type> sph_hankel_1(T1 v, T2 x)
151 return sph_hankel_1(v, x, policies::policy<>());
154 template <class T1, class T2, class Policy>
155 inline std::complex<typename detail::bessel_traits<T1, T2, Policy>::result_type> sph_hankel_2(T1 v, T2 x, const Policy&)
157 BOOST_FPU_EXCEPTION_GUARD
158 typedef typename detail::bessel_traits<T1, T2, Policy>::result_type result_type;
159 typedef typename policies::evaluation<result_type, Policy>::type value_type;
160 typedef typename policies::normalise<
162 policies::promote_float<false>,
163 policies::promote_double<false>,
164 policies::discrete_quantile<>,
165 policies::assert_undefined<> >::type forwarding_policy;
167 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%)");
170 template <class T1, class T2>
171 inline std::complex<typename detail::bessel_traits<T1, T2, policies::policy<> >::result_type> sph_hankel_2(T1 v, T2 x)
173 return sph_hankel_2(v, x, policies::policy<>());
178 #endif // BOOST_MATH_HANKEL_HPP