calculate_constants.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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_pi_minus_three<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  261. {
  262. return pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(3);
  263. }
  264. template <class T>
  265. template<int N>
  266. inline T constant_four_minus_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  267. {
  268. return static_cast<T>(4) - pi<T, policies::policy<policies::digits2<N> > >();
  269. }
  270. template <class T>
  271. template<int N>
  272. inline T constant_pow23_four_minus_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  273. {
  274. BOOST_MATH_STD_USING
  275. return pow(four_minus_pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1.5));
  276. }
  277. template <class T>
  278. template<int N>
  279. inline T constant_exp_minus_half<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  280. {
  281. BOOST_MATH_STD_USING
  282. return exp(static_cast<T>(-0.5));
  283. }
  284. // Pi
  285. template <class T>
  286. template<int N>
  287. inline T constant_one_div_root_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  288. {
  289. return static_cast<T>(1) / root_two<T, policies::policy<policies::digits2<N> > >();
  290. }
  291. template <class T>
  292. template<int N>
  293. inline T constant_one_div_root_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  294. {
  295. return static_cast<T>(1) / root_pi<T, policies::policy<policies::digits2<N> > >();
  296. }
  297. template <class T>
  298. template<int N>
  299. inline T constant_one_div_root_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  300. {
  301. return static_cast<T>(1) / root_two_pi<T, policies::policy<policies::digits2<N> > >();
  302. }
  303. template <class T>
  304. template<int N>
  305. inline T constant_root_one_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  306. {
  307. BOOST_MATH_STD_USING
  308. return sqrt(static_cast<T>(1) / pi<T, policies::policy<policies::digits2<N> > >());
  309. }
  310. template <class T>
  311. template<int N>
  312. inline T constant_four_thirds_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  313. {
  314. BOOST_MATH_STD_USING
  315. return pi<T, policies::policy<policies::digits2<N> > >() * static_cast<T>(4) / static_cast<T>(3);
  316. }
  317. template <class T>
  318. template<int N>
  319. inline T constant_half_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  320. {
  321. BOOST_MATH_STD_USING
  322. return pi<T, policies::policy<policies::digits2<N> > >() / static_cast<T>(2);
  323. }
  324. template <class T>
  325. template<int N>
  326. inline T constant_third_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>(3);
  330. }
  331. template <class T>
  332. template<int N>
  333. inline T constant_sixth_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>(6);
  337. }
  338. template <class T>
  339. template<int N>
  340. inline T constant_two_thirds_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>(2) / static_cast<T>(3);
  344. }
  345. template <class T>
  346. template<int N>
  347. inline T constant_three_quarters_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>(3) / static_cast<T>(4);
  351. }
  352. template <class T>
  353. template<int N>
  354. inline T constant_pi_pow_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  355. {
  356. BOOST_MATH_STD_USING
  357. return pow(pi<T, policies::policy<policies::digits2<N> > >(), e<T, policies::policy<policies::digits2<N> > >()); //
  358. }
  359. template <class T>
  360. template<int N>
  361. inline T constant_pi_sqr<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> > >()
  365. * pi<T, policies::policy<policies::digits2<N> > >() ; //
  366. }
  367. template <class T>
  368. template<int N>
  369. inline T constant_pi_sqr_div_six<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  370. {
  371. BOOST_MATH_STD_USING
  372. return pi<T, policies::policy<policies::digits2<N> > >()
  373. * pi<T, policies::policy<policies::digits2<N> > >()
  374. / static_cast<T>(6); //
  375. }
  376. template <class T>
  377. template<int N>
  378. inline T constant_pi_cubed<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  379. {
  380. BOOST_MATH_STD_USING
  381. return pi<T, policies::policy<policies::digits2<N> > >()
  382. * pi<T, policies::policy<policies::digits2<N> > >()
  383. * pi<T, policies::policy<policies::digits2<N> > >()
  384. ; //
  385. }
  386. template <class T>
  387. template<int N>
  388. inline T constant_cbrt_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  389. {
  390. BOOST_MATH_STD_USING
  391. return pow(pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1)/ static_cast<T>(3));
  392. }
  393. template <class T>
  394. template<int N>
  395. inline T constant_one_div_cbrt_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  396. {
  397. BOOST_MATH_STD_USING
  398. return static_cast<T>(1)
  399. / pow(pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1)/ static_cast<T>(3));
  400. }
  401. // Euler's e
  402. template <class T>
  403. template<int N>
  404. inline T constant_e_pow_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  405. {
  406. BOOST_MATH_STD_USING
  407. return pow(e<T, policies::policy<policies::digits2<N> > >(), pi<T, policies::policy<policies::digits2<N> > >()); //
  408. }
  409. template <class T>
  410. template<int N>
  411. inline T constant_root_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  412. {
  413. BOOST_MATH_STD_USING
  414. return sqrt(e<T, policies::policy<policies::digits2<N> > >());
  415. }
  416. template <class T>
  417. template<int N>
  418. inline T constant_log10_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  419. {
  420. BOOST_MATH_STD_USING
  421. return log10(e<T, policies::policy<policies::digits2<N> > >());
  422. }
  423. template <class T>
  424. template<int N>
  425. inline T constant_one_div_log10_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  426. {
  427. BOOST_MATH_STD_USING
  428. return static_cast<T>(1) /
  429. log10(e<T, policies::policy<policies::digits2<N> > >());
  430. }
  431. // Trigonometric
  432. template <class T>
  433. template<int N>
  434. inline T constant_degree<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  435. {
  436. BOOST_MATH_STD_USING
  437. return pi<T, policies::policy<policies::digits2<N> > >()
  438. / static_cast<T>(180)
  439. ; //
  440. }
  441. template <class T>
  442. template<int N>
  443. inline T constant_radian<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  444. {
  445. BOOST_MATH_STD_USING
  446. return static_cast<T>(180)
  447. / pi<T, policies::policy<policies::digits2<N> > >()
  448. ; //
  449. }
  450. template <class T>
  451. template<int N>
  452. inline T constant_sin_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  453. {
  454. BOOST_MATH_STD_USING
  455. return sin(static_cast<T>(1)) ; //
  456. }
  457. template <class T>
  458. template<int N>
  459. inline T constant_cos_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  460. {
  461. BOOST_MATH_STD_USING
  462. return cos(static_cast<T>(1)) ; //
  463. }
  464. template <class T>
  465. template<int N>
  466. inline T constant_sinh_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  467. {
  468. BOOST_MATH_STD_USING
  469. return sinh(static_cast<T>(1)) ; //
  470. }
  471. template <class T>
  472. template<int N>
  473. inline T constant_cosh_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  474. {
  475. BOOST_MATH_STD_USING
  476. return cosh(static_cast<T>(1)) ; //
  477. }
  478. template <class T>
  479. template<int N>
  480. inline T constant_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  481. {
  482. BOOST_MATH_STD_USING
  483. return (static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) ; //
  484. }
  485. template <class T>
  486. template<int N>
  487. inline T constant_ln_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  488. {
  489. BOOST_MATH_STD_USING
  490. //return log(phi<T, policies::policy<policies::digits2<N> > >()); // ???
  491. return log((static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) );
  492. }
  493. template <class T>
  494. template<int N>
  495. inline T constant_one_div_ln_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  496. {
  497. BOOST_MATH_STD_USING
  498. return static_cast<T>(1) /
  499. log((static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) );
  500. }
  501. // Zeta
  502. template <class T>
  503. template<int N>
  504. inline T constant_zeta_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  505. {
  506. BOOST_MATH_STD_USING
  507. return pi<T, policies::policy<policies::digits2<N> > >()
  508. * pi<T, policies::policy<policies::digits2<N> > >()
  509. /static_cast<T>(6);
  510. }
  511. template <class T>
  512. template<int N>
  513. inline T constant_zeta_three<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  514. {
  515. // http://mathworld.wolfram.com/AperysConstant.html
  516. // http://en.wikipedia.org/wiki/Mathematical_constant
  517. // http://oeis.org/A002117/constant
  518. //T zeta3("1.20205690315959428539973816151144999076"
  519. // "4986292340498881792271555341838205786313"
  520. // "09018645587360933525814619915");
  521. //"1.202056903159594285399738161511449990, 76498629234049888179227155534183820578631309018645587360933525814619915" A002117
  522. // 1.202056903159594285399738161511449990, 76498629234049888179227155534183820578631309018645587360933525814619915780, +00);
  523. //"1.2020569031595942 double
  524. // http://www.spaennare.se/SSPROG/ssnum.pdf // section 11, Algorithm for Apery's constant zeta(3).
  525. // Programs to Calculate some Mathematical Constants to Large Precision, Document Version 1.50
  526. // by Stefan Spannare September 19, 2007
  527. // zeta(3) = 1/64 * sum
  528. BOOST_MATH_STD_USING
  529. T n_fact=static_cast<T>(1); // build n! for n = 0.
  530. T sum = static_cast<double>(77); // Start with n = 0 case.
  531. // for n = 0, (77/1) /64 = 1.203125
  532. //double lim = std::numeric_limits<double>::epsilon();
  533. T lim = N ? ldexp(T(1), 1 - (std::min)(N, tools::digits<T>())) : tools::epsilon<T>();
  534. for(unsigned int n = 1; n < 40; ++n)
  535. { // three to five decimal digits per term, so 40 should be plenty for 100 decimal digits.
  536. //cout << "n = " << n << endl;
  537. n_fact *= n; // n!
  538. 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
  539. T num = ((205 * n * n) + (250 * n) + 77) * n_fact_p10; // 205n^2 + 250n + 77
  540. // int nn = (2 * n + 1);
  541. // T d = factorial(nn); // inline factorial.
  542. T d = 1;
  543. for(unsigned int i = 1; i <= (n+n + 1); ++i) // (2n + 1)
  544. {
  545. d *= i;
  546. }
  547. T den = d * d * d * d * d; // [(2n+1)!]^5
  548. //cout << "den = " << den << endl;
  549. T term = num/den;
  550. if (n % 2 != 0)
  551. { //term *= -1;
  552. sum -= term;
  553. }
  554. else
  555. {
  556. sum += term;
  557. }
  558. //cout << "term = " << term << endl;
  559. //cout << "sum/64 = " << sum/64 << endl;
  560. if(abs(term) < lim)
  561. {
  562. break;
  563. }
  564. }
  565. return sum / 64;
  566. }
  567. template <class T>
  568. template<int N>
  569. inline T constant_catalan<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  570. { // http://oeis.org/A006752/constant
  571. //T c("0.915965594177219015054603514932384110774"
  572. //"149374281672134266498119621763019776254769479356512926115106248574");
  573. // 9.159655941772190150546035149323841107, 74149374281672134266498119621763019776254769479356512926115106248574422619, -01);
  574. // This is equation (entry) 31 from
  575. // http://www-2.cs.cmu.edu/~adamchik/articles/catalan/catalan.htm
  576. // See also http://www.mpfr.org/algorithms.pdf
  577. BOOST_MATH_STD_USING
  578. T k_fact = 1;
  579. T tk_fact = 1;
  580. T sum = 1;
  581. T term;
  582. T lim = N ? ldexp(T(1), 1 - (std::min)(N, tools::digits<T>())) : tools::epsilon<T>();
  583. for(unsigned k = 1;; ++k)
  584. {
  585. k_fact *= k;
  586. tk_fact *= (2 * k) * (2 * k - 1);
  587. term = k_fact * k_fact / (tk_fact * (2 * k + 1) * (2 * k + 1));
  588. sum += term;
  589. if(term < lim)
  590. {
  591. break;
  592. }
  593. }
  594. return boost::math::constants::pi<T, boost::math::policies::policy<> >()
  595. * log(2 + boost::math::constants::root_three<T, boost::math::policies::policy<> >())
  596. / 8
  597. + 3 * sum / 8;
  598. }
  599. namespace khinchin_detail{
  600. template <class T>
  601. T zeta_polynomial_series(T s, T sc, int digits)
  602. {
  603. BOOST_MATH_STD_USING
  604. //
  605. // This is algorithm 3 from:
  606. //
  607. // "An Efficient Algorithm for the Riemann Zeta Function", P. Borwein,
  608. // Canadian Mathematical Society, Conference Proceedings, 2000.
  609. // See: http://www.cecm.sfu.ca/personal/pborwein/PAPERS/P155.pdf
  610. //
  611. BOOST_MATH_STD_USING
  612. int n = (digits * 19) / 53;
  613. T sum = 0;
  614. T two_n = ldexp(T(1), n);
  615. int ej_sign = 1;
  616. for(int j = 0; j < n; ++j)
  617. {
  618. sum += ej_sign * -two_n / pow(T(j + 1), s);
  619. ej_sign = -ej_sign;
  620. }
  621. T ej_sum = 1;
  622. T ej_term = 1;
  623. for(int j = n; j <= 2 * n - 1; ++j)
  624. {
  625. sum += ej_sign * (ej_sum - two_n) / pow(T(j + 1), s);
  626. ej_sign = -ej_sign;
  627. ej_term *= 2 * n - j;
  628. ej_term /= j - n + 1;
  629. ej_sum += ej_term;
  630. }
  631. return -sum / (two_n * (1 - pow(T(2), sc)));
  632. }
  633. template <class T>
  634. T khinchin(int digits)
  635. {
  636. BOOST_MATH_STD_USING
  637. T sum = 0;
  638. T term;
  639. T lim = ldexp(T(1), 1-digits);
  640. T factor = 0;
  641. unsigned last_k = 1;
  642. T num = 1;
  643. for(unsigned n = 1;; ++n)
  644. {
  645. for(unsigned k = last_k; k <= 2 * n - 1; ++k)
  646. {
  647. factor += num / k;
  648. num = -num;
  649. }
  650. last_k = 2 * n;
  651. term = (zeta_polynomial_series(T(2 * n), T(1 - T(2 * n)), digits) - 1) * factor / n;
  652. sum += term;
  653. if(term < lim)
  654. break;
  655. }
  656. return exp(sum / boost::math::constants::ln_two<T, boost::math::policies::policy<> >());
  657. }
  658. }
  659. template <class T>
  660. template<int N>
  661. inline T constant_khinchin<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  662. {
  663. int n = N ? (std::min)(N, tools::digits<T>()) : tools::digits<T>();
  664. return khinchin_detail::khinchin<T>(n);
  665. }
  666. template <class T>
  667. template<int N>
  668. inline T constant_extreme_value_skewness<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  669. { // from e_float constants.cpp
  670. // Mathematica: N[12 Sqrt[6] Zeta[3]/Pi^3, 1101]
  671. BOOST_MATH_STD_USING
  672. T ev(12 * sqrt(static_cast<T>(6)) * zeta_three<T, policies::policy<policies::digits2<N> > >()
  673. / pi_cubed<T, policies::policy<policies::digits2<N> > >() );
  674. //T ev(
  675. //"1.1395470994046486574927930193898461120875997958365518247216557100852480077060706857071875468869385150"
  676. //"1894272048688553376986765366075828644841024041679714157616857834895702411080704529137366329462558680"
  677. //"2015498788776135705587959418756809080074611906006528647805347822929577145038743873949415294942796280"
  678. //"0895597703063466053535550338267721294164578901640163603544404938283861127819804918174973533694090594"
  679. //"3094963822672055237678432023017824416203652657301470473548274848068762500300316769691474974950757965"
  680. //"8640779777748741897542093874605477776538884083378029488863880220988107155275203245233994097178778984"
  681. //"3488995668362387892097897322246698071290011857605809901090220903955815127463328974447572119951192970"
  682. //"3684453635456559086126406960279692862247058250100678008419431185138019869693206366891639436908462809"
  683. //"9756051372711251054914491837034685476095423926553367264355374652153595857163724698198860485357368964"
  684. //"3807049634423621246870868566707915720704996296083373077647528285782964567312903914752617978405994377"
  685. //"9064157147206717895272199736902453130842229559980076472936976287378945035706933650987259357729800315");
  686. return ev;
  687. }
  688. namespace detail{
  689. //
  690. // Calculation of the Glaisher constant depends upon calculating the
  691. // derivative of the zeta function at 2, we can then use the relation:
  692. // zeta'(2) = 1/6 pi^2 [euler + ln(2pi)-12ln(A)]
  693. // To get the constant A.
  694. // See equation 45 at http://mathworld.wolfram.com/RiemannZetaFunction.html.
  695. //
  696. // The derivative of the zeta function is computed by direct differentiation
  697. // of the relation:
  698. // (1-2^(1-s))zeta(s) = SUM(n=0, INF){ (-n)^n / (n+1)^s }
  699. // Which gives us 2 slowly converging but alternating sums to compute,
  700. // for this we use Algorithm 1 from "Convergent Acceleration of Alternating Series",
  701. // Henri Cohen, Fernando Rodriguez Villegas and Don Zagier, Experimental Mathematics 9:1 (1999).
  702. // See http://www.math.utexas.edu/users/villegas/publications/conv-accel.pdf
  703. //
  704. template <class T>
  705. T zeta_series_derivative_2(unsigned digits)
  706. {
  707. // Derivative of the series part, evaluated at 2:
  708. BOOST_MATH_STD_USING
  709. int n = digits * 301 * 13 / 10000;
  710. boost::math::itrunc((std::numeric_limits<T>::digits10 + 1) * 1.3);
  711. T d = pow(3 + sqrt(T(8)), n);
  712. d = (d + 1 / d) / 2;
  713. T b = -1;
  714. T c = -d;
  715. T s = 0;
  716. for(int k = 0; k < n; ++k)
  717. {
  718. T a = -log(T(k+1)) / ((k+1) * (k+1));
  719. c = b - c;
  720. s = s + c * a;
  721. b = (k + n) * (k - n) * b / ((k + T(0.5f)) * (k + 1));
  722. }
  723. return s / d;
  724. }
  725. template <class T>
  726. T zeta_series_2(unsigned digits)
  727. {
  728. // Series part of zeta at 2:
  729. BOOST_MATH_STD_USING
  730. int n = digits * 301 * 13 / 10000;
  731. T d = pow(3 + sqrt(T(8)), n);
  732. d = (d + 1 / d) / 2;
  733. T b = -1;
  734. T c = -d;
  735. T s = 0;
  736. for(int k = 0; k < n; ++k)
  737. {
  738. T a = T(1) / ((k + 1) * (k + 1));
  739. c = b - c;
  740. s = s + c * a;
  741. b = (k + n) * (k - n) * b / ((k + T(0.5f)) * (k + 1));
  742. }
  743. return s / d;
  744. }
  745. template <class T>
  746. inline T zeta_series_lead_2()
  747. {
  748. // lead part at 2:
  749. return 2;
  750. }
  751. template <class T>
  752. inline T zeta_series_derivative_lead_2()
  753. {
  754. // derivative of lead part at 2:
  755. return -2 * boost::math::constants::ln_two<T>();
  756. }
  757. template <class T>
  758. inline T zeta_derivative_2(unsigned n)
  759. {
  760. // zeta derivative at 2:
  761. return zeta_series_derivative_2<T>(n) * zeta_series_lead_2<T>()
  762. + zeta_series_derivative_lead_2<T>() * zeta_series_2<T>(n);
  763. }
  764. } // namespace detail
  765. template <class T>
  766. template<int N>
  767. inline T constant_glaisher<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  768. {
  769. BOOST_MATH_STD_USING
  770. typedef policies::policy<policies::digits2<N> > forwarding_policy;
  771. int n = N ? (std::min)(N, tools::digits<T>()) : tools::digits<T>();
  772. T v = detail::zeta_derivative_2<T>(n);
  773. v *= 6;
  774. v /= boost::math::constants::pi<T, forwarding_policy>() * boost::math::constants::pi<T, forwarding_policy>();
  775. v -= boost::math::constants::euler<T, forwarding_policy>();
  776. v -= log(2 * boost::math::constants::pi<T, forwarding_policy>());
  777. v /= -12;
  778. return exp(v);
  779. /*
  780. // from http://mpmath.googlecode.com/svn/data/glaisher.txt
  781. // 20,000 digits of the Glaisher-Kinkelin constant A = exp(1/2 - zeta'(-1))
  782. // Computed using A = exp((6 (-zeta'(2))/pi^2 + log 2 pi + gamma)/12)
  783. // with Euler-Maclaurin summation for zeta'(2).
  784. T g(
  785. "1.282427129100622636875342568869791727767688927325001192063740021740406308858826"
  786. "46112973649195820237439420646120399000748933157791362775280404159072573861727522"
  787. "14334327143439787335067915257366856907876561146686449997784962754518174312394652"
  788. "76128213808180219264516851546143919901083573730703504903888123418813674978133050"
  789. "93770833682222494115874837348064399978830070125567001286994157705432053927585405"
  790. "81731588155481762970384743250467775147374600031616023046613296342991558095879293"
  791. "36343887288701988953460725233184702489001091776941712153569193674967261270398013"
  792. "52652668868978218897401729375840750167472114895288815996668743164513890306962645"
  793. "59870469543740253099606800842447417554061490189444139386196089129682173528798629"
  794. "88434220366989900606980888785849587494085307347117090132667567503310523405221054"
  795. "14176776156308191919997185237047761312315374135304725819814797451761027540834943"
  796. "14384965234139453373065832325673954957601692256427736926358821692159870775858274"
  797. "69575162841550648585890834128227556209547002918593263079373376942077522290940187");
  798. return g;
  799. */
  800. }
  801. template <class T>
  802. template<int N>
  803. inline T constant_rayleigh_skewness<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  804. { // From e_float
  805. // 1100 digits of the Rayleigh distribution skewness
  806. // Mathematica: N[2 Sqrt[Pi] (Pi - 3)/((4 - Pi)^(3/2)), 1100]
  807. BOOST_MATH_STD_USING
  808. T rs(2 * root_pi<T, policies::policy<policies::digits2<N> > >()
  809. * pi_minus_three<T, policies::policy<policies::digits2<N> > >()
  810. / pow(four_minus_pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(3./2))
  811. );
  812. // 6.31110657818937138191899351544227779844042203134719497658094585692926819617473725459905027032537306794400047264,
  813. //"0.6311106578189371381918993515442277798440422031347194976580945856929268196174737254599050270325373067"
  814. //"9440004726436754739597525250317640394102954301685809920213808351450851396781817932734836994829371322"
  815. //"5797376021347531983451654130317032832308462278373358624120822253764532674177325950686466133508511968"
  816. //"2389168716630349407238090652663422922072397393006683401992961569208109477307776249225072042971818671"
  817. //"4058887072693437217879039875871765635655476241624825389439481561152126886932506682176611183750503553"
  818. //"1218982627032068396407180216351425758181396562859085306247387212297187006230007438534686340210168288"
  819. //"8956816965453815849613622117088096547521391672977226658826566757207615552041767516828171274858145957"
  820. //"6137539156656005855905288420585194082284972984285863898582313048515484073396332610565441264220790791"
  821. //"0194897267890422924599776483890102027823328602965235306539844007677157873140562950510028206251529523"
  822. //"7428049693650605954398446899724157486062545281504433364675815915402937209673727753199567661561209251"
  823. //"4695589950526053470201635372590001578503476490223746511106018091907936826431407434894024396366284848"); ;
  824. return rs;
  825. }
  826. template <class T>
  827. template<int N>
  828. inline T constant_rayleigh_kurtosis_excess<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  829. { // - (6 Pi^2 - 24 Pi + 16)/((Pi - 4)^2)
  830. // Might provide and calculate this using pi_minus_four.
  831. BOOST_MATH_STD_USING
  832. return - (((static_cast<T>(6) * pi<T, policies::policy<policies::digits2<N> > >()
  833. * pi<T, policies::policy<policies::digits2<N> > >())
  834. - (static_cast<T>(24) * pi<T, policies::policy<policies::digits2<N> > >()) + static_cast<T>(16) )
  835. /
  836. ((pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4))
  837. * (pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4)))
  838. );
  839. }
  840. template <class T>
  841. template<int N>
  842. inline T constant_rayleigh_kurtosis<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpl::int_<N>))
  843. { // 3 - (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>(3) - (((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. }}}} // namespaces
  855. #endif // BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED