mpfr.hpp 30 KB

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