bessel_yn.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, nullptr, pol);
  27. }
  28. if (x <= 0)
  29. {
  30. return policies::raise_domain_error<T>(function, "Got x = %1%, but x must be > 0, complex result not supported.", x, pol);
  31. }
  32. //
  33. // Reflection comes first:
  34. //
  35. if (n < 0)
  36. {
  37. factor = static_cast<T>((n & 0x1) ? -1 : 1); // Y_{-n}(z) = (-1)^n Y_n(z)
  38. n = -n;
  39. }
  40. else
  41. {
  42. factor = 1;
  43. }
  44. if(x < policies::get_epsilon<T, Policy>())
  45. {
  46. T scale = 1;
  47. value = bessel_yn_small_z(n, x, &scale, pol);
  48. if (tools::max_value<T>() * fabs(scale) < fabs(value))
  49. return boost::math::sign(scale) * boost::math::sign(value) * policies::raise_overflow_error<T>(function, nullptr, pol);
  50. value /= scale;
  51. }
  52. else if(asymptotic_bessel_large_x_limit(n, x))
  53. {
  54. value = factor * asymptotic_bessel_y_large_x_2(static_cast<T>(abs(n)), x, pol);
  55. }
  56. else if (n == 0)
  57. {
  58. value = bessel_y0(x, pol);
  59. }
  60. else if (n == 1)
  61. {
  62. value = factor * bessel_y1(x, pol);
  63. }
  64. else
  65. {
  66. prev = bessel_y0(x, pol);
  67. current = bessel_y1(x, pol);
  68. int k = 1;
  69. BOOST_MATH_ASSERT(k < n);
  70. policies::check_series_iterations<T>("boost::math::bessel_y_n<%1%>(%1%,%1%)", n, pol);
  71. T mult = 2 * k / x;
  72. value = mult * current - prev;
  73. prev = current;
  74. current = value;
  75. ++k;
  76. if((mult > 1) && (fabs(current) > 1))
  77. {
  78. prev /= current;
  79. factor /= current;
  80. value /= current;
  81. current = 1;
  82. }
  83. while(k < n)
  84. {
  85. mult = 2 * k / x;
  86. value = mult * current - prev;
  87. prev = current;
  88. current = value;
  89. ++k;
  90. }
  91. if (fabs(tools::max_value<T>() * factor) < fabs(value))
  92. return sign(value) * sign(factor) * policies::raise_overflow_error<T>(function, nullptr, pol);
  93. value /= factor;
  94. }
  95. return value;
  96. }
  97. }}} // namespaces
  98. #endif // BOOST_MATH_BESSEL_YN_HPP