Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:15:25

0001 //  (C) Copyright John Maddock 2006.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_SF_DIGAMMA_HPP
0007 #define BOOST_MATH_SF_DIGAMMA_HPP
0008 
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #pragma warning(push)
0012 #pragma warning(disable:4702) // Unreachable code (release mode only warning)
0013 #endif
0014 
0015 #include <boost/math/special_functions/math_fwd.hpp>
0016 #include <boost/math/tools/rational.hpp>
0017 #include <boost/math/tools/series.hpp>
0018 #include <boost/math/tools/promotion.hpp>
0019 #include <boost/math/policies/error_handling.hpp>
0020 #include <boost/math/constants/constants.hpp>
0021 #include <boost/math/tools/big_constant.hpp>
0022 
0023 #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
0024 //
0025 // This is the only way we can avoid
0026 // warning: non-standard suffix on floating constant [-Wpedantic]
0027 // when building with -Wall -pedantic.  Neither __extension__
0028 // nor #pragma diagnostic ignored work :(
0029 //
0030 #pragma GCC system_header
0031 #endif
0032 
0033 namespace boost{
0034 namespace math{
0035 namespace detail{
0036 //
0037 // Begin by defining the smallest value for which it is safe to
0038 // use the asymptotic expansion for digamma:
0039 //
0040 inline unsigned digamma_large_lim(const std::integral_constant<int, 0>*)
0041 {  return 20;  }
0042 inline unsigned digamma_large_lim(const std::integral_constant<int, 113>*)
0043 {  return 20;  }
0044 inline unsigned digamma_large_lim(const void*)
0045 {  return 10;  }
0046 //
0047 // Implementations of the asymptotic expansion come next,
0048 // the coefficients of the series have been evaluated
0049 // in advance at high precision, and the series truncated
0050 // at the first term that's too small to effect the result.
0051 // Note that the series becomes divergent after a while
0052 // so truncation is very important.
0053 //
0054 // This first one gives 34-digit precision for x >= 20:
0055 //
0056 template <class T>
0057 inline T digamma_imp_large(T x, const std::integral_constant<int, 113>*)
0058 {
0059    BOOST_MATH_STD_USING // ADL of std functions.
0060    static const T P[] = {
0061       BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
0062       BOOST_MATH_BIG_CONSTANT(T, 113, -0.0083333333333333333333333333333333333333333333333333),
0063       BOOST_MATH_BIG_CONSTANT(T, 113, 0.003968253968253968253968253968253968253968253968254),
0064       BOOST_MATH_BIG_CONSTANT(T, 113, -0.0041666666666666666666666666666666666666666666666667),
0065       BOOST_MATH_BIG_CONSTANT(T, 113, 0.0075757575757575757575757575757575757575757575757576),
0066       BOOST_MATH_BIG_CONSTANT(T, 113, -0.021092796092796092796092796092796092796092796092796),
0067       BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
0068       BOOST_MATH_BIG_CONSTANT(T, 113, -0.44325980392156862745098039215686274509803921568627),
0069       BOOST_MATH_BIG_CONSTANT(T, 113, 3.0539543302701197438039543302701197438039543302701),
0070       BOOST_MATH_BIG_CONSTANT(T, 113, -26.456212121212121212121212121212121212121212121212),
0071       BOOST_MATH_BIG_CONSTANT(T, 113, 281.4601449275362318840579710144927536231884057971),
0072       BOOST_MATH_BIG_CONSTANT(T, 113, -3607.510546398046398046398046398046398046398046398),
0073       BOOST_MATH_BIG_CONSTANT(T, 113, 54827.583333333333333333333333333333333333333333333),
0074       BOOST_MATH_BIG_CONSTANT(T, 113, -974936.82385057471264367816091954022988505747126437),
0075       BOOST_MATH_BIG_CONSTANT(T, 113, 20052695.796688078946143462272494530559046688078946),
0076       BOOST_MATH_BIG_CONSTANT(T, 113, -472384867.72162990196078431372549019607843137254902),
0077       BOOST_MATH_BIG_CONSTANT(T, 113, 12635724795.916666666666666666666666666666666666667)
0078    };
0079    x -= 1;
0080    T result = log(x);
0081    result += 1 / (2 * x);
0082    T z = 1 / (x*x);
0083    result -= z * tools::evaluate_polynomial(P, z);
0084    return result;
0085 }
0086 //
0087 // 19-digit precision for x >= 10:
0088 //
0089 template <class T>
0090 inline T digamma_imp_large(T x, const std::integral_constant<int, 64>*)
0091 {
0092    BOOST_MATH_STD_USING // ADL of std functions.
0093    static const T P[] = {
0094       BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
0095       BOOST_MATH_BIG_CONSTANT(T, 64, -0.0083333333333333333333333333333333333333333333333333),
0096       BOOST_MATH_BIG_CONSTANT(T, 64, 0.003968253968253968253968253968253968253968253968254),
0097       BOOST_MATH_BIG_CONSTANT(T, 64, -0.0041666666666666666666666666666666666666666666666667),
0098       BOOST_MATH_BIG_CONSTANT(T, 64, 0.0075757575757575757575757575757575757575757575757576),
0099       BOOST_MATH_BIG_CONSTANT(T, 64, -0.021092796092796092796092796092796092796092796092796),
0100       BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
0101       BOOST_MATH_BIG_CONSTANT(T, 64, -0.44325980392156862745098039215686274509803921568627),
0102       BOOST_MATH_BIG_CONSTANT(T, 64, 3.0539543302701197438039543302701197438039543302701),
0103       BOOST_MATH_BIG_CONSTANT(T, 64, -26.456212121212121212121212121212121212121212121212),
0104       BOOST_MATH_BIG_CONSTANT(T, 64, 281.4601449275362318840579710144927536231884057971),
0105    };
0106    x -= 1;
0107    T result = log(x);
0108    result += 1 / (2 * x);
0109    T z = 1 / (x*x);
0110    result -= z * tools::evaluate_polynomial(P, z);
0111    return result;
0112 }
0113 //
0114 // 17-digit precision for x >= 10:
0115 //
0116 template <class T>
0117 inline T digamma_imp_large(T x, const std::integral_constant<int, 53>*)
0118 {
0119    BOOST_MATH_STD_USING // ADL of std functions.
0120    static const T P[] = {
0121       0.083333333333333333333333333333333333333333333333333,
0122       -0.0083333333333333333333333333333333333333333333333333,
0123       0.003968253968253968253968253968253968253968253968254,
0124       -0.0041666666666666666666666666666666666666666666666667,
0125       0.0075757575757575757575757575757575757575757575757576,
0126       -0.021092796092796092796092796092796092796092796092796,
0127       0.083333333333333333333333333333333333333333333333333,
0128       -0.44325980392156862745098039215686274509803921568627
0129    };
0130    x -= 1;
0131    T result = log(x);
0132    result += 1 / (2 * x);
0133    T z = 1 / (x*x);
0134    result -= z * tools::evaluate_polynomial(P, z);
0135    return result;
0136 }
0137 //
0138 // 9-digit precision for x >= 10:
0139 //
0140 template <class T>
0141 inline T digamma_imp_large(T x, const std::integral_constant<int, 24>*)
0142 {
0143    BOOST_MATH_STD_USING // ADL of std functions.
0144    static const T P[] = {
0145       0.083333333333333333333333333333333333333333333333333f,
0146       -0.0083333333333333333333333333333333333333333333333333f,
0147       0.003968253968253968253968253968253968253968253968254f
0148    };
0149    x -= 1;
0150    T result = log(x);
0151    result += 1 / (2 * x);
0152    T z = 1 / (x*x);
0153    result -= z * tools::evaluate_polynomial(P, z);
0154    return result;
0155 }
0156 //
0157 // Fully generic asymptotic expansion in terms of Bernoulli numbers, see:
0158 // http://functions.wolfram.com/06.14.06.0012.01
0159 //
0160 template <class T>
0161 struct digamma_series_func
0162 {
0163 private:
0164    int k;
0165    T xx;
0166    T term;
0167 public:
0168    digamma_series_func(T x) : k(1), xx(x * x), term(1 / (x * x)) {}
0169    T operator()()
0170    {
0171       T result = term * boost::math::bernoulli_b2n<T>(k) / (2 * k);
0172       term /= xx;
0173       ++k;
0174       return result;
0175    }
0176    typedef T result_type;
0177 };
0178 
0179 template <class T, class Policy>
0180 inline T digamma_imp_large(T x, const Policy& pol, const std::integral_constant<int, 0>*)
0181 {
0182    BOOST_MATH_STD_USING
0183    digamma_series_func<T> s(x);
0184    T result = log(x) - 1 / (2 * x);
0185    std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
0186    result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, -result);
0187    result = -result;
0188    policies::check_series_iterations<T>("boost::math::digamma<%1%>(%1%)", max_iter, pol);
0189    return result;
0190 }
0191 //
0192 // Now follow rational approximations over the range [1,2].
0193 //
0194 // 35-digit precision:
0195 //
0196 template <class T>
0197 T digamma_imp_1_2(T x, const std::integral_constant<int, 113>*)
0198 {
0199    //
0200    // Now the approximation, we use the form:
0201    //
0202    // digamma(x) = (x - root) * (Y + R(x-1))
0203    //
0204    // Where root is the location of the positive root of digamma,
0205    // Y is a constant, and R is optimised for low absolute error
0206    // compared to Y.
0207    //
0208    // Max error found at 128-bit long double precision:  5.541e-35
0209    // Maximum Deviation Found (approximation error):     1.965e-35
0210    //
0211    // LCOV_EXCL_START
0212    static const float Y = 0.99558162689208984375F;
0213 
0214    static const T root1 = T(1569415565) / 1073741824uL;
0215    static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
0216    static const T root3 = ((T(111616537) / 1073741824uL) / 1073741824uL) / 1073741824uL;
0217    static const T root4 = (((T(503992070) / 1073741824uL) / 1073741824uL) / 1073741824uL) / 1073741824uL;
0218    static const T root5 = BOOST_MATH_BIG_CONSTANT(T, 113, 0.52112228569249997894452490385577338504019838794544e-36);
0219 
0220    static const T P[] = {
0221       BOOST_MATH_BIG_CONSTANT(T, 113, 0.25479851061131551526977464225335883769),
0222       BOOST_MATH_BIG_CONSTANT(T, 113, -0.18684290534374944114622235683619897417),
0223       BOOST_MATH_BIG_CONSTANT(T, 113, -0.80360876047931768958995775910991929922),
0224       BOOST_MATH_BIG_CONSTANT(T, 113, -0.67227342794829064330498117008564270136),
0225       BOOST_MATH_BIG_CONSTANT(T, 113, -0.26569010991230617151285010695543858005),
0226       BOOST_MATH_BIG_CONSTANT(T, 113, -0.05775672694575986971640757748003553385),
0227       BOOST_MATH_BIG_CONSTANT(T, 113, -0.0071432147823164975485922555833274240665),
0228       BOOST_MATH_BIG_CONSTANT(T, 113, -0.00048740753910766168912364555706064993274),
0229       BOOST_MATH_BIG_CONSTANT(T, 113, -0.16454996865214115723416538844975174761e-4),
0230       BOOST_MATH_BIG_CONSTANT(T, 113, -0.20327832297631728077731148515093164955e-6)
0231    };
0232    static const T Q[] = {
0233       BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
0234       BOOST_MATH_BIG_CONSTANT(T, 113, 2.6210924610812025425088411043163287646),
0235       BOOST_MATH_BIG_CONSTANT(T, 113, 2.6850757078559596612621337395886392594),
0236       BOOST_MATH_BIG_CONSTANT(T, 113, 1.4320913706209965531250495490639289418),
0237       BOOST_MATH_BIG_CONSTANT(T, 113, 0.4410872083455009362557012239501953402),
0238       BOOST_MATH_BIG_CONSTANT(T, 113, 0.081385727399251729505165509278152487225),
0239       BOOST_MATH_BIG_CONSTANT(T, 113, 0.0089478633066857163432104815183858149496),
0240       BOOST_MATH_BIG_CONSTANT(T, 113, 0.00055861622855066424871506755481997374154),
0241       BOOST_MATH_BIG_CONSTANT(T, 113, 0.1760168552357342401304462967950178554e-4),
0242       BOOST_MATH_BIG_CONSTANT(T, 113, 0.20585454493572473724556649516040874384e-6),
0243       BOOST_MATH_BIG_CONSTANT(T, 113, -0.90745971844439990284514121823069162795e-11),
0244       BOOST_MATH_BIG_CONSTANT(T, 113, 0.48857673606545846774761343500033283272e-13),
0245    };
0246    // LCOV_EXCL_STOP
0247    T g = x - root1;
0248    g -= root2;
0249    g -= root3;
0250    g -= root4;
0251    g -= root5;
0252    T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
0253    T result = g * Y + g * r;
0254 
0255    return result;
0256 }
0257 //
0258 // 19-digit precision:
0259 //
0260 template <class T>
0261 T digamma_imp_1_2(T x, const std::integral_constant<int, 64>*)
0262 {
0263    //
0264    // Now the approximation, we use the form:
0265    //
0266    // digamma(x) = (x - root) * (Y + R(x-1))
0267    //
0268    // Where root is the location of the positive root of digamma,
0269    // Y is a constant, and R is optimised for low absolute error
0270    // compared to Y.
0271    //
0272    // Max error found at 80-bit long double precision:   5.016e-20
0273    // Maximum Deviation Found (approximation error):     3.575e-20
0274    //
0275    // LCOV_EXCL_START
0276    static const float Y = 0.99558162689208984375F;
0277 
0278    static const T root1 = T(1569415565) / 1073741824uL;
0279    static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
0280    static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 64, 0.9016312093258695918615325266959189453125e-19);
0281 
0282    static const T P[] = {
0283       BOOST_MATH_BIG_CONSTANT(T, 64, 0.254798510611315515235),
0284       BOOST_MATH_BIG_CONSTANT(T, 64, -0.314628554532916496608),
0285       BOOST_MATH_BIG_CONSTANT(T, 64, -0.665836341559876230295),
0286       BOOST_MATH_BIG_CONSTANT(T, 64, -0.314767657147375752913),
0287       BOOST_MATH_BIG_CONSTANT(T, 64, -0.0541156266153505273939),
0288       BOOST_MATH_BIG_CONSTANT(T, 64, -0.00289268368333918761452)
0289    };
0290    static const T Q[] = {
0291       BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
0292       BOOST_MATH_BIG_CONSTANT(T, 64, 2.1195759927055347547),
0293       BOOST_MATH_BIG_CONSTANT(T, 64, 1.54350554664961128724),
0294       BOOST_MATH_BIG_CONSTANT(T, 64, 0.486986018231042975162),
0295       BOOST_MATH_BIG_CONSTANT(T, 64, 0.0660481487173569812846),
0296       BOOST_MATH_BIG_CONSTANT(T, 64, 0.00298999662592323990972),
0297       BOOST_MATH_BIG_CONSTANT(T, 64, -0.165079794012604905639e-5),
0298       BOOST_MATH_BIG_CONSTANT(T, 64, 0.317940243105952177571e-7)
0299    };
0300    // LCOV_EXCL_STOP
0301    T g = x - root1;
0302    g -= root2;
0303    g -= root3;
0304    T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
0305    T result = g * Y + g * r;
0306 
0307    return result;
0308 }
0309 //
0310 // 18-digit precision:
0311 //
0312 template <class T>
0313 T digamma_imp_1_2(T x, const std::integral_constant<int, 53>*)
0314 {
0315    //
0316    // Now the approximation, we use the form:
0317    //
0318    // digamma(x) = (x - root) * (Y + R(x-1))
0319    //
0320    // Where root is the location of the positive root of digamma,
0321    // Y is a constant, and R is optimised for low absolute error
0322    // compared to Y.
0323    //
0324    // Maximum Deviation Found:               1.466e-18
0325    // At double precision, max error found:  2.452e-17
0326    //
0327    // LCOV_EXCL_START
0328    static const float Y = 0.99558162689208984F;
0329 
0330    static const T root1 = T(1569415565) / 1073741824uL;
0331    static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
0332    static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 53, 0.9016312093258695918615325266959189453125e-19);
0333 
0334    static const T P[] = {
0335       BOOST_MATH_BIG_CONSTANT(T, 53, 0.25479851061131551),
0336       BOOST_MATH_BIG_CONSTANT(T, 53, -0.32555031186804491),
0337       BOOST_MATH_BIG_CONSTANT(T, 53, -0.65031853770896507),
0338       BOOST_MATH_BIG_CONSTANT(T, 53, -0.28919126444774784),
0339       BOOST_MATH_BIG_CONSTANT(T, 53, -0.045251321448739056),
0340       BOOST_MATH_BIG_CONSTANT(T, 53, -0.0020713321167745952)
0341    };
0342    static const T Q[] = {
0343       BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
0344       BOOST_MATH_BIG_CONSTANT(T, 53, 2.0767117023730469),
0345       BOOST_MATH_BIG_CONSTANT(T, 53, 1.4606242909763515),
0346       BOOST_MATH_BIG_CONSTANT(T, 53, 0.43593529692665969),
0347       BOOST_MATH_BIG_CONSTANT(T, 53, 0.054151797245674225),
0348       BOOST_MATH_BIG_CONSTANT(T, 53, 0.0021284987017821144),
0349       BOOST_MATH_BIG_CONSTANT(T, 53, -0.55789841321675513e-6)
0350    };
0351    // LCOV_EXCL_STOP
0352    T g = x - root1;
0353    g -= root2;
0354    g -= root3;
0355    T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
0356    T result = g * Y + g * r;
0357 
0358    return result;
0359 }
0360 //
0361 // 9-digit precision:
0362 //
0363 template <class T>
0364 inline T digamma_imp_1_2(T x, const std::integral_constant<int, 24>*)
0365 {
0366    //
0367    // Now the approximation, we use the form:
0368    //
0369    // digamma(x) = (x - root) * (Y + R(x-1))
0370    //
0371    // Where root is the location of the positive root of digamma,
0372    // Y is a constant, and R is optimised for low absolute error
0373    // compared to Y.
0374    //
0375    // Maximum Deviation Found:              3.388e-010
0376    // At float precision, max error found:  2.008725e-008
0377    //
0378    // LCOV_EXCL_START
0379    static const float Y = 0.99558162689208984f;
0380    static const T root = 1532632.0f / 1048576;
0381    static const T root_minor = static_cast<T>(0.3700660185912626595423257213284682051735604e-6L);
0382    static const T P[] = {
0383       0.25479851023250261e0f,
0384       -0.44981331915268368e0f,
0385       -0.43916936919946835e0f,
0386       -0.61041765350579073e-1f
0387    };
0388    static const T Q[] = {
0389       0.1e1f,
0390       0.15890202430554952e1f,
0391       0.65341249856146947e0f,
0392       0.63851690523355715e-1f
0393    };
0394    // LCOV_EXCL_STOP
0395    T g = x - root;
0396    g -= root_minor;
0397    T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
0398    T result = g * Y + g * r;
0399 
0400    return result;
0401 }
0402 
0403 template <class T, class Tag, class Policy>
0404 T digamma_imp(T x, const Tag* t, const Policy& pol)
0405 {
0406    //
0407    // This handles reflection of negative arguments, and all our
0408    // error handling, then forwards to the T-specific approximation.
0409    //
0410    BOOST_MATH_STD_USING // ADL of std functions.
0411 
0412    T result = 0;
0413    //
0414    // Check for negative arguments and use reflection:
0415    //
0416    if(x <= -1)
0417    {
0418       // Reflect:
0419       x = 1 - x;
0420       // Argument reduction for tan:
0421       T remainder = x - floor(x);
0422       // Shift to negative if > 0.5:
0423       if(remainder > T(0.5))
0424       {
0425          remainder -= 1;
0426       }
0427       //
0428       // check for evaluation at a negative pole:
0429       //
0430       if(remainder == 0)
0431       {
0432          return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", nullptr, (1-x), pol);
0433       }
0434       result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
0435    }
0436    if(x == 0)
0437       return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", nullptr, x, pol);
0438    //
0439    // If we're above the lower-limit for the
0440    // asymptotic expansion then use it:
0441    //
0442    if(x >= digamma_large_lim(t))
0443    {
0444       result += digamma_imp_large(x, t);
0445    }
0446    else
0447    {
0448       //
0449       // If x > 2 reduce to the interval [1,2]:
0450       //
0451       while(x > 2)
0452       {
0453          x -= 1;
0454          result += 1/x;
0455       }
0456       //
0457       // If x < 1 use recurrence to shift to > 1:
0458       //
0459       while(x < 1)
0460       {
0461          result -= 1/x;
0462          x += 1;
0463       }
0464       result += digamma_imp_1_2(x, t);
0465    }
0466    return result;
0467 }
0468 
0469 template <class T, class Policy>
0470 T digamma_imp(T x, const std::integral_constant<int, 0>* t, const Policy& pol)
0471 {
0472    //
0473    // This handles reflection of negative arguments, and all our
0474    // error handling, then forwards to the T-specific approximation.
0475    //
0476    // This is covered by our real_concept tests, but these are disabled for
0477    // code coverage runs for performance reasons.
0478    // LCOV_EXCL_START
0479    //
0480    BOOST_MATH_STD_USING // ADL of std functions.
0481 
0482    T result = 0;
0483    //
0484    // Check for negative arguments and use reflection:
0485    //
0486    if(x <= -1)
0487    {
0488       // Reflect:
0489       x = 1 - x;
0490       // Argument reduction for tan:
0491       T remainder = x - floor(x);
0492       // Shift to negative if > 0.5:
0493       if(remainder > T(0.5))
0494       {
0495          remainder -= 1;
0496       }
0497       //
0498       // check for evaluation at a negative pole:
0499       //
0500       if(remainder == 0)
0501       {
0502          return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", nullptr, (1 - x), pol);
0503       }
0504       result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
0505    }
0506    if(x == 0)
0507       return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", nullptr, x, pol);
0508    //
0509    // If we're above the lower-limit for the
0510    // asymptotic expansion then use it, the
0511    // limit is a linear interpolation with
0512    // limit = 10 at 50 bit precision and
0513    // limit = 250 at 1000 bit precision.
0514    //
0515    int lim = 10 + ((tools::digits<T>() - 50) * 240L) / 950;
0516    T two_x = ldexp(x, 1);
0517    if(x >= lim)
0518    {
0519       result += digamma_imp_large(x, pol, t);
0520    }
0521    else if(floor(x) == x)
0522    {
0523       //
0524       // Special case for integer arguments, see
0525       // http://functions.wolfram.com/06.14.03.0001.01
0526       //
0527       result = -constants::euler<T, Policy>();
0528       T val = 1;
0529       while(val < x)
0530       {
0531          result += 1 / val;
0532          val += 1;
0533       }
0534    }
0535    else if(floor(two_x) == two_x)
0536    {
0537       //
0538       // Special case for half integer arguments, see:
0539       // http://functions.wolfram.com/06.14.03.0007.01
0540       //
0541       result = -2 * constants::ln_two<T, Policy>() - constants::euler<T, Policy>();
0542       int n = itrunc(x);
0543       if(n)
0544       {
0545          for(int k = 1; k < n; ++k)
0546             result += 1 / T(k);
0547          for(int k = n; k <= 2 * n - 1; ++k)
0548             result += 2 / T(k);
0549       }
0550    }
0551    else
0552    {
0553       //
0554       // Rescale so we can use the asymptotic expansion:
0555       //
0556       while(x < lim)
0557       {
0558          result -= 1 / x;
0559          x += 1;
0560       }
0561       result += digamma_imp_large(x, pol, t);
0562    }
0563    return result;
0564    // LCOV_EXCL_STOP
0565 }
0566 
0567 } // namespace detail
0568 
0569 template <class T, class Policy>
0570 inline typename tools::promote_args<T>::type
0571    digamma(T x, const Policy&)
0572 {
0573    typedef typename tools::promote_args<T>::type result_type;
0574    typedef typename policies::evaluation<result_type, Policy>::type value_type;
0575    typedef typename policies::precision<T, Policy>::type precision_type;
0576    typedef std::integral_constant<int,
0577       (precision_type::value <= 0) || (precision_type::value > 113) ? 0 :
0578       precision_type::value <= 24 ? 24 :
0579       precision_type::value <= 53 ? 53 :
0580       precision_type::value <= 64 ? 64 :
0581       precision_type::value <= 113 ? 113 : 0 > tag_type;
0582    typedef typename policies::normalise<
0583       Policy,
0584       policies::promote_float<false>,
0585       policies::promote_double<false>,
0586       policies::discrete_quantile<>,
0587       policies::assert_undefined<> >::type forwarding_policy;
0588 
0589    return policies::checked_narrowing_cast<result_type, Policy>(detail::digamma_imp(
0590       static_cast<value_type>(x),
0591       static_cast<const tag_type*>(nullptr), forwarding_policy()), "boost::math::digamma<%1%>(%1%)");
0592 }
0593 
0594 template <class T>
0595 inline typename tools::promote_args<T>::type
0596    digamma(T x)
0597 {
0598    return digamma(x, policies::policy<>());
0599 }
0600 
0601 } // namespace math
0602 } // namespace boost
0603 
0604 #ifdef _MSC_VER
0605 #pragma warning(pop)
0606 #endif
0607 
0608 #endif
0609