polygamma.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2013 Nikhar Agrawal
  3. // Copyright 2013 Christopher Kormanyos
  4. // Copyright 2014 John Maddock
  5. // Copyright 2013 Paul Bristow
  6. // Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_
  10. #define _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_
  11. #include <cmath>
  12. #include <limits>
  13. #include <boost/cstdint.hpp>
  14. #include <boost/math/policies/policy.hpp>
  15. #include <boost/math/special_functions/bernoulli.hpp>
  16. #include <boost/math/special_functions/trunc.hpp>
  17. #include <boost/math/special_functions/zeta.hpp>
  18. #include <boost/math/special_functions/digamma.hpp>
  19. #include <boost/math/special_functions/sin_pi.hpp>
  20. #include <boost/math/special_functions/cos_pi.hpp>
  21. #include <boost/math/special_functions/pow.hpp>
  22. #include <boost/mpl/if.hpp>
  23. #include <boost/mpl/int.hpp>
  24. #include <boost/static_assert.hpp>
  25. #include <boost/type_traits/is_convertible.hpp>
  26. #ifdef _MSC_VER
  27. #pragma once
  28. #pragma warning(push)
  29. #pragma warning(disable:4702) // Unreachable code (release mode only warning)
  30. #endif
  31. namespace boost { namespace math { namespace detail{
  32. template<class T, class Policy>
  33. T polygamma_atinfinityplus(const int n, const T& x, const Policy& pol, const char* function) // for large values of x such as for x> 400
  34. {
  35. // See http://functions.wolfram.com/GammaBetaErf/PolyGamma2/06/02/0001/
  36. BOOST_MATH_STD_USING
  37. //
  38. // sum == current value of accumulated sum.
  39. // term == value of current term to be added to sum.
  40. // part_term == value of current term excluding the Bernoulli number part
  41. //
  42. if(n + x == x)
  43. {
  44. // x is crazy large, just concentrate on the first part of the expression and use logs:
  45. if(n == 1) return 1 / x;
  46. T nlx = n * log(x);
  47. if((nlx < tools::log_max_value<T>()) && (n < (int)max_factorial<T>::value))
  48. return ((n & 1) ? 1 : -1) * boost::math::factorial<T>(n - 1) * pow(x, -n);
  49. else
  50. return ((n & 1) ? 1 : -1) * exp(boost::math::lgamma(T(n), pol) - n * log(x));
  51. }
  52. T term, sum, part_term;
  53. T x_squared = x * x;
  54. //
  55. // Start by setting part_term to:
  56. //
  57. // (n-1)! / x^(n+1)
  58. //
  59. // which is common to both the first term of the series (with k = 1)
  60. // and to the leading part.
  61. // We can then get to the leading term by:
  62. //
  63. // part_term * (n + 2 * x) / 2
  64. //
  65. // and to the first term in the series
  66. // (excluding the Bernoulli number) by:
  67. //
  68. // part_term n * (n + 1) / (2x)
  69. //
  70. // If either the factorial would overflow,
  71. // or the power term underflows, this just gets set to 0 and then we
  72. // know that we have to use logs for the initial terms:
  73. //
  74. part_term = ((n > (int)boost::math::max_factorial<T>::value) && (T(n) * n > tools::log_max_value<T>()))
  75. ? T(0) : static_cast<T>(boost::math::factorial<T>(n - 1, pol) * pow(x, -n - 1));
  76. if(part_term == 0)
  77. {
  78. // Either n is very large, or the power term underflows,
  79. // set the initial values of part_term, term and sum via logs:
  80. part_term = static_cast<T>(boost::math::lgamma(n, pol) - (n + 1) * log(x));
  81. sum = exp(part_term + log(n + 2 * x) - boost::math::constants::ln_two<T>());
  82. part_term += log(T(n) * (n + 1)) - boost::math::constants::ln_two<T>() - log(x);
  83. part_term = exp(part_term);
  84. }
  85. else
  86. {
  87. sum = part_term * (n + 2 * x) / 2;
  88. part_term *= (T(n) * (n + 1)) / 2;
  89. part_term /= x;
  90. }
  91. //
  92. // If the leading term is 0, so is the result:
  93. //
  94. if(sum == 0)
  95. return sum;
  96. for(unsigned k = 1;;)
  97. {
  98. term = part_term * boost::math::bernoulli_b2n<T>(k, pol);
  99. sum += term;
  100. //
  101. // Normal termination condition:
  102. //
  103. if(fabs(term / sum) < tools::epsilon<T>())
  104. break;
  105. //
  106. // Increment our counter, and move part_term on to the next value:
  107. //
  108. ++k;
  109. part_term *= T(n + 2 * k - 2) * (n - 1 + 2 * k);
  110. part_term /= (2 * k - 1) * 2 * k;
  111. part_term /= x_squared;
  112. //
  113. // Emergency get out termination condition:
  114. //
  115. if(k > policies::get_max_series_iterations<Policy>())
  116. {
  117. return policies::raise_evaluation_error(function, "Series did not converge, closest value was %1%", sum, pol);
  118. }
  119. }
  120. if((n - 1) & 1)
  121. sum = -sum;
  122. return sum;
  123. }
  124. template<class T, class Policy>
  125. T polygamma_attransitionplus(const int n, const T& x, const Policy& pol, const char* function)
  126. {
  127. // See: http://functions.wolfram.com/GammaBetaErf/PolyGamma2/16/01/01/0017/
  128. // Use N = (0.4 * digits) + (4 * n) for target value for x:
  129. BOOST_MATH_STD_USING
  130. const int d4d = static_cast<int>(0.4F * policies::digits_base10<T, Policy>());
  131. const int N = d4d + (4 * n);
  132. const int m = n;
  133. const int iter = N - itrunc(x);
  134. if(iter > (int)policies::get_max_series_iterations<Policy>())
  135. return policies::raise_evaluation_error<T>(function, ("Exceeded maximum series evaluations evaluating at n = " + boost::lexical_cast<std::string>(n) + " and x = %1%").c_str(), x, pol);
  136. const int minus_m_minus_one = -m - 1;
  137. T z(x);
  138. T sum0(0);
  139. T z_plus_k_pow_minus_m_minus_one(0);
  140. // Forward recursion to larger x, need to check for overflow first though:
  141. if(log(z + iter) * minus_m_minus_one > -tools::log_max_value<T>())
  142. {
  143. for(int k = 1; k <= iter; ++k)
  144. {
  145. z_plus_k_pow_minus_m_minus_one = pow(z, minus_m_minus_one);
  146. sum0 += z_plus_k_pow_minus_m_minus_one;
  147. z += 1;
  148. }
  149. sum0 *= boost::math::factorial<T>(n);
  150. }
  151. else
  152. {
  153. for(int k = 1; k <= iter; ++k)
  154. {
  155. T log_term = log(z) * minus_m_minus_one + boost::math::lgamma(T(n + 1), pol);
  156. sum0 += exp(log_term);
  157. z += 1;
  158. }
  159. }
  160. if((n - 1) & 1)
  161. sum0 = -sum0;
  162. return sum0 + polygamma_atinfinityplus(n, z, pol, function);
  163. }
  164. template <class T, class Policy>
  165. T polygamma_nearzero(int n, T x, const Policy& pol, const char* function)
  166. {
  167. BOOST_MATH_STD_USING
  168. //
  169. // If we take this expansion for polygamma: http://functions.wolfram.com/06.15.06.0003.02
  170. // and substitute in this expression for polygamma(n, 1): http://functions.wolfram.com/06.15.03.0009.01
  171. // we get an alternating series for polygamma when x is small in terms of zeta functions of
  172. // integer arguments (which are easy to evaluate, at least when the integer is even).
  173. //
  174. // In order to avoid spurious overflow, save the n! term for later, and rescale at the end:
  175. //
  176. T scale = boost::math::factorial<T>(n, pol);
  177. //
  178. // "factorial_part" contains everything except the zeta function
  179. // evaluations in each term:
  180. //
  181. T factorial_part = 1;
  182. //
  183. // "prefix" is what we'll be adding the accumulated sum to, it will
  184. // be n! / z^(n+1), but since we're scaling by n! it's just
  185. // 1 / z^(n+1) for now:
  186. //
  187. T prefix = pow(x, n + 1);
  188. if(prefix == 0)
  189. return boost::math::policies::raise_overflow_error<T>(function, 0, pol);
  190. prefix = 1 / prefix;
  191. //
  192. // First term in the series is necessarily < zeta(2) < 2, so
  193. // ignore the sum if it will have no effect on the result anyway:
  194. //
  195. if(prefix > 2 / policies::get_epsilon<T, Policy>())
  196. return ((n & 1) ? 1 : -1) *
  197. (tools::max_value<T>() / prefix < scale ? policies::raise_overflow_error<T>(function, 0, pol) : prefix * scale);
  198. //
  199. // As this is an alternating series we could accelerate it using
  200. // "Convergence Acceleration of Alternating Series",
  201. // Henri Cohen, Fernando Rodriguez Villegas, and Don Zagier, Experimental Mathematics, 1999.
  202. // In practice however, it appears not to make any difference to the number of terms
  203. // required except in some edge cases which are filtered out anyway before we get here.
  204. //
  205. T sum = prefix;
  206. for(unsigned k = 0;;)
  207. {
  208. // Get the k'th term:
  209. T term = factorial_part * boost::math::zeta(T(k + n + 1), pol);
  210. sum += term;
  211. // Termination condition:
  212. if(fabs(term) < fabs(sum * boost::math::policies::get_epsilon<T, Policy>()))
  213. break;
  214. //
  215. // Move on k and factorial_part:
  216. //
  217. ++k;
  218. factorial_part *= (-x * (n + k)) / k;
  219. //
  220. // Last chance exit:
  221. //
  222. if(k > policies::get_max_series_iterations<Policy>())
  223. return policies::raise_evaluation_error<T>(function, "Series did not converge, best value is %1%", sum, pol);
  224. }
  225. //
  226. // We need to multiply by the scale, at each stage checking for oveflow:
  227. //
  228. if(boost::math::tools::max_value<T>() / scale < sum)
  229. return boost::math::policies::raise_overflow_error<T>(function, 0, pol);
  230. sum *= scale;
  231. return n & 1 ? sum : T(-sum);
  232. }
  233. //
  234. // Helper function which figures out which slot our coefficient is in
  235. // given an angle multiplier for the cosine term of power:
  236. //
  237. template <class Table>
  238. typename Table::value_type::reference dereference_table(Table& table, unsigned row, unsigned power)
  239. {
  240. return table[row][power / 2];
  241. }
  242. template <class T, class Policy>
  243. T poly_cot_pi(int n, T x, T xc, const Policy& pol, const char* function)
  244. {
  245. BOOST_MATH_STD_USING
  246. // Return n'th derivative of cot(pi*x) at x, these are simply
  247. // tabulated for up to n = 9, beyond that it is possible to
  248. // calculate coefficients as follows:
  249. //
  250. // The general form of each derivative is:
  251. //
  252. // pi^n * SUM{k=0, n} C[k,n] * cos^k(pi * x) * csc^(n+1)(pi * x)
  253. //
  254. // With constant C[0,1] = -1 and all other C[k,n] = 0;
  255. // Then for each k < n+1:
  256. // C[k-1, n+1] -= k * C[k, n];
  257. // C[k+1, n+1] += (k-n-1) * C[k, n];
  258. //
  259. // Note that there are many different ways of representing this derivative thanks to
  260. // the many trigomonetric identies available. In particular, the sum of powers of
  261. // cosines could be replaced by a sum of cosine multiple angles, and indeed if you
  262. // plug the derivative into Mathematica this is the form it will give. The two
  263. // forms are related via the Chebeshev polynomials of the first kind and
  264. // T_n(cos(x)) = cos(n x). The polynomial form has the great advantage that
  265. // all the cosine terms are zero at half integer arguments - right where this
  266. // function has it's minumum - thus avoiding cancellation error in this region.
  267. //
  268. // And finally, since every other term in the polynomials is zero, we can save
  269. // space by only storing the non-zero terms. This greatly complexifies
  270. // subscripting the tables in the calculation, but halves the storage space
  271. // (and complexity for that matter).
  272. //
  273. T s = fabs(x) < fabs(xc) ? boost::math::sin_pi(x, pol) : boost::math::sin_pi(xc, pol);
  274. T c = boost::math::cos_pi(x, pol);
  275. switch(n)
  276. {
  277. case 1:
  278. return -constants::pi<T, Policy>() / (s * s);
  279. case 2:
  280. {
  281. return 2 * constants::pi<T, Policy>() * constants::pi<T, Policy>() * c / boost::math::pow<3>(s, pol);
  282. }
  283. case 3:
  284. {
  285. int P[] = { -2, -4 };
  286. return boost::math::pow<3>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<4>(s, pol);
  287. }
  288. case 4:
  289. {
  290. int P[] = { 16, 8 };
  291. return boost::math::pow<4>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<5>(s, pol);
  292. }
  293. case 5:
  294. {
  295. int P[] = { -16, -88, -16 };
  296. return boost::math::pow<5>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<6>(s, pol);
  297. }
  298. case 6:
  299. {
  300. int P[] = { 272, 416, 32 };
  301. return boost::math::pow<6>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<7>(s, pol);
  302. }
  303. case 7:
  304. {
  305. int P[] = { -272, -2880, -1824, -64 };
  306. return boost::math::pow<7>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<8>(s, pol);
  307. }
  308. case 8:
  309. {
  310. int P[] = { 7936, 24576, 7680, 128 };
  311. return boost::math::pow<8>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<9>(s, pol);
  312. }
  313. case 9:
  314. {
  315. int P[] = { -7936, -137216, -185856, -31616, -256 };
  316. return boost::math::pow<9>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<10>(s, pol);
  317. }
  318. case 10:
  319. {
  320. int P[] = { 353792, 1841152, 1304832, 128512, 512 };
  321. return boost::math::pow<10>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<11>(s, pol);
  322. }
  323. case 11:
  324. {
  325. int P[] = { -353792, -9061376, -21253376, -8728576, -518656, -1024};
  326. return boost::math::pow<11>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<12>(s, pol);
  327. }
  328. case 12:
  329. {
  330. int P[] = { 22368256, 175627264, 222398464, 56520704, 2084864, 2048 };
  331. return boost::math::pow<12>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<13>(s, pol);
  332. }
  333. #ifndef BOOST_NO_LONG_LONG
  334. case 13:
  335. {
  336. long long P[] = { -22368256LL, -795300864LL, -2868264960LL, -2174832640LL, -357888000LL, -8361984LL, -4096 };
  337. return boost::math::pow<13>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<14>(s, pol);
  338. }
  339. case 14:
  340. {
  341. long long P[] = { 1903757312LL, 21016670208LL, 41731645440LL, 20261765120LL, 2230947840LL, 33497088LL, 8192 };
  342. return boost::math::pow<14>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<15>(s, pol);
  343. }
  344. case 15:
  345. {
  346. long long P[] = { -1903757312LL, -89702612992LL, -460858269696LL, -559148810240LL, -182172651520LL, -13754155008LL, -134094848LL, -16384 };
  347. return boost::math::pow<15>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<16>(s, pol);
  348. }
  349. case 16:
  350. {
  351. long long P[] = { 209865342976LL, 3099269660672LL, 8885192097792LL, 7048869314560LL, 1594922762240LL, 84134068224LL, 536608768LL, 32768 };
  352. return boost::math::pow<16>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<17>(s, pol);
  353. }
  354. case 17:
  355. {
  356. long long P[] = { -209865342976LL, -12655654469632LL, -87815735738368LL, -155964390375424LL, -84842998005760LL, -13684856848384LL, -511780323328LL, -2146926592LL, -65536 };
  357. return boost::math::pow<17>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<18>(s, pol);
  358. }
  359. case 18:
  360. {
  361. long long P[] = { 29088885112832LL, 553753414467584LL, 2165206642589696LL, 2550316668551168LL, 985278548541440LL, 115620218667008LL, 3100738912256LL, 8588754944LL, 131072 };
  362. return boost::math::pow<18>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<19>(s, pol);
  363. }
  364. case 19:
  365. {
  366. long long P[] = { -29088885112832LL, -2184860175433728LL, -19686087844429824LL, -48165109676113920LL, -39471306959486976LL, -11124607890751488LL, -965271355195392LL, -18733264797696LL, -34357248000LL, -262144 };
  367. return boost::math::pow<19>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<20>(s, pol);
  368. }
  369. case 20:
  370. {
  371. long long P[] = { 4951498053124096LL, 118071834535526400LL, 603968063567560704LL, 990081991141490688LL, 584901762421358592LL, 122829335169859584LL, 7984436548730880LL, 112949304754176LL, 137433710592LL, 524288 };
  372. return boost::math::pow<20>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<21>(s, pol);
  373. }
  374. #endif
  375. }
  376. //
  377. // We'll have to compute the coefficients up to n,
  378. // complexity is O(n^2) which we don't worry about for now
  379. // as the values are computed once and then cached.
  380. // However, if the final evaluation would have too many
  381. // terms just bail out right away:
  382. //
  383. if((unsigned)n / 2u > policies::get_max_series_iterations<Policy>())
  384. 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);
  385. #ifdef BOOST_HAS_THREADS
  386. static boost::detail::lightweight_mutex m;
  387. boost::detail::lightweight_mutex::scoped_lock l(m);
  388. #endif
  389. static int digits = tools::digits<T>();
  390. static std::vector<std::vector<T> > table(1, std::vector<T>(1, T(-1)));
  391. int current_digits = tools::digits<T>();
  392. if(digits != current_digits)
  393. {
  394. // Oh my... our precision has changed!
  395. table = std::vector<std::vector<T> >(1, std::vector<T>(1, T(-1)));
  396. digits = current_digits;
  397. }
  398. int index = n - 1;
  399. if(index >= (int)table.size())
  400. {
  401. for(int i = (int)table.size() - 1; i < index; ++i)
  402. {
  403. int offset = i & 1; // 1 if the first cos power is 0, otherwise 0.
  404. int sin_order = i + 2; // order of the sin term
  405. int max_cos_order = sin_order - 1; // largest order of the polynomial of cos terms
  406. int max_columns = (max_cos_order - offset) / 2; // How many entries there are in the current row.
  407. int next_offset = offset ? 0 : 1;
  408. int next_max_columns = (max_cos_order + 1 - next_offset) / 2; // How many entries there will be in the next row
  409. table.push_back(std::vector<T>(next_max_columns + 1, T(0)));
  410. for(int column = 0; column <= max_columns; ++column)
  411. {
  412. int cos_order = 2 * column + offset; // order of the cosine term in entry "column"
  413. BOOST_ASSERT(column < (int)table[i].size());
  414. BOOST_ASSERT((cos_order + 1) / 2 < (int)table[i + 1].size());
  415. table[i + 1][(cos_order + 1) / 2] += ((cos_order - sin_order) * table[i][column]) / (sin_order - 1);
  416. if(cos_order)
  417. table[i + 1][(cos_order - 1) / 2] += (-cos_order * table[i][column]) / (sin_order - 1);
  418. }
  419. }
  420. }
  421. T sum = boost::math::tools::evaluate_even_polynomial(&table[index][0], c, table[index].size());
  422. if(index & 1)
  423. sum *= c; // First coeffient is order 1, and really an odd polynomial.
  424. if(sum == 0)
  425. return sum;
  426. //
  427. // The remaining terms are computed using logs since the powers and factorials
  428. // get real large real quick:
  429. //
  430. T power_terms = n * log(boost::math::constants::pi<T>());
  431. if(s == 0)
  432. return sum * boost::math::policies::raise_overflow_error<T>(function, 0, pol);
  433. power_terms -= log(fabs(s)) * (n + 1);
  434. power_terms += boost::math::lgamma(T(n));
  435. power_terms += log(fabs(sum));
  436. if(power_terms > boost::math::tools::log_max_value<T>())
  437. return sum * boost::math::policies::raise_overflow_error<T>(function, 0, pol);
  438. return exp(power_terms) * ((s < 0) && ((n + 1) & 1) ? -1 : 1) * boost::math::sign(sum);
  439. }
  440. template <class T, class Policy>
  441. struct polygamma_initializer
  442. {
  443. struct init
  444. {
  445. init()
  446. {
  447. // Forces initialization of our table of coefficients and mutex:
  448. boost::math::polygamma(30, T(-2.5f), Policy());
  449. }
  450. void force_instantiate()const{}
  451. };
  452. static const init initializer;
  453. static void force_instantiate()
  454. {
  455. initializer.force_instantiate();
  456. }
  457. };
  458. template <class T, class Policy>
  459. const typename polygamma_initializer<T, Policy>::init polygamma_initializer<T, Policy>::initializer;
  460. template<class T, class Policy>
  461. inline T polygamma_imp(const int n, T x, const Policy &pol)
  462. {
  463. BOOST_MATH_STD_USING
  464. static const char* function = "boost::math::polygamma<%1%>(int, %1%)";
  465. polygamma_initializer<T, Policy>::initializer.force_instantiate();
  466. if(n < 0)
  467. return policies::raise_domain_error<T>(function, "Order must be >= 0, but got %1%", static_cast<T>(n), pol);
  468. if(x < 0)
  469. {
  470. if(floor(x) == x)
  471. {
  472. //
  473. // Result is infinity if x is odd, and a pole error if x is even.
  474. //
  475. if(lltrunc(x) & 1)
  476. return policies::raise_overflow_error<T>(function, 0, pol);
  477. else
  478. return policies::raise_pole_error<T>(function, "Evaluation at negative integer %1%", x, pol);
  479. }
  480. T z = 1 - x;
  481. T result = polygamma_imp(n, z, pol) + constants::pi<T, Policy>() * poly_cot_pi(n, z, x, pol, function);
  482. return n & 1 ? T(-result) : result;
  483. }
  484. //
  485. // Limit for use of small-x-series is chosen
  486. // so that the series doesn't go too divergent
  487. // in the first few terms. Ordinarily this
  488. // would mean setting the limit to ~ 1 / n,
  489. // but we can tolerate a small amount of divergence:
  490. //
  491. T small_x_limit = (std::min)(T(T(5) / n), T(0.25f));
  492. if(x < small_x_limit)
  493. {
  494. return polygamma_nearzero(n, x, pol, function);
  495. }
  496. else if(x > 0.4F * policies::digits_base10<T, Policy>() + 4.0f * n)
  497. {
  498. return polygamma_atinfinityplus(n, x, pol, function);
  499. }
  500. else if(x == 1)
  501. {
  502. return (n & 1 ? 1 : -1) * boost::math::factorial<T>(n, pol) * boost::math::zeta(T(n + 1), pol);
  503. }
  504. else if(x == 0.5f)
  505. {
  506. T result = (n & 1 ? 1 : -1) * boost::math::factorial<T>(n, pol) * boost::math::zeta(T(n + 1), pol);
  507. if(fabs(result) >= ldexp(tools::max_value<T>(), -n - 1))
  508. return boost::math::sign(result) * policies::raise_overflow_error<T>(function, 0, pol);
  509. result *= ldexp(T(1), n + 1) - 1;
  510. return result;
  511. }
  512. else
  513. {
  514. return polygamma_attransitionplus(n, x, pol, function);
  515. }
  516. }
  517. } } } // namespace boost::math::detail
  518. #ifdef _MSC_VER
  519. #pragma warning(pop)
  520. #endif
  521. #endif // _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_