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 6 #ifndef BOOST_MATH_BESSEL_K1_HPP 7 #define BOOST_MATH_BESSEL_K1_HPP 8 9 #ifdef _MSC_VER 10 #pragma once 11 #endif 12 13 #include <boost/math/tools/rational.hpp> 14 #include <boost/math/tools/big_constant.hpp> 15 #include <boost/math/policies/error_handling.hpp> 16 #include <boost/assert.hpp> 17 18 // Modified Bessel function of the second kind of order one 19 // minimax rational approximations on intervals, see 20 // Russon and Blair, Chalk River Report AECL-3461, 1969 21 22 namespace boost { namespace math { namespace detail{ 23 24 template <typename T, typename Policy> 25 T bessel_k1(T x, const Policy&); 26 27 template <class T, class Policy> 28 struct bessel_k1_initializer 29 { 30 struct init 31 { initboost::math::detail::bessel_k1_initializer::init32 init() 33 { 34 do_init(); 35 } do_initboost::math::detail::bessel_k1_initializer::init36 static void do_init() 37 { 38 bessel_k1(T(1), Policy()); 39 } force_instantiateboost::math::detail::bessel_k1_initializer::init40 void force_instantiate()const{} 41 }; 42 static const init initializer; force_instantiateboost::math::detail::bessel_k1_initializer43 static void force_instantiate() 44 { 45 initializer.force_instantiate(); 46 } 47 }; 48 49 template <class T, class Policy> 50 const typename bessel_k1_initializer<T, Policy>::init bessel_k1_initializer<T, Policy>::initializer; 51 52 template <typename T, typename Policy> 53 T bessel_k1(T x, const Policy& pol) 54 { 55 bessel_k1_initializer<T, Policy>::force_instantiate(); 56 57 static const T P1[] = { 58 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2149374878243304548e+06)), 59 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.1938920065420586101e+05)), 60 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.7733324035147015630e+05)), 61 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.1885382604084798576e+03)), 62 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 9.9991373567429309922e+01)), 63 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.8127070456878442310e-01)) 64 }; 65 static const T Q1[] = { 66 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2149374878243304548e+06)), 67 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.7264298672067697862e+04)), 68 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.8143915754538725829e+02)), 69 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)) 70 }; 71 static const T P2[] = { 72 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.0)), 73 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.3531161492785421328e+06)), 74 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.4758069205414222471e+05)), 75 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -4.5051623763436087023e+03)), 76 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -5.3103913335180275253e+01)), 77 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2795590826955002390e-01)) 78 }; 79 static const T Q2[] = { 80 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.7062322985570842656e+06)), 81 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.3117653211351080007e+04)), 82 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -3.0507151578787595807e+02)), 83 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)) 84 }; 85 static const T P3[] = { 86 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2196792496874548962e+00)), 87 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.4137176114230414036e+01)), 88 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4122953486801312910e+02)), 89 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.3319486433183221990e+03)), 90 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.8590657697910288226e+03)), 91 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4540675585544584407e+03)), 92 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.3123742209168871550e+03)), 93 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.1094256146537402173e+02)), 94 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.3182609918569941308e+02)), 95 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.5584584631176030810e+00)), 96 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.4257745859173138767e-02)) 97 }; 98 static const T Q3[] = { 99 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.7710478032601086579e+00)), 100 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4552228452758912848e+01)), 101 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.5951223655579051357e+02)), 102 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 9.6929165726802648634e+02)), 103 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.9448440788918006154e+03)), 104 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1181000487171943810e+03)), 105 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.2082692316002348638e+03)), 106 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.3031020088765390854e+02)), 107 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.6001069306861518855e+01)), 108 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)) 109 }; 110 T value, factor, r, r1, r2; 111 112 BOOST_MATH_STD_USING 113 using namespace boost::math::tools; 114 115 static const char* function = "boost::math::bessel_k1<%1%>(%1%,%1%)"; 116 117 if (x < 0) 118 { 119 return policies::raise_domain_error<T>(function, 120 "Got x = %1%, but argument x must be non-negative, complex number result not supported.", x, pol); 121 } 122 if (x == 0) 123 { 124 return policies::raise_overflow_error<T>(function, 0, pol); 125 } 126 if (x <= 1) // x in (0, 1] 127 { 128 T y = x * x; 129 r1 = evaluate_polynomial(P1, y) / evaluate_polynomial(Q1, y); 130 r2 = evaluate_polynomial(P2, y) / evaluate_polynomial(Q2, y); 131 factor = log(x); 132 value = (r1 + factor * r2) / x; 133 } 134 else // x in (1, \infty) 135 { 136 T y = 1 / x; 137 r = evaluate_polynomial(P3, y) / evaluate_polynomial(Q3, y); 138 factor = exp(-x) / sqrt(x); 139 value = factor * r; 140 } 141 142 return value; 143 } 144 145 }}} // namespaces 146 147 #endif // BOOST_MATH_BESSEL_K1_HPP 148 149