ell_integral.tcc 26 KB

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