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 _GLIBCXX_TR1_ELL_INTEGRAL_TCC
49 #define _GLIBCXX_TR1_ELL_INTEGRAL_TCC 1
56 // [5.2] Special functions
59 * @ingroup tr1_math_spec_func
64 // Implementation-space details.
70 * @brief Return the Carlson elliptic function @f$ R_F(x,y,z) @f$
73 * The Carlson elliptic function of the first kind is defined by:
75 * R_F(x,y,z) = \frac{1}{2} \int_0^\infty
76 * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}}
79 * @param __x The first of three symmetric arguments.
80 * @param __y The second of three symmetric arguments.
81 * @param __z The third of three symmetric arguments.
82 * @return The Carlson elliptic function of the first kind.
84 template<typename _Tp>
86 __ellint_rf(const _Tp __x, const _Tp __y, const _Tp __z)
88 const _Tp __min = std::numeric_limits<_Tp>::min();
89 const _Tp __max = std::numeric_limits<_Tp>::max();
90 const _Tp __lolim = _Tp(5) * __min;
91 const _Tp __uplim = __max / _Tp(5);
93 if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
94 std::__throw_domain_error(__N("Argument less than zero "
96 else if (__x + __y < __lolim || __x + __z < __lolim
97 || __y + __z < __lolim)
98 std::__throw_domain_error(__N("Argument too small in __ellint_rf"));
101 const _Tp __c0 = _Tp(1) / _Tp(4);
102 const _Tp __c1 = _Tp(1) / _Tp(24);
103 const _Tp __c2 = _Tp(1) / _Tp(10);
104 const _Tp __c3 = _Tp(3) / _Tp(44);
105 const _Tp __c4 = _Tp(1) / _Tp(14);
111 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
112 const _Tp __errtol = std::pow(__eps, _Tp(1) / _Tp(6));
114 _Tp __xndev, __yndev, __zndev;
116 const unsigned int __max_iter = 100;
117 for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
119 __mu = (__xn + __yn + __zn) / _Tp(3);
120 __xndev = 2 - (__mu + __xn) / __mu;
121 __yndev = 2 - (__mu + __yn) / __mu;
122 __zndev = 2 - (__mu + __zn) / __mu;
123 _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
124 __epsilon = std::max(__epsilon, std::abs(__zndev));
125 if (__epsilon < __errtol)
127 const _Tp __xnroot = std::sqrt(__xn);
128 const _Tp __ynroot = std::sqrt(__yn);
129 const _Tp __znroot = std::sqrt(__zn);
130 const _Tp __lambda = __xnroot * (__ynroot + __znroot)
131 + __ynroot * __znroot;
132 __xn = __c0 * (__xn + __lambda);
133 __yn = __c0 * (__yn + __lambda);
134 __zn = __c0 * (__zn + __lambda);
137 const _Tp __e2 = __xndev * __yndev - __zndev * __zndev;
138 const _Tp __e3 = __xndev * __yndev * __zndev;
139 const _Tp __s = _Tp(1) + (__c1 * __e2 - __c2 - __c3 * __e3) * __e2
142 return __s / std::sqrt(__mu);
148 * @brief Return the complete elliptic integral of the first kind
149 * @f$ K(k) @f$ by series expansion.
151 * The complete elliptic integral of the first kind is defined as
153 * K(k) = F(k,\pi/2) = \int_0^{\pi/2}\frac{d\theta}
154 * {\sqrt{1 - k^2sin^2\theta}}
157 * This routine is not bad as long as |k| is somewhat smaller than 1
158 * but is not is good as the Carlson elliptic integral formulation.
160 * @param __k The argument of the complete elliptic function.
161 * @return The complete elliptic function of the first kind.
163 template<typename _Tp>
165 __comp_ellint_1_series(const _Tp __k)
168 const _Tp __kk = __k * __k;
170 _Tp __term = __kk / _Tp(4);
171 _Tp __sum = _Tp(1) + __term;
173 const unsigned int __max_iter = 1000;
174 for (unsigned int __i = 2; __i < __max_iter; ++__i)
176 __term *= (2 * __i - 1) * __kk / (2 * __i);
177 if (__term < std::numeric_limits<_Tp>::epsilon())
182 return __numeric_constants<_Tp>::__pi_2() * __sum;
187 * @brief Return the complete elliptic integral of the first kind
188 * @f$ K(k) @f$ using the Carlson formulation.
190 * The complete elliptic integral of the first kind is defined as
192 * K(k) = F(k,\pi/2) = \int_0^{\pi/2}\frac{d\theta}
193 * {\sqrt{1 - k^2 sin^2\theta}}
195 * where @f$ F(k,\phi) @f$ is the incomplete elliptic integral of the
198 * @param __k The argument of the complete elliptic function.
199 * @return The complete elliptic function of the first kind.
201 template<typename _Tp>
203 __comp_ellint_1(const _Tp __k)
207 return std::numeric_limits<_Tp>::quiet_NaN();
208 else if (std::abs(__k) >= _Tp(1))
209 return std::numeric_limits<_Tp>::quiet_NaN();
211 return __ellint_rf(_Tp(0), _Tp(1) - __k * __k, _Tp(1));
216 * @brief Return the incomplete elliptic integral of the first kind
217 * @f$ F(k,\phi) @f$ using the Carlson formulation.
219 * The incomplete elliptic integral of the first kind is defined as
221 * F(k,\phi) = \int_0^{\phi}\frac{d\theta}
222 * {\sqrt{1 - k^2 sin^2\theta}}
225 * @param __k The argument of the elliptic function.
226 * @param __phi The integral limit argument of the elliptic function.
227 * @return The elliptic function of the first kind.
229 template<typename _Tp>
231 __ellint_1(const _Tp __k, const _Tp __phi)
234 if (__isnan(__k) || __isnan(__phi))
235 return std::numeric_limits<_Tp>::quiet_NaN();
236 else if (std::abs(__k) > _Tp(1))
237 std::__throw_domain_error(__N("Bad argument in __ellint_1."));
240 // Reduce phi to -pi/2 < phi < +pi/2.
241 const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
243 const _Tp __phi_red = __phi
244 - __n * __numeric_constants<_Tp>::__pi();
246 const _Tp __s = std::sin(__phi_red);
247 const _Tp __c = std::cos(__phi_red);
250 * __ellint_rf(__c * __c,
251 _Tp(1) - __k * __k * __s * __s, _Tp(1));
256 return __F + _Tp(2) * __n * __comp_ellint_1(__k);
262 * @brief Return the complete elliptic integral of the second kind
263 * @f$ E(k) @f$ by series expansion.
265 * The complete elliptic integral of the second kind is defined as
267 * E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
270 * This routine is not bad as long as |k| is somewhat smaller than 1
271 * but is not is good as the Carlson elliptic integral formulation.
273 * @param __k The argument of the complete elliptic function.
274 * @return The complete elliptic function of the second kind.
276 template<typename _Tp>
278 __comp_ellint_2_series(const _Tp __k)
281 const _Tp __kk = __k * __k;
286 const unsigned int __max_iter = 1000;
287 for (unsigned int __i = 2; __i < __max_iter; ++__i)
289 const _Tp __i2m = 2 * __i - 1;
290 const _Tp __i2 = 2 * __i;
291 __term *= __i2m * __i2m * __kk / (__i2 * __i2);
292 if (__term < std::numeric_limits<_Tp>::epsilon())
294 __sum += __term / __i2m;
297 return __numeric_constants<_Tp>::__pi_2() * (_Tp(1) - __sum);
302 * @brief Return the Carlson elliptic function of the second kind
303 * @f$ R_D(x,y,z) = R_J(x,y,z,z) @f$ where
304 * @f$ R_J(x,y,z,p) @f$ is the Carlson elliptic function
307 * The Carlson elliptic function of the second kind is defined by:
309 * R_D(x,y,z) = \frac{3}{2} \int_0^\infty
310 * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{3/2}}
313 * Based on Carlson's algorithms:
314 * - B. C. Carlson Numer. Math. 33, 1 (1979)
315 * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
316 * - Nunerical Recipes in C, 2nd ed, pp. 261-269,
317 * by Press, Teukolsky, Vetterling, Flannery (1992)
319 * @param __x The first of two symmetric arguments.
320 * @param __y The second of two symmetric arguments.
321 * @param __z The third argument.
322 * @return The Carlson elliptic function of the second kind.
324 template<typename _Tp>
326 __ellint_rd(const _Tp __x, const _Tp __y, const _Tp __z)
328 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
329 const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
330 const _Tp __min = std::numeric_limits<_Tp>::min();
331 const _Tp __max = std::numeric_limits<_Tp>::max();
332 const _Tp __lolim = _Tp(2) / std::pow(__max, _Tp(2) / _Tp(3));
333 const _Tp __uplim = std::pow(_Tp(0.1L) * __errtol / __min, _Tp(2) / _Tp(3));
335 if (__x < _Tp(0) || __y < _Tp(0))
336 std::__throw_domain_error(__N("Argument less than zero "
338 else if (__x + __y < __lolim || __z < __lolim)
339 std::__throw_domain_error(__N("Argument too small "
343 const _Tp __c0 = _Tp(1) / _Tp(4);
344 const _Tp __c1 = _Tp(3) / _Tp(14);
345 const _Tp __c2 = _Tp(1) / _Tp(6);
346 const _Tp __c3 = _Tp(9) / _Tp(22);
347 const _Tp __c4 = _Tp(3) / _Tp(26);
352 _Tp __sigma = _Tp(0);
353 _Tp __power4 = _Tp(1);
356 _Tp __xndev, __yndev, __zndev;
358 const unsigned int __max_iter = 100;
359 for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
361 __mu = (__xn + __yn + _Tp(3) * __zn) / _Tp(5);
362 __xndev = (__mu - __xn) / __mu;
363 __yndev = (__mu - __yn) / __mu;
364 __zndev = (__mu - __zn) / __mu;
365 _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
366 __epsilon = std::max(__epsilon, std::abs(__zndev));
367 if (__epsilon < __errtol)
369 _Tp __xnroot = std::sqrt(__xn);
370 _Tp __ynroot = std::sqrt(__yn);
371 _Tp __znroot = std::sqrt(__zn);
372 _Tp __lambda = __xnroot * (__ynroot + __znroot)
373 + __ynroot * __znroot;
374 __sigma += __power4 / (__znroot * (__zn + __lambda));
376 __xn = __c0 * (__xn + __lambda);
377 __yn = __c0 * (__yn + __lambda);
378 __zn = __c0 * (__zn + __lambda);
381 _Tp __ea = __xndev * __yndev;
382 _Tp __eb = __zndev * __zndev;
383 _Tp __ec = __ea - __eb;
384 _Tp __ed = __ea - _Tp(6) * __eb;
385 _Tp __ef = __ed + __ec + __ec;
386 _Tp __s1 = __ed * (-__c1 + __c3 * __ed
387 / _Tp(3) - _Tp(3) * __c4 * __zndev * __ef
391 + __zndev * (-__c3 * __ec - __zndev * __c4 - __ea));
393 return _Tp(3) * __sigma + __power4 * (_Tp(1) + __s1 + __s2)
394 / (__mu * std::sqrt(__mu));
400 * @brief Return the complete elliptic integral of the second kind
401 * @f$ E(k) @f$ using the Carlson formulation.
403 * The complete elliptic integral of the second kind is defined as
405 * E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
408 * @param __k The argument of the complete elliptic function.
409 * @return The complete elliptic function of the second kind.
411 template<typename _Tp>
413 __comp_ellint_2(const _Tp __k)
417 return std::numeric_limits<_Tp>::quiet_NaN();
418 else if (std::abs(__k) == 1)
420 else if (std::abs(__k) > _Tp(1))
421 std::__throw_domain_error(__N("Bad argument in __comp_ellint_2."));
424 const _Tp __kk = __k * __k;
426 return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
427 - __kk * __ellint_rd(_Tp(0), _Tp(1) - __kk, _Tp(1)) / _Tp(3);
433 * @brief Return the incomplete elliptic integral of the second kind
434 * @f$ E(k,\phi) @f$ using the Carlson formulation.
436 * The incomplete elliptic integral of the second kind is defined as
438 * E(k,\phi) = \int_0^{\phi} \sqrt{1 - k^2 sin^2\theta}
441 * @param __k The argument of the elliptic function.
442 * @param __phi The integral limit argument of the elliptic function.
443 * @return The elliptic function of the second kind.
445 template<typename _Tp>
447 __ellint_2(const _Tp __k, const _Tp __phi)
450 if (__isnan(__k) || __isnan(__phi))
451 return std::numeric_limits<_Tp>::quiet_NaN();
452 else if (std::abs(__k) > _Tp(1))
453 std::__throw_domain_error(__N("Bad argument in __ellint_2."));
456 // Reduce phi to -pi/2 < phi < +pi/2.
457 const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
459 const _Tp __phi_red = __phi
460 - __n * __numeric_constants<_Tp>::__pi();
462 const _Tp __kk = __k * __k;
463 const _Tp __s = std::sin(__phi_red);
464 const _Tp __ss = __s * __s;
465 const _Tp __sss = __ss * __s;
466 const _Tp __c = std::cos(__phi_red);
467 const _Tp __cc = __c * __c;
470 * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
472 * __ellint_rd(__cc, _Tp(1) - __kk * __ss, _Tp(1))
478 return __E + _Tp(2) * __n * __comp_ellint_2(__k);
484 * @brief Return the Carlson elliptic function
485 * @f$ R_C(x,y) = R_F(x,y,y) @f$ where @f$ R_F(x,y,z) @f$
486 * is the Carlson elliptic function of the first kind.
488 * The Carlson elliptic function is defined by:
490 * R_C(x,y) = \frac{1}{2} \int_0^\infty
491 * \frac{dt}{(t + x)^{1/2}(t + y)}
494 * Based on Carlson's algorithms:
495 * - B. C. Carlson Numer. Math. 33, 1 (1979)
496 * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
497 * - Nunerical Recipes in C, 2nd ed, pp. 261-269,
498 * by Press, Teukolsky, Vetterling, Flannery (1992)
500 * @param __x The first argument.
501 * @param __y The second argument.
502 * @return The Carlson elliptic function.
504 template<typename _Tp>
506 __ellint_rc(const _Tp __x, const _Tp __y)
508 const _Tp __min = std::numeric_limits<_Tp>::min();
509 const _Tp __max = std::numeric_limits<_Tp>::max();
510 const _Tp __lolim = _Tp(5) * __min;
511 const _Tp __uplim = __max / _Tp(5);
513 if (__x < _Tp(0) || __y < _Tp(0) || __x + __y < __lolim)
514 std::__throw_domain_error(__N("Argument less than zero "
518 const _Tp __c0 = _Tp(1) / _Tp(4);
519 const _Tp __c1 = _Tp(1) / _Tp(7);
520 const _Tp __c2 = _Tp(9) / _Tp(22);
521 const _Tp __c3 = _Tp(3) / _Tp(10);
522 const _Tp __c4 = _Tp(3) / _Tp(8);
527 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
528 const _Tp __errtol = std::pow(__eps / _Tp(30), _Tp(1) / _Tp(6));
532 const unsigned int __max_iter = 100;
533 for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
535 __mu = (__xn + _Tp(2) * __yn) / _Tp(3);
536 __sn = (__yn + __mu) / __mu - _Tp(2);
537 if (std::abs(__sn) < __errtol)
539 const _Tp __lambda = _Tp(2) * std::sqrt(__xn) * std::sqrt(__yn)
541 __xn = __c0 * (__xn + __lambda);
542 __yn = __c0 * (__yn + __lambda);
545 _Tp __s = __sn * __sn
546 * (__c3 + __sn*(__c1 + __sn * (__c4 + __sn * __c2)));
548 return (_Tp(1) + __s) / std::sqrt(__mu);
554 * @brief Return the Carlson elliptic function @f$ R_J(x,y,z,p) @f$
557 * The Carlson elliptic function of the third kind is defined by:
559 * R_J(x,y,z,p) = \frac{3}{2} \int_0^\infty
560 * \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}(t + p)}
563 * Based on Carlson's algorithms:
564 * - B. C. Carlson Numer. Math. 33, 1 (1979)
565 * - B. C. Carlson, Special Functions of Applied Mathematics (1977)
566 * - Nunerical Recipes in C, 2nd ed, pp. 261-269,
567 * by Press, Teukolsky, Vetterling, Flannery (1992)
569 * @param __x The first of three symmetric arguments.
570 * @param __y The second of three symmetric arguments.
571 * @param __z The third of three symmetric arguments.
572 * @param __p The fourth argument.
573 * @return The Carlson elliptic function of the fourth kind.
575 template<typename _Tp>
577 __ellint_rj(const _Tp __x, const _Tp __y, const _Tp __z, const _Tp __p)
579 const _Tp __min = std::numeric_limits<_Tp>::min();
580 const _Tp __max = std::numeric_limits<_Tp>::max();
581 const _Tp __lolim = std::pow(_Tp(5) * __min, _Tp(1)/_Tp(3));
582 const _Tp __uplim = _Tp(0.3L)
583 * std::pow(_Tp(0.2L) * __max, _Tp(1)/_Tp(3));
585 if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
586 std::__throw_domain_error(__N("Argument less than zero "
588 else if (__x + __y < __lolim || __x + __z < __lolim
589 || __y + __z < __lolim || __p < __lolim)
590 std::__throw_domain_error(__N("Argument too small "
594 const _Tp __c0 = _Tp(1) / _Tp(4);
595 const _Tp __c1 = _Tp(3) / _Tp(14);
596 const _Tp __c2 = _Tp(1) / _Tp(3);
597 const _Tp __c3 = _Tp(3) / _Tp(22);
598 const _Tp __c4 = _Tp(3) / _Tp(26);
604 _Tp __sigma = _Tp(0);
605 _Tp __power4 = _Tp(1);
607 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
608 const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
611 _Tp __xndev, __yndev, __zndev, __pndev;
613 const unsigned int __max_iter = 100;
614 for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
616 __mu = (__xn + __yn + __zn + _Tp(2) * __pn) / _Tp(5);
617 __xndev = (__mu - __xn) / __mu;
618 __yndev = (__mu - __yn) / __mu;
619 __zndev = (__mu - __zn) / __mu;
620 __pndev = (__mu - __pn) / __mu;
621 _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
622 __epsilon = std::max(__epsilon, std::abs(__zndev));
623 __epsilon = std::max(__epsilon, std::abs(__pndev));
624 if (__epsilon < __errtol)
626 const _Tp __xnroot = std::sqrt(__xn);
627 const _Tp __ynroot = std::sqrt(__yn);
628 const _Tp __znroot = std::sqrt(__zn);
629 const _Tp __lambda = __xnroot * (__ynroot + __znroot)
630 + __ynroot * __znroot;
631 const _Tp __alpha1 = __pn * (__xnroot + __ynroot + __znroot)
632 + __xnroot * __ynroot * __znroot;
633 const _Tp __alpha2 = __alpha1 * __alpha1;
634 const _Tp __beta = __pn * (__pn + __lambda)
636 __sigma += __power4 * __ellint_rc(__alpha2, __beta);
638 __xn = __c0 * (__xn + __lambda);
639 __yn = __c0 * (__yn + __lambda);
640 __zn = __c0 * (__zn + __lambda);
641 __pn = __c0 * (__pn + __lambda);
644 _Tp __ea = __xndev * (__yndev + __zndev) + __yndev * __zndev;
645 _Tp __eb = __xndev * __yndev * __zndev;
646 _Tp __ec = __pndev * __pndev;
647 _Tp __e2 = __ea - _Tp(3) * __ec;
648 _Tp __e3 = __eb + _Tp(2) * __pndev * (__ea - __ec);
649 _Tp __s1 = _Tp(1) + __e2 * (-__c1 + _Tp(3) * __c3 * __e2 / _Tp(4)
650 - _Tp(3) * __c4 * __e3 / _Tp(2));
651 _Tp __s2 = __eb * (__c2 / _Tp(2)
652 + __pndev * (-__c3 - __c3 + __pndev * __c4));
653 _Tp __s3 = __pndev * __ea * (__c2 - __pndev * __c3)
654 - __c2 * __pndev * __ec;
656 return _Tp(3) * __sigma + __power4 * (__s1 + __s2 + __s3)
657 / (__mu * std::sqrt(__mu));
663 * @brief Return the complete elliptic integral of the third kind
664 * @f$ \Pi(k,\nu) = \Pi(k,\nu,\pi/2) @f$ using the
665 * Carlson formulation.
667 * The complete elliptic integral of the third kind is defined as
669 * \Pi(k,\nu) = \int_0^{\pi/2}
671 * {(1 - \nu \sin^2\theta)\sqrt{1 - k^2 \sin^2\theta}}
674 * @param __k The argument of the elliptic function.
675 * @param __nu The second argument of the elliptic function.
676 * @return The complete elliptic function of the third kind.
678 template<typename _Tp>
680 __comp_ellint_3(const _Tp __k, const _Tp __nu)
683 if (__isnan(__k) || __isnan(__nu))
684 return std::numeric_limits<_Tp>::quiet_NaN();
685 else if (__nu == _Tp(1))
686 return std::numeric_limits<_Tp>::infinity();
687 else if (std::abs(__k) > _Tp(1))
688 std::__throw_domain_error(__N("Bad argument in __comp_ellint_3."));
691 const _Tp __kk = __k * __k;
693 return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
695 * __ellint_rj(_Tp(0), _Tp(1) - __kk, _Tp(1), _Tp(1) + __nu)
702 * @brief Return the incomplete elliptic integral of the third kind
703 * @f$ \Pi(k,\nu,\phi) @f$ using the Carlson formulation.
705 * The incomplete elliptic integral of the third kind is defined as
707 * \Pi(k,\nu,\phi) = \int_0^{\phi}
709 * {(1 - \nu \sin^2\theta)
710 * \sqrt{1 - k^2 \sin^2\theta}}
713 * @param __k The argument of the elliptic function.
714 * @param __nu The second argument of the elliptic function.
715 * @param __phi The integral limit argument of the elliptic function.
716 * @return The elliptic function of the third kind.
718 template<typename _Tp>
720 __ellint_3(const _Tp __k, const _Tp __nu, const _Tp __phi)
723 if (__isnan(__k) || __isnan(__nu) || __isnan(__phi))
724 return std::numeric_limits<_Tp>::quiet_NaN();
725 else if (std::abs(__k) > _Tp(1))
726 std::__throw_domain_error(__N("Bad argument in __ellint_3."));
729 // Reduce phi to -pi/2 < phi < +pi/2.
730 const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
732 const _Tp __phi_red = __phi
733 - __n * __numeric_constants<_Tp>::__pi();
735 const _Tp __kk = __k * __k;
736 const _Tp __s = std::sin(__phi_red);
737 const _Tp __ss = __s * __s;
738 const _Tp __sss = __ss * __s;
739 const _Tp __c = std::cos(__phi_red);
740 const _Tp __cc = __c * __c;
743 * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
745 * __ellint_rj(__cc, _Tp(1) - __kk * __ss, _Tp(1),
746 _Tp(1) + __nu * __ss) / _Tp(3);
751 return __Pi + _Tp(2) * __n * __comp_ellint_3(__k, __nu);
755 } // namespace std::tr1::__detail
757 /* @} */ // group tr1_math_spec_func
762 #endif // _GLIBCXX_TR1_ELL_INTEGRAL_TCC