digamma.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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)
  5. #ifndef BOOST_MATH_SF_DIGAMMA_HPP
  6. #define BOOST_MATH_SF_DIGAMMA_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #pragma warning(push)
  10. #pragma warning(disable:4702) // Unreachable code (release mode only warning)
  11. #endif
  12. #include <boost/math/special_functions/math_fwd.hpp>
  13. #include <boost/math/tools/rational.hpp>
  14. #include <boost/math/tools/series.hpp>
  15. #include <boost/math/tools/promotion.hpp>
  16. #include <boost/math/policies/error_handling.hpp>
  17. #include <boost/math/constants/constants.hpp>
  18. #include <boost/mpl/comparison.hpp>
  19. #include <boost/math/tools/big_constant.hpp>
  20. namespace boost{
  21. namespace math{
  22. namespace detail{
  23. //
  24. // Begin by defining the smallest value for which it is safe to
  25. // use the asymptotic expansion for digamma:
  26. //
  27. inline unsigned digamma_large_lim(const mpl::int_<0>*)
  28. { return 20; }
  29. inline unsigned digamma_large_lim(const mpl::int_<113>*)
  30. { return 20; }
  31. inline unsigned digamma_large_lim(const void*)
  32. { return 10; }
  33. //
  34. // Implementations of the asymptotic expansion come next,
  35. // the coefficients of the series have been evaluated
  36. // in advance at high precision, and the series truncated
  37. // at the first term that's too small to effect the result.
  38. // Note that the series becomes divergent after a while
  39. // so truncation is very important.
  40. //
  41. // This first one gives 34-digit precision for x >= 20:
  42. //
  43. template <class T>
  44. inline T digamma_imp_large(T x, const mpl::int_<113>*)
  45. {
  46. BOOST_MATH_STD_USING // ADL of std functions.
  47. static const T P[] = {
  48. BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
  49. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0083333333333333333333333333333333333333333333333333),
  50. BOOST_MATH_BIG_CONSTANT(T, 113, 0.003968253968253968253968253968253968253968253968254),
  51. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0041666666666666666666666666666666666666666666666667),
  52. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0075757575757575757575757575757575757575757575757576),
  53. BOOST_MATH_BIG_CONSTANT(T, 113, -0.021092796092796092796092796092796092796092796092796),
  54. BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
  55. BOOST_MATH_BIG_CONSTANT(T, 113, -0.44325980392156862745098039215686274509803921568627),
  56. BOOST_MATH_BIG_CONSTANT(T, 113, 3.0539543302701197438039543302701197438039543302701),
  57. BOOST_MATH_BIG_CONSTANT(T, 113, -26.456212121212121212121212121212121212121212121212),
  58. BOOST_MATH_BIG_CONSTANT(T, 113, 281.4601449275362318840579710144927536231884057971),
  59. BOOST_MATH_BIG_CONSTANT(T, 113, -3607.510546398046398046398046398046398046398046398),
  60. BOOST_MATH_BIG_CONSTANT(T, 113, 54827.583333333333333333333333333333333333333333333),
  61. BOOST_MATH_BIG_CONSTANT(T, 113, -974936.82385057471264367816091954022988505747126437),
  62. BOOST_MATH_BIG_CONSTANT(T, 113, 20052695.796688078946143462272494530559046688078946),
  63. BOOST_MATH_BIG_CONSTANT(T, 113, -472384867.72162990196078431372549019607843137254902),
  64. BOOST_MATH_BIG_CONSTANT(T, 113, 12635724795.916666666666666666666666666666666666667)
  65. };
  66. x -= 1;
  67. T result = log(x);
  68. result += 1 / (2 * x);
  69. T z = 1 / (x*x);
  70. result -= z * tools::evaluate_polynomial(P, z);
  71. return result;
  72. }
  73. //
  74. // 19-digit precision for x >= 10:
  75. //
  76. template <class T>
  77. inline T digamma_imp_large(T x, const mpl::int_<64>*)
  78. {
  79. BOOST_MATH_STD_USING // ADL of std functions.
  80. static const T P[] = {
  81. BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
  82. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0083333333333333333333333333333333333333333333333333),
  83. BOOST_MATH_BIG_CONSTANT(T, 64, 0.003968253968253968253968253968253968253968253968254),
  84. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0041666666666666666666666666666666666666666666666667),
  85. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0075757575757575757575757575757575757575757575757576),
  86. BOOST_MATH_BIG_CONSTANT(T, 64, -0.021092796092796092796092796092796092796092796092796),
  87. BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
  88. BOOST_MATH_BIG_CONSTANT(T, 64, -0.44325980392156862745098039215686274509803921568627),
  89. BOOST_MATH_BIG_CONSTANT(T, 64, 3.0539543302701197438039543302701197438039543302701),
  90. BOOST_MATH_BIG_CONSTANT(T, 64, -26.456212121212121212121212121212121212121212121212),
  91. BOOST_MATH_BIG_CONSTANT(T, 64, 281.4601449275362318840579710144927536231884057971),
  92. };
  93. x -= 1;
  94. T result = log(x);
  95. result += 1 / (2 * x);
  96. T z = 1 / (x*x);
  97. result -= z * tools::evaluate_polynomial(P, z);
  98. return result;
  99. }
  100. //
  101. // 17-digit precision for x >= 10:
  102. //
  103. template <class T>
  104. inline T digamma_imp_large(T x, const mpl::int_<53>*)
  105. {
  106. BOOST_MATH_STD_USING // ADL of std functions.
  107. static const T P[] = {
  108. BOOST_MATH_BIG_CONSTANT(T, 53, 0.083333333333333333333333333333333333333333333333333),
  109. BOOST_MATH_BIG_CONSTANT(T, 53, -0.0083333333333333333333333333333333333333333333333333),
  110. BOOST_MATH_BIG_CONSTANT(T, 53, 0.003968253968253968253968253968253968253968253968254),
  111. BOOST_MATH_BIG_CONSTANT(T, 53, -0.0041666666666666666666666666666666666666666666666667),
  112. BOOST_MATH_BIG_CONSTANT(T, 53, 0.0075757575757575757575757575757575757575757575757576),
  113. BOOST_MATH_BIG_CONSTANT(T, 53, -0.021092796092796092796092796092796092796092796092796),
  114. BOOST_MATH_BIG_CONSTANT(T, 53, 0.083333333333333333333333333333333333333333333333333),
  115. BOOST_MATH_BIG_CONSTANT(T, 53, -0.44325980392156862745098039215686274509803921568627)
  116. };
  117. x -= 1;
  118. T result = log(x);
  119. result += 1 / (2 * x);
  120. T z = 1 / (x*x);
  121. result -= z * tools::evaluate_polynomial(P, z);
  122. return result;
  123. }
  124. //
  125. // 9-digit precision for x >= 10:
  126. //
  127. template <class T>
  128. inline T digamma_imp_large(T x, const mpl::int_<24>*)
  129. {
  130. BOOST_MATH_STD_USING // ADL of std functions.
  131. static const T P[] = {
  132. BOOST_MATH_BIG_CONSTANT(T, 24, 0.083333333333333333333333333333333333333333333333333),
  133. BOOST_MATH_BIG_CONSTANT(T, 24, -0.0083333333333333333333333333333333333333333333333333),
  134. BOOST_MATH_BIG_CONSTANT(T, 24, 0.003968253968253968253968253968253968253968253968254)
  135. };
  136. x -= 1;
  137. T result = log(x);
  138. result += 1 / (2 * x);
  139. T z = 1 / (x*x);
  140. result -= z * tools::evaluate_polynomial(P, z);
  141. return result;
  142. }
  143. //
  144. // Fully generic asymptotic expansion in terms of Bernoulli numbers, see:
  145. // http://functions.wolfram.com/06.14.06.0012.01
  146. //
  147. template <class T>
  148. struct digamma_series_func
  149. {
  150. private:
  151. int k;
  152. T xx;
  153. T term;
  154. public:
  155. digamma_series_func(T x) : k(1), xx(x * x), term(1 / (x * x)) {}
  156. T operator()()
  157. {
  158. T result = term * boost::math::bernoulli_b2n<T>(k) / (2 * k);
  159. term /= xx;
  160. ++k;
  161. return result;
  162. }
  163. typedef T result_type;
  164. };
  165. template <class T, class Policy>
  166. inline T digamma_imp_large(T x, const Policy& pol, const mpl::int_<0>*)
  167. {
  168. BOOST_MATH_STD_USING
  169. digamma_series_func<T> s(x);
  170. T result = log(x) - 1 / (2 * x);
  171. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  172. result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, -result);
  173. result = -result;
  174. policies::check_series_iterations<T>("boost::math::digamma<%1%>(%1%)", max_iter, pol);
  175. return result;
  176. }
  177. //
  178. // Now follow rational approximations over the range [1,2].
  179. //
  180. // 35-digit precision:
  181. //
  182. template <class T>
  183. T digamma_imp_1_2(T x, const mpl::int_<113>*)
  184. {
  185. //
  186. // Now the approximation, we use the form:
  187. //
  188. // digamma(x) = (x - root) * (Y + R(x-1))
  189. //
  190. // Where root is the location of the positive root of digamma,
  191. // Y is a constant, and R is optimised for low absolute error
  192. // compared to Y.
  193. //
  194. // Max error found at 128-bit long double precision: 5.541e-35
  195. // Maximum Deviation Found (approximation error): 1.965e-35
  196. //
  197. static const float Y = 0.99558162689208984375F;
  198. static const T root1 = T(1569415565) / 1073741824uL;
  199. static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
  200. static const T root3 = ((T(111616537) / 1073741824uL) / 1073741824uL) / 1073741824uL;
  201. static const T root4 = (((T(503992070) / 1073741824uL) / 1073741824uL) / 1073741824uL) / 1073741824uL;
  202. static const T root5 = BOOST_MATH_BIG_CONSTANT(T, 113, 0.52112228569249997894452490385577338504019838794544e-36);
  203. static const T P[] = {
  204. BOOST_MATH_BIG_CONSTANT(T, 113, 0.25479851061131551526977464225335883769),
  205. BOOST_MATH_BIG_CONSTANT(T, 113, -0.18684290534374944114622235683619897417),
  206. BOOST_MATH_BIG_CONSTANT(T, 113, -0.80360876047931768958995775910991929922),
  207. BOOST_MATH_BIG_CONSTANT(T, 113, -0.67227342794829064330498117008564270136),
  208. BOOST_MATH_BIG_CONSTANT(T, 113, -0.26569010991230617151285010695543858005),
  209. BOOST_MATH_BIG_CONSTANT(T, 113, -0.05775672694575986971640757748003553385),
  210. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0071432147823164975485922555833274240665),
  211. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00048740753910766168912364555706064993274),
  212. BOOST_MATH_BIG_CONSTANT(T, 113, -0.16454996865214115723416538844975174761e-4),
  213. BOOST_MATH_BIG_CONSTANT(T, 113, -0.20327832297631728077731148515093164955e-6)
  214. };
  215. static const T Q[] = {
  216. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  217. BOOST_MATH_BIG_CONSTANT(T, 113, 2.6210924610812025425088411043163287646),
  218. BOOST_MATH_BIG_CONSTANT(T, 113, 2.6850757078559596612621337395886392594),
  219. BOOST_MATH_BIG_CONSTANT(T, 113, 1.4320913706209965531250495490639289418),
  220. BOOST_MATH_BIG_CONSTANT(T, 113, 0.4410872083455009362557012239501953402),
  221. BOOST_MATH_BIG_CONSTANT(T, 113, 0.081385727399251729505165509278152487225),
  222. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0089478633066857163432104815183858149496),
  223. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00055861622855066424871506755481997374154),
  224. BOOST_MATH_BIG_CONSTANT(T, 113, 0.1760168552357342401304462967950178554e-4),
  225. BOOST_MATH_BIG_CONSTANT(T, 113, 0.20585454493572473724556649516040874384e-6),
  226. BOOST_MATH_BIG_CONSTANT(T, 113, -0.90745971844439990284514121823069162795e-11),
  227. BOOST_MATH_BIG_CONSTANT(T, 113, 0.48857673606545846774761343500033283272e-13),
  228. };
  229. T g = x - root1;
  230. g -= root2;
  231. g -= root3;
  232. g -= root4;
  233. g -= root5;
  234. T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
  235. T result = g * Y + g * r;
  236. return result;
  237. }
  238. //
  239. // 19-digit precision:
  240. //
  241. template <class T>
  242. T digamma_imp_1_2(T x, const mpl::int_<64>*)
  243. {
  244. //
  245. // Now the approximation, we use the form:
  246. //
  247. // digamma(x) = (x - root) * (Y + R(x-1))
  248. //
  249. // Where root is the location of the positive root of digamma,
  250. // Y is a constant, and R is optimised for low absolute error
  251. // compared to Y.
  252. //
  253. // Max error found at 80-bit long double precision: 5.016e-20
  254. // Maximum Deviation Found (approximation error): 3.575e-20
  255. //
  256. static const float Y = 0.99558162689208984375F;
  257. static const T root1 = T(1569415565) / 1073741824uL;
  258. static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
  259. static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 64, 0.9016312093258695918615325266959189453125e-19);
  260. static const T P[] = {
  261. BOOST_MATH_BIG_CONSTANT(T, 64, 0.254798510611315515235),
  262. BOOST_MATH_BIG_CONSTANT(T, 64, -0.314628554532916496608),
  263. BOOST_MATH_BIG_CONSTANT(T, 64, -0.665836341559876230295),
  264. BOOST_MATH_BIG_CONSTANT(T, 64, -0.314767657147375752913),
  265. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0541156266153505273939),
  266. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00289268368333918761452)
  267. };
  268. static const T Q[] = {
  269. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  270. BOOST_MATH_BIG_CONSTANT(T, 64, 2.1195759927055347547),
  271. BOOST_MATH_BIG_CONSTANT(T, 64, 1.54350554664961128724),
  272. BOOST_MATH_BIG_CONSTANT(T, 64, 0.486986018231042975162),
  273. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0660481487173569812846),
  274. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00298999662592323990972),
  275. BOOST_MATH_BIG_CONSTANT(T, 64, -0.165079794012604905639e-5),
  276. BOOST_MATH_BIG_CONSTANT(T, 64, 0.317940243105952177571e-7)
  277. };
  278. T g = x - root1;
  279. g -= root2;
  280. g -= root3;
  281. T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
  282. T result = g * Y + g * r;
  283. return result;
  284. }
  285. //
  286. // 18-digit precision:
  287. //
  288. template <class T>
  289. T digamma_imp_1_2(T x, const mpl::int_<53>*)
  290. {
  291. //
  292. // Now the approximation, we use the form:
  293. //
  294. // digamma(x) = (x - root) * (Y + R(x-1))
  295. //
  296. // Where root is the location of the positive root of digamma,
  297. // Y is a constant, and R is optimised for low absolute error
  298. // compared to Y.
  299. //
  300. // Maximum Deviation Found: 1.466e-18
  301. // At double precision, max error found: 2.452e-17
  302. //
  303. static const float Y = 0.99558162689208984F;
  304. static const T root1 = T(1569415565) / 1073741824uL;
  305. static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
  306. static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 53, 0.9016312093258695918615325266959189453125e-19);
  307. static const T P[] = {
  308. BOOST_MATH_BIG_CONSTANT(T, 53, 0.25479851061131551),
  309. BOOST_MATH_BIG_CONSTANT(T, 53, -0.32555031186804491),
  310. BOOST_MATH_BIG_CONSTANT(T, 53, -0.65031853770896507),
  311. BOOST_MATH_BIG_CONSTANT(T, 53, -0.28919126444774784),
  312. BOOST_MATH_BIG_CONSTANT(T, 53, -0.045251321448739056),
  313. BOOST_MATH_BIG_CONSTANT(T, 53, -0.0020713321167745952)
  314. };
  315. static const T Q[] = {
  316. BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
  317. BOOST_MATH_BIG_CONSTANT(T, 53, 2.0767117023730469),
  318. BOOST_MATH_BIG_CONSTANT(T, 53, 1.4606242909763515),
  319. BOOST_MATH_BIG_CONSTANT(T, 53, 0.43593529692665969),
  320. BOOST_MATH_BIG_CONSTANT(T, 53, 0.054151797245674225),
  321. BOOST_MATH_BIG_CONSTANT(T, 53, 0.0021284987017821144),
  322. BOOST_MATH_BIG_CONSTANT(T, 53, -0.55789841321675513e-6)
  323. };
  324. T g = x - root1;
  325. g -= root2;
  326. g -= root3;
  327. T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
  328. T result = g * Y + g * r;
  329. return result;
  330. }
  331. //
  332. // 9-digit precision:
  333. //
  334. template <class T>
  335. inline T digamma_imp_1_2(T x, const mpl::int_<24>*)
  336. {
  337. //
  338. // Now the approximation, we use the form:
  339. //
  340. // digamma(x) = (x - root) * (Y + R(x-1))
  341. //
  342. // Where root is the location of the positive root of digamma,
  343. // Y is a constant, and R is optimised for low absolute error
  344. // compared to Y.
  345. //
  346. // Maximum Deviation Found: 3.388e-010
  347. // At float precision, max error found: 2.008725e-008
  348. //
  349. static const float Y = 0.99558162689208984f;
  350. static const T root = 1532632.0f / 1048576;
  351. static const T root_minor = static_cast<T>(0.3700660185912626595423257213284682051735604e-6L);
  352. static const T P[] = {
  353. 0.25479851023250261e0f,
  354. -0.44981331915268368e0f,
  355. -0.43916936919946835e0f,
  356. -0.61041765350579073e-1f
  357. };
  358. static const T Q[] = {
  359. 0.1e1,
  360. 0.15890202430554952e1f,
  361. 0.65341249856146947e0f,
  362. 0.63851690523355715e-1f
  363. };
  364. T g = x - root;
  365. g -= root_minor;
  366. T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
  367. T result = g * Y + g * r;
  368. return result;
  369. }
  370. template <class T, class Tag, class Policy>
  371. T digamma_imp(T x, const Tag* t, const Policy& pol)
  372. {
  373. //
  374. // This handles reflection of negative arguments, and all our
  375. // error handling, then forwards to the T-specific approximation.
  376. //
  377. BOOST_MATH_STD_USING // ADL of std functions.
  378. T result = 0;
  379. //
  380. // Check for negative arguments and use reflection:
  381. //
  382. if(x <= -1)
  383. {
  384. // Reflect:
  385. x = 1 - x;
  386. // Argument reduction for tan:
  387. T remainder = x - floor(x);
  388. // Shift to negative if > 0.5:
  389. if(remainder > 0.5)
  390. {
  391. remainder -= 1;
  392. }
  393. //
  394. // check for evaluation at a negative pole:
  395. //
  396. if(remainder == 0)
  397. {
  398. return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, (1-x), pol);
  399. }
  400. result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
  401. }
  402. if(x == 0)
  403. return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, x, pol);
  404. //
  405. // If we're above the lower-limit for the
  406. // asymptotic expansion then use it:
  407. //
  408. if(x >= digamma_large_lim(t))
  409. {
  410. result += digamma_imp_large(x, t);
  411. }
  412. else
  413. {
  414. //
  415. // If x > 2 reduce to the interval [1,2]:
  416. //
  417. while(x > 2)
  418. {
  419. x -= 1;
  420. result += 1/x;
  421. }
  422. //
  423. // If x < 1 use recurrance to shift to > 1:
  424. //
  425. while(x < 1)
  426. {
  427. result -= 1/x;
  428. x += 1;
  429. }
  430. result += digamma_imp_1_2(x, t);
  431. }
  432. return result;
  433. }
  434. template <class T, class Policy>
  435. T digamma_imp(T x, const mpl::int_<0>* t, const Policy& pol)
  436. {
  437. //
  438. // This handles reflection of negative arguments, and all our
  439. // error handling, then forwards to the T-specific approximation.
  440. //
  441. BOOST_MATH_STD_USING // ADL of std functions.
  442. T result = 0;
  443. //
  444. // Check for negative arguments and use reflection:
  445. //
  446. if(x <= -1)
  447. {
  448. // Reflect:
  449. x = 1 - x;
  450. // Argument reduction for tan:
  451. T remainder = x - floor(x);
  452. // Shift to negative if > 0.5:
  453. if(remainder > 0.5)
  454. {
  455. remainder -= 1;
  456. }
  457. //
  458. // check for evaluation at a negative pole:
  459. //
  460. if(remainder == 0)
  461. {
  462. return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, (1 - x), pol);
  463. }
  464. result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
  465. }
  466. if(x == 0)
  467. return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, x, pol);
  468. //
  469. // If we're above the lower-limit for the
  470. // asymptotic expansion then use it, the
  471. // limit is a linear interpolation with
  472. // limit = 10 at 50 bit precision and
  473. // limit = 250 at 1000 bit precision.
  474. //
  475. int lim = 10 + ((tools::digits<T>() - 50) * 240L) / 950;
  476. T two_x = ldexp(x, 1);
  477. if(x >= lim)
  478. {
  479. result += digamma_imp_large(x, pol, t);
  480. }
  481. else if(floor(x) == x)
  482. {
  483. //
  484. // Special case for integer arguments, see
  485. // http://functions.wolfram.com/06.14.03.0001.01
  486. //
  487. result = -constants::euler<T, Policy>();
  488. T val = 1;
  489. while(val < x)
  490. {
  491. result += 1 / val;
  492. val += 1;
  493. }
  494. }
  495. else if(floor(two_x) == two_x)
  496. {
  497. //
  498. // Special case for half integer arguments, see:
  499. // http://functions.wolfram.com/06.14.03.0007.01
  500. //
  501. result = -2 * constants::ln_two<T, Policy>() - constants::euler<T, Policy>();
  502. int n = itrunc(x);
  503. if(n)
  504. {
  505. for(int k = 1; k < n; ++k)
  506. result += 1 / T(k);
  507. for(int k = n; k <= 2 * n - 1; ++k)
  508. result += 2 / T(k);
  509. }
  510. }
  511. else
  512. {
  513. //
  514. // Rescale so we can use the asymptotic expansion:
  515. //
  516. while(x < lim)
  517. {
  518. result -= 1 / x;
  519. x += 1;
  520. }
  521. result += digamma_imp_large(x, pol, t);
  522. }
  523. return result;
  524. }
  525. //
  526. // Initializer: ensure all our constants are initialized prior to the first call of main:
  527. //
  528. template <class T, class Policy>
  529. struct digamma_initializer
  530. {
  531. struct init
  532. {
  533. init()
  534. {
  535. typedef typename policies::precision<T, Policy>::type precision_type;
  536. do_init(mpl::bool_<precision_type::value && (precision_type::value <= 113)>());
  537. }
  538. void do_init(const mpl::true_&)
  539. {
  540. boost::math::digamma(T(1.5), Policy());
  541. boost::math::digamma(T(500), Policy());
  542. }
  543. void do_init(const mpl::false_&){}
  544. void force_instantiate()const{}
  545. };
  546. static const init initializer;
  547. static void force_instantiate()
  548. {
  549. initializer.force_instantiate();
  550. }
  551. };
  552. template <class T, class Policy>
  553. const typename digamma_initializer<T, Policy>::init digamma_initializer<T, Policy>::initializer;
  554. } // namespace detail
  555. template <class T, class Policy>
  556. inline typename tools::promote_args<T>::type
  557. digamma(T x, const Policy&)
  558. {
  559. typedef typename tools::promote_args<T>::type result_type;
  560. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  561. typedef typename policies::precision<T, Policy>::type precision_type;
  562. typedef typename mpl::if_<
  563. mpl::or_<
  564. mpl::less_equal<precision_type, mpl::int_<0> >,
  565. mpl::greater<precision_type, mpl::int_<114> >
  566. >,
  567. mpl::int_<0>,
  568. typename mpl::if_<
  569. mpl::less<precision_type, mpl::int_<25> >,
  570. mpl::int_<24>,
  571. typename mpl::if_<
  572. mpl::less<precision_type, mpl::int_<54> >,
  573. mpl::int_<53>,
  574. typename mpl::if_<
  575. mpl::less<precision_type, mpl::int_<65> >,
  576. mpl::int_<64>,
  577. mpl::int_<113>
  578. >::type
  579. >::type
  580. >::type
  581. >::type tag_type;
  582. typedef typename policies::normalise<
  583. Policy,
  584. policies::promote_float<false>,
  585. policies::promote_double<false>,
  586. policies::discrete_quantile<>,
  587. policies::assert_undefined<> >::type forwarding_policy;
  588. // Force initialization of constants:
  589. detail::digamma_initializer<value_type, forwarding_policy>::force_instantiate();
  590. return policies::checked_narrowing_cast<result_type, Policy>(detail::digamma_imp(
  591. static_cast<value_type>(x),
  592. static_cast<const tag_type*>(0), forwarding_policy()), "boost::math::digamma<%1%>(%1%)");
  593. }
  594. template <class T>
  595. inline typename tools::promote_args<T>::type
  596. digamma(T x)
  597. {
  598. return digamma(x, policies::policy<>());
  599. }
  600. } // namespace math
  601. } // namespace boost
  602. #ifdef _MSC_VER
  603. #pragma warning(pop)
  604. #endif
  605. #endif