hypergeometric.tcc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. // Special functions -*- C++ -*-
  2. // Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License and
  19. // a copy of the GCC Runtime Library Exception along with this program;
  20. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. // <http://www.gnu.org/licenses/>.
  22. /** @file tr1/hypergeometric.tcc
  23. * This is an internal header file, included by other library headers.
  24. * Do not attempt to use it directly. @headername{tr1/cmath}
  25. */
  26. //
  27. // ISO C++ 14882 TR1: 5.2 Special functions
  28. //
  29. // Written by Edward Smith-Rowland based:
  30. // (1) Handbook of Mathematical Functions,
  31. // ed. Milton Abramowitz and Irene A. Stegun,
  32. // Dover Publications,
  33. // Section 6, pp. 555-566
  34. // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
  35. #ifndef _GLIBCXX_TR1_HYPERGEOMETRIC_TCC
  36. #define _GLIBCXX_TR1_HYPERGEOMETRIC_TCC 1
  37. namespace std _GLIBCXX_VISIBILITY(default)
  38. {
  39. namespace tr1
  40. {
  41. // [5.2] Special functions
  42. // Implementation-space details.
  43. namespace __detail
  44. {
  45. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  46. /**
  47. * @brief This routine returns the confluent hypergeometric function
  48. * by series expansion.
  49. *
  50. * @f[
  51. * _1F_1(a;c;x) = \frac{\Gamma(c)}{\Gamma(a)}
  52. * \sum_{n=0}^{\infty}
  53. * \frac{\Gamma(a+n)}{\Gamma(c+n)}
  54. * \frac{x^n}{n!}
  55. * @f]
  56. *
  57. * If a and b are integers and a < 0 and either b > 0 or b < a
  58. * then the series is a polynomial with a finite number of
  59. * terms. If b is an integer and b <= 0 the confluent
  60. * hypergeometric function is undefined.
  61. *
  62. * @param __a The "numerator" parameter.
  63. * @param __c The "denominator" parameter.
  64. * @param __x The argument of the confluent hypergeometric function.
  65. * @return The confluent hypergeometric function.
  66. */
  67. template<typename _Tp>
  68. _Tp
  69. __conf_hyperg_series(_Tp __a, _Tp __c, _Tp __x)
  70. {
  71. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  72. _Tp __term = _Tp(1);
  73. _Tp __Fac = _Tp(1);
  74. const unsigned int __max_iter = 100000;
  75. unsigned int __i;
  76. for (__i = 0; __i < __max_iter; ++__i)
  77. {
  78. __term *= (__a + _Tp(__i)) * __x
  79. / ((__c + _Tp(__i)) * _Tp(1 + __i));
  80. if (std::abs(__term) < __eps)
  81. {
  82. break;
  83. }
  84. __Fac += __term;
  85. }
  86. if (__i == __max_iter)
  87. std::__throw_runtime_error(__N("Series failed to converge "
  88. "in __conf_hyperg_series."));
  89. return __Fac;
  90. }
  91. /**
  92. * @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
  93. * by an iterative procedure described in
  94. * Luke, Algorithms for the Computation of Mathematical Functions.
  95. *
  96. * Like the case of the 2F1 rational approximations, these are
  97. * probably guaranteed to converge for x < 0, barring gross
  98. * numerical instability in the pre-asymptotic regime.
  99. */
  100. template<typename _Tp>
  101. _Tp
  102. __conf_hyperg_luke(_Tp __a, _Tp __c, _Tp __xin)
  103. {
  104. const _Tp __big = std::pow(std::numeric_limits<_Tp>::max(), _Tp(0.16L));
  105. const int __nmax = 20000;
  106. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  107. const _Tp __x = -__xin;
  108. const _Tp __x3 = __x * __x * __x;
  109. const _Tp __t0 = __a / __c;
  110. const _Tp __t1 = (__a + _Tp(1)) / (_Tp(2) * __c);
  111. const _Tp __t2 = (__a + _Tp(2)) / (_Tp(2) * (__c + _Tp(1)));
  112. _Tp __F = _Tp(1);
  113. _Tp __prec;
  114. _Tp __Bnm3 = _Tp(1);
  115. _Tp __Bnm2 = _Tp(1) + __t1 * __x;
  116. _Tp __Bnm1 = _Tp(1) + __t2 * __x * (_Tp(1) + __t1 / _Tp(3) * __x);
  117. _Tp __Anm3 = _Tp(1);
  118. _Tp __Anm2 = __Bnm2 - __t0 * __x;
  119. _Tp __Anm1 = __Bnm1 - __t0 * (_Tp(1) + __t2 * __x) * __x
  120. + __t0 * __t1 * (__c / (__c + _Tp(1))) * __x * __x;
  121. int __n = 3;
  122. while(1)
  123. {
  124. _Tp __npam1 = _Tp(__n - 1) + __a;
  125. _Tp __npcm1 = _Tp(__n - 1) + __c;
  126. _Tp __npam2 = _Tp(__n - 2) + __a;
  127. _Tp __npcm2 = _Tp(__n - 2) + __c;
  128. _Tp __tnm1 = _Tp(2 * __n - 1);
  129. _Tp __tnm3 = _Tp(2 * __n - 3);
  130. _Tp __tnm5 = _Tp(2 * __n - 5);
  131. _Tp __F1 = (_Tp(__n - 2) - __a) / (_Tp(2) * __tnm3 * __npcm1);
  132. _Tp __F2 = (_Tp(__n) + __a) * __npam1
  133. / (_Tp(4) * __tnm1 * __tnm3 * __npcm2 * __npcm1);
  134. _Tp __F3 = -__npam2 * __npam1 * (_Tp(__n - 2) - __a)
  135. / (_Tp(8) * __tnm3 * __tnm3 * __tnm5
  136. * (_Tp(__n - 3) + __c) * __npcm2 * __npcm1);
  137. _Tp __E = -__npam1 * (_Tp(__n - 1) - __c)
  138. / (_Tp(2) * __tnm3 * __npcm2 * __npcm1);
  139. _Tp __An = (_Tp(1) + __F1 * __x) * __Anm1
  140. + (__E + __F2 * __x) * __x * __Anm2 + __F3 * __x3 * __Anm3;
  141. _Tp __Bn = (_Tp(1) + __F1 * __x) * __Bnm1
  142. + (__E + __F2 * __x) * __x * __Bnm2 + __F3 * __x3 * __Bnm3;
  143. _Tp __r = __An / __Bn;
  144. __prec = std::abs((__F - __r) / __F);
  145. __F = __r;
  146. if (__prec < __eps || __n > __nmax)
  147. break;
  148. if (std::abs(__An) > __big || std::abs(__Bn) > __big)
  149. {
  150. __An /= __big;
  151. __Bn /= __big;
  152. __Anm1 /= __big;
  153. __Bnm1 /= __big;
  154. __Anm2 /= __big;
  155. __Bnm2 /= __big;
  156. __Anm3 /= __big;
  157. __Bnm3 /= __big;
  158. }
  159. else if (std::abs(__An) < _Tp(1) / __big
  160. || std::abs(__Bn) < _Tp(1) / __big)
  161. {
  162. __An *= __big;
  163. __Bn *= __big;
  164. __Anm1 *= __big;
  165. __Bnm1 *= __big;
  166. __Anm2 *= __big;
  167. __Bnm2 *= __big;
  168. __Anm3 *= __big;
  169. __Bnm3 *= __big;
  170. }
  171. ++__n;
  172. __Bnm3 = __Bnm2;
  173. __Bnm2 = __Bnm1;
  174. __Bnm1 = __Bn;
  175. __Anm3 = __Anm2;
  176. __Anm2 = __Anm1;
  177. __Anm1 = __An;
  178. }
  179. if (__n >= __nmax)
  180. std::__throw_runtime_error(__N("Iteration failed to converge "
  181. "in __conf_hyperg_luke."));
  182. return __F;
  183. }
  184. /**
  185. * @brief Return the confluent hypogeometric function
  186. * @f$ _1F_1(a;c;x) @f$.
  187. *
  188. * @todo Handle b == nonpositive integer blowup - return NaN.
  189. *
  190. * @param __a The @a numerator parameter.
  191. * @param __c The @a denominator parameter.
  192. * @param __x The argument of the confluent hypergeometric function.
  193. * @return The confluent hypergeometric function.
  194. */
  195. template<typename _Tp>
  196. _Tp
  197. __conf_hyperg(_Tp __a, _Tp __c, _Tp __x)
  198. {
  199. #if _GLIBCXX_USE_C99_MATH_TR1
  200. const _Tp __c_nint = std::tr1::nearbyint(__c);
  201. #else
  202. const _Tp __c_nint = static_cast<int>(__c + _Tp(0.5L));
  203. #endif
  204. if (__isnan(__a) || __isnan(__c) || __isnan(__x))
  205. return std::numeric_limits<_Tp>::quiet_NaN();
  206. else if (__c_nint == __c && __c_nint <= 0)
  207. return std::numeric_limits<_Tp>::infinity();
  208. else if (__a == _Tp(0))
  209. return _Tp(1);
  210. else if (__c == __a)
  211. return std::exp(__x);
  212. else if (__x < _Tp(0))
  213. return __conf_hyperg_luke(__a, __c, __x);
  214. else
  215. return __conf_hyperg_series(__a, __c, __x);
  216. }
  217. /**
  218. * @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
  219. * by series expansion.
  220. *
  221. * The hypogeometric function is defined by
  222. * @f[
  223. * _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
  224. * \sum_{n=0}^{\infty}
  225. * \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
  226. * \frac{x^n}{n!}
  227. * @f]
  228. *
  229. * This works and it's pretty fast.
  230. *
  231. * @param __a The first @a numerator parameter.
  232. * @param __a The second @a numerator parameter.
  233. * @param __c The @a denominator parameter.
  234. * @param __x The argument of the confluent hypergeometric function.
  235. * @return The confluent hypergeometric function.
  236. */
  237. template<typename _Tp>
  238. _Tp
  239. __hyperg_series(_Tp __a, _Tp __b, _Tp __c, _Tp __x)
  240. {
  241. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  242. _Tp __term = _Tp(1);
  243. _Tp __Fabc = _Tp(1);
  244. const unsigned int __max_iter = 100000;
  245. unsigned int __i;
  246. for (__i = 0; __i < __max_iter; ++__i)
  247. {
  248. __term *= (__a + _Tp(__i)) * (__b + _Tp(__i)) * __x
  249. / ((__c + _Tp(__i)) * _Tp(1 + __i));
  250. if (std::abs(__term) < __eps)
  251. {
  252. break;
  253. }
  254. __Fabc += __term;
  255. }
  256. if (__i == __max_iter)
  257. std::__throw_runtime_error(__N("Series failed to converge "
  258. "in __hyperg_series."));
  259. return __Fabc;
  260. }
  261. /**
  262. * @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
  263. * by an iterative procedure described in
  264. * Luke, Algorithms for the Computation of Mathematical Functions.
  265. */
  266. template<typename _Tp>
  267. _Tp
  268. __hyperg_luke(_Tp __a, _Tp __b, _Tp __c, _Tp __xin)
  269. {
  270. const _Tp __big = std::pow(std::numeric_limits<_Tp>::max(), _Tp(0.16L));
  271. const int __nmax = 20000;
  272. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  273. const _Tp __x = -__xin;
  274. const _Tp __x3 = __x * __x * __x;
  275. const _Tp __t0 = __a * __b / __c;
  276. const _Tp __t1 = (__a + _Tp(1)) * (__b + _Tp(1)) / (_Tp(2) * __c);
  277. const _Tp __t2 = (__a + _Tp(2)) * (__b + _Tp(2))
  278. / (_Tp(2) * (__c + _Tp(1)));
  279. _Tp __F = _Tp(1);
  280. _Tp __Bnm3 = _Tp(1);
  281. _Tp __Bnm2 = _Tp(1) + __t1 * __x;
  282. _Tp __Bnm1 = _Tp(1) + __t2 * __x * (_Tp(1) + __t1 / _Tp(3) * __x);
  283. _Tp __Anm3 = _Tp(1);
  284. _Tp __Anm2 = __Bnm2 - __t0 * __x;
  285. _Tp __Anm1 = __Bnm1 - __t0 * (_Tp(1) + __t2 * __x) * __x
  286. + __t0 * __t1 * (__c / (__c + _Tp(1))) * __x * __x;
  287. int __n = 3;
  288. while (1)
  289. {
  290. const _Tp __npam1 = _Tp(__n - 1) + __a;
  291. const _Tp __npbm1 = _Tp(__n - 1) + __b;
  292. const _Tp __npcm1 = _Tp(__n - 1) + __c;
  293. const _Tp __npam2 = _Tp(__n - 2) + __a;
  294. const _Tp __npbm2 = _Tp(__n - 2) + __b;
  295. const _Tp __npcm2 = _Tp(__n - 2) + __c;
  296. const _Tp __tnm1 = _Tp(2 * __n - 1);
  297. const _Tp __tnm3 = _Tp(2 * __n - 3);
  298. const _Tp __tnm5 = _Tp(2 * __n - 5);
  299. const _Tp __n2 = __n * __n;
  300. const _Tp __F1 = (_Tp(3) * __n2 + (__a + __b - _Tp(6)) * __n
  301. + _Tp(2) - __a * __b - _Tp(2) * (__a + __b))
  302. / (_Tp(2) * __tnm3 * __npcm1);
  303. const _Tp __F2 = -(_Tp(3) * __n2 - (__a + __b + _Tp(6)) * __n
  304. + _Tp(2) - __a * __b) * __npam1 * __npbm1
  305. / (_Tp(4) * __tnm1 * __tnm3 * __npcm2 * __npcm1);
  306. const _Tp __F3 = (__npam2 * __npam1 * __npbm2 * __npbm1
  307. * (_Tp(__n - 2) - __a) * (_Tp(__n - 2) - __b))
  308. / (_Tp(8) * __tnm3 * __tnm3 * __tnm5
  309. * (_Tp(__n - 3) + __c) * __npcm2 * __npcm1);
  310. const _Tp __E = -__npam1 * __npbm1 * (_Tp(__n - 1) - __c)
  311. / (_Tp(2) * __tnm3 * __npcm2 * __npcm1);
  312. _Tp __An = (_Tp(1) + __F1 * __x) * __Anm1
  313. + (__E + __F2 * __x) * __x * __Anm2 + __F3 * __x3 * __Anm3;
  314. _Tp __Bn = (_Tp(1) + __F1 * __x) * __Bnm1
  315. + (__E + __F2 * __x) * __x * __Bnm2 + __F3 * __x3 * __Bnm3;
  316. const _Tp __r = __An / __Bn;
  317. const _Tp __prec = std::abs((__F - __r) / __F);
  318. __F = __r;
  319. if (__prec < __eps || __n > __nmax)
  320. break;
  321. if (std::abs(__An) > __big || std::abs(__Bn) > __big)
  322. {
  323. __An /= __big;
  324. __Bn /= __big;
  325. __Anm1 /= __big;
  326. __Bnm1 /= __big;
  327. __Anm2 /= __big;
  328. __Bnm2 /= __big;
  329. __Anm3 /= __big;
  330. __Bnm3 /= __big;
  331. }
  332. else if (std::abs(__An) < _Tp(1) / __big
  333. || std::abs(__Bn) < _Tp(1) / __big)
  334. {
  335. __An *= __big;
  336. __Bn *= __big;
  337. __Anm1 *= __big;
  338. __Bnm1 *= __big;
  339. __Anm2 *= __big;
  340. __Bnm2 *= __big;
  341. __Anm3 *= __big;
  342. __Bnm3 *= __big;
  343. }
  344. ++__n;
  345. __Bnm3 = __Bnm2;
  346. __Bnm2 = __Bnm1;
  347. __Bnm1 = __Bn;
  348. __Anm3 = __Anm2;
  349. __Anm2 = __Anm1;
  350. __Anm1 = __An;
  351. }
  352. if (__n >= __nmax)
  353. std::__throw_runtime_error(__N("Iteration failed to converge "
  354. "in __hyperg_luke."));
  355. return __F;
  356. }
  357. /**
  358. * @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$
  359. * by the reflection formulae in Abramowitz & Stegun formula
  360. * 15.3.6 for d = c - a - b not integral and formula 15.3.11 for
  361. * d = c - a - b integral. This assumes a, b, c != negative
  362. * integer.
  363. *
  364. * The hypogeometric function is defined by
  365. * @f[
  366. * _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
  367. * \sum_{n=0}^{\infty}
  368. * \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
  369. * \frac{x^n}{n!}
  370. * @f]
  371. *
  372. * The reflection formula for nonintegral @f$ d = c - a - b @f$ is:
  373. * @f[
  374. * _2F_1(a,b;c;x) = \frac{\Gamma(c)\Gamma(d)}{\Gamma(c-a)\Gamma(c-b)}
  375. * _2F_1(a,b;1-d;1-x)
  376. * + \frac{\Gamma(c)\Gamma(-d)}{\Gamma(a)\Gamma(b)}
  377. * _2F_1(c-a,c-b;1+d;1-x)
  378. * @f]
  379. *
  380. * The reflection formula for integral @f$ m = c - a - b @f$ is:
  381. * @f[
  382. * _2F_1(a,b;a+b+m;x) = \frac{\Gamma(m)\Gamma(a+b+m)}{\Gamma(a+m)\Gamma(b+m)}
  383. * \sum_{k=0}^{m-1} \frac{(m+a)_k(m+b)_k}{k!(1-m)_k}
  384. * -
  385. * @f]
  386. */
  387. template<typename _Tp>
  388. _Tp
  389. __hyperg_reflect(_Tp __a, _Tp __b, _Tp __c, _Tp __x)
  390. {
  391. const _Tp __d = __c - __a - __b;
  392. const int __intd = std::floor(__d + _Tp(0.5L));
  393. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  394. const _Tp __toler = _Tp(1000) * __eps;
  395. const _Tp __log_max = std::log(std::numeric_limits<_Tp>::max());
  396. const bool __d_integer = (std::abs(__d - __intd) < __toler);
  397. if (__d_integer)
  398. {
  399. const _Tp __ln_omx = std::log(_Tp(1) - __x);
  400. const _Tp __ad = std::abs(__d);
  401. _Tp __F1, __F2;
  402. _Tp __d1, __d2;
  403. if (__d >= _Tp(0))
  404. {
  405. __d1 = __d;
  406. __d2 = _Tp(0);
  407. }
  408. else
  409. {
  410. __d1 = _Tp(0);
  411. __d2 = __d;
  412. }
  413. const _Tp __lng_c = __log_gamma(__c);
  414. // Evaluate F1.
  415. if (__ad < __eps)
  416. {
  417. // d = c - a - b = 0.
  418. __F1 = _Tp(0);
  419. }
  420. else
  421. {
  422. bool __ok_d1 = true;
  423. _Tp __lng_ad, __lng_ad1, __lng_bd1;
  424. __try
  425. {
  426. __lng_ad = __log_gamma(__ad);
  427. __lng_ad1 = __log_gamma(__a + __d1);
  428. __lng_bd1 = __log_gamma(__b + __d1);
  429. }
  430. __catch(...)
  431. {
  432. __ok_d1 = false;
  433. }
  434. if (__ok_d1)
  435. {
  436. /* Gamma functions in the denominator are ok.
  437. * Proceed with evaluation.
  438. */
  439. _Tp __sum1 = _Tp(1);
  440. _Tp __term = _Tp(1);
  441. _Tp __ln_pre1 = __lng_ad + __lng_c + __d2 * __ln_omx
  442. - __lng_ad1 - __lng_bd1;
  443. /* Do F1 sum.
  444. */
  445. for (int __i = 1; __i < __ad; ++__i)
  446. {
  447. const int __j = __i - 1;
  448. __term *= (__a + __d2 + __j) * (__b + __d2 + __j)
  449. / (_Tp(1) + __d2 + __j) / __i * (_Tp(1) - __x);
  450. __sum1 += __term;
  451. }
  452. if (__ln_pre1 > __log_max)
  453. std::__throw_runtime_error(__N("Overflow of gamma functions"
  454. " in __hyperg_luke."));
  455. else
  456. __F1 = std::exp(__ln_pre1) * __sum1;
  457. }
  458. else
  459. {
  460. // Gamma functions in the denominator were not ok.
  461. // So the F1 term is zero.
  462. __F1 = _Tp(0);
  463. }
  464. } // end F1 evaluation
  465. // Evaluate F2.
  466. bool __ok_d2 = true;
  467. _Tp __lng_ad2, __lng_bd2;
  468. __try
  469. {
  470. __lng_ad2 = __log_gamma(__a + __d2);
  471. __lng_bd2 = __log_gamma(__b + __d2);
  472. }
  473. __catch(...)
  474. {
  475. __ok_d2 = false;
  476. }
  477. if (__ok_d2)
  478. {
  479. // Gamma functions in the denominator are ok.
  480. // Proceed with evaluation.
  481. const int __maxiter = 2000;
  482. const _Tp __psi_1 = -__numeric_constants<_Tp>::__gamma_e();
  483. const _Tp __psi_1pd = __psi(_Tp(1) + __ad);
  484. const _Tp __psi_apd1 = __psi(__a + __d1);
  485. const _Tp __psi_bpd1 = __psi(__b + __d1);
  486. _Tp __psi_term = __psi_1 + __psi_1pd - __psi_apd1
  487. - __psi_bpd1 - __ln_omx;
  488. _Tp __fact = _Tp(1);
  489. _Tp __sum2 = __psi_term;
  490. _Tp __ln_pre2 = __lng_c + __d1 * __ln_omx
  491. - __lng_ad2 - __lng_bd2;
  492. // Do F2 sum.
  493. int __j;
  494. for (__j = 1; __j < __maxiter; ++__j)
  495. {
  496. // Values for psi functions use recurrence;
  497. // Abramowitz & Stegun 6.3.5
  498. const _Tp __term1 = _Tp(1) / _Tp(__j)
  499. + _Tp(1) / (__ad + __j);
  500. const _Tp __term2 = _Tp(1) / (__a + __d1 + _Tp(__j - 1))
  501. + _Tp(1) / (__b + __d1 + _Tp(__j - 1));
  502. __psi_term += __term1 - __term2;
  503. __fact *= (__a + __d1 + _Tp(__j - 1))
  504. * (__b + __d1 + _Tp(__j - 1))
  505. / ((__ad + __j) * __j) * (_Tp(1) - __x);
  506. const _Tp __delta = __fact * __psi_term;
  507. __sum2 += __delta;
  508. if (std::abs(__delta) < __eps * std::abs(__sum2))
  509. break;
  510. }
  511. if (__j == __maxiter)
  512. std::__throw_runtime_error(__N("Sum F2 failed to converge "
  513. "in __hyperg_reflect"));
  514. if (__sum2 == _Tp(0))
  515. __F2 = _Tp(0);
  516. else
  517. __F2 = std::exp(__ln_pre2) * __sum2;
  518. }
  519. else
  520. {
  521. // Gamma functions in the denominator not ok.
  522. // So the F2 term is zero.
  523. __F2 = _Tp(0);
  524. } // end F2 evaluation
  525. const _Tp __sgn_2 = (__intd % 2 == 1 ? -_Tp(1) : _Tp(1));
  526. const _Tp __F = __F1 + __sgn_2 * __F2;
  527. return __F;
  528. }
  529. else
  530. {
  531. // d = c - a - b not an integer.
  532. // These gamma functions appear in the denominator, so we
  533. // catch their harmless domain errors and set the terms to zero.
  534. bool __ok1 = true;
  535. _Tp __sgn_g1ca = _Tp(0), __ln_g1ca = _Tp(0);
  536. _Tp __sgn_g1cb = _Tp(0), __ln_g1cb = _Tp(0);
  537. __try
  538. {
  539. __sgn_g1ca = __log_gamma_sign(__c - __a);
  540. __ln_g1ca = __log_gamma(__c - __a);
  541. __sgn_g1cb = __log_gamma_sign(__c - __b);
  542. __ln_g1cb = __log_gamma(__c - __b);
  543. }
  544. __catch(...)
  545. {
  546. __ok1 = false;
  547. }
  548. bool __ok2 = true;
  549. _Tp __sgn_g2a = _Tp(0), __ln_g2a = _Tp(0);
  550. _Tp __sgn_g2b = _Tp(0), __ln_g2b = _Tp(0);
  551. __try
  552. {
  553. __sgn_g2a = __log_gamma_sign(__a);
  554. __ln_g2a = __log_gamma(__a);
  555. __sgn_g2b = __log_gamma_sign(__b);
  556. __ln_g2b = __log_gamma(__b);
  557. }
  558. __catch(...)
  559. {
  560. __ok2 = false;
  561. }
  562. const _Tp __sgn_gc = __log_gamma_sign(__c);
  563. const _Tp __ln_gc = __log_gamma(__c);
  564. const _Tp __sgn_gd = __log_gamma_sign(__d);
  565. const _Tp __ln_gd = __log_gamma(__d);
  566. const _Tp __sgn_gmd = __log_gamma_sign(-__d);
  567. const _Tp __ln_gmd = __log_gamma(-__d);
  568. const _Tp __sgn1 = __sgn_gc * __sgn_gd * __sgn_g1ca * __sgn_g1cb;
  569. const _Tp __sgn2 = __sgn_gc * __sgn_gmd * __sgn_g2a * __sgn_g2b;
  570. _Tp __pre1, __pre2;
  571. if (__ok1 && __ok2)
  572. {
  573. _Tp __ln_pre1 = __ln_gc + __ln_gd - __ln_g1ca - __ln_g1cb;
  574. _Tp __ln_pre2 = __ln_gc + __ln_gmd - __ln_g2a - __ln_g2b
  575. + __d * std::log(_Tp(1) - __x);
  576. if (__ln_pre1 < __log_max && __ln_pre2 < __log_max)
  577. {
  578. __pre1 = std::exp(__ln_pre1);
  579. __pre2 = std::exp(__ln_pre2);
  580. __pre1 *= __sgn1;
  581. __pre2 *= __sgn2;
  582. }
  583. else
  584. {
  585. std::__throw_runtime_error(__N("Overflow of gamma functions "
  586. "in __hyperg_reflect"));
  587. }
  588. }
  589. else if (__ok1 && !__ok2)
  590. {
  591. _Tp __ln_pre1 = __ln_gc + __ln_gd - __ln_g1ca - __ln_g1cb;
  592. if (__ln_pre1 < __log_max)
  593. {
  594. __pre1 = std::exp(__ln_pre1);
  595. __pre1 *= __sgn1;
  596. __pre2 = _Tp(0);
  597. }
  598. else
  599. {
  600. std::__throw_runtime_error(__N("Overflow of gamma functions "
  601. "in __hyperg_reflect"));
  602. }
  603. }
  604. else if (!__ok1 && __ok2)
  605. {
  606. _Tp __ln_pre2 = __ln_gc + __ln_gmd - __ln_g2a - __ln_g2b
  607. + __d * std::log(_Tp(1) - __x);
  608. if (__ln_pre2 < __log_max)
  609. {
  610. __pre1 = _Tp(0);
  611. __pre2 = std::exp(__ln_pre2);
  612. __pre2 *= __sgn2;
  613. }
  614. else
  615. {
  616. std::__throw_runtime_error(__N("Overflow of gamma functions "
  617. "in __hyperg_reflect"));
  618. }
  619. }
  620. else
  621. {
  622. __pre1 = _Tp(0);
  623. __pre2 = _Tp(0);
  624. std::__throw_runtime_error(__N("Underflow of gamma functions "
  625. "in __hyperg_reflect"));
  626. }
  627. const _Tp __F1 = __hyperg_series(__a, __b, _Tp(1) - __d,
  628. _Tp(1) - __x);
  629. const _Tp __F2 = __hyperg_series(__c - __a, __c - __b, _Tp(1) + __d,
  630. _Tp(1) - __x);
  631. const _Tp __F = __pre1 * __F1 + __pre2 * __F2;
  632. return __F;
  633. }
  634. }
  635. /**
  636. * @brief Return the hypogeometric function @f$ _2F_1(a,b;c;x) @f$.
  637. *
  638. * The hypogeometric function is defined by
  639. * @f[
  640. * _2F_1(a,b;c;x) = \frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}
  641. * \sum_{n=0}^{\infty}
  642. * \frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}
  643. * \frac{x^n}{n!}
  644. * @f]
  645. *
  646. * @param __a The first @a numerator parameter.
  647. * @param __a The second @a numerator parameter.
  648. * @param __c The @a denominator parameter.
  649. * @param __x The argument of the confluent hypergeometric function.
  650. * @return The confluent hypergeometric function.
  651. */
  652. template<typename _Tp>
  653. _Tp
  654. __hyperg(_Tp __a, _Tp __b, _Tp __c, _Tp __x)
  655. {
  656. #if _GLIBCXX_USE_C99_MATH_TR1
  657. const _Tp __a_nint = std::tr1::nearbyint(__a);
  658. const _Tp __b_nint = std::tr1::nearbyint(__b);
  659. const _Tp __c_nint = std::tr1::nearbyint(__c);
  660. #else
  661. const _Tp __a_nint = static_cast<int>(__a + _Tp(0.5L));
  662. const _Tp __b_nint = static_cast<int>(__b + _Tp(0.5L));
  663. const _Tp __c_nint = static_cast<int>(__c + _Tp(0.5L));
  664. #endif
  665. const _Tp __toler = _Tp(1000) * std::numeric_limits<_Tp>::epsilon();
  666. if (std::abs(__x) >= _Tp(1))
  667. std::__throw_domain_error(__N("Argument outside unit circle "
  668. "in __hyperg."));
  669. else if (__isnan(__a) || __isnan(__b)
  670. || __isnan(__c) || __isnan(__x))
  671. return std::numeric_limits<_Tp>::quiet_NaN();
  672. else if (__c_nint == __c && __c_nint <= _Tp(0))
  673. return std::numeric_limits<_Tp>::infinity();
  674. else if (std::abs(__c - __b) < __toler || std::abs(__c - __a) < __toler)
  675. return std::pow(_Tp(1) - __x, __c - __a - __b);
  676. else if (__a >= _Tp(0) && __b >= _Tp(0) && __c >= _Tp(0)
  677. && __x >= _Tp(0) && __x < _Tp(0.995L))
  678. return __hyperg_series(__a, __b, __c, __x);
  679. else if (std::abs(__a) < _Tp(10) && std::abs(__b) < _Tp(10))
  680. {
  681. // For integer a and b the hypergeometric function is a
  682. // finite polynomial.
  683. if (__a < _Tp(0) && std::abs(__a - __a_nint) < __toler)
  684. return __hyperg_series(__a_nint, __b, __c, __x);
  685. else if (__b < _Tp(0) && std::abs(__b - __b_nint) < __toler)
  686. return __hyperg_series(__a, __b_nint, __c, __x);
  687. else if (__x < -_Tp(0.25L))
  688. return __hyperg_luke(__a, __b, __c, __x);
  689. else if (__x < _Tp(0.5L))
  690. return __hyperg_series(__a, __b, __c, __x);
  691. else
  692. if (std::abs(__c) > _Tp(10))
  693. return __hyperg_series(__a, __b, __c, __x);
  694. else
  695. return __hyperg_reflect(__a, __b, __c, __x);
  696. }
  697. else
  698. return __hyperg_luke(__a, __b, __c, __x);
  699. }
  700. _GLIBCXX_END_NAMESPACE_VERSION
  701. } // namespace std::tr1::__detail
  702. }
  703. }
  704. #endif // _GLIBCXX_TR1_HYPERGEOMETRIC_TCC