mpfr.hpp 30 KB

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