digamma.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #endif
  10. #include <boost/math/tools/rational.hpp>
  11. #include <boost/math/tools/promotion.hpp>
  12. #include <boost/math/policies/error_handling.hpp>
  13. #include <boost/math/constants/constants.hpp>
  14. #include <boost/mpl/comparison.hpp>
  15. #include <boost/math/tools/big_constant.hpp>
  16. namespace boost{
  17. namespace math{
  18. namespace detail{
  19. //
  20. // Begin by defining the smallest value for which it is safe to
  21. // use the asymptotic expansion for digamma:
  22. //
  23. inline unsigned digamma_large_lim(const mpl::int_<0>*)
  24. { return 20; }
  25. inline unsigned digamma_large_lim(const void*)
  26. { return 10; }
  27. //
  28. // Implementations of the asymptotic expansion come next,
  29. // the coefficients of the series have been evaluated
  30. // in advance at high precision, and the series truncated
  31. // at the first term that's too small to effect the result.
  32. // Note that the series becomes divergent after a while
  33. // so truncation is very important.
  34. //
  35. // This first one gives 34-digit precision for x >= 20:
  36. //
  37. template <class T>
  38. inline T digamma_imp_large(T x, const mpl::int_<0>*)
  39. {
  40. BOOST_MATH_STD_USING // ADL of std functions.
  41. static const T P[] = {
  42. BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
  43. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0083333333333333333333333333333333333333333333333333),
  44. BOOST_MATH_BIG_CONSTANT(T, 113, 0.003968253968253968253968253968253968253968253968254),
  45. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0041666666666666666666666666666666666666666666666667),
  46. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0075757575757575757575757575757575757575757575757576),
  47. BOOST_MATH_BIG_CONSTANT(T, 113, -0.021092796092796092796092796092796092796092796092796),
  48. BOOST_MATH_BIG_CONSTANT(T, 113, 0.083333333333333333333333333333333333333333333333333),
  49. BOOST_MATH_BIG_CONSTANT(T, 113, -0.44325980392156862745098039215686274509803921568627),
  50. BOOST_MATH_BIG_CONSTANT(T, 113, 3.0539543302701197438039543302701197438039543302701),
  51. BOOST_MATH_BIG_CONSTANT(T, 113, -26.456212121212121212121212121212121212121212121212),
  52. BOOST_MATH_BIG_CONSTANT(T, 113, 281.4601449275362318840579710144927536231884057971),
  53. BOOST_MATH_BIG_CONSTANT(T, 113, -3607.510546398046398046398046398046398046398046398),
  54. BOOST_MATH_BIG_CONSTANT(T, 113, 54827.583333333333333333333333333333333333333333333),
  55. BOOST_MATH_BIG_CONSTANT(T, 113, -974936.82385057471264367816091954022988505747126437),
  56. BOOST_MATH_BIG_CONSTANT(T, 113, 20052695.796688078946143462272494530559046688078946),
  57. BOOST_MATH_BIG_CONSTANT(T, 113, -472384867.72162990196078431372549019607843137254902),
  58. BOOST_MATH_BIG_CONSTANT(T, 113, 12635724795.916666666666666666666666666666666666667)
  59. };
  60. x -= 1;
  61. T result = log(x);
  62. result += 1 / (2 * x);
  63. T z = 1 / (x*x);
  64. result -= z * tools::evaluate_polynomial(P, z);
  65. return result;
  66. }
  67. //
  68. // 19-digit precision for x >= 10:
  69. //
  70. template <class T>
  71. inline T digamma_imp_large(T x, const mpl::int_<64>*)
  72. {
  73. BOOST_MATH_STD_USING // ADL of std functions.
  74. static const T P[] = {
  75. BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
  76. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0083333333333333333333333333333333333333333333333333),
  77. BOOST_MATH_BIG_CONSTANT(T, 64, 0.003968253968253968253968253968253968253968253968254),
  78. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0041666666666666666666666666666666666666666666666667),
  79. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0075757575757575757575757575757575757575757575757576),
  80. BOOST_MATH_BIG_CONSTANT(T, 64, -0.021092796092796092796092796092796092796092796092796),
  81. BOOST_MATH_BIG_CONSTANT(T, 64, 0.083333333333333333333333333333333333333333333333333),
  82. BOOST_MATH_BIG_CONSTANT(T, 64, -0.44325980392156862745098039215686274509803921568627),
  83. BOOST_MATH_BIG_CONSTANT(T, 64, 3.0539543302701197438039543302701197438039543302701),
  84. BOOST_MATH_BIG_CONSTANT(T, 64, -26.456212121212121212121212121212121212121212121212),
  85. BOOST_MATH_BIG_CONSTANT(T, 64, 281.4601449275362318840579710144927536231884057971),
  86. };
  87. x -= 1;
  88. T result = log(x);
  89. result += 1 / (2 * x);
  90. T z = 1 / (x*x);
  91. result -= z * tools::evaluate_polynomial(P, z);
  92. return result;
  93. }
  94. //
  95. // 17-digit precision for x >= 10:
  96. //
  97. template <class T>
  98. inline T digamma_imp_large(T x, const mpl::int_<53>*)
  99. {
  100. BOOST_MATH_STD_USING // ADL of std functions.
  101. static const T P[] = {
  102. BOOST_MATH_BIG_CONSTANT(T, 53, 0.083333333333333333333333333333333333333333333333333),
  103. BOOST_MATH_BIG_CONSTANT(T, 53, -0.0083333333333333333333333333333333333333333333333333),
  104. BOOST_MATH_BIG_CONSTANT(T, 53, 0.003968253968253968253968253968253968253968253968254),
  105. BOOST_MATH_BIG_CONSTANT(T, 53, -0.0041666666666666666666666666666666666666666666666667),
  106. BOOST_MATH_BIG_CONSTANT(T, 53, 0.0075757575757575757575757575757575757575757575757576),
  107. BOOST_MATH_BIG_CONSTANT(T, 53, -0.021092796092796092796092796092796092796092796092796),
  108. BOOST_MATH_BIG_CONSTANT(T, 53, 0.083333333333333333333333333333333333333333333333333),
  109. BOOST_MATH_BIG_CONSTANT(T, 53, -0.44325980392156862745098039215686274509803921568627)
  110. };
  111. x -= 1;
  112. T result = log(x);
  113. result += 1 / (2 * x);
  114. T z = 1 / (x*x);
  115. result -= z * tools::evaluate_polynomial(P, z);
  116. return result;
  117. }
  118. //
  119. // 9-digit precision for x >= 10:
  120. //
  121. template <class T>
  122. inline T digamma_imp_large(T x, const mpl::int_<24>*)
  123. {
  124. BOOST_MATH_STD_USING // ADL of std functions.
  125. static const T P[] = {
  126. BOOST_MATH_BIG_CONSTANT(T, 24, 0.083333333333333333333333333333333333333333333333333),
  127. BOOST_MATH_BIG_CONSTANT(T, 24, -0.0083333333333333333333333333333333333333333333333333),
  128. BOOST_MATH_BIG_CONSTANT(T, 24, 0.003968253968253968253968253968253968253968253968254)
  129. };
  130. x -= 1;
  131. T result = log(x);
  132. result += 1 / (2 * x);
  133. T z = 1 / (x*x);
  134. result -= z * tools::evaluate_polynomial(P, z);
  135. return result;
  136. }
  137. //
  138. // Now follow rational approximations over the range [1,2].
  139. //
  140. // 35-digit precision:
  141. //
  142. template <class T>
  143. T digamma_imp_1_2(T x, const mpl::int_<0>*)
  144. {
  145. //
  146. // Now the approximation, we use the form:
  147. //
  148. // digamma(x) = (x - root) * (Y + R(x-1))
  149. //
  150. // Where root is the location of the positive root of digamma,
  151. // Y is a constant, and R is optimised for low absolute error
  152. // compared to Y.
  153. //
  154. // Max error found at 128-bit long double precision: 5.541e-35
  155. // Maximum Deviation Found (approximation error): 1.965e-35
  156. //
  157. static const float Y = 0.99558162689208984375F;
  158. static const T root1 = T(1569415565) / 1073741824uL;
  159. static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
  160. static const T root3 = ((T(111616537) / 1073741824uL) / 1073741824uL) / 1073741824uL;
  161. static const T root4 = (((T(503992070) / 1073741824uL) / 1073741824uL) / 1073741824uL) / 1073741824uL;
  162. static const T root5 = BOOST_MATH_BIG_CONSTANT(T, 113, 0.52112228569249997894452490385577338504019838794544e-36);
  163. static const T P[] = {
  164. BOOST_MATH_BIG_CONSTANT(T, 113, 0.25479851061131551526977464225335883769),
  165. BOOST_MATH_BIG_CONSTANT(T, 113, -0.18684290534374944114622235683619897417),
  166. BOOST_MATH_BIG_CONSTANT(T, 113, -0.80360876047931768958995775910991929922),
  167. BOOST_MATH_BIG_CONSTANT(T, 113, -0.67227342794829064330498117008564270136),
  168. BOOST_MATH_BIG_CONSTANT(T, 113, -0.26569010991230617151285010695543858005),
  169. BOOST_MATH_BIG_CONSTANT(T, 113, -0.05775672694575986971640757748003553385),
  170. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0071432147823164975485922555833274240665),
  171. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00048740753910766168912364555706064993274),
  172. BOOST_MATH_BIG_CONSTANT(T, 113, -0.16454996865214115723416538844975174761e-4),
  173. BOOST_MATH_BIG_CONSTANT(T, 113, -0.20327832297631728077731148515093164955e-6)
  174. };
  175. static const T Q[] = {
  176. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  177. BOOST_MATH_BIG_CONSTANT(T, 113, 2.6210924610812025425088411043163287646),
  178. BOOST_MATH_BIG_CONSTANT(T, 113, 2.6850757078559596612621337395886392594),
  179. BOOST_MATH_BIG_CONSTANT(T, 113, 1.4320913706209965531250495490639289418),
  180. BOOST_MATH_BIG_CONSTANT(T, 113, 0.4410872083455009362557012239501953402),
  181. BOOST_MATH_BIG_CONSTANT(T, 113, 0.081385727399251729505165509278152487225),
  182. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0089478633066857163432104815183858149496),
  183. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00055861622855066424871506755481997374154),
  184. BOOST_MATH_BIG_CONSTANT(T, 113, 0.1760168552357342401304462967950178554e-4),
  185. BOOST_MATH_BIG_CONSTANT(T, 113, 0.20585454493572473724556649516040874384e-6),
  186. BOOST_MATH_BIG_CONSTANT(T, 113, -0.90745971844439990284514121823069162795e-11),
  187. BOOST_MATH_BIG_CONSTANT(T, 113, 0.48857673606545846774761343500033283272e-13),
  188. };
  189. T g = x - root1;
  190. g -= root2;
  191. g -= root3;
  192. g -= root4;
  193. g -= root5;
  194. T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
  195. T result = g * Y + g * r;
  196. return result;
  197. }
  198. //
  199. // 19-digit precision:
  200. //
  201. template <class T>
  202. T digamma_imp_1_2(T x, const mpl::int_<64>*)
  203. {
  204. //
  205. // Now the approximation, we use the form:
  206. //
  207. // digamma(x) = (x - root) * (Y + R(x-1))
  208. //
  209. // Where root is the location of the positive root of digamma,
  210. // Y is a constant, and R is optimised for low absolute error
  211. // compared to Y.
  212. //
  213. // Max error found at 80-bit long double precision: 5.016e-20
  214. // Maximum Deviation Found (approximation error): 3.575e-20
  215. //
  216. static const float Y = 0.99558162689208984375F;
  217. static const T root1 = T(1569415565) / 1073741824uL;
  218. static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
  219. static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 64, 0.9016312093258695918615325266959189453125e-19);
  220. static const T P[] = {
  221. BOOST_MATH_BIG_CONSTANT(T, 64, 0.254798510611315515235),
  222. BOOST_MATH_BIG_CONSTANT(T, 64, -0.314628554532916496608),
  223. BOOST_MATH_BIG_CONSTANT(T, 64, -0.665836341559876230295),
  224. BOOST_MATH_BIG_CONSTANT(T, 64, -0.314767657147375752913),
  225. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0541156266153505273939),
  226. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00289268368333918761452)
  227. };
  228. static const T Q[] = {
  229. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  230. BOOST_MATH_BIG_CONSTANT(T, 64, 2.1195759927055347547),
  231. BOOST_MATH_BIG_CONSTANT(T, 64, 1.54350554664961128724),
  232. BOOST_MATH_BIG_CONSTANT(T, 64, 0.486986018231042975162),
  233. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0660481487173569812846),
  234. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00298999662592323990972),
  235. BOOST_MATH_BIG_CONSTANT(T, 64, -0.165079794012604905639e-5),
  236. BOOST_MATH_BIG_CONSTANT(T, 64, 0.317940243105952177571e-7)
  237. };
  238. T g = x - root1;
  239. g -= root2;
  240. g -= root3;
  241. T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
  242. T result = g * Y + g * r;
  243. return result;
  244. }
  245. //
  246. // 18-digit precision:
  247. //
  248. template <class T>
  249. T digamma_imp_1_2(T x, const mpl::int_<53>*)
  250. {
  251. //
  252. // Now the approximation, we use the form:
  253. //
  254. // digamma(x) = (x - root) * (Y + R(x-1))
  255. //
  256. // Where root is the location of the positive root of digamma,
  257. // Y is a constant, and R is optimised for low absolute error
  258. // compared to Y.
  259. //
  260. // Maximum Deviation Found: 1.466e-18
  261. // At double precision, max error found: 2.452e-17
  262. //
  263. static const float Y = 0.99558162689208984F;
  264. static const T root1 = T(1569415565) / 1073741824uL;
  265. static const T root2 = (T(381566830) / 1073741824uL) / 1073741824uL;
  266. static const T root3 = BOOST_MATH_BIG_CONSTANT(T, 53, 0.9016312093258695918615325266959189453125e-19);
  267. static const T P[] = {
  268. BOOST_MATH_BIG_CONSTANT(T, 53, 0.25479851061131551),
  269. BOOST_MATH_BIG_CONSTANT(T, 53, -0.32555031186804491),
  270. BOOST_MATH_BIG_CONSTANT(T, 53, -0.65031853770896507),
  271. BOOST_MATH_BIG_CONSTANT(T, 53, -0.28919126444774784),
  272. BOOST_MATH_BIG_CONSTANT(T, 53, -0.045251321448739056),
  273. BOOST_MATH_BIG_CONSTANT(T, 53, -0.0020713321167745952)
  274. };
  275. static const T Q[] = {
  276. BOOST_MATH_BIG_CONSTANT(T, 53, 1),
  277. BOOST_MATH_BIG_CONSTANT(T, 53, 2.0767117023730469),
  278. BOOST_MATH_BIG_CONSTANT(T, 53, 1.4606242909763515),
  279. BOOST_MATH_BIG_CONSTANT(T, 53, 0.43593529692665969),
  280. BOOST_MATH_BIG_CONSTANT(T, 53, 0.054151797245674225),
  281. BOOST_MATH_BIG_CONSTANT(T, 53, 0.0021284987017821144),
  282. BOOST_MATH_BIG_CONSTANT(T, 53, -0.55789841321675513e-6)
  283. };
  284. T g = x - root1;
  285. g -= root2;
  286. g -= root3;
  287. T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
  288. T result = g * Y + g * r;
  289. return result;
  290. }
  291. //
  292. // 9-digit precision:
  293. //
  294. template <class T>
  295. inline T digamma_imp_1_2(T x, const mpl::int_<24>*)
  296. {
  297. //
  298. // Now the approximation, we use the form:
  299. //
  300. // digamma(x) = (x - root) * (Y + R(x-1))
  301. //
  302. // Where root is the location of the positive root of digamma,
  303. // Y is a constant, and R is optimised for low absolute error
  304. // compared to Y.
  305. //
  306. // Maximum Deviation Found: 3.388e-010
  307. // At float precision, max error found: 2.008725e-008
  308. //
  309. static const float Y = 0.99558162689208984f;
  310. static const T root = 1532632.0f / 1048576;
  311. static const T root_minor = static_cast<T>(0.3700660185912626595423257213284682051735604e-6L);
  312. static const T P[] = {
  313. 0.25479851023250261e0,
  314. -0.44981331915268368e0,
  315. -0.43916936919946835e0,
  316. -0.61041765350579073e-1
  317. };
  318. static const T Q[] = {
  319. 0.1e1,
  320. 0.15890202430554952e1,
  321. 0.65341249856146947e0,
  322. 0.63851690523355715e-1
  323. };
  324. T g = x - root;
  325. g -= root_minor;
  326. T r = tools::evaluate_polynomial(P, T(x-1)) / tools::evaluate_polynomial(Q, T(x-1));
  327. T result = g * Y + g * r;
  328. return result;
  329. }
  330. template <class T, class Tag, class Policy>
  331. T digamma_imp(T x, const Tag* t, const Policy& pol)
  332. {
  333. //
  334. // This handles reflection of negative arguments, and all our
  335. // error handling, then forwards to the T-specific approximation.
  336. //
  337. BOOST_MATH_STD_USING // ADL of std functions.
  338. T result = 0;
  339. //
  340. // Check for negative arguments and use reflection:
  341. //
  342. if(x < 0)
  343. {
  344. // Reflect:
  345. x = 1 - x;
  346. // Argument reduction for tan:
  347. T remainder = x - floor(x);
  348. // Shift to negative if > 0.5:
  349. if(remainder > 0.5)
  350. {
  351. remainder -= 1;
  352. }
  353. //
  354. // check for evaluation at a negative pole:
  355. //
  356. if(remainder == 0)
  357. {
  358. return policies::raise_pole_error<T>("boost::math::digamma<%1%>(%1%)", 0, (1-x), pol);
  359. }
  360. result = constants::pi<T>() / tan(constants::pi<T>() * remainder);
  361. }
  362. //
  363. // If we're above the lower-limit for the
  364. // asymptotic expansion then use it:
  365. //
  366. if(x >= digamma_large_lim(t))
  367. {
  368. result += digamma_imp_large(x, t);
  369. }
  370. else
  371. {
  372. //
  373. // If x > 2 reduce to the interval [1,2]:
  374. //
  375. while(x > 2)
  376. {
  377. x -= 1;
  378. result += 1/x;
  379. }
  380. //
  381. // If x < 1 use recurrance to shift to > 1:
  382. //
  383. if(x < 1)
  384. {
  385. result = -1/x;
  386. x += 1;
  387. }
  388. result += digamma_imp_1_2(x, t);
  389. }
  390. return result;
  391. }
  392. //
  393. // Initializer: ensure all our constants are initialized prior to the first call of main:
  394. //
  395. template <class T, class Policy>
  396. struct digamma_initializer
  397. {
  398. struct init
  399. {
  400. init()
  401. {
  402. boost::math::digamma(T(1.5), Policy());
  403. boost::math::digamma(T(500), Policy());
  404. }
  405. void force_instantiate()const{}
  406. };
  407. static const init initializer;
  408. static void force_instantiate()
  409. {
  410. initializer.force_instantiate();
  411. }
  412. };
  413. template <class T, class Policy>
  414. const typename digamma_initializer<T, Policy>::init digamma_initializer<T, Policy>::initializer;
  415. } // namespace detail
  416. template <class T, class Policy>
  417. inline typename tools::promote_args<T>::type
  418. digamma(T x, const Policy& pol)
  419. {
  420. typedef typename tools::promote_args<T>::type result_type;
  421. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  422. typedef typename policies::precision<T, Policy>::type precision_type;
  423. typedef typename mpl::if_<
  424. mpl::or_<
  425. mpl::less_equal<precision_type, mpl::int_<0> >,
  426. mpl::greater<precision_type, mpl::int_<64> >
  427. >,
  428. mpl::int_<0>,
  429. typename mpl::if_<
  430. mpl::less<precision_type, mpl::int_<25> >,
  431. mpl::int_<24>,
  432. typename mpl::if_<
  433. mpl::less<precision_type, mpl::int_<54> >,
  434. mpl::int_<53>,
  435. mpl::int_<64>
  436. >::type
  437. >::type
  438. >::type tag_type;
  439. // Force initialization of constants:
  440. detail::digamma_initializer<result_type, Policy>::force_instantiate();
  441. return policies::checked_narrowing_cast<result_type, Policy>(detail::digamma_imp(
  442. static_cast<value_type>(x),
  443. static_cast<const tag_type*>(0), pol), "boost::math::digamma<%1%>(%1%)");
  444. }
  445. template <class T>
  446. inline typename tools::promote_args<T>::type
  447. digamma(T x)
  448. {
  449. return digamma(x, policies::policy<>());
  450. }
  451. } // namespace math
  452. } // namespace boost
  453. #endif