ell_integral.tcc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // Special functions -*- C++ -*-
  2. // Copyright (C) 2006-2018 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/ell_integral.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 on:
  30. // (1) B. C. Carlson Numer. Math. 33, 1 (1979)
  31. // (2) B. C. Carlson, Special Functions of Applied Mathematics (1977)
  32. // (3) The Gnu Scientific Library, http://www.gnu.org/software/gsl
  33. // (4) Numerical Recipes in C, 2nd ed, by W. H. Press, S. A. Teukolsky,
  34. // W. T. Vetterling, B. P. Flannery, Cambridge University Press
  35. // (1992), pp. 261-269
  36. #ifndef _GLIBCXX_TR1_ELL_INTEGRAL_TCC
  37. #define _GLIBCXX_TR1_ELL_INTEGRAL_TCC 1
  38. namespace std _GLIBCXX_VISIBILITY(default)
  39. {
  40. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  41. #if _GLIBCXX_USE_STD_SPEC_FUNCS
  42. #elif defined(_GLIBCXX_TR1_CMATH)
  43. namespace tr1
  44. {
  45. #else
  46. # error do not include this header directly, use <cmath> or <tr1/cmath>
  47. #endif
  48. // [5.2] Special functions
  49. // Implementation-space details.
  50. namespace __detail
  51. {
  52. /**
  53. * @brief Return the Carlson elliptic function @f$ R_F(x,y,z) @f$
  54. * of the first kind.
  55. *
  56. * The Carlson elliptic function of the first kind is defined by:
  57. * @f[
  58. * R_F(x,y,z) = \frac{1}{2} \int_0^\infty
  59. * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}}
  60. * @f]
  61. *
  62. * @param __x The first of three symmetric arguments.
  63. * @param __y The second of three symmetric arguments.
  64. * @param __z The third of three symmetric arguments.
  65. * @return The Carlson elliptic function of the first kind.
  66. */
  67. template<typename _Tp>
  68. _Tp
  69. __ellint_rf(_Tp __x, _Tp __y, _Tp __z)
  70. {
  71. const _Tp __min = std::numeric_limits<_Tp>::min();
  72. const _Tp __max = std::numeric_limits<_Tp>::max();
  73. const _Tp __lolim = _Tp(5) * __min;
  74. const _Tp __uplim = __max / _Tp(5);
  75. if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
  76. std::__throw_domain_error(__N("Argument less than zero "
  77. "in __ellint_rf."));
  78. else if (__x + __y < __lolim || __x + __z < __lolim
  79. || __y + __z < __lolim)
  80. std::__throw_domain_error(__N("Argument too small in __ellint_rf"));
  81. else
  82. {
  83. const _Tp __c0 = _Tp(1) / _Tp(4);
  84. const _Tp __c1 = _Tp(1) / _Tp(24);
  85. const _Tp __c2 = _Tp(1) / _Tp(10);
  86. const _Tp __c3 = _Tp(3) / _Tp(44);
  87. const _Tp __c4 = _Tp(1) / _Tp(14);
  88. _Tp __xn = __x;
  89. _Tp __yn = __y;
  90. _Tp __zn = __z;
  91. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  92. const _Tp __errtol = std::pow(__eps, _Tp(1) / _Tp(6));
  93. _Tp __mu;
  94. _Tp __xndev, __yndev, __zndev;
  95. const unsigned int __max_iter = 100;
  96. for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
  97. {
  98. __mu = (__xn + __yn + __zn) / _Tp(3);
  99. __xndev = 2 - (__mu + __xn) / __mu;
  100. __yndev = 2 - (__mu + __yn) / __mu;
  101. __zndev = 2 - (__mu + __zn) / __mu;
  102. _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
  103. __epsilon = std::max(__epsilon, std::abs(__zndev));
  104. if (__epsilon < __errtol)
  105. break;
  106. const _Tp __xnroot = std::sqrt(__xn);
  107. const _Tp __ynroot = std::sqrt(__yn);
  108. const _Tp __znroot = std::sqrt(__zn);
  109. const _Tp __lambda = __xnroot * (__ynroot + __znroot)
  110. + __ynroot * __znroot;
  111. __xn = __c0 * (__xn + __lambda);
  112. __yn = __c0 * (__yn + __lambda);
  113. __zn = __c0 * (__zn + __lambda);
  114. }
  115. const _Tp __e2 = __xndev * __yndev - __zndev * __zndev;
  116. const _Tp __e3 = __xndev * __yndev * __zndev;
  117. const _Tp __s = _Tp(1) + (__c1 * __e2 - __c2 - __c3 * __e3) * __e2
  118. + __c4 * __e3;
  119. return __s / std::sqrt(__mu);
  120. }
  121. }
  122. /**
  123. * @brief Return the complete elliptic integral of the first kind
  124. * @f$ K(k) @f$ by series expansion.
  125. *
  126. * The complete elliptic integral of the first kind is defined as
  127. * @f[
  128. * K(k) = F(k,\pi/2) = \int_0^{\pi/2}\frac{d\theta}
  129. * {\sqrt{1 - k^2sin^2\theta}}
  130. * @f]
  131. *
  132. * This routine is not bad as long as |k| is somewhat smaller than 1
  133. * but is not is good as the Carlson elliptic integral formulation.
  134. *
  135. * @param __k The argument of the complete elliptic function.
  136. * @return The complete elliptic function of the first kind.
  137. */
  138. template<typename _Tp>
  139. _Tp
  140. __comp_ellint_1_series(_Tp __k)
  141. {
  142. const _Tp __kk = __k * __k;
  143. _Tp __term = __kk / _Tp(4);
  144. _Tp __sum = _Tp(1) + __term;
  145. const unsigned int __max_iter = 1000;
  146. for (unsigned int __i = 2; __i < __max_iter; ++__i)
  147. {
  148. __term *= (2 * __i - 1) * __kk / (2 * __i);
  149. if (__term < std::numeric_limits<_Tp>::epsilon())
  150. break;
  151. __sum += __term;
  152. }
  153. return __numeric_constants<_Tp>::__pi_2() * __sum;
  154. }
  155. /**
  156. * @brief Return the complete elliptic integral of the first kind
  157. * @f$ K(k) @f$ using the Carlson formulation.
  158. *
  159. * The complete elliptic integral of the first kind is defined as
  160. * @f[
  161. * K(k) = F(k,\pi/2) = \int_0^{\pi/2}\frac{d\theta}
  162. * {\sqrt{1 - k^2 sin^2\theta}}
  163. * @f]
  164. * where @f$ F(k,\phi) @f$ is the incomplete elliptic integral of the
  165. * first kind.
  166. *
  167. * @param __k The argument of the complete elliptic function.
  168. * @return The complete elliptic function of the first kind.
  169. */
  170. template<typename _Tp>
  171. _Tp
  172. __comp_ellint_1(_Tp __k)
  173. {
  174. if (__isnan(__k))
  175. return std::numeric_limits<_Tp>::quiet_NaN();
  176. else if (std::abs(__k) >= _Tp(1))
  177. return std::numeric_limits<_Tp>::quiet_NaN();
  178. else
  179. return __ellint_rf(_Tp(0), _Tp(1) - __k * __k, _Tp(1));
  180. }
  181. /**
  182. * @brief Return the incomplete elliptic integral of the first kind
  183. * @f$ F(k,\phi) @f$ using the Carlson formulation.
  184. *
  185. * The incomplete elliptic integral of the first kind is defined as
  186. * @f[
  187. * F(k,\phi) = \int_0^{\phi}\frac{d\theta}
  188. * {\sqrt{1 - k^2 sin^2\theta}}
  189. * @f]
  190. *
  191. * @param __k The argument of the elliptic function.
  192. * @param __phi The integral limit argument of the elliptic function.
  193. * @return The elliptic function of the first kind.
  194. */
  195. template<typename _Tp>
  196. _Tp
  197. __ellint_1(_Tp __k, _Tp __phi)
  198. {
  199. if (__isnan(__k) || __isnan(__phi))
  200. return std::numeric_limits<_Tp>::quiet_NaN();
  201. else if (std::abs(__k) > _Tp(1))
  202. std::__throw_domain_error(__N("Bad argument in __ellint_1."));
  203. else
  204. {
  205. // Reduce phi to -pi/2 < phi < +pi/2.
  206. const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
  207. + _Tp(0.5L));
  208. const _Tp __phi_red = __phi
  209. - __n * __numeric_constants<_Tp>::__pi();
  210. const _Tp __s = std::sin(__phi_red);
  211. const _Tp __c = std::cos(__phi_red);
  212. const _Tp __F = __s
  213. * __ellint_rf(__c * __c,
  214. _Tp(1) - __k * __k * __s * __s, _Tp(1));
  215. if (__n == 0)
  216. return __F;
  217. else
  218. return __F + _Tp(2) * __n * __comp_ellint_1(__k);
  219. }
  220. }
  221. /**
  222. * @brief Return the complete elliptic integral of the second kind
  223. * @f$ E(k) @f$ by series expansion.
  224. *
  225. * The complete elliptic integral of the second kind is defined as
  226. * @f[
  227. * E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
  228. * @f]
  229. *
  230. * This routine is not bad as long as |k| is somewhat smaller than 1
  231. * but is not is good as the Carlson elliptic integral formulation.
  232. *
  233. * @param __k The argument of the complete elliptic function.
  234. * @return The complete elliptic function of the second kind.
  235. */
  236. template<typename _Tp>
  237. _Tp
  238. __comp_ellint_2_series(_Tp __k)
  239. {
  240. const _Tp __kk = __k * __k;
  241. _Tp __term = __kk;
  242. _Tp __sum = __term;
  243. const unsigned int __max_iter = 1000;
  244. for (unsigned int __i = 2; __i < __max_iter; ++__i)
  245. {
  246. const _Tp __i2m = 2 * __i - 1;
  247. const _Tp __i2 = 2 * __i;
  248. __term *= __i2m * __i2m * __kk / (__i2 * __i2);
  249. if (__term < std::numeric_limits<_Tp>::epsilon())
  250. break;
  251. __sum += __term / __i2m;
  252. }
  253. return __numeric_constants<_Tp>::__pi_2() * (_Tp(1) - __sum);
  254. }
  255. /**
  256. * @brief Return the Carlson elliptic function of the second kind
  257. * @f$ R_D(x,y,z) = R_J(x,y,z,z) @f$ where
  258. * @f$ R_J(x,y,z,p) @f$ is the Carlson elliptic function
  259. * of the third kind.
  260. *
  261. * The Carlson elliptic function of the second kind is defined by:
  262. * @f[
  263. * R_D(x,y,z) = \frac{3}{2} \int_0^\infty
  264. * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{3/2}}
  265. * @f]
  266. *
  267. * Based on Carlson's algorithms:
  268. * - B. C. Carlson Numer. Math. 33, 1 (1979)
  269. * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
  270. * - Numerical Recipes in C, 2nd ed, pp. 261-269,
  271. * by Press, Teukolsky, Vetterling, Flannery (1992)
  272. *
  273. * @param __x The first of two symmetric arguments.
  274. * @param __y The second of two symmetric arguments.
  275. * @param __z The third argument.
  276. * @return The Carlson elliptic function of the second kind.
  277. */
  278. template<typename _Tp>
  279. _Tp
  280. __ellint_rd(_Tp __x, _Tp __y, _Tp __z)
  281. {
  282. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  283. const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
  284. const _Tp __min = std::numeric_limits<_Tp>::min();
  285. const _Tp __max = std::numeric_limits<_Tp>::max();
  286. const _Tp __lolim = _Tp(2) / std::pow(__max, _Tp(2) / _Tp(3));
  287. const _Tp __uplim = std::pow(_Tp(0.1L) * __errtol / __min, _Tp(2) / _Tp(3));
  288. if (__x < _Tp(0) || __y < _Tp(0))
  289. std::__throw_domain_error(__N("Argument less than zero "
  290. "in __ellint_rd."));
  291. else if (__x + __y < __lolim || __z < __lolim)
  292. std::__throw_domain_error(__N("Argument too small "
  293. "in __ellint_rd."));
  294. else
  295. {
  296. const _Tp __c0 = _Tp(1) / _Tp(4);
  297. const _Tp __c1 = _Tp(3) / _Tp(14);
  298. const _Tp __c2 = _Tp(1) / _Tp(6);
  299. const _Tp __c3 = _Tp(9) / _Tp(22);
  300. const _Tp __c4 = _Tp(3) / _Tp(26);
  301. _Tp __xn = __x;
  302. _Tp __yn = __y;
  303. _Tp __zn = __z;
  304. _Tp __sigma = _Tp(0);
  305. _Tp __power4 = _Tp(1);
  306. _Tp __mu;
  307. _Tp __xndev, __yndev, __zndev;
  308. const unsigned int __max_iter = 100;
  309. for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
  310. {
  311. __mu = (__xn + __yn + _Tp(3) * __zn) / _Tp(5);
  312. __xndev = (__mu - __xn) / __mu;
  313. __yndev = (__mu - __yn) / __mu;
  314. __zndev = (__mu - __zn) / __mu;
  315. _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
  316. __epsilon = std::max(__epsilon, std::abs(__zndev));
  317. if (__epsilon < __errtol)
  318. break;
  319. _Tp __xnroot = std::sqrt(__xn);
  320. _Tp __ynroot = std::sqrt(__yn);
  321. _Tp __znroot = std::sqrt(__zn);
  322. _Tp __lambda = __xnroot * (__ynroot + __znroot)
  323. + __ynroot * __znroot;
  324. __sigma += __power4 / (__znroot * (__zn + __lambda));
  325. __power4 *= __c0;
  326. __xn = __c0 * (__xn + __lambda);
  327. __yn = __c0 * (__yn + __lambda);
  328. __zn = __c0 * (__zn + __lambda);
  329. }
  330. // Note: __ea is an SPU badname.
  331. _Tp __eaa = __xndev * __yndev;
  332. _Tp __eb = __zndev * __zndev;
  333. _Tp __ec = __eaa - __eb;
  334. _Tp __ed = __eaa - _Tp(6) * __eb;
  335. _Tp __ef = __ed + __ec + __ec;
  336. _Tp __s1 = __ed * (-__c1 + __c3 * __ed
  337. / _Tp(3) - _Tp(3) * __c4 * __zndev * __ef
  338. / _Tp(2));
  339. _Tp __s2 = __zndev
  340. * (__c2 * __ef
  341. + __zndev * (-__c3 * __ec - __zndev * __c4 - __eaa));
  342. return _Tp(3) * __sigma + __power4 * (_Tp(1) + __s1 + __s2)
  343. / (__mu * std::sqrt(__mu));
  344. }
  345. }
  346. /**
  347. * @brief Return the complete elliptic integral of the second kind
  348. * @f$ E(k) @f$ using the Carlson formulation.
  349. *
  350. * The complete elliptic integral of the second kind is defined as
  351. * @f[
  352. * E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
  353. * @f]
  354. *
  355. * @param __k The argument of the complete elliptic function.
  356. * @return The complete elliptic function of the second kind.
  357. */
  358. template<typename _Tp>
  359. _Tp
  360. __comp_ellint_2(_Tp __k)
  361. {
  362. if (__isnan(__k))
  363. return std::numeric_limits<_Tp>::quiet_NaN();
  364. else if (std::abs(__k) == 1)
  365. return _Tp(1);
  366. else if (std::abs(__k) > _Tp(1))
  367. std::__throw_domain_error(__N("Bad argument in __comp_ellint_2."));
  368. else
  369. {
  370. const _Tp __kk = __k * __k;
  371. return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
  372. - __kk * __ellint_rd(_Tp(0), _Tp(1) - __kk, _Tp(1)) / _Tp(3);
  373. }
  374. }
  375. /**
  376. * @brief Return the incomplete elliptic integral of the second kind
  377. * @f$ E(k,\phi) @f$ using the Carlson formulation.
  378. *
  379. * The incomplete elliptic integral of the second kind is defined as
  380. * @f[
  381. * E(k,\phi) = \int_0^{\phi} \sqrt{1 - k^2 sin^2\theta}
  382. * @f]
  383. *
  384. * @param __k The argument of the elliptic function.
  385. * @param __phi The integral limit argument of the elliptic function.
  386. * @return The elliptic function of the second kind.
  387. */
  388. template<typename _Tp>
  389. _Tp
  390. __ellint_2(_Tp __k, _Tp __phi)
  391. {
  392. if (__isnan(__k) || __isnan(__phi))
  393. return std::numeric_limits<_Tp>::quiet_NaN();
  394. else if (std::abs(__k) > _Tp(1))
  395. std::__throw_domain_error(__N("Bad argument in __ellint_2."));
  396. else
  397. {
  398. // Reduce phi to -pi/2 < phi < +pi/2.
  399. const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
  400. + _Tp(0.5L));
  401. const _Tp __phi_red = __phi
  402. - __n * __numeric_constants<_Tp>::__pi();
  403. const _Tp __kk = __k * __k;
  404. const _Tp __s = std::sin(__phi_red);
  405. const _Tp __ss = __s * __s;
  406. const _Tp __sss = __ss * __s;
  407. const _Tp __c = std::cos(__phi_red);
  408. const _Tp __cc = __c * __c;
  409. const _Tp __E = __s
  410. * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
  411. - __kk * __sss
  412. * __ellint_rd(__cc, _Tp(1) - __kk * __ss, _Tp(1))
  413. / _Tp(3);
  414. if (__n == 0)
  415. return __E;
  416. else
  417. return __E + _Tp(2) * __n * __comp_ellint_2(__k);
  418. }
  419. }
  420. /**
  421. * @brief Return the Carlson elliptic function
  422. * @f$ R_C(x,y) = R_F(x,y,y) @f$ where @f$ R_F(x,y,z) @f$
  423. * is the Carlson elliptic function of the first kind.
  424. *
  425. * The Carlson elliptic function is defined by:
  426. * @f[
  427. * R_C(x,y) = \frac{1}{2} \int_0^\infty
  428. * \frac{dt}{(t + x)^{1/2}(t + y)}
  429. * @f]
  430. *
  431. * Based on Carlson's algorithms:
  432. * - B. C. Carlson Numer. Math. 33, 1 (1979)
  433. * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
  434. * - Numerical Recipes in C, 2nd ed, pp. 261-269,
  435. * by Press, Teukolsky, Vetterling, Flannery (1992)
  436. *
  437. * @param __x The first argument.
  438. * @param __y The second argument.
  439. * @return The Carlson elliptic function.
  440. */
  441. template<typename _Tp>
  442. _Tp
  443. __ellint_rc(_Tp __x, _Tp __y)
  444. {
  445. const _Tp __min = std::numeric_limits<_Tp>::min();
  446. const _Tp __max = std::numeric_limits<_Tp>::max();
  447. const _Tp __lolim = _Tp(5) * __min;
  448. const _Tp __uplim = __max / _Tp(5);
  449. if (__x < _Tp(0) || __y < _Tp(0) || __x + __y < __lolim)
  450. std::__throw_domain_error(__N("Argument less than zero "
  451. "in __ellint_rc."));
  452. else
  453. {
  454. const _Tp __c0 = _Tp(1) / _Tp(4);
  455. const _Tp __c1 = _Tp(1) / _Tp(7);
  456. const _Tp __c2 = _Tp(9) / _Tp(22);
  457. const _Tp __c3 = _Tp(3) / _Tp(10);
  458. const _Tp __c4 = _Tp(3) / _Tp(8);
  459. _Tp __xn = __x;
  460. _Tp __yn = __y;
  461. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  462. const _Tp __errtol = std::pow(__eps / _Tp(30), _Tp(1) / _Tp(6));
  463. _Tp __mu;
  464. _Tp __sn;
  465. const unsigned int __max_iter = 100;
  466. for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
  467. {
  468. __mu = (__xn + _Tp(2) * __yn) / _Tp(3);
  469. __sn = (__yn + __mu) / __mu - _Tp(2);
  470. if (std::abs(__sn) < __errtol)
  471. break;
  472. const _Tp __lambda = _Tp(2) * std::sqrt(__xn) * std::sqrt(__yn)
  473. + __yn;
  474. __xn = __c0 * (__xn + __lambda);
  475. __yn = __c0 * (__yn + __lambda);
  476. }
  477. _Tp __s = __sn * __sn
  478. * (__c3 + __sn*(__c1 + __sn * (__c4 + __sn * __c2)));
  479. return (_Tp(1) + __s) / std::sqrt(__mu);
  480. }
  481. }
  482. /**
  483. * @brief Return the Carlson elliptic function @f$ R_J(x,y,z,p) @f$
  484. * of the third kind.
  485. *
  486. * The Carlson elliptic function of the third kind is defined by:
  487. * @f[
  488. * R_J(x,y,z,p) = \frac{3}{2} \int_0^\infty
  489. * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}(t + p)}
  490. * @f]
  491. *
  492. * Based on Carlson's algorithms:
  493. * - B. C. Carlson Numer. Math. 33, 1 (1979)
  494. * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
  495. * - Numerical Recipes in C, 2nd ed, pp. 261-269,
  496. * by Press, Teukolsky, Vetterling, Flannery (1992)
  497. *
  498. * @param __x The first of three symmetric arguments.
  499. * @param __y The second of three symmetric arguments.
  500. * @param __z The third of three symmetric arguments.
  501. * @param __p The fourth argument.
  502. * @return The Carlson elliptic function of the fourth kind.
  503. */
  504. template<typename _Tp>
  505. _Tp
  506. __ellint_rj(_Tp __x, _Tp __y, _Tp __z, _Tp __p)
  507. {
  508. const _Tp __min = std::numeric_limits<_Tp>::min();
  509. const _Tp __max = std::numeric_limits<_Tp>::max();
  510. const _Tp __lolim = std::pow(_Tp(5) * __min, _Tp(1)/_Tp(3));
  511. const _Tp __uplim = _Tp(0.3L)
  512. * std::pow(_Tp(0.2L) * __max, _Tp(1)/_Tp(3));
  513. if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
  514. std::__throw_domain_error(__N("Argument less than zero "
  515. "in __ellint_rj."));
  516. else if (__x + __y < __lolim || __x + __z < __lolim
  517. || __y + __z < __lolim || __p < __lolim)
  518. std::__throw_domain_error(__N("Argument too small "
  519. "in __ellint_rj"));
  520. else
  521. {
  522. const _Tp __c0 = _Tp(1) / _Tp(4);
  523. const _Tp __c1 = _Tp(3) / _Tp(14);
  524. const _Tp __c2 = _Tp(1) / _Tp(3);
  525. const _Tp __c3 = _Tp(3) / _Tp(22);
  526. const _Tp __c4 = _Tp(3) / _Tp(26);
  527. _Tp __xn = __x;
  528. _Tp __yn = __y;
  529. _Tp __zn = __z;
  530. _Tp __pn = __p;
  531. _Tp __sigma = _Tp(0);
  532. _Tp __power4 = _Tp(1);
  533. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  534. const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
  535. _Tp __lambda, __mu;
  536. _Tp __xndev, __yndev, __zndev, __pndev;
  537. const unsigned int __max_iter = 100;
  538. for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
  539. {
  540. __mu = (__xn + __yn + __zn + _Tp(2) * __pn) / _Tp(5);
  541. __xndev = (__mu - __xn) / __mu;
  542. __yndev = (__mu - __yn) / __mu;
  543. __zndev = (__mu - __zn) / __mu;
  544. __pndev = (__mu - __pn) / __mu;
  545. _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
  546. __epsilon = std::max(__epsilon, std::abs(__zndev));
  547. __epsilon = std::max(__epsilon, std::abs(__pndev));
  548. if (__epsilon < __errtol)
  549. break;
  550. const _Tp __xnroot = std::sqrt(__xn);
  551. const _Tp __ynroot = std::sqrt(__yn);
  552. const _Tp __znroot = std::sqrt(__zn);
  553. const _Tp __lambda = __xnroot * (__ynroot + __znroot)
  554. + __ynroot * __znroot;
  555. const _Tp __alpha1 = __pn * (__xnroot + __ynroot + __znroot)
  556. + __xnroot * __ynroot * __znroot;
  557. const _Tp __alpha2 = __alpha1 * __alpha1;
  558. const _Tp __beta = __pn * (__pn + __lambda)
  559. * (__pn + __lambda);
  560. __sigma += __power4 * __ellint_rc(__alpha2, __beta);
  561. __power4 *= __c0;
  562. __xn = __c0 * (__xn + __lambda);
  563. __yn = __c0 * (__yn + __lambda);
  564. __zn = __c0 * (__zn + __lambda);
  565. __pn = __c0 * (__pn + __lambda);
  566. }
  567. // Note: __ea is an SPU badname.
  568. _Tp __eaa = __xndev * (__yndev + __zndev) + __yndev * __zndev;
  569. _Tp __eb = __xndev * __yndev * __zndev;
  570. _Tp __ec = __pndev * __pndev;
  571. _Tp __e2 = __eaa - _Tp(3) * __ec;
  572. _Tp __e3 = __eb + _Tp(2) * __pndev * (__eaa - __ec);
  573. _Tp __s1 = _Tp(1) + __e2 * (-__c1 + _Tp(3) * __c3 * __e2 / _Tp(4)
  574. - _Tp(3) * __c4 * __e3 / _Tp(2));
  575. _Tp __s2 = __eb * (__c2 / _Tp(2)
  576. + __pndev * (-__c3 - __c3 + __pndev * __c4));
  577. _Tp __s3 = __pndev * __eaa * (__c2 - __pndev * __c3)
  578. - __c2 * __pndev * __ec;
  579. return _Tp(3) * __sigma + __power4 * (__s1 + __s2 + __s3)
  580. / (__mu * std::sqrt(__mu));
  581. }
  582. }
  583. /**
  584. * @brief Return the complete elliptic integral of the third kind
  585. * @f$ \Pi(k,\nu) = \Pi(k,\nu,\pi/2) @f$ using the
  586. * Carlson formulation.
  587. *
  588. * The complete elliptic integral of the third kind is defined as
  589. * @f[
  590. * \Pi(k,\nu) = \int_0^{\pi/2}
  591. * \frac{d\theta}
  592. * {(1 - \nu \sin^2\theta)\sqrt{1 - k^2 \sin^2\theta}}
  593. * @f]
  594. *
  595. * @param __k The argument of the elliptic function.
  596. * @param __nu The second argument of the elliptic function.
  597. * @return The complete elliptic function of the third kind.
  598. */
  599. template<typename _Tp>
  600. _Tp
  601. __comp_ellint_3(_Tp __k, _Tp __nu)
  602. {
  603. if (__isnan(__k) || __isnan(__nu))
  604. return std::numeric_limits<_Tp>::quiet_NaN();
  605. else if (__nu == _Tp(1))
  606. return std::numeric_limits<_Tp>::infinity();
  607. else if (std::abs(__k) > _Tp(1))
  608. std::__throw_domain_error(__N("Bad argument in __comp_ellint_3."));
  609. else
  610. {
  611. const _Tp __kk = __k * __k;
  612. return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
  613. + __nu
  614. * __ellint_rj(_Tp(0), _Tp(1) - __kk, _Tp(1), _Tp(1) - __nu)
  615. / _Tp(3);
  616. }
  617. }
  618. /**
  619. * @brief Return the incomplete elliptic integral of the third kind
  620. * @f$ \Pi(k,\nu,\phi) @f$ using the Carlson formulation.
  621. *
  622. * The incomplete elliptic integral of the third kind is defined as
  623. * @f[
  624. * \Pi(k,\nu,\phi) = \int_0^{\phi}
  625. * \frac{d\theta}
  626. * {(1 - \nu \sin^2\theta)
  627. * \sqrt{1 - k^2 \sin^2\theta}}
  628. * @f]
  629. *
  630. * @param __k The argument of the elliptic function.
  631. * @param __nu The second argument of the elliptic function.
  632. * @param __phi The integral limit argument of the elliptic function.
  633. * @return The elliptic function of the third kind.
  634. */
  635. template<typename _Tp>
  636. _Tp
  637. __ellint_3(_Tp __k, _Tp __nu, _Tp __phi)
  638. {
  639. if (__isnan(__k) || __isnan(__nu) || __isnan(__phi))
  640. return std::numeric_limits<_Tp>::quiet_NaN();
  641. else if (std::abs(__k) > _Tp(1))
  642. std::__throw_domain_error(__N("Bad argument in __ellint_3."));
  643. else
  644. {
  645. // Reduce phi to -pi/2 < phi < +pi/2.
  646. const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
  647. + _Tp(0.5L));
  648. const _Tp __phi_red = __phi
  649. - __n * __numeric_constants<_Tp>::__pi();
  650. const _Tp __kk = __k * __k;
  651. const _Tp __s = std::sin(__phi_red);
  652. const _Tp __ss = __s * __s;
  653. const _Tp __sss = __ss * __s;
  654. const _Tp __c = std::cos(__phi_red);
  655. const _Tp __cc = __c * __c;
  656. const _Tp __Pi = __s
  657. * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
  658. + __nu * __sss
  659. * __ellint_rj(__cc, _Tp(1) - __kk * __ss, _Tp(1),
  660. _Tp(1) - __nu * __ss) / _Tp(3);
  661. if (__n == 0)
  662. return __Pi;
  663. else
  664. return __Pi + _Tp(2) * __n * __comp_ellint_3(__k, __nu);
  665. }
  666. }
  667. } // namespace __detail
  668. #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
  669. } // namespace tr1
  670. #endif
  671. _GLIBCXX_END_NAMESPACE_VERSION
  672. }
  673. #endif // _GLIBCXX_TR1_ELL_INTEGRAL_TCC