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