1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008
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 _GLIBCXX_TR1_POLY_LAGUERRE_TCC
48 #define _GLIBCXX_TR1_POLY_LAGUERRE_TCC 1
55 // [5.2] Special functions
57 // Implementation-space details.
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
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.
73 * This is from the GNU Scientific Library.
75 template<typename _Tpa, typename _Tp>
77 __poly_laguerre_large_n(const unsigned __n, const _Tpa __alpha1,
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));
94 const _Tp __lg_b = __log_gamma(_Tp(__n) + __b);
95 const _Tp __lnfact = __log_gamma(_Tp(__n + 1));
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
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;
115 * @brief Evaluate the polynomial based on the confluent hypergeometric
116 * function in a safe way, with no restriction on the arguments.
118 * The associated Laguerre function is defined by
120 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
121 * _1F_1(-n; \alpha + 1; x)
123 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
124 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
126 * This function assumes x != 0.
128 * This is from the GNU Scientific Library.
130 template<typename _Tpa, typename _Tp>
132 __poly_laguerre_hyperg(const unsigned int __n, const _Tpa __alpha1,
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)));
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;
147 for (int __k = int(__n) - 1; __k >= 0; --__k)
149 __term *= ((__b + _Tp(__k)) / _Tp(int(__n) - __k))
150 * _Tp(__k + 1) / __mx;
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$
163 * The associated Laguerre function is defined by
165 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
166 * _1F_1(-n; \alpha + 1; x)
168 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
169 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
171 * The associated Laguerre polynomial is defined for integral
172 * @f$ \alpha = m @f$ by:
174 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
176 * where the Laguerre polynomial is defined by:
178 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
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.
187 template<typename _Tpa, typename _Tp>
189 __poly_laguerre_recursion(const unsigned int __n,
190 const _Tpa __alpha1, const _Tp __x)
197 // Compute l_1^alpha.
198 _Tp __l_1 = -__x + _Tp(1) + _Tp(__alpha1);
202 // Compute l_n^alpha by recursion on n.
206 for (unsigned int __nn = 2; __nn <= __n; ++__nn)
208 __l_n = (_Tp(2 * __nn - 1) + _Tp(__alpha1) - __x)
210 - (_Tp(__nn - 1) + _Tp(__alpha1)) * __l_n2 / _Tp(__nn);
220 * @brief This routine returns the associated Laguerre polynomial
221 * of order n, degree @f$ \alpha @f$: @f$ L_n^alpha(x) @f$.
223 * The associated Laguerre function is defined by
225 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
226 * _1F_1(-n; \alpha + 1; x)
228 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
229 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
231 * The associated Laguerre polynomial is defined for integral
232 * @f$ \alpha = m @f$ by:
234 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
236 * where the Laguerre polynomial is defined by:
238 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
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.
247 template<typename _Tpa, typename _Tp>
249 __poly_laguerre(const unsigned int __n, const _Tpa __alpha1,
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();
261 return _Tp(1) + _Tp(__alpha1) - __x;
262 else if (__x == _Tp(0))
264 _Tp __prod = _Tp(__alpha1) + _Tp(1);
265 for (unsigned int __k = 2; __k <= __n; ++__k)
266 __prod *= (_Tp(__alpha1) + _Tp(__k)) / _Tp(__k);
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);
276 return __poly_laguerre_hyperg(__n, __alpha1, __x);
281 * @brief This routine returns the associated Laguerre polynomial
282 * of order n, degree m: @f$ L_n^m(x) @f$.
284 * The associated Laguerre polynomial is defined for integral
285 * @f$ \alpha = m @f$ by:
287 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
289 * where the Laguerre polynomial is defined by:
291 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
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.
300 template<typename _Tp>
302 __assoc_laguerre(const unsigned int __n, const unsigned int __m,
305 return __poly_laguerre<unsigned int, _Tp>(__n, __m, __x);
310 * @brief This routine returns the Laguerre polynomial
311 * of order n: @f$ L_n(x) @f$.
313 * The Laguerre polynomial is defined by:
315 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
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
323 template<typename _Tp>
325 __laguerre(const unsigned int __n, const _Tp __x)
327 return __poly_laguerre<unsigned int, _Tp>(__n, 0, __x);
330 } // namespace std::tr1::__detail
334 #endif // _GLIBCXX_TR1_POLY_LAGUERRE_TCC