zeta.hpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. // Copyright John Maddock 2007, 2014.
  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_ZETA_HPP
  6. #define BOOST_MATH_ZETA_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/tools/precision.hpp>
  12. #include <boost/math/tools/series.hpp>
  13. #include <boost/math/tools/big_constant.hpp>
  14. #include <boost/math/policies/error_handling.hpp>
  15. #include <boost/math/special_functions/gamma.hpp>
  16. #include <boost/math/special_functions/factorials.hpp>
  17. #include <boost/math/special_functions/sin_pi.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{ namespace math{ namespace detail{
  28. #if 0
  29. //
  30. // This code is commented out because we have a better more rapidly converging series
  31. // now. Retained for future reference and in case the new code causes any issues down the line....
  32. //
  33. template <class T, class Policy>
  34. struct zeta_series_cache_size
  35. {
  36. //
  37. // Work how large to make our cache size when evaluating the series
  38. // evaluation: normally this is just large enough for the series
  39. // to have converged, but for arbitrary precision types we need a
  40. // really large cache to achieve reasonable precision in a reasonable
  41. // time. This is important when constructing rational approximations
  42. // to zeta for example.
  43. //
  44. typedef typename boost::math::policies::precision<T,Policy>::type precision_type;
  45. typedef typename mpl::if_<
  46. mpl::less_equal<precision_type, std::integral_constant<int, 0> >,
  47. std::integral_constant<int, 5000>,
  48. typename mpl::if_<
  49. mpl::less_equal<precision_type, std::integral_constant<int, 64> >,
  50. std::integral_constant<int, 70>,
  51. typename mpl::if_<
  52. mpl::less_equal<precision_type, std::integral_constant<int, 113> >,
  53. std::integral_constant<int, 100>,
  54. std::integral_constant<int, 5000>
  55. >::type
  56. >::type
  57. >::type type;
  58. };
  59. template <class T, class Policy>
  60. T zeta_series_imp(T s, T sc, const Policy&)
  61. {
  62. //
  63. // Series evaluation from:
  64. // Havil, J. Gamma: Exploring Euler's Constant.
  65. // Princeton, NJ: Princeton University Press, 2003.
  66. //
  67. // See also http://mathworld.wolfram.com/RiemannZetaFunction.html
  68. //
  69. BOOST_MATH_STD_USING
  70. T sum = 0;
  71. T mult = 0.5;
  72. T change;
  73. typedef typename zeta_series_cache_size<T,Policy>::type cache_size;
  74. T powers[cache_size::value] = { 0, };
  75. unsigned n = 0;
  76. do{
  77. T binom = -static_cast<T>(n);
  78. T nested_sum = 1;
  79. if(n < sizeof(powers) / sizeof(powers[0]))
  80. powers[n] = pow(static_cast<T>(n + 1), -s);
  81. for(unsigned k = 1; k <= n; ++k)
  82. {
  83. T p;
  84. if(k < sizeof(powers) / sizeof(powers[0]))
  85. {
  86. p = powers[k];
  87. //p = pow(k + 1, -s);
  88. }
  89. else
  90. p = pow(static_cast<T>(k + 1), -s);
  91. nested_sum += binom * p;
  92. binom *= (k - static_cast<T>(n)) / (k + 1);
  93. }
  94. change = mult * nested_sum;
  95. sum += change;
  96. mult /= 2;
  97. ++n;
  98. }while(fabs(change / sum) > tools::epsilon<T>());
  99. return sum * 1 / -boost::math::powm1(T(2), sc);
  100. }
  101. //
  102. // Classical p-series:
  103. //
  104. template <class T>
  105. struct zeta_series2
  106. {
  107. typedef T result_type;
  108. zeta_series2(T _s) : s(-_s), k(1){}
  109. T operator()()
  110. {
  111. BOOST_MATH_STD_USING
  112. return pow(static_cast<T>(k++), s);
  113. }
  114. private:
  115. T s;
  116. unsigned k;
  117. };
  118. template <class T, class Policy>
  119. inline T zeta_series2_imp(T s, const Policy& pol)
  120. {
  121. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();;
  122. zeta_series2<T> f(s);
  123. T result = tools::sum_series(
  124. f,
  125. policies::get_epsilon<T, Policy>(),
  126. max_iter);
  127. policies::check_series_iterations<T>("boost::math::zeta_series2<%1%>(%1%)", max_iter, pol);
  128. return result;
  129. }
  130. #endif
  131. template <class T, class Policy>
  132. T zeta_polynomial_series(T s, T sc, Policy const &)
  133. {
  134. //
  135. // This is algorithm 3 from:
  136. //
  137. // "An Efficient Algorithm for the Riemann Zeta Function", P. Borwein,
  138. // Canadian Mathematical Society, Conference Proceedings.
  139. // See: http://www.cecm.sfu.ca/personal/pborwein/PAPERS/P155.pdf
  140. //
  141. BOOST_MATH_STD_USING
  142. int n = itrunc(T(log(boost::math::tools::epsilon<T>()) / -2));
  143. T sum = 0;
  144. T two_n = ldexp(T(1), n);
  145. int ej_sign = 1;
  146. for(int j = 0; j < n; ++j)
  147. {
  148. sum += ej_sign * -two_n / pow(T(j + 1), s);
  149. ej_sign = -ej_sign;
  150. }
  151. T ej_sum = 1;
  152. T ej_term = 1;
  153. for(int j = n; j <= 2 * n - 1; ++j)
  154. {
  155. sum += ej_sign * (ej_sum - two_n) / pow(T(j + 1), s);
  156. ej_sign = -ej_sign;
  157. ej_term *= 2 * n - j;
  158. ej_term /= j - n + 1;
  159. ej_sum += ej_term;
  160. }
  161. return -sum / (two_n * (-powm1(T(2), sc)));
  162. }
  163. template <class T, class Policy>
  164. T zeta_imp_prec(T s, T sc, const Policy& pol, const std::integral_constant<int, 0>&)
  165. {
  166. BOOST_MATH_STD_USING
  167. T result;
  168. if(s >= policies::digits<T, Policy>())
  169. return 1;
  170. result = zeta_polynomial_series(s, sc, pol);
  171. #if 0
  172. // Old code archived for future reference:
  173. //
  174. // Only use power series if it will converge in 100
  175. // iterations or less: the more iterations it consumes
  176. // the slower convergence becomes so we have to be very
  177. // careful in it's usage.
  178. //
  179. if (s > -log(tools::epsilon<T>()) / 4.5)
  180. result = detail::zeta_series2_imp(s, pol);
  181. else
  182. result = detail::zeta_series_imp(s, sc, pol);
  183. #endif
  184. return result;
  185. }
  186. template <class T, class Policy>
  187. inline T zeta_imp_prec(T s, T sc, const Policy&, const std::integral_constant<int, 53>&)
  188. {
  189. BOOST_MATH_STD_USING
  190. T result;
  191. if(s < 1)
  192. {
  193. // Rational Approximation
  194. // Maximum Deviation Found: 2.020e-18
  195. // Expected Error Term: -2.020e-18
  196. // Max error found at double precision: 3.994987e-17
  197. static const T P[6] = {
  198. static_cast<T>(0.24339294433593750202L),
  199. static_cast<T>(-0.49092470516353571651L),
  200. static_cast<T>(0.0557616214776046784287L),
  201. static_cast<T>(-0.00320912498879085894856L),
  202. static_cast<T>(0.000451534528645796438704L),
  203. static_cast<T>(-0.933241270357061460782e-5L),
  204. };
  205. static const T Q[6] = {
  206. static_cast<T>(1L),
  207. static_cast<T>(-0.279960334310344432495L),
  208. static_cast<T>(0.0419676223309986037706L),
  209. static_cast<T>(-0.00413421406552171059003L),
  210. static_cast<T>(0.00024978985622317935355L),
  211. static_cast<T>(-0.101855788418564031874e-4L),
  212. };
  213. result = tools::evaluate_polynomial(P, sc) / tools::evaluate_polynomial(Q, sc);
  214. result -= 1.2433929443359375F;
  215. result += (sc);
  216. result /= (sc);
  217. }
  218. else if(s <= 2)
  219. {
  220. // Maximum Deviation Found: 9.007e-20
  221. // Expected Error Term: 9.007e-20
  222. static const T P[6] = {
  223. static_cast<T>(0.577215664901532860516L),
  224. static_cast<T>(0.243210646940107164097L),
  225. static_cast<T>(0.0417364673988216497593L),
  226. static_cast<T>(0.00390252087072843288378L),
  227. static_cast<T>(0.000249606367151877175456L),
  228. static_cast<T>(0.110108440976732897969e-4L),
  229. };
  230. static const T Q[6] = {
  231. static_cast<T>(1.0),
  232. static_cast<T>(0.295201277126631761737L),
  233. static_cast<T>(0.043460910607305495864L),
  234. static_cast<T>(0.00434930582085826330659L),
  235. static_cast<T>(0.000255784226140488490982L),
  236. static_cast<T>(0.10991819782396112081e-4L),
  237. };
  238. result = tools::evaluate_polynomial(P, T(-sc)) / tools::evaluate_polynomial(Q, T(-sc));
  239. result += 1 / (-sc);
  240. }
  241. else if(s <= 4)
  242. {
  243. // Maximum Deviation Found: 5.946e-22
  244. // Expected Error Term: -5.946e-22
  245. static const float Y = 0.6986598968505859375;
  246. static const T P[6] = {
  247. static_cast<T>(-0.0537258300023595030676L),
  248. static_cast<T>(0.0445163473292365591906L),
  249. static_cast<T>(0.0128677673534519952905L),
  250. static_cast<T>(0.00097541770457391752726L),
  251. static_cast<T>(0.769875101573654070925e-4L),
  252. static_cast<T>(0.328032510000383084155e-5L),
  253. };
  254. static const T Q[7] = {
  255. 1.0f,
  256. static_cast<T>(0.33383194553034051422L),
  257. static_cast<T>(0.0487798431291407621462L),
  258. static_cast<T>(0.00479039708573558490716L),
  259. static_cast<T>(0.000270776703956336357707L),
  260. static_cast<T>(0.106951867532057341359e-4L),
  261. static_cast<T>(0.236276623974978646399e-7L),
  262. };
  263. result = tools::evaluate_polynomial(P, T(s - 2)) / tools::evaluate_polynomial(Q, T(s - 2));
  264. result += Y + 1 / (-sc);
  265. }
  266. else if(s <= 7)
  267. {
  268. // Maximum Deviation Found: 2.955e-17
  269. // Expected Error Term: 2.955e-17
  270. // Max error found at double precision: 2.009135e-16
  271. static const T P[6] = {
  272. static_cast<T>(-2.49710190602259410021L),
  273. static_cast<T>(-2.60013301809475665334L),
  274. static_cast<T>(-0.939260435377109939261L),
  275. static_cast<T>(-0.138448617995741530935L),
  276. static_cast<T>(-0.00701721240549802377623L),
  277. static_cast<T>(-0.229257310594893932383e-4L),
  278. };
  279. static const T Q[9] = {
  280. 1.0f,
  281. static_cast<T>(0.706039025937745133628L),
  282. static_cast<T>(0.15739599649558626358L),
  283. static_cast<T>(0.0106117950976845084417L),
  284. static_cast<T>(-0.36910273311764618902e-4L),
  285. static_cast<T>(0.493409563927590008943e-5L),
  286. static_cast<T>(-0.234055487025287216506e-6L),
  287. static_cast<T>(0.718833729365459760664e-8L),
  288. static_cast<T>(-0.1129200113474947419e-9L),
  289. };
  290. result = tools::evaluate_polynomial(P, T(s - 4)) / tools::evaluate_polynomial(Q, T(s - 4));
  291. result = 1 + exp(result);
  292. }
  293. else if(s < 15)
  294. {
  295. // Maximum Deviation Found: 7.117e-16
  296. // Expected Error Term: 7.117e-16
  297. // Max error found at double precision: 9.387771e-16
  298. static const T P[7] = {
  299. static_cast<T>(-4.78558028495135619286L),
  300. static_cast<T>(-1.89197364881972536382L),
  301. static_cast<T>(-0.211407134874412820099L),
  302. static_cast<T>(-0.000189204758260076688518L),
  303. static_cast<T>(0.00115140923889178742086L),
  304. static_cast<T>(0.639949204213164496988e-4L),
  305. static_cast<T>(0.139348932445324888343e-5L),
  306. };
  307. static const T Q[9] = {
  308. 1.0f,
  309. static_cast<T>(0.244345337378188557777L),
  310. static_cast<T>(0.00873370754492288653669L),
  311. static_cast<T>(-0.00117592765334434471562L),
  312. static_cast<T>(-0.743743682899933180415e-4L),
  313. static_cast<T>(-0.21750464515767984778e-5L),
  314. static_cast<T>(0.471001264003076486547e-8L),
  315. static_cast<T>(-0.833378440625385520576e-10L),
  316. static_cast<T>(0.699841545204845636531e-12L),
  317. };
  318. result = tools::evaluate_polynomial(P, T(s - 7)) / tools::evaluate_polynomial(Q, T(s - 7));
  319. result = 1 + exp(result);
  320. }
  321. else if(s < 36)
  322. {
  323. // Max error in interpolated form: 1.668e-17
  324. // Max error found at long double precision: 1.669714e-17
  325. static const T P[8] = {
  326. static_cast<T>(-10.3948950573308896825L),
  327. static_cast<T>(-2.85827219671106697179L),
  328. static_cast<T>(-0.347728266539245787271L),
  329. static_cast<T>(-0.0251156064655346341766L),
  330. static_cast<T>(-0.00119459173416968685689L),
  331. static_cast<T>(-0.382529323507967522614e-4L),
  332. static_cast<T>(-0.785523633796723466968e-6L),
  333. static_cast<T>(-0.821465709095465524192e-8L),
  334. };
  335. static const T Q[10] = {
  336. 1.0f,
  337. static_cast<T>(0.208196333572671890965L),
  338. static_cast<T>(0.0195687657317205033485L),
  339. static_cast<T>(0.00111079638102485921877L),
  340. static_cast<T>(0.408507746266039256231e-4L),
  341. static_cast<T>(0.955561123065693483991e-6L),
  342. static_cast<T>(0.118507153474022900583e-7L),
  343. static_cast<T>(0.222609483627352615142e-14L),
  344. };
  345. result = tools::evaluate_polynomial(P, T(s - 15)) / tools::evaluate_polynomial(Q, T(s - 15));
  346. result = 1 + exp(result);
  347. }
  348. else if(s < 56)
  349. {
  350. result = 1 + pow(T(2), -s);
  351. }
  352. else
  353. {
  354. result = 1;
  355. }
  356. return result;
  357. }
  358. template <class T, class Policy>
  359. T zeta_imp_prec(T s, T sc, const Policy&, const std::integral_constant<int, 64>&)
  360. {
  361. BOOST_MATH_STD_USING
  362. T result;
  363. if(s < 1)
  364. {
  365. // Rational Approximation
  366. // Maximum Deviation Found: 3.099e-20
  367. // Expected Error Term: 3.099e-20
  368. // Max error found at long double precision: 5.890498e-20
  369. static const T P[6] = {
  370. BOOST_MATH_BIG_CONSTANT(T, 64, 0.243392944335937499969),
  371. BOOST_MATH_BIG_CONSTANT(T, 64, -0.496837806864865688082),
  372. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0680008039723709987107),
  373. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00511620413006619942112),
  374. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000455369899250053003335),
  375. BOOST_MATH_BIG_CONSTANT(T, 64, -0.279496685273033761927e-4),
  376. };
  377. static const T Q[7] = {
  378. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  379. BOOST_MATH_BIG_CONSTANT(T, 64, -0.30425480068225790522),
  380. BOOST_MATH_BIG_CONSTANT(T, 64, 0.050052748580371598736),
  381. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00519355671064700627862),
  382. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000360623385771198350257),
  383. BOOST_MATH_BIG_CONSTANT(T, 64, -0.159600883054550987633e-4),
  384. BOOST_MATH_BIG_CONSTANT(T, 64, 0.339770279812410586032e-6),
  385. };
  386. result = tools::evaluate_polynomial(P, sc) / tools::evaluate_polynomial(Q, sc);
  387. result -= 1.2433929443359375F;
  388. result += (sc);
  389. result /= (sc);
  390. }
  391. else if(s <= 2)
  392. {
  393. // Maximum Deviation Found: 1.059e-21
  394. // Expected Error Term: 1.059e-21
  395. // Max error found at long double precision: 1.626303e-19
  396. static const T P[6] = {
  397. BOOST_MATH_BIG_CONSTANT(T, 64, 0.577215664901532860605),
  398. BOOST_MATH_BIG_CONSTANT(T, 64, 0.222537368917162139445),
  399. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0356286324033215682729),
  400. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00304465292366350081446),
  401. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000178102511649069421904),
  402. BOOST_MATH_BIG_CONSTANT(T, 64, 0.700867470265983665042e-5),
  403. };
  404. static const T Q[7] = {
  405. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  406. BOOST_MATH_BIG_CONSTANT(T, 64, 0.259385759149531030085),
  407. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0373974962106091316854),
  408. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00332735159183332820617),
  409. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000188690420706998606469),
  410. BOOST_MATH_BIG_CONSTANT(T, 64, 0.635994377921861930071e-5),
  411. BOOST_MATH_BIG_CONSTANT(T, 64, 0.226583954978371199405e-7),
  412. };
  413. result = tools::evaluate_polynomial(P, T(-sc)) / tools::evaluate_polynomial(Q, T(-sc));
  414. result += 1 / (-sc);
  415. }
  416. else if(s <= 4)
  417. {
  418. // Maximum Deviation Found: 5.946e-22
  419. // Expected Error Term: -5.946e-22
  420. static const float Y = 0.6986598968505859375;
  421. static const T P[7] = {
  422. BOOST_MATH_BIG_CONSTANT(T, 64, -0.053725830002359501027),
  423. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0470551187571475844778),
  424. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0101339410415759517471),
  425. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00100240326666092854528),
  426. BOOST_MATH_BIG_CONSTANT(T, 64, 0.685027119098122814867e-4),
  427. BOOST_MATH_BIG_CONSTANT(T, 64, 0.390972820219765942117e-5),
  428. BOOST_MATH_BIG_CONSTANT(T, 64, 0.540319769113543934483e-7),
  429. };
  430. static const T Q[8] = {
  431. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  432. BOOST_MATH_BIG_CONSTANT(T, 64, 0.286577739726542730421),
  433. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0447355811517733225843),
  434. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00430125107610252363302),
  435. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000284956969089786662045),
  436. BOOST_MATH_BIG_CONSTANT(T, 64, 0.116188101609848411329e-4),
  437. BOOST_MATH_BIG_CONSTANT(T, 64, 0.278090318191657278204e-6),
  438. BOOST_MATH_BIG_CONSTANT(T, 64, -0.19683620233222028478e-8),
  439. };
  440. result = tools::evaluate_polynomial(P, T(s - 2)) / tools::evaluate_polynomial(Q, T(s - 2));
  441. result += Y + 1 / (-sc);
  442. }
  443. else if(s <= 7)
  444. {
  445. // Max error found at long double precision: 8.132216e-19
  446. static const T P[8] = {
  447. BOOST_MATH_BIG_CONSTANT(T, 64, -2.49710190602259407065),
  448. BOOST_MATH_BIG_CONSTANT(T, 64, -3.36664913245960625334),
  449. BOOST_MATH_BIG_CONSTANT(T, 64, -1.77180020623777595452),
  450. BOOST_MATH_BIG_CONSTANT(T, 64, -0.464717885249654313933),
  451. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0643694921293579472583),
  452. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00464265386202805715487),
  453. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000165556579779704340166),
  454. BOOST_MATH_BIG_CONSTANT(T, 64, -0.252884970740994069582e-5),
  455. };
  456. static const T Q[9] = {
  457. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  458. BOOST_MATH_BIG_CONSTANT(T, 64, 1.01300131390690459085),
  459. BOOST_MATH_BIG_CONSTANT(T, 64, 0.387898115758643503827),
  460. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0695071490045701135188),
  461. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00586908595251442839291),
  462. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000217752974064612188616),
  463. BOOST_MATH_BIG_CONSTANT(T, 64, 0.397626583349419011731e-5),
  464. BOOST_MATH_BIG_CONSTANT(T, 64, -0.927884739284359700764e-8),
  465. BOOST_MATH_BIG_CONSTANT(T, 64, 0.119810501805618894381e-9),
  466. };
  467. result = tools::evaluate_polynomial(P, T(s - 4)) / tools::evaluate_polynomial(Q, T(s - 4));
  468. result = 1 + exp(result);
  469. }
  470. else if(s < 15)
  471. {
  472. // Max error in interpolated form: 1.133e-18
  473. // Max error found at long double precision: 2.183198e-18
  474. static const T P[9] = {
  475. BOOST_MATH_BIG_CONSTANT(T, 64, -4.78558028495135548083),
  476. BOOST_MATH_BIG_CONSTANT(T, 64, -3.23873322238609358947),
  477. BOOST_MATH_BIG_CONSTANT(T, 64, -0.892338582881021799922),
  478. BOOST_MATH_BIG_CONSTANT(T, 64, -0.131326296217965913809),
  479. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0115651591773783712996),
  480. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000657728968362695775205),
  481. BOOST_MATH_BIG_CONSTANT(T, 64, -0.252051328129449973047e-4),
  482. BOOST_MATH_BIG_CONSTANT(T, 64, -0.626503445372641798925e-6),
  483. BOOST_MATH_BIG_CONSTANT(T, 64, -0.815696314790853893484e-8),
  484. };
  485. static const T Q[9] = {
  486. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  487. BOOST_MATH_BIG_CONSTANT(T, 64, 0.525765665400123515036),
  488. BOOST_MATH_BIG_CONSTANT(T, 64, 0.10852641753657122787),
  489. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0115669945375362045249),
  490. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000732896513858274091966),
  491. BOOST_MATH_BIG_CONSTANT(T, 64, 0.30683952282420248448e-4),
  492. BOOST_MATH_BIG_CONSTANT(T, 64, 0.819649214609633126119e-6),
  493. BOOST_MATH_BIG_CONSTANT(T, 64, 0.117957556472335968146e-7),
  494. BOOST_MATH_BIG_CONSTANT(T, 64, -0.193432300973017671137e-12),
  495. };
  496. result = tools::evaluate_polynomial(P, T(s - 7)) / tools::evaluate_polynomial(Q, T(s - 7));
  497. result = 1 + exp(result);
  498. }
  499. else if(s < 42)
  500. {
  501. // Max error in interpolated form: 1.668e-17
  502. // Max error found at long double precision: 1.669714e-17
  503. static const T P[9] = {
  504. BOOST_MATH_BIG_CONSTANT(T, 64, -10.3948950573308861781),
  505. BOOST_MATH_BIG_CONSTANT(T, 64, -2.82646012777913950108),
  506. BOOST_MATH_BIG_CONSTANT(T, 64, -0.342144362739570333665),
  507. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0249285145498722647472),
  508. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00122493108848097114118),
  509. BOOST_MATH_BIG_CONSTANT(T, 64, -0.423055371192592850196e-4),
  510. BOOST_MATH_BIG_CONSTANT(T, 64, -0.1025215577185967488e-5),
  511. BOOST_MATH_BIG_CONSTANT(T, 64, -0.165096762663509467061e-7),
  512. BOOST_MATH_BIG_CONSTANT(T, 64, -0.145392555873022044329e-9),
  513. };
  514. static const T Q[10] = {
  515. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  516. BOOST_MATH_BIG_CONSTANT(T, 64, 0.205135978585281988052),
  517. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0192359357875879453602),
  518. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00111496452029715514119),
  519. BOOST_MATH_BIG_CONSTANT(T, 64, 0.434928449016693986857e-4),
  520. BOOST_MATH_BIG_CONSTANT(T, 64, 0.116911068726610725891e-5),
  521. BOOST_MATH_BIG_CONSTANT(T, 64, 0.206704342290235237475e-7),
  522. BOOST_MATH_BIG_CONSTANT(T, 64, 0.209772836100827647474e-9),
  523. BOOST_MATH_BIG_CONSTANT(T, 64, -0.939798249922234703384e-16),
  524. BOOST_MATH_BIG_CONSTANT(T, 64, 0.264584017421245080294e-18),
  525. };
  526. result = tools::evaluate_polynomial(P, T(s - 15)) / tools::evaluate_polynomial(Q, T(s - 15));
  527. result = 1 + exp(result);
  528. }
  529. else if(s < 63)
  530. {
  531. result = 1 + pow(T(2), -s);
  532. }
  533. else
  534. {
  535. result = 1;
  536. }
  537. return result;
  538. }
  539. template <class T, class Policy>
  540. T zeta_imp_prec(T s, T sc, const Policy&, const std::integral_constant<int, 113>&)
  541. {
  542. BOOST_MATH_STD_USING
  543. T result;
  544. if(s < 1)
  545. {
  546. // Rational Approximation
  547. // Maximum Deviation Found: 9.493e-37
  548. // Expected Error Term: 9.492e-37
  549. // Max error found at long double precision: 7.281332e-31
  550. static const T P[10] = {
  551. BOOST_MATH_BIG_CONSTANT(T, 113, -1.0),
  552. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0353008629988648122808504280990313668),
  553. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0107795651204927743049369868548706909),
  554. BOOST_MATH_BIG_CONSTANT(T, 113, 0.000523961870530500751114866884685172975),
  555. BOOST_MATH_BIG_CONSTANT(T, 113, -0.661805838304910731947595897966487515e-4),
  556. BOOST_MATH_BIG_CONSTANT(T, 113, -0.658932670403818558510656304189164638e-5),
  557. BOOST_MATH_BIG_CONSTANT(T, 113, -0.103437265642266106533814021041010453e-6),
  558. BOOST_MATH_BIG_CONSTANT(T, 113, 0.116818787212666457105375746642927737e-7),
  559. BOOST_MATH_BIG_CONSTANT(T, 113, 0.660690993901506912123512551294239036e-9),
  560. BOOST_MATH_BIG_CONSTANT(T, 113, 0.113103113698388531428914333768142527e-10),
  561. };
  562. static const T Q[11] = {
  563. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  564. BOOST_MATH_BIG_CONSTANT(T, 113, -0.387483472099602327112637481818565459),
  565. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0802265315091063135271497708694776875),
  566. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0110727276164171919280036408995078164),
  567. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00112552716946286252000434849173787243),
  568. BOOST_MATH_BIG_CONSTANT(T, 113, -0.874554160748626916455655180296834352e-4),
  569. BOOST_MATH_BIG_CONSTANT(T, 113, 0.530097847491828379568636739662278322e-5),
  570. BOOST_MATH_BIG_CONSTANT(T, 113, -0.248461553590496154705565904497247452e-6),
  571. BOOST_MATH_BIG_CONSTANT(T, 113, 0.881834921354014787309644951507523899e-8),
  572. BOOST_MATH_BIG_CONSTANT(T, 113, -0.217062446168217797598596496310953025e-9),
  573. BOOST_MATH_BIG_CONSTANT(T, 113, 0.315823200002384492377987848307151168e-11),
  574. };
  575. result = tools::evaluate_polynomial(P, sc) / tools::evaluate_polynomial(Q, sc);
  576. result += (sc);
  577. result /= (sc);
  578. }
  579. else if(s <= 2)
  580. {
  581. // Maximum Deviation Found: 1.616e-37
  582. // Expected Error Term: -1.615e-37
  583. static const T P[10] = {
  584. BOOST_MATH_BIG_CONSTANT(T, 113, 0.577215664901532860606512090082402431),
  585. BOOST_MATH_BIG_CONSTANT(T, 113, 0.255597968739771510415479842335906308),
  586. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0494056503552807274142218876983542205),
  587. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00551372778611700965268920983472292325),
  588. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00043667616723970574871427830895192731),
  589. BOOST_MATH_BIG_CONSTANT(T, 113, 0.268562259154821957743669387915239528e-4),
  590. BOOST_MATH_BIG_CONSTANT(T, 113, 0.109249633923016310141743084480436612e-5),
  591. BOOST_MATH_BIG_CONSTANT(T, 113, 0.273895554345300227466534378753023924e-7),
  592. BOOST_MATH_BIG_CONSTANT(T, 113, 0.583103205551702720149237384027795038e-9),
  593. BOOST_MATH_BIG_CONSTANT(T, 113, -0.835774625259919268768735944711219256e-11),
  594. };
  595. static const T Q[11] = {
  596. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  597. BOOST_MATH_BIG_CONSTANT(T, 113, 0.316661751179735502065583176348292881),
  598. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0540401806533507064453851182728635272),
  599. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00598621274107420237785899476374043797),
  600. BOOST_MATH_BIG_CONSTANT(T, 113, 0.000474907812321704156213038740142079615),
  601. BOOST_MATH_BIG_CONSTANT(T, 113, 0.272125421722314389581695715835862418e-4),
  602. BOOST_MATH_BIG_CONSTANT(T, 113, 0.112649552156479800925522445229212933e-5),
  603. BOOST_MATH_BIG_CONSTANT(T, 113, 0.301838975502992622733000078063330461e-7),
  604. BOOST_MATH_BIG_CONSTANT(T, 113, 0.422960728687211282539769943184270106e-9),
  605. BOOST_MATH_BIG_CONSTANT(T, 113, -0.377105263588822468076813329270698909e-11),
  606. BOOST_MATH_BIG_CONSTANT(T, 113, -0.581926559304525152432462127383600681e-13),
  607. };
  608. result = tools::evaluate_polynomial(P, T(-sc)) / tools::evaluate_polynomial(Q, T(-sc));
  609. result += 1 / (-sc);
  610. }
  611. else if(s <= 4)
  612. {
  613. // Maximum Deviation Found: 1.891e-36
  614. // Expected Error Term: -1.891e-36
  615. // Max error found: 2.171527e-35
  616. static const float Y = 0.6986598968505859375;
  617. static const T P[11] = {
  618. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0537258300023595010275848333539748089),
  619. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0429086930802630159457448174466342553),
  620. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0136148228754303412510213395034056857),
  621. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00190231601036042925183751238033763915),
  622. BOOST_MATH_BIG_CONSTANT(T, 113, 0.000186880390916311438818302549192456581),
  623. BOOST_MATH_BIG_CONSTANT(T, 113, 0.145347370745893262394287982691323657e-4),
  624. BOOST_MATH_BIG_CONSTANT(T, 113, 0.805843276446813106414036600485884885e-6),
  625. BOOST_MATH_BIG_CONSTANT(T, 113, 0.340818159286739137503297172091882574e-7),
  626. BOOST_MATH_BIG_CONSTANT(T, 113, 0.115762357488748996526167305116837246e-8),
  627. BOOST_MATH_BIG_CONSTANT(T, 113, 0.231904754577648077579913403645767214e-10),
  628. BOOST_MATH_BIG_CONSTANT(T, 113, 0.340169592866058506675897646629036044e-12),
  629. };
  630. static const T Q[12] = {
  631. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  632. BOOST_MATH_BIG_CONSTANT(T, 113, 0.363755247765087100018556983050520554),
  633. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0696581979014242539385695131258321598),
  634. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00882208914484611029571547753782014817),
  635. BOOST_MATH_BIG_CONSTANT(T, 113, 0.000815405623261946661762236085660996718),
  636. BOOST_MATH_BIG_CONSTANT(T, 113, 0.571366167062457197282642344940445452e-4),
  637. BOOST_MATH_BIG_CONSTANT(T, 113, 0.309278269271853502353954062051797838e-5),
  638. BOOST_MATH_BIG_CONSTANT(T, 113, 0.12822982083479010834070516053794262e-6),
  639. BOOST_MATH_BIG_CONSTANT(T, 113, 0.397876357325018976733953479182110033e-8),
  640. BOOST_MATH_BIG_CONSTANT(T, 113, 0.8484432107648683277598472295289279e-10),
  641. BOOST_MATH_BIG_CONSTANT(T, 113, 0.105677416606909614301995218444080615e-11),
  642. BOOST_MATH_BIG_CONSTANT(T, 113, 0.547223964564003701979951154093005354e-15),
  643. };
  644. result = tools::evaluate_polynomial(P, T(s - 2)) / tools::evaluate_polynomial(Q, T(s - 2));
  645. result += Y + 1 / (-sc);
  646. }
  647. else if(s <= 6)
  648. {
  649. // Max error in interpolated form: 1.510e-37
  650. // Max error found at long double precision: 2.769266e-34
  651. static const T Y = 3.28348541259765625F;
  652. static const T P[13] = {
  653. BOOST_MATH_BIG_CONSTANT(T, 113, 0.786383506575062179339611614117697622),
  654. BOOST_MATH_BIG_CONSTANT(T, 113, 0.495766593395271370974685959652073976),
  655. BOOST_MATH_BIG_CONSTANT(T, 113, -0.409116737851754766422360889037532228),
  656. BOOST_MATH_BIG_CONSTANT(T, 113, -0.57340744006238263817895456842655987),
  657. BOOST_MATH_BIG_CONSTANT(T, 113, -0.280479899797421910694892949057963111),
  658. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0753148409447590257157585696212649869),
  659. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0122934003684672788499099362823748632),
  660. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00126148398446193639247961370266962927),
  661. BOOST_MATH_BIG_CONSTANT(T, 113, -0.828465038179772939844657040917364896e-4),
  662. BOOST_MATH_BIG_CONSTANT(T, 113, -0.361008916706050977143208468690645684e-5),
  663. BOOST_MATH_BIG_CONSTANT(T, 113, -0.109879825497910544424797771195928112e-6),
  664. BOOST_MATH_BIG_CONSTANT(T, 113, -0.214539416789686920918063075528797059e-8),
  665. BOOST_MATH_BIG_CONSTANT(T, 113, -0.15090220092460596872172844424267351e-10),
  666. };
  667. static const T Q[14] = {
  668. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  669. BOOST_MATH_BIG_CONSTANT(T, 113, 1.69490865837142338462982225731926485),
  670. BOOST_MATH_BIG_CONSTANT(T, 113, 1.22697696630994080733321401255942464),
  671. BOOST_MATH_BIG_CONSTANT(T, 113, 0.495409420862526540074366618006341533),
  672. BOOST_MATH_BIG_CONSTANT(T, 113, 0.122368084916843823462872905024259633),
  673. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0191412993625268971656513890888208623),
  674. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00191401538628980617753082598351559642),
  675. BOOST_MATH_BIG_CONSTANT(T, 113, 0.000123318142456272424148930280876444459),
  676. BOOST_MATH_BIG_CONSTANT(T, 113, 0.531945488232526067889835342277595709e-5),
  677. BOOST_MATH_BIG_CONSTANT(T, 113, 0.161843184071894368337068779669116236e-6),
  678. BOOST_MATH_BIG_CONSTANT(T, 113, 0.305796079600152506743828859577462778e-8),
  679. BOOST_MATH_BIG_CONSTANT(T, 113, 0.233582592298450202680170811044408894e-10),
  680. BOOST_MATH_BIG_CONSTANT(T, 113, -0.275363878344548055574209713637734269e-13),
  681. BOOST_MATH_BIG_CONSTANT(T, 113, 0.221564186807357535475441900517843892e-15),
  682. };
  683. result = tools::evaluate_polynomial(P, T(s - 4)) / tools::evaluate_polynomial(Q, T(s - 4));
  684. result -= Y;
  685. result = 1 + exp(result);
  686. }
  687. else if(s < 10)
  688. {
  689. // Max error in interpolated form: 1.999e-34
  690. // Max error found at long double precision: 2.156186e-33
  691. static const T P[13] = {
  692. BOOST_MATH_BIG_CONSTANT(T, 113, -4.0545627381873738086704293881227365),
  693. BOOST_MATH_BIG_CONSTANT(T, 113, -4.70088348734699134347906176097717782),
  694. BOOST_MATH_BIG_CONSTANT(T, 113, -2.36921550900925512951976617607678789),
  695. BOOST_MATH_BIG_CONSTANT(T, 113, -0.684322583796369508367726293719322866),
  696. BOOST_MATH_BIG_CONSTANT(T, 113, -0.126026534540165129870721937592996324),
  697. BOOST_MATH_BIG_CONSTANT(T, 113, -0.015636903921778316147260572008619549),
  698. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00135442294754728549644376325814460807),
  699. BOOST_MATH_BIG_CONSTANT(T, 113, -0.842793965853572134365031384646117061e-4),
  700. BOOST_MATH_BIG_CONSTANT(T, 113, -0.385602133791111663372015460784978351e-5),
  701. BOOST_MATH_BIG_CONSTANT(T, 113, -0.130458500394692067189883214401478539e-6),
  702. BOOST_MATH_BIG_CONSTANT(T, 113, -0.315861074947230418778143153383660035e-8),
  703. BOOST_MATH_BIG_CONSTANT(T, 113, -0.500334720512030826996373077844707164e-10),
  704. BOOST_MATH_BIG_CONSTANT(T, 113, -0.420204769185233365849253969097184005e-12),
  705. };
  706. static const T Q[14] = {
  707. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  708. BOOST_MATH_BIG_CONSTANT(T, 113, 0.97663511666410096104783358493318814),
  709. BOOST_MATH_BIG_CONSTANT(T, 113, 0.40878780231201806504987368939673249),
  710. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0963890666609396058945084107597727252),
  711. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0142207619090854604824116070866614505),
  712. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00139010220902667918476773423995750877),
  713. BOOST_MATH_BIG_CONSTANT(T, 113, 0.940669540194694997889636696089994734e-4),
  714. BOOST_MATH_BIG_CONSTANT(T, 113, 0.458220848507517004399292480807026602e-5),
  715. BOOST_MATH_BIG_CONSTANT(T, 113, 0.16345521617741789012782420625435495e-6),
  716. BOOST_MATH_BIG_CONSTANT(T, 113, 0.414007452533083304371566316901024114e-8),
  717. BOOST_MATH_BIG_CONSTANT(T, 113, 0.68701473543366328016953742622661377e-10),
  718. BOOST_MATH_BIG_CONSTANT(T, 113, 0.603461891080716585087883971886075863e-12),
  719. BOOST_MATH_BIG_CONSTANT(T, 113, 0.294670713571839023181857795866134957e-16),
  720. BOOST_MATH_BIG_CONSTANT(T, 113, -0.147003914536437243143096875069813451e-18),
  721. };
  722. result = tools::evaluate_polynomial(P, T(s - 6)) / tools::evaluate_polynomial(Q, T(s - 6));
  723. result = 1 + exp(result);
  724. }
  725. else if(s < 17)
  726. {
  727. // Max error in interpolated form: 1.641e-32
  728. // Max error found at long double precision: 1.696121e-32
  729. static const T P[13] = {
  730. BOOST_MATH_BIG_CONSTANT(T, 113, -6.91319491921722925920883787894829678),
  731. BOOST_MATH_BIG_CONSTANT(T, 113, -3.65491257639481960248690596951049048),
  732. BOOST_MATH_BIG_CONSTANT(T, 113, -0.813557553449954526442644544105257881),
  733. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0994317301685870959473658713841138083),
  734. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00726896610245676520248617014211734906),
  735. BOOST_MATH_BIG_CONSTANT(T, 113, -0.000317253318715075854811266230916762929),
  736. BOOST_MATH_BIG_CONSTANT(T, 113, -0.66851422826636750855184211580127133e-5),
  737. BOOST_MATH_BIG_CONSTANT(T, 113, 0.879464154730985406003332577806849971e-7),
  738. BOOST_MATH_BIG_CONSTANT(T, 113, 0.113838903158254250631678791998294628e-7),
  739. BOOST_MATH_BIG_CONSTANT(T, 113, 0.379184410304927316385211327537817583e-9),
  740. BOOST_MATH_BIG_CONSTANT(T, 113, 0.612992858643904887150527613446403867e-11),
  741. BOOST_MATH_BIG_CONSTANT(T, 113, 0.347873737198164757035457841688594788e-13),
  742. BOOST_MATH_BIG_CONSTANT(T, 113, -0.289187187441625868404494665572279364e-15),
  743. };
  744. static const T Q[14] = {
  745. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  746. BOOST_MATH_BIG_CONSTANT(T, 113, 0.427310044448071818775721584949868806),
  747. BOOST_MATH_BIG_CONSTANT(T, 113, 0.074602514873055756201435421385243062),
  748. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00688651562174480772901425121653945942),
  749. BOOST_MATH_BIG_CONSTANT(T, 113, 0.000360174847635115036351323894321880445),
  750. BOOST_MATH_BIG_CONSTANT(T, 113, 0.973556847713307543918865405758248777e-5),
  751. BOOST_MATH_BIG_CONSTANT(T, 113, -0.853455848314516117964634714780874197e-8),
  752. BOOST_MATH_BIG_CONSTANT(T, 113, -0.118203513654855112421673192194622826e-7),
  753. BOOST_MATH_BIG_CONSTANT(T, 113, -0.462521662511754117095006543363328159e-9),
  754. BOOST_MATH_BIG_CONSTANT(T, 113, -0.834212591919475633107355719369463143e-11),
  755. BOOST_MATH_BIG_CONSTANT(T, 113, -0.5354594751002702935740220218582929e-13),
  756. BOOST_MATH_BIG_CONSTANT(T, 113, 0.406451690742991192964889603000756203e-15),
  757. BOOST_MATH_BIG_CONSTANT(T, 113, 0.887948682401000153828241615760146728e-19),
  758. BOOST_MATH_BIG_CONSTANT(T, 113, -0.34980761098820347103967203948619072e-21),
  759. };
  760. result = tools::evaluate_polynomial(P, T(s - 10)) / tools::evaluate_polynomial(Q, T(s - 10));
  761. result = 1 + exp(result);
  762. }
  763. else if(s < 30)
  764. {
  765. // Max error in interpolated form: 1.563e-31
  766. // Max error found at long double precision: 1.562725e-31
  767. static const T P[13] = {
  768. BOOST_MATH_BIG_CONSTANT(T, 113, -11.7824798233959252791987402769438322),
  769. BOOST_MATH_BIG_CONSTANT(T, 113, -4.36131215284987731928174218354118102),
  770. BOOST_MATH_BIG_CONSTANT(T, 113, -0.732260980060982349410898496846972204),
  771. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0744985185694913074484248803015717388),
  772. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00517228281320594683022294996292250527),
  773. BOOST_MATH_BIG_CONSTANT(T, 113, -0.000260897206152101522569969046299309939),
  774. BOOST_MATH_BIG_CONSTANT(T, 113, -0.989553462123121764865178453128769948e-5),
  775. BOOST_MATH_BIG_CONSTANT(T, 113, -0.286916799741891410827712096608826167e-6),
  776. BOOST_MATH_BIG_CONSTANT(T, 113, -0.637262477796046963617949532211619729e-8),
  777. BOOST_MATH_BIG_CONSTANT(T, 113, -0.106796831465628373325491288787760494e-9),
  778. BOOST_MATH_BIG_CONSTANT(T, 113, -0.129343095511091870860498356205376823e-11),
  779. BOOST_MATH_BIG_CONSTANT(T, 113, -0.102397936697965977221267881716672084e-13),
  780. BOOST_MATH_BIG_CONSTANT(T, 113, -0.402663128248642002351627980255756363e-16),
  781. };
  782. static const T Q[14] = {
  783. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  784. BOOST_MATH_BIG_CONSTANT(T, 113, 0.311288325355705609096155335186466508),
  785. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0438318468940415543546769437752132748),
  786. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00374396349183199548610264222242269536),
  787. BOOST_MATH_BIG_CONSTANT(T, 113, 0.000218707451200585197339671707189281302),
  788. BOOST_MATH_BIG_CONSTANT(T, 113, 0.927578767487930747532953583797351219e-5),
  789. BOOST_MATH_BIG_CONSTANT(T, 113, 0.294145760625753561951137473484889639e-6),
  790. BOOST_MATH_BIG_CONSTANT(T, 113, 0.704618586690874460082739479535985395e-8),
  791. BOOST_MATH_BIG_CONSTANT(T, 113, 0.126333332872897336219649130062221257e-9),
  792. BOOST_MATH_BIG_CONSTANT(T, 113, 0.16317315713773503718315435769352765e-11),
  793. BOOST_MATH_BIG_CONSTANT(T, 113, 0.137846712823719515148344938160275695e-13),
  794. BOOST_MATH_BIG_CONSTANT(T, 113, 0.580975420554224366450994232723910583e-16),
  795. BOOST_MATH_BIG_CONSTANT(T, 113, -0.291354445847552426900293580511392459e-22),
  796. BOOST_MATH_BIG_CONSTANT(T, 113, 0.73614324724785855925025452085443636e-25),
  797. };
  798. result = tools::evaluate_polynomial(P, T(s - 17)) / tools::evaluate_polynomial(Q, T(s - 17));
  799. result = 1 + exp(result);
  800. }
  801. else if(s < 74)
  802. {
  803. // Max error in interpolated form: 2.311e-27
  804. // Max error found at long double precision: 2.297544e-27
  805. static const T P[14] = {
  806. BOOST_MATH_BIG_CONSTANT(T, 113, -20.7944102007844314586649688802236072),
  807. BOOST_MATH_BIG_CONSTANT(T, 113, -4.95759941987499442499908748130192187),
  808. BOOST_MATH_BIG_CONSTANT(T, 113, -0.563290752832461751889194629200298688),
  809. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0406197001137935911912457120706122877),
  810. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0020846534789473022216888863613422293),
  811. BOOST_MATH_BIG_CONSTANT(T, 113, -0.808095978462109173749395599401375667e-4),
  812. BOOST_MATH_BIG_CONSTANT(T, 113, -0.244706022206249301640890603610060959e-5),
  813. BOOST_MATH_BIG_CONSTANT(T, 113, -0.589477682919645930544382616501666572e-7),
  814. BOOST_MATH_BIG_CONSTANT(T, 113, -0.113699573675553496343617442433027672e-8),
  815. BOOST_MATH_BIG_CONSTANT(T, 113, -0.174767860183598149649901223128011828e-10),
  816. BOOST_MATH_BIG_CONSTANT(T, 113, -0.210051620306761367764549971980026474e-12),
  817. BOOST_MATH_BIG_CONSTANT(T, 113, -0.189187969537370950337212675466400599e-14),
  818. BOOST_MATH_BIG_CONSTANT(T, 113, -0.116313253429564048145641663778121898e-16),
  819. BOOST_MATH_BIG_CONSTANT(T, 113, -0.376708747782400769427057630528578187e-19),
  820. };
  821. static const T Q[16] = {
  822. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  823. BOOST_MATH_BIG_CONSTANT(T, 113, 0.205076752981410805177554569784219717),
  824. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0202526722696670378999575738524540269),
  825. BOOST_MATH_BIG_CONSTANT(T, 113, 0.001278305290005994980069466658219057),
  826. BOOST_MATH_BIG_CONSTANT(T, 113, 0.576404779858501791742255670403304787e-4),
  827. BOOST_MATH_BIG_CONSTANT(T, 113, 0.196477049872253010859712483984252067e-5),
  828. BOOST_MATH_BIG_CONSTANT(T, 113, 0.521863830500876189501054079974475762e-7),
  829. BOOST_MATH_BIG_CONSTANT(T, 113, 0.109524209196868135198775445228552059e-8),
  830. BOOST_MATH_BIG_CONSTANT(T, 113, 0.181698713448644481083966260949267825e-10),
  831. BOOST_MATH_BIG_CONSTANT(T, 113, 0.234793316975091282090312036524695562e-12),
  832. BOOST_MATH_BIG_CONSTANT(T, 113, 0.227490441461460571047545264251399048e-14),
  833. BOOST_MATH_BIG_CONSTANT(T, 113, 0.151500292036937400913870642638520668e-16),
  834. BOOST_MATH_BIG_CONSTANT(T, 113, 0.543475775154780935815530649335936121e-19),
  835. BOOST_MATH_BIG_CONSTANT(T, 113, 0.241647013434111434636554455083309352e-28),
  836. BOOST_MATH_BIG_CONSTANT(T, 113, -0.557103423021951053707162364713587374e-31),
  837. BOOST_MATH_BIG_CONSTANT(T, 113, 0.618708773442584843384712258199645166e-34),
  838. };
  839. result = tools::evaluate_polynomial(P, T(s - 30)) / tools::evaluate_polynomial(Q, T(s - 30));
  840. result = 1 + exp(result);
  841. }
  842. else if(s < 117)
  843. {
  844. result = 1 + pow(T(2), -s);
  845. }
  846. else
  847. {
  848. result = 1;
  849. }
  850. return result;
  851. }
  852. template <class T, class Policy>
  853. T zeta_imp_odd_integer(int s, const T&, const Policy&, const std::true_type&)
  854. {
  855. static const T results[] = {
  856. BOOST_MATH_BIG_CONSTANT(T, 113, 1.2020569031595942853997381615114500), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0369277551433699263313654864570342), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0083492773819228268397975498497968), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0020083928260822144178527692324121), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0004941886041194645587022825264699), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0001227133475784891467518365263574), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000305882363070204935517285106451), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000076371976378997622736002935630), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000019082127165539389256569577951), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000004769329867878064631167196044), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000001192199259653110730677887189), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000298035035146522801860637051), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000074507117898354294919810042), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000018626597235130490064039099), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000004656629065033784072989233), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000001164155017270051977592974), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000291038504449709968692943), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000072759598350574810145209), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000018189896503070659475848), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000004547473783042154026799), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000001136868407680227849349), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000284217097688930185546), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000071054273952108527129), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000017763568435791203275), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000004440892103143813364), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000001110223025141066134), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000277555756213612417), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000069388939045441537), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000017347234760475766), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000004336808690020650), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000001084202172494241), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000271050543122347), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000067762635780452), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000016940658945098), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000004235164736273), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000001058791184068), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000264697796017), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000066174449004), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000016543612251), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000004135903063), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000001033975766), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000258493941), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000064623485), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000016155871), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000004038968), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000001009742), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000252435), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000063109), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000015777), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000003944), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000000986), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000000247), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000000062), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000000015), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000000004), BOOST_MATH_BIG_CONSTANT(T, 113, 1.0000000000000000000000000000000001),
  857. };
  858. return s > 113 ? 1 : results[(s - 3) / 2];
  859. }
  860. template <class T, class Policy>
  861. T zeta_imp_odd_integer(int s, const T& sc, const Policy& pol, const std::false_type&)
  862. {
  863. #ifdef BOOST_MATH_NO_THREAD_LOCAL_WITH_NON_TRIVIAL_TYPES
  864. static_assert(std::is_trivially_destructible<T>::value, "Your platform does not support thread_local with non-trivial types, last checked with Mingw-x64-8.1, Jan 2021. Please try a Mingw build with the POSIX threading model, see https://sourceforge.net/p/mingw-w64/bugs/527/");
  865. #endif
  866. static BOOST_MATH_THREAD_LOCAL bool is_init = false;
  867. static BOOST_MATH_THREAD_LOCAL T results[50] = {};
  868. static BOOST_MATH_THREAD_LOCAL int digits = tools::digits<T>();
  869. int current_digits = tools::digits<T>();
  870. if(digits != current_digits)
  871. {
  872. // Oh my precision has changed...
  873. is_init = false;
  874. }
  875. if(!is_init)
  876. {
  877. is_init = true;
  878. digits = current_digits;
  879. for(unsigned k = 0; k < sizeof(results) / sizeof(results[0]); ++k)
  880. {
  881. T arg = k * 2 + 3;
  882. T c_arg = 1 - arg;
  883. results[k] = zeta_polynomial_series(arg, c_arg, pol);
  884. }
  885. }
  886. unsigned index = (s - 3) / 2;
  887. return index >= sizeof(results) / sizeof(results[0]) ? zeta_polynomial_series(T(s), sc, pol): results[index];
  888. }
  889. template <class T, class Policy, class Tag>
  890. T zeta_imp(T s, T sc, const Policy& pol, const Tag& tag)
  891. {
  892. BOOST_MATH_STD_USING
  893. static const char* function = "boost::math::zeta<%1%>";
  894. if(sc == 0)
  895. return policies::raise_pole_error<T>(
  896. function,
  897. "Evaluation of zeta function at pole %1%",
  898. s, pol);
  899. T result;
  900. //
  901. // Trivial case:
  902. //
  903. if(s > policies::digits<T, Policy>())
  904. return 1;
  905. //
  906. // Start by seeing if we have a simple closed form:
  907. //
  908. if(floor(s) == s)
  909. {
  910. #ifndef BOOST_NO_EXCEPTIONS
  911. // Without exceptions we expect itrunc to return INT_MAX on overflow
  912. // and we fall through anyway.
  913. try
  914. {
  915. #endif
  916. int v = itrunc(s);
  917. if(v == s)
  918. {
  919. if(v < 0)
  920. {
  921. if(((-v) & 1) == 0)
  922. return 0;
  923. int n = (-v + 1) / 2;
  924. if(n <= (int)boost::math::max_bernoulli_b2n<T>::value)
  925. return T((-v & 1) ? -1 : 1) * boost::math::unchecked_bernoulli_b2n<T>(n) / (1 - v);
  926. }
  927. else if((v & 1) == 0)
  928. {
  929. if(((v / 2) <= (int)boost::math::max_bernoulli_b2n<T>::value) && (v <= (int)boost::math::max_factorial<T>::value))
  930. return T(((v / 2 - 1) & 1) ? -1 : 1) * ldexp(T(1), v - 1) * pow(constants::pi<T, Policy>(), v) *
  931. boost::math::unchecked_bernoulli_b2n<T>(v / 2) / boost::math::unchecked_factorial<T>(v);
  932. return T(((v / 2 - 1) & 1) ? -1 : 1) * ldexp(T(1), v - 1) * pow(constants::pi<T, Policy>(), v) *
  933. boost::math::bernoulli_b2n<T>(v / 2) / boost::math::factorial<T>(v, pol);
  934. }
  935. else
  936. return zeta_imp_odd_integer(v, sc, pol, std::integral_constant<bool, (Tag::value <= 113) && Tag::value>());
  937. }
  938. #ifndef BOOST_NO_EXCEPTIONS
  939. }
  940. catch(const boost::math::rounding_error&){} // Just fall through, s is too large to round
  941. catch(const std::overflow_error&){}
  942. #endif
  943. }
  944. if(fabs(s) < tools::root_epsilon<T>())
  945. {
  946. result = -0.5f - constants::log_root_two_pi<T, Policy>() * s;
  947. }
  948. else if(s < 0)
  949. {
  950. std::swap(s, sc);
  951. if(floor(sc/2) == sc/2)
  952. result = 0;
  953. else
  954. {
  955. if(s > max_factorial<T>::value)
  956. {
  957. T mult = boost::math::sin_pi(0.5f * sc, pol) * 2 * zeta_imp(s, sc, pol, tag);
  958. result = boost::math::lgamma(s, pol);
  959. result -= s * log(2 * constants::pi<T>());
  960. if(result > tools::log_max_value<T>())
  961. return sign(mult) * policies::raise_overflow_error<T>(function, 0, pol);
  962. result = exp(result);
  963. if(tools::max_value<T>() / fabs(mult) < result)
  964. return boost::math::sign(mult) * policies::raise_overflow_error<T>(function, 0, pol);
  965. result *= mult;
  966. }
  967. else
  968. {
  969. result = boost::math::sin_pi(0.5f * sc, pol)
  970. * 2 * pow(2 * constants::pi<T>(), -s)
  971. * boost::math::tgamma(s, pol)
  972. * zeta_imp(s, sc, pol, tag);
  973. }
  974. }
  975. }
  976. else
  977. {
  978. result = zeta_imp_prec(s, sc, pol, tag);
  979. }
  980. return result;
  981. }
  982. template <class T, class Policy, class tag>
  983. struct zeta_initializer
  984. {
  985. struct init
  986. {
  987. init()
  988. {
  989. do_init(tag());
  990. }
  991. static void do_init(const std::integral_constant<int, 0>&){ boost::math::zeta(static_cast<T>(5), Policy()); }
  992. static void do_init(const std::integral_constant<int, 53>&){ boost::math::zeta(static_cast<T>(5), Policy()); }
  993. static void do_init(const std::integral_constant<int, 64>&)
  994. {
  995. boost::math::zeta(static_cast<T>(0.5), Policy());
  996. boost::math::zeta(static_cast<T>(1.5), Policy());
  997. boost::math::zeta(static_cast<T>(3.5), Policy());
  998. boost::math::zeta(static_cast<T>(6.5), Policy());
  999. boost::math::zeta(static_cast<T>(14.5), Policy());
  1000. boost::math::zeta(static_cast<T>(40.5), Policy());
  1001. boost::math::zeta(static_cast<T>(5), Policy());
  1002. }
  1003. static void do_init(const std::integral_constant<int, 113>&)
  1004. {
  1005. boost::math::zeta(static_cast<T>(0.5), Policy());
  1006. boost::math::zeta(static_cast<T>(1.5), Policy());
  1007. boost::math::zeta(static_cast<T>(3.5), Policy());
  1008. boost::math::zeta(static_cast<T>(5.5), Policy());
  1009. boost::math::zeta(static_cast<T>(9.5), Policy());
  1010. boost::math::zeta(static_cast<T>(16.5), Policy());
  1011. boost::math::zeta(static_cast<T>(25.5), Policy());
  1012. boost::math::zeta(static_cast<T>(70.5), Policy());
  1013. boost::math::zeta(static_cast<T>(5), Policy());
  1014. }
  1015. void force_instantiate()const{}
  1016. };
  1017. static const init initializer;
  1018. static void force_instantiate()
  1019. {
  1020. initializer.force_instantiate();
  1021. }
  1022. };
  1023. template <class T, class Policy, class tag>
  1024. const typename zeta_initializer<T, Policy, tag>::init zeta_initializer<T, Policy, tag>::initializer;
  1025. } // detail
  1026. template <class T, class Policy>
  1027. inline typename tools::promote_args<T>::type zeta(T s, const Policy&)
  1028. {
  1029. typedef typename tools::promote_args<T>::type result_type;
  1030. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1031. typedef typename policies::precision<result_type, Policy>::type precision_type;
  1032. typedef typename policies::normalise<
  1033. Policy,
  1034. policies::promote_float<false>,
  1035. policies::promote_double<false>,
  1036. policies::discrete_quantile<>,
  1037. policies::assert_undefined<> >::type forwarding_policy;
  1038. typedef std::integral_constant<int,
  1039. precision_type::value <= 0 ? 0 :
  1040. precision_type::value <= 53 ? 53 :
  1041. precision_type::value <= 64 ? 64 :
  1042. precision_type::value <= 113 ? 113 : 0
  1043. > tag_type;
  1044. detail::zeta_initializer<value_type, forwarding_policy, tag_type>::force_instantiate();
  1045. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::zeta_imp(
  1046. static_cast<value_type>(s),
  1047. static_cast<value_type>(1 - static_cast<value_type>(s)),
  1048. forwarding_policy(),
  1049. tag_type()), "boost::math::zeta<%1%>(%1%)");
  1050. }
  1051. template <class T>
  1052. inline typename tools::promote_args<T>::type zeta(T s)
  1053. {
  1054. return zeta(s, policies::policy<>());
  1055. }
  1056. }} // namespaces
  1057. #endif // BOOST_MATH_ZETA_HPP