Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:07

0001 
0002 ///////////////////////////////////////////////////////////////////////////////
0003 //  Copyright 2013 Nikhar Agrawal
0004 //  Copyright 2013 Christopher Kormanyos
0005 //  Copyright 2014 John Maddock
0006 //  Copyright 2013 Paul Bristow
0007 //  Distributed under the Boost
0008 //  Software License, Version 1.0. (See accompanying file
0009 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
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) // Unreachable code (release mode only warning)
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) // for large values of x such as for x> 400
0042   {
0043      // See http://functions.wolfram.com/GammaBetaErf/PolyGamma2/06/02/0001/
0044      BOOST_MATH_STD_USING
0045      //
0046      // sum       == current value of accumulated sum.
0047      // term      == value of current term to be added to sum.
0048      // part_term == value of current term excluding the Bernoulli number part
0049      //
0050      if(n + x == x)
0051      {
0052         // x is crazy large, just concentrate on the first part of the expression and use logs:
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      // Start by setting part_term to:
0064      //
0065      // (n-1)! / x^(n+1)
0066      //
0067      // which is common to both the first term of the series (with k = 1)
0068      // and to the leading part.
0069      // We can then get to the leading term by:
0070      //
0071      // part_term * (n + 2 * x) / 2
0072      //
0073      // and to the first term in the series
0074      // (excluding the Bernoulli number) by:
0075      //
0076      // part_term n * (n + 1) / (2x)
0077      //
0078      // If either the factorial would overflow,
0079      // or the power term underflows, this just gets set to 0 and then we
0080      // know that we have to use logs for the initial terms:
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         // Either n is very large, or the power term underflows,
0087         // set the initial values of part_term, term and sum via logs:
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      // If the leading term is 0, so is the result:
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         // Normal termination condition:
0111         //
0112         if(fabs(term / sum) < tools::epsilon<T>())
0113            break;
0114         //
0115         // Increment our counter, and move part_term on to the next value:
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         // Emergency get out termination condition:
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     // See: http://functions.wolfram.com/GammaBetaErf/PolyGamma2/16/01/01/0017/
0140 
0141     // Use N = (0.4 * digits) + (4 * n) for target value for x:
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     // Forward recursion to larger x, need to check for overflow first though:
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      // If we take this expansion for polygamma: http://functions.wolfram.com/06.15.06.0003.02
0189      // and substitute in this expression for polygamma(n, 1): http://functions.wolfram.com/06.15.03.0009.01
0190      // we get an alternating series for polygamma when x is small in terms of zeta functions of
0191      // integer arguments (which are easy to evaluate, at least when the integer is even).
0192      //
0193      // In order to avoid spurious overflow, save the n! term for later, and rescale at the end:
0194      //
0195      T scale = boost::math::factorial<T>(n, pol);
0196      //
0197      // "factorial_part" contains everything except the zeta function
0198      // evaluations in each term:
0199      //
0200      T factorial_part = 1;
0201      //
0202      // "prefix" is what we'll be adding the accumulated sum to, it will
0203      // be n! / z^(n+1), but since we're scaling by n! it's just
0204      // 1 / z^(n+1) for now:
0205      //
0206      T prefix = static_cast<T>(pow(x, T(n + 1)));  // Warning supression: Integer power returns at least a double
0207      if(prefix == 0)
0208         return boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0209      prefix = 1 / prefix;
0210      //
0211      // First term in the series is necessarily < zeta(2) < 2, so
0212      // ignore the sum if it will have no effect on the result anyway:
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      // As this is an alternating series we could accelerate it using
0219      // "Convergence Acceleration of Alternating Series",
0220      // Henri Cohen, Fernando Rodriguez Villegas, and Don Zagier, Experimental Mathematics, 1999.
0221      // In practice however, it appears not to make any difference to the number of terms
0222      // required except in some edge cases which are filtered out anyway before we get here.
0223      //
0224      T sum = prefix;
0225      for(unsigned k = 0;;)
0226      {
0227         // Get the k'th term:
0228         T term = factorial_part * boost::math::zeta(T(k + n + 1), pol);
0229         sum += term;
0230         // Termination condition:
0231         if(fabs(term) < fabs(sum * boost::math::policies::get_epsilon<T, Policy>()))
0232            break;
0233         //
0234         // Move on k and factorial_part:
0235         //
0236         ++k;
0237         factorial_part *= (-x * (n + k)) / k;
0238         //
0239         // Last chance exit:
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      // We need to multiply by the scale, at each stage checking for overflow:
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   // Helper function which figures out which slot our coefficient is in
0255   // given an angle multiplier for the cosine term of power:
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      // Return n'th derivative of cot(pi*x) at x, these are simply
0270      // tabulated for up to n = 9, beyond that it is possible to
0271      // calculate coefficients as follows:
0272      //
0273      // The general form of each derivative is:
0274      //
0275      // pi^n * SUM{k=0, n} C[k,n] * cos^k(pi * x) * csc^(n+1)(pi * x)
0276      //
0277      // With constant C[0,1] = -1 and all other C[k,n] = 0;
0278      // Then for each k < n+1:
0279      // C[k-1, n+1]  -= k * C[k, n];
0280      // C[k+1, n+1]  += (k-n-1) * C[k, n];
0281      //
0282      // Note that there are many different ways of representing this derivative thanks to
0283      // the many trigonometric identies available.  In particular, the sum of powers of
0284      // cosines could be replaced by a sum of cosine multiple angles, and indeed if you
0285      // plug the derivative into Mathematica this is the form it will give.  The two
0286      // forms are related via the Chebeshev polynomials of the first kind and
0287      // T_n(cos(x)) = cos(n x).  The polynomial form has the great advantage that
0288      // all the cosine terms are zero at half integer arguments - right where this
0289      // function has it's minimum - thus avoiding cancellation error in this region.
0290      //
0291      // And finally, since every other term in the polynomials is zero, we can save
0292      // space by only storing the non-zero terms.  This greatly complexifies
0293      // subscripting the tables in the calculation, but halves the storage space
0294      // (and complexity for that matter).
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      // We'll have to compute the coefficients up to n,
0402      // complexity is O(n^2) which we don't worry about for now
0403      // as the values are computed once and then cached.
0404      // However, if the final evaluation would have too many
0405      // terms just bail out right away:
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         // Oh my... our precision has changed!
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; // 1 if the first cos power is 0, otherwise 0.
0432            int sin_order = i + 2;  // order of the sin term
0433            int max_cos_order = sin_order - 1;  // largest order of the polynomial of cos terms
0434            int max_columns = (max_cos_order - offset) / 2;  // How many entries there are in the current row.
0435            int next_offset = offset ? 0 : 1;
0436            int next_max_columns = (max_cos_order + 1 - next_offset) / 2;  // How many entries there will be in the next row
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;  // order of the cosine term in entry "column"
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;  // First coefficient is order 1, and really an odd polynomial.
0454      if(sum == 0)
0455         return sum;
0456      //
0457      // The remaining terms are computed using logs since the powers and factorials
0458      // get real large real quick:
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            // Forces initialization of our table of coefficients and mutex:
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           // Result is infinity if x is odd, and a pole error if x is even.
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     // Limit for use of small-x-series is chosen
0521     // so that the series doesn't go too divergent
0522     // in the first few terms.  Ordinarily this
0523     // would mean setting the limit to ~ 1 / n,
0524     // but we can tolerate a small amount of divergence:
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 } } } // namespace boost::math::detail
0554 
0555 #ifdef _MSC_VER
0556 #pragma warning(pop)
0557 #endif
0558 
0559 #endif // _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_
0560