bessel_yn.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2006 Xiaogang Zhang
  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. #ifndef BOOST_MATH_BESSEL_YN_HPP
  6. #define BOOST_MATH_BESSEL_YN_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/detail/bessel_y0.hpp>
  11. #include <boost/math/special_functions/detail/bessel_y1.hpp>
  12. #include <boost/math/special_functions/detail/bessel_jy_series.hpp>
  13. #include <boost/math/policies/error_handling.hpp>
  14. // Bessel function of the second kind of integer order
  15. // Y_n(z) is the dominant solution, forward recurrence always OK (though unstable)
  16. namespace boost { namespace math { namespace detail{
  17. template <typename T, typename Policy>
  18. T bessel_yn(int n, T x, const Policy& pol)
  19. {
  20. BOOST_MATH_STD_USING
  21. T value, factor, current, prev;
  22. using namespace boost::math::tools;
  23. static const char* function = "boost::math::bessel_yn<%1%>(%1%,%1%)";
  24. if ((x == 0) && (n == 0))
  25. {
  26. return -policies::raise_overflow_error<T>(function, 0, pol);
  27. }
  28. if (x <= 0)
  29. {
  30. return policies::raise_domain_error<T>(function,
  31. "Got x = %1%, but x must be > 0, complex result not supported.", x, pol);
  32. }
  33. //
  34. // Reflection comes first:
  35. //
  36. if (n < 0)
  37. {
  38. factor = static_cast<T>((n & 0x1) ? -1 : 1); // Y_{-n}(z) = (-1)^n Y_n(z)
  39. n = -n;
  40. }
  41. else
  42. {
  43. factor = 1;
  44. }
  45. if(x < policies::get_epsilon<T, Policy>())
  46. {
  47. T scale = 1;
  48. value = bessel_yn_small_z(n, x, &scale, pol);
  49. if(tools::max_value<T>() * fabs(scale) < fabs(value))
  50. return boost::math::sign(scale) * boost::math::sign(value) * policies::raise_overflow_error<T>(function, 0, pol);
  51. value /= scale;
  52. }
  53. else if(asymptotic_bessel_large_x_limit(n, x))
  54. {
  55. value = factor * asymptotic_bessel_y_large_x_2(static_cast<T>(abs(n)), x);
  56. }
  57. else if (n == 0)
  58. {
  59. value = bessel_y0(x, pol);
  60. }
  61. else if (n == 1)
  62. {
  63. value = factor * bessel_y1(x, pol);
  64. }
  65. else
  66. {
  67. prev = bessel_y0(x, pol);
  68. current = bessel_y1(x, pol);
  69. int k = 1;
  70. BOOST_ASSERT(k < n);
  71. policies::check_series_iterations<T>("boost::math::bessel_y_n<%1%>(%1%,%1%)", n, pol);
  72. T mult = 2 * k / x;
  73. value = mult * current - prev;
  74. prev = current;
  75. current = value;
  76. ++k;
  77. if((mult > 1) && (fabs(current) > 1))
  78. {
  79. prev /= current;
  80. factor /= current;
  81. value /= current;
  82. current = 1;
  83. }
  84. while(k < n)
  85. {
  86. mult = 2 * k / x;
  87. value = mult * current - prev;
  88. prev = current;
  89. current = value;
  90. ++k;
  91. }
  92. if(fabs(tools::max_value<T>() * factor) < fabs(value))
  93. return sign(value) * sign(factor) * policies::raise_overflow_error<T>(function, 0, pol);
  94. value /= factor;
  95. }
  96. return value;
  97. }
  98. }}} // namespaces
  99. #endif // BOOST_MATH_BESSEL_YN_HPP