bessel_yn.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 = (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 (n == 0)
  54. {
  55. value = bessel_y0(x, pol);
  56. }
  57. else if (n == 1)
  58. {
  59. value = factor * bessel_y1(x, pol);
  60. }
  61. else
  62. {
  63. prev = bessel_y0(x, pol);
  64. current = bessel_y1(x, pol);
  65. int k = 1;
  66. BOOST_ASSERT(k < n);
  67. policies::check_series_iterations<T>("boost::math::bessel_y_n<%1%>(%1%,%1%)", n, pol);
  68. do
  69. {
  70. T fact = 2 * k / x;
  71. if((fact > 1) && ((tools::max_value<T>() - fabs(prev)) / fact < fabs(current)))
  72. {
  73. prev /= current;
  74. factor /= current;
  75. current = 1;
  76. }
  77. value = fact * current - prev;
  78. prev = current;
  79. current = value;
  80. ++k;
  81. }
  82. while(k < n);
  83. if(fabs(tools::max_value<T>() * factor) < fabs(value))
  84. return sign(value) * sign(value) * policies::raise_overflow_error<T>(function, 0, pol);
  85. value /= factor;
  86. }
  87. return value;
  88. }
  89. }}} // namespaces
  90. #endif // BOOST_MATH_BESSEL_YN_HPP