jacobi_zeta.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) 2015 John Maddock
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. #ifndef BOOST_MATH_ELLINT_JZ_HPP
  7. #define BOOST_MATH_ELLINT_JZ_HPP
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #endif
  11. #include <boost/math/special_functions/math_fwd.hpp>
  12. #include <boost/math/special_functions/ellint_1.hpp>
  13. #include <boost/math/special_functions/ellint_rj.hpp>
  14. #include <boost/math/special_functions/sign.hpp>
  15. #include <boost/math/constants/constants.hpp>
  16. #include <boost/math/policies/error_handling.hpp>
  17. #include <boost/math/tools/workaround.hpp>
  18. // Elliptic integral the Jacobi Zeta function.
  19. namespace boost { namespace math {
  20. namespace detail{
  21. // Elliptic integral - Jacobi Zeta
  22. template <typename T, typename Policy>
  23. T jacobi_zeta_imp(T phi, T k, const Policy& pol)
  24. {
  25. BOOST_MATH_STD_USING
  26. using namespace boost::math::tools;
  27. using namespace boost::math::constants;
  28. bool invert = false;
  29. if(phi < 0)
  30. {
  31. phi = fabs(phi);
  32. invert = true;
  33. }
  34. T result;
  35. T sinp = sin(phi);
  36. T cosp = cos(phi);
  37. T s2 = sinp * sinp;
  38. T k2 = k * k;
  39. T kp = 1 - k2;
  40. if(k == 1)
  41. result = sinp * (boost::math::sign)(cosp); // We get here by simplifying JacobiZeta[w, 1] in Mathematica, and the fact that 0 <= phi.
  42. else
  43. result = k2 * sinp * cosp * sqrt(1 - k2 * s2) * ellint_rj_imp(T(0), kp, T(1), T(1 - k2 * s2), pol) / (3 * ellint_k_imp(k, pol));
  44. return invert ? T(-result) : result;
  45. }
  46. } // detail
  47. template <class T1, class T2, class Policy>
  48. inline typename tools::promote_args<T1, T2>::type jacobi_zeta(T1 k, T2 phi, const Policy& pol)
  49. {
  50. typedef typename tools::promote_args<T1, T2>::type result_type;
  51. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  52. return policies::checked_narrowing_cast<result_type, Policy>(detail::jacobi_zeta_imp(static_cast<value_type>(phi), static_cast<value_type>(k), pol), "boost::math::jacobi_zeta<%1%>(%1%,%1%)");
  53. }
  54. template <class T1, class T2>
  55. inline typename tools::promote_args<T1, T2>::type jacobi_zeta(T1 k, T2 phi)
  56. {
  57. return boost::math::jacobi_zeta(k, phi, policies::policy<>());
  58. }
  59. }} // namespaces
  60. #endif // BOOST_MATH_ELLINT_D_HPP