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/mpl/comparison.hpp>
22 #include <boost/math/tools/big_constant.hpp>
28 // Begin by defining the smallest value for which it is safe to
29 // use the asymptotic expansion for digamma:
31 inline unsigned digamma_large_lim(const mpl::int_<0>*)
33 inline unsigned digamma_large_lim(const mpl::int_<113>*)
35 inline unsigned digamma_large_lim(const void*)
38 // Implementations of the asymptotic expansion come next,
39 // the coefficients of the series have been evaluated
40 // in advance at high precision, and the series truncated
41 // at the first term that's too small to effect the result.
42 // Note that the series becomes divergent after a while
43 // so truncation is very important.
45 // This first one gives 34-digit precision for x >= 20:
48 inline T digamma_imp_large(T x, const mpl::int_<113>*)
50 BOOST_MATH_STD_USING // ADL of std functions.
51 static const T P[] = {
52 BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
53 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0083333333333333333333333333333333333333333333333333),
54 BOOST_MATH_BIG_CONSTANT(T, 113, 0.003968253968253968253968253968253968253968253968254),
55 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0041666666666666666666666666666666666666666666666667),
56 BOOST_MATH_BIG_CONSTANT(T, 113, 0.0075757575757575757575757575757575757575757575757576),
57 BOOST_MATH_BIG_CONSTANT(T, 113, -0.021092796092796092796092796092796092796092796092796),
58 BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
59 BOOST_MATH_BIG_CONSTANT(T, 113, -0.44325980392156862745098039215686274509803921568627),
60 BOOST_MATH_BIG_CONSTANT(T, 113, 3.0539543302701197438039543302701197438039543302701),
61 BOOST_MATH_BIG_CONSTANT(T, 113, -26.456212121212121212121212121212121212121212121212),
62 BOOST_MATH_BIG_CONSTANT(T, 113, 281.4601449275362318840579710144927536231884057971),
63 BOOST_MATH_BIG_CONSTANT(T, 113, -3607.510546398046398046398046398046398046398046398),
64 BOOST_MATH_BIG_CONSTANT(T, 113, 54827.583333333333333333333333333333333333333333333),
65 BOOST_MATH_BIG_CONSTANT(T, 113, -974936.82385057471264367816091954022988505747126437),
66 BOOST_MATH_BIG_CONSTANT(T, 113, 20052695.796688078946143462272494530559046688078946),
67 BOOST_MATH_BIG_CONSTANT(T, 113, -472384867.72162990196078431372549019607843137254902),
68 BOOST_MATH_BIG_CONSTANT(T, 113, 12635724795.916666666666666666666666666666666666667)
72 result += 1 / (2 * x);
74 result -= z * tools::evaluate_polynomial(P, z);
78 // 19-digit precision for x >= 10:
81 inline T digamma_imp_large(T x, const mpl::int_<64>*)
83 BOOST_MATH_STD_USING // ADL of std functions.
84 static const T P[] = {
85 BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
86 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0083333333333333333333333333333333333333333333333333),
87 BOOST_MATH_BIG_CONSTANT(T, 64, 0.003968253968253968253968253968253968253968253968254),
88 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0041666666666666666666666666666666666666666666666667),
89 BOOST_MATH_BIG_CONSTANT(T, 64, 0.0075757575757575757575757575757575757575757575757576),
90 BOOST_MATH_BIG_CONSTANT(T, 64, -0.021092796092796092796092796092796092796092796092796),
91 BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
92 BOOST_MATH_BIG_CONSTANT(T, 64, -0.44325980392156862745098039215686274509803921568627),
93 BOOST_MATH_BIG_CONSTANT(T, 64, 3.0539543302701197438039543302701197438039543302701),
94 BOOST_MATH_BIG_CONSTANT(T, 64, -26.456212121212121212121212121212121212121212121212),
95 BOOST_MATH_BIG_CONSTANT(T, 64, 281.4601449275362318840579710144927536231884057971),
99 result += 1 / (2 * x);
101 result -= z * tools::evaluate_polynomial(P, z);
105 // 17-digit precision for x >= 10:
108 inline T digamma_imp_large(T x, const mpl::int_<53>*)
110 BOOST_MATH_STD_USING // ADL of std functions.
111 static const T P[] = {
112 BOOST_MATH_BIG_CONSTANT(T, 53, 0.083333333333333333333333333333333333333333333333333),
113 BOOST_MATH_BIG_CONSTANT(T, 53, -0.0083333333333333333333333333333333333333333333333333),
114 BOOST_MATH_BIG_CONSTANT(T, 53, 0.003968253968253968253968253968253968253968253968254),
115 BOOST_MATH_BIG_CONSTANT(T, 53, -0.0041666666666666666666666666666666666666666666666667),
116 BOOST_MATH_BIG_CONSTANT(T, 53, 0.0075757575757575757575757575757575757575757575757576),
117 BOOST_MATH_BIG_CONSTANT(T, 53, -0.021092796092796092796092796092796092796092796092796),
118 BOOST_MATH_BIG_CONSTANT(T, 53, 0.083333333333333333333333333333333333333333333333333),
119 BOOST_MATH_BIG_CONSTANT(T, 53, -0.44325980392156862745098039215686274509803921568627)
123 result += 1 / (2 * x);
125 result -= z * tools::evaluate_polynomial(P, z);
129 // 9-digit precision for x >= 10:
132 inline T digamma_imp_large(T x, const mpl::int_<24>*)
134 BOOST_MATH_STD_USING // ADL of std functions.
135 static const T P[] = {
136 BOOST_MATH_BIG_CONSTANT(T, 24, 0.083333333333333333333333333333333333333333333333333),
137 BOOST_MATH_BIG_CONSTANT(T, 24, -0.0083333333333333333333333333333333333333333333333333),
138 BOOST_MATH_BIG_CONSTANT(T, 24, 0.003968253968253968253968253968253968253968253968254)
142 result += 1 / (2 * x);
144 result -= z * tools::evaluate_polynomial(P, z);
148 // Fully generic asymptotic expansion in terms of Bernoulli numbers, see:
149 // http://functions.wolfram.com/06.14.06.0012.01
152 struct digamma_series_func
159 digamma_series_func(T x) : k(1), xx(x * x), term(1 / (x * x)) {}
162 T result = term * boost::math::bernoulli_b2n<T>(k) / (2 * k);
167 typedef T result_type;
170 template <class T, class Policy>
171 inline T digamma_imp_large(T x, const Policy& pol, const mpl::int_<0>*)
174 digamma_series_func<T> s(x);
175 T result = log(x) - 1 / (2 * x);
176 boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
177 result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, -result);
179 policies::check_series_iterations<T>("boost::math::digamma<%1%>(%1%)", max_iter, pol);
183 // Now follow rational approximations over the range [1,2].
185 // 35-digit precision:
188 T digamma_imp_1_2(T x, const mpl::int_<113>*)
191 // Now the approximation, we use the form:
193 // digamma(x) = (x - root) * (Y + R(x-1))
195 // Where root is the location of the positive root of digamma,
196 // Y is a constant, and R is optimised for low absolute error
199 // Max error found at 128-bit long double precision: 5.541e-35
200 // Maximum Deviation Found (approximation error): 1.965e-35
202 static const float Y = 0.99558162689208984375F;
204 static const T root1 = T(1569415565) / 1073741824uL;
205 static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
206 static const T root3 = ((T(111616537) / 1073741824uL) / 1073741824uL) / 1073741824uL;
207 static const T root4 = (((T(503992070) / 1073741824uL) / 1073741824uL) / 1073741824uL) / 1073741824uL;
208 static const T root5 = BOOST_MATH_BIG_CONSTANT(T, 113, 0.52112228569249997894452490385577338504019838794544e-36);
210 static const T P[] = {
211 BOOST_MATH_BIG_CONSTANT(T, 113, 0.25479851061131551526977464225335883769),
212 BOOST_MATH_BIG_CONSTANT(T, 113, -0.18684290534374944114622235683619897417),
213 BOOST_MATH_BIG_CONSTANT(T, 113, -0.80360876047931768958995775910991929922),
214 BOOST_MATH_BIG_CONSTANT(T, 113, -0.67227342794829064330498117008564270136),
215 BOOST_MATH_BIG_CONSTANT(T, 113, -0.26569010991230617151285010695543858005),
216 BOOST_MATH_BIG_CONSTANT(T, 113, -0.05775672694575986971640757748003553385),
217 BOOST_MATH_BIG_CONSTANT(T, 113, -0.0071432147823164975485922555833274240665),
218 BOOST_MATH_BIG_CONSTANT(T, 113, -0.00048740753910766168912364555706064993274),
219 BOOST_MATH_BIG_CONSTANT(T, 113, -0.16454996865214115723416538844975174761e-4),
220 BOOST_MATH_BIG_CONSTANT(T, 113, -0.20327832297631728077731148515093164955e-6)
222 static const T Q[] = {
223 BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
224 BOOST_MATH_BIG_CONSTANT(T, 113, 2.6210924610812025425088411043163287646),
225 BOOST_MATH_BIG_CONSTANT(T, 113, 2.6850757078559596612621337395886392594),
226 BOOST_MATH_BIG_CONSTANT(T, 113, 1.4320913706209965531250495490639289418),
227 BOOST_MATH_BIG_CONSTANT(T, 113, 0.4410872083455009362557012239501953402),
228 BOOST_MATH_BIG_CONSTANT(T, 113, 0.081385727399251729505165509278152487225),
229 BOOST_MATH_BIG_CONSTANT(T, 113, 0.0089478633066857163432104815183858149496),
230 BOOST_MATH_BIG_CONSTANT(T, 113, 0.00055861622855066424871506755481997374154),
231 BOOST_MATH_BIG_CONSTANT(T, 113, 0.1760168552357342401304462967950178554e-4),
232 BOOST_MATH_BIG_CONSTANT(T, 113, 0.20585454493572473724556649516040874384e-6),
233 BOOST_MATH_BIG_CONSTANT(T, 113, -0.90745971844439990284514121823069162795e-11),
234 BOOST_MATH_BIG_CONSTANT(T, 113, 0.48857673606545846774761343500033283272e-13),
241 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
242 T result = g * Y + g * r;
247 // 19-digit precision:
250 T digamma_imp_1_2(T x, const mpl::int_<64>*)
253 // Now the approximation, we use the form:
255 // digamma(x) = (x - root) * (Y + R(x-1))
257 // Where root is the location of the positive root of digamma,
258 // Y is a constant, and R is optimised for low absolute error
261 // Max error found at 80-bit long double precision: 5.016e-20
262 // Maximum Deviation Found (approximation error): 3.575e-20
264 static const float Y = 0.99558162689208984375F;
266 static const T root1 = T(1569415565) / 1073741824uL;
267 static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
268 static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 64, 0.9016312093258695918615325266959189453125e-19);
270 static const T P[] = {
271 BOOST_MATH_BIG_CONSTANT(T, 64, 0.254798510611315515235),
272 BOOST_MATH_BIG_CONSTANT(T, 64, -0.314628554532916496608),
273 BOOST_MATH_BIG_CONSTANT(T, 64, -0.665836341559876230295),
274 BOOST_MATH_BIG_CONSTANT(T, 64, -0.314767657147375752913),
275 BOOST_MATH_BIG_CONSTANT(T, 64, -0.0541156266153505273939),
276 BOOST_MATH_BIG_CONSTANT(T, 64, -0.00289268368333918761452)
278 static const T Q[] = {
279 BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
280 BOOST_MATH_BIG_CONSTANT(T, 64, 2.1195759927055347547),
281 BOOST_MATH_BIG_CONSTANT(T, 64, 1.54350554664961128724),
282 BOOST_MATH_BIG_CONSTANT(T, 64, 0.486986018231042975162),
283 BOOST_MATH_BIG_CONSTANT(T, 64, 0.0660481487173569812846),
284 BOOST_MATH_BIG_CONSTANT(T, 64, 0.00298999662592323990972),
285 BOOST_MATH_BIG_CONSTANT(T, 64, -0.165079794012604905639e-5),
286 BOOST_MATH_BIG_CONSTANT(T, 64, 0.317940243105952177571e-7)
291 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
292 T result = g * Y + g * r;
297 // 18-digit precision:
300 T digamma_imp_1_2(T x, const mpl::int_<53>*)
303 // Now the approximation, we use the form:
305 // digamma(x) = (x - root) * (Y + R(x-1))
307 // Where root is the location of the positive root of digamma,
308 // Y is a constant, and R is optimised for low absolute error
311 // Maximum Deviation Found: 1.466e-18
312 // At double precision, max error found: 2.452e-17
314 static const float Y = 0.99558162689208984F;
316 static const T root1 = T(1569415565) / 1073741824uL;
317 static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
318 static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 53, 0.9016312093258695918615325266959189453125e-19);
320 static const T P[] = {
321 BOOST_MATH_BIG_CONSTANT(T, 53, 0.25479851061131551),
322 BOOST_MATH_BIG_CONSTANT(T, 53, -0.32555031186804491),
323 BOOST_MATH_BIG_CONSTANT(T, 53, -0.65031853770896507),
324 BOOST_MATH_BIG_CONSTANT(T, 53, -0.28919126444774784),
325 BOOST_MATH_BIG_CONSTANT(T, 53, -0.045251321448739056),
326 BOOST_MATH_BIG_CONSTANT(T, 53, -0.0020713321167745952)
328 static const T Q[] = {
329 BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
330 BOOST_MATH_BIG_CONSTANT(T, 53, 2.0767117023730469),
331 BOOST_MATH_BIG_CONSTANT(T, 53, 1.4606242909763515),
332 BOOST_MATH_BIG_CONSTANT(T, 53, 0.43593529692665969),
333 BOOST_MATH_BIG_CONSTANT(T, 53, 0.054151797245674225),
334 BOOST_MATH_BIG_CONSTANT(T, 53, 0.0021284987017821144),
335 BOOST_MATH_BIG_CONSTANT(T, 53, -0.55789841321675513e-6)
340 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
341 T result = g * Y + g * r;
346 // 9-digit precision:
349 inline T digamma_imp_1_2(T x, const mpl::int_<24>*)
352 // Now the approximation, we use the form:
354 // digamma(x) = (x - root) * (Y + R(x-1))
356 // Where root is the location of the positive root of digamma,
357 // Y is a constant, and R is optimised for low absolute error
360 // Maximum Deviation Found: 3.388e-010
361 // At float precision, max error found: 2.008725e-008
363 static const float Y = 0.99558162689208984f;
364 static const T root = 1532632.0f / 1048576;
365 static const T root_minor = static_cast<T>(0.3700660185912626595423257213284682051735604e-6L);
366 static const T P[] = {
367 0.25479851023250261e0f,
368 -0.44981331915268368e0f,
369 -0.43916936919946835e0f,
370 -0.61041765350579073e-1f
372 static const T Q[] = {
374 0.15890202430554952e1f,
375 0.65341249856146947e0f,
376 0.63851690523355715e-1f
380 T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
381 T result = g * Y + g * r;
386 template <class T, class Tag, class Policy>
387 T digamma_imp(T x, const Tag* t, const Policy& pol)
390 // This handles reflection of negative arguments, and all our
391 // error handling, then forwards to the T-specific approximation.
393 BOOST_MATH_STD_USING // ADL of std functions.
397 // Check for negative arguments and use reflection:
403 // Argument reduction for tan:
404 T remainder = x - floor(x);
405 // Shift to negative if > 0.5:
411 // check for evaluation at a negative pole:
415 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, (1-x), pol);
417 result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
420 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, x, pol);
422 // If we're above the lower-limit for the
423 // asymptotic expansion then use it:
425 if(x >= digamma_large_lim(t))
427 result += digamma_imp_large(x, t);
432 // If x > 2 reduce to the interval [1,2]:
440 // If x < 1 use recurrance to shift to > 1:
447 result += digamma_imp_1_2(x, t);
452 template <class T, class Policy>
453 T digamma_imp(T x, const mpl::int_<0>* t, const Policy& pol)
456 // This handles reflection of negative arguments, and all our
457 // error handling, then forwards to the T-specific approximation.
459 BOOST_MATH_STD_USING // ADL of std functions.
463 // Check for negative arguments and use reflection:
469 // Argument reduction for tan:
470 T remainder = x - floor(x);
471 // Shift to negative if > 0.5:
477 // check for evaluation at a negative pole:
481 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, (1 - x), pol);
483 result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
486 return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, x, pol);
488 // If we're above the lower-limit for the
489 // asymptotic expansion then use it, the
490 // limit is a linear interpolation with
491 // limit = 10 at 50 bit precision and
492 // limit = 250 at 1000 bit precision.
494 int lim = 10 + ((tools::digits<T>() - 50) * 240L) / 950;
495 T two_x = ldexp(x, 1);
498 result += digamma_imp_large(x, pol, t);
500 else if(floor(x) == x)
503 // Special case for integer arguments, see
504 // http://functions.wolfram.com/06.14.03.0001.01
506 result = -constants::euler<T, Policy>();
514 else if(floor(two_x) == two_x)
517 // Special case for half integer arguments, see:
518 // http://functions.wolfram.com/06.14.03.0007.01
520 result = -2 * constants::ln_two<T, Policy>() - constants::euler<T, Policy>();
524 for(int k = 1; k < n; ++k)
526 for(int k = n; k <= 2 * n - 1; ++k)
533 // Rescale so we can use the asymptotic expansion:
540 result += digamma_imp_large(x, pol, t);
545 // Initializer: ensure all our constants are initialized prior to the first call of main:
547 template <class T, class Policy>
548 struct digamma_initializer
554 typedef typename policies::precision<T, Policy>::type precision_type;
555 do_init(mpl::bool_<precision_type::value && (precision_type::value <= 113)>());
557 void do_init(const mpl::true_&)
559 boost::math::digamma(T(1.5), Policy());
560 boost::math::digamma(T(500), Policy());
562 void do_init(const mpl::false_&){}
563 void force_instantiate()const{}
565 static const init initializer;
566 static void force_instantiate()
568 initializer.force_instantiate();
572 template <class T, class Policy>
573 const typename digamma_initializer<T, Policy>::init digamma_initializer<T, Policy>::initializer;
575 } // namespace detail
577 template <class T, class Policy>
578 inline typename tools::promote_args<T>::type
579 digamma(T x, const Policy&)
581 typedef typename tools::promote_args<T>::type result_type;
582 typedef typename policies::evaluation<result_type, Policy>::type value_type;
583 typedef typename policies::precision<T, Policy>::type precision_type;
584 typedef typename mpl::if_<
586 mpl::less_equal<precision_type, mpl::int_<0> >,
587 mpl::greater<precision_type, mpl::int_<114> >
591 mpl::less<precision_type, mpl::int_<25> >,
594 mpl::less<precision_type, mpl::int_<54> >,
597 mpl::less<precision_type, mpl::int_<65> >,
605 typedef typename policies::normalise<
607 policies::promote_float<false>,
608 policies::promote_double<false>,
609 policies::discrete_quantile<>,
610 policies::assert_undefined<> >::type forwarding_policy;
612 // Force initialization of constants:
613 detail::digamma_initializer<value_type, forwarding_policy>::force_instantiate();
615 return policies::checked_narrowing_cast<result_type, Policy>(detail::digamma_imp(
616 static_cast<value_type>(x),
617 static_cast<const tag_type*>(0), forwarding_policy()), "boost::math::digamma<%1%>(%1%)");
621 inline typename tools::promote_args<T>::type
624 return digamma(x, policies::policy<>());