| 1 |
// Copyright (c) 2011 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 |
#ifndef BOOST_MATH_BESSEL_JN_SERIES_HPP |
| 7 |
#define BOOST_MATH_BESSEL_JN_SERIES_HPP |
| 8 |
|
| 9 |
#ifdef _MSC_VER |
| 10 |
#pragma once |
| 11 |
#endif |
| 12 |
|
| 13 |
#include <boost/math/tools/config.hpp> |
| 14 |
#include <boost/math/tools/assert.hpp> |
| 15 |
#include <boost/math/tools/cstdint.hpp> |
| 16 |
|
| 17 |
namespace boost { namespace math { namespace detail{ |
| 18 |
|
| 19 |
template <class T, class Policy> |
| 20 |
struct bessel_j_small_z_series_term |
| 21 |
{ |
| 22 |
typedef T result_type; |
| 23 |
|
| 24 |
BOOST_MATH_GPU_ENABLED bessel_j_small_z_series_term(T v_, T x) |
| 25 |
: N(0), v(v_) |
| 26 |
{ |
| 27 |
BOOST_MATH_STD_USING |
| 28 |
mult = x / 2; |
| 29 |
mult *= -mult; |
| 30 |
term = 1; |
| 31 |
} |
| 32 |
BOOST_MATH_GPU_ENABLED T operator()() |
| 33 |
{ |
| 34 |
T r = term; |
| 35 |
++N; |
| 36 |
term *= mult / (N * (N + v)); |
| 37 |
return r; |
| 38 |
} |
| 39 |
private: |
| 40 |
unsigned N; |
| 41 |
T v; |
| 42 |
T mult; |
| 43 |
T term; |
| 44 |
}; |
| 45 |
// |
| 46 |
// Series evaluation for BesselJ(v, z) as z -> 0. |
| 47 |
// See http://functions.wolfram.com/Bessel-TypeFunctions/BesselJ/06/01/04/01/01/0003/ |
| 48 |
// Converges rapidly for all z << v. |
| 49 |
// |
| 50 |
template <class T, class Policy> |
| 51 |
BOOST_MATH_GPU_ENABLED inline T bessel_j_small_z_series(T v, T x, const Policy& pol) |
| 52 |
{ |
| 53 |
BOOST_MATH_STD_USING |
| 54 |
T prefix; |
| 55 |
if(v < max_factorial<T>::value) |
| 56 |
{ |
| 57 |
prefix = pow(x / 2, v) / boost::math::tgamma(v+1, pol); |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
prefix = v * log(x / 2) - boost::math::lgamma(v+1, pol); |
| 62 |
prefix = exp(prefix); |
| 63 |
} |
| 64 |
if(0 == prefix) |
| 65 |
return prefix; |
| 66 |
|
| 67 |
bessel_j_small_z_series_term<T, Policy> s(v, x); |
| 68 |
boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>(); |
| 69 |
|
| 70 |
T result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter); |
| 71 |
|
| 72 |
policies::check_series_iterations<T>("boost::math::bessel_j_small_z_series<%1%>(%1%,%1%)", max_iter, pol); |
| 73 |
return prefix * result; |
| 74 |
} |
| 75 |
|
| 76 |
template <class T, class Policy> |
| 77 |
struct bessel_y_small_z_series_term_a |
| 78 |
{ |
| 79 |
typedef T result_type; |
| 80 |
|
| 81 |
BOOST_MATH_GPU_ENABLED bessel_y_small_z_series_term_a(T v_, T x) |
| 82 |
: N(0), v(v_) |
| 83 |
{ |
| 84 |
BOOST_MATH_STD_USING |
| 85 |
mult = x / 2; |
| 86 |
mult *= -mult; |
| 87 |
term = 1; |
| 88 |
} |
| 89 |
BOOST_MATH_GPU_ENABLED T operator()() |
| 90 |
{ |
| 91 |
BOOST_MATH_STD_USING |
| 92 |
T r = term; |
| 93 |
++N; |
| 94 |
term *= mult / (N * (N - v)); |
| 95 |
return r; |
| 96 |
} |
| 97 |
private: |
| 98 |
unsigned N; |
| 99 |
T v; |
| 100 |
T mult; |
| 101 |
T term; |
| 102 |
}; |
| 103 |
|
| 104 |
template <class T, class Policy> |
| 105 |
struct bessel_y_small_z_series_term_b |
| 106 |
{ |
| 107 |
typedef T result_type; |
| 108 |
|
| 109 |
BOOST_MATH_GPU_ENABLED bessel_y_small_z_series_term_b(T v_, T x) |
| 110 |
: N(0), v(v_) |
| 111 |
{ |
| 112 |
BOOST_MATH_STD_USING |
| 113 |
mult = x / 2; |
| 114 |
mult *= -mult; |
| 115 |
term = 1; |
| 116 |
} |
| 117 |
BOOST_MATH_GPU_ENABLED T operator()() |
| 118 |
{ |
| 119 |
T r = term; |
| 120 |
++N; |
| 121 |
term *= mult / (N * (N + v)); |
| 122 |
return r; |
| 123 |
} |
| 124 |
private: |
| 125 |
unsigned N; |
| 126 |
T v; |
| 127 |
T mult; |
| 128 |
T term; |
| 129 |
}; |
| 130 |
// |
| 131 |
// Series form for BesselY as z -> 0, |
| 132 |
// see: http://functions.wolfram.com/Bessel-TypeFunctions/BesselY/06/01/04/01/01/0003/ |
| 133 |
// This series is only useful when the second term is small compared to the first |
| 134 |
// otherwise we get catastrophic cancellation errors. |
| 135 |
// |
| 136 |
// Approximating tgamma(v) by v^v, and assuming |tgamma(-z)| < eps we end up requiring: |
| 137 |
// eps/2 * v^v(x/2)^-v > (x/2)^v or log(eps/2) > v log((x/2)^2/v) |
| 138 |
// |
| 139 |
template <class T, class Policy> |
| 140 |
BOOST_MATH_GPU_ENABLED inline T bessel_y_small_z_series(T v, T x, T* pscale, const Policy& pol) |
| 141 |
{ |
| 142 |
BOOST_MATH_STD_USING |
| 143 |
constexpr auto function = "bessel_y_small_z_series<%1%>(%1%,%1%)"; |
| 144 |
T prefix; |
| 145 |
T gam; |
| 146 |
T p = log(x / 2); |
| 147 |
T scale = 1; |
| 148 |
bool need_logs = (v >= max_factorial<T>::value) || (tools::log_max_value<T>() / v < fabs(p)); |
| 149 |
|
| 150 |
if(!need_logs) |
| 151 |
{ |
| 152 |
gam = boost::math::tgamma(v, pol); |
| 153 |
p = pow(x / 2, v); |
| 154 |
if(tools::max_value<T>() * p < gam) |
| 155 |
{ |
| 156 |
scale /= gam; |
| 157 |
gam = 1; |
| 158 |
/* |
| 159 |
* We can never get here, it would require p < 1/max_value. |
| 160 |
if(tools::max_value<T>() * p < gam) |
| 161 |
{ |
| 162 |
return -policies::raise_overflow_error<T>(function, nullptr, pol); |
| 163 |
} |
| 164 |
*/ |
| 165 |
} |
| 166 |
prefix = -gam / (constants::pi<T>() * p); |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
gam = boost::math::lgamma(v, pol); |
| 171 |
p = v * p; |
| 172 |
prefix = gam - log(constants::pi<T>()) - p; |
| 173 |
if(tools::log_max_value<T>() < prefix) |
| 174 |
{ |
| 175 |
prefix -= log(tools::max_value<T>() / 4); |
| 176 |
scale /= (tools::max_value<T>() / 4); |
| 177 |
if(tools::log_max_value<T>() < prefix) |
| 178 |
{ |
| 179 |
return -policies::raise_overflow_error<T>(function, nullptr, pol); |
| 180 |
} |
| 181 |
} |
| 182 |
prefix = -exp(prefix); |
| 183 |
} |
| 184 |
bessel_y_small_z_series_term_a<T, Policy> s(v, x); |
| 185 |
boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>(); |
| 186 |
*pscale = scale; |
| 187 |
|
| 188 |
T result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter); |
| 189 |
|
| 190 |
policies::check_series_iterations<T>("boost::math::bessel_y_small_z_series<%1%>(%1%,%1%)", max_iter, pol); |
| 191 |
result *= prefix; |
| 192 |
|
| 193 |
if(!need_logs) |
| 194 |
{ |
| 195 |
prefix = boost::math::tgamma(-v, pol) * boost::math::cos_pi(v, pol) * p / constants::pi<T>(); |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
int sgn {}; |
| 200 |
prefix = boost::math::lgamma(-v, &sgn, pol) + p; |
| 201 |
prefix = exp(prefix) * sgn / constants::pi<T>(); |
| 202 |
} |
| 203 |
bessel_y_small_z_series_term_b<T, Policy> s2(v, x); |
| 204 |
max_iter = policies::get_max_series_iterations<Policy>(); |
| 205 |
|
| 206 |
T b = boost::math::tools::sum_series(s2, boost::math::policies::get_epsilon<T, Policy>(), max_iter); |
| 207 |
|
| 208 |
result -= scale * prefix * b; |
| 209 |
return result; |
| 210 |
} |
| 211 |
|
| 212 |
template <class T, class Policy> |
| 213 |
BOOST_MATH_GPU_ENABLED T bessel_yn_small_z(int n, T z, T* scale, const Policy& pol) |
| 214 |
{ |
| 215 |
// |
| 216 |
// See http://functions.wolfram.com/Bessel-TypeFunctions/BesselY/06/01/04/01/02/ |
| 217 |
// |
| 218 |
// Note that when called we assume that x < epsilon and n is a positive integer. |
| 219 |
// |
| 220 |
BOOST_MATH_STD_USING |
| 221 |
BOOST_MATH_ASSERT(n >= 0); |
| 222 |
BOOST_MATH_ASSERT((z < policies::get_epsilon<T, Policy>())); |
| 223 |
|
| 224 |
if(n == 0) |
| 225 |
{ |
| 226 |
return (2 / constants::pi<T>()) * (log(z / 2) + constants::euler<T>()); |
| 227 |
} |
| 228 |
else if(n == 1) |
| 229 |
{ |
| 230 |
return (z / constants::pi<T>()) * log(z / 2) |
| 231 |
- 2 / (constants::pi<T>() * z) |
| 232 |
- (z / (2 * constants::pi<T>())) * (1 - 2 * constants::euler<T>()); |
| 233 |
} |
| 234 |
else if(n == 2) |
| 235 |
{ |
| 236 |
return (z * z) / (4 * constants::pi<T>()) * log(z / 2) |
| 237 |
- (4 / (constants::pi<T>() * z * z)) |
| 238 |
- ((z * z) / (8 * constants::pi<T>())) * (T(3)/2 - 2 * constants::euler<T>()); |
| 239 |
} |
| 240 |
else |
| 241 |
{ |
| 242 |
#if (defined(__GNUC__) && __GNUC__ == 13) |
| 243 |
auto p = static_cast<T>(pow(z / 2, T(n))); |
| 244 |
#else |
| 245 |
auto p = static_cast<T>(pow(z / 2, n)); |
| 246 |
#endif |
| 247 |
|
| 248 |
T result = -((boost::math::factorial<T>(static_cast<unsigned>(n - 1), pol) / constants::pi<T>())); |
| 249 |
if(p * tools::max_value<T>() < fabs(result)) |
| 250 |
{ |
| 251 |
T div = tools::max_value<T>() / 8; |
| 252 |
result /= div; |
| 253 |
*scale /= div; |
| 254 |
if(p * tools::max_value<T>() < result) |
| 255 |
{ |
| 256 |
// Impossible to get here?? |
| 257 |
return -policies::raise_overflow_error<T>("bessel_yn_small_z<%1%>(%1%,%1%)", nullptr, pol); // LCOV_EXCL_LINE |
| 258 |
} |
| 259 |
} |
| 260 |
return result / p; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
}}} // namespaces |
| 265 |
|
| 266 |
#endif // BOOST_MATH_BESSEL_JN_SERIES_HPP |
| 267 |
|