| 1 |
// Copyright (c) 2007 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 |
// |
| 7 |
// This is a partial header, do not include on it's own!!! |
| 8 |
// |
| 9 |
// Contains asymptotic expansions for Bessel J(v,x) and Y(v,x) |
| 10 |
// functions, as x -> INF. |
| 11 |
// |
| 12 |
#ifndef BOOST_MATH_SF_DETAIL_BESSEL_JY_ASYM_HPP |
| 13 |
#define BOOST_MATH_SF_DETAIL_BESSEL_JY_ASYM_HPP |
| 14 |
|
| 15 |
#ifdef _MSC_VER |
| 16 |
#pragma once |
| 17 |
#endif |
| 18 |
|
| 19 |
#include <boost/math/tools/config.hpp> |
| 20 |
#include <boost/math/constants/constants.hpp> |
| 21 |
#include <boost/math/special_functions/factorials.hpp> |
| 22 |
#include <boost/math/special_functions/fpclassify.hpp> |
| 23 |
|
| 24 |
namespace boost{ namespace math{ namespace detail{ |
| 25 |
|
| 26 |
template <class T> |
| 27 |
BOOST_MATH_GPU_ENABLED inline T asymptotic_bessel_amplitude(T v, T x) |
| 28 |
{ |
| 29 |
// Calculate the amplitude of J(v, x) and Y(v, x) for large |
| 30 |
// x: see A&S 9.2.28. |
| 31 |
BOOST_MATH_STD_USING |
| 32 |
T s = 1; |
| 33 |
T mu = 4 * v * v; |
| 34 |
T txq = 2 * x; |
| 35 |
txq *= txq; |
| 36 |
|
| 37 |
s += (mu - 1) / (2 * txq); |
| 38 |
s += 3 * (mu - 1) * (mu - 9) / (txq * txq * 8); |
| 39 |
s += 15 * (mu - 1) * (mu - 9) * (mu - 25) / (txq * txq * txq * 8 * 6); |
| 40 |
|
| 41 |
return sqrt(s * 2 / (constants::pi<T>() * x)); |
| 42 |
} |
| 43 |
|
| 44 |
template <class T> |
| 45 |
BOOST_MATH_GPU_ENABLED T asymptotic_bessel_phase_mx(T v, T x) |
| 46 |
{ |
| 47 |
// |
| 48 |
// Calculate the phase of J(v, x) and Y(v, x) for large x. |
| 49 |
// See A&S 9.2.29. |
| 50 |
// Note that the result returned is the phase less (x - PI(v/2 + 1/4)) |
| 51 |
// which we'll factor in later when we calculate the sines/cosines of the result: |
| 52 |
// |
| 53 |
T mu = 4 * v * v; |
| 54 |
T denom = 4 * x; |
| 55 |
T denom_mult = denom * denom; |
| 56 |
|
| 57 |
T s = 0; |
| 58 |
s += (mu - 1) / (2 * denom); |
| 59 |
denom *= denom_mult; |
| 60 |
s += (mu - 1) * (mu - 25) / (6 * denom); |
| 61 |
denom *= denom_mult; |
| 62 |
s += (mu - 1) * (mu * mu - 114 * mu + 1073) / (5 * denom); |
| 63 |
denom *= denom_mult; |
| 64 |
s += (mu - 1) * (5 * mu * mu * mu - 1535 * mu * mu + 54703 * mu - 375733) / (14 * denom); |
| 65 |
return s; |
| 66 |
} |
| 67 |
|
| 68 |
template <class T, class Policy> |
| 69 |
BOOST_MATH_GPU_ENABLED inline T asymptotic_bessel_y_large_x_2(T v, T x, const Policy& pol) |
| 70 |
{ |
| 71 |
// See A&S 9.2.19. |
| 72 |
BOOST_MATH_STD_USING |
| 73 |
// Get the phase and amplitude: |
| 74 |
T ampl = asymptotic_bessel_amplitude(v, x); |
| 75 |
if (0 == ampl) |
| 76 |
return ampl; |
| 77 |
T phase = asymptotic_bessel_phase_mx(v, x); |
| 78 |
BOOST_MATH_INSTRUMENT_VARIABLE(ampl); |
| 79 |
BOOST_MATH_INSTRUMENT_VARIABLE(phase); |
| 80 |
// |
| 81 |
// Calculate the sine of the phase, using |
| 82 |
// sine/cosine addition rules to factor in |
| 83 |
// the x - PI(v/2 + 1/4) term not added to the |
| 84 |
// phase when we calculated it. |
| 85 |
// |
| 86 |
T cx = cos(x); |
| 87 |
T sx = sin(x); |
| 88 |
T ci = boost::math::cos_pi(v / 2 + 0.25f, pol); |
| 89 |
T si = boost::math::sin_pi(v / 2 + 0.25f, pol); |
| 90 |
T sin_phase = sin(phase) * (cx * ci + sx * si) + cos(phase) * (sx * ci - cx * si); |
| 91 |
BOOST_MATH_INSTRUMENT_CODE(sin(phase)); |
| 92 |
BOOST_MATH_INSTRUMENT_CODE(cos(x)); |
| 93 |
BOOST_MATH_INSTRUMENT_CODE(cos(phase)); |
| 94 |
BOOST_MATH_INSTRUMENT_CODE(sin(x)); |
| 95 |
return sin_phase * ampl; |
| 96 |
} |
| 97 |
|
| 98 |
template <class T, class Policy> |
| 99 |
BOOST_MATH_GPU_ENABLED inline T asymptotic_bessel_j_large_x_2(T v, T x, const Policy& pol) |
| 100 |
{ |
| 101 |
// See A&S 9.2.19. |
| 102 |
BOOST_MATH_STD_USING |
| 103 |
// Get the phase and amplitude: |
| 104 |
T ampl = asymptotic_bessel_amplitude(v, x); |
| 105 |
if (0 == ampl) |
| 106 |
return ampl; // shortcut. |
| 107 |
T phase = asymptotic_bessel_phase_mx(v, x); |
| 108 |
BOOST_MATH_INSTRUMENT_VARIABLE(ampl); |
| 109 |
BOOST_MATH_INSTRUMENT_VARIABLE(phase); |
| 110 |
// |
| 111 |
// Calculate the sine of the phase, using |
| 112 |
// sine/cosine addition rules to factor in |
| 113 |
// the x - PI(v/2 + 1/4) term not added to the |
| 114 |
// phase when we calculated it. |
| 115 |
// |
| 116 |
BOOST_MATH_INSTRUMENT_CODE(cos(phase)); |
| 117 |
BOOST_MATH_INSTRUMENT_CODE(cos(x)); |
| 118 |
BOOST_MATH_INSTRUMENT_CODE(sin(phase)); |
| 119 |
BOOST_MATH_INSTRUMENT_CODE(sin(x)); |
| 120 |
T cx = cos(x); |
| 121 |
T sx = sin(x); |
| 122 |
T ci = boost::math::cos_pi(v / 2 + 0.25f, pol); |
| 123 |
T si = boost::math::sin_pi(v / 2 + 0.25f, pol); |
| 124 |
T sin_phase = cos(phase) * (cx * ci + sx * si) - sin(phase) * (sx * ci - cx * si); |
| 125 |
BOOST_MATH_INSTRUMENT_VARIABLE(sin_phase); |
| 126 |
return sin_phase * ampl; |
| 127 |
} |
| 128 |
|
| 129 |
template <class T> |
| 130 |
BOOST_MATH_GPU_ENABLED inline bool asymptotic_bessel_large_x_limit(int v, const T& x) |
| 131 |
{ |
| 132 |
BOOST_MATH_STD_USING |
| 133 |
// |
| 134 |
// Determines if x is large enough compared to v to take the asymptotic |
| 135 |
// forms above. From A&S 9.2.28 we require: |
| 136 |
// v < x * eps^1/8 |
| 137 |
// and from A&S 9.2.29 we require: |
| 138 |
// v^12/10 < 1.5 * x * eps^1/10 |
| 139 |
// using the former seems to work OK in practice with broadly similar |
| 140 |
// error rates either side of the divide for v < 10000. |
| 141 |
// At double precision eps^1/8 ~= 0.01. |
| 142 |
// |
| 143 |
BOOST_MATH_ASSERT(v >= 0); |
| 144 |
return (v ? v : 1) < x * 0.004f; |
| 145 |
} |
| 146 |
|
| 147 |
template <class T> |
| 148 |
BOOST_MATH_GPU_ENABLED inline bool asymptotic_bessel_large_x_limit(const T& v, const T& x) |
| 149 |
{ |
| 150 |
BOOST_MATH_STD_USING |
| 151 |
// |
| 152 |
// Determines if x is large enough compared to v to take the asymptotic |
| 153 |
// forms above. From A&S 9.2.28 we require: |
| 154 |
// v < x * eps^1/8 |
| 155 |
// and from A&S 9.2.29 we require: |
| 156 |
// v^12/10 < 1.5 * x * eps^1/10 |
| 157 |
// using the former seems to work OK in practice with broadly similar |
| 158 |
// error rates either side of the divide for v < 10000. |
| 159 |
// At double precision eps^1/8 ~= 0.01. |
| 160 |
// |
| 161 |
return BOOST_MATH_GPU_SAFE_MAX(T(fabs(v)), T(1)) < x * sqrt(tools::forth_root_epsilon<T>()); |
| 162 |
} |
| 163 |
|
| 164 |
template <class T, class Policy> |
| 165 |
BOOST_MATH_GPU_ENABLED void temme_asymptotic_y_small_x(T v, T x, T* Y, T* Y1, const Policy& pol) |
| 166 |
{ |
| 167 |
T c = 1; |
| 168 |
T p = (v / boost::math::sin_pi(v, pol)) * pow(x / 2, -v) / boost::math::tgamma(1 - v, pol); |
| 169 |
T q = (v / boost::math::sin_pi(v, pol)) * pow(x / 2, v) / boost::math::tgamma(1 + v, pol); |
| 170 |
T f = (p - q) / v; |
| 171 |
T g_prefix = boost::math::sin_pi(v / 2, pol); |
| 172 |
g_prefix *= g_prefix * 2 / v; |
| 173 |
T g = f + g_prefix * q; |
| 174 |
T h = p; |
| 175 |
T c_mult = -x * x / 4; |
| 176 |
|
| 177 |
T y(c * g), y1(c * h); |
| 178 |
|
| 179 |
for(int k = 1; k < policies::get_max_series_iterations<Policy>(); ++k) |
| 180 |
{ |
| 181 |
f = (k * f + p + q) / (k*k - v*v); |
| 182 |
p /= k - v; |
| 183 |
q /= k + v; |
| 184 |
c *= c_mult / k; |
| 185 |
T c1 = pow(-x * x / 4, T(k)) / factorial<T>(k, pol); |
| 186 |
g = f + g_prefix * q; |
| 187 |
h = -k * g + p; |
| 188 |
y += c * g; |
| 189 |
y1 += c * h; |
| 190 |
if(c * g / tools::epsilon<T>() < y) |
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
*Y = -y; |
| 195 |
*Y1 = (-2 / x) * y1; |
| 196 |
} |
| 197 |
|
| 198 |
template <class T, class Policy> |
| 199 |
BOOST_MATH_GPU_ENABLED T asymptotic_bessel_i_large_x(T v, T x, const Policy& pol) |
| 200 |
{ |
| 201 |
BOOST_MATH_STD_USING // ADL of std names |
| 202 |
T s = 1; |
| 203 |
T mu = 4 * v * v; |
| 204 |
T ex = 8 * x; |
| 205 |
T num = mu - 1; |
| 206 |
T denom = ex; |
| 207 |
|
| 208 |
s -= num / denom; |
| 209 |
|
| 210 |
num *= mu - 9; |
| 211 |
denom *= ex * 2; |
| 212 |
s += num / denom; |
| 213 |
|
| 214 |
num *= mu - 25; |
| 215 |
denom *= ex * 3; |
| 216 |
s -= num / denom; |
| 217 |
|
| 218 |
// Try and avoid overflow to the last minute: |
| 219 |
T e = exp(x/2); |
| 220 |
|
| 221 |
s = e * (e * s / sqrt(2 * x * constants::pi<T>())); |
| 222 |
|
| 223 |
return (boost::math::isfinite)(s) ? |
| 224 |
s : policies::raise_overflow_error<T>("boost::math::asymptotic_bessel_i_large_x<%1%>(%1%,%1%)", nullptr, pol); |
| 225 |
} |
| 226 |
|
| 227 |
}}} // namespaces |
| 228 |
|
| 229 |
#endif |
| 230 |
|