File indexing completed on 2025-01-18 09:40:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_
0012 #define _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_
0013
0014 #include <cmath>
0015 #include <limits>
0016 #include <string>
0017 #include <boost/math/policies/policy.hpp>
0018 #include <boost/math/special_functions/bernoulli.hpp>
0019 #include <boost/math/special_functions/trunc.hpp>
0020 #include <boost/math/special_functions/zeta.hpp>
0021 #include <boost/math/special_functions/digamma.hpp>
0022 #include <boost/math/special_functions/sin_pi.hpp>
0023 #include <boost/math/special_functions/cos_pi.hpp>
0024 #include <boost/math/special_functions/pow.hpp>
0025 #include <boost/math/tools/assert.hpp>
0026 #include <boost/math/tools/config.hpp>
0027
0028 #ifdef BOOST_HAS_THREADS
0029 #include <mutex>
0030 #endif
0031
0032 #ifdef _MSC_VER
0033 #pragma once
0034 #pragma warning(push)
0035 #pragma warning(disable:4702)
0036 #endif
0037
0038 namespace boost { namespace math { namespace detail{
0039
0040 template<class T, class Policy>
0041 T polygamma_atinfinityplus(const int n, const T& x, const Policy& pol, const char* function)
0042 {
0043
0044 BOOST_MATH_STD_USING
0045
0046
0047
0048
0049
0050 if(n + x == x)
0051 {
0052
0053 if(n == 1) return 1 / x;
0054 T nlx = n * log(x);
0055 if((nlx < tools::log_max_value<T>()) && (n < (int)max_factorial<T>::value))
0056 return ((n & 1) ? 1 : -1) * boost::math::factorial<T>(n - 1, pol) * static_cast<T>(pow(x, T(-n)));
0057 else
0058 return ((n & 1) ? 1 : -1) * exp(boost::math::lgamma(T(n), pol) - n * log(x));
0059 }
0060 T term, sum, part_term;
0061 T x_squared = x * x;
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 part_term = ((n > (int)boost::math::max_factorial<T>::value) && (T(n) * n > tools::log_max_value<T>()))
0083 ? T(0) : static_cast<T>(boost::math::factorial<T>(n - 1, pol) * pow(x, T(-n - 1)));
0084 if(part_term == 0)
0085 {
0086
0087
0088 part_term = static_cast<T>(T(boost::math::lgamma(n, pol)) - (n + 1) * log(x));
0089 sum = exp(part_term + log(n + 2 * x) - boost::math::constants::ln_two<T>());
0090 part_term += log(T(n) * (n + 1)) - boost::math::constants::ln_two<T>() - log(x);
0091 part_term = exp(part_term);
0092 }
0093 else
0094 {
0095 sum = part_term * (n + 2 * x) / 2;
0096 part_term *= (T(n) * (n + 1)) / 2;
0097 part_term /= x;
0098 }
0099
0100
0101
0102 if(sum == 0)
0103 return sum;
0104
0105 for(unsigned k = 1;;)
0106 {
0107 term = part_term * boost::math::bernoulli_b2n<T>(k, pol);
0108 sum += term;
0109
0110
0111
0112 if(fabs(term / sum) < tools::epsilon<T>())
0113 break;
0114
0115
0116
0117 ++k;
0118 part_term *= T(n + 2 * k - 2) * (n - 1 + 2 * k);
0119 part_term /= (2 * k - 1) * 2 * k;
0120 part_term /= x_squared;
0121
0122
0123
0124 if(k > policies::get_max_series_iterations<Policy>())
0125 {
0126 return policies::raise_evaluation_error(function, "Series did not converge, closest value was %1%", sum, pol);
0127 }
0128 }
0129
0130 if((n - 1) & 1)
0131 sum = -sum;
0132
0133 return sum;
0134 }
0135
0136 template<class T, class Policy>
0137 T polygamma_attransitionplus(const int n, const T& x, const Policy& pol, const char* function)
0138 {
0139
0140
0141
0142 BOOST_MATH_STD_USING
0143 const int d4d = static_cast<int>(0.4F * policies::digits_base10<T, Policy>());
0144 const int N = d4d + (4 * n);
0145 const int m = n;
0146 const int iter = N - itrunc(x);
0147
0148 if(iter > (int)policies::get_max_series_iterations<Policy>())
0149 return policies::raise_evaluation_error<T>(function, ("Exceeded maximum series evaluations evaluating at n = " + std::to_string(n) + " and x = %1%").c_str(), x, pol);
0150
0151 const int minus_m_minus_one = -m - 1;
0152
0153 T z(x);
0154 T sum0(0);
0155 T z_plus_k_pow_minus_m_minus_one(0);
0156
0157
0158 if(log(z + iter) * minus_m_minus_one > -tools::log_max_value<T>())
0159 {
0160 for(int k = 1; k <= iter; ++k)
0161 {
0162 z_plus_k_pow_minus_m_minus_one = static_cast<T>(pow(z, T(minus_m_minus_one)));
0163 sum0 += z_plus_k_pow_minus_m_minus_one;
0164 z += 1;
0165 }
0166 sum0 *= boost::math::factorial<T>(n, pol);
0167 }
0168 else
0169 {
0170 for(int k = 1; k <= iter; ++k)
0171 {
0172 T log_term = log(z) * minus_m_minus_one + boost::math::lgamma(T(n + 1), pol);
0173 sum0 += exp(log_term);
0174 z += 1;
0175 }
0176 }
0177 if((n - 1) & 1)
0178 sum0 = -sum0;
0179
0180 return sum0 + polygamma_atinfinityplus(n, z, pol, function);
0181 }
0182
0183 template <class T, class Policy>
0184 T polygamma_nearzero(int n, T x, const Policy& pol, const char* function)
0185 {
0186 BOOST_MATH_STD_USING
0187
0188
0189
0190
0191
0192
0193
0194
0195 T scale = boost::math::factorial<T>(n, pol);
0196
0197
0198
0199
0200 T factorial_part = 1;
0201
0202
0203
0204
0205
0206 T prefix = static_cast<T>(pow(x, T(n + 1)));
0207 if(prefix == 0)
0208 return boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0209 prefix = 1 / prefix;
0210
0211
0212
0213
0214 if(prefix > 2 / policies::get_epsilon<T, Policy>())
0215 return ((n & 1) ? 1 : -1) *
0216 (tools::max_value<T>() / prefix < scale ? policies::raise_overflow_error<T>(function, nullptr, pol) : prefix * scale);
0217
0218
0219
0220
0221
0222
0223
0224 T sum = prefix;
0225 for(unsigned k = 0;;)
0226 {
0227
0228 T term = factorial_part * boost::math::zeta(T(k + n + 1), pol);
0229 sum += term;
0230
0231 if(fabs(term) < fabs(sum * boost::math::policies::get_epsilon<T, Policy>()))
0232 break;
0233
0234
0235
0236 ++k;
0237 factorial_part *= (-x * (n + k)) / k;
0238
0239
0240
0241 if(k > policies::get_max_series_iterations<Policy>())
0242 return policies::raise_evaluation_error<T>(function, "Series did not converge, best value is %1%", sum, pol);
0243 }
0244
0245
0246
0247 if(boost::math::tools::max_value<T>() / scale < sum)
0248 return boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0249 sum *= scale;
0250 return n & 1 ? sum : T(-sum);
0251 }
0252
0253
0254
0255
0256
0257 template <class Table>
0258 typename Table::value_type::reference dereference_table(Table& table, unsigned row, unsigned power)
0259 {
0260 return table[row][power / 2];
0261 }
0262
0263
0264
0265 template <class T, class Policy>
0266 T poly_cot_pi(int n, T x, T xc, const Policy& pol, const char* function)
0267 {
0268 BOOST_MATH_STD_USING
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 T s = fabs(x) < fabs(xc) ? boost::math::sin_pi(x, pol) : boost::math::sin_pi(xc, pol);
0297 T c = boost::math::cos_pi(x, pol);
0298 switch(n)
0299 {
0300 case 1:
0301 return -constants::pi<T, Policy>() / (s * s);
0302 case 2:
0303 {
0304 return 2 * constants::pi<T, Policy>() * constants::pi<T, Policy>() * c / boost::math::pow<3>(s, pol);
0305 }
0306 case 3:
0307 {
0308 constexpr int P[] = { -2, -4 };
0309 return boost::math::pow<3>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<4>(s, pol);
0310 }
0311 case 4:
0312 {
0313 constexpr int P[] = { 16, 8 };
0314 return boost::math::pow<4>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<5>(s, pol);
0315 }
0316 case 5:
0317 {
0318 constexpr int P[] = { -16, -88, -16 };
0319 return boost::math::pow<5>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<6>(s, pol);
0320 }
0321 case 6:
0322 {
0323 constexpr int P[] = { 272, 416, 32 };
0324 return boost::math::pow<6>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<7>(s, pol);
0325 }
0326 case 7:
0327 {
0328 constexpr int P[] = { -272, -2880, -1824, -64 };
0329 return boost::math::pow<7>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<8>(s, pol);
0330 }
0331 case 8:
0332 {
0333 constexpr int P[] = { 7936, 24576, 7680, 128 };
0334 return boost::math::pow<8>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<9>(s, pol);
0335 }
0336 case 9:
0337 {
0338 constexpr int P[] = { -7936, -137216, -185856, -31616, -256 };
0339 return boost::math::pow<9>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<10>(s, pol);
0340 }
0341 case 10:
0342 {
0343 constexpr int P[] = { 353792, 1841152, 1304832, 128512, 512 };
0344 return boost::math::pow<10>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<11>(s, pol);
0345 }
0346 case 11:
0347 {
0348 constexpr int P[] = { -353792, -9061376, -21253376, -8728576, -518656, -1024};
0349 return boost::math::pow<11>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<12>(s, pol);
0350 }
0351 case 12:
0352 {
0353 constexpr int P[] = { 22368256, 175627264, 222398464, 56520704, 2084864, 2048 };
0354 return boost::math::pow<12>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<13>(s, pol);
0355 }
0356 #ifndef BOOST_NO_LONG_LONG
0357 case 13:
0358 {
0359 constexpr long long P[] = { -22368256LL, -795300864LL, -2868264960LL, -2174832640LL, -357888000LL, -8361984LL, -4096 };
0360 return boost::math::pow<13>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<14>(s, pol);
0361 }
0362 case 14:
0363 {
0364 constexpr long long P[] = { 1903757312LL, 21016670208LL, 41731645440LL, 20261765120LL, 2230947840LL, 33497088LL, 8192 };
0365 return boost::math::pow<14>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<15>(s, pol);
0366 }
0367 case 15:
0368 {
0369 constexpr long long P[] = { -1903757312LL, -89702612992LL, -460858269696LL, -559148810240LL, -182172651520LL, -13754155008LL, -134094848LL, -16384 };
0370 return boost::math::pow<15>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<16>(s, pol);
0371 }
0372 case 16:
0373 {
0374 constexpr long long P[] = { 209865342976LL, 3099269660672LL, 8885192097792LL, 7048869314560LL, 1594922762240LL, 84134068224LL, 536608768LL, 32768 };
0375 return boost::math::pow<16>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<17>(s, pol);
0376 }
0377 case 17:
0378 {
0379 constexpr long long P[] = { -209865342976LL, -12655654469632LL, -87815735738368LL, -155964390375424LL, -84842998005760LL, -13684856848384LL, -511780323328LL, -2146926592LL, -65536 };
0380 return boost::math::pow<17>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<18>(s, pol);
0381 }
0382 case 18:
0383 {
0384 constexpr long long P[] = { 29088885112832LL, 553753414467584LL, 2165206642589696LL, 2550316668551168LL, 985278548541440LL, 115620218667008LL, 3100738912256LL, 8588754944LL, 131072 };
0385 return boost::math::pow<18>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<19>(s, pol);
0386 }
0387 case 19:
0388 {
0389 constexpr long long P[] = { -29088885112832LL, -2184860175433728LL, -19686087844429824LL, -48165109676113920LL, -39471306959486976LL, -11124607890751488LL, -965271355195392LL, -18733264797696LL, -34357248000LL, -262144 };
0390 return boost::math::pow<19>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<20>(s, pol);
0391 }
0392 case 20:
0393 {
0394 constexpr long long P[] = { 4951498053124096LL, 118071834535526400LL, 603968063567560704LL, 990081991141490688LL, 584901762421358592LL, 122829335169859584LL, 7984436548730880LL, 112949304754176LL, 137433710592LL, 524288 };
0395 return boost::math::pow<20>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<21>(s, pol);
0396 }
0397 #endif
0398 }
0399
0400
0401
0402
0403
0404
0405
0406
0407 if((unsigned)n / 2u > policies::get_max_series_iterations<Policy>())
0408 return policies::raise_evaluation_error<T>(function, "The value of n is so large that we're unable to compute the result in reasonable time, best guess is %1%", 0, pol);
0409 #ifdef BOOST_HAS_THREADS
0410 static std::mutex m;
0411 std::lock_guard<std::mutex> l(m);
0412 #endif
0413 static int digits = tools::digits<T>();
0414 static std::vector<std::vector<T> > table(1, std::vector<T>(1, T(-1)));
0415
0416 int current_digits = tools::digits<T>();
0417
0418 if(digits != current_digits)
0419 {
0420
0421 table = std::vector<std::vector<T> >(1, std::vector<T>(1, T(-1)));
0422 digits = current_digits;
0423 }
0424
0425 int index = n - 1;
0426
0427 if(index >= (int)table.size())
0428 {
0429 for(int i = (int)table.size() - 1; i < index; ++i)
0430 {
0431 int offset = i & 1;
0432 int sin_order = i + 2;
0433 int max_cos_order = sin_order - 1;
0434 int max_columns = (max_cos_order - offset) / 2;
0435 int next_offset = offset ? 0 : 1;
0436 int next_max_columns = (max_cos_order + 1 - next_offset) / 2;
0437 table.push_back(std::vector<T>(next_max_columns + 1, T(0)));
0438
0439 for(int column = 0; column <= max_columns; ++column)
0440 {
0441 int cos_order = 2 * column + offset;
0442 BOOST_MATH_ASSERT(column < (int)table[i].size());
0443 BOOST_MATH_ASSERT((cos_order + 1) / 2 < (int)table[i + 1].size());
0444 table[i + 1][(cos_order + 1) / 2] += ((cos_order - sin_order) * table[i][column]) / (sin_order - 1);
0445 if(cos_order)
0446 table[i + 1][(cos_order - 1) / 2] += (-cos_order * table[i][column]) / (sin_order - 1);
0447 }
0448 }
0449
0450 }
0451 T sum = boost::math::tools::evaluate_even_polynomial(&table[index][0], c, table[index].size());
0452 if(index & 1)
0453 sum *= c;
0454 if(sum == 0)
0455 return sum;
0456
0457
0458
0459
0460 T power_terms = n * log(boost::math::constants::pi<T>());
0461 if(s == 0)
0462 return sum * boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0463 power_terms -= log(fabs(s)) * (n + 1);
0464 power_terms += boost::math::lgamma(T(n), pol);
0465 power_terms += log(fabs(sum));
0466
0467 if(power_terms > boost::math::tools::log_max_value<T>())
0468 return sum * boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0469
0470 return exp(power_terms) * ((s < 0) && ((n + 1) & 1) ? -1 : 1) * boost::math::sign(sum);
0471 }
0472
0473 template <class T, class Policy>
0474 struct polygamma_initializer
0475 {
0476 struct init
0477 {
0478 init()
0479 {
0480
0481 boost::math::polygamma(30, T(-2.5f), Policy());
0482 }
0483 void force_instantiate()const{}
0484 };
0485 static const init initializer;
0486 static void force_instantiate()
0487 {
0488 initializer.force_instantiate();
0489 }
0490 };
0491
0492 template <class T, class Policy>
0493 const typename polygamma_initializer<T, Policy>::init polygamma_initializer<T, Policy>::initializer;
0494
0495 template<class T, class Policy>
0496 inline T polygamma_imp(const int n, T x, const Policy &pol)
0497 {
0498 BOOST_MATH_STD_USING
0499 static const char* function = "boost::math::polygamma<%1%>(int, %1%)";
0500 polygamma_initializer<T, Policy>::initializer.force_instantiate();
0501 if(n < 0)
0502 return policies::raise_domain_error<T>(function, "Order must be >= 0, but got %1%", static_cast<T>(n), pol);
0503 if(x < 0)
0504 {
0505 if(floor(x) == x)
0506 {
0507
0508
0509
0510 if(lltrunc(x) & 1)
0511 return policies::raise_overflow_error<T>(function, nullptr, pol);
0512 else
0513 return policies::raise_pole_error<T>(function, "Evaluation at negative integer %1%", x, pol);
0514 }
0515 T z = 1 - x;
0516 T result = polygamma_imp(n, z, pol) + constants::pi<T, Policy>() * poly_cot_pi(n, z, x, pol, function);
0517 return n & 1 ? T(-result) : result;
0518 }
0519
0520
0521
0522
0523
0524
0525
0526 T small_x_limit = (std::min)(T(T(5) / n), T(0.25f));
0527 if(x < small_x_limit)
0528 {
0529 return polygamma_nearzero(n, x, pol, function);
0530 }
0531 else if(x > 0.4F * policies::digits_base10<T, Policy>() + 4.0f * n)
0532 {
0533 return polygamma_atinfinityplus(n, x, pol, function);
0534 }
0535 else if(x == 1)
0536 {
0537 return (n & 1 ? 1 : -1) * boost::math::factorial<T>(n, pol) * boost::math::zeta(T(n + 1), pol);
0538 }
0539 else if(x == 0.5f)
0540 {
0541 T result = (n & 1 ? 1 : -1) * boost::math::factorial<T>(n, pol) * boost::math::zeta(T(n + 1), pol);
0542 if(fabs(result) >= ldexp(tools::max_value<T>(), -n - 1))
0543 return boost::math::sign(result) * policies::raise_overflow_error<T>(function, nullptr, pol);
0544 result *= ldexp(T(1), n + 1) - 1;
0545 return result;
0546 }
0547 else
0548 {
0549 return polygamma_attransitionplus(n, x, pol, function);
0550 }
0551 }
0552
0553 } } }
0554
0555 #ifdef _MSC_VER
0556 #pragma warning(pop)
0557 #endif
0558
0559 #endif
0560