You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

756 lines
27KB

  1. // Special functions -*- C++ -*-
  2. // Copyright (C) 2006-2020 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. _Tp __ea = __xndev * __yndev;
  331. _Tp __eb = __zndev * __zndev;
  332. _Tp __ec = __ea - __eb;
  333. _Tp __ed = __ea - _Tp(6) * __eb;
  334. _Tp __ef = __ed + __ec + __ec;
  335. _Tp __s1 = __ed * (-__c1 + __c3 * __ed
  336. / _Tp(3) - _Tp(3) * __c4 * __zndev * __ef
  337. / _Tp(2));
  338. _Tp __s2 = __zndev
  339. * (__c2 * __ef
  340. + __zndev * (-__c3 * __ec - __zndev * __c4 - __ea));
  341. return _Tp(3) * __sigma + __power4 * (_Tp(1) + __s1 + __s2)
  342. / (__mu * std::sqrt(__mu));
  343. }
  344. }
  345. /**
  346. * @brief Return the complete elliptic integral of the second kind
  347. * @f$ E(k) @f$ using the Carlson formulation.
  348. *
  349. * The complete elliptic integral of the second kind is defined as
  350. * @f[
  351. * E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
  352. * @f]
  353. *
  354. * @param __k The argument of the complete elliptic function.
  355. * @return The complete elliptic function of the second kind.
  356. */
  357. template<typename _Tp>
  358. _Tp
  359. __comp_ellint_2(_Tp __k)
  360. {
  361. if (__isnan(__k))
  362. return std::numeric_limits<_Tp>::quiet_NaN();
  363. else if (std::abs(__k) == 1)
  364. return _Tp(1);
  365. else if (std::abs(__k) > _Tp(1))
  366. std::__throw_domain_error(__N("Bad argument in __comp_ellint_2."));
  367. else
  368. {
  369. const _Tp __kk = __k * __k;
  370. return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
  371. - __kk * __ellint_rd(_Tp(0), _Tp(1) - __kk, _Tp(1)) / _Tp(3);
  372. }
  373. }
  374. /**
  375. * @brief Return the incomplete elliptic integral of the second kind
  376. * @f$ E(k,\phi) @f$ using the Carlson formulation.
  377. *
  378. * The incomplete elliptic integral of the second kind is defined as
  379. * @f[
  380. * E(k,\phi) = \int_0^{\phi} \sqrt{1 - k^2 sin^2\theta}
  381. * @f]
  382. *
  383. * @param __k The argument of the elliptic function.
  384. * @param __phi The integral limit argument of the elliptic function.
  385. * @return The elliptic function of the second kind.
  386. */
  387. template<typename _Tp>
  388. _Tp
  389. __ellint_2(_Tp __k, _Tp __phi)
  390. {
  391. if (__isnan(__k) || __isnan(__phi))
  392. return std::numeric_limits<_Tp>::quiet_NaN();
  393. else if (std::abs(__k) > _Tp(1))
  394. std::__throw_domain_error(__N("Bad argument in __ellint_2."));
  395. else
  396. {
  397. // Reduce phi to -pi/2 < phi < +pi/2.
  398. const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
  399. + _Tp(0.5L));
  400. const _Tp __phi_red = __phi
  401. - __n * __numeric_constants<_Tp>::__pi();
  402. const _Tp __kk = __k * __k;
  403. const _Tp __s = std::sin(__phi_red);
  404. const _Tp __ss = __s * __s;
  405. const _Tp __sss = __ss * __s;
  406. const _Tp __c = std::cos(__phi_red);
  407. const _Tp __cc = __c * __c;
  408. const _Tp __E = __s
  409. * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
  410. - __kk * __sss
  411. * __ellint_rd(__cc, _Tp(1) - __kk * __ss, _Tp(1))
  412. / _Tp(3);
  413. if (__n == 0)
  414. return __E;
  415. else
  416. return __E + _Tp(2) * __n * __comp_ellint_2(__k);
  417. }
  418. }
  419. /**
  420. * @brief Return the Carlson elliptic function
  421. * @f$ R_C(x,y) = R_F(x,y,y) @f$ where @f$ R_F(x,y,z) @f$
  422. * is the Carlson elliptic function of the first kind.
  423. *
  424. * The Carlson elliptic function is defined by:
  425. * @f[
  426. * R_C(x,y) = \frac{1}{2} \int_0^\infty
  427. * \frac{dt}{(t + x)^{1/2}(t + y)}
  428. * @f]
  429. *
  430. * Based on Carlson's algorithms:
  431. * - B. C. Carlson Numer. Math. 33, 1 (1979)
  432. * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
  433. * - Numerical Recipes in C, 2nd ed, pp. 261-269,
  434. * by Press, Teukolsky, Vetterling, Flannery (1992)
  435. *
  436. * @param __x The first argument.
  437. * @param __y The second argument.
  438. * @return The Carlson elliptic function.
  439. */
  440. template<typename _Tp>
  441. _Tp
  442. __ellint_rc(_Tp __x, _Tp __y)
  443. {
  444. const _Tp __min = std::numeric_limits<_Tp>::min();
  445. const _Tp __max = std::numeric_limits<_Tp>::max();
  446. const _Tp __lolim = _Tp(5) * __min;
  447. const _Tp __uplim = __max / _Tp(5);
  448. if (__x < _Tp(0) || __y < _Tp(0) || __x + __y < __lolim)
  449. std::__throw_domain_error(__N("Argument less than zero "
  450. "in __ellint_rc."));
  451. else
  452. {
  453. const _Tp __c0 = _Tp(1) / _Tp(4);
  454. const _Tp __c1 = _Tp(1) / _Tp(7);
  455. const _Tp __c2 = _Tp(9) / _Tp(22);
  456. const _Tp __c3 = _Tp(3) / _Tp(10);
  457. const _Tp __c4 = _Tp(3) / _Tp(8);
  458. _Tp __xn = __x;
  459. _Tp __yn = __y;
  460. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  461. const _Tp __errtol = std::pow(__eps / _Tp(30), _Tp(1) / _Tp(6));
  462. _Tp __mu;
  463. _Tp __sn;
  464. const unsigned int __max_iter = 100;
  465. for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
  466. {
  467. __mu = (__xn + _Tp(2) * __yn) / _Tp(3);
  468. __sn = (__yn + __mu) / __mu - _Tp(2);
  469. if (std::abs(__sn) < __errtol)
  470. break;
  471. const _Tp __lambda = _Tp(2) * std::sqrt(__xn) * std::sqrt(__yn)
  472. + __yn;
  473. __xn = __c0 * (__xn + __lambda);
  474. __yn = __c0 * (__yn + __lambda);
  475. }
  476. _Tp __s = __sn * __sn
  477. * (__c3 + __sn*(__c1 + __sn * (__c4 + __sn * __c2)));
  478. return (_Tp(1) + __s) / std::sqrt(__mu);
  479. }
  480. }
  481. /**
  482. * @brief Return the Carlson elliptic function @f$ R_J(x,y,z,p) @f$
  483. * of the third kind.
  484. *
  485. * The Carlson elliptic function of the third kind is defined by:
  486. * @f[
  487. * R_J(x,y,z,p) = \frac{3}{2} \int_0^\infty
  488. * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}(t + p)}
  489. * @f]
  490. *
  491. * Based on Carlson's algorithms:
  492. * - B. C. Carlson Numer. Math. 33, 1 (1979)
  493. * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
  494. * - Numerical Recipes in C, 2nd ed, pp. 261-269,
  495. * by Press, Teukolsky, Vetterling, Flannery (1992)
  496. *
  497. * @param __x The first of three symmetric arguments.
  498. * @param __y The second of three symmetric arguments.
  499. * @param __z The third of three symmetric arguments.
  500. * @param __p The fourth argument.
  501. * @return The Carlson elliptic function of the fourth kind.
  502. */
  503. template<typename _Tp>
  504. _Tp
  505. __ellint_rj(_Tp __x, _Tp __y, _Tp __z, _Tp __p)
  506. {
  507. const _Tp __min = std::numeric_limits<_Tp>::min();
  508. const _Tp __max = std::numeric_limits<_Tp>::max();
  509. const _Tp __lolim = std::pow(_Tp(5) * __min, _Tp(1)/_Tp(3));
  510. const _Tp __uplim = _Tp(0.3L)
  511. * std::pow(_Tp(0.2L) * __max, _Tp(1)/_Tp(3));
  512. if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
  513. std::__throw_domain_error(__N("Argument less than zero "
  514. "in __ellint_rj."));
  515. else if (__x + __y < __lolim || __x + __z < __lolim
  516. || __y + __z < __lolim || __p < __lolim)
  517. std::__throw_domain_error(__N("Argument too small "
  518. "in __ellint_rj"));
  519. else
  520. {
  521. const _Tp __c0 = _Tp(1) / _Tp(4);
  522. const _Tp __c1 = _Tp(3) / _Tp(14);
  523. const _Tp __c2 = _Tp(1) / _Tp(3);
  524. const _Tp __c3 = _Tp(3) / _Tp(22);
  525. const _Tp __c4 = _Tp(3) / _Tp(26);
  526. _Tp __xn = __x;
  527. _Tp __yn = __y;
  528. _Tp __zn = __z;
  529. _Tp __pn = __p;
  530. _Tp __sigma = _Tp(0);
  531. _Tp __power4 = _Tp(1);
  532. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  533. const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
  534. _Tp __lambda, __mu;
  535. _Tp __xndev, __yndev, __zndev, __pndev;
  536. const unsigned int __max_iter = 100;
  537. for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
  538. {
  539. __mu = (__xn + __yn + __zn + _Tp(2) * __pn) / _Tp(5);
  540. __xndev = (__mu - __xn) / __mu;
  541. __yndev = (__mu - __yn) / __mu;
  542. __zndev = (__mu - __zn) / __mu;
  543. __pndev = (__mu - __pn) / __mu;
  544. _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
  545. __epsilon = std::max(__epsilon, std::abs(__zndev));
  546. __epsilon = std::max(__epsilon, std::abs(__pndev));
  547. if (__epsilon < __errtol)
  548. break;
  549. const _Tp __xnroot = std::sqrt(__xn);
  550. const _Tp __ynroot = std::sqrt(__yn);
  551. const _Tp __znroot = std::sqrt(__zn);
  552. const _Tp __lambda = __xnroot * (__ynroot + __znroot)
  553. + __ynroot * __znroot;
  554. const _Tp __alpha1 = __pn * (__xnroot + __ynroot + __znroot)
  555. + __xnroot * __ynroot * __znroot;
  556. const _Tp __alpha2 = __alpha1 * __alpha1;
  557. const _Tp __beta = __pn * (__pn + __lambda)
  558. * (__pn + __lambda);
  559. __sigma += __power4 * __ellint_rc(__alpha2, __beta);
  560. __power4 *= __c0;
  561. __xn = __c0 * (__xn + __lambda);
  562. __yn = __c0 * (__yn + __lambda);
  563. __zn = __c0 * (__zn + __lambda);
  564. __pn = __c0 * (__pn + __lambda);
  565. }
  566. _Tp __ea = __xndev * (__yndev + __zndev) + __yndev * __zndev;
  567. _Tp __eb = __xndev * __yndev * __zndev;
  568. _Tp __ec = __pndev * __pndev;
  569. _Tp __e2 = __ea - _Tp(3) * __ec;
  570. _Tp __e3 = __eb + _Tp(2) * __pndev * (__ea - __ec);
  571. _Tp __s1 = _Tp(1) + __e2 * (-__c1 + _Tp(3) * __c3 * __e2 / _Tp(4)
  572. - _Tp(3) * __c4 * __e3 / _Tp(2));
  573. _Tp __s2 = __eb * (__c2 / _Tp(2)
  574. + __pndev * (-__c3 - __c3 + __pndev * __c4));
  575. _Tp __s3 = __pndev * __ea * (__c2 - __pndev * __c3)
  576. - __c2 * __pndev * __ec;
  577. return _Tp(3) * __sigma + __power4 * (__s1 + __s2 + __s3)
  578. / (__mu * std::sqrt(__mu));
  579. }
  580. }
  581. /**
  582. * @brief Return the complete elliptic integral of the third kind
  583. * @f$ \Pi(k,\nu) = \Pi(k,\nu,\pi/2) @f$ using the
  584. * Carlson formulation.
  585. *
  586. * The complete elliptic integral of the third kind is defined as
  587. * @f[
  588. * \Pi(k,\nu) = \int_0^{\pi/2}
  589. * \frac{d\theta}
  590. * {(1 - \nu \sin^2\theta)\sqrt{1 - k^2 \sin^2\theta}}
  591. * @f]
  592. *
  593. * @param __k The argument of the elliptic function.
  594. * @param __nu The second argument of the elliptic function.
  595. * @return The complete elliptic function of the third kind.
  596. */
  597. template<typename _Tp>
  598. _Tp
  599. __comp_ellint_3(_Tp __k, _Tp __nu)
  600. {
  601. if (__isnan(__k) || __isnan(__nu))
  602. return std::numeric_limits<_Tp>::quiet_NaN();
  603. else if (__nu == _Tp(1))
  604. return std::numeric_limits<_Tp>::infinity();
  605. else if (std::abs(__k) > _Tp(1))
  606. std::__throw_domain_error(__N("Bad argument in __comp_ellint_3."));
  607. else
  608. {
  609. const _Tp __kk = __k * __k;
  610. return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
  611. + __nu
  612. * __ellint_rj(_Tp(0), _Tp(1) - __kk, _Tp(1), _Tp(1) - __nu)
  613. / _Tp(3);
  614. }
  615. }
  616. /**
  617. * @brief Return the incomplete elliptic integral of the third kind
  618. * @f$ \Pi(k,\nu,\phi) @f$ using the Carlson formulation.
  619. *
  620. * The incomplete elliptic integral of the third kind is defined as
  621. * @f[
  622. * \Pi(k,\nu,\phi) = \int_0^{\phi}
  623. * \frac{d\theta}
  624. * {(1 - \nu \sin^2\theta)
  625. * \sqrt{1 - k^2 \sin^2\theta}}
  626. * @f]
  627. *
  628. * @param __k The argument of the elliptic function.
  629. * @param __nu The second argument of the elliptic function.
  630. * @param __phi The integral limit argument of the elliptic function.
  631. * @return The elliptic function of the third kind.
  632. */
  633. template<typename _Tp>
  634. _Tp
  635. __ellint_3(_Tp __k, _Tp __nu, _Tp __phi)
  636. {
  637. if (__isnan(__k) || __isnan(__nu) || __isnan(__phi))
  638. return std::numeric_limits<_Tp>::quiet_NaN();
  639. else if (std::abs(__k) > _Tp(1))
  640. std::__throw_domain_error(__N("Bad argument in __ellint_3."));
  641. else
  642. {
  643. // Reduce phi to -pi/2 < phi < +pi/2.
  644. const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
  645. + _Tp(0.5L));
  646. const _Tp __phi_red = __phi
  647. - __n * __numeric_constants<_Tp>::__pi();
  648. const _Tp __kk = __k * __k;
  649. const _Tp __s = std::sin(__phi_red);
  650. const _Tp __ss = __s * __s;
  651. const _Tp __sss = __ss * __s;
  652. const _Tp __c = std::cos(__phi_red);
  653. const _Tp __cc = __c * __c;
  654. const _Tp __Pi = __s
  655. * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
  656. + __nu * __sss
  657. * __ellint_rj(__cc, _Tp(1) - __kk * __ss, _Tp(1),
  658. _Tp(1) - __nu * __ss) / _Tp(3);
  659. if (__n == 0)
  660. return __Pi;
  661. else
  662. return __Pi + _Tp(2) * __n * __comp_ellint_3(__k, __nu);
  663. }
  664. }
  665. } // namespace __detail
  666. #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
  667. } // namespace tr1
  668. #endif
  669. _GLIBCXX_END_NAMESPACE_VERSION
  670. }
  671. #endif // _GLIBCXX_TR1_ELL_INTEGRAL_TCC