File indexing completed on 2025-09-15 08:40:10
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_MATH_SF_DIGAMMA_HPP
0008 #define BOOST_MATH_SF_DIGAMMA_HPP
0009
0010 #ifdef _MSC_VER
0011 #pragma once
0012 #pragma warning(push)
0013 #pragma warning(disable:4702)
0014 #endif
0015
0016 #include <boost/math/tools/config.hpp>
0017 #include <boost/math/tools/type_traits.hpp>
0018 #include <boost/math/tools/rational.hpp>
0019 #include <boost/math/tools/promotion.hpp>
0020 #include <boost/math/policies/policy.hpp>
0021 #include <boost/math/policies/error_handling.hpp>
0022 #include <boost/math/constants/constants.hpp>
0023
0024 #ifndef BOOST_MATH_HAS_NVRTC
0025 #include <boost/math/special_functions/math_fwd.hpp>
0026 #include <boost/math/tools/series.hpp>
0027 #include <boost/math/policies/error_handling.hpp>
0028 #include <boost/math/constants/constants.hpp>
0029 #include <boost/math/tools/big_constant.hpp>
0030 #endif
0031
0032 #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
0033
0034
0035
0036
0037
0038
0039 #pragma GCC system_header
0040 #endif
0041
0042 namespace boost{
0043 namespace math{
0044 namespace detail{
0045
0046
0047
0048
0049 BOOST_MATH_GPU_ENABLED inline unsigned digamma_large_lim(const boost::math::integral_constant<int, 0>*)
0050 { return 20; }
0051 BOOST_MATH_GPU_ENABLED inline unsigned digamma_large_lim(const boost::math::integral_constant<int, 113>*)
0052 { return 20; }
0053 BOOST_MATH_GPU_ENABLED inline unsigned digamma_large_lim(const void*)
0054 { return 10; }
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 #ifndef BOOST_MATH_HAS_NVRTC
0067 template <class T>
0068 inline T digamma_imp_large(T x, const boost::math::integral_constant<int, 113>*)
0069 {
0070 BOOST_MATH_STD_USING
0071 static const T P[] = {
0072 BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
0073 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0083333333333333333333333333333333333333333333333333),
0074 BOOST_MATH_BIG_CONSTANT(T, 113, 0.003968253968253968253968253968253968253968253968254),
0075 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0041666666666666666666666666666666666666666666666667),
0076 BOOST_MATH_BIG_CONSTANT(T, 113, 0.0075757575757575757575757575757575757575757575757576),
0077 BOOST_MATH_BIG_CONSTANT(T, 113, -0.021092796092796092796092796092796092796092796092796),
0078 BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
0079 BOOST_MATH_BIG_CONSTANT(T, 113, -0.44325980392156862745098039215686274509803921568627),
0080 BOOST_MATH_BIG_CONSTANT(T, 113, 3.0539543302701197438039543302701197438039543302701),
0081 BOOST_MATH_BIG_CONSTANT(T, 113, -26.456212121212121212121212121212121212121212121212),
0082 BOOST_MATH_BIG_CONSTANT(T, 113, 281.4601449275362318840579710144927536231884057971),
0083 BOOST_MATH_BIG_CONSTANT(T, 113, -3607.510546398046398046398046398046398046398046398),
0084 BOOST_MATH_BIG_CONSTANT(T, 113, 54827.583333333333333333333333333333333333333333333),
0085 BOOST_MATH_BIG_CONSTANT(T, 113, -974936.82385057471264367816091954022988505747126437),
0086 BOOST_MATH_BIG_CONSTANT(T, 113, 20052695.796688078946143462272494530559046688078946),
0087 BOOST_MATH_BIG_CONSTANT(T, 113, -472384867.72162990196078431372549019607843137254902),
0088 BOOST_MATH_BIG_CONSTANT(T, 113, 12635724795.916666666666666666666666666666666666667)
0089 };
0090 x -= 1;
0091 T result = log(x);
0092 result += 1 / (2 * x);
0093 T z = 1 / (x*x);
0094 result -= z * tools::evaluate_polynomial(P, z);
0095 return result;
0096 }
0097
0098
0099
0100 template <class T>
0101 inline T digamma_imp_large(T x, const boost::math::integral_constant<int, 64>*)
0102 {
0103 BOOST_MATH_STD_USING
0104 static const T P[] = {
0105 BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
0106 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0083333333333333333333333333333333333333333333333333),
0107 BOOST_MATH_BIG_CONSTANT(T, 64, 0.003968253968253968253968253968253968253968253968254),
0108 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0041666666666666666666666666666666666666666666666667),
0109 BOOST_MATH_BIG_CONSTANT(T, 64, 0.0075757575757575757575757575757575757575757575757576),
0110 BOOST_MATH_BIG_CONSTANT(T, 64, -0.021092796092796092796092796092796092796092796092796),
0111 BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
0112 BOOST_MATH_BIG_CONSTANT(T, 64, -0.44325980392156862745098039215686274509803921568627),
0113 BOOST_MATH_BIG_CONSTANT(T, 64, 3.0539543302701197438039543302701197438039543302701),
0114 BOOST_MATH_BIG_CONSTANT(T, 64, -26.456212121212121212121212121212121212121212121212),
0115 BOOST_MATH_BIG_CONSTANT(T, 64, 281.4601449275362318840579710144927536231884057971),
0116 };
0117 x -= 1;
0118 T result = log(x);
0119 result += 1 / (2 * x);
0120 T z = 1 / (x*x);
0121 result -= z * tools::evaluate_polynomial(P, z);
0122 return result;
0123 }
0124 #endif
0125
0126
0127
0128 template <class T>
0129 BOOST_MATH_GPU_ENABLED inline T digamma_imp_large(T x, const boost::math::integral_constant<int, 53>*)
0130 {
0131 BOOST_MATH_STD_USING
0132 BOOST_MATH_STATIC const T P[] = {
0133 0.083333333333333333333333333333333333333333333333333,
0134 -0.0083333333333333333333333333333333333333333333333333,
0135 0.003968253968253968253968253968253968253968253968254,
0136 -0.0041666666666666666666666666666666666666666666666667,
0137 0.0075757575757575757575757575757575757575757575757576,
0138 -0.021092796092796092796092796092796092796092796092796,
0139 0.083333333333333333333333333333333333333333333333333,
0140 -0.44325980392156862745098039215686274509803921568627
0141 };
0142 x -= 1;
0143 T result = log(x);
0144 result += 1 / (2 * x);
0145 T z = 1 / (x*x);
0146 result -= z * tools::evaluate_polynomial(P, z);
0147 return result;
0148 }
0149
0150
0151
0152 template <class T>
0153 BOOST_MATH_GPU_ENABLED inline T digamma_imp_large(T x, const boost::math::integral_constant<int, 24>*)
0154 {
0155 BOOST_MATH_STD_USING
0156 BOOST_MATH_STATIC const T P[] = {
0157 0.083333333333333333333333333333333333333333333333333f,
0158 -0.0083333333333333333333333333333333333333333333333333f,
0159 0.003968253968253968253968253968253968253968253968254f
0160 };
0161 x -= 1;
0162 T result = log(x);
0163 result += 1 / (2 * x);
0164 T z = 1 / (x*x);
0165 result -= z * tools::evaluate_polynomial(P, z);
0166 return result;
0167 }
0168
0169 #ifndef BOOST_MATH_HAS_NVRTC
0170
0171
0172
0173
0174 template <class T>
0175 struct digamma_series_func
0176 {
0177 private:
0178 int k;
0179 T xx;
0180 T term;
0181 public:
0182 digamma_series_func(T x) : k(1), xx(x * x), term(1 / (x * x)) {}
0183 T operator()()
0184 {
0185 T result = term * boost::math::bernoulli_b2n<T>(k) / (2 * k);
0186 term /= xx;
0187 ++k;
0188 return result;
0189 }
0190 typedef T result_type;
0191 };
0192
0193 template <class T, class Policy>
0194 inline T digamma_imp_large(T x, const Policy& pol, const boost::math::integral_constant<int, 0>*)
0195 {
0196 BOOST_MATH_STD_USING
0197 digamma_series_func<T> s(x);
0198 T result = log(x) - 1 / (2 * x);
0199 std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
0200 result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, -result);
0201 result = -result;
0202 policies::check_series_iterations<T>("boost::math::digamma<%1%>(%1%)", max_iter, pol);
0203 return result;
0204 }
0205
0206
0207
0208
0209
0210 template <class T>
0211 T digamma_imp_1_2(T x, const boost::math::integral_constant<int, 113>*)
0212 {
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226 static const float Y = 0.99558162689208984375F;
0227
0228 static const T root1 = T(1569415565) / 1073741824uL;
0229 static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
0230 static const T root3 = ((T(111616537) / 1073741824uL) / 1073741824uL) / 1073741824uL;
0231 static const T root4 = (((T(503992070) / 1073741824uL) / 1073741824uL) / 1073741824uL) / 1073741824uL;
0232 static const T root5 = BOOST_MATH_BIG_CONSTANT(T, 113, 0.52112228569249997894452490385577338504019838794544e-36);
0233
0234 static const T P[] = {
0235 BOOST_MATH_BIG_CONSTANT(T, 113, 0.25479851061131551526977464225335883769),
0236 BOOST_MATH_BIG_CONSTANT(T, 113, -0.18684290534374944114622235683619897417),
0237 BOOST_MATH_BIG_CONSTANT(T, 113, -0.80360876047931768958995775910991929922),
0238 BOOST_MATH_BIG_CONSTANT(T, 113, -0.67227342794829064330498117008564270136),
0239 BOOST_MATH_BIG_CONSTANT(T, 113, -0.26569010991230617151285010695543858005),
0240 BOOST_MATH_BIG_CONSTANT(T, 113, -0.05775672694575986971640757748003553385),
0241 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0071432147823164975485922555833274240665),
0242 BOOST_MATH_BIG_CONSTANT(T, 113, -0.00048740753910766168912364555706064993274),
0243 BOOST_MATH_BIG_CONSTANT(T, 113, -0.16454996865214115723416538844975174761e-4),
0244 BOOST_MATH_BIG_CONSTANT(T, 113, -0.20327832297631728077731148515093164955e-6)
0245 };
0246 static const T Q[] = {
0247 BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
0248 BOOST_MATH_BIG_CONSTANT(T, 113, 2.6210924610812025425088411043163287646),
0249 BOOST_MATH_BIG_CONSTANT(T, 113, 2.6850757078559596612621337395886392594),
0250 BOOST_MATH_BIG_CONSTANT(T, 113, 1.4320913706209965531250495490639289418),
0251 BOOST_MATH_BIG_CONSTANT(T, 113, 0.4410872083455009362557012239501953402),
0252 BOOST_MATH_BIG_CONSTANT(T, 113, 0.081385727399251729505165509278152487225),
0253 BOOST_MATH_BIG_CONSTANT(T, 113, 0.0089478633066857163432104815183858149496),
0254 BOOST_MATH_BIG_CONSTANT(T, 113, 0.00055861622855066424871506755481997374154),
0255 BOOST_MATH_BIG_CONSTANT(T, 113, 0.1760168552357342401304462967950178554e-4),
0256 BOOST_MATH_BIG_CONSTANT(T, 113, 0.20585454493572473724556649516040874384e-6),
0257 BOOST_MATH_BIG_CONSTANT(T, 113, -0.90745971844439990284514121823069162795e-11),
0258 BOOST_MATH_BIG_CONSTANT(T, 113, 0.48857673606545846774761343500033283272e-13),
0259 };
0260
0261 T g = x - root1;
0262 g -= root2;
0263 g -= root3;
0264 g -= root4;
0265 g -= root5;
0266 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
0267 T result = g * Y + g * r;
0268
0269 return result;
0270 }
0271
0272
0273
0274 template <class T>
0275 T digamma_imp_1_2(T x, const boost::math::integral_constant<int, 64>*)
0276 {
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290 static const float Y = 0.99558162689208984375F;
0291
0292 static const T root1 = T(1569415565) / 1073741824uL;
0293 static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
0294 static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 64, 0.9016312093258695918615325266959189453125e-19);
0295
0296 static const T P[] = {
0297 BOOST_MATH_BIG_CONSTANT(T, 64, 0.254798510611315515235),
0298 BOOST_MATH_BIG_CONSTANT(T, 64, -0.314628554532916496608),
0299 BOOST_MATH_BIG_CONSTANT(T, 64, -0.665836341559876230295),
0300 BOOST_MATH_BIG_CONSTANT(T, 64, -0.314767657147375752913),
0301 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0541156266153505273939),
0302 BOOST_MATH_BIG_CONSTANT(T, 64, -0.00289268368333918761452)
0303 };
0304 static const T Q[] = {
0305 BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
0306 BOOST_MATH_BIG_CONSTANT(T, 64, 2.1195759927055347547),
0307 BOOST_MATH_BIG_CONSTANT(T, 64, 1.54350554664961128724),
0308 BOOST_MATH_BIG_CONSTANT(T, 64, 0.486986018231042975162),
0309 BOOST_MATH_BIG_CONSTANT(T, 64, 0.0660481487173569812846),
0310 BOOST_MATH_BIG_CONSTANT(T, 64, 0.00298999662592323990972),
0311 BOOST_MATH_BIG_CONSTANT(T, 64, -0.165079794012604905639e-5),
0312 BOOST_MATH_BIG_CONSTANT(T, 64, 0.317940243105952177571e-7)
0313 };
0314
0315 T g = x - root1;
0316 g -= root2;
0317 g -= root3;
0318 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
0319 T result = g * Y + g * r;
0320
0321 return result;
0322 }
0323
0324 #endif
0325
0326
0327
0328 template <class T>
0329 BOOST_MATH_GPU_ENABLED T digamma_imp_1_2(T x, const boost::math::integral_constant<int, 53>*)
0330 {
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344 BOOST_MATH_STATIC const float Y = 0.99558162689208984F;
0345
0346 BOOST_MATH_STATIC const T root1 = T(1569415565) / 1073741824uL;
0347 BOOST_MATH_STATIC const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
0348 BOOST_MATH_STATIC const T root3 = BOOST_MATH_BIG_CONSTANT(T, 53, 0.9016312093258695918615325266959189453125e-19);
0349
0350 BOOST_MATH_STATIC const T P[] = {
0351 BOOST_MATH_BIG_CONSTANT(T, 53, 0.25479851061131551),
0352 BOOST_MATH_BIG_CONSTANT(T, 53, -0.32555031186804491),
0353 BOOST_MATH_BIG_CONSTANT(T, 53, -0.65031853770896507),
0354 BOOST_MATH_BIG_CONSTANT(T, 53, -0.28919126444774784),
0355 BOOST_MATH_BIG_CONSTANT(T, 53, -0.045251321448739056),
0356 BOOST_MATH_BIG_CONSTANT(T, 53, -0.0020713321167745952)
0357 };
0358 BOOST_MATH_STATIC const T Q[] = {
0359 BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
0360 BOOST_MATH_BIG_CONSTANT(T, 53, 2.0767117023730469),
0361 BOOST_MATH_BIG_CONSTANT(T, 53, 1.4606242909763515),
0362 BOOST_MATH_BIG_CONSTANT(T, 53, 0.43593529692665969),
0363 BOOST_MATH_BIG_CONSTANT(T, 53, 0.054151797245674225),
0364 BOOST_MATH_BIG_CONSTANT(T, 53, 0.0021284987017821144),
0365 BOOST_MATH_BIG_CONSTANT(T, 53, -0.55789841321675513e-6)
0366 };
0367
0368 T g = x - root1;
0369 g -= root2;
0370 g -= root3;
0371 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
0372 T result = g * Y + g * r;
0373
0374 return result;
0375 }
0376
0377
0378
0379 template <class T>
0380 BOOST_MATH_GPU_ENABLED inline T digamma_imp_1_2(T x, const boost::math::integral_constant<int, 24>*)
0381 {
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395 BOOST_MATH_STATIC const float Y = 0.99558162689208984f;
0396 BOOST_MATH_STATIC const T root = 1532632.0f / 1048576;
0397 BOOST_MATH_STATIC const T root_minor = static_cast<T>(0.3700660185912626595423257213284682051735604e-6L);
0398 BOOST_MATH_STATIC const T P[] = {
0399 0.25479851023250261e0f,
0400 -0.44981331915268368e0f,
0401 -0.43916936919946835e0f,
0402 -0.61041765350579073e-1f
0403 };
0404 BOOST_MATH_STATIC const T Q[] = {
0405 0.1e1f,
0406 0.15890202430554952e1f,
0407 0.65341249856146947e0f,
0408 0.63851690523355715e-1f
0409 };
0410
0411 T g = x - root;
0412 g -= root_minor;
0413 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
0414 T result = g * Y + g * r;
0415
0416 return result;
0417 }
0418
0419 template <class T, class Tag, class Policy>
0420 BOOST_MATH_GPU_ENABLED T digamma_imp(T x, const Tag* t, const Policy& pol)
0421 {
0422
0423
0424
0425
0426 BOOST_MATH_STD_USING
0427
0428 T result = 0;
0429
0430
0431
0432 if(x <= -1)
0433 {
0434
0435 x = 1 - x;
0436
0437 T remainder = x - floor(x);
0438
0439 if(remainder > T(0.5))
0440 {
0441 remainder -= 1;
0442 }
0443
0444
0445
0446 if(remainder == 0)
0447 {
0448 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", nullptr, (1-x), pol);
0449 }
0450 result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
0451 }
0452 if(x == 0)
0453 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", nullptr, x, pol);
0454
0455
0456
0457
0458 #ifndef BOOST_MATH_HAS_NVRTC
0459 if(x >= digamma_large_lim(t))
0460 {
0461 result += digamma_imp_large(x, t);
0462 }
0463 else
0464 #endif
0465 {
0466
0467
0468
0469 while(x > 2)
0470 {
0471 x -= 1;
0472 result += 1/x;
0473 }
0474
0475
0476
0477 while(x < 1)
0478 {
0479 result -= 1/x;
0480 x += 1;
0481 }
0482 result += digamma_imp_1_2(x, t);
0483 }
0484 return result;
0485 }
0486
0487 #ifndef BOOST_MATH_HAS_NVRTC
0488
0489 template <class T, class Policy>
0490 T digamma_imp(T x, const boost::math::integral_constant<int, 0>* t, const Policy& pol)
0491 {
0492
0493
0494
0495
0496
0497
0498
0499
0500 BOOST_MATH_STD_USING
0501
0502 T result = 0;
0503
0504
0505
0506 if(x <= -1)
0507 {
0508
0509 x = 1 - x;
0510
0511 T remainder = x - floor(x);
0512
0513 if(remainder > T(0.5))
0514 {
0515 remainder -= 1;
0516 }
0517
0518
0519
0520 if(remainder == 0)
0521 {
0522 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", nullptr, (1 - x), pol);
0523 }
0524 result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
0525 }
0526 if(x == 0)
0527 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", nullptr, x, pol);
0528
0529
0530
0531
0532
0533
0534
0535 int lim = 10 + ((tools::digits<T>() - 50) * 240L) / 950;
0536 T two_x = ldexp(x, 1);
0537 if(x >= lim)
0538 {
0539 result += digamma_imp_large(x, pol, t);
0540 }
0541 else if(floor(x) == x)
0542 {
0543
0544
0545
0546
0547 result = -constants::euler<T, Policy>();
0548 T val = 1;
0549 while(val < x)
0550 {
0551 result += 1 / val;
0552 val += 1;
0553 }
0554 }
0555 else if(floor(two_x) == two_x)
0556 {
0557
0558
0559
0560
0561 result = -2 * constants::ln_two<T, Policy>() - constants::euler<T, Policy>();
0562 int n = itrunc(x);
0563 if(n)
0564 {
0565 for(int k = 1; k < n; ++k)
0566 result += 1 / T(k);
0567 for(int k = n; k <= 2 * n - 1; ++k)
0568 result += 2 / T(k);
0569 }
0570 }
0571 else
0572 {
0573
0574
0575
0576 while(x < lim)
0577 {
0578 result -= 1 / x;
0579 x += 1;
0580 }
0581 result += digamma_imp_large(x, pol, t);
0582 }
0583 return result;
0584
0585 }
0586
0587 #endif
0588
0589 }
0590
0591 template <class T, class Policy>
0592 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type
0593 digamma(T x, const Policy&)
0594 {
0595 typedef typename tools::promote_args<T>::type result_type;
0596 typedef typename policies::evaluation<result_type, Policy>::type value_type;
0597 typedef typename policies::precision<T, Policy>::type precision_type;
0598 typedef boost::math::integral_constant<int,
0599 (precision_type::value <= 0) || (precision_type::value > 113) ? 0 :
0600 precision_type::value <= 24 ? 24 :
0601 precision_type::value <= 53 ? 53 :
0602 precision_type::value <= 64 ? 64 :
0603 precision_type::value <= 113 ? 113 : 0 > tag_type;
0604 typedef typename policies::normalise<
0605 Policy,
0606 policies::promote_float<false>,
0607 policies::promote_double<false>,
0608 policies::discrete_quantile<>,
0609 policies::assert_undefined<> >::type forwarding_policy;
0610
0611 return policies::checked_narrowing_cast<result_type, Policy>(detail::digamma_imp(
0612 static_cast<value_type>(x),
0613 static_cast<const tag_type*>(nullptr), forwarding_policy()), "boost::math::digamma<%1%>(%1%)");
0614 }
0615
0616 template <class T>
0617 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type
0618 digamma(T x)
0619 {
0620 return digamma(x, policies::policy<>());
0621 }
0622
0623 }
0624 }
0625
0626 #ifdef _MSC_VER
0627 #pragma warning(pop)
0628 #endif
0629
0630 #endif
0631