]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/tr1/legendre_function.tcc
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / tr1 / legendre_function.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/legendre_function.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) Handbook of Mathematical Functions,
42 //       ed. Milton Abramowitz and Irene A. Stegun,
43 //       Dover Publications,
44 //       Section 8, pp. 331-341
45 //   (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
46 //   (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
47 //       W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
48 //       2nd ed, pp. 252-254
49
50 #ifndef _GLIBCXX_TR1_LEGENDRE_FUNCTION_TCC
51 #define _GLIBCXX_TR1_LEGENDRE_FUNCTION_TCC 1
52
53 #include "special_function_util.h"
54
55 namespace std
56 {
57 namespace tr1
58 {
59
60   // [5.2] Special functions
61
62   // Implementation-space details.
63   namespace __detail
64   {
65
66     /**
67      *   @brief  Return the Legendre polynomial by recursion on order
68      *           @f$ l @f$.
69      * 
70      *   The Legendre function of @f$ l @f$ and @f$ x @f$,
71      *   @f$ P_l(x) @f$, is defined by:
72      *   @f[
73      *     P_l(x) = \frac{1}{2^l l!}\frac{d^l}{dx^l}(x^2 - 1)^{l}
74      *   @f]
75      * 
76      *   @param  l  The order of the Legendre polynomial.  @f$l >= 0@f$.
77      *   @param  x  The argument of the Legendre polynomial.  @f$|x| <= 1@f$.
78      */
79     template<typename _Tp>
80     _Tp
81     __poly_legendre_p(const unsigned int __l, const _Tp __x)
82     {
83
84       if ((__x < _Tp(-1)) || (__x > _Tp(+1)))
85         std::__throw_domain_error(__N("Argument out of range"
86                                       " in __poly_legendre_p."));
87       else if (__isnan(__x))
88         return std::numeric_limits<_Tp>::quiet_NaN();
89       else if (__x == +_Tp(1))
90         return +_Tp(1);
91       else if (__x == -_Tp(1))
92         return (__l % 2 == 1 ? -_Tp(1) : +_Tp(1));
93       else
94         {
95           _Tp __p_lm2 = _Tp(1);
96           if (__l == 0)
97             return __p_lm2;
98
99           _Tp __p_lm1 = __x;
100           if (__l == 1)
101             return __p_lm1;
102
103           _Tp __p_l = 0;
104           for (unsigned int __ll = 2; __ll <= __l; ++__ll)
105             {
106               //  This arrangement is supposed to be better for roundoff
107               //  protection, Arfken, 2nd Ed, Eq 12.17a.
108               __p_l = _Tp(2) * __x * __p_lm1 - __p_lm2
109                     - (__x * __p_lm1 - __p_lm2) / _Tp(__ll);
110               __p_lm2 = __p_lm1;
111               __p_lm1 = __p_l;
112             }
113
114           return __p_l;
115         }
116     }
117
118
119     /**
120      *   @brief  Return the associated Legendre function by recursion
121      *           on @f$ l @f$.
122      * 
123      *   The associated Legendre function is derived from the Legendre function
124      *   @f$ P_l(x) @f$ by the Rodrigues formula:
125      *   @f[
126      *     P_l^m(x) = (1 - x^2)^{m/2}\frac{d^m}{dx^m}P_l(x)
127      *   @f]
128      * 
129      *   @param  l  The order of the associated Legendre function.
130      *              @f$ l >= 0 @f$.
131      *   @param  m  The order of the associated Legendre function.
132      *              @f$ m <= l @f$.
133      *   @param  x  The argument of the associated Legendre function.
134      *              @f$ |x| <= 1 @f$.
135      */
136     template<typename _Tp>
137     _Tp
138     __assoc_legendre_p(const unsigned int __l, const unsigned int __m,
139                        const _Tp __x)
140     {
141
142       if (__x < _Tp(-1) || __x > _Tp(+1))
143         std::__throw_domain_error(__N("Argument out of range"
144                                       " in __assoc_legendre_p."));
145       else if (__m > __l)
146         std::__throw_domain_error(__N("Degree out of range"
147                                       " in __assoc_legendre_p."));
148       else if (__isnan(__x))
149         return std::numeric_limits<_Tp>::quiet_NaN();
150       else if (__m == 0)
151         return __poly_legendre_p(__l, __x);
152       else
153         {
154           _Tp __p_mm = _Tp(1);
155           if (__m > 0)
156             {
157               //  Two square roots seem more accurate more of the time
158               //  than just one.
159               _Tp __root = std::sqrt(_Tp(1) - __x) * std::sqrt(_Tp(1) + __x);
160               _Tp __fact = _Tp(1);
161               for (unsigned int __i = 1; __i <= __m; ++__i)
162                 {
163                   __p_mm *= -__fact * __root;
164                   __fact += _Tp(2);
165                 }
166             }
167           if (__l == __m)
168             return __p_mm;
169
170           _Tp __p_mp1m = _Tp(2 * __m + 1) * __x * __p_mm;
171           if (__l == __m + 1)
172             return __p_mp1m;
173
174           _Tp __p_lm2m = __p_mm;
175           _Tp __P_lm1m = __p_mp1m;
176           _Tp __p_lm = _Tp(0);
177           for (unsigned int __j = __m + 2; __j <= __l; ++__j)
178             {
179               __p_lm = (_Tp(2 * __j - 1) * __x * __P_lm1m
180                       - _Tp(__j + __m - 1) * __p_lm2m) / _Tp(__j - __m);
181               __p_lm2m = __P_lm1m;
182               __P_lm1m = __p_lm;
183             }
184
185           return __p_lm;
186         }
187     }
188
189
190     /**
191      *   @brief  Return the spherical associated Legendre function.
192      * 
193      *   The spherical associated Legendre function of @f$ l @f$, @f$ m @f$,
194      *   and @f$ \theta @f$ is defined as @f$ Y_l^m(\theta,0) @f$ where
195      *   @f[
196      *      Y_l^m(\theta,\phi) = (-1)^m[\frac{(2l+1)}{4\pi}
197      *                                  \frac{(l-m)!}{(l+m)!}]
198      *                     P_l^m(\cos\theta) \exp^{im\phi}
199      *   @f]
200      *   is the spherical harmonic function and @f$ P_l^m(x) @f$ is the
201      *   associated Legendre function.
202      * 
203      *   This function differs from the associated Legendre function by
204      *   argument (@f$x = \cos(\theta)@f$) and by a normalization factor
205      *   but this factor is rather large for large @f$ l @f$ and @f$ m @f$
206      *   and so this function is stable for larger differences of @f$ l @f$
207      *   and @f$ m @f$.
208      * 
209      *   @param  l  The order of the spherical associated Legendre function.
210      *              @f$ l >= 0 @f$.
211      *   @param  m  The order of the spherical associated Legendre function.
212      *              @f$ m <= l @f$.
213      *   @param  theta  The radian angle argument of the spherical associated
214      *                  Legendre function.
215      */
216     template <typename _Tp>
217     _Tp
218     __sph_legendre(const unsigned int __l, const unsigned int __m,
219                    const _Tp __theta)
220     {
221       if (__isnan(__theta))
222         return std::numeric_limits<_Tp>::quiet_NaN();
223
224       const _Tp __x = std::cos(__theta);
225
226       if (__l < __m)
227         {
228           std::__throw_domain_error(__N("Bad argument "
229                                         "in __sph_legendre."));
230         }
231       else if (__m == 0)
232         {
233           _Tp __P = __poly_legendre_p(__l, __x);
234           _Tp __fact = std::sqrt(_Tp(2 * __l + 1)
235                      / (_Tp(4) * __numeric_constants<_Tp>::__pi()));
236           __P *= __fact;
237           return __P;
238         }
239       else if (__x == _Tp(1) || __x == -_Tp(1))
240         {
241           //  m > 0 here
242           return _Tp(0);
243         }
244       else
245         {
246           // m > 0 and |x| < 1 here
247
248           // Starting value for recursion.
249           // Y_m^m(x) = sqrt( (2m+1)/(4pi m) gamma(m+1/2)/gamma(m) )
250           //             (-1)^m (1-x^2)^(m/2) / pi^(1/4)
251           const _Tp __sgn = ( __m % 2 == 1 ? -_Tp(1) : _Tp(1));
252           const _Tp __y_mp1m_factor = __x * std::sqrt(_Tp(2 * __m + 3));
253 #if _GLIBCXX_USE_C99_MATH_TR1
254           const _Tp __lncirc = std::tr1::log1p(-__x * __x);
255 #else
256           const _Tp __lncirc = std::log(_Tp(1) - __x * __x);
257 #endif
258           //  Gamma(m+1/2) / Gamma(m)
259 #if _GLIBCXX_USE_C99_MATH_TR1
260           const _Tp __lnpoch = std::tr1::lgamma(_Tp(__m + _Tp(0.5L)))
261                              - std::tr1::lgamma(_Tp(__m));
262 #else
263           const _Tp __lnpoch = __log_gamma(_Tp(__m + _Tp(0.5L)))
264                              - __log_gamma(_Tp(__m));
265 #endif
266           const _Tp __lnpre_val =
267                     -_Tp(0.25L) * __numeric_constants<_Tp>::__lnpi()
268                     + _Tp(0.5L) * (__lnpoch + __m * __lncirc);
269           _Tp __sr = std::sqrt((_Tp(2) + _Tp(1) / __m)
270                    / (_Tp(4) * __numeric_constants<_Tp>::__pi()));
271           _Tp __y_mm = __sgn * __sr * std::exp(__lnpre_val);
272           _Tp __y_mp1m = __y_mp1m_factor * __y_mm;
273
274           if (__l == __m)
275             {
276               return __y_mm;
277             }
278           else if (__l == __m + 1)
279             {
280               return __y_mp1m;
281             }
282           else
283             {
284               _Tp __y_lm = _Tp(0);
285
286               // Compute Y_l^m, l > m+1, upward recursion on l.
287               for ( int __ll = __m + 2; __ll <= __l; ++__ll)
288                 {
289                   const _Tp __rat1 = _Tp(__ll - __m) / _Tp(__ll + __m);
290                   const _Tp __rat2 = _Tp(__ll - __m - 1) / _Tp(__ll + __m - 1);
291                   const _Tp __fact1 = std::sqrt(__rat1 * _Tp(2 * __ll + 1)
292                                                        * _Tp(2 * __ll - 1));
293                   const _Tp __fact2 = std::sqrt(__rat1 * __rat2 * _Tp(2 * __ll + 1)
294                                                                 / _Tp(2 * __ll - 3));
295                   __y_lm = (__x * __y_mp1m * __fact1
296                          - (__ll + __m - 1) * __y_mm * __fact2) / _Tp(__ll - __m);
297                   __y_mm = __y_mp1m;
298                   __y_mp1m = __y_lm;
299                 }
300
301               return __y_lm;
302             }
303         }
304     }
305
306   } // namespace std::tr1::__detail
307 }
308 }
309
310 #endif // _GLIBCXX_TR1_LEGENDRE_FUNCTION_TCC