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/poly_laguerre.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) 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 _TR1_POLY_LAGUERRE_TCC
48 #define _TR1_POLY_LAGUERRE_TCC 1
52 _GLIBCXX_BEGIN_NAMESPACE(_GLIBCXX_TR1)
54 // [5.2] Special functions
57 * @ingroup tr1_math_spec_func
62 // Implementation-space details.
69 * @brief This routine returns the associated Laguerre polynomial
70 * of order @f$ n @f$, degree @f$ \alpha @f$ for large n.
71 * Abramowitz & Stegun, 13.5.21
73 * @param __n The order of the Laguerre function.
74 * @param __alpha The degree of the Laguerre function.
75 * @param __x The argument of the Laguerre function.
76 * @return The value of the Laguerre function of order n,
77 * degree @f$ \alpha @f$, and argument x.
79 * This is from the GNU Scientific Library.
81 template<typename _Tpa, typename _Tp>
83 __poly_laguerre_large_n(const unsigned __n, const _Tpa __alpha1,
86 const _Tp __a = -_Tp(__n);
87 const _Tp __b = _Tp(__alpha1) + _Tp(1);
88 const _Tp __eta = _Tp(2) * __b - _Tp(4) * __a;
89 const _Tp __cos2th = __x / __eta;
90 const _Tp __sin2th = _Tp(1) - __cos2th;
91 const _Tp __th = std::acos(std::sqrt(__cos2th));
92 const _Tp __pre_h = __numeric_constants<_Tp>::__pi_2()
93 * __numeric_constants<_Tp>::__pi_2()
94 * __eta * __eta * __cos2th * __sin2th;
96 #if _GLIBCXX_USE_C99_MATH_TR1
97 const _Tp __lg_b = std::_GLIBCXX_TR1::lgamma(_Tp(__n) + __b);
98 const _Tp __lnfact = std::_GLIBCXX_TR1::lgamma(_Tp(__n + 1));
100 const _Tp __lg_b = __log_gamma(_Tp(__n) + __b);
101 const _Tp __lnfact = __log_gamma(_Tp(__n + 1));
104 _Tp __pre_term1 = _Tp(0.5L) * (_Tp(1) - __b)
105 * std::log(_Tp(0.25L) * __x * __eta);
106 _Tp __pre_term2 = _Tp(0.25L) * std::log(__pre_h);
107 _Tp __lnpre = __lg_b - __lnfact + _Tp(0.5L) * __x
108 + __pre_term1 - __pre_term2;
109 _Tp __ser_term1 = std::sin(__a * __numeric_constants<_Tp>::__pi());
110 _Tp __ser_term2 = std::sin(_Tp(0.25L) * __eta
112 - std::sin(_Tp(2) * __th))
113 + __numeric_constants<_Tp>::__pi_4());
114 _Tp __ser = __ser_term1 + __ser_term2;
116 return std::exp(__lnpre) * __ser;
121 * @brief Evaluate the polynomial based on the confluent hypergeometric
122 * function in a safe way, with no restriction on the arguments.
124 * The associated Laguerre function is defined by
126 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
127 * _1F_1(-n; \alpha + 1; x)
129 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
130 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
132 * This function assumes x != 0.
134 * This is from the GNU Scientific Library.
136 template<typename _Tpa, typename _Tp>
138 __poly_laguerre_hyperg(const unsigned int __n, const _Tpa __alpha1,
141 const _Tp __b = _Tp(__alpha1) + _Tp(1);
142 const _Tp __mx = -__x;
143 const _Tp __tc_sgn = (__x < _Tp(0) ? _Tp(1)
144 : ((__n % 2 == 1) ? -_Tp(1) : _Tp(1)));
147 const _Tp __ax = std::abs(__x);
148 for (unsigned int __k = 1; __k <= __n; ++__k)
149 __tc *= (__ax / __k);
151 _Tp __term = __tc * __tc_sgn;
153 for (int __k = int(__n) - 1; __k >= 0; --__k)
155 __term *= ((__b + _Tp(__k)) / _Tp(int(__n) - __k))
156 * _Tp(__k + 1) / __mx;
165 * @brief This routine returns the associated Laguerre polynomial
166 * of order @f$ n @f$, degree @f$ \alpha @f$: @f$ L_n^\alpha(x) @f$
169 * The associated Laguerre function is defined by
171 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
172 * _1F_1(-n; \alpha + 1; x)
174 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
175 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
177 * The associated Laguerre polynomial is defined for integral
178 * @f$ \alpha = m @f$ by:
180 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
182 * where the Laguerre polynomial is defined by:
184 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
187 * @param __n The order of the Laguerre function.
188 * @param __alpha The degree of the Laguerre function.
189 * @param __x The argument of the Laguerre function.
190 * @return The value of the Laguerre function of order n,
191 * degree @f$ \alpha @f$, and argument x.
193 template<typename _Tpa, typename _Tp>
195 __poly_laguerre_recursion(const unsigned int __n,
196 const _Tpa __alpha1, const _Tp __x)
203 // Compute l_1^alpha.
204 _Tp __l_1 = -__x + _Tp(1) + _Tp(__alpha1);
208 // Compute l_n^alpha by recursion on n.
212 for (unsigned int __nn = 2; __nn <= __n; ++__nn)
214 __l_n = (_Tp(2 * __nn - 1) + _Tp(__alpha1) - __x)
216 - (_Tp(__nn - 1) + _Tp(__alpha1)) * __l_n2 / _Tp(__nn);
226 * @brief This routine returns the associated Laguerre polynomial
227 * of order n, degree @f$ \alpha @f$: @f$ L_n^alpha(x) @f$.
229 * The associated Laguerre function is defined by
231 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
232 * _1F_1(-n; \alpha + 1; x)
234 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
235 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
237 * The associated Laguerre polynomial is defined for integral
238 * @f$ \alpha = m @f$ by:
240 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
242 * where the Laguerre polynomial is defined by:
244 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
247 * @param __n The order of the Laguerre function.
248 * @param __alpha The degree of the Laguerre function.
249 * @param __x The argument of the Laguerre function.
250 * @return The value of the Laguerre function of order n,
251 * degree @f$ \alpha @f$, and argument x.
253 template<typename _Tpa, typename _Tp>
255 __poly_laguerre(const unsigned int __n, const _Tpa __alpha1,
259 std::__throw_domain_error(__N("Negative argument "
260 "in __poly_laguerre."));
261 // Return NaN on NaN input.
262 else if (__isnan(__x))
263 return std::numeric_limits<_Tp>::quiet_NaN();
267 return _Tp(1) + _Tp(__alpha1) - __x;
268 else if (__x == _Tp(0))
270 _Tp __prod = _Tp(__alpha1) + _Tp(1);
271 for (unsigned int __k = 2; __k <= __n; ++__k)
272 __prod *= (_Tp(__alpha1) + _Tp(__k)) / _Tp(__k);
275 else if (__n > 10000000 && _Tp(__alpha1) > -_Tp(1)
276 && __x < _Tp(2) * (_Tp(__alpha1) + _Tp(1)) + _Tp(4 * __n))
277 return __poly_laguerre_large_n(__n, __alpha1, __x);
278 else if (_Tp(__alpha1) >= _Tp(0)
279 || (__x > _Tp(0) && _Tp(__alpha1) < -_Tp(__n + 1)))
280 return __poly_laguerre_recursion(__n, __alpha1, __x);
282 return __poly_laguerre_hyperg(__n, __alpha1, __x);
287 * @brief This routine returns the associated Laguerre polynomial
288 * of order n, degree m: @f$ L_n^m @f$.
290 * The associated Laguerre polynomial is defined for integral
291 * @f$ \alpha = m @f$ by:
293 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
295 * where the Laguerre polynomial is defined by:
297 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
300 * @param __n The order of the Laguerre polynomial.
301 * @param __m The degree of the Laguerre polynomial.
302 * @param __x The argument of the Laguerre polynomial.
303 * @return The value of the associated Laguerre polynomial of order n,
304 * degree m, and argument x.
306 template<typename _Tp>
308 __assoc_laguerre(const unsigned int __n, const unsigned int __m,
311 return __poly_laguerre<unsigned int, _Tp>(__n, __m, __x);
316 * @brief This routine returns the associated Laguerre polynomial
317 * of order n: @f$ L_n(x) @f$.
319 * The Laguerre polynomial is defined by:
321 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
324 * @param __n The order of the Laguerre polynomial.
325 * @param __x The argument of the Laguerre polynomial.
326 * @return The value of the Laguerre polynomial of order n
329 template<typename _Tp>
331 __laguerre(const unsigned int __n, const _Tp __x)
333 return __poly_laguerre<unsigned int, _Tp>(__n, 0, __x);
336 } // namespace std::tr1::__detail
338 /* @} */ // group tr1_math_spec_func
340 _GLIBCXX_END_NAMESPACE
343 #endif // _TR1_POLY_LAGUERRE_TCC