Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / tr1 / poly_laguerre.tcc
blob24cdd1813215d7adf6a343b65ef22f0f81338c09
1 // Special functions -*- C++ -*-
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.
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,
20 // USA.
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/poly_laguerre.tcc
32  *  This is an internal header file, included by other library headers.
33  *  You should not attempt to use it directly.
34  */
37 // ISO C++ 14882 TR1: 5.2  Special functions
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 13, pp. 509-510, Section 22 pp. 773-802
45 //   (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
47 #ifndef _GLIBCXX_TR1_POLY_LAGUERRE_TCC
48 #define _GLIBCXX_TR1_POLY_LAGUERRE_TCC 1
50 namespace std
52 namespace tr1
55   // [5.2] Special functions
57   // Implementation-space details.
58   namespace __detail
59   {
62     /**
63      *   @brief This routine returns the associated Laguerre polynomial 
64      *          of order @f$ n @f$, degree @f$ \alpha @f$ for large n.
65      *   Abramowitz & Stegun, 13.5.21
66      *
67      *   @param __n The order of the Laguerre function.
68      *   @param __alpha The degree of the Laguerre function.
69      *   @param __x The argument of the Laguerre function.
70      *   @return The value of the Laguerre function of order n,
71      *           degree @f$ \alpha @f$, and argument x.
72      *
73      *  This is from the GNU Scientific Library.
74      */
75     template<typename _Tpa, typename _Tp>
76     _Tp
77     __poly_laguerre_large_n(const unsigned __n, const _Tpa __alpha1,
78                             const _Tp __x)
79     {
80       const _Tp __a = -_Tp(__n);
81       const _Tp __b = _Tp(__alpha1) + _Tp(1);
82       const _Tp __eta = _Tp(2) * __b - _Tp(4) * __a;
83       const _Tp __cos2th = __x / __eta;
84       const _Tp __sin2th = _Tp(1) - __cos2th;
85       const _Tp __th = std::acos(std::sqrt(__cos2th));
86       const _Tp __pre_h = __numeric_constants<_Tp>::__pi_2()
87                         * __numeric_constants<_Tp>::__pi_2()
88                         * __eta * __eta * __cos2th * __sin2th;
90 #if _GLIBCXX_USE_C99_MATH_TR1
91       const _Tp __lg_b = std::tr1::lgamma(_Tp(__n) + __b);
92       const _Tp __lnfact = std::tr1::lgamma(_Tp(__n + 1));
93 #else
94       const _Tp __lg_b = __log_gamma(_Tp(__n) + __b);
95       const _Tp __lnfact = __log_gamma(_Tp(__n + 1));
96 #endif
98       _Tp __pre_term1 = _Tp(0.5L) * (_Tp(1) - __b)
99                       * std::log(_Tp(0.25L) * __x * __eta);
100       _Tp __pre_term2 = _Tp(0.25L) * std::log(__pre_h);
101       _Tp __lnpre = __lg_b - __lnfact + _Tp(0.5L) * __x
102                       + __pre_term1 - __pre_term2;
103       _Tp __ser_term1 = std::sin(__a * __numeric_constants<_Tp>::__pi());
104       _Tp __ser_term2 = std::sin(_Tp(0.25L) * __eta
105                               * (_Tp(2) * __th
106                                - std::sin(_Tp(2) * __th))
107                                + __numeric_constants<_Tp>::__pi_4());
108       _Tp __ser = __ser_term1 + __ser_term2;
110       return std::exp(__lnpre) * __ser;
111     }
114     /**
115      *  @brief  Evaluate the polynomial based on the confluent hypergeometric
116      *          function in a safe way, with no restriction on the arguments.
117      *
118      *   The associated Laguerre function is defined by
119      *   @f[
120      *       L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
121      *                       _1F_1(-n; \alpha + 1; x)
122      *   @f]
123      *   where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
124      *   @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
125      *
126      *  This function assumes x != 0.
127      *
128      *  This is from the GNU Scientific Library.
129      */
130     template<typename _Tpa, typename _Tp>
131     _Tp
132     __poly_laguerre_hyperg(const unsigned int __n, const _Tpa __alpha1,
133                            const _Tp __x)
134     {
135       const _Tp __b = _Tp(__alpha1) + _Tp(1);
136       const _Tp __mx = -__x;
137       const _Tp __tc_sgn = (__x < _Tp(0) ? _Tp(1)
138                          : ((__n % 2 == 1) ? -_Tp(1) : _Tp(1)));
139       //  Get |x|^n/n!
140       _Tp __tc = _Tp(1);
141       const _Tp __ax = std::abs(__x);
142       for (unsigned int __k = 1; __k <= __n; ++__k)
143         __tc *= (__ax / __k);
145       _Tp __term = __tc * __tc_sgn;
146       _Tp __sum = __term;
147       for (int __k = int(__n) - 1; __k >= 0; --__k)
148         {
149           __term *= ((__b + _Tp(__k)) / _Tp(int(__n) - __k))
150                   * _Tp(__k + 1) / __mx;
151           __sum += __term;
152         }
154       return __sum;
155     }
158     /**
159      *   @brief This routine returns the associated Laguerre polynomial 
160      *          of order @f$ n @f$, degree @f$ \alpha @f$: @f$ L_n^\alpha(x) @f$
161      *          by recursion.
162      *
163      *   The associated Laguerre function is defined by
164      *   @f[
165      *       L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
166      *                       _1F_1(-n; \alpha + 1; x)
167      *   @f]
168      *   where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
169      *   @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
170      *
171      *   The associated Laguerre polynomial is defined for integral
172      *   @f$ \alpha = m @f$ by:
173      *   @f[
174      *       L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
175      *   @f]
176      *   where the Laguerre polynomial is defined by:
177      *   @f[
178      *       L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
179      *   @f]
180      *
181      *   @param __n The order of the Laguerre function.
182      *   @param __alpha The degree of the Laguerre function.
183      *   @param __x The argument of the Laguerre function.
184      *   @return The value of the Laguerre function of order n,
185      *           degree @f$ \alpha @f$, and argument x.
186      */
187     template<typename _Tpa, typename _Tp>
188     _Tp
189     __poly_laguerre_recursion(const unsigned int __n,
190                               const _Tpa __alpha1, const _Tp __x)
191     {
192       //   Compute l_0.
193       _Tp __l_0 = _Tp(1);
194       if  (__n == 0)
195         return __l_0;
197       //  Compute l_1^alpha.
198       _Tp __l_1 = -__x + _Tp(1) + _Tp(__alpha1);
199       if  (__n == 1)
200         return __l_1;
202       //  Compute l_n^alpha by recursion on n.
203       _Tp __l_n2 = __l_0;
204       _Tp __l_n1 = __l_1;
205       _Tp __l_n = _Tp(0);
206       for  (unsigned int __nn = 2; __nn <= __n; ++__nn)
207         {
208             __l_n = (_Tp(2 * __nn - 1) + _Tp(__alpha1) - __x)
209                   * __l_n1 / _Tp(__nn)
210                   - (_Tp(__nn - 1) + _Tp(__alpha1)) * __l_n2 / _Tp(__nn);
211             __l_n2 = __l_n1;
212             __l_n1 = __l_n;
213         }
215       return __l_n;
216     }
219     /**
220      *   @brief This routine returns the associated Laguerre polynomial
221      *          of order n, degree @f$ \alpha @f$: @f$ L_n^alpha(x) @f$.
222      *
223      *   The associated Laguerre function is defined by
224      *   @f[
225      *       L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
226      *                       _1F_1(-n; \alpha + 1; x)
227      *   @f]
228      *   where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
229      *   @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
230      *
231      *   The associated Laguerre polynomial is defined for integral
232      *   @f$ \alpha = m @f$ by:
233      *   @f[
234      *       L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
235      *   @f]
236      *   where the Laguerre polynomial is defined by:
237      *   @f[
238      *       L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
239      *   @f]
240      *
241      *   @param __n The order of the Laguerre function.
242      *   @param __alpha The degree of the Laguerre function.
243      *   @param __x The argument of the Laguerre function.
244      *   @return The value of the Laguerre function of order n,
245      *           degree @f$ \alpha @f$, and argument x.
246      */
247     template<typename _Tpa, typename _Tp>
248     inline _Tp
249     __poly_laguerre(const unsigned int __n, const _Tpa __alpha1,
250                     const _Tp __x)
251     {
252       if (__x < _Tp(0))
253         std::__throw_domain_error(__N("Negative argument "
254                                       "in __poly_laguerre."));
255       //  Return NaN on NaN input.
256       else if (__isnan(__x))
257         return std::numeric_limits<_Tp>::quiet_NaN();
258       else if (__n == 0)
259         return _Tp(1);
260       else if (__n == 1)
261         return _Tp(1) + _Tp(__alpha1) - __x;
262       else if (__x == _Tp(0))
263         {
264           _Tp __prod = _Tp(__alpha1) + _Tp(1);
265           for (unsigned int __k = 2; __k <= __n; ++__k)
266             __prod *= (_Tp(__alpha1) + _Tp(__k)) / _Tp(__k);
267           return __prod;
268         }
269       else if (__n > 10000000 && _Tp(__alpha1) > -_Tp(1)
270             && __x < _Tp(2) * (_Tp(__alpha1) + _Tp(1)) + _Tp(4 * __n))
271         return __poly_laguerre_large_n(__n, __alpha1, __x);
272       else if (_Tp(__alpha1) >= _Tp(0)
273            || (__x > _Tp(0) && _Tp(__alpha1) < -_Tp(__n + 1)))
274         return __poly_laguerre_recursion(__n, __alpha1, __x);
275       else
276         return __poly_laguerre_hyperg(__n, __alpha1, __x);
277     }
280     /**
281      *   @brief This routine returns the associated Laguerre polynomial
282      *          of order n, degree m: @f$ L_n^m(x) @f$.
283      *
284      *   The associated Laguerre polynomial is defined for integral
285      *   @f$ \alpha = m @f$ by:
286      *   @f[
287      *       L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
288      *   @f]
289      *   where the Laguerre polynomial is defined by:
290      *   @f[
291      *       L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
292      *   @f]
293      *
294      *   @param __n The order of the Laguerre polynomial.
295      *   @param __m The degree of the Laguerre polynomial.
296      *   @param __x The argument of the Laguerre polynomial.
297      *   @return The value of the associated Laguerre polynomial of order n,
298      *           degree m, and argument x.
299      */
300     template<typename _Tp>
301     inline _Tp
302     __assoc_laguerre(const unsigned int __n, const unsigned int __m,
303                      const _Tp __x)
304     {
305       return __poly_laguerre<unsigned int, _Tp>(__n, __m, __x);
306     }
309     /**
310      *   @brief This routine returns the Laguerre polynomial
311      *          of order n: @f$ L_n(x) @f$.
312      *
313      *   The Laguerre polynomial is defined by:
314      *   @f[
315      *       L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
316      *   @f]
317      *
318      *   @param __n The order of the Laguerre polynomial.
319      *   @param __x The argument of the Laguerre polynomial.
320      *   @return The value of the Laguerre polynomial of order n
321      *           and argument x.
322      */
323     template<typename _Tp>
324     inline _Tp
325     __laguerre(const unsigned int __n, const _Tp __x)
326     {
327       return __poly_laguerre<unsigned int, _Tp>(__n, 0, __x);
328     }
330   } // namespace std::tr1::__detail
334 #endif // _GLIBCXX_TR1_POLY_LAGUERRE_TCC