trigamma.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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_TRIGAMMA_HPP
  6. #define BOOST_MATH_SF_TRIGAMMA_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/tools/rational.hpp>
  12. #include <boost/math/tools/series.hpp>
  13. #include <boost/math/tools/promotion.hpp>
  14. #include <boost/math/policies/error_handling.hpp>
  15. #include <boost/math/constants/constants.hpp>
  16. #include <boost/math/tools/big_constant.hpp>
  17. #include <boost/math/special_functions/polygamma.hpp>
  18. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  19. //
  20. // This is the only way we can avoid
  21. // warning: non-standard suffix on floating constant [-Wpedantic]
  22. // when building with -Wall -pedantic. Neither __extension__
  23. // nor #pragma diagnostic ignored work :(
  24. //
  25. #pragma GCC system_header
  26. #endif
  27. namespace boost{
  28. namespace math{
  29. namespace detail{
  30. template<class T, class Policy>
  31. T polygamma_imp(const int n, T x, const Policy &pol);
  32. template <class T, class Policy>
  33. T trigamma_prec(T x, const std::integral_constant<int, 53>*, const Policy&)
  34. {
  35. // Max error in interpolated form: 3.736e-017
  36. static const T offset = BOOST_MATH_BIG_CONSTANT(T, 53, 2.1093254089355469);
  37. static const T P_1_2[] = {
  38. BOOST_MATH_BIG_CONSTANT(T, 53, -1.1093280605946045),
  39. BOOST_MATH_BIG_CONSTANT(T, 53, -3.8310674472619321),
  40. BOOST_MATH_BIG_CONSTANT(T, 53, -3.3703848401898283),
  41. BOOST_MATH_BIG_CONSTANT(T, 53, 0.28080574467981213),
  42. BOOST_MATH_BIG_CONSTANT(T, 53, 1.6638069578676164),
  43. BOOST_MATH_BIG_CONSTANT(T, 53, 0.64468386819102836),
  44. };
  45. static const T Q_1_2[] = {
  46. BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
  47. BOOST_MATH_BIG_CONSTANT(T, 53, 3.4535389668541151),
  48. BOOST_MATH_BIG_CONSTANT(T, 53, 4.5208926987851437),
  49. BOOST_MATH_BIG_CONSTANT(T, 53, 2.7012734178351534),
  50. BOOST_MATH_BIG_CONSTANT(T, 53, 0.64468798399785611),
  51. BOOST_MATH_BIG_CONSTANT(T, 53, -0.20314516859987728e-6),
  52. };
  53. // Max error in interpolated form: 1.159e-017
  54. static const T P_2_4[] = {
  55. BOOST_MATH_BIG_CONSTANT(T, 53, -0.13803835004508849e-7),
  56. BOOST_MATH_BIG_CONSTANT(T, 53, 0.50000049158540261),
  57. BOOST_MATH_BIG_CONSTANT(T, 53, 1.6077979838469348),
  58. BOOST_MATH_BIG_CONSTANT(T, 53, 2.5645435828098254),
  59. BOOST_MATH_BIG_CONSTANT(T, 53, 2.0534873203680393),
  60. BOOST_MATH_BIG_CONSTANT(T, 53, 0.74566981111565923),
  61. };
  62. static const T Q_2_4[] = {
  63. BOOST_MATH_BIG_CONSTANT(T, 53, 1.0),
  64. BOOST_MATH_BIG_CONSTANT(T, 53, 2.8822787662376169),
  65. BOOST_MATH_BIG_CONSTANT(T, 53, 4.1681660554090917),
  66. BOOST_MATH_BIG_CONSTANT(T, 53, 2.7853527819234466),
  67. BOOST_MATH_BIG_CONSTANT(T, 53, 0.74967671848044792),
  68. BOOST_MATH_BIG_CONSTANT(T, 53, -0.00057069112416246805),
  69. };
  70. // Maximum Deviation Found: 6.896e-018
  71. // Expected Error Term : -6.895e-018
  72. // Maximum Relative Change in Control Points : 8.497e-004
  73. static const T P_4_inf[] = {
  74. static_cast<T>(0.68947581948701249e-17L),
  75. static_cast<T>(0.49999999999998975L),
  76. static_cast<T>(1.0177274392923795L),
  77. static_cast<T>(2.498208511343429L),
  78. static_cast<T>(2.1921221359427595L),
  79. static_cast<T>(1.5897035272532764L),
  80. static_cast<T>(0.40154388356961734L),
  81. };
  82. static const T Q_4_inf[] = {
  83. static_cast<T>(1.0L),
  84. static_cast<T>(1.7021215452463932L),
  85. static_cast<T>(4.4290431747556469L),
  86. static_cast<T>(2.9745631894384922L),
  87. static_cast<T>(2.3013614809773616L),
  88. static_cast<T>(0.28360399799075752L),
  89. static_cast<T>(0.022892987908906897L),
  90. };
  91. if(x <= 2)
  92. {
  93. return (offset + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x);
  94. }
  95. else if(x <= 4)
  96. {
  97. T y = 1 / x;
  98. return (1 + tools::evaluate_polynomial(P_2_4, y) / tools::evaluate_polynomial(Q_2_4, y)) / x;
  99. }
  100. T y = 1 / x;
  101. return (1 + tools::evaluate_polynomial(P_4_inf, y) / tools::evaluate_polynomial(Q_4_inf, y)) / x;
  102. }
  103. template <class T, class Policy>
  104. T trigamma_prec(T x, const std::integral_constant<int, 64>*, const Policy&)
  105. {
  106. // Max error in interpolated form: 1.178e-020
  107. static const T offset_1_2 = BOOST_MATH_BIG_CONSTANT(T, 64, 2.109325408935546875);
  108. static const T P_1_2[] = {
  109. BOOST_MATH_BIG_CONSTANT(T, 64, -1.10932535608960258341),
  110. BOOST_MATH_BIG_CONSTANT(T, 64, -4.18793841543017129052),
  111. BOOST_MATH_BIG_CONSTANT(T, 64, -4.63865531898487734531),
  112. BOOST_MATH_BIG_CONSTANT(T, 64, -0.919832884430500908047),
  113. BOOST_MATH_BIG_CONSTANT(T, 64, 1.68074038333180423012),
  114. BOOST_MATH_BIG_CONSTANT(T, 64, 1.21172611429185622377),
  115. BOOST_MATH_BIG_CONSTANT(T, 64, 0.259635673503366427284),
  116. };
  117. static const T Q_1_2[] = {
  118. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  119. BOOST_MATH_BIG_CONSTANT(T, 64, 3.77521119359546982995),
  120. BOOST_MATH_BIG_CONSTANT(T, 64, 5.664338024578956321),
  121. BOOST_MATH_BIG_CONSTANT(T, 64, 4.25995134879278028361),
  122. BOOST_MATH_BIG_CONSTANT(T, 64, 1.62956638448940402182),
  123. BOOST_MATH_BIG_CONSTANT(T, 64, 0.259635512844691089868),
  124. BOOST_MATH_BIG_CONSTANT(T, 64, 0.629642219810618032207e-8),
  125. };
  126. // Max error in interpolated form: 3.912e-020
  127. static const T P_2_8[] = {
  128. BOOST_MATH_BIG_CONSTANT(T, 64, -0.387540035162952880976e-11),
  129. BOOST_MATH_BIG_CONSTANT(T, 64, 0.500000000276430504),
  130. BOOST_MATH_BIG_CONSTANT(T, 64, 3.21926880986360957306),
  131. BOOST_MATH_BIG_CONSTANT(T, 64, 10.2550347708483445775),
  132. BOOST_MATH_BIG_CONSTANT(T, 64, 18.9002075150709144043),
  133. BOOST_MATH_BIG_CONSTANT(T, 64, 21.0357215832399705625),
  134. BOOST_MATH_BIG_CONSTANT(T, 64, 13.4346512182925923978),
  135. BOOST_MATH_BIG_CONSTANT(T, 64, 3.98656291026448279118),
  136. };
  137. static const T Q_2_8[] = {
  138. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  139. BOOST_MATH_BIG_CONSTANT(T, 64, 6.10520430478613667724),
  140. BOOST_MATH_BIG_CONSTANT(T, 64, 18.475001060603645512),
  141. BOOST_MATH_BIG_CONSTANT(T, 64, 31.7087534567758405638),
  142. BOOST_MATH_BIG_CONSTANT(T, 64, 31.908814523890465398),
  143. BOOST_MATH_BIG_CONSTANT(T, 64, 17.4175479039227084798),
  144. BOOST_MATH_BIG_CONSTANT(T, 64, 3.98749106958394941276),
  145. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000115917322224411128566),
  146. };
  147. // Maximum Deviation Found: 2.635e-020
  148. // Expected Error Term : 2.635e-020
  149. // Maximum Relative Change in Control Points : 1.791e-003
  150. static const T P_8_inf[] = {
  151. BOOST_MATH_BIG_CONSTANT(T, 64, -0.263527875092466899848e-19),
  152. BOOST_MATH_BIG_CONSTANT(T, 64, 0.500000000000000058145),
  153. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0730121433777364138677),
  154. BOOST_MATH_BIG_CONSTANT(T, 64, 1.94505878379957149534),
  155. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0517092358874932620529),
  156. BOOST_MATH_BIG_CONSTANT(T, 64, 1.07995383547483921121),
  157. };
  158. static const T Q_8_inf[] = {
  159. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  160. BOOST_MATH_BIG_CONSTANT(T, 64, -0.187309046577818095504),
  161. BOOST_MATH_BIG_CONSTANT(T, 64, 3.95255391645238842975),
  162. BOOST_MATH_BIG_CONSTANT(T, 64, -1.14743283327078949087),
  163. BOOST_MATH_BIG_CONSTANT(T, 64, 2.52989799376344914499),
  164. BOOST_MATH_BIG_CONSTANT(T, 64, -0.627414303172402506396),
  165. BOOST_MATH_BIG_CONSTANT(T, 64, 0.141554248216425512536),
  166. };
  167. if(x <= 2)
  168. {
  169. return (offset_1_2 + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x);
  170. }
  171. else if(x <= 8)
  172. {
  173. T y = 1 / x;
  174. return (1 + tools::evaluate_polynomial(P_2_8, y) / tools::evaluate_polynomial(Q_2_8, y)) / x;
  175. }
  176. T y = 1 / x;
  177. return (1 + tools::evaluate_polynomial(P_8_inf, y) / tools::evaluate_polynomial(Q_8_inf, y)) / x;
  178. }
  179. template <class T, class Policy>
  180. T trigamma_prec(T x, const std::integral_constant<int, 113>*, const Policy&)
  181. {
  182. // Max error in interpolated form: 1.916e-035
  183. static const T P_1_2[] = {
  184. BOOST_MATH_BIG_CONSTANT(T, 113, -0.999999999999999082554457936871832533),
  185. BOOST_MATH_BIG_CONSTANT(T, 113, -4.71237311120865266379041700054847734),
  186. BOOST_MATH_BIG_CONSTANT(T, 113, -7.94125711970499027763789342500817316),
  187. BOOST_MATH_BIG_CONSTANT(T, 113, -5.74657746697664735258222071695644535),
  188. BOOST_MATH_BIG_CONSTANT(T, 113, -0.404213349456398905981223965160595687),
  189. BOOST_MATH_BIG_CONSTANT(T, 113, 2.47877781178642876561595890095758896),
  190. BOOST_MATH_BIG_CONSTANT(T, 113, 2.07714151702455125992166949812126433),
  191. BOOST_MATH_BIG_CONSTANT(T, 113, 0.858877899162360138844032265418028567),
  192. BOOST_MATH_BIG_CONSTANT(T, 113, 0.20499222604410032375789018837922397),
  193. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0272103140348194747360175268778415049),
  194. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0015764849020876949848954081173520686),
  195. };
  196. static const T Q_1_2[] = {
  197. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  198. BOOST_MATH_BIG_CONSTANT(T, 113, 4.71237311120863419878375031457715223),
  199. BOOST_MATH_BIG_CONSTANT(T, 113, 9.58619118655339853449127952145877467),
  200. BOOST_MATH_BIG_CONSTANT(T, 113, 11.0940067269829372437561421279054968),
  201. BOOST_MATH_BIG_CONSTANT(T, 113, 8.09075424749327792073276309969037885),
  202. BOOST_MATH_BIG_CONSTANT(T, 113, 3.87705890159891405185343806884451286),
  203. BOOST_MATH_BIG_CONSTANT(T, 113, 1.22758678701914477836330837816976782),
  204. BOOST_MATH_BIG_CONSTANT(T, 113, 0.249092040606385004109672077814668716),
  205. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0295750413900655597027079600025569048),
  206. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00157648490200498142247694709728858139),
  207. BOOST_MATH_BIG_CONSTANT(T, 113, 0.161264050344059471721062360645432809e-14),
  208. };
  209. // Max error in interpolated form: 8.958e-035
  210. static const T P_2_4[] = {
  211. BOOST_MATH_BIG_CONSTANT(T, 113, -2.55843734739907925764326773972215085),
  212. BOOST_MATH_BIG_CONSTANT(T, 113, -12.2830208240542011967952466273455887),
  213. BOOST_MATH_BIG_CONSTANT(T, 113, -23.9195022162767993526575786066414403),
  214. BOOST_MATH_BIG_CONSTANT(T, 113, -24.9256431504823483094158828285470862),
  215. BOOST_MATH_BIG_CONSTANT(T, 113, -14.7979122765478779075108064826412285),
  216. BOOST_MATH_BIG_CONSTANT(T, 113, -4.46654453928610666393276765059122272),
  217. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0191439033405649675717082465687845002),
  218. BOOST_MATH_BIG_CONSTANT(T, 113, 0.515412052554351265708917209749037352),
  219. BOOST_MATH_BIG_CONSTANT(T, 113, 0.195378348786064304378247325360320038),
  220. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0334761282624174313035014426794245393),
  221. BOOST_MATH_BIG_CONSTANT(T, 113, 0.002373665205942206348500250056602687),
  222. };
  223. static const T Q_2_4[] = {
  224. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  225. BOOST_MATH_BIG_CONSTANT(T, 113, 4.80098558454419907830670928248659245),
  226. BOOST_MATH_BIG_CONSTANT(T, 113, 9.99220727843170133895059300223445265),
  227. BOOST_MATH_BIG_CONSTANT(T, 113, 11.8896146167631330735386697123464976),
  228. BOOST_MATH_BIG_CONSTANT(T, 113, 8.96613256683809091593793565879092581),
  229. BOOST_MATH_BIG_CONSTANT(T, 113, 4.47254136149624110878909334574485751),
  230. BOOST_MATH_BIG_CONSTANT(T, 113, 1.48600982028196527372434773913633152),
  231. BOOST_MATH_BIG_CONSTANT(T, 113, 0.319570735766764237068541501137990078),
  232. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0407358345787680953107374215319322066),
  233. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00237366520593271641375755486420859837),
  234. BOOST_MATH_BIG_CONSTANT(T, 113, 0.239554887903526152679337256236302116e-15),
  235. BOOST_MATH_BIG_CONSTANT(T, 113, -0.294749244740618656265237072002026314e-17),
  236. };
  237. static const T y_offset_2_4 = BOOST_MATH_BIG_CONSTANT(T, 113, 3.558437347412109375);
  238. // Max error in interpolated form: 4.319e-035
  239. static const T P_4_8[] = {
  240. BOOST_MATH_BIG_CONSTANT(T, 113, 0.166626112697021464248967707021688845e-16),
  241. BOOST_MATH_BIG_CONSTANT(T, 113, 0.499999999999997739552090249208808197),
  242. BOOST_MATH_BIG_CONSTANT(T, 113, 6.40270945019053817915772473771553187),
  243. BOOST_MATH_BIG_CONSTANT(T, 113, 41.3833374155000608013677627389343329),
  244. BOOST_MATH_BIG_CONSTANT(T, 113, 166.803341854562809335667241074035245),
  245. BOOST_MATH_BIG_CONSTANT(T, 113, 453.39964786925369319960722793414521),
  246. BOOST_MATH_BIG_CONSTANT(T, 113, 851.153712317697055375935433362983944),
  247. BOOST_MATH_BIG_CONSTANT(T, 113, 1097.70657567285059133109286478004458),
  248. BOOST_MATH_BIG_CONSTANT(T, 113, 938.431232478455316020076349367632922),
  249. BOOST_MATH_BIG_CONSTANT(T, 113, 487.268001604651932322080970189930074),
  250. BOOST_MATH_BIG_CONSTANT(T, 113, 119.953445242335730062471193124820659),
  251. };
  252. static const T Q_4_8[] = {
  253. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  254. BOOST_MATH_BIG_CONSTANT(T, 113, 12.4720855670474488978638945855932398),
  255. BOOST_MATH_BIG_CONSTANT(T, 113, 78.6093129753298570701376952709727391),
  256. BOOST_MATH_BIG_CONSTANT(T, 113, 307.470246050318322489781182863190127),
  257. BOOST_MATH_BIG_CONSTANT(T, 113, 805.140686101151538537565264188630079),
  258. BOOST_MATH_BIG_CONSTANT(T, 113, 1439.12019760292146454787601409644413),
  259. BOOST_MATH_BIG_CONSTANT(T, 113, 1735.6105285756048831268586001383127),
  260. BOOST_MATH_BIG_CONSTANT(T, 113, 1348.32500712856328019355198611280536),
  261. BOOST_MATH_BIG_CONSTANT(T, 113, 607.225985860570846699704222144650563),
  262. BOOST_MATH_BIG_CONSTANT(T, 113, 119.952317857277045332558673164517227),
  263. BOOST_MATH_BIG_CONSTANT(T, 113, 0.000140165918355036060868680809129436084),
  264. };
  265. // Maximum Deviation Found: 2.867e-035
  266. // Expected Error Term : 2.866e-035
  267. // Maximum Relative Change in Control Points : 2.662e-004
  268. static const T P_8_16[] = {
  269. BOOST_MATH_BIG_CONSTANT(T, 113, -0.184828315274146610610872315609837439e-19),
  270. BOOST_MATH_BIG_CONSTANT(T, 113, 0.500000000000000004122475157735807738),
  271. BOOST_MATH_BIG_CONSTANT(T, 113, 3.02533865247313349284875558880415875),
  272. BOOST_MATH_BIG_CONSTANT(T, 113, 13.5995927517457371243039532492642734),
  273. BOOST_MATH_BIG_CONSTANT(T, 113, 35.3132224283087906757037999452941588),
  274. BOOST_MATH_BIG_CONSTANT(T, 113, 67.1639424550714159157603179911505619),
  275. BOOST_MATH_BIG_CONSTANT(T, 113, 83.5767733658513967581959839367419891),
  276. BOOST_MATH_BIG_CONSTANT(T, 113, 71.073491212235705900866411319363501),
  277. BOOST_MATH_BIG_CONSTANT(T, 113, 35.8621515614725564575893663483998663),
  278. BOOST_MATH_BIG_CONSTANT(T, 113, 8.72152231639983491987779743154333318),
  279. };
  280. static const T Q_8_16[] = {
  281. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  282. BOOST_MATH_BIG_CONSTANT(T, 113, 5.71734397161293452310624822415866372),
  283. BOOST_MATH_BIG_CONSTANT(T, 113, 25.293404179620438179337103263274815),
  284. BOOST_MATH_BIG_CONSTANT(T, 113, 62.2619767967468199111077640625328469),
  285. BOOST_MATH_BIG_CONSTANT(T, 113, 113.955048909238993473389714972250235),
  286. BOOST_MATH_BIG_CONSTANT(T, 113, 130.807138328938966981862203944329408),
  287. BOOST_MATH_BIG_CONSTANT(T, 113, 102.423146902337654110717764213057753),
  288. BOOST_MATH_BIG_CONSTANT(T, 113, 44.0424772805245202514468199602123565),
  289. BOOST_MATH_BIG_CONSTANT(T, 113, 8.89898032477904072082994913461386099),
  290. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0296627336872039988632793863671456398),
  291. };
  292. // Maximum Deviation Found: 1.079e-035
  293. // Expected Error Term : -1.079e-035
  294. // Maximum Relative Change in Control Points : 7.884e-003
  295. static const T P_16_inf[] = {
  296. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0),
  297. BOOST_MATH_BIG_CONSTANT(T, 113, 0.500000000000000000000000000000087317),
  298. BOOST_MATH_BIG_CONSTANT(T, 113, 0.345625669885456215194494735902663968),
  299. BOOST_MATH_BIG_CONSTANT(T, 113, 9.62895499360842232127552650044647769),
  300. BOOST_MATH_BIG_CONSTANT(T, 113, 3.5936085382439026269301003761320812),
  301. BOOST_MATH_BIG_CONSTANT(T, 113, 49.459599118438883265036646019410669),
  302. BOOST_MATH_BIG_CONSTANT(T, 113, 7.77519237321893917784735690560496607),
  303. BOOST_MATH_BIG_CONSTANT(T, 113, 74.4536074488178075948642351179304121),
  304. BOOST_MATH_BIG_CONSTANT(T, 113, 2.75209340397069050436806159297952699),
  305. BOOST_MATH_BIG_CONSTANT(T, 113, 23.9292359711471667884504840186561598),
  306. };
  307. static const T Q_16_inf[] = {
  308. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  309. BOOST_MATH_BIG_CONSTANT(T, 113, 0.357918006437579097055656138920742037),
  310. BOOST_MATH_BIG_CONSTANT(T, 113, 19.1386039850709849435325005484512944),
  311. BOOST_MATH_BIG_CONSTANT(T, 113, 0.874349081464143606016221431763364517),
  312. BOOST_MATH_BIG_CONSTANT(T, 113, 98.6516097434855572678195488061432509),
  313. BOOST_MATH_BIG_CONSTANT(T, 113, -16.1051972833382893468655223662534306),
  314. BOOST_MATH_BIG_CONSTANT(T, 113, 154.316860216253720989145047141653727),
  315. BOOST_MATH_BIG_CONSTANT(T, 113, -40.2026880424378986053105969312264534),
  316. BOOST_MATH_BIG_CONSTANT(T, 113, 60.1679136674264778074736441126810223),
  317. BOOST_MATH_BIG_CONSTANT(T, 113, -13.3414844622256422644504472438320114),
  318. BOOST_MATH_BIG_CONSTANT(T, 113, 2.53795636200649908779512969030363442),
  319. };
  320. if(x <= 2)
  321. {
  322. return (2 + boost::math::tools::evaluate_polynomial(P_1_2, x) / tools::evaluate_polynomial(Q_1_2, x)) / (x * x);
  323. }
  324. else if(x <= 4)
  325. {
  326. return (y_offset_2_4 + boost::math::tools::evaluate_polynomial(P_2_4, x) / tools::evaluate_polynomial(Q_2_4, x)) / (x * x);
  327. }
  328. else if(x <= 8)
  329. {
  330. T y = 1 / x;
  331. return (1 + tools::evaluate_polynomial(P_4_8, y) / tools::evaluate_polynomial(Q_4_8, y)) / x;
  332. }
  333. else if(x <= 16)
  334. {
  335. T y = 1 / x;
  336. return (1 + tools::evaluate_polynomial(P_8_16, y) / tools::evaluate_polynomial(Q_8_16, y)) / x;
  337. }
  338. T y = 1 / x;
  339. return (1 + tools::evaluate_polynomial(P_16_inf, y) / tools::evaluate_polynomial(Q_16_inf, y)) / x;
  340. }
  341. template <class T, class Tag, class Policy>
  342. T trigamma_imp(T x, const Tag* t, const Policy& pol)
  343. {
  344. //
  345. // This handles reflection of negative arguments, and all our
  346. // error handling, then forwards to the T-specific approximation.
  347. //
  348. BOOST_MATH_STD_USING // ADL of std functions.
  349. T result = 0;
  350. //
  351. // Check for negative arguments and use reflection:
  352. //
  353. if(x <= 0)
  354. {
  355. // Reflect:
  356. T z = 1 - x;
  357. // Argument reduction for tan:
  358. if(floor(x) == x)
  359. {
  360. return policies::raise_pole_error<T>("boost::math::trigamma<%1%>(%1%)", 0, (1-x), pol);
  361. }
  362. T s = fabs(x) < fabs(z) ? boost::math::sin_pi(x, pol) : boost::math::sin_pi(z, pol);
  363. return -trigamma_imp(z, t, pol) + boost::math::pow<2>(constants::pi<T>()) / (s * s);
  364. }
  365. if(x < 1)
  366. {
  367. result = 1 / (x * x);
  368. x += 1;
  369. }
  370. return result + trigamma_prec(x, t, pol);
  371. }
  372. template <class T, class Policy>
  373. T trigamma_imp(T x, const std::integral_constant<int, 0>*, const Policy& pol)
  374. {
  375. return polygamma_imp(1, x, pol);
  376. }
  377. //
  378. // Initializer: ensure all our constants are initialized prior to the first call of main:
  379. //
  380. template <class T, class Policy>
  381. struct trigamma_initializer
  382. {
  383. struct init
  384. {
  385. init()
  386. {
  387. typedef typename policies::precision<T, Policy>::type precision_type;
  388. do_init(std::integral_constant<bool, precision_type::value && (precision_type::value <= 113)>());
  389. }
  390. void do_init(const std::true_type&)
  391. {
  392. boost::math::trigamma(T(2.5), Policy());
  393. }
  394. void do_init(const std::false_type&){}
  395. void force_instantiate()const{}
  396. };
  397. static const init initializer;
  398. static void force_instantiate()
  399. {
  400. initializer.force_instantiate();
  401. }
  402. };
  403. template <class T, class Policy>
  404. const typename trigamma_initializer<T, Policy>::init trigamma_initializer<T, Policy>::initializer;
  405. } // namespace detail
  406. template <class T, class Policy>
  407. inline typename tools::promote_args<T>::type
  408. trigamma(T x, const Policy&)
  409. {
  410. typedef typename tools::promote_args<T>::type result_type;
  411. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  412. typedef typename policies::precision<T, Policy>::type precision_type;
  413. typedef std::integral_constant<int,
  414. precision_type::value <= 0 ? 0 :
  415. precision_type::value <= 53 ? 53 :
  416. precision_type::value <= 64 ? 64 :
  417. precision_type::value <= 113 ? 113 : 0
  418. > tag_type;
  419. typedef typename policies::normalise<
  420. Policy,
  421. policies::promote_float<false>,
  422. policies::promote_double<false>,
  423. policies::discrete_quantile<>,
  424. policies::assert_undefined<> >::type forwarding_policy;
  425. // Force initialization of constants:
  426. detail::trigamma_initializer<value_type, forwarding_policy>::force_instantiate();
  427. return policies::checked_narrowing_cast<result_type, Policy>(detail::trigamma_imp(
  428. static_cast<value_type>(x),
  429. static_cast<const tag_type*>(0), forwarding_policy()), "boost::math::trigamma<%1%>(%1%)");
  430. }
  431. template <class T>
  432. inline typename tools::promote_args<T>::type
  433. trigamma(T x)
  434. {
  435. return trigamma(x, policies::policy<>());
  436. }
  437. } // namespace math
  438. } // namespace boost
  439. #endif