1 // (C) Copyright John Maddock 2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_SF_DIGAMMA_HPP
7 #define BOOST_MATH_SF_DIGAMMA_HPP
12 #pragma warning(disable:4702) // Unreachable code (release mode only warning)
15 #include <boost/math/special_functions/math_fwd.hpp>
16 #include <boost/math/tools/rational.hpp>
17 #include <boost/math/tools/series.hpp>
18 #include <boost/math/tools/promotion.hpp>
19 #include <boost/math/policies/error_handling.hpp>
20 #include <boost/math/constants/constants.hpp>
21 #include <boost/math/tools/big_constant.hpp>
23 #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
25 // This is the only way we can avoid
26 // warning: non-standard suffix on floating constant [-Wpedantic]
27 // when building with -Wall -pedantic. Neither __extension__
28 // nor #pragma diagnostic ignored work :(
30 #pragma GCC system_header
37 // Begin by defining the smallest value for which it is safe to
38 // use the asymptotic expansion for digamma:
40 inline unsigned digamma_large_lim(const std::integral_constant<int, 0>*)
42 inline unsigned digamma_large_lim(const std::integral_constant<int, 113>*)
44 inline unsigned digamma_large_lim(const void*)
47 // Implementations of the asymptotic expansion come next,
48 // the coefficients of the series have been evaluated
49 // in advance at high precision, and the series truncated
50 // at the first term that's too small to effect the result.
51 // Note that the series becomes divergent after a while
52 // so truncation is very important.
54 // This first one gives 34-digit precision for x >= 20:
57 inline T digamma_imp_large(T x, const std::integral_constant<int, 113>*)
59 BOOST_MATH_STD_USING // ADL of std functions.
60 static const T P[] = {
61 BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
62 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0083333333333333333333333333333333333333333333333333),
63 BOOST_MATH_BIG_CONSTANT(T, 113, 0.003968253968253968253968253968253968253968253968254),
64 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0041666666666666666666666666666666666666666666666667),
65 BOOST_MATH_BIG_CONSTANT(T, 113, 0.0075757575757575757575757575757575757575757575757576),
66 BOOST_MATH_BIG_CONSTANT(T, 113, -0.021092796092796092796092796092796092796092796092796),
67 BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
68 BOOST_MATH_BIG_CONSTANT(T, 113, -0.44325980392156862745098039215686274509803921568627),
69 BOOST_MATH_BIG_CONSTANT(T, 113, 3.0539543302701197438039543302701197438039543302701),
70 BOOST_MATH_BIG_CONSTANT(T, 113, -26.456212121212121212121212121212121212121212121212),
71 BOOST_MATH_BIG_CONSTANT(T, 113, 281.4601449275362318840579710144927536231884057971),
72 BOOST_MATH_BIG_CONSTANT(T, 113, -3607.510546398046398046398046398046398046398046398),
73 BOOST_MATH_BIG_CONSTANT(T, 113, 54827.583333333333333333333333333333333333333333333),
74 BOOST_MATH_BIG_CONSTANT(T, 113, -974936.82385057471264367816091954022988505747126437),
75 BOOST_MATH_BIG_CONSTANT(T, 113, 20052695.796688078946143462272494530559046688078946),
76 BOOST_MATH_BIG_CONSTANT(T, 113, -472384867.72162990196078431372549019607843137254902),
77 BOOST_MATH_BIG_CONSTANT(T, 113, 12635724795.916666666666666666666666666666666666667)
81 result += 1 / (2 * x);
83 result -= z * tools::evaluate_polynomial(P, z);
87 // 19-digit precision for x >= 10:
90 inline T digamma_imp_large(T x, const std::integral_constant<int, 64>*)
92 BOOST_MATH_STD_USING // ADL of std functions.
93 static const T P[] = {
94 BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
95 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0083333333333333333333333333333333333333333333333333),
96 BOOST_MATH_BIG_CONSTANT(T, 64, 0.003968253968253968253968253968253968253968253968254),
97 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0041666666666666666666666666666666666666666666666667),
98 BOOST_MATH_BIG_CONSTANT(T, 64, 0.0075757575757575757575757575757575757575757575757576),
99 BOOST_MATH_BIG_CONSTANT(T, 64, -0.021092796092796092796092796092796092796092796092796),
100 BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
101 BOOST_MATH_BIG_CONSTANT(T, 64, -0.44325980392156862745098039215686274509803921568627),
102 BOOST_MATH_BIG_CONSTANT(T, 64, 3.0539543302701197438039543302701197438039543302701),
103 BOOST_MATH_BIG_CONSTANT(T, 64, -26.456212121212121212121212121212121212121212121212),
104 BOOST_MATH_BIG_CONSTANT(T, 64, 281.4601449275362318840579710144927536231884057971),
108 result += 1 / (2 * x);
110 result -= z * tools::evaluate_polynomial(P, z);
114 // 17-digit precision for x >= 10:
117 inline T digamma_imp_large(T x, const std::integral_constant<int, 53>*)
119 BOOST_MATH_STD_USING // ADL of std functions.
120 static const T P[] = {
121 BOOST_MATH_BIG_CONSTANT(T, 53, 0.083333333333333333333333333333333333333333333333333),
122 BOOST_MATH_BIG_CONSTANT(T, 53, -0.0083333333333333333333333333333333333333333333333333),
123 BOOST_MATH_BIG_CONSTANT(T, 53, 0.003968253968253968253968253968253968253968253968254),
124 BOOST_MATH_BIG_CONSTANT(T, 53, -0.0041666666666666666666666666666666666666666666666667),
125 BOOST_MATH_BIG_CONSTANT(T, 53, 0.0075757575757575757575757575757575757575757575757576),
126 BOOST_MATH_BIG_CONSTANT(T, 53, -0.021092796092796092796092796092796092796092796092796),
127 BOOST_MATH_BIG_CONSTANT(T, 53, 0.083333333333333333333333333333333333333333333333333),
128 BOOST_MATH_BIG_CONSTANT(T, 53, -0.44325980392156862745098039215686274509803921568627)
132 result += 1 / (2 * x);
134 result -= z * tools::evaluate_polynomial(P, z);
138 // 9-digit precision for x >= 10:
141 inline T digamma_imp_large(T x, const std::integral_constant<int, 24>*)
143 BOOST_MATH_STD_USING // ADL of std functions.
144 static const T P[] = {
145 BOOST_MATH_BIG_CONSTANT(T, 24, 0.083333333333333333333333333333333333333333333333333),
146 BOOST_MATH_BIG_CONSTANT(T, 24, -0.0083333333333333333333333333333333333333333333333333),
147 BOOST_MATH_BIG_CONSTANT(T, 24, 0.003968253968253968253968253968253968253968253968254)
151 result += 1 / (2 * x);
153 result -= z * tools::evaluate_polynomial(P, z);
157 // Fully generic asymptotic expansion in terms of Bernoulli numbers, see:
158 // http://functions.wolfram.com/06.14.06.0012.01
161 struct digamma_series_func
168 digamma_series_func(T x) : k(1), xx(x * x), term(1 / (x * x)) {}
171 T result = term * boost::math::bernoulli_b2n<T>(k) / (2 * k);
176 typedef T result_type;
179 template <class T, class Policy>
180 inline T digamma_imp_large(T x, const Policy& pol, const std::integral_constant<int, 0>*)
183 digamma_series_func<T> s(x);
184 T result = log(x) - 1 / (2 * x);
185 std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
186 result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, -result);
188 policies::check_series_iterations<T>("boost::math::digamma<%1%>(%1%)", max_iter, pol);
192 // Now follow rational approximations over the range [1,2].
194 // 35-digit precision:
197 T digamma_imp_1_2(T x, const std::integral_constant<int, 113>*)
200 // Now the approximation, we use the form:
202 // digamma(x) = (x - root) * (Y + R(x-1))
204 // Where root is the location of the positive root of digamma,
205 // Y is a constant, and R is optimised for low absolute error
208 // Max error found at 128-bit long double precision: 5.541e-35
209 // Maximum Deviation Found (approximation error): 1.965e-35
211 static const float Y = 0.99558162689208984375F;
213 static const T root1 = T(1569415565) / 1073741824uL;
214 static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
215 static const T root3 = ((T(111616537) / 1073741824uL) / 1073741824uL) / 1073741824uL;
216 static const T root4 = (((T(503992070) / 1073741824uL) / 1073741824uL) / 1073741824uL) / 1073741824uL;
217 static const T root5 = BOOST_MATH_BIG_CONSTANT(T, 113, 0.52112228569249997894452490385577338504019838794544e-36);
219 static const T P[] = {
220 BOOST_MATH_BIG_CONSTANT(T, 113, 0.25479851061131551526977464225335883769),
221 BOOST_MATH_BIG_CONSTANT(T, 113, -0.18684290534374944114622235683619897417),
222 BOOST_MATH_BIG_CONSTANT(T, 113, -0.80360876047931768958995775910991929922),
223 BOOST_MATH_BIG_CONSTANT(T, 113, -0.67227342794829064330498117008564270136),
224 BOOST_MATH_BIG_CONSTANT(T, 113, -0.26569010991230617151285010695543858005),
225 BOOST_MATH_BIG_CONSTANT(T, 113, -0.05775672694575986971640757748003553385),
226 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0071432147823164975485922555833274240665),
227 BOOST_MATH_BIG_CONSTANT(T, 113, -0.00048740753910766168912364555706064993274),
228 BOOST_MATH_BIG_CONSTANT(T, 113, -0.16454996865214115723416538844975174761e-4),
229 BOOST_MATH_BIG_CONSTANT(T, 113, -0.20327832297631728077731148515093164955e-6)
231 static const T Q[] = {
232 BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
233 BOOST_MATH_BIG_CONSTANT(T, 113, 2.6210924610812025425088411043163287646),
234 BOOST_MATH_BIG_CONSTANT(T, 113, 2.6850757078559596612621337395886392594),
235 BOOST_MATH_BIG_CONSTANT(T, 113, 1.4320913706209965531250495490639289418),
236 BOOST_MATH_BIG_CONSTANT(T, 113, 0.4410872083455009362557012239501953402),
237 BOOST_MATH_BIG_CONSTANT(T, 113, 0.081385727399251729505165509278152487225),
238 BOOST_MATH_BIG_CONSTANT(T, 113, 0.0089478633066857163432104815183858149496),
239 BOOST_MATH_BIG_CONSTANT(T, 113, 0.00055861622855066424871506755481997374154),
240 BOOST_MATH_BIG_CONSTANT(T, 113, 0.1760168552357342401304462967950178554e-4),
241 BOOST_MATH_BIG_CONSTANT(T, 113, 0.20585454493572473724556649516040874384e-6),
242 BOOST_MATH_BIG_CONSTANT(T, 113, -0.90745971844439990284514121823069162795e-11),
243 BOOST_MATH_BIG_CONSTANT(T, 113, 0.48857673606545846774761343500033283272e-13),
250 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
251 T result = g * Y + g * r;
256 // 19-digit precision:
259 T digamma_imp_1_2(T x, const std::integral_constant<int, 64>*)
262 // Now the approximation, we use the form:
264 // digamma(x) = (x - root) * (Y + R(x-1))
266 // Where root is the location of the positive root of digamma,
267 // Y is a constant, and R is optimised for low absolute error
270 // Max error found at 80-bit long double precision: 5.016e-20
271 // Maximum Deviation Found (approximation error): 3.575e-20
273 static const float Y = 0.99558162689208984375F;
275 static const T root1 = T(1569415565) / 1073741824uL;
276 static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
277 static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 64, 0.9016312093258695918615325266959189453125e-19);
279 static const T P[] = {
280 BOOST_MATH_BIG_CONSTANT(T, 64, 0.254798510611315515235),
281 BOOST_MATH_BIG_CONSTANT(T, 64, -0.314628554532916496608),
282 BOOST_MATH_BIG_CONSTANT(T, 64, -0.665836341559876230295),
283 BOOST_MATH_BIG_CONSTANT(T, 64, -0.314767657147375752913),
284 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0541156266153505273939),
285 BOOST_MATH_BIG_CONSTANT(T, 64, -0.00289268368333918761452)
287 static const T Q[] = {
288 BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
289 BOOST_MATH_BIG_CONSTANT(T, 64, 2.1195759927055347547),
290 BOOST_MATH_BIG_CONSTANT(T, 64, 1.54350554664961128724),
291 BOOST_MATH_BIG_CONSTANT(T, 64, 0.486986018231042975162),
292 BOOST_MATH_BIG_CONSTANT(T, 64, 0.0660481487173569812846),
293 BOOST_MATH_BIG_CONSTANT(T, 64, 0.00298999662592323990972),
294 BOOST_MATH_BIG_CONSTANT(T, 64, -0.165079794012604905639e-5),
295 BOOST_MATH_BIG_CONSTANT(T, 64, 0.317940243105952177571e-7)
300 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
301 T result = g * Y + g * r;
306 // 18-digit precision:
309 T digamma_imp_1_2(T x, const std::integral_constant<int, 53>*)
312 // Now the approximation, we use the form:
314 // digamma(x) = (x - root) * (Y + R(x-1))
316 // Where root is the location of the positive root of digamma,
317 // Y is a constant, and R is optimised for low absolute error
320 // Maximum Deviation Found: 1.466e-18
321 // At double precision, max error found: 2.452e-17
323 static const float Y = 0.99558162689208984F;
325 static const T root1 = T(1569415565) / 1073741824uL;
326 static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
327 static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 53, 0.9016312093258695918615325266959189453125e-19);
329 static const T P[] = {
330 BOOST_MATH_BIG_CONSTANT(T, 53, 0.25479851061131551),
331 BOOST_MATH_BIG_CONSTANT(T, 53, -0.32555031186804491),
332 BOOST_MATH_BIG_CONSTANT(T, 53, -0.65031853770896507),
333 BOOST_MATH_BIG_CONSTANT(T, 53, -0.28919126444774784),
334 BOOST_MATH_BIG_CONSTANT(T, 53, -0.045251321448739056),
335 BOOST_MATH_BIG_CONSTANT(T, 53, -0.0020713321167745952)
337 static const T Q[] = {
338 BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
339 BOOST_MATH_BIG_CONSTANT(T, 53, 2.0767117023730469),
340 BOOST_MATH_BIG_CONSTANT(T, 53, 1.4606242909763515),
341 BOOST_MATH_BIG_CONSTANT(T, 53, 0.43593529692665969),
342 BOOST_MATH_BIG_CONSTANT(T, 53, 0.054151797245674225),
343 BOOST_MATH_BIG_CONSTANT(T, 53, 0.0021284987017821144),
344 BOOST_MATH_BIG_CONSTANT(T, 53, -0.55789841321675513e-6)
349 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
350 T result = g * Y + g * r;
355 // 9-digit precision:
358 inline T digamma_imp_1_2(T x, const std::integral_constant<int, 24>*)
361 // Now the approximation, we use the form:
363 // digamma(x) = (x - root) * (Y + R(x-1))
365 // Where root is the location of the positive root of digamma,
366 // Y is a constant, and R is optimised for low absolute error
369 // Maximum Deviation Found: 3.388e-010
370 // At float precision, max error found: 2.008725e-008
372 static const float Y = 0.99558162689208984f;
373 static const T root = 1532632.0f / 1048576;
374 static const T root_minor = static_cast<T>(0.3700660185912626595423257213284682051735604e-6L);
375 static const T P[] = {
376 0.25479851023250261e0f,
377 -0.44981331915268368e0f,
378 -0.43916936919946835e0f,
379 -0.61041765350579073e-1f
381 static const T Q[] = {
383 0.15890202430554952e1f,
384 0.65341249856146947e0f,
385 0.63851690523355715e-1f
389 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
390 T result = g * Y + g * r;
395 template <class T, class Tag, class Policy>
396 T digamma_imp(T x, const Tag* t, const Policy& pol)
399 // This handles reflection of negative arguments, and all our
400 // error handling, then forwards to the T-specific approximation.
402 BOOST_MATH_STD_USING // ADL of std functions.
406 // Check for negative arguments and use reflection:
412 // Argument reduction for tan:
413 T remainder = x - floor(x);
414 // Shift to negative if > 0.5:
420 // check for evaluation at a negative pole:
424 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, (1-x), pol);
426 result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
429 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, x, pol);
431 // If we're above the lower-limit for the
432 // asymptotic expansion then use it:
434 if(x >= digamma_large_lim(t))
436 result += digamma_imp_large(x, t);
441 // If x > 2 reduce to the interval [1,2]:
449 // If x < 1 use recurrence to shift to > 1:
456 result += digamma_imp_1_2(x, t);
461 template <class T, class Policy>
462 T digamma_imp(T x, const std::integral_constant<int, 0>* t, const Policy& pol)
465 // This handles reflection of negative arguments, and all our
466 // error handling, then forwards to the T-specific approximation.
468 BOOST_MATH_STD_USING // ADL of std functions.
472 // Check for negative arguments and use reflection:
478 // Argument reduction for tan:
479 T remainder = x - floor(x);
480 // Shift to negative if > 0.5:
486 // check for evaluation at a negative pole:
490 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, (1 - x), pol);
492 result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
495 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, x, pol);
497 // If we're above the lower-limit for the
498 // asymptotic expansion then use it, the
499 // limit is a linear interpolation with
500 // limit = 10 at 50 bit precision and
501 // limit = 250 at 1000 bit precision.
503 int lim = 10 + ((tools::digits<T>() - 50) * 240L) / 950;
504 T two_x = ldexp(x, 1);
507 result += digamma_imp_large(x, pol, t);
509 else if(floor(x) == x)
512 // Special case for integer arguments, see
513 // http://functions.wolfram.com/06.14.03.0001.01
515 result = -constants::euler<T, Policy>();
523 else if(floor(two_x) == two_x)
526 // Special case for half integer arguments, see:
527 // http://functions.wolfram.com/06.14.03.0007.01
529 result = -2 * constants::ln_two<T, Policy>() - constants::euler<T, Policy>();
533 for(int k = 1; k < n; ++k)
535 for(int k = n; k <= 2 * n - 1; ++k)
542 // Rescale so we can use the asymptotic expansion:
549 result += digamma_imp_large(x, pol, t);
554 // Initializer: ensure all our constants are initialized prior to the first call of main:
556 template <class T, class Policy>
557 struct digamma_initializer
563 typedef typename policies::precision<T, Policy>::type precision_type;
564 do_init(std::integral_constant<bool, precision_type::value && (precision_type::value <= 113)>());
566 void do_init(const std::true_type&)
568 boost::math::digamma(T(1.5), Policy());
569 boost::math::digamma(T(500), Policy());
571 void do_init(const std::false_type&){}
572 void force_instantiate()const{}
574 static const init initializer;
575 static void force_instantiate()
577 initializer.force_instantiate();
581 template <class T, class Policy>
582 const typename digamma_initializer<T, Policy>::init digamma_initializer<T, Policy>::initializer;
584 } // namespace detail
586 template <class T, class Policy>
587 inline typename tools::promote_args<T>::type
588 digamma(T x, const Policy&)
590 typedef typename tools::promote_args<T>::type result_type;
591 typedef typename policies::evaluation<result_type, Policy>::type value_type;
592 typedef typename policies::precision<T, Policy>::type precision_type;
593 typedef std::integral_constant<int,
594 (precision_type::value <= 0) || (precision_type::value > 113) ? 0 :
595 precision_type::value <= 24 ? 24 :
596 precision_type::value <= 53 ? 53 :
597 precision_type::value <= 64 ? 64 :
598 precision_type::value <= 113 ? 113 : 0 > tag_type;
599 typedef typename policies::normalise<
601 policies::promote_float<false>,
602 policies::promote_double<false>,
603 policies::discrete_quantile<>,
604 policies::assert_undefined<> >::type forwarding_policy;
606 // Force initialization of constants:
607 detail::digamma_initializer<value_type, forwarding_policy>::force_instantiate();
609 return policies::checked_narrowing_cast<result_type, Policy>(detail::digamma_imp(
610 static_cast<value_type>(x),
611 static_cast<const tag_type*>(0), forwarding_policy()), "boost::math::digamma<%1%>(%1%)");
615 inline typename tools::promote_args<T>::type
618 return digamma(x, policies::policy<>());