1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006-2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file tr1/ell_integral.tcc
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882 TR1: 5.2 Special functions
40 // Written by Edward Smith-Rowland based on:
41 // (1) B. C. Carlson Numer. Math. 33, 1 (1979)
42 // (2) B. C. Carlson, Special Functions of Applied Mathematics (1977)
43 // (3) The Gnu Scientific Library, http://www.gnu.org/software/gsl
44 // (4) Numerical Recipes in C, 2nd ed, by W. H. Press, S. A. Teukolsky,
45 // W. T. Vetterling, B. P. Flannery, Cambridge University Press
46 // (1992), pp. 261-269
48 #ifndef _TR1_ELL_INTEGRAL_TCC
49 #define _TR1_ELL_INTEGRAL_TCC 1
53 _GLIBCXX_BEGIN_NAMESPACE(_GLIBCXX_TR1)
55 // [5.2] Special functions
58 * @ingroup tr1_math_spec_func
63 // Implementation-space details.
69 * @brief Return the Carlson elliptic function @f$ R_F(x,y,z) @f$
72 * The Carlson elliptic function of the first kind is defined by:
74 * R_F(x,y,z) = \frac{1}{2} \int_0^\infty
75 * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}}
78 * @param __x The first of three symmetric arguments.
79 * @param __y The second of three symmetric arguments.
80 * @param __z The third of three symmetric arguments.
81 * @return The Carlson elliptic function of the first kind.
83 template<typename _Tp>
85 __ellint_rf(const _Tp __x, const _Tp __y, const _Tp __z)
87 const _Tp __min = std::numeric_limits<_Tp>::min();
88 const _Tp __max = std::numeric_limits<_Tp>::max();
89 const _Tp __lolim = _Tp(5) * __min;
90 const _Tp __uplim = __max / _Tp(5);
92 if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
93 std::__throw_domain_error(__N("Argument less than zero "
95 else if (__x + __y < __lolim || __x + __z < __lolim
96 || __y + __z < __lolim)
97 std::__throw_domain_error(__N("Argument too small in __ellint_rf"));
100 const _Tp __c0 = _Tp(1) / _Tp(4);
101 const _Tp __c1 = _Tp(1) / _Tp(24);
102 const _Tp __c2 = _Tp(1) / _Tp(10);
103 const _Tp __c3 = _Tp(3) / _Tp(44);
104 const _Tp __c4 = _Tp(1) / _Tp(14);
110 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
111 const _Tp __errtol = std::pow(__eps, _Tp(1) / _Tp(6));
113 _Tp __xndev, __yndev, __zndev;
115 const unsigned int __max_iter = 100;
116 for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
118 __mu = (__xn + __yn + __zn) / _Tp(3);
119 __xndev = 2 - (__mu + __xn) / __mu;
120 __yndev = 2 - (__mu + __yn) / __mu;
121 __zndev = 2 - (__mu + __zn) / __mu;
122 _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
123 __epsilon = std::max(__epsilon, std::abs(__zndev));
124 if (__epsilon < __errtol)
126 const _Tp __xnroot = std::sqrt(__xn);
127 const _Tp __ynroot = std::sqrt(__yn);
128 const _Tp __znroot = std::sqrt(__zn);
129 const _Tp __lambda = __xnroot * (__ynroot + __znroot)
130 + __ynroot * __znroot;
131 __xn = __c0 * (__xn + __lambda);
132 __yn = __c0 * (__yn + __lambda);
133 __zn = __c0 * (__zn + __lambda);
136 const _Tp __e2 = __xndev * __yndev - __zndev * __zndev;
137 const _Tp __e3 = __xndev * __yndev * __zndev;
138 const _Tp __s = _Tp(1) + (__c1 * __e2 - __c2 - __c3 * __e3) * __e2
141 return __s / std::sqrt(__mu);
147 * @brief Return the complete elliptic integral of the first kind
148 * @f$ K(k) @f$ by series expansion.
150 * The complete elliptic integral of the first kind is defined as
152 * K(k) = F(k,\pi/2) = \int_0^{\pi/2}\frac{d\theta}
153 * {\sqrt{1 - k^2sin^2\theta}}
156 * This routine is not bad as long as |k| is somewhat smaller than 1
157 * but is not is good as the Carlson elliptic integral formulation.
159 * @param __k The argument of the complete elliptic function.
160 * @return The complete elliptic function of the first kind.
162 template<typename _Tp>
164 __comp_ellint_1_series(const _Tp __k)
167 const _Tp __kk = __k * __k;
169 _Tp __term = __kk / _Tp(4);
170 _Tp __sum = _Tp(1) + __term;
172 const unsigned int __max_iter = 1000;
173 for (unsigned int __i = 2; __i < __max_iter; ++__i)
175 __term *= (2 * __i - 1) * __kk / (2 * __i);
176 if (__term < std::numeric_limits<_Tp>::epsilon())
181 return __numeric_constants<_Tp>::__pi_2() * __sum;
186 * @brief Return the complete elliptic integral of the first kind
187 * @f$ K(k) @f$ using the Carlson formulation.
189 * The complete elliptic integral of the first kind is defined as
191 * K(k) = F(k,\pi/2) = \int_0^{\pi/2}\frac{d\theta}
192 * {\sqrt{1 - k^2 sin^2\theta}}
194 * where @f$ F(k,\phi) @f$ is the incomplete elliptic integral of the
197 * @param __k The argument of the complete elliptic function.
198 * @return The complete elliptic function of the first kind.
200 template<typename _Tp>
202 __comp_ellint_1(const _Tp __k)
206 return std::numeric_limits<_Tp>::quiet_NaN();
207 else if (std::abs(__k) >= _Tp(1))
208 return std::numeric_limits<_Tp>::quiet_NaN();
210 return __ellint_rf(_Tp(0), _Tp(1) - __k * __k, _Tp(1));
215 * @brief Return the incomplete elliptic integral of the first kind
216 * @f$ F(k,\phi) @f$ using the Carlson formulation.
218 * The incomplete elliptic integral of the first kind is defined as
220 * F(k,\phi) = \int_0^{\phi}\frac{d\theta}
221 * {\sqrt{1 - k^2 sin^2\theta}}
224 * @param __k The argument of the elliptic function.
225 * @param __phi The integral limit argument of the elliptic function.
226 * @return The elliptic function of the first kind.
228 template<typename _Tp>
230 __ellint_1(const _Tp __k, const _Tp __phi)
233 if (__isnan(__k) || __isnan(__phi))
234 return std::numeric_limits<_Tp>::quiet_NaN();
235 else if (std::abs(__k) > _Tp(1))
236 std::__throw_domain_error(__N("Bad argument in __ellint_1."));
239 // Reduce phi to -pi/2 < phi < +pi/2.
240 const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
242 const _Tp __phi_red = __phi
243 - __n * __numeric_constants<_Tp>::__pi();
245 const _Tp __s = std::sin(__phi_red);
246 const _Tp __c = std::cos(__phi_red);
249 * __ellint_rf(__c * __c,
250 _Tp(1) - __k * __k * __s * __s, _Tp(1));
255 return __F + _Tp(2) * __n * __comp_ellint_1(__k);
261 * @brief Return the complete elliptic integral of the second kind
262 * @f$ E(k) @f$ by series expansion.
264 * The complete elliptic integral of the second kind is defined as
266 * E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
269 * This routine is not bad as long as |k| is somewhat smaller than 1
270 * but is not is good as the Carlson elliptic integral formulation.
272 * @param __k The argument of the complete elliptic function.
273 * @return The complete elliptic function of the second kind.
275 template<typename _Tp>
277 __comp_ellint_2_series(const _Tp __k)
280 const _Tp __kk = __k * __k;
285 const unsigned int __max_iter = 1000;
286 for (unsigned int __i = 2; __i < __max_iter; ++__i)
288 const _Tp __i2m = 2 * __i - 1;
289 const _Tp __i2 = 2 * __i;
290 __term *= __i2m * __i2m * __kk / (__i2 * __i2);
291 if (__term < std::numeric_limits<_Tp>::epsilon())
293 __sum += __term / __i2m;
296 return __numeric_constants<_Tp>::__pi_2() * (_Tp(1) - __sum);
301 * @brief Return the Carlson elliptic function of the second kind
302 * @f$ R_D(x,y,z) = R_J(x,y,z,z) @f$ where
303 * @f$ R_J(x,y,z,p) @f$ is the Carlson elliptic function
306 * The Carlson elliptic function of the second kind is defined by:
308 * R_D(x,y,z) = \frac{3}{2} \int_0^\infty
309 * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{3/2}}
312 * Based on Carlson's algorithms:
313 * - B. C. Carlson Numer. Math. 33, 1 (1979)
314 * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
315 * - Nunerical Recipes in C, 2nd ed, pp. 261-269,
316 * by Press, Teukolsky, Vetterling, Flannery (1992)
318 * @param __x The first of two symmetric arguments.
319 * @param __y The second of two symmetric arguments.
320 * @param __z The third argument.
321 * @return The Carlson elliptic function of the second kind.
323 template<typename _Tp>
325 __ellint_rd(const _Tp __x, const _Tp __y, const _Tp __z)
327 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
328 const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
329 const _Tp __min = std::numeric_limits<_Tp>::min();
330 const _Tp __max = std::numeric_limits<_Tp>::max();
331 const _Tp __lolim = _Tp(2) / std::pow(__max, _Tp(2) / _Tp(3));
332 const _Tp __uplim = std::pow(_Tp(0.1L) * __errtol / __min, _Tp(2) / _Tp(3));
334 if (__x < _Tp(0) || __y < _Tp(0))
335 std::__throw_domain_error(__N("Argument less than zero "
337 else if (__x + __y < __lolim || __z < __lolim)
338 std::__throw_domain_error(__N("Argument too small "
342 const _Tp __c0 = _Tp(1) / _Tp(4);
343 const _Tp __c1 = _Tp(3) / _Tp(14);
344 const _Tp __c2 = _Tp(1) / _Tp(6);
345 const _Tp __c3 = _Tp(9) / _Tp(22);
346 const _Tp __c4 = _Tp(3) / _Tp(26);
351 _Tp __sigma = _Tp(0);
352 _Tp __power4 = _Tp(1);
355 _Tp __xndev, __yndev, __zndev;
357 const unsigned int __max_iter = 100;
358 for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
360 __mu = (__xn + __yn + _Tp(3) * __zn) / _Tp(5);
361 __xndev = (__mu - __xn) / __mu;
362 __yndev = (__mu - __yn) / __mu;
363 __zndev = (__mu - __zn) / __mu;
364 _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
365 __epsilon = std::max(__epsilon, std::abs(__zndev));
366 if (__epsilon < __errtol)
368 _Tp __xnroot = std::sqrt(__xn);
369 _Tp __ynroot = std::sqrt(__yn);
370 _Tp __znroot = std::sqrt(__zn);
371 _Tp __lambda = __xnroot * (__ynroot + __znroot)
372 + __ynroot * __znroot;
373 __sigma += __power4 / (__znroot * (__zn + __lambda));
375 __xn = __c0 * (__xn + __lambda);
376 __yn = __c0 * (__yn + __lambda);
377 __zn = __c0 * (__zn + __lambda);
380 _Tp __ea = __xndev * __yndev;
381 _Tp __eb = __zndev * __zndev;
382 _Tp __ec = __ea - __eb;
383 _Tp __ed = __ea - _Tp(6) * __eb;
384 _Tp __ef = __ed + __ec + __ec;
385 _Tp __s1 = __ed * (-__c1 + __c3 * __ed
386 / _Tp(3) - _Tp(3) * __c4 * __zndev * __ef
390 + __zndev * (-__c3 * __ec - __zndev * __c4 - __ea));
392 return _Tp(3) * __sigma + __power4 * (_Tp(1) + __s1 + __s2)
393 / (__mu * std::sqrt(__mu));
399 * @brief Return the complete elliptic integral of the second kind
400 * @f$ E(k) @f$ using the Carlson formulation.
402 * The complete elliptic integral of the second kind is defined as
404 * E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
407 * @param __k The argument of the complete elliptic function.
408 * @return The complete elliptic function of the second kind.
410 template<typename _Tp>
412 __comp_ellint_2(const _Tp __k)
416 return std::numeric_limits<_Tp>::quiet_NaN();
417 else if (std::abs(__k) == 1)
419 else if (std::abs(__k) > _Tp(1))
420 std::__throw_domain_error(__N("Bad argument in __comp_ellint_2."));
423 const _Tp __kk = __k * __k;
425 return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
426 - __kk * __ellint_rd(_Tp(0), _Tp(1) - __kk, _Tp(1)) / _Tp(3);
432 * @brief Return the incomplete elliptic integral of the second kind
433 * @f$ E(k,\phi) @f$ using the Carlson formulation.
435 * The incomplete elliptic integral of the second kind is defined as
437 * E(k,\phi) = \int_0^{\phi} \sqrt{1 - k^2 sin^2\theta}
440 * @param __k The argument of the elliptic function.
441 * @param __phi The integral limit argument of the elliptic function.
442 * @return The elliptic function of the second kind.
444 template<typename _Tp>
446 __ellint_2(const _Tp __k, const _Tp __phi)
449 if (__isnan(__k) || __isnan(__phi))
450 return std::numeric_limits<_Tp>::quiet_NaN();
451 else if (std::abs(__k) > _Tp(1))
452 std::__throw_domain_error(__N("Bad argument in __ellint_2."));
455 // Reduce phi to -pi/2 < phi < +pi/2.
456 const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
458 const _Tp __phi_red = __phi
459 - __n * __numeric_constants<_Tp>::__pi();
461 const _Tp __kk = __k * __k;
462 const _Tp __s = std::sin(__phi_red);
463 const _Tp __ss = __s * __s;
464 const _Tp __sss = __ss * __s;
465 const _Tp __c = std::cos(__phi_red);
466 const _Tp __cc = __c * __c;
469 * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
471 * __ellint_rd(__cc, _Tp(1) - __kk * __ss, _Tp(1))
477 return __E + _Tp(2) * __n * __comp_ellint_2(__k);
483 * @brief Return the Carlson elliptic function
484 * @f$ R_C(x,y) = R_F(x,y,y) @f$ where @f$ R_F(x,y,z) @f$
485 * is the Carlson elliptic function of the first kind.
487 * The Carlson elliptic function is defined by:
489 * R_C(x,y) = \frac{1}{2} \int_0^\infty
490 * \frac{dt}{(t + x)^{1/2}(t + y)}
493 * Based on Carlson's algorithms:
494 * - B. C. Carlson Numer. Math. 33, 1 (1979)
495 * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
496 * - Nunerical Recipes in C, 2nd ed, pp. 261-269,
497 * by Press, Teukolsky, Vetterling, Flannery (1992)
499 * @param __x The first argument.
500 * @param __y The second argument.
501 * @return The Carlson elliptic function.
503 template<typename _Tp>
505 __ellint_rc(const _Tp __x, const _Tp __y)
507 const _Tp __min = std::numeric_limits<_Tp>::min();
508 const _Tp __max = std::numeric_limits<_Tp>::max();
509 const _Tp __lolim = _Tp(5) * __min;
510 const _Tp __uplim = __max / _Tp(5);
512 if (__x < _Tp(0) || __y < _Tp(0) || __x + __y < __lolim)
513 std::__throw_domain_error(__N("Argument less than zero "
517 const _Tp __c0 = _Tp(1) / _Tp(4);
518 const _Tp __c1 = _Tp(1) / _Tp(7);
519 const _Tp __c2 = _Tp(9) / _Tp(22);
520 const _Tp __c3 = _Tp(3) / _Tp(10);
521 const _Tp __c4 = _Tp(3) / _Tp(8);
526 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
527 const _Tp __errtol = std::pow(__eps / _Tp(30), _Tp(1) / _Tp(6));
531 const unsigned int __max_iter = 100;
532 for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
534 __mu = (__xn + _Tp(2) * __yn) / _Tp(3);
535 __sn = (__yn + __mu) / __mu - _Tp(2);
536 if (std::abs(__sn) < __errtol)
538 const _Tp __lambda = _Tp(2) * std::sqrt(__xn) * std::sqrt(__yn)
540 __xn = __c0 * (__xn + __lambda);
541 __yn = __c0 * (__yn + __lambda);
544 _Tp __s = __sn * __sn
545 * (__c3 + __sn*(__c1 + __sn * (__c4 + __sn * __c2)));
547 return (_Tp(1) + __s) / std::sqrt(__mu);
553 * @brief Return the Carlson elliptic function @f$ R_J(x,y,z,p) @f$
556 * The Carlson elliptic function of the third kind is defined by:
558 * R_J(x,y,z,p) = \frac{3}{2} \int_0^\infty
559 * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}(t + p)}
562 * Based on Carlson's algorithms:
563 * - B. C. Carlson Numer. Math. 33, 1 (1979)
564 * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
565 * - Nunerical Recipes in C, 2nd ed, pp. 261-269,
566 * by Press, Teukolsky, Vetterling, Flannery (1992)
568 * @param __x The first of three symmetric arguments.
569 * @param __y The second of three symmetric arguments.
570 * @param __z The third of three symmetric arguments.
571 * @param __p The fourth argument.
572 * @return The Carlson elliptic function of the fourth kind.
574 template<typename _Tp>
576 __ellint_rj(const _Tp __x, const _Tp __y, const _Tp __z, const _Tp __p)
578 const _Tp __min = std::numeric_limits<_Tp>::min();
579 const _Tp __max = std::numeric_limits<_Tp>::max();
580 const _Tp __lolim = std::pow(_Tp(5) * __min, _Tp(1)/_Tp(3));
581 const _Tp __uplim = _Tp(0.3L)
582 * std::pow(_Tp(0.2L) * __max, _Tp(1)/_Tp(3));
584 if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
585 std::__throw_domain_error(__N("Argument less than zero "
587 else if (__x + __y < __lolim || __x + __z < __lolim
588 || __y + __z < __lolim || __p < __lolim)
589 std::__throw_domain_error(__N("Argument too small "
593 const _Tp __c0 = _Tp(1) / _Tp(4);
594 const _Tp __c1 = _Tp(3) / _Tp(14);
595 const _Tp __c2 = _Tp(1) / _Tp(3);
596 const _Tp __c3 = _Tp(3) / _Tp(22);
597 const _Tp __c4 = _Tp(3) / _Tp(26);
603 _Tp __sigma = _Tp(0);
604 _Tp __power4 = _Tp(1);
606 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
607 const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
610 _Tp __xndev, __yndev, __zndev, __pndev;
612 const unsigned int __max_iter = 100;
613 for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
615 __mu = (__xn + __yn + __zn + _Tp(2) * __pn) / _Tp(5);
616 __xndev = (__mu - __xn) / __mu;
617 __yndev = (__mu - __yn) / __mu;
618 __zndev = (__mu - __zn) / __mu;
619 __pndev = (__mu - __pn) / __mu;
620 _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
621 __epsilon = std::max(__epsilon, std::abs(__zndev));
622 __epsilon = std::max(__epsilon, std::abs(__pndev));
623 if (__epsilon < __errtol)
625 const _Tp __xnroot = std::sqrt(__xn);
626 const _Tp __ynroot = std::sqrt(__yn);
627 const _Tp __znroot = std::sqrt(__zn);
628 const _Tp __lambda = __xnroot * (__ynroot + __znroot)
629 + __ynroot * __znroot;
630 const _Tp __alpha1 = __pn * (__xnroot + __ynroot + __znroot)
631 + __xnroot * __ynroot * __znroot;
632 const _Tp __alpha2 = __alpha1 * __alpha1;
633 const _Tp __beta = __pn * (__pn + __lambda)
635 __sigma += __power4 * __ellint_rc(__alpha2, __beta);
637 __xn = __c0 * (__xn + __lambda);
638 __yn = __c0 * (__yn + __lambda);
639 __zn = __c0 * (__zn + __lambda);
640 __pn = __c0 * (__pn + __lambda);
643 _Tp __ea = __xndev * (__yndev + __zndev) + __yndev * __zndev;
644 _Tp __eb = __xndev * __yndev * __zndev;
645 _Tp __ec = __pndev * __pndev;
646 _Tp __e2 = __ea - _Tp(3) * __ec;
647 _Tp __e3 = __eb + _Tp(2) * __pndev * (__ea - __ec);
648 _Tp __s1 = _Tp(1) + __e2 * (-__c1 + _Tp(3) * __c3 * __e2 / _Tp(4)
649 - _Tp(3) * __c4 * __e3 / _Tp(2));
650 _Tp __s2 = __eb * (__c2 / _Tp(2)
651 + __pndev * (-__c3 - __c3 + __pndev * __c4));
652 _Tp __s3 = __pndev * __ea * (__c2 - __pndev * __c3)
653 - __c2 * __pndev * __ec;
655 return _Tp(3) * __sigma + __power4 * (__s1 + __s2 + __s3)
656 / (__mu * std::sqrt(__mu));
662 * @brief Return the complete elliptic integral of the third kind
663 * @f$ \Pi(k,\nu) = \Pi(k,\nu,\pi/2) @f$ using the
664 * Carlson formulation.
666 * The complete elliptic integral of the third kind is defined as
668 * \Pi(k,\nu) = \int_0^{\pi/2}
670 * {(1 - \nu \sin^2\theta)\sqrt{1 - k^2 \sin^2\theta}}
673 * @param __k The argument of the elliptic function.
674 * @param __nu The second argument of the elliptic function.
675 * @return The complete elliptic function of the third kind.
677 template<typename _Tp>
679 __comp_ellint_3(const _Tp __k, const _Tp __nu)
682 if (__isnan(__k) || __isnan(__nu))
683 return std::numeric_limits<_Tp>::quiet_NaN();
684 else if (__nu == _Tp(1))
685 return std::numeric_limits<_Tp>::infinity();
686 else if (std::abs(__k) > _Tp(1))
687 std::__throw_domain_error(__N("Bad argument in __comp_ellint_3."));
690 const _Tp __kk = __k * __k;
692 return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
694 * __ellint_rj(_Tp(0), _Tp(1) - __kk, _Tp(1), _Tp(1) + __nu)
701 * @brief Return the incomplete elliptic integral of the third kind
702 * @f$ \Pi(k,\nu,\phi) @f$ using the Carlson formulation.
704 * The incomplete elliptic integral of the third kind is defined as
706 * \Pi(k,\nu,\phi) = \int_0^{\phi}
708 * {(1 - \nu \sin^2\theta)
709 * \sqrt{1 - k^2 \sin^2\theta}}
712 * @param __k The argument of the elliptic function.
713 * @param __nu The second argument of the elliptic function.
714 * @param __phi The integral limit argument of the elliptic function.
715 * @return The elliptic function of the third kind.
717 template<typename _Tp>
719 __ellint_3(const _Tp __k, const _Tp __nu, const _Tp __phi)
722 if (__isnan(__k) || __isnan(__nu) || __isnan(__phi))
723 return std::numeric_limits<_Tp>::quiet_NaN();
724 else if (std::abs(__k) > _Tp(1))
725 std::__throw_domain_error(__N("Bad argument in __ellint_3."));
728 // Reduce phi to -pi/2 < phi < +pi/2.
729 const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
731 const _Tp __phi_red = __phi
732 - __n * __numeric_constants<_Tp>::__pi();
734 const _Tp __kk = __k * __k;
735 const _Tp __s = std::sin(__phi_red);
736 const _Tp __ss = __s * __s;
737 const _Tp __sss = __ss * __s;
738 const _Tp __c = std::cos(__phi_red);
739 const _Tp __cc = __c * __c;
742 * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
744 * __ellint_rj(__cc, _Tp(1) - __kk * __ss, _Tp(1),
745 _Tp(1) + __nu * __ss) / _Tp(3);
750 return __Pi + _Tp(2) * __n * __comp_ellint_3(__k, __nu);
754 } // namespace std::tr1::__detail
756 /* @} */ // group tr1_math_spec_func
758 _GLIBCXX_END_NAMESPACE
761 #endif // _TR1_ELL_INTEGRAL_TCC