mpfr.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. // Copyright John Maddock 2008.
  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. //
  6. // Wrapper that works with mpfr_class defined in gmpfrxx.h
  7. // See http://math.berkeley.edu/~wilken/code/gmpfrxx/
  8. // Also requires the gmp and mpfr libraries.
  9. //
  10. #ifndef BOOST_MATH_MPLFR_BINDINGS_HPP
  11. #define BOOST_MATH_MPLFR_BINDINGS_HPP
  12. #include <boost/config.hpp>
  13. #include <boost/lexical_cast.hpp>
  14. #ifdef BOOST_MSVC
  15. //
  16. // We get a lot of warnings from the gmp, mpfr and gmpfrxx headers,
  17. // disable them here, so we only see warnings from *our* code:
  18. //
  19. #pragma warning(push)
  20. #pragma warning(disable: 4127 4800 4512)
  21. #endif
  22. #include <gmpfrxx.h>
  23. #ifdef BOOST_MSVC
  24. #pragma warning(pop)
  25. #endif
  26. #include <boost/math/tools/precision.hpp>
  27. #include <boost/math/tools/real_cast.hpp>
  28. #include <boost/math/policies/policy.hpp>
  29. #include <boost/math/distributions/fwd.hpp>
  30. #include <boost/math/special_functions/math_fwd.hpp>
  31. #include <boost/math/bindings/detail/big_digamma.hpp>
  32. #include <boost/math/bindings/detail/big_lanczos.hpp>
  33. inline mpfr_class fabs(const mpfr_class& v)
  34. {
  35. return abs(v);
  36. }
  37. template <class T, class U>
  38. inline mpfr_class fabs(const __gmp_expr<T,U>& v)
  39. {
  40. return abs(static_cast<mpfr_class>(v));
  41. }
  42. inline mpfr_class pow(const mpfr_class& b, const mpfr_class& e)
  43. {
  44. mpfr_class result;
  45. mpfr_pow(result.__get_mp(), b.__get_mp(), e.__get_mp(), GMP_RNDN);
  46. return result;
  47. }
  48. /*
  49. template <class T, class U, class V, class W>
  50. inline mpfr_class pow(const __gmp_expr<T,U>& b, const __gmp_expr<V,W>& e)
  51. {
  52. return pow(static_cast<mpfr_class>(b), static_cast<mpfr_class>(e));
  53. }
  54. */
  55. inline mpfr_class ldexp(const mpfr_class& v, int e)
  56. {
  57. //int e = mpfr_get_exp(*v.__get_mp());
  58. mpfr_class result(v);
  59. mpfr_set_exp(result.__get_mp(), e);
  60. return result;
  61. }
  62. template <class T, class U>
  63. inline mpfr_class ldexp(const __gmp_expr<T,U>& v, int e)
  64. {
  65. return ldexp(static_cast<mpfr_class>(v), e);
  66. }
  67. inline mpfr_class frexp(const mpfr_class& v, int* expon)
  68. {
  69. int e = mpfr_get_exp(v.__get_mp());
  70. mpfr_class result(v);
  71. mpfr_set_exp(result.__get_mp(), 0);
  72. *expon = e;
  73. return result;
  74. }
  75. template <class T, class U>
  76. inline mpfr_class frexp(const __gmp_expr<T,U>& v, int* expon)
  77. {
  78. return frexp(static_cast<mpfr_class>(v), expon);
  79. }
  80. inline mpfr_class fmod(const mpfr_class& v1, const mpfr_class& v2)
  81. {
  82. mpfr_class n;
  83. if(v1 < 0)
  84. n = ceil(v1 / v2);
  85. else
  86. n = floor(v1 / v2);
  87. return v1 - n * v2;
  88. }
  89. template <class T, class U, class V, class W>
  90. inline mpfr_class fmod(const __gmp_expr<T,U>& v1, const __gmp_expr<V,W>& v2)
  91. {
  92. return fmod(static_cast<mpfr_class>(v1), static_cast<mpfr_class>(v2));
  93. }
  94. template <class Policy>
  95. inline mpfr_class modf(const mpfr_class& v, long long* ipart, const Policy& pol)
  96. {
  97. *ipart = lltrunc(v, pol);
  98. return v - boost::math::tools::real_cast<mpfr_class>(*ipart);
  99. }
  100. template <class T, class U, class Policy>
  101. inline mpfr_class modf(const __gmp_expr<T,U>& v, long long* ipart, const Policy& pol)
  102. {
  103. return modf(static_cast<mpfr_class>(v), ipart, pol);
  104. }
  105. template <class Policy>
  106. inline int iround(mpfr_class const& x, const Policy&)
  107. {
  108. return boost::math::tools::real_cast<int>(boost::math::round(x, typename boost::math::policies::normalise<Policy, boost::math::policies::rounding_error< boost::math::policies::throw_on_error> >::type()));
  109. }
  110. template <class T, class U, class Policy>
  111. inline int iround(__gmp_expr<T,U> const& x, const Policy& pol)
  112. {
  113. return iround(static_cast<mpfr_class>(x), pol);
  114. }
  115. template <class Policy>
  116. inline long lround(mpfr_class const& x, const Policy&)
  117. {
  118. return boost::math::tools::real_cast<long>(boost::math::round(x, typename boost::math::policies::normalise<Policy, boost::math::policies::rounding_error< boost::math::policies::throw_on_error> >::type()));
  119. }
  120. template <class T, class U, class Policy>
  121. inline long lround(__gmp_expr<T,U> const& x, const Policy& pol)
  122. {
  123. return lround(static_cast<mpfr_class>(x), pol);
  124. }
  125. template <class Policy>
  126. inline long long llround(mpfr_class const& x, const Policy&)
  127. {
  128. return boost::math::tools::real_cast<long long>(boost::math::round(x, typename boost::math::policies::normalise<Policy, boost::math::policies::rounding_error< boost::math::policies::throw_on_error> >::type()));
  129. }
  130. template <class T, class U, class Policy>
  131. inline long long llround(__gmp_expr<T,U> const& x, const Policy& pol)
  132. {
  133. return llround(static_cast<mpfr_class>(x), pol);
  134. }
  135. template <class Policy>
  136. inline int itrunc(mpfr_class const& x, const Policy&)
  137. {
  138. return boost::math::tools::real_cast<int>(boost::math::trunc(x, typename boost::math::policies::normalise<Policy, boost::math::policies::rounding_error< boost::math::policies::throw_on_error> >::type()));
  139. }
  140. template <class T, class U, class Policy>
  141. inline int itrunc(__gmp_expr<T,U> const& x, const Policy& pol)
  142. {
  143. return itrunc(static_cast<mpfr_class>(x), pol);
  144. }
  145. template <class Policy>
  146. inline long ltrunc(mpfr_class const& x, const Policy&)
  147. {
  148. return boost::math::tools::real_cast<long>(boost::math::trunc(x, typename boost::math::policies::normalise<Policy, boost::math::policies::rounding_error< boost::math::policies::throw_on_error> >::type()));
  149. }
  150. template <class T, class U, class Policy>
  151. inline long ltrunc(__gmp_expr<T,U> const& x, const Policy& pol)
  152. {
  153. return ltrunc(static_cast<mpfr_class>(x), pol);
  154. }
  155. template <class Policy>
  156. inline long long lltrunc(mpfr_class const& x, const Policy&)
  157. {
  158. return boost::math::tools::real_cast<long long>(boost::math::trunc(x, typename boost::math::policies::normalise<Policy, boost::math::policies::rounding_error< boost::math::policies::throw_on_error> >::type()));
  159. }
  160. template <class T, class U, class Policy>
  161. inline long long lltrunc(__gmp_expr<T,U> const& x, const Policy& pol)
  162. {
  163. return lltrunc(static_cast<mpfr_class>(x), pol);
  164. }
  165. namespace boost{ namespace math{
  166. #if defined(__GNUC__) && (__GNUC__ < 4)
  167. using ::iround;
  168. using ::lround;
  169. using ::llround;
  170. using ::itrunc;
  171. using ::ltrunc;
  172. using ::lltrunc;
  173. using ::modf;
  174. #endif
  175. namespace lanczos{
  176. struct mpfr_lanczos
  177. {
  178. static mpfr_class lanczos_sum(const mpfr_class& z)
  179. {
  180. unsigned long p = z.get_dprec();
  181. if(p <= 72)
  182. return lanczos13UDT::lanczos_sum(z);
  183. else if(p <= 120)
  184. return lanczos22UDT::lanczos_sum(z);
  185. else if(p <= 170)
  186. return lanczos31UDT::lanczos_sum(z);
  187. else //if(p <= 370) approx 100 digit precision:
  188. return lanczos61UDT::lanczos_sum(z);
  189. }
  190. static mpfr_class lanczos_sum_expG_scaled(const mpfr_class& z)
  191. {
  192. unsigned long p = z.get_dprec();
  193. if(p <= 72)
  194. return lanczos13UDT::lanczos_sum_expG_scaled(z);
  195. else if(p <= 120)
  196. return lanczos22UDT::lanczos_sum_expG_scaled(z);
  197. else if(p <= 170)
  198. return lanczos31UDT::lanczos_sum_expG_scaled(z);
  199. else //if(p <= 370) approx 100 digit precision:
  200. return lanczos61UDT::lanczos_sum_expG_scaled(z);
  201. }
  202. static mpfr_class lanczos_sum_near_1(const mpfr_class& z)
  203. {
  204. unsigned long p = z.get_dprec();
  205. if(p <= 72)
  206. return lanczos13UDT::lanczos_sum_near_1(z);
  207. else if(p <= 120)
  208. return lanczos22UDT::lanczos_sum_near_1(z);
  209. else if(p <= 170)
  210. return lanczos31UDT::lanczos_sum_near_1(z);
  211. else //if(p <= 370) approx 100 digit precision:
  212. return lanczos61UDT::lanczos_sum_near_1(z);
  213. }
  214. static mpfr_class lanczos_sum_near_2(const mpfr_class& z)
  215. {
  216. unsigned long p = z.get_dprec();
  217. if(p <= 72)
  218. return lanczos13UDT::lanczos_sum_near_2(z);
  219. else if(p <= 120)
  220. return lanczos22UDT::lanczos_sum_near_2(z);
  221. else if(p <= 170)
  222. return lanczos31UDT::lanczos_sum_near_2(z);
  223. else //if(p <= 370) approx 100 digit precision:
  224. return lanczos61UDT::lanczos_sum_near_2(z);
  225. }
  226. static mpfr_class g()
  227. {
  228. unsigned long p = mpfr_class::get_dprec();
  229. if(p <= 72)
  230. return lanczos13UDT::g();
  231. else if(p <= 120)
  232. return lanczos22UDT::g();
  233. else if(p <= 170)
  234. return lanczos31UDT::g();
  235. else //if(p <= 370) approx 100 digit precision:
  236. return lanczos61UDT::g();
  237. }
  238. };
  239. template<class Policy>
  240. struct lanczos<mpfr_class, Policy>
  241. {
  242. typedef mpfr_lanczos type;
  243. };
  244. } // namespace lanczos
  245. namespace constants{
  246. template <class Real, class Policy>
  247. struct construction_traits;
  248. template <class Policy>
  249. struct construction_traits<mpfr_class, Policy>
  250. {
  251. typedef mpl::int_<0> type;
  252. };
  253. }
  254. namespace tools
  255. {
  256. template <class T, class U>
  257. struct promote_arg<__gmp_expr<T,U> >
  258. { // If T is integral type, then promote to double.
  259. typedef mpfr_class type;
  260. };
  261. template<>
  262. inline int digits<mpfr_class>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpfr_class))
  263. {
  264. return mpfr_class::get_dprec();
  265. }
  266. namespace detail{
  267. template<class I>
  268. void convert_to_long_result(mpfr_class const& r, I& result)
  269. {
  270. result = 0;
  271. I last_result(0);
  272. mpfr_class t(r);
  273. double term;
  274. do
  275. {
  276. term = real_cast<double>(t);
  277. last_result = result;
  278. result += static_cast<I>(term);
  279. t -= term;
  280. }while(result != last_result);
  281. }
  282. }
  283. template <>
  284. inline mpfr_class real_cast<mpfr_class, long long>(long long t)
  285. {
  286. mpfr_class result;
  287. int expon = 0;
  288. int sign = 1;
  289. if(t < 0)
  290. {
  291. sign = -1;
  292. t = -t;
  293. }
  294. while(t)
  295. {
  296. result += ldexp((double)(t & 0xffffL), expon);
  297. expon += 32;
  298. t >>= 32;
  299. }
  300. return result * sign;
  301. }
  302. template <>
  303. inline unsigned real_cast<unsigned, mpfr_class>(mpfr_class t)
  304. {
  305. return t.get_ui();
  306. }
  307. template <>
  308. inline int real_cast<int, mpfr_class>(mpfr_class t)
  309. {
  310. return t.get_si();
  311. }
  312. template <>
  313. inline double real_cast<double, mpfr_class>(mpfr_class t)
  314. {
  315. return t.get_d();
  316. }
  317. template <>
  318. inline float real_cast<float, mpfr_class>(mpfr_class t)
  319. {
  320. return static_cast<float>(t.get_d());
  321. }
  322. template <>
  323. inline long real_cast<long, mpfr_class>(mpfr_class t)
  324. {
  325. long result;
  326. detail::convert_to_long_result(t, result);
  327. return result;
  328. }
  329. template <>
  330. inline long long real_cast<long long, mpfr_class>(mpfr_class t)
  331. {
  332. long long result;
  333. detail::convert_to_long_result(t, result);
  334. return result;
  335. }
  336. template <>
  337. inline mpfr_class max_value<mpfr_class>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpfr_class))
  338. {
  339. static bool has_init = false;
  340. static mpfr_class val;
  341. if(!has_init)
  342. {
  343. val = 0.5;
  344. mpfr_set_exp(val.__get_mp(), mpfr_get_emax());
  345. has_init = true;
  346. }
  347. return val;
  348. }
  349. template <>
  350. inline mpfr_class min_value<mpfr_class>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpfr_class))
  351. {
  352. static bool has_init = false;
  353. static mpfr_class val;
  354. if(!has_init)
  355. {
  356. val = 0.5;
  357. mpfr_set_exp(val.__get_mp(), mpfr_get_emin());
  358. has_init = true;
  359. }
  360. return val;
  361. }
  362. template <>
  363. inline mpfr_class log_max_value<mpfr_class>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpfr_class))
  364. {
  365. static bool has_init = false;
  366. static mpfr_class val = max_value<mpfr_class>();
  367. if(!has_init)
  368. {
  369. val = log(val);
  370. has_init = true;
  371. }
  372. return val;
  373. }
  374. template <>
  375. inline mpfr_class log_min_value<mpfr_class>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpfr_class))
  376. {
  377. static bool has_init = false;
  378. static mpfr_class val = max_value<mpfr_class>();
  379. if(!has_init)
  380. {
  381. val = log(val);
  382. has_init = true;
  383. }
  384. return val;
  385. }
  386. template <>
  387. inline mpfr_class epsilon<mpfr_class>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpfr_class))
  388. {
  389. return ldexp(mpfr_class(1), 1-boost::math::policies::digits<mpfr_class, boost::math::policies::policy<> >());
  390. }
  391. } // namespace tools
  392. namespace policies{
  393. template <class T, class U, class Policy>
  394. struct evaluation<__gmp_expr<T, U>, Policy>
  395. {
  396. typedef mpfr_class type;
  397. };
  398. }
  399. template <class Policy>
  400. inline mpfr_class skewness(const extreme_value_distribution<mpfr_class, Policy>& /*dist*/)
  401. {
  402. //
  403. // This is 12 * sqrt(6) * zeta(3) / pi^3:
  404. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  405. //
  406. return boost::lexical_cast<mpfr_class>("1.1395470994046486574927930193898461120875997958366");
  407. }
  408. template <class Policy>
  409. inline mpfr_class skewness(const rayleigh_distribution<mpfr_class, Policy>& /*dist*/)
  410. {
  411. // using namespace boost::math::constants;
  412. return boost::lexical_cast<mpfr_class>("0.63111065781893713819189935154422777984404221106391");
  413. // Computed using NTL at 150 bit, about 50 decimal digits.
  414. // return 2 * root_pi<RealType>() * pi_minus_three<RealType>() / pow23_four_minus_pi<RealType>();
  415. }
  416. template <class Policy>
  417. inline mpfr_class kurtosis(const rayleigh_distribution<mpfr_class, Policy>& /*dist*/)
  418. {
  419. // using namespace boost::math::constants;
  420. return boost::lexical_cast<mpfr_class>("3.2450893006876380628486604106197544154170667057995");
  421. // Computed using NTL at 150 bit, about 50 decimal digits.
  422. // return 3 - (6 * pi<RealType>() * pi<RealType>() - 24 * pi<RealType>() + 16) /
  423. // (four_minus_pi<RealType>() * four_minus_pi<RealType>());
  424. }
  425. template <class Policy>
  426. inline mpfr_class kurtosis_excess(const rayleigh_distribution<mpfr_class, Policy>& /*dist*/)
  427. {
  428. //using namespace boost::math::constants;
  429. // Computed using NTL at 150 bit, about 50 decimal digits.
  430. return boost::lexical_cast<mpfr_class>("0.2450893006876380628486604106197544154170667057995");
  431. // return -(6 * pi<RealType>() * pi<RealType>() - 24 * pi<RealType>() + 16) /
  432. // (four_minus_pi<RealType>() * four_minus_pi<RealType>());
  433. } // kurtosis
  434. namespace detail{
  435. //
  436. // Version of Digamma accurate to ~100 decimal digits.
  437. //
  438. template <class Policy>
  439. mpfr_class digamma_imp(mpfr_class x, const mpl::int_<0>* , const Policy& pol)
  440. {
  441. //
  442. // This handles reflection of negative arguments, and all our
  443. // empfr_classor handling, then forwards to the T-specific approximation.
  444. //
  445. BOOST_MATH_STD_USING // ADL of std functions.
  446. mpfr_class result = 0;
  447. //
  448. // Check for negative arguments and use reflection:
  449. //
  450. if(x < 0)
  451. {
  452. // Reflect:
  453. x = 1 - x;
  454. // Argument reduction for tan:
  455. mpfr_class remainder = x - floor(x);
  456. // Shift to negative if > 0.5:
  457. if(remainder > 0.5)
  458. {
  459. remainder -= 1;
  460. }
  461. //
  462. // check for evaluation at a negative pole:
  463. //
  464. if(remainder == 0)
  465. {
  466. return policies::raise_pole_error<mpfr_class>("boost::math::digamma<%1%>(%1%)", 0, (1-x), pol);
  467. }
  468. result = constants::pi<mpfr_class>() / tan(constants::pi<mpfr_class>() * remainder);
  469. }
  470. result += big_digamma(x);
  471. return result;
  472. }
  473. //
  474. // Specialisations of this function provides the initial
  475. // starting guess for Halley iteration:
  476. //
  477. template <class Policy>
  478. inline mpfr_class erf_inv_imp(const mpfr_class& p, const mpfr_class& q, const Policy&, const boost::mpl::int_<64>*)
  479. {
  480. BOOST_MATH_STD_USING // for ADL of std names.
  481. mpfr_class result = 0;
  482. if(p <= 0.5)
  483. {
  484. //
  485. // Evaluate inverse erf using the rational approximation:
  486. //
  487. // x = p(p+10)(Y+R(p))
  488. //
  489. // Where Y is a constant, and R(p) is optimised for a low
  490. // absolute empfr_classor compared to |Y|.
  491. //
  492. // double: Max empfr_classor found: 2.001849e-18
  493. // long double: Max empfr_classor found: 1.017064e-20
  494. // Maximum Deviation Found (actual empfr_classor term at infinite precision) 8.030e-21
  495. //
  496. static const float Y = 0.0891314744949340820313f;
  497. static const mpfr_class P[] = {
  498. -0.000508781949658280665617,
  499. -0.00836874819741736770379,
  500. 0.0334806625409744615033,
  501. -0.0126926147662974029034,
  502. -0.0365637971411762664006,
  503. 0.0219878681111168899165,
  504. 0.00822687874676915743155,
  505. -0.00538772965071242932965
  506. };
  507. static const mpfr_class Q[] = {
  508. 1,
  509. -0.970005043303290640362,
  510. -1.56574558234175846809,
  511. 1.56221558398423026363,
  512. 0.662328840472002992063,
  513. -0.71228902341542847553,
  514. -0.0527396382340099713954,
  515. 0.0795283687341571680018,
  516. -0.00233393759374190016776,
  517. 0.000886216390456424707504
  518. };
  519. mpfr_class g = p * (p + 10);
  520. mpfr_class r = tools::evaluate_polynomial(P, p) / tools::evaluate_polynomial(Q, p);
  521. result = g * Y + g * r;
  522. }
  523. else if(q >= 0.25)
  524. {
  525. //
  526. // Rational approximation for 0.5 > q >= 0.25
  527. //
  528. // x = sqrt(-2*log(q)) / (Y + R(q))
  529. //
  530. // Where Y is a constant, and R(q) is optimised for a low
  531. // absolute empfr_classor compared to Y.
  532. //
  533. // double : Max empfr_classor found: 7.403372e-17
  534. // long double : Max empfr_classor found: 6.084616e-20
  535. // Maximum Deviation Found (empfr_classor term) 4.811e-20
  536. //
  537. static const float Y = 2.249481201171875f;
  538. static const mpfr_class P[] = {
  539. -0.202433508355938759655,
  540. 0.105264680699391713268,
  541. 8.37050328343119927838,
  542. 17.6447298408374015486,
  543. -18.8510648058714251895,
  544. -44.6382324441786960818,
  545. 17.445385985570866523,
  546. 21.1294655448340526258,
  547. -3.67192254707729348546
  548. };
  549. static const mpfr_class Q[] = {
  550. 1,
  551. 6.24264124854247537712,
  552. 3.9713437953343869095,
  553. -28.6608180499800029974,
  554. -20.1432634680485188801,
  555. 48.5609213108739935468,
  556. 10.8268667355460159008,
  557. -22.6436933413139721736,
  558. 1.72114765761200282724
  559. };
  560. mpfr_class g = sqrt(-2 * log(q));
  561. mpfr_class xs = q - 0.25;
  562. mpfr_class r = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  563. result = g / (Y + r);
  564. }
  565. else
  566. {
  567. //
  568. // For q < 0.25 we have a series of rational approximations all
  569. // of the general form:
  570. //
  571. // let: x = sqrt(-log(q))
  572. //
  573. // Then the result is given by:
  574. //
  575. // x(Y+R(x-B))
  576. //
  577. // where Y is a constant, B is the lowest value of x for which
  578. // the approximation is valid, and R(x-B) is optimised for a low
  579. // absolute empfr_classor compared to Y.
  580. //
  581. // Note that almost all code will really go through the first
  582. // or maybe second approximation. After than we're dealing with very
  583. // small input values indeed: 80 and 128 bit long double's go all the
  584. // way down to ~ 1e-5000 so the "tail" is rather long...
  585. //
  586. mpfr_class x = sqrt(-log(q));
  587. if(x < 3)
  588. {
  589. // Max empfr_classor found: 1.089051e-20
  590. static const float Y = 0.807220458984375f;
  591. static const mpfr_class P[] = {
  592. -0.131102781679951906451,
  593. -0.163794047193317060787,
  594. 0.117030156341995252019,
  595. 0.387079738972604337464,
  596. 0.337785538912035898924,
  597. 0.142869534408157156766,
  598. 0.0290157910005329060432,
  599. 0.00214558995388805277169,
  600. -0.679465575181126350155e-6,
  601. 0.285225331782217055858e-7,
  602. -0.681149956853776992068e-9
  603. };
  604. static const mpfr_class Q[] = {
  605. 1,
  606. 3.46625407242567245975,
  607. 5.38168345707006855425,
  608. 4.77846592945843778382,
  609. 2.59301921623620271374,
  610. 0.848854343457902036425,
  611. 0.152264338295331783612,
  612. 0.01105924229346489121
  613. };
  614. mpfr_class xs = x - 1.125;
  615. mpfr_class R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  616. result = Y * x + R * x;
  617. }
  618. else if(x < 6)
  619. {
  620. // Max empfr_classor found: 8.389174e-21
  621. static const float Y = 0.93995571136474609375f;
  622. static const mpfr_class P[] = {
  623. -0.0350353787183177984712,
  624. -0.00222426529213447927281,
  625. 0.0185573306514231072324,
  626. 0.00950804701325919603619,
  627. 0.00187123492819559223345,
  628. 0.000157544617424960554631,
  629. 0.460469890584317994083e-5,
  630. -0.230404776911882601748e-9,
  631. 0.266339227425782031962e-11
  632. };
  633. static const mpfr_class Q[] = {
  634. 1,
  635. 1.3653349817554063097,
  636. 0.762059164553623404043,
  637. 0.220091105764131249824,
  638. 0.0341589143670947727934,
  639. 0.00263861676657015992959,
  640. 0.764675292302794483503e-4
  641. };
  642. mpfr_class xs = x - 3;
  643. mpfr_class R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  644. result = Y * x + R * x;
  645. }
  646. else if(x < 18)
  647. {
  648. // Max empfr_classor found: 1.481312e-19
  649. static const float Y = 0.98362827301025390625f;
  650. static const mpfr_class P[] = {
  651. -0.0167431005076633737133,
  652. -0.00112951438745580278863,
  653. 0.00105628862152492910091,
  654. 0.000209386317487588078668,
  655. 0.149624783758342370182e-4,
  656. 0.449696789927706453732e-6,
  657. 0.462596163522878599135e-8,
  658. -0.281128735628831791805e-13,
  659. 0.99055709973310326855e-16
  660. };
  661. static const mpfr_class Q[] = {
  662. 1,
  663. 0.591429344886417493481,
  664. 0.138151865749083321638,
  665. 0.0160746087093676504695,
  666. 0.000964011807005165528527,
  667. 0.275335474764726041141e-4,
  668. 0.282243172016108031869e-6
  669. };
  670. mpfr_class xs = x - 6;
  671. mpfr_class R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  672. result = Y * x + R * x;
  673. }
  674. else if(x < 44)
  675. {
  676. // Max empfr_classor found: 5.697761e-20
  677. static const float Y = 0.99714565277099609375f;
  678. static const mpfr_class P[] = {
  679. -0.0024978212791898131227,
  680. -0.779190719229053954292e-5,
  681. 0.254723037413027451751e-4,
  682. 0.162397777342510920873e-5,
  683. 0.396341011304801168516e-7,
  684. 0.411632831190944208473e-9,
  685. 0.145596286718675035587e-11,
  686. -0.116765012397184275695e-17
  687. };
  688. static const mpfr_class Q[] = {
  689. 1,
  690. 0.207123112214422517181,
  691. 0.0169410838120975906478,
  692. 0.000690538265622684595676,
  693. 0.145007359818232637924e-4,
  694. 0.144437756628144157666e-6,
  695. 0.509761276599778486139e-9
  696. };
  697. mpfr_class xs = x - 18;
  698. mpfr_class R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  699. result = Y * x + R * x;
  700. }
  701. else
  702. {
  703. // Max empfr_classor found: 1.279746e-20
  704. static const float Y = 0.99941349029541015625f;
  705. static const mpfr_class P[] = {
  706. -0.000539042911019078575891,
  707. -0.28398759004727721098e-6,
  708. 0.899465114892291446442e-6,
  709. 0.229345859265920864296e-7,
  710. 0.225561444863500149219e-9,
  711. 0.947846627503022684216e-12,
  712. 0.135880130108924861008e-14,
  713. -0.348890393399948882918e-21
  714. };
  715. static const mpfr_class Q[] = {
  716. 1,
  717. 0.0845746234001899436914,
  718. 0.00282092984726264681981,
  719. 0.468292921940894236786e-4,
  720. 0.399968812193862100054e-6,
  721. 0.161809290887904476097e-8,
  722. 0.231558608310259605225e-11
  723. };
  724. mpfr_class xs = x - 44;
  725. mpfr_class R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  726. result = Y * x + R * x;
  727. }
  728. }
  729. return result;
  730. }
  731. inline mpfr_class bessel_i0(mpfr_class x)
  732. {
  733. static const mpfr_class P1[] = {
  734. boost::lexical_cast<mpfr_class>("-2.2335582639474375249e+15"),
  735. boost::lexical_cast<mpfr_class>("-5.5050369673018427753e+14"),
  736. boost::lexical_cast<mpfr_class>("-3.2940087627407749166e+13"),
  737. boost::lexical_cast<mpfr_class>("-8.4925101247114157499e+11"),
  738. boost::lexical_cast<mpfr_class>("-1.1912746104985237192e+10"),
  739. boost::lexical_cast<mpfr_class>("-1.0313066708737980747e+08"),
  740. boost::lexical_cast<mpfr_class>("-5.9545626019847898221e+05"),
  741. boost::lexical_cast<mpfr_class>("-2.4125195876041896775e+03"),
  742. boost::lexical_cast<mpfr_class>("-7.0935347449210549190e+00"),
  743. boost::lexical_cast<mpfr_class>("-1.5453977791786851041e-02"),
  744. boost::lexical_cast<mpfr_class>("-2.5172644670688975051e-05"),
  745. boost::lexical_cast<mpfr_class>("-3.0517226450451067446e-08"),
  746. boost::lexical_cast<mpfr_class>("-2.6843448573468483278e-11"),
  747. boost::lexical_cast<mpfr_class>("-1.5982226675653184646e-14"),
  748. boost::lexical_cast<mpfr_class>("-5.2487866627945699800e-18"),
  749. };
  750. static const mpfr_class Q1[] = {
  751. boost::lexical_cast<mpfr_class>("-2.2335582639474375245e+15"),
  752. boost::lexical_cast<mpfr_class>("7.8858692566751002988e+12"),
  753. boost::lexical_cast<mpfr_class>("-1.2207067397808979846e+10"),
  754. boost::lexical_cast<mpfr_class>("1.0377081058062166144e+07"),
  755. boost::lexical_cast<mpfr_class>("-4.8527560179962773045e+03"),
  756. boost::lexical_cast<mpfr_class>("1.0"),
  757. };
  758. static const mpfr_class P2[] = {
  759. boost::lexical_cast<mpfr_class>("-2.2210262233306573296e-04"),
  760. boost::lexical_cast<mpfr_class>("1.3067392038106924055e-02"),
  761. boost::lexical_cast<mpfr_class>("-4.4700805721174453923e-01"),
  762. boost::lexical_cast<mpfr_class>("5.5674518371240761397e+00"),
  763. boost::lexical_cast<mpfr_class>("-2.3517945679239481621e+01"),
  764. boost::lexical_cast<mpfr_class>("3.1611322818701131207e+01"),
  765. boost::lexical_cast<mpfr_class>("-9.6090021968656180000e+00"),
  766. };
  767. static const mpfr_class Q2[] = {
  768. boost::lexical_cast<mpfr_class>("-5.5194330231005480228e-04"),
  769. boost::lexical_cast<mpfr_class>("3.2547697594819615062e-02"),
  770. boost::lexical_cast<mpfr_class>("-1.1151759188741312645e+00"),
  771. boost::lexical_cast<mpfr_class>("1.3982595353892851542e+01"),
  772. boost::lexical_cast<mpfr_class>("-6.0228002066743340583e+01"),
  773. boost::lexical_cast<mpfr_class>("8.5539563258012929600e+01"),
  774. boost::lexical_cast<mpfr_class>("-3.1446690275135491500e+01"),
  775. boost::lexical_cast<mpfr_class>("1.0"),
  776. };
  777. mpfr_class value, factor, r;
  778. BOOST_MATH_STD_USING
  779. using namespace boost::math::tools;
  780. if (x < 0)
  781. {
  782. x = -x; // even function
  783. }
  784. if (x == 0)
  785. {
  786. return static_cast<mpfr_class>(1);
  787. }
  788. if (x <= 15) // x in (0, 15]
  789. {
  790. mpfr_class y = x * x;
  791. value = evaluate_polynomial(P1, y) / evaluate_polynomial(Q1, y);
  792. }
  793. else // x in (15, \infty)
  794. {
  795. mpfr_class y = 1 / x - mpfr_class(1) / 15;
  796. r = evaluate_polynomial(P2, y) / evaluate_polynomial(Q2, y);
  797. factor = exp(x) / sqrt(x);
  798. value = factor * r;
  799. }
  800. return value;
  801. }
  802. inline mpfr_class bessel_i1(mpfr_class x)
  803. {
  804. static const mpfr_class P1[] = {
  805. static_cast<mpfr_class>("-1.4577180278143463643e+15"),
  806. static_cast<mpfr_class>("-1.7732037840791591320e+14"),
  807. static_cast<mpfr_class>("-6.9876779648010090070e+12"),
  808. static_cast<mpfr_class>("-1.3357437682275493024e+11"),
  809. static_cast<mpfr_class>("-1.4828267606612366099e+09"),
  810. static_cast<mpfr_class>("-1.0588550724769347106e+07"),
  811. static_cast<mpfr_class>("-5.1894091982308017540e+04"),
  812. static_cast<mpfr_class>("-1.8225946631657315931e+02"),
  813. static_cast<mpfr_class>("-4.7207090827310162436e-01"),
  814. static_cast<mpfr_class>("-9.1746443287817501309e-04"),
  815. static_cast<mpfr_class>("-1.3466829827635152875e-06"),
  816. static_cast<mpfr_class>("-1.4831904935994647675e-09"),
  817. static_cast<mpfr_class>("-1.1928788903603238754e-12"),
  818. static_cast<mpfr_class>("-6.5245515583151902910e-16"),
  819. static_cast<mpfr_class>("-1.9705291802535139930e-19"),
  820. };
  821. static const mpfr_class Q1[] = {
  822. static_cast<mpfr_class>("-2.9154360556286927285e+15"),
  823. static_cast<mpfr_class>("9.7887501377547640438e+12"),
  824. static_cast<mpfr_class>("-1.4386907088588283434e+10"),
  825. static_cast<mpfr_class>("1.1594225856856884006e+07"),
  826. static_cast<mpfr_class>("-5.1326864679904189920e+03"),
  827. static_cast<mpfr_class>("1.0"),
  828. };
  829. static const mpfr_class P2[] = {
  830. static_cast<mpfr_class>("1.4582087408985668208e-05"),
  831. static_cast<mpfr_class>("-8.9359825138577646443e-04"),
  832. static_cast<mpfr_class>("2.9204895411257790122e-02"),
  833. static_cast<mpfr_class>("-3.4198728018058047439e-01"),
  834. static_cast<mpfr_class>("1.3960118277609544334e+00"),
  835. static_cast<mpfr_class>("-1.9746376087200685843e+00"),
  836. static_cast<mpfr_class>("8.5591872901933459000e-01"),
  837. static_cast<mpfr_class>("-6.0437159056137599999e-02"),
  838. };
  839. static const mpfr_class Q2[] = {
  840. static_cast<mpfr_class>("3.7510433111922824643e-05"),
  841. static_cast<mpfr_class>("-2.2835624489492512649e-03"),
  842. static_cast<mpfr_class>("7.4212010813186530069e-02"),
  843. static_cast<mpfr_class>("-8.5017476463217924408e-01"),
  844. static_cast<mpfr_class>("3.2593714889036996297e+00"),
  845. static_cast<mpfr_class>("-3.8806586721556593450e+00"),
  846. static_cast<mpfr_class>("1.0"),
  847. };
  848. mpfr_class value, factor, r, w;
  849. BOOST_MATH_STD_USING
  850. using namespace boost::math::tools;
  851. w = abs(x);
  852. if (x == 0)
  853. {
  854. return static_cast<mpfr_class>(0);
  855. }
  856. if (w <= 15) // w in (0, 15]
  857. {
  858. mpfr_class y = x * x;
  859. r = evaluate_polynomial(P1, y) / evaluate_polynomial(Q1, y);
  860. factor = w;
  861. value = factor * r;
  862. }
  863. else // w in (15, \infty)
  864. {
  865. mpfr_class y = 1 / w - mpfr_class(1) / 15;
  866. r = evaluate_polynomial(P2, y) / evaluate_polynomial(Q2, y);
  867. factor = exp(w) / sqrt(w);
  868. value = factor * r;
  869. }
  870. if (x < 0)
  871. {
  872. value *= -value; // odd function
  873. }
  874. return value;
  875. }
  876. } // namespace detail
  877. }
  878. template<> struct is_convertible<long double, mpfr_class> : public mpl::false_{};
  879. }
  880. #endif // BOOST_MATH_MPLFR_BINDINGS_HPP