2 // (C) Copyright John Maddock 2006.
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_MATH_SPECIAL_SPHERICAL_HARMONIC_HPP
8 #define BOOST_MATH_SPECIAL_SPHERICAL_HARMONIC_HPP
14 #include <boost/math/special_functions/math_fwd.hpp>
15 #include <boost/math/special_functions/legendre.hpp>
16 #include <boost/math/tools/workaround.hpp>
25 // Calculates the prefix term that's common to the real
26 // and imaginary parts. Does *not* fix up the sign of the result
29 template <class T, class Policy>
30 inline T spherical_harmonic_prefix(unsigned n, unsigned m, T theta, const Policy& pol)
37 T sin_theta = sin(theta);
40 T leg = detail::legendre_p_imp(n, m, x, static_cast<T>(pow(fabs(sin_theta), T(m))), pol);
42 T prefix = boost::math::tgamma_delta_ratio(static_cast<T>(n - m + 1), static_cast<T>(2 * m), pol);
43 prefix *= (2 * n + 1) / (4 * constants::pi<T>());
44 prefix = sqrt(prefix);
50 template <class T, class Policy>
51 T spherical_harmonic_r(unsigned n, int m, T theta, T phi, const Policy& pol)
53 BOOST_MATH_STD_USING // ADL of std functions
58 // Reflect and adjust sign if m < 0:
64 // Check phase if theta is outside [0, PI]:
65 T mod = boost::math::tools::fmod_workaround(theta, T(2 * constants::pi<T>()));
67 mod += 2 * constants::pi<T>();
68 if(mod > constants::pi<T>())
71 // Get the value and adjust sign as required:
72 T prefix = spherical_harmonic_prefix(n, m, theta, pol);
73 prefix *= cos(m * phi);
74 return sign ? T(-prefix) : prefix;
77 template <class T, class Policy>
78 T spherical_harmonic_i(unsigned n, int m, T theta, T phi, const Policy& pol)
80 BOOST_MATH_STD_USING // ADL of std functions
85 // Reflect and adjust sign if m < 0:
91 // Check phase if theta is outside [0, PI]:
92 T mod = boost::math::tools::fmod_workaround(theta, T(2 * constants::pi<T>()));
94 mod += 2 * constants::pi<T>();
95 if(mod > constants::pi<T>())
98 // Get the value and adjust sign as required:
99 T prefix = spherical_harmonic_prefix(n, m, theta, pol);
100 prefix *= sin(m * phi);
101 return sign ? T(-prefix) : prefix;
104 template <class T, class U, class Policy>
105 std::complex<T> spherical_harmonic(unsigned n, int m, U theta, U phi, const Policy& pol)
109 // Sort out the signs:
115 // Reflect and adjust sign if m < 0:
122 // Check phase if theta is outside [0, PI]:
123 U mod = boost::math::tools::fmod_workaround(theta, U(2 * constants::pi<U>()));
125 mod += 2 * constants::pi<U>();
126 if(mod > constants::pi<U>())
133 // Calculate the value:
135 U prefix = spherical_harmonic_prefix(n, m, theta, pol);
136 U r = prefix * cos(m * phi);
137 U i = prefix * sin(m * phi);
145 static const char* function = "boost::math::spherical_harmonic<%1%>(int, int, %1%, %1%)";
146 return std::complex<T>(policies::checked_narrowing_cast<T, Policy>(r, function), policies::checked_narrowing_cast<T, Policy>(i, function));
149 } // namespace detail
151 template <class T1, class T2, class Policy>
152 inline std::complex<typename tools::promote_args<T1, T2>::type>
153 spherical_harmonic(unsigned n, int m, T1 theta, T2 phi, const Policy& pol)
155 typedef typename tools::promote_args<T1, T2>::type result_type;
156 typedef typename policies::evaluation<result_type, Policy>::type value_type;
157 return detail::spherical_harmonic<result_type, value_type>(n, m, static_cast<value_type>(theta), static_cast<value_type>(phi), pol);
160 template <class T1, class T2>
161 inline std::complex<typename tools::promote_args<T1, T2>::type>
162 spherical_harmonic(unsigned n, int m, T1 theta, T2 phi)
164 return boost::math::spherical_harmonic(n, m, theta, phi, policies::policy<>());
167 template <class T1, class T2, class Policy>
168 inline typename tools::promote_args<T1, T2>::type
169 spherical_harmonic_r(unsigned n, int m, T1 theta, T2 phi, const Policy& pol)
171 typedef typename tools::promote_args<T1, T2>::type result_type;
172 typedef typename policies::evaluation<result_type, Policy>::type value_type;
173 return policies::checked_narrowing_cast<result_type, Policy>(detail::spherical_harmonic_r(n, m, static_cast<value_type>(theta), static_cast<value_type>(phi), pol), "bost::math::spherical_harmonic_r<%1%>(unsigned, int, %1%, %1%)");
176 template <class T1, class T2>
177 inline typename tools::promote_args<T1, T2>::type
178 spherical_harmonic_r(unsigned n, int m, T1 theta, T2 phi)
180 return boost::math::spherical_harmonic_r(n, m, theta, phi, policies::policy<>());
183 template <class T1, class T2, class Policy>
184 inline typename tools::promote_args<T1, T2>::type
185 spherical_harmonic_i(unsigned n, int m, T1 theta, T2 phi, const Policy& pol)
187 typedef typename tools::promote_args<T1, T2>::type result_type;
188 typedef typename policies::evaluation<result_type, Policy>::type value_type;
189 return policies::checked_narrowing_cast<result_type, Policy>(detail::spherical_harmonic_i(n, m, static_cast<value_type>(theta), static_cast<value_type>(phi), pol), "boost::math::spherical_harmonic_i<%1%>(unsigned, int, %1%, %1%)");
192 template <class T1, class T2>
193 inline typename tools::promote_args<T1, T2>::type
194 spherical_harmonic_i(unsigned n, int m, T1 theta, T2 phi)
196 return boost::math::spherical_harmonic_i(n, m, theta, phi, policies::policy<>());
202 #endif // BOOST_MATH_SPECIAL_SPHERICAL_HARMONIC_HPP