114 lines · plain
1// Copyright (c) 2006 Xiaogang Zhang2// Use, modification and distribution are subject to the3// Boost Software License, Version 1.0. (See accompanying file4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_BESSEL_YN_HPP7#define BOOST_MATH_BESSEL_YN_HPP8 9#ifdef _MSC_VER10#pragma once11#endif12 13#include <boost/math/tools/config.hpp>14#include <boost/math/special_functions/detail/bessel_y0.hpp>15#include <boost/math/special_functions/detail/bessel_y1.hpp>16#include <boost/math/special_functions/detail/bessel_jy_series.hpp>17#include <boost/math/special_functions/sign.hpp>18#include <boost/math/policies/error_handling.hpp>19 20// Bessel function of the second kind of integer order21// Y_n(z) is the dominant solution, forward recurrence always OK (though unstable)22 23namespace boost { namespace math { namespace detail{24 25template <typename T, typename Policy>26BOOST_MATH_GPU_ENABLED T bessel_yn(int n, T x, const Policy& pol)27{28 BOOST_MATH_STD_USING29 T value, factor, current, prev;30 31 using namespace boost::math::tools;32 33 constexpr auto function = "boost::math::bessel_yn<%1%>(%1%,%1%)";34 35 if ((x == 0) && (n == 0))36 {37 return -policies::raise_overflow_error<T>(function, nullptr, pol);38 }39 if (x <= 0)40 {41 return policies::raise_domain_error<T>(function, "Got x = %1%, but x must be > 0, complex result not supported.", x, pol);42 }43 44 //45 // Reflection comes first:46 //47 if (n < 0)48 {49 factor = static_cast<T>((n & 0x1) ? -1 : 1); // Y_{-n}(z) = (-1)^n Y_n(z)50 n = -n;51 }52 else53 {54 factor = 1;55 }56 if(x < policies::get_epsilon<T, Policy>())57 {58 T scale = 1;59 value = bessel_yn_small_z(n, x, &scale, pol);60 if (tools::max_value<T>() * fabs(scale) < fabs(value))61 return boost::math::sign(scale) * boost::math::sign(value) * policies::raise_overflow_error<T>(function, nullptr, pol);62 value = (factor * value) / scale;63 }64 else if(asymptotic_bessel_large_x_limit(n, x))65 {66 value = factor * asymptotic_bessel_y_large_x_2(static_cast<T>(abs(n)), x, pol);67 }68 else if (n == 0)69 {70 value = bessel_y0(x, pol);71 }72 else if (n == 1)73 {74 value = factor * bessel_y1(x, pol);75 }76 else77 {78 prev = bessel_y0(x, pol);79 current = bessel_y1(x, pol);80 int k = 1;81 BOOST_MATH_ASSERT(k < n);82 policies::check_series_iterations<T>("boost::math::bessel_y_n<%1%>(%1%,%1%)", n, pol);83 T mult = 2 * k / x;84 value = mult * current - prev;85 prev = current;86 current = value;87 ++k;88 if((mult > 1) && (fabs(current) > 1))89 {90 prev /= current;91 factor /= current;92 value /= current;93 current = 1;94 }95 while(k < n)96 {97 mult = 2 * k / x;98 value = mult * current - prev;99 prev = current;100 current = value;101 ++k;102 }103 if (fabs(tools::max_value<T>() * factor) < fabs(value))104 return sign(value) * sign(factor) * policies::raise_overflow_error<T>(function, nullptr, pol);105 value /= factor;106 }107 return value;108}109 110}}} // namespaces111 112#endif // BOOST_MATH_BESSEL_YN_HPP113 114