| 1 |
// Copyright (c) 2013 Christopher Kormanyos |
| 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 |
// This work is based on an earlier work: |
| 7 |
// "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations", |
| 8 |
// in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469 |
| 9 |
// |
| 10 |
// This header contains implementation details for estimating the zeros |
| 11 |
// of cylindrical Bessel and Neumann functions on the positive real axis. |
| 12 |
// Support is included for both positive as well as negative order. |
| 13 |
// Various methods are used to estimate the roots. These include |
| 14 |
// empirical curve fitting and McMahon's asymptotic approximation |
| 15 |
// for small order, uniform asymptotic expansion for large order, |
| 16 |
// and iteration and root interlacing for negative order. |
| 17 |
// |
| 18 |
#ifndef BOOST_MATH_BESSEL_JY_ZERO_2013_01_18_HPP_ |
| 19 |
#define BOOST_MATH_BESSEL_JY_ZERO_2013_01_18_HPP_ |
| 20 |
|
| 21 |
#include <boost/math/tools/config.hpp> |
| 22 |
#include <boost/math/tools/tuple.hpp> |
| 23 |
#include <boost/math/tools/precision.hpp> |
| 24 |
#include <boost/math/tools/cstdint.hpp> |
| 25 |
#include <boost/math/tools/roots.hpp> |
| 26 |
#include <boost/math/constants/constants.hpp> |
| 27 |
#include <boost/math/special_functions/cbrt.hpp> |
| 28 |
#include <boost/math/special_functions/detail/airy_ai_bi_zero.hpp> |
| 29 |
|
| 30 |
#ifndef BOOST_MATH_HAS_NVRTC |
| 31 |
#include <boost/math/special_functions/math_fwd.hpp> |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef BOOST_MATH_ENABLE_CUDA |
| 35 |
# pragma nv_diag_suppress 20012 |
| 36 |
#endif |
| 37 |
|
| 38 |
namespace boost { namespace math { |
| 39 |
namespace detail |
| 40 |
{ |
| 41 |
namespace bessel_zero |
| 42 |
{ |
| 43 |
template<class T> |
| 44 |
BOOST_MATH_GPU_ENABLED T equation_nist_10_21_19(const T& v, const T& a) |
| 45 |
{ |
| 46 |
// Get the initial estimate of the m'th root of Jv or Yv. |
| 47 |
// This subroutine is used for the order m with m > 1. |
| 48 |
// The order m has been used to create the input parameter a. |
| 49 |
|
| 50 |
// This is Eq. 10.21.19 in the NIST Handbook. |
| 51 |
const T mu = (v * v) * 4U; |
| 52 |
const T mu_minus_one = mu - T(1); |
| 53 |
const T eight_a_inv = T(1) / (a * 8U); |
| 54 |
const T eight_a_inv_squared = eight_a_inv * eight_a_inv; |
| 55 |
|
| 56 |
const T term3 = ((mu_minus_one * 4U) * ((mu * 7U) - T(31U) )) / 3U; |
| 57 |
const T term5 = ((mu_minus_one * 32U) * ((((mu * 83U) - T(982U) ) * mu) + T(3779U) )) / 15U; |
| 58 |
const T term7 = ((mu_minus_one * 64U) * ((((((mu * 6949U) - T(153855UL)) * mu) + T(1585743UL)) * mu) - T(6277237UL))) / 105U; |
| 59 |
|
| 60 |
return a + (((( - term7 |
| 61 |
* eight_a_inv_squared - term5) |
| 62 |
* eight_a_inv_squared - term3) |
| 63 |
* eight_a_inv_squared - mu_minus_one) |
| 64 |
* eight_a_inv); |
| 65 |
} |
| 66 |
|
| 67 |
template<typename T> |
| 68 |
class equation_as_9_3_39_and_its_derivative |
| 69 |
{ |
| 70 |
public: |
| 71 |
BOOST_MATH_GPU_ENABLED explicit equation_as_9_3_39_and_its_derivative(const T& zt) : zeta(zt) { } |
| 72 |
|
| 73 |
BOOST_MATH_GPU_ENABLED equation_as_9_3_39_and_its_derivative(const equation_as_9_3_39_and_its_derivative&) = default; |
| 74 |
|
| 75 |
BOOST_MATH_GPU_ENABLED boost::math::tuple<T, T> operator()(const T& z) const |
| 76 |
{ |
| 77 |
BOOST_MATH_STD_USING // ADL of std names, needed for acos, sqrt. |
| 78 |
|
| 79 |
// Return the function of zeta that is implicitly defined |
| 80 |
// in A&S Eq. 9.3.39 as a function of z. The function is |
| 81 |
// returned along with its derivative with respect to z. |
| 82 |
|
| 83 |
const T zsq_minus_one_sqrt = sqrt((z * z) - T(1)); |
| 84 |
|
| 85 |
const T the_function( |
| 86 |
zsq_minus_one_sqrt |
| 87 |
- ( acos(T(1) / z) + ((T(2) / 3U) * (zeta * sqrt(zeta))))); |
| 88 |
|
| 89 |
const T its_derivative(zsq_minus_one_sqrt / z); |
| 90 |
|
| 91 |
return boost::math::tuple<T, T>(the_function, its_derivative); |
| 92 |
} |
| 93 |
|
| 94 |
private: |
| 95 |
const equation_as_9_3_39_and_its_derivative& operator=(const equation_as_9_3_39_and_its_derivative&) = delete; |
| 96 |
const T zeta; |
| 97 |
}; |
| 98 |
|
| 99 |
template<class T, class Policy> |
| 100 |
BOOST_MATH_GPU_ENABLED T equation_as_9_5_26(const T& v, const T& ai_bi_root, const Policy& pol) |
| 101 |
{ |
| 102 |
BOOST_MATH_STD_USING // ADL of std names, needed for log, sqrt. |
| 103 |
|
| 104 |
// Obtain the estimate of the m'th zero of Jv or Yv. |
| 105 |
// The order m has been used to create the input parameter ai_bi_root. |
| 106 |
// Here, v is larger than about 2.2. The estimate is computed |
| 107 |
// from Abramowitz and Stegun Eqs. 9.5.22 and 9.5.26, page 371. |
| 108 |
// |
| 109 |
// The inversion of z as a function of zeta is mentioned in the text |
| 110 |
// following A&S Eq. 9.5.26. Here, we accomplish the inversion by |
| 111 |
// performing a Taylor expansion of Eq. 9.3.39 for large z to order 2 |
| 112 |
// and solving the resulting quadratic equation, thereby taking |
| 113 |
// the positive root of the quadratic. |
| 114 |
// In other words: (2/3)(-zeta)^(3/2) approx = z + 1/(2z) - pi/2. |
| 115 |
// This leads to: z^2 - [(2/3)(-zeta)^(3/2) + pi/2]z + 1/2 = 0. |
| 116 |
// |
| 117 |
// With this initial estimate, Newton-Raphson iteration is used |
| 118 |
// to refine the value of the estimate of the root of z |
| 119 |
// as a function of zeta. |
| 120 |
|
| 121 |
const T v_pow_third(boost::math::cbrt(v, pol)); |
| 122 |
const T v_pow_minus_two_thirds(T(1) / (v_pow_third * v_pow_third)); |
| 123 |
|
| 124 |
// Obtain zeta using the order v combined with the m'th root of |
| 125 |
// an airy function, as shown in A&S Eq. 9.5.22. |
| 126 |
const T zeta = v_pow_minus_two_thirds * (-ai_bi_root); |
| 127 |
|
| 128 |
const T zeta_sqrt = sqrt(zeta); |
| 129 |
|
| 130 |
// Set up a quadratic equation based on the Taylor series |
| 131 |
// expansion mentioned above. |
| 132 |
const T b = -((((zeta * zeta_sqrt) * 2U) / 3U) + boost::math::constants::half_pi<T>()); |
| 133 |
|
| 134 |
// Solve the quadratic equation, taking the positive root. |
| 135 |
const T z_estimate = (-b + sqrt((b * b) - T(2))) / 2U; |
| 136 |
|
| 137 |
// Establish the range, the digits, and the iteration limit |
| 138 |
// for the upcoming root-finding. |
| 139 |
const T range_zmin = (std::max<T>)(z_estimate - T(1), T(1)); |
| 140 |
const T range_zmax = z_estimate + T(1); |
| 141 |
|
| 142 |
const auto my_digits10 = static_cast<int>(static_cast<float>(boost::math::tools::digits<T>() * 0.301F)); |
| 143 |
|
| 144 |
// Select the maximum allowed iterations based on the number |
| 145 |
// of decimal digits in the numeric type T, being at least 12. |
| 146 |
const auto iterations_allowed = static_cast<boost::math::uintmax_t>(BOOST_MATH_GPU_SAFE_MAX(12, my_digits10 * 2)); |
| 147 |
|
| 148 |
boost::math::uintmax_t iterations_used = iterations_allowed; |
| 149 |
|
| 150 |
// Calculate the root of z as a function of zeta. |
| 151 |
const T z = boost::math::tools::newton_raphson_iterate( |
| 152 |
boost::math::detail::bessel_zero::equation_as_9_3_39_and_its_derivative<T>(zeta), |
| 153 |
z_estimate, |
| 154 |
range_zmin, |
| 155 |
range_zmax, |
| 156 |
BOOST_MATH_GPU_SAFE_MIN(boost::math::tools::digits<T>(), boost::math::tools::digits<float>()), |
| 157 |
iterations_used); |
| 158 |
|
| 159 |
static_cast<void>(iterations_used); |
| 160 |
|
| 161 |
// Continue with the implementation of A&S Eq. 9.3.39. |
| 162 |
const T zsq_minus_one = (z * z) - T(1); |
| 163 |
const T zsq_minus_one_sqrt = sqrt(zsq_minus_one); |
| 164 |
|
| 165 |
// This is A&S Eq. 9.3.42. |
| 166 |
const T b0_term_5_24 = T(5) / ((zsq_minus_one * zsq_minus_one_sqrt) * 24U); |
| 167 |
const T b0_term_1_8 = T(1) / ( zsq_minus_one_sqrt * 8U); |
| 168 |
const T b0_term_5_48 = T(5) / ((zeta * zeta) * 48U); |
| 169 |
|
| 170 |
const T b0 = -b0_term_5_48 + ((b0_term_5_24 + b0_term_1_8) / zeta_sqrt); |
| 171 |
|
| 172 |
// This is the second line of A&S Eq. 9.5.26 for f_k with k = 1. |
| 173 |
const T f1 = ((z * zeta_sqrt) * b0) / zsq_minus_one_sqrt; |
| 174 |
|
| 175 |
// This is A&S Eq. 9.5.22 expanded to k = 1 (i.e., one term in the series). |
| 176 |
return (v * z) + (f1 / v); |
| 177 |
} |
| 178 |
|
| 179 |
namespace cyl_bessel_j_zero_detail |
| 180 |
{ |
| 181 |
template<class T, class Policy> |
| 182 |
BOOST_MATH_GPU_ENABLED T equation_nist_10_21_40_a(const T& v, const Policy& pol) |
| 183 |
{ |
| 184 |
const T v_pow_third(boost::math::cbrt(v, pol)); |
| 185 |
const T v_pow_minus_two_thirds(T(1) / (v_pow_third * v_pow_third)); |
| 186 |
|
| 187 |
return v * ((((( + T(0.043) |
| 188 |
* v_pow_minus_two_thirds - T(0.0908)) |
| 189 |
* v_pow_minus_two_thirds - T(0.00397)) |
| 190 |
* v_pow_minus_two_thirds + T(1.033150)) |
| 191 |
* v_pow_minus_two_thirds + T(1.8557571)) |
| 192 |
* v_pow_minus_two_thirds + T(1)); |
| 193 |
} |
| 194 |
|
| 195 |
template<class T, class Policy> |
| 196 |
class function_object_jv |
| 197 |
{ |
| 198 |
public: |
| 199 |
BOOST_MATH_GPU_ENABLED function_object_jv(const T& v, |
| 200 |
const Policy& pol) : my_v(v), |
| 201 |
my_pol(pol) { } |
| 202 |
|
| 203 |
BOOST_MATH_GPU_ENABLED function_object_jv(const function_object_jv&) = default; |
| 204 |
|
| 205 |
BOOST_MATH_GPU_ENABLED T operator()(const T& x) const |
| 206 |
{ |
| 207 |
return boost::math::cyl_bessel_j(my_v, x, my_pol); |
| 208 |
} |
| 209 |
|
| 210 |
private: |
| 211 |
const T my_v; |
| 212 |
const Policy& my_pol; |
| 213 |
const function_object_jv& operator=(const function_object_jv&) = delete; |
| 214 |
}; |
| 215 |
|
| 216 |
template<class T, class Policy> |
| 217 |
class function_object_jv_and_jv_prime |
| 218 |
{ |
| 219 |
public: |
| 220 |
BOOST_MATH_GPU_ENABLED function_object_jv_and_jv_prime( |
| 221 |
const T& v, |
| 222 |
const bool order_is_zero, |
| 223 |
const Policy& pol) : my_v(v), |
| 224 |
my_order_is_zero(order_is_zero), |
| 225 |
my_pol(pol) { } |
| 226 |
|
| 227 |
function_object_jv_and_jv_prime(const function_object_jv_and_jv_prime&) = default; |
| 228 |
|
| 229 |
BOOST_MATH_GPU_ENABLED boost::math::tuple<T, T> operator()(const T& x) const |
| 230 |
{ |
| 231 |
// Obtain Jv(x) and Jv'(x). |
| 232 |
// Chris's original code called the Bessel function implementation layer direct, |
| 233 |
// but that circumvented optimizations for integer-orders. Call the documented |
| 234 |
// top level functions instead, and let them sort out which implementation to use. |
| 235 |
T j_v; |
| 236 |
T j_v_prime; |
| 237 |
|
| 238 |
if(my_order_is_zero) |
| 239 |
{ |
| 240 |
j_v = boost::math::cyl_bessel_j(0, x, my_pol); |
| 241 |
j_v_prime = -boost::math::cyl_bessel_j(1, x, my_pol); |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
j_v = boost::math::cyl_bessel_j( my_v, x, my_pol); |
| 246 |
const T j_v_m1 (boost::math::cyl_bessel_j(T(my_v - 1), x, my_pol)); |
| 247 |
j_v_prime = j_v_m1 - ((my_v * j_v) / x); |
| 248 |
} |
| 249 |
|
| 250 |
// Return a tuple containing both Jv(x) and Jv'(x). |
| 251 |
return boost::math::make_tuple(j_v, j_v_prime); |
| 252 |
} |
| 253 |
|
| 254 |
private: |
| 255 |
const T my_v; |
| 256 |
const bool my_order_is_zero; |
| 257 |
const Policy& my_pol; |
| 258 |
const function_object_jv_and_jv_prime& operator=(const function_object_jv_and_jv_prime&) = delete; |
| 259 |
}; |
| 260 |
|
| 261 |
template<class T> BOOST_MATH_GPU_ENABLED bool my_bisection_unreachable_tolerance(const T&, const T&) { return false; } |
| 262 |
|
| 263 |
template<class T, class Policy> |
| 264 |
BOOST_MATH_GPU_ENABLED T initial_guess(const T& v, const int m, const Policy& pol) |
| 265 |
{ |
| 266 |
BOOST_MATH_STD_USING // ADL of std names, needed for floor. |
| 267 |
|
| 268 |
// Compute an estimate of the m'th root of cyl_bessel_j. |
| 269 |
|
| 270 |
T guess; |
| 271 |
|
| 272 |
// There is special handling for negative order. |
| 273 |
if(v < 0) |
| 274 |
{ |
| 275 |
if((m == 1) && (v > -0.5F)) |
| 276 |
{ |
| 277 |
// For small, negative v, use the results of empirical curve fitting. |
| 278 |
// Mathematica(R) session for the coefficients: |
| 279 |
// Table[{n, BesselJZero[n, 1]}, {n, -(1/2), 0, 1/10}] |
| 280 |
// N[%, 20] |
| 281 |
// Fit[%, {n^0, n^1, n^2, n^3, n^4, n^5, n^6}, n] |
| 282 |
guess = ((((( - T(0.2321156900729) |
| 283 |
* v - T(0.1493247777488)) |
| 284 |
* v - T(0.15205419167239)) |
| 285 |
* v + T(0.07814930561249)) |
| 286 |
* v - T(0.17757573537688)) |
| 287 |
* v + T(1.542805677045663)) |
| 288 |
* v + T(2.40482555769577277); |
| 289 |
|
| 290 |
return guess; |
| 291 |
} |
| 292 |
|
| 293 |
// Create the positive order and extract its positive floor integer part. |
| 294 |
const T vv(-v); |
| 295 |
const T vv_floor(floor(vv)); |
| 296 |
|
| 297 |
// The to-be-found root is bracketed by the roots of the |
| 298 |
// Bessel function whose reflected, positive integer order |
| 299 |
// is less than, but nearest to vv. |
| 300 |
|
| 301 |
T root_hi = boost::math::detail::bessel_zero::cyl_bessel_j_zero_detail::initial_guess(vv_floor, m, pol); |
| 302 |
T root_lo; |
| 303 |
|
| 304 |
if(m == 1) |
| 305 |
{ |
| 306 |
// The estimate of the first root for negative order is found using |
| 307 |
// an adaptive range-searching algorithm. |
| 308 |
root_lo = T(root_hi - 0.1F); |
| 309 |
|
| 310 |
const bool hi_end_of_bracket_is_negative = (boost::math::cyl_bessel_j(v, root_hi, pol) < 0); |
| 311 |
|
| 312 |
while((root_lo > boost::math::tools::epsilon<T>())) |
| 313 |
{ |
| 314 |
const bool lo_end_of_bracket_is_negative = (boost::math::cyl_bessel_j(v, root_lo, pol) < 0); |
| 315 |
|
| 316 |
if(hi_end_of_bracket_is_negative != lo_end_of_bracket_is_negative) |
| 317 |
{ |
| 318 |
break; |
| 319 |
} |
| 320 |
|
| 321 |
root_hi = root_lo; |
| 322 |
|
| 323 |
// Decrease the lower end of the bracket using an adaptive algorithm. |
| 324 |
if(root_lo > 0.5F) |
| 325 |
{ |
| 326 |
root_lo -= 0.5F; |
| 327 |
} |
| 328 |
else |
| 329 |
{ |
| 330 |
root_lo *= 0.75F; // LCOV_EXCL_LINE probably unreachable, but hard to prove? |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
root_lo = boost::math::detail::bessel_zero::cyl_bessel_j_zero_detail::initial_guess(vv_floor, m - 1, pol); |
| 337 |
} |
| 338 |
|
| 339 |
// Perform several steps of bisection iteration to refine the guess. |
| 340 |
boost::math::uintmax_t number_of_iterations(12U); |
| 341 |
|
| 342 |
// Do the bisection iteration. |
| 343 |
const boost::math::tuple<T, T> guess_pair = |
| 344 |
boost::math::tools::bisect( |
| 345 |
boost::math::detail::bessel_zero::cyl_bessel_j_zero_detail::function_object_jv<T, Policy>(v, pol), |
| 346 |
root_lo, |
| 347 |
root_hi, |
| 348 |
boost::math::detail::bessel_zero::cyl_bessel_j_zero_detail::my_bisection_unreachable_tolerance<T>, |
| 349 |
number_of_iterations); |
| 350 |
|
| 351 |
return (boost::math::get<0>(guess_pair) + boost::math::get<1>(guess_pair)) / 2U; |
| 352 |
} |
| 353 |
|
| 354 |
if(m == 1U) |
| 355 |
{ |
| 356 |
// Get the initial estimate of the first root. |
| 357 |
|
| 358 |
if(v < 2.2F) |
| 359 |
{ |
| 360 |
// For small v, use the results of empirical curve fitting. |
| 361 |
// Mathematica(R) session for the coefficients: |
| 362 |
// Table[{n, BesselJZero[n, 1]}, {n, 0, 22/10, 1/10}] |
| 363 |
// N[%, 20] |
| 364 |
// Fit[%, {n^0, n^1, n^2, n^3, n^4, n^5, n^6}, n] |
| 365 |
guess = ((((( - T(0.0008342379046010) |
| 366 |
* v + T(0.007590035637410)) |
| 367 |
* v - T(0.030640914772013)) |
| 368 |
* v + T(0.078232088020106)) |
| 369 |
* v - T(0.169668712590620)) |
| 370 |
* v + T(1.542187960073750)) |
| 371 |
* v + T(2.4048359915254634); |
| 372 |
} |
| 373 |
else |
| 374 |
{ |
| 375 |
// For larger v, use the first line of Eqs. 10.21.40 in the NIST Handbook. |
| 376 |
guess = boost::math::detail::bessel_zero::cyl_bessel_j_zero_detail::equation_nist_10_21_40_a(v, pol); |
| 377 |
} |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
if(v < 2.2F) |
| 382 |
{ |
| 383 |
// Use Eq. 10.21.19 in the NIST Handbook. |
| 384 |
const T a(((v + T(m * 2U)) - T(0.5)) * boost::math::constants::half_pi<T>()); |
| 385 |
|
| 386 |
guess = boost::math::detail::bessel_zero::equation_nist_10_21_19(v, a); |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
// Get an estimate of the m'th root of airy_ai. |
| 391 |
const T airy_ai_root(boost::math::detail::airy_zero::airy_ai_zero_detail::initial_guess<T>(m, pol)); |
| 392 |
|
| 393 |
// Use Eq. 9.5.26 in the A&S Handbook. |
| 394 |
guess = boost::math::detail::bessel_zero::equation_as_9_5_26(v, airy_ai_root, pol); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
return guess; |
| 399 |
} |
| 400 |
} // namespace cyl_bessel_j_zero_detail |
| 401 |
|
| 402 |
namespace cyl_neumann_zero_detail |
| 403 |
{ |
| 404 |
template<class T, class Policy> |
| 405 |
BOOST_MATH_GPU_ENABLED T equation_nist_10_21_40_b(const T& v, const Policy& pol) |
| 406 |
{ |
| 407 |
const T v_pow_third(boost::math::cbrt(v, pol)); |
| 408 |
const T v_pow_minus_two_thirds(T(1) / (v_pow_third * v_pow_third)); |
| 409 |
|
| 410 |
return v * ((((( - T(0.001) |
| 411 |
* v_pow_minus_two_thirds - T(0.0060)) |
| 412 |
* v_pow_minus_two_thirds + T(0.01198)) |
| 413 |
* v_pow_minus_two_thirds + T(0.260351)) |
| 414 |
* v_pow_minus_two_thirds + T(0.9315768)) |
| 415 |
* v_pow_minus_two_thirds + T(1)); |
| 416 |
} |
| 417 |
|
| 418 |
template<class T, class Policy> |
| 419 |
class function_object_yv |
| 420 |
{ |
| 421 |
public: |
| 422 |
BOOST_MATH_GPU_ENABLED function_object_yv(const T& v, |
| 423 |
const Policy& pol) : my_v(v), |
| 424 |
my_pol(pol) { } |
| 425 |
|
| 426 |
BOOST_MATH_GPU_ENABLED function_object_yv(const function_object_yv&) = default; |
| 427 |
|
| 428 |
BOOST_MATH_GPU_ENABLED T operator()(const T& x) const |
| 429 |
{ |
| 430 |
return boost::math::cyl_neumann(my_v, x, my_pol); |
| 431 |
} |
| 432 |
|
| 433 |
private: |
| 434 |
const T my_v; |
| 435 |
const Policy& my_pol; |
| 436 |
const function_object_yv& operator=(const function_object_yv&) = delete; |
| 437 |
}; |
| 438 |
|
| 439 |
template<class T, class Policy> |
| 440 |
class function_object_yv_and_yv_prime |
| 441 |
{ |
| 442 |
public: |
| 443 |
BOOST_MATH_GPU_ENABLED function_object_yv_and_yv_prime(const T& v, |
| 444 |
const Policy& pol) : my_v(v), |
| 445 |
my_pol(pol) { } |
| 446 |
|
| 447 |
BOOST_MATH_GPU_ENABLED function_object_yv_and_yv_prime(const function_object_yv_and_yv_prime&) = default; |
| 448 |
|
| 449 |
BOOST_MATH_GPU_ENABLED boost::math::tuple<T, T> operator()(const T& x) const |
| 450 |
{ |
| 451 |
const T half_epsilon(boost::math::tools::epsilon<T>() / 2U); |
| 452 |
|
| 453 |
const bool order_is_zero = ((my_v > -half_epsilon) && (my_v < +half_epsilon)); |
| 454 |
|
| 455 |
// Obtain Yv(x) and Yv'(x). |
| 456 |
// Chris's original code called the Bessel function implementation layer direct, |
| 457 |
// but that circumvented optimizations for integer-orders. Call the documented |
| 458 |
// top level functions instead, and let them sort out which implementation to use. |
| 459 |
T y_v; |
| 460 |
T y_v_prime; |
| 461 |
|
| 462 |
if(order_is_zero) |
| 463 |
{ |
| 464 |
y_v = boost::math::cyl_neumann(0, x, my_pol); |
| 465 |
y_v_prime = -boost::math::cyl_neumann(1, x, my_pol); |
| 466 |
} |
| 467 |
else |
| 468 |
{ |
| 469 |
y_v = boost::math::cyl_neumann( my_v, x, my_pol); |
| 470 |
const T y_v_m1 (boost::math::cyl_neumann(T(my_v - 1), x, my_pol)); |
| 471 |
y_v_prime = y_v_m1 - ((my_v * y_v) / x); |
| 472 |
} |
| 473 |
|
| 474 |
// Return a tuple containing both Yv(x) and Yv'(x). |
| 475 |
return boost::math::make_tuple(y_v, y_v_prime); |
| 476 |
} |
| 477 |
|
| 478 |
private: |
| 479 |
const T my_v; |
| 480 |
const Policy& my_pol; |
| 481 |
const function_object_yv_and_yv_prime& operator=(const function_object_yv_and_yv_prime&) = delete; |
| 482 |
}; |
| 483 |
|
| 484 |
template<class T> BOOST_MATH_GPU_ENABLED bool my_bisection_unreachable_tolerance(const T&, const T&) { return false; } |
| 485 |
|
| 486 |
template<class T, class Policy> |
| 487 |
BOOST_MATH_GPU_ENABLED T initial_guess(const T& v, const int m, const Policy& pol) |
| 488 |
{ |
| 489 |
BOOST_MATH_STD_USING // ADL of std names, needed for floor. |
| 490 |
|
| 491 |
// Compute an estimate of the m'th root of cyl_neumann. |
| 492 |
|
| 493 |
T guess; |
| 494 |
|
| 495 |
// There is special handling for negative order. |
| 496 |
if(v < 0) |
| 497 |
{ |
| 498 |
// Create the positive order and extract its positive floor and ceiling integer parts. |
| 499 |
const T vv(-v); |
| 500 |
const T vv_floor(floor(vv)); |
| 501 |
|
| 502 |
// The to-be-found root is bracketed by the roots of the |
| 503 |
// Bessel function whose reflected, positive integer order |
| 504 |
// is less than, but nearest to vv. |
| 505 |
|
| 506 |
// The special case of negative, half-integer order uses |
| 507 |
// the relation between Yv and spherical Bessel functions |
| 508 |
// in order to obtain the bracket for the root. |
| 509 |
// In these special cases, cyl_neumann(-n/2, x) = sph_bessel_j(+n/2, x) |
| 510 |
// for v = -n/2. |
| 511 |
|
| 512 |
T root_hi; |
| 513 |
T root_lo; |
| 514 |
|
| 515 |
if(m == 1) |
| 516 |
{ |
| 517 |
// The estimate of the first root for negative order is found using |
| 518 |
// an adaptive range-searching algorithm. |
| 519 |
// Take special precautions for the discontinuity at negative, |
| 520 |
// half-integer orders and use different brackets above and below these. |
| 521 |
if(T(vv - vv_floor) < 0.5F) |
| 522 |
{ |
| 523 |
root_hi = boost::math::detail::bessel_zero::cyl_neumann_zero_detail::initial_guess(vv_floor, m, pol); |
| 524 |
} |
| 525 |
else |
| 526 |
{ |
| 527 |
root_hi = boost::math::detail::bessel_zero::cyl_bessel_j_zero_detail::initial_guess(T(vv_floor + 0.5F), m, pol); |
| 528 |
} |
| 529 |
|
| 530 |
root_lo = T(root_hi - 0.1F); |
| 531 |
|
| 532 |
const bool hi_end_of_bracket_is_negative = (boost::math::cyl_neumann(v, root_hi, pol) < 0); |
| 533 |
|
| 534 |
while((root_lo > boost::math::tools::epsilon<T>())) |
| 535 |
{ |
| 536 |
const bool lo_end_of_bracket_is_negative = (boost::math::cyl_neumann(v, root_lo, pol) < 0); |
| 537 |
|
| 538 |
if(hi_end_of_bracket_is_negative != lo_end_of_bracket_is_negative) |
| 539 |
{ |
| 540 |
break; |
| 541 |
} |
| 542 |
|
| 543 |
root_hi = root_lo; |
| 544 |
|
| 545 |
// Decrease the lower end of the bracket using an adaptive algorithm. |
| 546 |
if(root_lo > 0.5F) |
| 547 |
{ |
| 548 |
root_lo -= 0.5F; |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
root_lo *= 0.75F; // LCOV_EXCL_LINE probably unreachable, but hard to prove? |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
else |
| 557 |
{ |
| 558 |
if(T(vv - vv_floor) < 0.5F) |
| 559 |
{ |
| 560 |
root_lo = boost::math::detail::bessel_zero::cyl_neumann_zero_detail::initial_guess(vv_floor, m - 1, pol); |
| 561 |
root_hi = boost::math::detail::bessel_zero::cyl_neumann_zero_detail::initial_guess(vv_floor, m, pol); |
| 562 |
root_lo += 0.01F; |
| 563 |
root_hi += 0.01F; |
| 564 |
} |
| 565 |
else |
| 566 |
{ |
| 567 |
root_lo = boost::math::detail::bessel_zero::cyl_bessel_j_zero_detail::initial_guess(T(vv_floor + 0.5F), m - 1, pol); |
| 568 |
root_hi = boost::math::detail::bessel_zero::cyl_bessel_j_zero_detail::initial_guess(T(vv_floor + 0.5F), m, pol); |
| 569 |
root_lo += 0.01F; |
| 570 |
root_hi += 0.01F; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
// Perform several steps of bisection iteration to refine the guess. |
| 575 |
boost::math::uintmax_t number_of_iterations(12U); |
| 576 |
|
| 577 |
// Do the bisection iteration. |
| 578 |
const boost::math::tuple<T, T> guess_pair = |
| 579 |
boost::math::tools::bisect( |
| 580 |
boost::math::detail::bessel_zero::cyl_neumann_zero_detail::function_object_yv<T, Policy>(v, pol), |
| 581 |
root_lo, |
| 582 |
root_hi, |
| 583 |
boost::math::detail::bessel_zero::cyl_neumann_zero_detail::my_bisection_unreachable_tolerance<T>, |
| 584 |
number_of_iterations); |
| 585 |
|
| 586 |
return (boost::math::get<0>(guess_pair) + boost::math::get<1>(guess_pair)) / 2U; |
| 587 |
} |
| 588 |
|
| 589 |
if(m == 1U) |
| 590 |
{ |
| 591 |
// Get the initial estimate of the first root. |
| 592 |
|
| 593 |
if(v < 2.2F) |
| 594 |
{ |
| 595 |
// For small v, use the results of empirical curve fitting. |
| 596 |
// Mathematica(R) session for the coefficients: |
| 597 |
// Table[{n, BesselYZero[n, 1]}, {n, 0, 22/10, 1/10}] |
| 598 |
// N[%, 20] |
| 599 |
// Fit[%, {n^0, n^1, n^2, n^3, n^4, n^5, n^6}, n] |
| 600 |
guess = ((((( - T(0.0025095909235652) |
| 601 |
* v + T(0.021291887049053)) |
| 602 |
* v - T(0.076487785486526)) |
| 603 |
* v + T(0.159110268115362)) |
| 604 |
* v - T(0.241681668765196)) |
| 605 |
* v + T(1.4437846310885244)) |
| 606 |
* v + T(0.89362115190200490); |
| 607 |
} |
| 608 |
else |
| 609 |
{ |
| 610 |
// For larger v, use the second line of Eqs. 10.21.40 in the NIST Handbook. |
| 611 |
guess = boost::math::detail::bessel_zero::cyl_neumann_zero_detail::equation_nist_10_21_40_b(v, pol); |
| 612 |
} |
| 613 |
} |
| 614 |
else |
| 615 |
{ |
| 616 |
if(v < 2.2F) |
| 617 |
{ |
| 618 |
// Use Eq. 10.21.19 in the NIST Handbook. |
| 619 |
const T a(((v + T(m * 2U)) - T(1.5)) * boost::math::constants::half_pi<T>()); |
| 620 |
|
| 621 |
guess = boost::math::detail::bessel_zero::equation_nist_10_21_19(v, a); |
| 622 |
} |
| 623 |
else |
| 624 |
{ |
| 625 |
// Get an estimate of the m'th root of airy_bi. |
| 626 |
const T airy_bi_root(boost::math::detail::airy_zero::airy_bi_zero_detail::initial_guess<T>(m, pol)); |
| 627 |
|
| 628 |
// Use Eq. 9.5.26 in the A&S Handbook. |
| 629 |
guess = boost::math::detail::bessel_zero::equation_as_9_5_26(v, airy_bi_root, pol); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
return guess; |
| 634 |
} |
| 635 |
} // namespace cyl_neumann_zero_detail |
| 636 |
} // namespace bessel_zero |
| 637 |
} } } // namespace boost::math::detail |
| 638 |
|
| 639 |
#ifdef BOOST_MATH_ENABLE_CUDA |
| 640 |
# pragma nv_diag_default 20012 |
| 641 |
#endif |
| 642 |
|
| 643 |
#endif // BOOST_MATH_BESSEL_JY_ZERO_2013_01_18_HPP_ |