hypergeometric_pFq_checked_series.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2018 John Maddock
  3. // Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HYPERGEOMETRIC_PFQ_SERIES_HPP_
  7. #define BOOST_HYPERGEOMETRIC_PFQ_SERIES_HPP_
  8. #ifndef BOOST_MATH_PFQ_MAX_B_TERMS
  9. # define BOOST_MATH_PFQ_MAX_B_TERMS 5
  10. #endif
  11. #include <boost/array.hpp>
  12. #include <boost/math/special_functions/detail/hypergeometric_series.hpp>
  13. namespace boost { namespace math { namespace detail {
  14. template <class Seq, class Real>
  15. unsigned set_crossover_locations(const Seq& aj, const Seq& bj, const Real& z, unsigned int* crossover_locations)
  16. {
  17. BOOST_MATH_STD_USING
  18. unsigned N_terms = 0;
  19. if(aj.size() == 1 && bj.size() == 1)
  20. {
  21. //
  22. // For 1F1 we can work out where the peaks in the series occur,
  23. // which is to say when:
  24. //
  25. // (a + k)z / (k(b + k)) == +-1
  26. //
  27. // Then we are at either a maxima or a minima in the series, and the
  28. // last such point must be a maxima since the series is globally convergent.
  29. // Potentially then we are solving 2 quadratic equations and have up to 4
  30. // solutions, any solutions which are complex or negative are discarded,
  31. // leaving us with 4 options:
  32. //
  33. // 0 solutions: The series is directly convergent.
  34. // 1 solution : The series diverges to a maxima before coverging.
  35. // 2 solutions: The series is initially convergent, followed by divergence to a maxima before final convergence.
  36. // 3 solutions: The series diverges to a maxima, converges to a minima before diverging again to a second maxima before final convergence.
  37. // 4 solutions: The series converges to a minima before diverging to a maxima, converging to a minima, diverging to a second maxima and then converging.
  38. //
  39. // The first 2 situations are adequately handled by direct series evalution, while the 2,3 and 4 solutions are not.
  40. //
  41. Real a = *aj.begin();
  42. Real b = *bj.begin();
  43. Real sq = 4 * a * z + b * b - 2 * b * z + z * z;
  44. if (sq >= 0)
  45. {
  46. Real t = (-sqrt(sq) - b + z) / 2;
  47. if (t >= 0)
  48. {
  49. crossover_locations[N_terms] = itrunc(t);
  50. ++N_terms;
  51. }
  52. t = (sqrt(sq) - b + z) / 2;
  53. if (t >= 0)
  54. {
  55. crossover_locations[N_terms] = itrunc(t);
  56. ++N_terms;
  57. }
  58. }
  59. sq = -4 * a * z + b * b + 2 * b * z + z * z;
  60. if (sq >= 0)
  61. {
  62. Real t = (-sqrt(sq) - b - z) / 2;
  63. if (t >= 0)
  64. {
  65. crossover_locations[N_terms] = itrunc(t);
  66. ++N_terms;
  67. }
  68. t = (sqrt(sq) - b - z) / 2;
  69. if (t >= 0)
  70. {
  71. crossover_locations[N_terms] = itrunc(t);
  72. ++N_terms;
  73. }
  74. }
  75. std::sort(crossover_locations, crossover_locations + N_terms, std::less<Real>());
  76. //
  77. // Now we need to discard every other terms, as these are the minima:
  78. //
  79. switch (N_terms)
  80. {
  81. case 0:
  82. case 1:
  83. break;
  84. case 2:
  85. crossover_locations[0] = crossover_locations[1];
  86. --N_terms;
  87. break;
  88. case 3:
  89. crossover_locations[1] = crossover_locations[2];
  90. --N_terms;
  91. break;
  92. case 4:
  93. crossover_locations[0] = crossover_locations[1];
  94. crossover_locations[1] = crossover_locations[3];
  95. N_terms -= 2;
  96. break;
  97. }
  98. }
  99. else
  100. {
  101. unsigned n = 0;
  102. for (auto bi = bj.begin(); bi != bj.end(); ++bi, ++n)
  103. {
  104. crossover_locations[n] = *bi >= 0 ? 0 : itrunc(-*bi) + 1;
  105. }
  106. std::sort(crossover_locations, crossover_locations + bj.size(), std::less<Real>());
  107. N_terms = (unsigned)bj.size();
  108. }
  109. return N_terms;
  110. }
  111. template <class Seq, class Real, class Policy, class Terminal>
  112. std::pair<Real, Real> hypergeometric_pFq_checked_series_impl(const Seq& aj, const Seq& bj, const Real& z, const Policy& pol, const Terminal& termination, int& log_scale)
  113. {
  114. BOOST_MATH_STD_USING
  115. Real result = 1;
  116. Real abs_result = 1;
  117. Real term = 1;
  118. Real term0 = 0;
  119. Real tol = boost::math::policies::get_epsilon<Real, Policy>();
  120. boost::uintmax_t k = 0;
  121. Real upper_limit(sqrt(boost::math::tools::max_value<Real>())), diff;
  122. Real lower_limit(1 / upper_limit);
  123. int log_scaling_factor = itrunc(boost::math::tools::log_max_value<Real>()) - 2;
  124. Real scaling_factor = exp(Real(log_scaling_factor));
  125. Real term_m1;
  126. int local_scaling = 0;
  127. if ((aj.size() == 1) && (bj.size() == 0))
  128. {
  129. if (fabs(z) > 1)
  130. {
  131. if ((z > 0) && (floor(*aj.begin()) != *aj.begin()))
  132. {
  133. Real r = policies::raise_domain_error("boost::math::hypergeometric_pFq", "Got p == 1 and q == 0 and |z| > 1, result is imaginary", z, pol);
  134. return std::make_pair(r, r);
  135. }
  136. std::pair<Real, Real> r = hypergeometric_pFq_checked_series_impl(aj, bj, Real(1 / z), pol, termination, log_scale);
  137. Real mul = pow(-z, -*aj.begin());
  138. r.first *= mul;
  139. r.second *= mul;
  140. return r;
  141. }
  142. }
  143. if (aj.size() > bj.size())
  144. {
  145. if (aj.size() == bj.size() + 1)
  146. {
  147. if (fabs(z) > 1)
  148. {
  149. Real r = policies::raise_domain_error("boost::math::hypergeometric_pFq", "Got p == q+1 and |z| > 1, series does not converge", z, pol);
  150. return std::make_pair(r, r);
  151. }
  152. if (fabs(z) == 1)
  153. {
  154. Real s = 0;
  155. for (auto i = bj.begin(); i != bj.end(); ++i)
  156. s += *i;
  157. for (auto i = aj.begin(); i != aj.end(); ++i)
  158. s -= *i;
  159. if ((z == 1) && (s <= 0))
  160. {
  161. Real r = policies::raise_domain_error("boost::math::hypergeometric_pFq", "Got p == q+1 and |z| == 1, in a situation where the series does not converge", z, pol);
  162. return std::make_pair(r, r);
  163. }
  164. if ((z == -1) && (s <= -1))
  165. {
  166. Real r = policies::raise_domain_error("boost::math::hypergeometric_pFq", "Got p == q+1 and |z| == 1, in a situation where the series does not converge", z, pol);
  167. return std::make_pair(r, r);
  168. }
  169. }
  170. }
  171. else
  172. {
  173. Real r = policies::raise_domain_error("boost::math::hypergeometric_pFq", "Got p > q+1, series does not converge", z, pol);
  174. return std::make_pair(r, r);
  175. }
  176. }
  177. while (!termination(k))
  178. {
  179. for (auto ai = aj.begin(); ai != aj.end(); ++ai)
  180. {
  181. term *= *ai + k;
  182. }
  183. if (term == 0)
  184. {
  185. // There is a negative integer in the aj's:
  186. return std::make_pair(result, abs_result);
  187. }
  188. for (auto bi = bj.begin(); bi != bj.end(); ++bi)
  189. {
  190. if (*bi + k == 0)
  191. {
  192. // The series is undefined:
  193. result = boost::math::policies::raise_domain_error("boost::math::hypergeometric_pFq<%1%>", "One of the b values was the negative integer %1%", *bi, pol);
  194. return std::make_pair(result, result);
  195. }
  196. term /= *bi + k;
  197. }
  198. term *= z;
  199. ++k;
  200. term /= k;
  201. //std::cout << k << " " << *bj.begin() + k << " " << result << " " << term << /*" " << term_at_k(*aj.begin(), *bj.begin(), z, k, pol) <<*/ std::endl;
  202. result += term;
  203. abs_result += abs(term);
  204. //std::cout << "k = " << k << " term = " << term * exp(log_scale) << " result = " << result * exp(log_scale) << " abs_result = " << abs_result * exp(log_scale) << std::endl;
  205. //
  206. // Rescaling:
  207. //
  208. if (fabs(abs_result) >= upper_limit)
  209. {
  210. abs_result /= scaling_factor;
  211. result /= scaling_factor;
  212. term /= scaling_factor;
  213. log_scale += log_scaling_factor;
  214. local_scaling += log_scaling_factor;
  215. }
  216. if (fabs(abs_result) < lower_limit)
  217. {
  218. abs_result *= scaling_factor;
  219. result *= scaling_factor;
  220. term *= scaling_factor;
  221. log_scale -= log_scaling_factor;
  222. local_scaling -= log_scaling_factor;
  223. }
  224. if ((abs(result * tol) > abs(term)) && (abs(term0) > abs(term)))
  225. break;
  226. if (abs_result * tol > abs(result))
  227. {
  228. // We have no correct bits in the result... just give up!
  229. result = boost::math::policies::raise_evaluation_error("boost::math::hypergeometric_pFq<%1%>", "Cancellation is so severe that no bits in the reuslt are correct, last result was %1%", Real(result * exp(Real(log_scale))), pol);
  230. return std::make_pair(result, result);
  231. }
  232. term0 = term;
  233. }
  234. //std::cout << "result = " << result << std::endl;
  235. //std::cout << "local_scaling = " << local_scaling << std::endl;
  236. //std::cout << "Norm result = " << std::setprecision(35) << boost::multiprecision::mpfr_float_50(result) * exp(boost::multiprecision::mpfr_float_50(local_scaling)) << std::endl;
  237. //
  238. // We have to be careful when one of the b's crosses the origin:
  239. //
  240. if(bj.size() > BOOST_MATH_PFQ_MAX_B_TERMS)
  241. policies::raise_domain_error<Real>("boost::math::hypergeometric_pFq<%1%>(Seq, Seq, %1%)",
  242. "The number of b terms must be less than the value of BOOST_MATH_PFQ_MAX_B_TERMS (" BOOST_STRINGIZE(BOOST_MATH_PFQ_MAX_B_TERMS) "), but got %1%.",
  243. Real(bj.size()), pol);
  244. unsigned crossover_locations[BOOST_MATH_PFQ_MAX_B_TERMS];
  245. unsigned N_crossovers = set_crossover_locations(aj, bj, z, crossover_locations);
  246. bool terminate = false; // Set to true if one of the a's passes through the origin and terminates the series.
  247. for (unsigned n = 0; n < N_crossovers; ++n)
  248. {
  249. if (k < crossover_locations[n])
  250. {
  251. for (auto ai = aj.begin(); ai != aj.end(); ++ai)
  252. {
  253. if ((*ai < 0) && (floor(*ai) == *ai) && (*ai > crossover_locations[n]))
  254. return std::make_pair(result, abs_result); // b's will never cross the origin!
  255. }
  256. //
  257. // local results:
  258. //
  259. Real loop_result = 0;
  260. Real loop_abs_result = 0;
  261. int loop_scale = 0;
  262. //
  263. // loop_error_scale will be used to increase the size of the error
  264. // estimate (absolute sum), based on the errors inherent in calculating
  265. // the pochhammer symbols.
  266. //
  267. Real loop_error_scale = 0;
  268. //boost::multiprecision::mpfi_float err_est = 0;
  269. //
  270. // b hasn't crossed the origin yet and the series may spring back into life at that point
  271. // so we need to jump forward to that term and then evaluate forwards and backwards from there:
  272. //
  273. unsigned s = crossover_locations[n];
  274. boost::uintmax_t backstop = k;
  275. int s1(1), s2(1);
  276. term = 0;
  277. for (auto ai = aj.begin(); ai != aj.end(); ++ai)
  278. {
  279. if ((floor(*ai) == *ai) && (*ai < 0) && (-*ai <= s))
  280. {
  281. // One of the a terms has passed through zero and terminated the series:
  282. terminate = true;
  283. break;
  284. }
  285. else
  286. {
  287. int ls = 1;
  288. Real p = log_pochhammer(*ai, s, pol, &ls);
  289. s1 *= ls;
  290. term += p;
  291. loop_error_scale = (std::max)(p, loop_error_scale);
  292. //err_est += boost::multiprecision::mpfi_float(p);
  293. }
  294. }
  295. //std::cout << "term = " << term << std::endl;
  296. if (terminate)
  297. break;
  298. for (auto bi = bj.begin(); bi != bj.end(); ++bi)
  299. {
  300. int ls = 1;
  301. Real p = log_pochhammer(*bi, s, pol, &ls);
  302. s2 *= ls;
  303. term -= p;
  304. loop_error_scale = (std::max)(p, loop_error_scale);
  305. //err_est -= boost::multiprecision::mpfi_float(p);
  306. }
  307. //std::cout << "term = " << term << std::endl;
  308. Real p = lgamma(Real(s + 1), pol);
  309. term -= p;
  310. loop_error_scale = (std::max)(p, loop_error_scale);
  311. //err_est -= boost::multiprecision::mpfi_float(p);
  312. p = s * log(fabs(z));
  313. term += p;
  314. loop_error_scale = (std::max)(p, loop_error_scale);
  315. //err_est += boost::multiprecision::mpfi_float(p);
  316. //err_est = exp(err_est);
  317. //std::cout << err_est << std::endl;
  318. //
  319. // Convert loop_error scale to the absolute error
  320. // in term after exp is applied:
  321. //
  322. loop_error_scale *= tools::epsilon<Real>();
  323. //
  324. // Convert to relative error after exp:
  325. //
  326. loop_error_scale = fabs(expm1(loop_error_scale));
  327. //
  328. // Convert to multiplier for the error term:
  329. //
  330. loop_error_scale /= tools::epsilon<Real>();
  331. if (z < 0)
  332. s1 *= (s & 1 ? -1 : 1);
  333. if (term <= tools::log_min_value<Real>())
  334. {
  335. // rescale if we can:
  336. int scale = itrunc(floor(term - tools::log_min_value<Real>()) - 2);
  337. term -= scale;
  338. loop_scale += scale;
  339. }
  340. if (term > 10)
  341. {
  342. int scale = itrunc(floor(term));
  343. term -= scale;
  344. loop_scale += scale;
  345. }
  346. //std::cout << "term = " << term << std::endl;
  347. term = s1 * s2 * exp(term);
  348. //std::cout << "term = " << term << std::endl;
  349. //std::cout << "loop_scale = " << loop_scale << std::endl;
  350. k = s;
  351. term0 = term;
  352. int saved_loop_scale = loop_scale;
  353. bool terms_are_growing = true;
  354. bool trivial_small_series_check = false;
  355. do
  356. {
  357. loop_result += term;
  358. loop_abs_result += fabs(term);
  359. //std::cout << "k = " << k << " term = " << term * exp(loop_scale) << " result = " << loop_result * exp(loop_scale) << " abs_result = " << loop_abs_result * exp(loop_scale) << std::endl;
  360. if (fabs(loop_result) >= upper_limit)
  361. {
  362. loop_result /= scaling_factor;
  363. loop_abs_result /= scaling_factor;
  364. term /= scaling_factor;
  365. loop_scale += log_scaling_factor;
  366. }
  367. if (fabs(loop_result) < lower_limit)
  368. {
  369. loop_result *= scaling_factor;
  370. loop_abs_result *= scaling_factor;
  371. term *= scaling_factor;
  372. loop_scale -= log_scaling_factor;
  373. }
  374. term_m1 = term;
  375. for (auto ai = aj.begin(); ai != aj.end(); ++ai)
  376. {
  377. term *= *ai + k;
  378. }
  379. if (term == 0)
  380. {
  381. // There is a negative integer in the aj's:
  382. return std::make_pair(result, abs_result);
  383. }
  384. for (auto bi = bj.begin(); bi != bj.end(); ++bi)
  385. {
  386. if (*bi + k == 0)
  387. {
  388. // The series is undefined:
  389. result = boost::math::policies::raise_domain_error("boost::math::hypergeometric_pFq<%1%>", "One of the b values was the negative integer %1%", *bi, pol);
  390. return std::make_pair(result, result);
  391. }
  392. term /= *bi + k;
  393. }
  394. term *= z / (k + 1);
  395. ++k;
  396. diff = fabs(term / loop_result);
  397. terms_are_growing = fabs(term) > fabs(term_m1);
  398. if (!trivial_small_series_check && !terms_are_growing)
  399. {
  400. //
  401. // Now that we have started to converge, check to see if the value of
  402. // this local sum is trivially small compared to the result. If so
  403. // abort this part of the series.
  404. //
  405. trivial_small_series_check = true;
  406. Real d;
  407. if (loop_scale > local_scaling)
  408. {
  409. int rescale = local_scaling - loop_scale;
  410. if (rescale < tools::log_min_value<Real>())
  411. d = 1; // arbtrary value, we want to keep going
  412. else
  413. d = fabs(term / (result * exp(Real(rescale))));
  414. }
  415. else
  416. {
  417. int rescale = loop_scale - local_scaling;
  418. if (rescale < tools::log_min_value<Real>())
  419. d = 0; // terminate this loop
  420. else
  421. d = fabs(term * exp(Real(rescale)) / result);
  422. }
  423. if (d < boost::math::policies::get_epsilon<Real, Policy>())
  424. break;
  425. }
  426. } while (!termination(k - s) && ((diff > boost::math::policies::get_epsilon<Real, Policy>()) || terms_are_growing));
  427. //std::cout << "Norm loop result = " << std::setprecision(35) << boost::multiprecision::mpfr_float_50(loop_result)* exp(boost::multiprecision::mpfr_float_50(loop_scale)) << std::endl;
  428. //
  429. // We now need to combine the results of the first series summation with whatever
  430. // local results we have now. First though, rescale abs_result by loop_error_scale
  431. // to factor in the error in the pochhammer terms at the start of this block:
  432. //
  433. boost::uintmax_t next_backstop = k;
  434. loop_abs_result += loop_error_scale * fabs(loop_result);
  435. if (loop_scale > local_scaling)
  436. {
  437. //
  438. // Need to shrink previous result:
  439. //
  440. int rescale = local_scaling - loop_scale;
  441. local_scaling = loop_scale;
  442. log_scale -= rescale;
  443. Real ex = exp(Real(rescale));
  444. result *= ex;
  445. abs_result *= ex;
  446. result += loop_result;
  447. abs_result += loop_abs_result;
  448. }
  449. else if (local_scaling > loop_scale)
  450. {
  451. //
  452. // Need to shrink local result:
  453. //
  454. int rescale = loop_scale - local_scaling;
  455. Real ex = exp(Real(rescale));
  456. loop_result *= ex;
  457. loop_abs_result *= ex;
  458. result += loop_result;
  459. abs_result += loop_abs_result;
  460. }
  461. else
  462. {
  463. result += loop_result;
  464. abs_result += loop_abs_result;
  465. }
  466. //
  467. // Now go backwards as well:
  468. //
  469. k = s;
  470. term = term0;
  471. loop_result = 0;
  472. loop_abs_result = 0;
  473. loop_scale = saved_loop_scale;
  474. trivial_small_series_check = false;
  475. do
  476. {
  477. --k;
  478. if (k == backstop)
  479. break;
  480. term_m1 = term;
  481. for (auto ai = aj.begin(); ai != aj.end(); ++ai)
  482. {
  483. term /= *ai + k;
  484. }
  485. for (auto bi = bj.begin(); bi != bj.end(); ++bi)
  486. {
  487. if (*bi + k == 0)
  488. {
  489. // The series is undefined:
  490. result = boost::math::policies::raise_domain_error("boost::math::hypergeometric_pFq<%1%>", "One of the b values was the negative integer %1%", *bi, pol);
  491. return std::make_pair(result, result);
  492. }
  493. term *= *bi + k;
  494. }
  495. term *= (k + 1) / z;
  496. loop_result += term;
  497. loop_abs_result += fabs(term);
  498. if (!trivial_small_series_check && (fabs(term) < fabs(term_m1)))
  499. {
  500. //
  501. // Now that we have started to converge, check to see if the value of
  502. // this local sum is trivially small compared to the result. If so
  503. // abort this part of the series.
  504. //
  505. trivial_small_series_check = true;
  506. Real d;
  507. if (loop_scale > local_scaling)
  508. {
  509. int rescale = local_scaling - loop_scale;
  510. if (rescale < tools::log_min_value<Real>())
  511. d = 1; // keep going
  512. else
  513. d = fabs(term / (result * exp(Real(rescale))));
  514. }
  515. else
  516. {
  517. int rescale = loop_scale - local_scaling;
  518. if (rescale < tools::log_min_value<Real>())
  519. d = 0; // stop, underflow
  520. else
  521. d = fabs(term * exp(Real(rescale)) / result);
  522. }
  523. if (d < boost::math::policies::get_epsilon<Real, Policy>())
  524. break;
  525. }
  526. //std::cout << "k = " << k << " result = " << result << " abs_result = " << abs_result << std::endl;
  527. if (fabs(loop_result) >= upper_limit)
  528. {
  529. loop_result /= scaling_factor;
  530. loop_abs_result /= scaling_factor;
  531. term /= scaling_factor;
  532. loop_scale += log_scaling_factor;
  533. }
  534. if (fabs(loop_result) < lower_limit)
  535. {
  536. loop_result *= scaling_factor;
  537. loop_abs_result *= scaling_factor;
  538. term *= scaling_factor;
  539. loop_scale -= log_scaling_factor;
  540. }
  541. diff = fabs(term / loop_result);
  542. } while (!termination(s - k) && ((diff > boost::math::policies::get_epsilon<Real, Policy>()) || (fabs(term) > fabs(term_m1))));
  543. //std::cout << "Norm loop result = " << std::setprecision(35) << boost::multiprecision::mpfr_float_50(loop_result)* exp(boost::multiprecision::mpfr_float_50(loop_scale)) << std::endl;
  544. //
  545. // We now need to combine the results of the first series summation with whatever
  546. // local results we have now. First though, rescale abs_result by loop_error_scale
  547. // to factor in the error in the pochhammer terms at the start of this block:
  548. //
  549. loop_abs_result += loop_error_scale * fabs(loop_result);
  550. //
  551. if (loop_scale > local_scaling)
  552. {
  553. //
  554. // Need to shrink previous result:
  555. //
  556. int rescale = local_scaling - loop_scale;
  557. local_scaling = loop_scale;
  558. log_scale -= rescale;
  559. Real ex = exp(Real(rescale));
  560. result *= ex;
  561. abs_result *= ex;
  562. result += loop_result;
  563. abs_result += loop_abs_result;
  564. }
  565. else if (local_scaling > loop_scale)
  566. {
  567. //
  568. // Need to shrink local result:
  569. //
  570. int rescale = loop_scale - local_scaling;
  571. Real ex = exp(Real(rescale));
  572. loop_result *= ex;
  573. loop_abs_result *= ex;
  574. result += loop_result;
  575. abs_result += loop_abs_result;
  576. }
  577. else
  578. {
  579. result += loop_result;
  580. abs_result += loop_abs_result;
  581. }
  582. //
  583. // Reset k to the largest k we reached
  584. //
  585. k = next_backstop;
  586. }
  587. }
  588. return std::make_pair(result, abs_result);
  589. }
  590. struct iteration_terminator
  591. {
  592. iteration_terminator(boost::uintmax_t i) : m(i) {}
  593. bool operator()(boost::uintmax_t v) const { return v >= m; }
  594. boost::uintmax_t m;
  595. };
  596. template <class Seq, class Real, class Policy>
  597. Real hypergeometric_pFq_checked_series_impl(const Seq& aj, const Seq& bj, const Real& z, const Policy& pol, int& log_scale)
  598. {
  599. BOOST_MATH_STD_USING
  600. iteration_terminator term(boost::math::policies::get_max_series_iterations<Policy>());
  601. std::pair<Real, Real> result = hypergeometric_pFq_checked_series_impl(aj, bj, z, pol, term, log_scale);
  602. //
  603. // Check to see how many digits we've lost, if it's more than half, raise an evaluation error -
  604. // this is an entirely arbitrary cut off, but not unreasonable.
  605. //
  606. if (result.second * sqrt(boost::math::policies::get_epsilon<Real, Policy>()) > abs(result.first))
  607. {
  608. return boost::math::policies::raise_evaluation_error("boost::math::hypergeometric_pFq<%1%>", "Cancellation is so severe that fewer than half the bits in the result are correct, last result was %1%", Real(result.first * exp(Real(log_scale))), pol);
  609. }
  610. return result.first;
  611. }
  612. template <class Real, class Policy>
  613. inline Real hypergeometric_1F1_checked_series_impl(const Real& a, const Real& b, const Real& z, const Policy& pol, int& log_scale)
  614. {
  615. boost::array<Real, 1> aj = { a };
  616. boost::array<Real, 1> bj = { b };
  617. return hypergeometric_pFq_checked_series_impl(aj, bj, z, pol, log_scale);
  618. }
  619. } } } // namespaces
  620. #endif // BOOST_HYPERGEOMETRIC_PFQ_SERIES_HPP_