1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file tr1/poly_laguerre.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{tr1/cmath}
32 // ISO C++ 14882 TR1: 5.2 Special functions
35 // Written by Edward Smith-Rowland based on:
36 // (1) Handbook of Mathematical Functions,
37 // Ed. Milton Abramowitz and Irene A. Stegun,
38 // Dover Publications,
39 // Section 13, pp. 509-510, Section 22 pp. 773-802
40 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
42 #ifndef _GLIBCXX_TR1_POLY_LAGUERRE_TCC
43 #define _GLIBCXX_TR1_POLY_LAGUERRE_TCC 1
45 namespace std _GLIBCXX_VISIBILITY(default)
49 // [5.2] Special functions
51 // Implementation-space details.
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57 * @brief This routine returns the associated Laguerre polynomial
58 * of order @f$ n @f$, degree @f$ \alpha @f$ for large n.
59 * Abramowitz & Stegun, 13.5.21
61 * @param __n The order of the Laguerre function.
62 * @param __alpha The degree of the Laguerre function.
63 * @param __x The argument of the Laguerre function.
64 * @return The value of the Laguerre function of order n,
65 * degree @f$ \alpha @f$, and argument x.
67 * This is from the GNU Scientific Library.
69 template<typename _Tpa, typename _Tp>
71 __poly_laguerre_large_n(const unsigned __n, const _Tpa __alpha1,
74 const _Tp __a = -_Tp(__n);
75 const _Tp __b = _Tp(__alpha1) + _Tp(1);
76 const _Tp __eta = _Tp(2) * __b - _Tp(4) * __a;
77 const _Tp __cos2th = __x / __eta;
78 const _Tp __sin2th = _Tp(1) - __cos2th;
79 const _Tp __th = std::acos(std::sqrt(__cos2th));
80 const _Tp __pre_h = __numeric_constants<_Tp>::__pi_2()
81 * __numeric_constants<_Tp>::__pi_2()
82 * __eta * __eta * __cos2th * __sin2th;
84 #if _GLIBCXX_USE_C99_MATH_TR1
85 const _Tp __lg_b = std::tr1::lgamma(_Tp(__n) + __b);
86 const _Tp __lnfact = std::tr1::lgamma(_Tp(__n + 1));
88 const _Tp __lg_b = __log_gamma(_Tp(__n) + __b);
89 const _Tp __lnfact = __log_gamma(_Tp(__n + 1));
92 _Tp __pre_term1 = _Tp(0.5L) * (_Tp(1) - __b)
93 * std::log(_Tp(0.25L) * __x * __eta);
94 _Tp __pre_term2 = _Tp(0.25L) * std::log(__pre_h);
95 _Tp __lnpre = __lg_b - __lnfact + _Tp(0.5L) * __x
96 + __pre_term1 - __pre_term2;
97 _Tp __ser_term1 = std::sin(__a * __numeric_constants<_Tp>::__pi());
98 _Tp __ser_term2 = std::sin(_Tp(0.25L) * __eta
100 - std::sin(_Tp(2) * __th))
101 + __numeric_constants<_Tp>::__pi_4());
102 _Tp __ser = __ser_term1 + __ser_term2;
104 return std::exp(__lnpre) * __ser;
109 * @brief Evaluate the polynomial based on the confluent hypergeometric
110 * function in a safe way, with no restriction on the arguments.
112 * The associated Laguerre function is defined by
114 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
115 * _1F_1(-n; \alpha + 1; x)
117 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
118 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
120 * This function assumes x != 0.
122 * This is from the GNU Scientific Library.
124 template<typename _Tpa, typename _Tp>
126 __poly_laguerre_hyperg(const unsigned int __n, const _Tpa __alpha1,
129 const _Tp __b = _Tp(__alpha1) + _Tp(1);
130 const _Tp __mx = -__x;
131 const _Tp __tc_sgn = (__x < _Tp(0) ? _Tp(1)
132 : ((__n % 2 == 1) ? -_Tp(1) : _Tp(1)));
135 const _Tp __ax = std::abs(__x);
136 for (unsigned int __k = 1; __k <= __n; ++__k)
137 __tc *= (__ax / __k);
139 _Tp __term = __tc * __tc_sgn;
141 for (int __k = int(__n) - 1; __k >= 0; --__k)
143 __term *= ((__b + _Tp(__k)) / _Tp(int(__n) - __k))
144 * _Tp(__k + 1) / __mx;
153 * @brief This routine returns the associated Laguerre polynomial
154 * of order @f$ n @f$, degree @f$ \alpha @f$: @f$ L_n^\alpha(x) @f$
157 * The associated Laguerre function is defined by
159 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
160 * _1F_1(-n; \alpha + 1; x)
162 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
163 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
165 * The associated Laguerre polynomial is defined for integral
166 * @f$ \alpha = m @f$ by:
168 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
170 * where the Laguerre polynomial is defined by:
172 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
175 * @param __n The order of the Laguerre function.
176 * @param __alpha The degree of the Laguerre function.
177 * @param __x The argument of the Laguerre function.
178 * @return The value of the Laguerre function of order n,
179 * degree @f$ \alpha @f$, and argument x.
181 template<typename _Tpa, typename _Tp>
183 __poly_laguerre_recursion(const unsigned int __n,
184 const _Tpa __alpha1, const _Tp __x)
191 // Compute l_1^alpha.
192 _Tp __l_1 = -__x + _Tp(1) + _Tp(__alpha1);
196 // Compute l_n^alpha by recursion on n.
200 for (unsigned int __nn = 2; __nn <= __n; ++__nn)
202 __l_n = (_Tp(2 * __nn - 1) + _Tp(__alpha1) - __x)
204 - (_Tp(__nn - 1) + _Tp(__alpha1)) * __l_n2 / _Tp(__nn);
214 * @brief This routine returns the associated Laguerre polynomial
215 * of order n, degree @f$ \alpha @f$: @f$ L_n^alpha(x) @f$.
217 * The associated Laguerre function is defined by
219 * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
220 * _1F_1(-n; \alpha + 1; x)
222 * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
223 * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
225 * The associated Laguerre polynomial is defined for integral
226 * @f$ \alpha = m @f$ by:
228 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
230 * where the Laguerre polynomial is defined by:
232 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
235 * @param __n The order of the Laguerre function.
236 * @param __alpha The degree of the Laguerre function.
237 * @param __x The argument of the Laguerre function.
238 * @return The value of the Laguerre function of order n,
239 * degree @f$ \alpha @f$, and argument x.
241 template<typename _Tpa, typename _Tp>
243 __poly_laguerre(const unsigned int __n, const _Tpa __alpha1,
247 std::__throw_domain_error(__N("Negative argument "
248 "in __poly_laguerre."));
249 // Return NaN on NaN input.
250 else if (__isnan(__x))
251 return std::numeric_limits<_Tp>::quiet_NaN();
255 return _Tp(1) + _Tp(__alpha1) - __x;
256 else if (__x == _Tp(0))
258 _Tp __prod = _Tp(__alpha1) + _Tp(1);
259 for (unsigned int __k = 2; __k <= __n; ++__k)
260 __prod *= (_Tp(__alpha1) + _Tp(__k)) / _Tp(__k);
263 else if (__n > 10000000 && _Tp(__alpha1) > -_Tp(1)
264 && __x < _Tp(2) * (_Tp(__alpha1) + _Tp(1)) + _Tp(4 * __n))
265 return __poly_laguerre_large_n(__n, __alpha1, __x);
266 else if (_Tp(__alpha1) >= _Tp(0)
267 || (__x > _Tp(0) && _Tp(__alpha1) < -_Tp(__n + 1)))
268 return __poly_laguerre_recursion(__n, __alpha1, __x);
270 return __poly_laguerre_hyperg(__n, __alpha1, __x);
275 * @brief This routine returns the associated Laguerre polynomial
276 * of order n, degree m: @f$ L_n^m(x) @f$.
278 * The associated Laguerre polynomial is defined for integral
279 * @f$ \alpha = m @f$ by:
281 * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
283 * where the Laguerre polynomial is defined by:
285 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
288 * @param __n The order of the Laguerre polynomial.
289 * @param __m The degree of the Laguerre polynomial.
290 * @param __x The argument of the Laguerre polynomial.
291 * @return The value of the associated Laguerre polynomial of order n,
292 * degree m, and argument x.
294 template<typename _Tp>
296 __assoc_laguerre(const unsigned int __n, const unsigned int __m,
299 return __poly_laguerre<unsigned int, _Tp>(__n, __m, __x);
304 * @brief This routine returns the Laguerre polynomial
305 * of order n: @f$ L_n(x) @f$.
307 * The Laguerre polynomial is defined by:
309 * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
312 * @param __n The order of the Laguerre polynomial.
313 * @param __x The argument of the Laguerre polynomial.
314 * @return The value of the Laguerre polynomial of order n
317 template<typename _Tp>
319 __laguerre(const unsigned int __n, const _Tp __x)
321 return __poly_laguerre<unsigned int, _Tp>(__n, 0, __x);
324 _GLIBCXX_END_NAMESPACE_VERSION
325 } // namespace std::tr1::__detail
329 #endif // _GLIBCXX_TR1_POLY_LAGUERRE_TCC