ell_integral.tcc

Go to the documentation of this file.
00001 // Special functions -*- C++ -*-
00002 
00003 // Copyright (C) 2006, 2007, 2008
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 //
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 //
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file tr1/ell_integral.tcc
00032  *  This is an internal header file, included by other library headers.
00033  *  You should not attempt to use it directly.
00034  */
00035 
00036 //
00037 // ISO C++ 14882 TR1: 5.2  Special functions
00038 //
00039 
00040 // Written by Edward Smith-Rowland based on:
00041 //   (1)  B. C. Carlson Numer. Math. 33, 1 (1979)
00042 //   (2)  B. C. Carlson, Special Functions of Applied Mathematics (1977)
00043 //   (3)  The Gnu Scientific Library, http://www.gnu.org/software/gsl
00044 //   (4)  Numerical Recipes in C, 2nd ed, by W. H. Press, S. A. Teukolsky,
00045 //        W. T. Vetterling, B. P. Flannery, Cambridge University Press
00046 //        (1992), pp. 261-269
00047 
00048 #ifndef _GLIBCXX_TR1_ELL_INTEGRAL_TCC
00049 #define _GLIBCXX_TR1_ELL_INTEGRAL_TCC 1
00050 
00051 namespace std
00052 {
00053 namespace tr1
00054 {
00055 
00056   // [5.2] Special functions
00057 
00058   // Implementation-space details.
00059   namespace __detail
00060   {
00061 
00062     /**
00063      *   @brief Return the Carlson elliptic function @f$ R_F(x,y,z) @f$
00064      *          of the first kind.
00065      * 
00066      *   The Carlson elliptic function of the first kind is defined by:
00067      *   @f[
00068      *       R_F(x,y,z) = \frac{1}{2} \int_0^\infty
00069      *                 \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}}
00070      *   @f]
00071      *
00072      *   @param  __x  The first of three symmetric arguments.
00073      *   @param  __y  The second of three symmetric arguments.
00074      *   @param  __z  The third of three symmetric arguments.
00075      *   @return  The Carlson elliptic function of the first kind.
00076      */
00077     template<typename _Tp>
00078     _Tp
00079     __ellint_rf(const _Tp __x, const _Tp __y, const _Tp __z)
00080     {
00081       const _Tp __min = std::numeric_limits<_Tp>::min();
00082       const _Tp __max = std::numeric_limits<_Tp>::max();
00083       const _Tp __lolim = _Tp(5) * __min;
00084       const _Tp __uplim = __max / _Tp(5);
00085 
00086       if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
00087         std::__throw_domain_error(__N("Argument less than zero "
00088                                       "in __ellint_rf."));
00089       else if (__x + __y < __lolim || __x + __z < __lolim
00090             || __y + __z < __lolim)
00091         std::__throw_domain_error(__N("Argument too small in __ellint_rf"));
00092       else
00093         {
00094           const _Tp __c0 = _Tp(1) / _Tp(4);
00095           const _Tp __c1 = _Tp(1) / _Tp(24);
00096           const _Tp __c2 = _Tp(1) / _Tp(10);
00097           const _Tp __c3 = _Tp(3) / _Tp(44);
00098           const _Tp __c4 = _Tp(1) / _Tp(14);
00099 
00100           _Tp __xn = __x;
00101           _Tp __yn = __y;
00102           _Tp __zn = __z;
00103 
00104           const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00105           const _Tp __errtol = std::pow(__eps, _Tp(1) / _Tp(6));
00106           _Tp __mu;
00107           _Tp __xndev, __yndev, __zndev;
00108 
00109           const unsigned int __max_iter = 100;
00110           for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
00111             {
00112               __mu = (__xn + __yn + __zn) / _Tp(3);
00113               __xndev = 2 - (__mu + __xn) / __mu;
00114               __yndev = 2 - (__mu + __yn) / __mu;
00115               __zndev = 2 - (__mu + __zn) / __mu;
00116               _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
00117               __epsilon = std::max(__epsilon, std::abs(__zndev));
00118               if (__epsilon < __errtol)
00119                 break;
00120               const _Tp __xnroot = std::sqrt(__xn);
00121               const _Tp __ynroot = std::sqrt(__yn);
00122               const _Tp __znroot = std::sqrt(__zn);
00123               const _Tp __lambda = __xnroot * (__ynroot + __znroot)
00124                                  + __ynroot * __znroot;
00125               __xn = __c0 * (__xn + __lambda);
00126               __yn = __c0 * (__yn + __lambda);
00127               __zn = __c0 * (__zn + __lambda);
00128             }
00129 
00130           const _Tp __e2 = __xndev * __yndev - __zndev * __zndev;
00131           const _Tp __e3 = __xndev * __yndev * __zndev;
00132           const _Tp __s  = _Tp(1) + (__c1 * __e2 - __c2 - __c3 * __e3) * __e2
00133                    + __c4 * __e3;
00134 
00135           return __s / std::sqrt(__mu);
00136         }
00137     }
00138 
00139 
00140     /**
00141      *   @brief Return the complete elliptic integral of the first kind
00142      *          @f$ K(k) @f$ by series expansion.
00143      * 
00144      *   The complete elliptic integral of the first kind is defined as
00145      *   @f[
00146      *     K(k) = F(k,\pi/2) = \int_0^{\pi/2}\frac{d\theta}
00147      *                              {\sqrt{1 - k^2sin^2\theta}}
00148      *   @f]
00149      * 
00150      *   This routine is not bad as long as |k| is somewhat smaller than 1
00151      *   but is not is good as the Carlson elliptic integral formulation.
00152      * 
00153      *   @param  __k  The argument of the complete elliptic function.
00154      *   @return  The complete elliptic function of the first kind.
00155      */
00156     template<typename _Tp>
00157     _Tp
00158     __comp_ellint_1_series(const _Tp __k)
00159     {
00160 
00161       const _Tp __kk = __k * __k;
00162 
00163       _Tp __term = __kk / _Tp(4);
00164       _Tp __sum = _Tp(1) + __term;
00165 
00166       const unsigned int __max_iter = 1000;
00167       for (unsigned int __i = 2; __i < __max_iter; ++__i)
00168         {
00169           __term *= (2 * __i - 1) * __kk / (2 * __i);
00170           if (__term < std::numeric_limits<_Tp>::epsilon())
00171             break;
00172           __sum += __term;
00173         }
00174 
00175       return __numeric_constants<_Tp>::__pi_2() * __sum;
00176     }
00177 
00178 
00179     /**
00180      *   @brief  Return the complete elliptic integral of the first kind
00181      *           @f$ K(k) @f$ using the Carlson formulation.
00182      * 
00183      *   The complete elliptic integral of the first kind is defined as
00184      *   @f[
00185      *     K(k) = F(k,\pi/2) = \int_0^{\pi/2}\frac{d\theta}
00186      *                                           {\sqrt{1 - k^2 sin^2\theta}}
00187      *   @f]
00188      *   where @f$ F(k,\phi) @f$ is the incomplete elliptic integral of the
00189      *   first kind.
00190      * 
00191      *   @param  __k  The argument of the complete elliptic function.
00192      *   @return  The complete elliptic function of the first kind.
00193      */
00194     template<typename _Tp>
00195     _Tp
00196     __comp_ellint_1(const _Tp __k)
00197     {
00198 
00199       if (__isnan(__k))
00200         return std::numeric_limits<_Tp>::quiet_NaN();
00201       else if (std::abs(__k) >= _Tp(1))
00202         return std::numeric_limits<_Tp>::quiet_NaN();
00203       else
00204         return __ellint_rf(_Tp(0), _Tp(1) - __k * __k, _Tp(1));
00205     }
00206 
00207 
00208     /**
00209      *   @brief  Return the incomplete elliptic integral of the first kind
00210      *           @f$ F(k,\phi) @f$ using the Carlson formulation.
00211      * 
00212      *   The incomplete elliptic integral of the first kind is defined as
00213      *   @f[
00214      *     F(k,\phi) = \int_0^{\phi}\frac{d\theta}
00215      *                                   {\sqrt{1 - k^2 sin^2\theta}}
00216      *   @f]
00217      * 
00218      *   @param  __k  The argument of the elliptic function.
00219      *   @param  __phi  The integral limit argument of the elliptic function.
00220      *   @return  The elliptic function of the first kind.
00221      */
00222     template<typename _Tp>
00223     _Tp
00224     __ellint_1(const _Tp __k, const _Tp __phi)
00225     {
00226 
00227       if (__isnan(__k) || __isnan(__phi))
00228         return std::numeric_limits<_Tp>::quiet_NaN();
00229       else if (std::abs(__k) > _Tp(1))
00230         std::__throw_domain_error(__N("Bad argument in __ellint_1."));
00231       else
00232         {
00233           //  Reduce phi to -pi/2 < phi < +pi/2.
00234           const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
00235                                    + _Tp(0.5L));
00236           const _Tp __phi_red = __phi
00237                               - __n * __numeric_constants<_Tp>::__pi();
00238 
00239           const _Tp __s = std::sin(__phi_red);
00240           const _Tp __c = std::cos(__phi_red);
00241 
00242           const _Tp __F = __s
00243                         * __ellint_rf(__c * __c,
00244                                 _Tp(1) - __k * __k * __s * __s, _Tp(1));
00245 
00246           if (__n == 0)
00247             return __F;
00248           else
00249             return __F + _Tp(2) * __n * __comp_ellint_1(__k);
00250         }
00251     }
00252 
00253 
00254     /**
00255      *   @brief Return the complete elliptic integral of the second kind
00256      *          @f$ E(k) @f$ by series expansion.
00257      * 
00258      *   The complete elliptic integral of the second kind is defined as
00259      *   @f[
00260      *     E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
00261      *   @f]
00262      * 
00263      *   This routine is not bad as long as |k| is somewhat smaller than 1
00264      *   but is not is good as the Carlson elliptic integral formulation.
00265      * 
00266      *   @param  __k  The argument of the complete elliptic function.
00267      *   @return  The complete elliptic function of the second kind.
00268      */
00269     template<typename _Tp>
00270     _Tp
00271     __comp_ellint_2_series(const _Tp __k)
00272     {
00273 
00274       const _Tp __kk = __k * __k;
00275 
00276       _Tp __term = __kk;
00277       _Tp __sum = __term;
00278 
00279       const unsigned int __max_iter = 1000;
00280       for (unsigned int __i = 2; __i < __max_iter; ++__i)
00281         {
00282           const _Tp __i2m = 2 * __i - 1;
00283           const _Tp __i2 = 2 * __i;
00284           __term *= __i2m * __i2m * __kk / (__i2 * __i2);
00285           if (__term < std::numeric_limits<_Tp>::epsilon())
00286             break;
00287           __sum += __term / __i2m;
00288         }
00289 
00290       return __numeric_constants<_Tp>::__pi_2() * (_Tp(1) - __sum);
00291     }
00292 
00293 
00294     /**
00295      *   @brief  Return the Carlson elliptic function of the second kind
00296      *           @f$ R_D(x,y,z) = R_J(x,y,z,z) @f$ where
00297      *           @f$ R_J(x,y,z,p) @f$ is the Carlson elliptic function
00298      *           of the third kind.
00299      * 
00300      *   The Carlson elliptic function of the second kind is defined by:
00301      *   @f[
00302      *       R_D(x,y,z) = \frac{3}{2} \int_0^\infty
00303      *                 \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{3/2}}
00304      *   @f]
00305      *
00306      *   Based on Carlson's algorithms:
00307      *   -  B. C. Carlson Numer. Math. 33, 1 (1979)
00308      *   -  B. C. Carlson, Special Functions of Applied Mathematics (1977)
00309      *   -  Numerical Recipes in C, 2nd ed, pp. 261-269,
00310      *      by Press, Teukolsky, Vetterling, Flannery (1992)
00311      *
00312      *   @param  __x  The first of two symmetric arguments.
00313      *   @param  __y  The second of two symmetric arguments.
00314      *   @param  __z  The third argument.
00315      *   @return  The Carlson elliptic function of the second kind.
00316      */
00317     template<typename _Tp>
00318     _Tp
00319     __ellint_rd(const _Tp __x, const _Tp __y, const _Tp __z)
00320     {
00321       const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00322       const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
00323       const _Tp __min = std::numeric_limits<_Tp>::min();
00324       const _Tp __max = std::numeric_limits<_Tp>::max();
00325       const _Tp __lolim = _Tp(2) / std::pow(__max, _Tp(2) / _Tp(3));
00326       const _Tp __uplim = std::pow(_Tp(0.1L) * __errtol / __min, _Tp(2) / _Tp(3));
00327 
00328       if (__x < _Tp(0) || __y < _Tp(0))
00329         std::__throw_domain_error(__N("Argument less than zero "
00330                                       "in __ellint_rd."));
00331       else if (__x + __y < __lolim || __z < __lolim)
00332         std::__throw_domain_error(__N("Argument too small "
00333                                       "in __ellint_rd."));
00334       else
00335         {
00336           const _Tp __c0 = _Tp(1) / _Tp(4);
00337           const _Tp __c1 = _Tp(3) / _Tp(14);
00338           const _Tp __c2 = _Tp(1) / _Tp(6);
00339           const _Tp __c3 = _Tp(9) / _Tp(22);
00340           const _Tp __c4 = _Tp(3) / _Tp(26);
00341 
00342           _Tp __xn = __x;
00343           _Tp __yn = __y;
00344           _Tp __zn = __z;
00345           _Tp __sigma = _Tp(0);
00346           _Tp __power4 = _Tp(1);
00347 
00348           _Tp __mu;
00349           _Tp __xndev, __yndev, __zndev;
00350 
00351           const unsigned int __max_iter = 100;
00352           for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
00353             {
00354               __mu = (__xn + __yn + _Tp(3) * __zn) / _Tp(5);
00355               __xndev = (__mu - __xn) / __mu;
00356               __yndev = (__mu - __yn) / __mu;
00357               __zndev = (__mu - __zn) / __mu;
00358               _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
00359               __epsilon = std::max(__epsilon, std::abs(__zndev));
00360               if (__epsilon < __errtol)
00361                 break;
00362               _Tp __xnroot = std::sqrt(__xn);
00363               _Tp __ynroot = std::sqrt(__yn);
00364               _Tp __znroot = std::sqrt(__zn);
00365               _Tp __lambda = __xnroot * (__ynroot + __znroot)
00366                            + __ynroot * __znroot;
00367               __sigma += __power4 / (__znroot * (__zn + __lambda));
00368               __power4 *= __c0;
00369               __xn = __c0 * (__xn + __lambda);
00370               __yn = __c0 * (__yn + __lambda);
00371               __zn = __c0 * (__zn + __lambda);
00372             }
00373 
00374           _Tp __ea = __xndev * __yndev;
00375           _Tp __eb = __zndev * __zndev;
00376           _Tp __ec = __ea - __eb;
00377           _Tp __ed = __ea - _Tp(6) * __eb;
00378           _Tp __ef = __ed + __ec + __ec;
00379           _Tp __s1 = __ed * (-__c1 + __c3 * __ed
00380                                    / _Tp(3) - _Tp(3) * __c4 * __zndev * __ef
00381                                    / _Tp(2));
00382           _Tp __s2 = __zndev
00383                    * (__c2 * __ef
00384                     + __zndev * (-__c3 * __ec - __zndev * __c4 - __ea));
00385 
00386           return _Tp(3) * __sigma + __power4 * (_Tp(1) + __s1 + __s2)
00387                                         / (__mu * std::sqrt(__mu));
00388         }
00389     }
00390 
00391 
00392     /**
00393      *   @brief  Return the complete elliptic integral of the second kind
00394      *           @f$ E(k) @f$ using the Carlson formulation.
00395      * 
00396      *   The complete elliptic integral of the second kind is defined as
00397      *   @f[
00398      *     E(k,\pi/2) = \int_0^{\pi/2}\sqrt{1 - k^2 sin^2\theta}
00399      *   @f]
00400      * 
00401      *   @param  __k  The argument of the complete elliptic function.
00402      *   @return  The complete elliptic function of the second kind.
00403      */
00404     template<typename _Tp>
00405     _Tp
00406     __comp_ellint_2(const _Tp __k)
00407     {
00408 
00409       if (__isnan(__k))
00410         return std::numeric_limits<_Tp>::quiet_NaN();
00411       else if (std::abs(__k) == 1)
00412         return _Tp(1);
00413       else if (std::abs(__k) > _Tp(1))
00414         std::__throw_domain_error(__N("Bad argument in __comp_ellint_2."));
00415       else
00416         {
00417           const _Tp __kk = __k * __k;
00418 
00419           return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
00420                - __kk * __ellint_rd(_Tp(0), _Tp(1) - __kk, _Tp(1)) / _Tp(3);
00421         }
00422     }
00423 
00424 
00425     /**
00426      *   @brief  Return the incomplete elliptic integral of the second kind
00427      *           @f$ E(k,\phi) @f$ using the Carlson formulation.
00428      * 
00429      *   The incomplete elliptic integral of the second kind is defined as
00430      *   @f[
00431      *     E(k,\phi) = \int_0^{\phi} \sqrt{1 - k^2 sin^2\theta}
00432      *   @f]
00433      * 
00434      *   @param  __k  The argument of the elliptic function.
00435      *   @param  __phi  The integral limit argument of the elliptic function.
00436      *   @return  The elliptic function of the second kind.
00437      */
00438     template<typename _Tp>
00439     _Tp
00440     __ellint_2(const _Tp __k, const _Tp __phi)
00441     {
00442 
00443       if (__isnan(__k) || __isnan(__phi))
00444         return std::numeric_limits<_Tp>::quiet_NaN();
00445       else if (std::abs(__k) > _Tp(1))
00446         std::__throw_domain_error(__N("Bad argument in __ellint_2."));
00447       else
00448         {
00449           //  Reduce phi to -pi/2 < phi < +pi/2.
00450           const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
00451                                    + _Tp(0.5L));
00452           const _Tp __phi_red = __phi
00453                               - __n * __numeric_constants<_Tp>::__pi();
00454 
00455           const _Tp __kk = __k * __k;
00456           const _Tp __s = std::sin(__phi_red);
00457           const _Tp __ss = __s * __s;
00458           const _Tp __sss = __ss * __s;
00459           const _Tp __c = std::cos(__phi_red);
00460           const _Tp __cc = __c * __c;
00461 
00462           const _Tp __E = __s
00463                         * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
00464                         - __kk * __sss
00465                         * __ellint_rd(__cc, _Tp(1) - __kk * __ss, _Tp(1))
00466                         / _Tp(3);
00467 
00468           if (__n == 0)
00469             return __E;
00470           else
00471             return __E + _Tp(2) * __n * __comp_ellint_2(__k);
00472         }
00473     }
00474 
00475 
00476     /**
00477      *   @brief  Return the Carlson elliptic function
00478      *           @f$ R_C(x,y) = R_F(x,y,y) @f$ where @f$ R_F(x,y,z) @f$
00479      *           is the Carlson elliptic function of the first kind.
00480      * 
00481      *   The Carlson elliptic function is defined by:
00482      *   @f[
00483      *       R_C(x,y) = \frac{1}{2} \int_0^\infty
00484      *                 \frac{dt}{(t + x)^{1/2}(t + y)}
00485      *   @f]
00486      *
00487      *   Based on Carlson's algorithms:
00488      *   -  B. C. Carlson Numer. Math. 33, 1 (1979)
00489      *   -  B. C. Carlson, Special Functions of Applied Mathematics (1977)
00490      *   -  Numerical Recipes in C, 2nd ed, pp. 261-269,
00491      *      by Press, Teukolsky, Vetterling, Flannery (1992)
00492      *
00493      *   @param  __x  The first argument.
00494      *   @param  __y  The second argument.
00495      *   @return  The Carlson elliptic function.
00496      */
00497     template<typename _Tp>
00498     _Tp
00499     __ellint_rc(const _Tp __x, const _Tp __y)
00500     {
00501       const _Tp __min = std::numeric_limits<_Tp>::min();
00502       const _Tp __max = std::numeric_limits<_Tp>::max();
00503       const _Tp __lolim = _Tp(5) * __min;
00504       const _Tp __uplim = __max / _Tp(5);
00505 
00506       if (__x < _Tp(0) || __y < _Tp(0) || __x + __y < __lolim)
00507         std::__throw_domain_error(__N("Argument less than zero "
00508                                       "in __ellint_rc."));
00509       else
00510         {
00511           const _Tp __c0 = _Tp(1) / _Tp(4);
00512           const _Tp __c1 = _Tp(1) / _Tp(7);
00513           const _Tp __c2 = _Tp(9) / _Tp(22);
00514           const _Tp __c3 = _Tp(3) / _Tp(10);
00515           const _Tp __c4 = _Tp(3) / _Tp(8);
00516 
00517           _Tp __xn = __x;
00518           _Tp __yn = __y;
00519 
00520           const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00521           const _Tp __errtol = std::pow(__eps / _Tp(30), _Tp(1) / _Tp(6));
00522           _Tp __mu;
00523           _Tp __sn;
00524 
00525           const unsigned int __max_iter = 100;
00526           for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
00527             {
00528               __mu = (__xn + _Tp(2) * __yn) / _Tp(3);
00529               __sn = (__yn + __mu) / __mu - _Tp(2);
00530               if (std::abs(__sn) < __errtol)
00531                 break;
00532               const _Tp __lambda = _Tp(2) * std::sqrt(__xn) * std::sqrt(__yn)
00533                              + __yn;
00534               __xn = __c0 * (__xn + __lambda);
00535               __yn = __c0 * (__yn + __lambda);
00536             }
00537 
00538           _Tp __s = __sn * __sn
00539                   * (__c3 + __sn*(__c1 + __sn * (__c4 + __sn * __c2)));
00540 
00541           return (_Tp(1) + __s) / std::sqrt(__mu);
00542         }
00543     }
00544 
00545 
00546     /**
00547      *   @brief  Return the Carlson elliptic function @f$ R_J(x,y,z,p) @f$
00548      *           of the third kind.
00549      * 
00550      *   The Carlson elliptic function of the third kind is defined by:
00551      *   @f[
00552      *       R_J(x,y,z,p) = \frac{3}{2} \int_0^\infty
00553      *       \frac{dt}{(t + x)^{1/2}(t + y)^{1/2}(t + z)^{1/2}(t + p)}
00554      *   @f]
00555      *
00556      *   Based on Carlson's algorithms:
00557      *   -  B. C. Carlson Numer. Math. 33, 1 (1979)
00558      *   -  B. C. Carlson, Special Functions of Applied Mathematics (1977)
00559      *   -  Numerical Recipes in C, 2nd ed, pp. 261-269,
00560      *      by Press, Teukolsky, Vetterling, Flannery (1992)
00561      *
00562      *   @param  __x  The first of three symmetric arguments.
00563      *   @param  __y  The second of three symmetric arguments.
00564      *   @param  __z  The third of three symmetric arguments.
00565      *   @param  __p  The fourth argument.
00566      *   @return  The Carlson elliptic function of the fourth kind.
00567      */
00568     template<typename _Tp>
00569     _Tp
00570     __ellint_rj(const _Tp __x, const _Tp __y, const _Tp __z, const _Tp __p)
00571     {
00572       const _Tp __min = std::numeric_limits<_Tp>::min();
00573       const _Tp __max = std::numeric_limits<_Tp>::max();
00574       const _Tp __lolim = std::pow(_Tp(5) * __min, _Tp(1)/_Tp(3));
00575       const _Tp __uplim = _Tp(0.3L)
00576                         * std::pow(_Tp(0.2L) * __max, _Tp(1)/_Tp(3));
00577 
00578       if (__x < _Tp(0) || __y < _Tp(0) || __z < _Tp(0))
00579         std::__throw_domain_error(__N("Argument less than zero "
00580                                       "in __ellint_rj."));
00581       else if (__x + __y < __lolim || __x + __z < __lolim
00582             || __y + __z < __lolim || __p < __lolim)
00583         std::__throw_domain_error(__N("Argument too small "
00584                                       "in __ellint_rj"));
00585       else
00586         {
00587           const _Tp __c0 = _Tp(1) / _Tp(4);
00588           const _Tp __c1 = _Tp(3) / _Tp(14);
00589           const _Tp __c2 = _Tp(1) / _Tp(3);
00590           const _Tp __c3 = _Tp(3) / _Tp(22);
00591           const _Tp __c4 = _Tp(3) / _Tp(26);
00592 
00593           _Tp __xn = __x;
00594           _Tp __yn = __y;
00595           _Tp __zn = __z;
00596           _Tp __pn = __p;
00597           _Tp __sigma = _Tp(0);
00598           _Tp __power4 = _Tp(1);
00599 
00600           const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
00601           const _Tp __errtol = std::pow(__eps / _Tp(8), _Tp(1) / _Tp(6));
00602 
00603           _Tp __lambda, __mu;
00604           _Tp __xndev, __yndev, __zndev, __pndev;
00605 
00606           const unsigned int __max_iter = 100;
00607           for (unsigned int __iter = 0; __iter < __max_iter; ++__iter)
00608             {
00609               __mu = (__xn + __yn + __zn + _Tp(2) * __pn) / _Tp(5);
00610               __xndev = (__mu - __xn) / __mu;
00611               __yndev = (__mu - __yn) / __mu;
00612               __zndev = (__mu - __zn) / __mu;
00613               __pndev = (__mu - __pn) / __mu;
00614               _Tp __epsilon = std::max(std::abs(__xndev), std::abs(__yndev));
00615               __epsilon = std::max(__epsilon, std::abs(__zndev));
00616               __epsilon = std::max(__epsilon, std::abs(__pndev));
00617               if (__epsilon < __errtol)
00618                 break;
00619               const _Tp __xnroot = std::sqrt(__xn);
00620               const _Tp __ynroot = std::sqrt(__yn);
00621               const _Tp __znroot = std::sqrt(__zn);
00622               const _Tp __lambda = __xnroot * (__ynroot + __znroot)
00623                                  + __ynroot * __znroot;
00624               const _Tp __alpha1 = __pn * (__xnroot + __ynroot + __znroot)
00625                                 + __xnroot * __ynroot * __znroot;
00626               const _Tp __alpha2 = __alpha1 * __alpha1;
00627               const _Tp __beta = __pn * (__pn + __lambda)
00628                                       * (__pn + __lambda);
00629               __sigma += __power4 * __ellint_rc(__alpha2, __beta);
00630               __power4 *= __c0;
00631               __xn = __c0 * (__xn + __lambda);
00632               __yn = __c0 * (__yn + __lambda);
00633               __zn = __c0 * (__zn + __lambda);
00634               __pn = __c0 * (__pn + __lambda);
00635             }
00636 
00637           _Tp __ea = __xndev * (__yndev + __zndev) + __yndev * __zndev;
00638           _Tp __eb = __xndev * __yndev * __zndev;
00639           _Tp __ec = __pndev * __pndev;
00640           _Tp __e2 = __ea - _Tp(3) * __ec;
00641           _Tp __e3 = __eb + _Tp(2) * __pndev * (__ea - __ec);
00642           _Tp __s1 = _Tp(1) + __e2 * (-__c1 + _Tp(3) * __c3 * __e2 / _Tp(4)
00643                             - _Tp(3) * __c4 * __e3 / _Tp(2));
00644           _Tp __s2 = __eb * (__c2 / _Tp(2)
00645                    + __pndev * (-__c3 - __c3 + __pndev * __c4));
00646           _Tp __s3 = __pndev * __ea * (__c2 - __pndev * __c3)
00647                    - __c2 * __pndev * __ec;
00648 
00649           return _Tp(3) * __sigma + __power4 * (__s1 + __s2 + __s3)
00650                                              / (__mu * std::sqrt(__mu));
00651         }
00652     }
00653 
00654 
00655     /**
00656      *   @brief Return the complete elliptic integral of the third kind
00657      *          @f$ \Pi(k,\nu) = \Pi(k,\nu,\pi/2) @f$ using the
00658      *          Carlson formulation.
00659      * 
00660      *   The complete elliptic integral of the third kind is defined as
00661      *   @f[
00662      *     \Pi(k,\nu) = \int_0^{\pi/2}
00663      *                   \frac{d\theta}
00664      *                 {(1 - \nu \sin^2\theta)\sqrt{1 - k^2 \sin^2\theta}}
00665      *   @f]
00666      * 
00667      *   @param  __k  The argument of the elliptic function.
00668      *   @param  __nu  The second argument of the elliptic function.
00669      *   @return  The complete elliptic function of the third kind.
00670      */
00671     template<typename _Tp>
00672     _Tp
00673     __comp_ellint_3(const _Tp __k, const _Tp __nu)
00674     {
00675 
00676       if (__isnan(__k) || __isnan(__nu))
00677         return std::numeric_limits<_Tp>::quiet_NaN();
00678       else if (__nu == _Tp(1))
00679         return std::numeric_limits<_Tp>::infinity();
00680       else if (std::abs(__k) > _Tp(1))
00681         std::__throw_domain_error(__N("Bad argument in __comp_ellint_3."));
00682       else
00683         {
00684           const _Tp __kk = __k * __k;
00685 
00686           return __ellint_rf(_Tp(0), _Tp(1) - __kk, _Tp(1))
00687                - __nu
00688                * __ellint_rj(_Tp(0), _Tp(1) - __kk, _Tp(1), _Tp(1) + __nu)
00689                / _Tp(3);
00690         }
00691     }
00692 
00693 
00694     /**
00695      *   @brief Return the incomplete elliptic integral of the third kind
00696      *          @f$ \Pi(k,\nu,\phi) @f$ using the Carlson formulation.
00697      * 
00698      *   The incomplete elliptic integral of the third kind is defined as
00699      *   @f[
00700      *     \Pi(k,\nu,\phi) = \int_0^{\phi}
00701      *                       \frac{d\theta}
00702      *                            {(1 - \nu \sin^2\theta)
00703      *                             \sqrt{1 - k^2 \sin^2\theta}}
00704      *   @f]
00705      * 
00706      *   @param  __k  The argument of the elliptic function.
00707      *   @param  __nu  The second argument of the elliptic function.
00708      *   @param  __phi  The integral limit argument of the elliptic function.
00709      *   @return  The elliptic function of the third kind.
00710      */
00711     template<typename _Tp>
00712     _Tp
00713     __ellint_3(const _Tp __k, const _Tp __nu, const _Tp __phi)
00714     {
00715 
00716       if (__isnan(__k) || __isnan(__nu) || __isnan(__phi))
00717         return std::numeric_limits<_Tp>::quiet_NaN();
00718       else if (std::abs(__k) > _Tp(1))
00719         std::__throw_domain_error(__N("Bad argument in __ellint_3."));
00720       else
00721         {
00722           //  Reduce phi to -pi/2 < phi < +pi/2.
00723           const int __n = std::floor(__phi / __numeric_constants<_Tp>::__pi()
00724                                    + _Tp(0.5L));
00725           const _Tp __phi_red = __phi
00726                               - __n * __numeric_constants<_Tp>::__pi();
00727 
00728           const _Tp __kk = __k * __k;
00729           const _Tp __s = std::sin(__phi_red);
00730           const _Tp __ss = __s * __s;
00731           const _Tp __sss = __ss * __s;
00732           const _Tp __c = std::cos(__phi_red);
00733           const _Tp __cc = __c * __c;
00734 
00735           const _Tp __Pi = __s
00736                          * __ellint_rf(__cc, _Tp(1) - __kk * __ss, _Tp(1))
00737                          - __nu * __sss
00738                          * __ellint_rj(__cc, _Tp(1) - __kk * __ss, _Tp(1),
00739                                        _Tp(1) + __nu * __ss) / _Tp(3);
00740 
00741           if (__n == 0)
00742             return __Pi;
00743           else
00744             return __Pi + _Tp(2) * __n * __comp_ellint_3(__k, __nu);
00745         }
00746     }
00747 
00748   } // namespace std::tr1::__detail
00749 }
00750 }
00751 
00752 #endif // _GLIBCXX_TR1_ELL_INTEGRAL_TCC
00753 

Generated on Fri Jan 23 20:12:11 2009 for libstdc++ by  doxygen 1.5.6