calculate_constants.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. // Copyright John Maddock 2010, 2012.
  2. // Copyright Paul A. Bristow 2011, 2012.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
  7. #define BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
  8. #include <boost/math/special_functions/trunc.hpp>
  9. namespace boost{ namespace math{ namespace constants{ namespace detail{
  10. template <class T>
  11. template<int N>
  12. inline T constant_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  13. {
  14. BOOST_MATH_STD_USING
  15. return ldexp(acos(T(0)), 1);
  16. /*
  17. // Although this code works well, it's usually more accurate to just call acos
  18. // and access the number types own representation of PI which is usually calculated
  19. // at slightly higher precision...
  20. T result;
  21. T a = 1;
  22. T b;
  23. T A(a);
  24. T B = 0.5f;
  25. T D = 0.25f;
  26. T lim;
  27. lim = boost::math::tools::epsilon<T>();
  28. unsigned k = 1;
  29. do
  30. {
  31. result = A + B;
  32. result = ldexp(result, -2);
  33. b = sqrt(B);
  34. a += b;
  35. a = ldexp(a, -1);
  36. A = a * a;
  37. B = A - result;
  38. B = ldexp(B, 1);
  39. result = A - B;
  40. bool neg = boost::math::sign(result) < 0;
  41. if(neg)
  42. result = -result;
  43. if(result <= lim)
  44. break;
  45. if(neg)
  46. result = -result;
  47. result = ldexp(result, k - 1);
  48. D -= result;
  49. ++k;
  50. lim = ldexp(lim, 1);
  51. }
  52. while(true);
  53. result = B / D;
  54. return result;
  55. */
  56. }
  57. template <class T>
  58. template<int N>
  59. inline T constant_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  60. {
  61. return 2 * pi<T, policies::policy<policies::digits2<N> > >();
  62. }
  63. template <class T> // 2 / pi
  64. template<int N>
  65. inline T constant_two_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  66. {
  67. return 2 / pi<T, policies::policy<policies::digits2<N> > >();
  68. }
  69. template <class T> // sqrt(2/pi)
  70. template <int N>
  71. inline T constant_root_two_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  72. {
  73. BOOST_MATH_STD_USING
  74. return sqrt((2 / pi<T, policies::policy<policies::digits2<N> > >()));
  75. }
  76. template <class T>
  77. template<int N>
  78. inline T constant_one_div_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  79. {
  80. return 1 / two_pi<T, policies::policy<policies::digits2<N> > >();
  81. }
  82. template <class T>
  83. template<int N>
  84. inline T constant_root_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  85. {
  86. BOOST_MATH_STD_USING
  87. return sqrt(pi<T, policies::policy<policies::digits2<N> > >());
  88. }
  89. template <class T>
  90. template<int N>
  91. inline T constant_root_half_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  92. {
  93. BOOST_MATH_STD_USING
  94. return sqrt(pi<T, policies::policy<policies::digits2<N> > >() / 2);
  95. }
  96. template <class T>
  97. template<int N>
  98. inline T constant_root_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  99. {
  100. BOOST_MATH_STD_USING
  101. return sqrt(two_pi<T, policies::policy<policies::digits2<N> > >());
  102. }
  103. template <class T>
  104. template<int N>
  105. inline T constant_log_root_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  106. {
  107. BOOST_MATH_STD_USING
  108. return log(root_two_pi<T, policies::policy<policies::digits2<N> > >());
  109. }
  110. template <class T>
  111. template<int N>
  112. inline T constant_root_ln_four<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  113. {
  114. BOOST_MATH_STD_USING
  115. return sqrt(log(static_cast<T>(4)));
  116. }
  117. template <class T>
  118. template<int N>
  119. inline T constant_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  120. {
  121. //
  122. // Although we can clearly calculate this from first principles, this hooks into
  123. // T's own notion of e, which hopefully will more accurate than one calculated to
  124. // a few epsilon:
  125. //
  126. BOOST_MATH_STD_USING
  127. return exp(static_cast<T>(1));
  128. }
  129. template <class T>
  130. template<int N>
  131. inline T constant_half<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  132. {
  133. return static_cast<T>(1) / static_cast<T>(2);
  134. }
  135. template <class T>
  136. template<int M>
  137. inline T constant_euler<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<M>))
  138. {
  139. BOOST_MATH_STD_USING
  140. //
  141. // This is the method described in:
  142. // "Some New Algorithms for High-Precision Computation of Euler's Constant"
  143. // Richard P Brent and Edwin M McMillan.
  144. // Mathematics of Computation, Volume 34, Number 149, Jan 1980, pages 305-312.
  145. // See equation 17 with p = 2.
  146. //
  147. T n = 3 + (M ? (std::min)(M, tools::digits<T>()) : tools::digits<T>()) / 4;
  148. T lim = M ? ldexp(T(1), 1 - (std::min)(M, tools::digits<T>())) : tools::epsilon<T>();
  149. T lnn = log(n);
  150. T term = 1;
  151. T N = -lnn;
  152. T D = 1;
  153. T Hk = 0;
  154. T one = 1;
  155. for(unsigned k = 1;; ++k)
  156. {
  157. term *= n * n;
  158. term /= k * k;
  159. Hk += one / k;
  160. N += term * (Hk - lnn);
  161. D += term;
  162. if(term < D * lim)
  163. break;
  164. }
  165. return N / D;
  166. }
  167. template <class T>
  168. template<int N>
  169. inline T constant_euler_sqr<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  170. {
  171. BOOST_MATH_STD_USING
  172. return euler<T, policies::policy<policies::digits2<N> > >()
  173. * euler<T, policies::policy<policies::digits2<N> > >();
  174. }
  175. template <class T>
  176. template<int N>
  177. inline T constant_one_div_euler<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  178. {
  179. BOOST_MATH_STD_USING
  180. return static_cast<T>(1)
  181. / euler<T, policies::policy<policies::digits2<N> > >();
  182. }
  183. template <class T>
  184. template<int N>
  185. inline T constant_root_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  186. {
  187. BOOST_MATH_STD_USING
  188. return sqrt(static_cast<T>(2));
  189. }
  190. template <class T>
  191. template<int N>
  192. inline T constant_root_three<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  193. {
  194. BOOST_MATH_STD_USING
  195. return sqrt(static_cast<T>(3));
  196. }
  197. template <class T>
  198. template<int N>
  199. inline T constant_half_root_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  200. {
  201. BOOST_MATH_STD_USING
  202. return sqrt(static_cast<T>(2)) / 2;
  203. }
  204. template <class T>
  205. template<int N>
  206. inline T constant_ln_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  207. {
  208. //
  209. // Although there are good ways to calculate this from scratch, this hooks into
  210. // T's own notion of log(2) which will hopefully be accurate to the full precision
  211. // of T:
  212. //
  213. BOOST_MATH_STD_USING
  214. return log(static_cast<T>(2));
  215. }
  216. template <class T>
  217. template<int N>
  218. inline T constant_ln_ten<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  219. {
  220. BOOST_MATH_STD_USING
  221. return log(static_cast<T>(10));
  222. }
  223. template <class T>
  224. template<int N>
  225. inline T constant_ln_ln_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  226. {
  227. BOOST_MATH_STD_USING
  228. return log(log(static_cast<T>(2)));
  229. }
  230. template <class T>
  231. template<int N>
  232. inline T constant_third<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  233. {
  234. BOOST_MATH_STD_USING
  235. return static_cast<T>(1) / static_cast<T>(3);
  236. }
  237. template <class T>
  238. template<int N>
  239. inline T constant_twothirds<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  240. {
  241. BOOST_MATH_STD_USING
  242. return static_cast<T>(2) / static_cast<T>(3);
  243. }
  244. template <class T>
  245. template<int N>
  246. inline T constant_two_thirds<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  247. {
  248. BOOST_MATH_STD_USING
  249. return static_cast<T>(2) / static_cast<T>(3);
  250. }
  251. template <class T>
  252. template<int N>
  253. inline T constant_three_quarters<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  254. {
  255. BOOST_MATH_STD_USING
  256. return static_cast<T>(3) / static_cast<T>(4);
  257. }
  258. template <class T>
  259. template<int N>
  260. inline T constant_sixth<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  261. {
  262. BOOST_MATH_STD_USING
  263. return static_cast<T>(1) / static_cast<T>(6);
  264. }
  265. // Pi and related constants.
  266. template <class T>
  267. template<int N>
  268. inline T constant_pi_minus_three<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  269. {
  270. return pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(3);
  271. }
  272. template <class T>
  273. template<int N>
  274. inline T constant_four_minus_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  275. {
  276. return static_cast<T>(4) - pi<T, policies::policy<policies::digits2<N> > >();
  277. }
  278. //template <class T>
  279. //template<int N>
  280. //inline T constant_pow23_four_minus_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  281. //{
  282. // BOOST_MATH_STD_USING
  283. // return pow(four_minus_pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1.5));
  284. //}
  285. template <class T>
  286. template<int N>
  287. inline T constant_exp_minus_half<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  288. {
  289. BOOST_MATH_STD_USING
  290. return exp(static_cast<T>(-0.5));
  291. }
  292. template <class T>
  293. template<int N>
  294. inline T constant_exp_minus_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  295. {
  296. BOOST_MATH_STD_USING
  297. return exp(static_cast<T>(-1.));
  298. }
  299. template <class T>
  300. template<int N>
  301. inline T constant_one_div_root_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  302. {
  303. return static_cast<T>(1) / root_two<T, policies::policy<policies::digits2<N> > >();
  304. }
  305. template <class T>
  306. template<int N>
  307. inline T constant_one_div_root_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  308. {
  309. return static_cast<T>(1) / root_pi<T, policies::policy<policies::digits2<N> > >();
  310. }
  311. template <class T>
  312. template<int N>
  313. inline T constant_one_div_root_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  314. {
  315. return static_cast<T>(1) / root_two_pi<T, policies::policy<policies::digits2<N> > >();
  316. }
  317. template <class T>
  318. template<int N>
  319. inline T constant_root_one_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  320. {
  321. BOOST_MATH_STD_USING
  322. return sqrt(static_cast<T>(1) / pi<T, policies::policy<policies::digits2<N> > >());
  323. }
  324. template <class T>
  325. template<int N>
  326. inline T constant_four_thirds_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  327. {
  328. BOOST_MATH_STD_USING
  329. return pi<T, policies::policy<policies::digits2<N> > >() * static_cast<T>(4) / static_cast<T>(3);
  330. }
  331. template <class T>
  332. template<int N>
  333. inline T constant_half_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  334. {
  335. BOOST_MATH_STD_USING
  336. return pi<T, policies::policy<policies::digits2<N> > >() / static_cast<T>(2);
  337. }
  338. template <class T>
  339. template<int N>
  340. inline T constant_third_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  341. {
  342. BOOST_MATH_STD_USING
  343. return pi<T, policies::policy<policies::digits2<N> > >() / static_cast<T>(3);
  344. }
  345. template <class T>
  346. template<int N>
  347. inline T constant_sixth_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  348. {
  349. BOOST_MATH_STD_USING
  350. return pi<T, policies::policy<policies::digits2<N> > >() / static_cast<T>(6);
  351. }
  352. template <class T>
  353. template<int N>
  354. inline T constant_two_thirds_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  355. {
  356. BOOST_MATH_STD_USING
  357. return pi<T, policies::policy<policies::digits2<N> > >() * static_cast<T>(2) / static_cast<T>(3);
  358. }
  359. template <class T>
  360. template<int N>
  361. inline T constant_three_quarters_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  362. {
  363. BOOST_MATH_STD_USING
  364. return pi<T, policies::policy<policies::digits2<N> > >() * static_cast<T>(3) / static_cast<T>(4);
  365. }
  366. template <class T>
  367. template<int N>
  368. inline T constant_pi_pow_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  369. {
  370. BOOST_MATH_STD_USING
  371. return pow(pi<T, policies::policy<policies::digits2<N> > >(), e<T, policies::policy<policies::digits2<N> > >()); //
  372. }
  373. template <class T>
  374. template<int N>
  375. inline T constant_pi_sqr<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  376. {
  377. BOOST_MATH_STD_USING
  378. return pi<T, policies::policy<policies::digits2<N> > >()
  379. * pi<T, policies::policy<policies::digits2<N> > >() ; //
  380. }
  381. template <class T>
  382. template<int N>
  383. inline T constant_pi_sqr_div_six<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  384. {
  385. BOOST_MATH_STD_USING
  386. return pi<T, policies::policy<policies::digits2<N> > >()
  387. * pi<T, policies::policy<policies::digits2<N> > >()
  388. / static_cast<T>(6); //
  389. }
  390. template <class T>
  391. template<int N>
  392. inline T constant_pi_cubed<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  393. {
  394. BOOST_MATH_STD_USING
  395. return pi<T, policies::policy<policies::digits2<N> > >()
  396. * pi<T, policies::policy<policies::digits2<N> > >()
  397. * pi<T, policies::policy<policies::digits2<N> > >()
  398. ; //
  399. }
  400. template <class T>
  401. template<int N>
  402. inline T constant_cbrt_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  403. {
  404. BOOST_MATH_STD_USING
  405. return pow(pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1)/ static_cast<T>(3));
  406. }
  407. template <class T>
  408. template<int N>
  409. inline T constant_one_div_cbrt_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  410. {
  411. BOOST_MATH_STD_USING
  412. return static_cast<T>(1)
  413. / pow(pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1)/ static_cast<T>(3));
  414. }
  415. // Euler's e
  416. template <class T>
  417. template<int N>
  418. inline T constant_e_pow_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  419. {
  420. BOOST_MATH_STD_USING
  421. return pow(e<T, policies::policy<policies::digits2<N> > >(), pi<T, policies::policy<policies::digits2<N> > >()); //
  422. }
  423. template <class T>
  424. template<int N>
  425. inline T constant_root_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  426. {
  427. BOOST_MATH_STD_USING
  428. return sqrt(e<T, policies::policy<policies::digits2<N> > >());
  429. }
  430. template <class T>
  431. template<int N>
  432. inline T constant_log10_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  433. {
  434. BOOST_MATH_STD_USING
  435. return log10(e<T, policies::policy<policies::digits2<N> > >());
  436. }
  437. template <class T>
  438. template<int N>
  439. inline T constant_one_div_log10_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  440. {
  441. BOOST_MATH_STD_USING
  442. return static_cast<T>(1) /
  443. log10(e<T, policies::policy<policies::digits2<N> > >());
  444. }
  445. // Trigonometric
  446. template <class T>
  447. template<int N>
  448. inline T constant_degree<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  449. {
  450. BOOST_MATH_STD_USING
  451. return pi<T, policies::policy<policies::digits2<N> > >()
  452. / static_cast<T>(180)
  453. ; //
  454. }
  455. template <class T>
  456. template<int N>
  457. inline T constant_radian<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  458. {
  459. BOOST_MATH_STD_USING
  460. return static_cast<T>(180)
  461. / pi<T, policies::policy<policies::digits2<N> > >()
  462. ; //
  463. }
  464. template <class T>
  465. template<int N>
  466. inline T constant_sin_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  467. {
  468. BOOST_MATH_STD_USING
  469. return sin(static_cast<T>(1)) ; //
  470. }
  471. template <class T>
  472. template<int N>
  473. inline T constant_cos_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  474. {
  475. BOOST_MATH_STD_USING
  476. return cos(static_cast<T>(1)) ; //
  477. }
  478. template <class T>
  479. template<int N>
  480. inline T constant_sinh_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  481. {
  482. BOOST_MATH_STD_USING
  483. return sinh(static_cast<T>(1)) ; //
  484. }
  485. template <class T>
  486. template<int N>
  487. inline T constant_cosh_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  488. {
  489. BOOST_MATH_STD_USING
  490. return cosh(static_cast<T>(1)) ; //
  491. }
  492. template <class T>
  493. template<int N>
  494. inline T constant_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  495. {
  496. BOOST_MATH_STD_USING
  497. return (static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) ; //
  498. }
  499. template <class T>
  500. template<int N>
  501. inline T constant_ln_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  502. {
  503. BOOST_MATH_STD_USING
  504. //return log(phi<T, policies::policy<policies::digits2<N> > >()); // ???
  505. return log((static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) );
  506. }
  507. template <class T>
  508. template<int N>
  509. inline T constant_one_div_ln_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  510. {
  511. BOOST_MATH_STD_USING
  512. return static_cast<T>(1) /
  513. log((static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) );
  514. }
  515. // Zeta
  516. template <class T>
  517. template<int N>
  518. inline T constant_zeta_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  519. {
  520. BOOST_MATH_STD_USING
  521. return pi<T, policies::policy<policies::digits2<N> > >()
  522. * pi<T, policies::policy<policies::digits2<N> > >()
  523. /static_cast<T>(6);
  524. }
  525. template <class T>
  526. template<int N>
  527. inline T constant_zeta_three<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  528. {
  529. // http://mathworld.wolfram.com/AperysConstant.html
  530. // http://en.wikipedia.org/wiki/Mathematical_constant
  531. // http://oeis.org/A002117/constant
  532. //T zeta3("1.20205690315959428539973816151144999076"
  533. // "4986292340498881792271555341838205786313"
  534. // "09018645587360933525814619915");
  535. //"1.202056903159594285399738161511449990, 76498629234049888179227155534183820578631309018645587360933525814619915" A002117
  536. // 1.202056903159594285399738161511449990, 76498629234049888179227155534183820578631309018645587360933525814619915780, +00);
  537. //"1.2020569031595942 double
  538. // http://www.spaennare.se/SSPROG/ssnum.pdf // section 11, Algorithm for Apery's constant zeta(3).
  539. // Programs to Calculate some Mathematical Constants to Large Precision, Document Version 1.50
  540. // by Stefan Spannare September 19, 2007
  541. // zeta(3) = 1/64 * sum
  542. BOOST_MATH_STD_USING
  543. T n_fact=static_cast<T>(1); // build n! for n = 0.
  544. T sum = static_cast<double>(77); // Start with n = 0 case.
  545. // for n = 0, (77/1) /64 = 1.203125
  546. //double lim = std::numeric_limits<double>::epsilon();
  547. T lim = N ? ldexp(T(1), 1 - (std::min)(N, tools::digits<T>())) : tools::epsilon<T>();
  548. for(unsigned int n = 1; n < 40; ++n)
  549. { // three to five decimal digits per term, so 40 should be plenty for 100 decimal digits.
  550. //cout << "n = " << n << endl;
  551. n_fact *= n; // n!
  552. T n_fact_p10 = n_fact * n_fact * n_fact * n_fact * n_fact * n_fact * n_fact * n_fact * n_fact * n_fact; // (n!)^10
  553. T num = ((205 * n * n) + (250 * n) + 77) * n_fact_p10; // 205n^2 + 250n + 77
  554. // int nn = (2 * n + 1);
  555. // T d = factorial(nn); // inline factorial.
  556. T d = 1;
  557. for(unsigned int i = 1; i <= (n+n + 1); ++i) // (2n + 1)
  558. {
  559. d *= i;
  560. }
  561. T den = d * d * d * d * d; // [(2n+1)!]^5
  562. //cout << "den = " << den << endl;
  563. T term = num/den;
  564. if (n % 2 != 0)
  565. { //term *= -1;
  566. sum -= term;
  567. }
  568. else
  569. {
  570. sum += term;
  571. }
  572. //cout << "term = " << term << endl;
  573. //cout << "sum/64 = " << sum/64 << endl;
  574. if(abs(term) < lim)
  575. {
  576. break;
  577. }
  578. }
  579. return sum / 64;
  580. }
  581. template <class T>
  582. template<int N>
  583. inline T constant_catalan<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  584. { // http://oeis.org/A006752/constant
  585. //T c("0.915965594177219015054603514932384110774"
  586. //"149374281672134266498119621763019776254769479356512926115106248574");
  587. // 9.159655941772190150546035149323841107, 74149374281672134266498119621763019776254769479356512926115106248574422619, -01);
  588. // This is equation (entry) 31 from
  589. // http://www-2.cs.cmu.edu/~adamchik/articles/catalan/catalan.htm
  590. // See also http://www.mpfr.org/algorithms.pdf
  591. BOOST_MATH_STD_USING
  592. T k_fact = 1;
  593. T tk_fact = 1;
  594. T sum = 1;
  595. T term;
  596. T lim = N ? ldexp(T(1), 1 - (std::min)(N, tools::digits<T>())) : tools::epsilon<T>();
  597. for(unsigned k = 1;; ++k)
  598. {
  599. k_fact *= k;
  600. tk_fact *= (2 * k) * (2 * k - 1);
  601. term = k_fact * k_fact / (tk_fact * (2 * k + 1) * (2 * k + 1));
  602. sum += term;
  603. if(term < lim)
  604. {
  605. break;
  606. }
  607. }
  608. return boost::math::constants::pi<T, boost::math::policies::policy<> >()
  609. * log(2 + boost::math::constants::root_three<T, boost::math::policies::policy<> >())
  610. / 8
  611. + 3 * sum / 8;
  612. }
  613. namespace khinchin_detail{
  614. template <class T>
  615. T zeta_polynomial_series(T s, T sc, int digits)
  616. {
  617. BOOST_MATH_STD_USING
  618. //
  619. // This is algorithm 3 from:
  620. //
  621. // "An Efficient Algorithm for the Riemann Zeta Function", P. Borwein,
  622. // Canadian Mathematical Society, Conference Proceedings, 2000.
  623. // See: http://www.cecm.sfu.ca/personal/pborwein/PAPERS/P155.pdf
  624. //
  625. BOOST_MATH_STD_USING
  626. int n = (digits * 19) / 53;
  627. T sum = 0;
  628. T two_n = ldexp(T(1), n);
  629. int ej_sign = 1;
  630. for(int j = 0; j < n; ++j)
  631. {
  632. sum += ej_sign * -two_n / pow(T(j + 1), s);
  633. ej_sign = -ej_sign;
  634. }
  635. T ej_sum = 1;
  636. T ej_term = 1;
  637. for(int j = n; j <= 2 * n - 1; ++j)
  638. {
  639. sum += ej_sign * (ej_sum - two_n) / pow(T(j + 1), s);
  640. ej_sign = -ej_sign;
  641. ej_term *= 2 * n - j;
  642. ej_term /= j - n + 1;
  643. ej_sum += ej_term;
  644. }
  645. return -sum / (two_n * (1 - pow(T(2), sc)));
  646. }
  647. template <class T>
  648. T khinchin(int digits)
  649. {
  650. BOOST_MATH_STD_USING
  651. T sum = 0;
  652. T term;
  653. T lim = ldexp(T(1), 1-digits);
  654. T factor = 0;
  655. unsigned last_k = 1;
  656. T num = 1;
  657. for(unsigned n = 1;; ++n)
  658. {
  659. for(unsigned k = last_k; k <= 2 * n - 1; ++k)
  660. {
  661. factor += num / k;
  662. num = -num;
  663. }
  664. last_k = 2 * n;
  665. term = (zeta_polynomial_series(T(2 * n), T(1 - T(2 * n)), digits) - 1) * factor / n;
  666. sum += term;
  667. if(term < lim)
  668. break;
  669. }
  670. return exp(sum / boost::math::constants::ln_two<T, boost::math::policies::policy<> >());
  671. }
  672. }
  673. template <class T>
  674. template<int N>
  675. inline T constant_khinchin<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  676. {
  677. int n = N ? (std::min)(N, tools::digits<T>()) : tools::digits<T>();
  678. return khinchin_detail::khinchin<T>(n);
  679. }
  680. template <class T>
  681. template<int N>
  682. inline T constant_extreme_value_skewness<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  683. { // from e_float constants.cpp
  684. // Mathematica: N[12 Sqrt[6] Zeta[3]/Pi^3, 1101]
  685. BOOST_MATH_STD_USING
  686. T ev(12 * sqrt(static_cast<T>(6)) * zeta_three<T, policies::policy<policies::digits2<N> > >()
  687. / pi_cubed<T, policies::policy<policies::digits2<N> > >() );
  688. //T ev(
  689. //"1.1395470994046486574927930193898461120875997958365518247216557100852480077060706857071875468869385150"
  690. //"1894272048688553376986765366075828644841024041679714157616857834895702411080704529137366329462558680"
  691. //"2015498788776135705587959418756809080074611906006528647805347822929577145038743873949415294942796280"
  692. //"0895597703063466053535550338267721294164578901640163603544404938283861127819804918174973533694090594"
  693. //"3094963822672055237678432023017824416203652657301470473548274848068762500300316769691474974950757965"
  694. //"8640779777748741897542093874605477776538884083378029488863880220988107155275203245233994097178778984"
  695. //"3488995668362387892097897322246698071290011857605809901090220903955815127463328974447572119951192970"
  696. //"3684453635456559086126406960279692862247058250100678008419431185138019869693206366891639436908462809"
  697. //"9756051372711251054914491837034685476095423926553367264355374652153595857163724698198860485357368964"
  698. //"3807049634423621246870868566707915720704996296083373077647528285782964567312903914752617978405994377"
  699. //"9064157147206717895272199736902453130842229559980076472936976287378945035706933650987259357729800315");
  700. return ev;
  701. }
  702. namespace detail{
  703. //
  704. // Calculation of the Glaisher constant depends upon calculating the
  705. // derivative of the zeta function at 2, we can then use the relation:
  706. // zeta'(2) = 1/6 pi^2 [euler + ln(2pi)-12ln(A)]
  707. // To get the constant A.
  708. // See equation 45 at http://mathworld.wolfram.com/RiemannZetaFunction.html.
  709. //
  710. // The derivative of the zeta function is computed by direct differentiation
  711. // of the relation:
  712. // (1-2^(1-s))zeta(s) = SUM(n=0, INF){ (-n)^n / (n+1)^s }
  713. // Which gives us 2 slowly converging but alternating sums to compute,
  714. // for this we use Algorithm 1 from "Convergent Acceleration of Alternating Series",
  715. // Henri Cohen, Fernando Rodriguez Villegas and Don Zagier, Experimental Mathematics 9:1 (1999).
  716. // See http://www.math.utexas.edu/users/villegas/publications/conv-accel.pdf
  717. //
  718. template <class T>
  719. T zeta_series_derivative_2(unsigned digits)
  720. {
  721. // Derivative of the series part, evaluated at 2:
  722. BOOST_MATH_STD_USING
  723. int n = digits * 301 * 13 / 10000;
  724. boost::math::itrunc((std::numeric_limits<T>::digits10 + 1) * 1.3);
  725. T d = pow(3 + sqrt(T(8)), n);
  726. d = (d + 1 / d) / 2;
  727. T b = -1;
  728. T c = -d;
  729. T s = 0;
  730. for(int k = 0; k < n; ++k)
  731. {
  732. T a = -log(T(k+1)) / ((k+1) * (k+1));
  733. c = b - c;
  734. s = s + c * a;
  735. b = (k + n) * (k - n) * b / ((k + T(0.5f)) * (k + 1));
  736. }
  737. return s / d;
  738. }
  739. template <class T>
  740. T zeta_series_2(unsigned digits)
  741. {
  742. // Series part of zeta at 2:
  743. BOOST_MATH_STD_USING
  744. int n = digits * 301 * 13 / 10000;
  745. T d = pow(3 + sqrt(T(8)), n);
  746. d = (d + 1 / d) / 2;
  747. T b = -1;
  748. T c = -d;
  749. T s = 0;
  750. for(int k = 0; k < n; ++k)
  751. {
  752. T a = T(1) / ((k + 1) * (k + 1));
  753. c = b - c;
  754. s = s + c * a;
  755. b = (k + n) * (k - n) * b / ((k + T(0.5f)) * (k + 1));
  756. }
  757. return s / d;
  758. }
  759. template <class T>
  760. inline T zeta_series_lead_2()
  761. {
  762. // lead part at 2:
  763. return 2;
  764. }
  765. template <class T>
  766. inline T zeta_series_derivative_lead_2()
  767. {
  768. // derivative of lead part at 2:
  769. return -2 * boost::math::constants::ln_two<T>();
  770. }
  771. template <class T>
  772. inline T zeta_derivative_2(unsigned n)
  773. {
  774. // zeta derivative at 2:
  775. return zeta_series_derivative_2<T>(n) * zeta_series_lead_2<T>()
  776. + zeta_series_derivative_lead_2<T>() * zeta_series_2<T>(n);
  777. }
  778. } // namespace detail
  779. template <class T>
  780. template<int N>
  781. inline T constant_glaisher<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  782. {
  783. BOOST_MATH_STD_USING
  784. typedef policies::policy<policies::digits2<N> > forwarding_policy;
  785. int n = N ? (std::min)(N, tools::digits<T>()) : tools::digits<T>();
  786. T v = detail::zeta_derivative_2<T>(n);
  787. v *= 6;
  788. v /= boost::math::constants::pi<T, forwarding_policy>() * boost::math::constants::pi<T, forwarding_policy>();
  789. v -= boost::math::constants::euler<T, forwarding_policy>();
  790. v -= log(2 * boost::math::constants::pi<T, forwarding_policy>());
  791. v /= -12;
  792. return exp(v);
  793. /*
  794. // from http://mpmath.googlecode.com/svn/data/glaisher.txt
  795. // 20,000 digits of the Glaisher-Kinkelin constant A = exp(1/2 - zeta'(-1))
  796. // Computed using A = exp((6 (-zeta'(2))/pi^2 + log 2 pi + gamma)/12)
  797. // with Euler-Maclaurin summation for zeta'(2).
  798. T g(
  799. "1.282427129100622636875342568869791727767688927325001192063740021740406308858826"
  800. "46112973649195820237439420646120399000748933157791362775280404159072573861727522"
  801. "14334327143439787335067915257366856907876561146686449997784962754518174312394652"
  802. "76128213808180219264516851546143919901083573730703504903888123418813674978133050"
  803. "93770833682222494115874837348064399978830070125567001286994157705432053927585405"
  804. "81731588155481762970384743250467775147374600031616023046613296342991558095879293"
  805. "36343887288701988953460725233184702489001091776941712153569193674967261270398013"
  806. "52652668868978218897401729375840750167472114895288815996668743164513890306962645"
  807. "59870469543740253099606800842447417554061490189444139386196089129682173528798629"
  808. "88434220366989900606980888785849587494085307347117090132667567503310523405221054"
  809. "14176776156308191919997185237047761312315374135304725819814797451761027540834943"
  810. "14384965234139453373065832325673954957601692256427736926358821692159870775858274"
  811. "69575162841550648585890834128227556209547002918593263079373376942077522290940187");
  812. return g;
  813. */
  814. }
  815. template <class T>
  816. template<int N>
  817. inline T constant_rayleigh_skewness<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  818. { // From e_float
  819. // 1100 digits of the Rayleigh distribution skewness
  820. // Mathematica: N[2 Sqrt[Pi] (Pi - 3)/((4 - Pi)^(3/2)), 1100]
  821. BOOST_MATH_STD_USING
  822. T rs(2 * root_pi<T, policies::policy<policies::digits2<N> > >()
  823. * pi_minus_three<T, policies::policy<policies::digits2<N> > >()
  824. / pow(four_minus_pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(3./2))
  825. );
  826. // 6.31110657818937138191899351544227779844042203134719497658094585692926819617473725459905027032537306794400047264,
  827. //"0.6311106578189371381918993515442277798440422031347194976580945856929268196174737254599050270325373067"
  828. //"9440004726436754739597525250317640394102954301685809920213808351450851396781817932734836994829371322"
  829. //"5797376021347531983451654130317032832308462278373358624120822253764532674177325950686466133508511968"
  830. //"2389168716630349407238090652663422922072397393006683401992961569208109477307776249225072042971818671"
  831. //"4058887072693437217879039875871765635655476241624825389439481561152126886932506682176611183750503553"
  832. //"1218982627032068396407180216351425758181396562859085306247387212297187006230007438534686340210168288"
  833. //"8956816965453815849613622117088096547521391672977226658826566757207615552041767516828171274858145957"
  834. //"6137539156656005855905288420585194082284972984285863898582313048515484073396332610565441264220790791"
  835. //"0194897267890422924599776483890102027823328602965235306539844007677157873140562950510028206251529523"
  836. //"7428049693650605954398446899724157486062545281504433364675815915402937209673727753199567661561209251"
  837. //"4695589950526053470201635372590001578503476490223746511106018091907936826431407434894024396366284848"); ;
  838. return rs;
  839. }
  840. template <class T>
  841. template<int N>
  842. inline T constant_rayleigh_kurtosis_excess<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  843. { // - (6 Pi^2 - 24 Pi + 16)/((Pi - 4)^2)
  844. // Might provide and calculate this using pi_minus_four.
  845. BOOST_MATH_STD_USING
  846. return - (((static_cast<T>(6) * pi<T, policies::policy<policies::digits2<N> > >()
  847. * pi<T, policies::policy<policies::digits2<N> > >())
  848. - (static_cast<T>(24) * pi<T, policies::policy<policies::digits2<N> > >()) + static_cast<T>(16) )
  849. /
  850. ((pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4))
  851. * (pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4)))
  852. );
  853. }
  854. template <class T>
  855. template<int N>
  856. inline T constant_rayleigh_kurtosis<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  857. { // 3 - (6 Pi^2 - 24 Pi + 16)/((Pi - 4)^2)
  858. // Might provide and calculate this using pi_minus_four.
  859. BOOST_MATH_STD_USING
  860. return static_cast<T>(3) - (((static_cast<T>(6) * pi<T, policies::policy<policies::digits2<N> > >()
  861. * pi<T, policies::policy<policies::digits2<N> > >())
  862. - (static_cast<T>(24) * pi<T, policies::policy<policies::digits2<N> > >()) + static_cast<T>(16) )
  863. /
  864. ((pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4))
  865. * (pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4)))
  866. );
  867. }
  868. template <class T>
  869. template<int N>
  870. inline T constant_log2_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  871. {
  872. return 1 / boost::math::constants::ln_two<T>();
  873. }
  874. template <class T>
  875. template<int N>
  876. inline T constant_quarter_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  877. {
  878. return boost::math::constants::pi<T>() / 4;
  879. }
  880. template <class T>
  881. template<int N>
  882. inline T constant_one_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  883. {
  884. return 1 / boost::math::constants::pi<T>();
  885. }
  886. template <class T>
  887. template<int N>
  888. inline T constant_two_div_root_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  889. {
  890. return 2 * boost::math::constants::one_div_root_pi<T>();
  891. }
  892. }
  893. }
  894. }
  895. } // namespaces
  896. #endif // BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED