Chris@102: Chris@102: /////////////////////////////////////////////////////////////////////////////// Chris@102: // Copyright 2013 Nikhar Agrawal Chris@102: // Copyright 2013 Christopher Kormanyos Chris@102: // Copyright 2014 John Maddock Chris@102: // Copyright 2013 Paul Bristow Chris@102: // Distributed under the Boost Chris@102: // Software License, Version 1.0. (See accompanying file Chris@102: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@102: Chris@102: #ifndef _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_ Chris@102: #define _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_ Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: namespace boost { namespace math { namespace detail{ Chris@102: Chris@102: template Chris@102: 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 Chris@102: { Chris@102: // See http://functions.wolfram.com/GammaBetaErf/PolyGamma2/06/02/0001/ Chris@102: BOOST_MATH_STD_USING Chris@102: // Chris@102: // sum == current value of accumulated sum. Chris@102: // term == value of current term to be added to sum. Chris@102: // part_term == value of current term excluding the Bernoulli number part Chris@102: // Chris@102: if(n + x == x) Chris@102: { Chris@102: // x is crazy large, just concentrate on the first part of the expression and use logs: Chris@102: if(n == 1) return 1 / x; Chris@102: T nlx = n * log(x); Chris@102: if((nlx < tools::log_max_value()) && (n < max_factorial::value)) Chris@102: return ((n & 1) ? 1 : -1) * boost::math::factorial(n - 1) * pow(x, -n); Chris@102: else Chris@102: return ((n & 1) ? 1 : -1) * exp(boost::math::lgamma(T(n), pol) - n * log(x)); Chris@102: } Chris@102: T term, sum, part_term; Chris@102: T x_squared = x * x; Chris@102: // Chris@102: // Start by setting part_term to: Chris@102: // Chris@102: // (n-1)! / x^(n+1) Chris@102: // Chris@102: // which is common to both the first term of the series (with k = 1) Chris@102: // and to the leading part. Chris@102: // We can then get to the leading term by: Chris@102: // Chris@102: // part_term * (n + 2 * x) / 2 Chris@102: // Chris@102: // and to the first term in the series Chris@102: // (excluding the Bernoulli number) by: Chris@102: // Chris@102: // part_term n * (n + 1) / (2x) Chris@102: // Chris@102: // If either the factorial would overflow, Chris@102: // or the power term underflows, this just gets set to 0 and then we Chris@102: // know that we have to use logs for the initial terms: Chris@102: // Chris@102: part_term = ((n > boost::math::max_factorial::value) && (T(n) * n > tools::log_max_value())) Chris@102: ? T(0) : static_cast(boost::math::factorial(n - 1, pol) * pow(x, -n - 1)); Chris@102: if(part_term == 0) Chris@102: { Chris@102: // Either n is very large, or the power term underflows, Chris@102: // set the initial values of part_term, term and sum via logs: Chris@102: part_term = boost::math::lgamma(n, pol) - (n + 1) * log(x); Chris@102: sum = exp(part_term + log(n + 2 * x) - boost::math::constants::ln_two()); Chris@102: part_term += log(T(n) * (n + 1)) - boost::math::constants::ln_two() - log(x); Chris@102: part_term = exp(part_term); Chris@102: } Chris@102: else Chris@102: { Chris@102: sum = part_term * (n + 2 * x) / 2; Chris@102: part_term *= (T(n) * (n + 1)) / 2; Chris@102: part_term /= x; Chris@102: } Chris@102: // Chris@102: // If the leading term is 0, so is the result: Chris@102: // Chris@102: if(sum == 0) Chris@102: return sum; Chris@102: Chris@102: for(unsigned k = 1;;) Chris@102: { Chris@102: term = part_term * boost::math::bernoulli_b2n(k, pol); Chris@102: sum += term; Chris@102: // Chris@102: // Normal termination condition: Chris@102: // Chris@102: if(fabs(term / sum) < tools::epsilon()) Chris@102: break; Chris@102: // Chris@102: // Increment our counter, and move part_term on to the next value: Chris@102: // Chris@102: ++k; Chris@102: part_term *= T(n + 2 * k - 2) * (n - 1 + 2 * k); Chris@102: part_term /= (2 * k - 1) * 2 * k; Chris@102: part_term /= x_squared; Chris@102: // Chris@102: // Emergency get out termination condition: Chris@102: // Chris@102: if(k > policies::get_max_series_iterations()) Chris@102: { Chris@102: return policies::raise_evaluation_error(function, "Series did not converge, closest value was %1%", sum, pol); Chris@102: } Chris@102: } Chris@102: Chris@102: if((n - 1) & 1) Chris@102: sum = -sum; Chris@102: Chris@102: return sum; Chris@102: } Chris@102: Chris@102: template Chris@102: T polygamma_attransitionplus(const int n, const T& x, const Policy& pol, const char* function) Chris@102: { Chris@102: // See: http://functions.wolfram.com/GammaBetaErf/PolyGamma2/16/01/01/0017/ Chris@102: Chris@102: // Use N = (0.4 * digits) + (4 * n) for target value for x: Chris@102: BOOST_MATH_STD_USING Chris@102: const int d4d = static_cast(0.4F * policies::digits_base10()); Chris@102: const int N = d4d + (4 * n); Chris@102: const int m = n; Chris@102: const int iter = N - itrunc(x); Chris@102: Chris@102: if(iter > (int)policies::get_max_series_iterations()) Chris@102: return policies::raise_evaluation_error(function, ("Exceeded maximum series evaluations evaluating at n = " + boost::lexical_cast(n) + " and x = %1%").c_str(), x, pol); Chris@102: Chris@102: const int minus_m_minus_one = -m - 1; Chris@102: Chris@102: T z(x); Chris@102: T sum0(0); Chris@102: T z_plus_k_pow_minus_m_minus_one(0); Chris@102: Chris@102: // Forward recursion to larger x, need to check for overflow first though: Chris@102: if(log(z + iter) * minus_m_minus_one > -tools::log_max_value()) Chris@102: { Chris@102: for(int k = 1; k <= iter; ++k) Chris@102: { Chris@102: z_plus_k_pow_minus_m_minus_one = pow(z, minus_m_minus_one); Chris@102: sum0 += z_plus_k_pow_minus_m_minus_one; Chris@102: z += 1; Chris@102: } Chris@102: sum0 *= boost::math::factorial(n); Chris@102: } Chris@102: else Chris@102: { Chris@102: for(int k = 1; k <= iter; ++k) Chris@102: { Chris@102: T log_term = log(z) * minus_m_minus_one + boost::math::lgamma(T(n + 1), pol); Chris@102: sum0 += exp(log_term); Chris@102: z += 1; Chris@102: } Chris@102: } Chris@102: if((n - 1) & 1) Chris@102: sum0 = -sum0; Chris@102: Chris@102: return sum0 + polygamma_atinfinityplus(n, z, pol, function); Chris@102: } Chris@102: Chris@102: template Chris@102: T polygamma_nearzero(int n, T x, const Policy& pol, const char* function) Chris@102: { Chris@102: BOOST_MATH_STD_USING Chris@102: // Chris@102: // If we take this expansion for polygamma: http://functions.wolfram.com/06.15.06.0003.02 Chris@102: // and substitute in this expression for polygamma(n, 1): http://functions.wolfram.com/06.15.03.0009.01 Chris@102: // we get an alternating series for polygamma when x is small in terms of zeta functions of Chris@102: // integer arguments (which are easy to evaluate, at least when the integer is even). Chris@102: // Chris@102: // In order to avoid spurious overflow, save the n! term for later, and rescale at the end: Chris@102: // Chris@102: T scale = boost::math::factorial(n, pol); Chris@102: // Chris@102: // "factorial_part" contains everything except the zeta function Chris@102: // evaluations in each term: Chris@102: // Chris@102: T factorial_part = 1; Chris@102: // Chris@102: // "prefix" is what we'll be adding the accumulated sum to, it will Chris@102: // be n! / z^(n+1), but since we're scaling by n! it's just Chris@102: // 1 / z^(n+1) for now: Chris@102: // Chris@102: T prefix = pow(x, n + 1); Chris@102: if(prefix == 0) Chris@102: return boost::math::policies::raise_overflow_error(function, 0, pol); Chris@102: prefix = 1 / prefix; Chris@102: // Chris@102: // First term in the series is necessarily < zeta(2) < 2, so Chris@102: // ignore the sum if it will have no effect on the result anyway: Chris@102: // Chris@102: if(prefix > 2 / policies::get_epsilon()) Chris@102: return ((n & 1) ? 1 : -1) * Chris@102: (tools::max_value() / prefix < scale ? policies::raise_overflow_error(function, 0, pol) : prefix * scale); Chris@102: // Chris@102: // As this is an alternating series we could accelerate it using Chris@102: // "Convergence Acceleration of Alternating Series", Chris@102: // Henri Cohen, Fernando Rodriguez Villegas, and Don Zagier, Experimental Mathematics, 1999. Chris@102: // In practice however, it appears not to make any difference to the number of terms Chris@102: // required except in some edge cases which are filtered out anyway before we get here. Chris@102: // Chris@102: T sum = prefix; Chris@102: for(unsigned k = 0;;) Chris@102: { Chris@102: // Get the k'th term: Chris@102: T term = factorial_part * boost::math::zeta(T(k + n + 1), pol); Chris@102: sum += term; Chris@102: // Termination condition: Chris@102: if(fabs(term) < fabs(sum * boost::math::policies::get_epsilon())) Chris@102: break; Chris@102: // Chris@102: // Move on k and factorial_part: Chris@102: // Chris@102: ++k; Chris@102: factorial_part *= (-x * (n + k)) / k; Chris@102: // Chris@102: // Last chance exit: Chris@102: // Chris@102: if(k > policies::get_max_series_iterations()) Chris@102: return policies::raise_evaluation_error(function, "Series did not converge, best value is %1%", sum, pol); Chris@102: } Chris@102: // Chris@102: // We need to multiply by the scale, at each stage checking for oveflow: Chris@102: // Chris@102: if(boost::math::tools::max_value() / scale < sum) Chris@102: return boost::math::policies::raise_overflow_error(function, 0, pol); Chris@102: sum *= scale; Chris@102: return n & 1 ? sum : -sum; Chris@102: } Chris@102: Chris@102: // Chris@102: // Helper function which figures out which slot our coefficient is in Chris@102: // given an angle multiplier for the cosine term of power: Chris@102: // Chris@102: template Chris@102: typename Table::value_type::reference dereference_table(Table& table, unsigned row, unsigned power) Chris@102: { Chris@102: return table[row][power / 2]; Chris@102: } Chris@102: Chris@102: Chris@102: Chris@102: template Chris@102: T poly_cot_pi(int n, T x, T xc, const Policy& pol, const char* function) Chris@102: { Chris@102: BOOST_MATH_STD_USING Chris@102: // Return n'th derivative of cot(pi*x) at x, these are simply Chris@102: // tabulated for up to n = 9, beyond that it is possible to Chris@102: // calculate coefficients as follows: Chris@102: // Chris@102: // The general form of each derivative is: Chris@102: // Chris@102: // pi^n * SUM{k=0, n} C[k,n] * cos^k(pi * x) * csc^(n+1)(pi * x) Chris@102: // Chris@102: // With constant C[0,1] = -1 and all other C[k,n] = 0; Chris@102: // Then for each k < n+1: Chris@102: // C[k-1, n+1] -= k * C[k, n]; Chris@102: // C[k+1, n+1] += (k-n-1) * C[k, n]; Chris@102: // Chris@102: // Note that there are many different ways of representing this derivative thanks to Chris@102: // the many trigomonetric identies available. In particular, the sum of powers of Chris@102: // cosines could be replaced by a sum of cosine multiple angles, and indeed if you Chris@102: // plug the derivative into Mathematica this is the form it will give. The two Chris@102: // forms are related via the Chebeshev polynomials of the first kind and Chris@102: // T_n(cos(x)) = cos(n x). The polynomial form has the great advantage that Chris@102: // all the cosine terms are zero at half integer arguments - right where this Chris@102: // function has it's minumum - thus avoiding cancellation error in this region. Chris@102: // Chris@102: // And finally, since every other term in the polynomials is zero, we can save Chris@102: // space by only storing the non-zero terms. This greatly complexifies Chris@102: // subscripting the tables in the calculation, but halves the storage space Chris@102: // (and complexity for that matter). Chris@102: // Chris@102: T s = fabs(x) < fabs(xc) ? boost::math::sin_pi(x, pol) : boost::math::sin_pi(xc, pol); Chris@102: T c = boost::math::cos_pi(x, pol); Chris@102: switch(n) Chris@102: { Chris@102: case 1: Chris@102: return -constants::pi() / (s * s); Chris@102: case 2: Chris@102: { Chris@102: return 2 * constants::pi() * constants::pi() * c / boost::math::pow<3>(s, pol); Chris@102: } Chris@102: case 3: Chris@102: { Chris@102: int P[] = { -2, -4 }; Chris@102: return boost::math::pow<3>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<4>(s, pol); Chris@102: } Chris@102: case 4: Chris@102: { Chris@102: int P[] = { 16, 8 }; Chris@102: return boost::math::pow<4>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<5>(s, pol); Chris@102: } Chris@102: case 5: Chris@102: { Chris@102: int P[] = { -16, -88, -16 }; Chris@102: return boost::math::pow<5>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<6>(s, pol); Chris@102: } Chris@102: case 6: Chris@102: { Chris@102: int P[] = { 272, 416, 32 }; Chris@102: return boost::math::pow<6>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<7>(s, pol); Chris@102: } Chris@102: case 7: Chris@102: { Chris@102: int P[] = { -272, -2880, -1824, -64 }; Chris@102: return boost::math::pow<7>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<8>(s, pol); Chris@102: } Chris@102: case 8: Chris@102: { Chris@102: int P[] = { 7936, 24576, 7680, 128 }; Chris@102: return boost::math::pow<8>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<9>(s, pol); Chris@102: } Chris@102: case 9: Chris@102: { Chris@102: int P[] = { -7936, -137216, -185856, -31616, -256 }; Chris@102: return boost::math::pow<9>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<10>(s, pol); Chris@102: } Chris@102: case 10: Chris@102: { Chris@102: int P[] = { 353792, 1841152, 1304832, 128512, 512 }; Chris@102: return boost::math::pow<10>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<11>(s, pol); Chris@102: } Chris@102: case 11: Chris@102: { Chris@102: int P[] = { -353792, -9061376, -21253376, -8728576, -518656, -1024}; Chris@102: return boost::math::pow<11>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<12>(s, pol); Chris@102: } Chris@102: case 12: Chris@102: { Chris@102: int P[] = { 22368256, 175627264, 222398464, 56520704, 2084864, 2048 }; Chris@102: return boost::math::pow<12>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<13>(s, pol); Chris@102: } Chris@102: #ifndef BOOST_NO_LONG_LONG Chris@102: case 13: Chris@102: { Chris@102: long long P[] = { -22368256LL, -795300864LL, -2868264960LL, -2174832640LL, -357888000LL, -8361984LL, -4096 }; Chris@102: return boost::math::pow<13>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<14>(s, pol); Chris@102: } Chris@102: case 14: Chris@102: { Chris@102: long long P[] = { 1903757312LL, 21016670208LL, 41731645440LL, 20261765120LL, 2230947840LL, 33497088LL, 8192 }; Chris@102: return boost::math::pow<14>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<15>(s, pol); Chris@102: } Chris@102: case 15: Chris@102: { Chris@102: long long P[] = { -1903757312LL, -89702612992LL, -460858269696LL, -559148810240LL, -182172651520LL, -13754155008LL, -134094848LL, -16384 }; Chris@102: return boost::math::pow<15>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<16>(s, pol); Chris@102: } Chris@102: case 16: Chris@102: { Chris@102: long long P[] = { 209865342976LL, 3099269660672LL, 8885192097792LL, 7048869314560LL, 1594922762240LL, 84134068224LL, 536608768LL, 32768 }; Chris@102: return boost::math::pow<16>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<17>(s, pol); Chris@102: } Chris@102: case 17: Chris@102: { Chris@102: long long P[] = { -209865342976LL, -12655654469632LL, -87815735738368LL, -155964390375424LL, -84842998005760LL, -13684856848384LL, -511780323328LL, -2146926592LL, -65536 }; Chris@102: return boost::math::pow<17>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<18>(s, pol); Chris@102: } Chris@102: case 18: Chris@102: { Chris@102: long long P[] = { 29088885112832LL, 553753414467584LL, 2165206642589696LL, 2550316668551168LL, 985278548541440LL, 115620218667008LL, 3100738912256LL, 8588754944LL, 131072 }; Chris@102: return boost::math::pow<18>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<19>(s, pol); Chris@102: } Chris@102: case 19: Chris@102: { Chris@102: long long P[] = { -29088885112832LL, -2184860175433728LL, -19686087844429824LL, -48165109676113920LL, -39471306959486976LL, -11124607890751488LL, -965271355195392LL, -18733264797696LL, -34357248000LL, -262144 }; Chris@102: return boost::math::pow<19>(constants::pi(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<20>(s, pol); Chris@102: } Chris@102: case 20: Chris@102: { Chris@102: long long P[] = { 4951498053124096LL, 118071834535526400LL, 603968063567560704LL, 990081991141490688LL, 584901762421358592LL, 122829335169859584LL, 7984436548730880LL, 112949304754176LL, 137433710592LL, 524288 }; Chris@102: return boost::math::pow<20>(constants::pi(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<21>(s, pol); Chris@102: } Chris@102: #endif Chris@102: } Chris@102: Chris@102: // Chris@102: // We'll have to compute the coefficients up to n, Chris@102: // complexity is O(n^2) which we don't worry about for now Chris@102: // as the values are computed once and then cached. Chris@102: // However, if the final evaluation would have too many Chris@102: // terms just bail out right away: Chris@102: // Chris@102: if((unsigned)n / 2u > policies::get_max_series_iterations()) Chris@102: return policies::raise_evaluation_error(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); Chris@102: #ifdef BOOST_HAS_THREADS Chris@102: static boost::detail::lightweight_mutex m; Chris@102: boost::detail::lightweight_mutex::scoped_lock l(m); Chris@102: #endif Chris@102: static std::vector > table(1, std::vector(1, T(-1))); Chris@102: Chris@102: int index = n - 1; Chris@102: Chris@102: if(index >= (int)table.size()) Chris@102: { Chris@102: for(int i = (int)table.size() - 1; i < index; ++i) Chris@102: { Chris@102: int offset = i & 1; // 1 if the first cos power is 0, otherwise 0. Chris@102: int sin_order = i + 2; // order of the sin term Chris@102: int max_cos_order = sin_order - 1; // largest order of the polynomial of cos terms Chris@102: int max_columns = (max_cos_order - offset) / 2; // How many entries there are in the current row. Chris@102: int next_offset = offset ? 0 : 1; Chris@102: int next_max_columns = (max_cos_order + 1 - next_offset) / 2; // How many entries there will be in the next row Chris@102: table.push_back(std::vector(next_max_columns + 1, T(0))); Chris@102: Chris@102: for(int column = 0; column <= max_columns; ++column) Chris@102: { Chris@102: int cos_order = 2 * column + offset; // order of the cosine term in entry "column" Chris@102: BOOST_ASSERT(column < (int)table[i].size()); Chris@102: BOOST_ASSERT((cos_order + 1) / 2 < (int)table[i + 1].size()); Chris@102: table[i + 1][(cos_order + 1) / 2] += ((cos_order - sin_order) * table[i][column]) / (sin_order - 1); Chris@102: if(cos_order) Chris@102: table[i + 1][(cos_order - 1) / 2] += (-cos_order * table[i][column]) / (sin_order - 1); Chris@102: } Chris@102: } Chris@102: Chris@102: } Chris@102: T sum = boost::math::tools::evaluate_even_polynomial(&table[index][0], c, table[index].size()); Chris@102: if(index & 1) Chris@102: sum *= c; // First coeffient is order 1, and really an odd polynomial. Chris@102: if(sum == 0) Chris@102: return sum; Chris@102: // Chris@102: // The remaining terms are computed using logs since the powers and factorials Chris@102: // get real large real quick: Chris@102: // Chris@102: T power_terms = n * log(boost::math::constants::pi()); Chris@102: if(s == 0) Chris@102: return sum * boost::math::policies::raise_overflow_error(function, 0, pol); Chris@102: power_terms -= log(fabs(s)) * (n + 1); Chris@102: power_terms += boost::math::lgamma(T(n)); Chris@102: power_terms += log(fabs(sum)); Chris@102: Chris@102: if(power_terms > boost::math::tools::log_max_value()) Chris@102: return sum * boost::math::policies::raise_overflow_error(function, 0, pol); Chris@102: Chris@102: return exp(power_terms) * ((s < 0) && ((n + 1) & 1) ? -1 : 1) * boost::math::sign(sum); Chris@102: } Chris@102: Chris@102: template Chris@102: struct polygamma_initializer Chris@102: { Chris@102: struct init Chris@102: { Chris@102: init() Chris@102: { Chris@102: // Forces initialization of our table of coefficients and mutex: Chris@102: boost::math::polygamma(30, T(-2.5f), Policy()); Chris@102: } Chris@102: void force_instantiate()const{} Chris@102: }; Chris@102: static const init initializer; Chris@102: static void force_instantiate() Chris@102: { Chris@102: initializer.force_instantiate(); Chris@102: } Chris@102: }; Chris@102: Chris@102: template Chris@102: const typename polygamma_initializer::init polygamma_initializer::initializer; Chris@102: Chris@102: template Chris@102: inline T polygamma_imp(const int n, T x, const Policy &pol) Chris@102: { Chris@102: BOOST_MATH_STD_USING Chris@102: static const char* function = "boost::math::polygamma<%1%>(int, %1%)"; Chris@102: polygamma_initializer::initializer.force_instantiate(); Chris@102: if(n < 0) Chris@102: return policies::raise_domain_error(function, "Order must be >= 0, but got %1%", static_cast(n), pol); Chris@102: if(x < 0) Chris@102: { Chris@102: if(floor(x) == x) Chris@102: { Chris@102: // Chris@102: // Result is infinity if x is odd, and a pole error if x is even. Chris@102: // Chris@102: if(lltrunc(x) & 1) Chris@102: return policies::raise_overflow_error(function, 0, pol); Chris@102: else Chris@102: return policies::raise_pole_error(function, "Evaluation at negative integer %1%", x, pol); Chris@102: } Chris@102: T z = 1 - x; Chris@102: T result = polygamma_imp(n, z, pol) + constants::pi() * poly_cot_pi(n, z, x, pol, function); Chris@102: return n & 1 ? T(-result) : result; Chris@102: } Chris@102: // Chris@102: // Limit for use of small-x-series is chosen Chris@102: // so that the series doesn't go too divergent Chris@102: // in the first few terms. Ordinarily this Chris@102: // would mean setting the limit to ~ 1 / n, Chris@102: // but we can tolerate a small amount of divergence: Chris@102: // Chris@102: T small_x_limit = std::min(T(T(5) / n), T(0.25f)); Chris@102: if(x < small_x_limit) Chris@102: { Chris@102: return polygamma_nearzero(n, x, pol, function); Chris@102: } Chris@102: else if(x > 0.4F * policies::digits_base10() + 4.0f * n) Chris@102: { Chris@102: return polygamma_atinfinityplus(n, x, pol, function); Chris@102: } Chris@102: else if(x == 1) Chris@102: { Chris@102: return (n & 1 ? 1 : -1) * boost::math::factorial(n, pol) * boost::math::zeta(T(n + 1), pol); Chris@102: } Chris@102: else if(x == 0.5f) Chris@102: { Chris@102: T result = (n & 1 ? 1 : -1) * boost::math::factorial(n, pol) * boost::math::zeta(T(n + 1), pol); Chris@102: if(fabs(result) >= ldexp(tools::max_value(), -n - 1)) Chris@102: return boost::math::sign(result) * policies::raise_overflow_error(function, 0, pol); Chris@102: result *= ldexp(T(1), n + 1) - 1; Chris@102: return result; Chris@102: } Chris@102: else Chris@102: { Chris@102: return polygamma_attransitionplus(n, x, pol, function); Chris@102: } Chris@102: } Chris@102: Chris@102: } } } // namespace boost::math::detail Chris@102: Chris@102: #endif // _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_ Chris@102: