jacobi_zeta.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/constants/constants.hpp>
  15. #include <boost/math/policies/error_handling.hpp>
  16. #include <boost/math/tools/workaround.hpp>
  17. // Elliptic integral the Jacobi Zeta function.
  18. namespace boost { namespace math {
  19. namespace detail{
  20. // Elliptic integral - Jacobi Zeta
  21. template <typename T, typename Policy>
  22. T jacobi_zeta_imp(T phi, T k, const Policy& pol)
  23. {
  24. BOOST_MATH_STD_USING
  25. using namespace boost::math::tools;
  26. using namespace boost::math::constants;
  27. bool invert = false;
  28. if(phi < 0)
  29. {
  30. phi = fabs(phi);
  31. invert = true;
  32. }
  33. T result;
  34. T sinp = sin(phi);
  35. T cosp = cos(phi);
  36. T s2 = sinp * sinp;
  37. T k2 = k * k;
  38. T kp = 1 - k2;
  39. if(k == 1)
  40. result = sinp * (boost::math::sign)(cosp); // We get here by simplifying JacobiZeta[w, 1] in Mathematica, and the fact that 0 <= phi.
  41. else
  42. 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));
  43. return invert ? T(-result) : result;
  44. }
  45. } // detail
  46. template <class T1, class T2, class Policy>
  47. inline typename tools::promote_args<T1, T2>::type jacobi_zeta(T1 k, T2 phi, const Policy& pol)
  48. {
  49. typedef typename tools::promote_args<T1, T2>::type result_type;
  50. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  51. 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%)");
  52. }
  53. template <class T1, class T2>
  54. inline typename tools::promote_args<T1, T2>::type jacobi_zeta(T1 k, T2 phi)
  55. {
  56. return boost::math::jacobi_zeta(k, phi, policies::policy<>());
  57. }
  58. }} // namespaces
  59. #endif // BOOST_MATH_ELLINT_D_HPP