poly_laguerre.tcc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Special functions -*- C++ -*-
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License and
  19. // a copy of the GCC Runtime Library Exception along with this program;
  20. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. // <http://www.gnu.org/licenses/>.
  22. /** @file tr1/poly_laguerre.tcc
  23. * This is an internal header file, included by other library headers.
  24. * Do not attempt to use it directly. @headername{tr1/cmath}
  25. */
  26. //
  27. // ISO C++ 14882 TR1: 5.2 Special functions
  28. //
  29. // Written by Edward Smith-Rowland based on:
  30. // (1) Handbook of Mathematical Functions,
  31. // Ed. Milton Abramowitz and Irene A. Stegun,
  32. // Dover Publications,
  33. // Section 13, pp. 509-510, Section 22 pp. 773-802
  34. // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
  35. #ifndef _GLIBCXX_TR1_POLY_LAGUERRE_TCC
  36. #define _GLIBCXX_TR1_POLY_LAGUERRE_TCC 1
  37. namespace std _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. #if _GLIBCXX_USE_STD_SPEC_FUNCS
  41. # define _GLIBCXX_MATH_NS ::std
  42. #elif defined(_GLIBCXX_TR1_CMATH)
  43. namespace tr1
  44. {
  45. # define _GLIBCXX_MATH_NS ::std::tr1
  46. #else
  47. # error do not include this header directly, use <cmath> or <tr1/cmath>
  48. #endif
  49. // [5.2] Special functions
  50. // Implementation-space details.
  51. namespace __detail
  52. {
  53. /**
  54. * @brief This routine returns the associated Laguerre polynomial
  55. * of order @f$ n @f$, degree @f$ \alpha @f$ for large n.
  56. * Abramowitz & Stegun, 13.5.21
  57. *
  58. * @param __n The order of the Laguerre function.
  59. * @param __alpha The degree of the Laguerre function.
  60. * @param __x The argument of the Laguerre function.
  61. * @return The value of the Laguerre function of order n,
  62. * degree @f$ \alpha @f$, and argument x.
  63. *
  64. * This is from the GNU Scientific Library.
  65. */
  66. template<typename _Tpa, typename _Tp>
  67. _Tp
  68. __poly_laguerre_large_n(unsigned __n, _Tpa __alpha1, _Tp __x)
  69. {
  70. const _Tp __a = -_Tp(__n);
  71. const _Tp __b = _Tp(__alpha1) + _Tp(1);
  72. const _Tp __eta = _Tp(2) * __b - _Tp(4) * __a;
  73. const _Tp __cos2th = __x / __eta;
  74. const _Tp __sin2th = _Tp(1) - __cos2th;
  75. const _Tp __th = std::acos(std::sqrt(__cos2th));
  76. const _Tp __pre_h = __numeric_constants<_Tp>::__pi_2()
  77. * __numeric_constants<_Tp>::__pi_2()
  78. * __eta * __eta * __cos2th * __sin2th;
  79. #if _GLIBCXX_USE_C99_MATH_TR1
  80. const _Tp __lg_b = _GLIBCXX_MATH_NS::lgamma(_Tp(__n) + __b);
  81. const _Tp __lnfact = _GLIBCXX_MATH_NS::lgamma(_Tp(__n + 1));
  82. #else
  83. const _Tp __lg_b = __log_gamma(_Tp(__n) + __b);
  84. const _Tp __lnfact = __log_gamma(_Tp(__n + 1));
  85. #endif
  86. _Tp __pre_term1 = _Tp(0.5L) * (_Tp(1) - __b)
  87. * std::log(_Tp(0.25L) * __x * __eta);
  88. _Tp __pre_term2 = _Tp(0.25L) * std::log(__pre_h);
  89. _Tp __lnpre = __lg_b - __lnfact + _Tp(0.5L) * __x
  90. + __pre_term1 - __pre_term2;
  91. _Tp __ser_term1 = std::sin(__a * __numeric_constants<_Tp>::__pi());
  92. _Tp __ser_term2 = std::sin(_Tp(0.25L) * __eta
  93. * (_Tp(2) * __th
  94. - std::sin(_Tp(2) * __th))
  95. + __numeric_constants<_Tp>::__pi_4());
  96. _Tp __ser = __ser_term1 + __ser_term2;
  97. return std::exp(__lnpre) * __ser;
  98. }
  99. /**
  100. * @brief Evaluate the polynomial based on the confluent hypergeometric
  101. * function in a safe way, with no restriction on the arguments.
  102. *
  103. * The associated Laguerre function is defined by
  104. * @f[
  105. * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
  106. * _1F_1(-n; \alpha + 1; x)
  107. * @f]
  108. * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
  109. * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
  110. *
  111. * This function assumes x != 0.
  112. *
  113. * This is from the GNU Scientific Library.
  114. */
  115. template<typename _Tpa, typename _Tp>
  116. _Tp
  117. __poly_laguerre_hyperg(unsigned int __n, _Tpa __alpha1, _Tp __x)
  118. {
  119. const _Tp __b = _Tp(__alpha1) + _Tp(1);
  120. const _Tp __mx = -__x;
  121. const _Tp __tc_sgn = (__x < _Tp(0) ? _Tp(1)
  122. : ((__n % 2 == 1) ? -_Tp(1) : _Tp(1)));
  123. // Get |x|^n/n!
  124. _Tp __tc = _Tp(1);
  125. const _Tp __ax = std::abs(__x);
  126. for (unsigned int __k = 1; __k <= __n; ++__k)
  127. __tc *= (__ax / __k);
  128. _Tp __term = __tc * __tc_sgn;
  129. _Tp __sum = __term;
  130. for (int __k = int(__n) - 1; __k >= 0; --__k)
  131. {
  132. __term *= ((__b + _Tp(__k)) / _Tp(int(__n) - __k))
  133. * _Tp(__k + 1) / __mx;
  134. __sum += __term;
  135. }
  136. return __sum;
  137. }
  138. /**
  139. * @brief This routine returns the associated Laguerre polynomial
  140. * of order @f$ n @f$, degree @f$ \alpha @f$: @f$ L_n^\alpha(x) @f$
  141. * by recursion.
  142. *
  143. * The associated Laguerre function is defined by
  144. * @f[
  145. * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
  146. * _1F_1(-n; \alpha + 1; x)
  147. * @f]
  148. * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
  149. * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
  150. *
  151. * The associated Laguerre polynomial is defined for integral
  152. * @f$ \alpha = m @f$ by:
  153. * @f[
  154. * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
  155. * @f]
  156. * where the Laguerre polynomial is defined by:
  157. * @f[
  158. * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
  159. * @f]
  160. *
  161. * @param __n The order of the Laguerre function.
  162. * @param __alpha The degree of the Laguerre function.
  163. * @param __x The argument of the Laguerre function.
  164. * @return The value of the Laguerre function of order n,
  165. * degree @f$ \alpha @f$, and argument x.
  166. */
  167. template<typename _Tpa, typename _Tp>
  168. _Tp
  169. __poly_laguerre_recursion(unsigned int __n, _Tpa __alpha1, _Tp __x)
  170. {
  171. // Compute l_0.
  172. _Tp __l_0 = _Tp(1);
  173. if (__n == 0)
  174. return __l_0;
  175. // Compute l_1^alpha.
  176. _Tp __l_1 = -__x + _Tp(1) + _Tp(__alpha1);
  177. if (__n == 1)
  178. return __l_1;
  179. // Compute l_n^alpha by recursion on n.
  180. _Tp __l_n2 = __l_0;
  181. _Tp __l_n1 = __l_1;
  182. _Tp __l_n = _Tp(0);
  183. for (unsigned int __nn = 2; __nn <= __n; ++__nn)
  184. {
  185. __l_n = (_Tp(2 * __nn - 1) + _Tp(__alpha1) - __x)
  186. * __l_n1 / _Tp(__nn)
  187. - (_Tp(__nn - 1) + _Tp(__alpha1)) * __l_n2 / _Tp(__nn);
  188. __l_n2 = __l_n1;
  189. __l_n1 = __l_n;
  190. }
  191. return __l_n;
  192. }
  193. /**
  194. * @brief This routine returns the associated Laguerre polynomial
  195. * of order n, degree @f$ \alpha @f$: @f$ L_n^alpha(x) @f$.
  196. *
  197. * The associated Laguerre function is defined by
  198. * @f[
  199. * L_n^\alpha(x) = \frac{(\alpha + 1)_n}{n!}
  200. * _1F_1(-n; \alpha + 1; x)
  201. * @f]
  202. * where @f$ (\alpha)_n @f$ is the Pochhammer symbol and
  203. * @f$ _1F_1(a; c; x) @f$ is the confluent hypergeometric function.
  204. *
  205. * The associated Laguerre polynomial is defined for integral
  206. * @f$ \alpha = m @f$ by:
  207. * @f[
  208. * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
  209. * @f]
  210. * where the Laguerre polynomial is defined by:
  211. * @f[
  212. * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
  213. * @f]
  214. *
  215. * @param __n The order of the Laguerre function.
  216. * @param __alpha The degree of the Laguerre function.
  217. * @param __x The argument of the Laguerre function.
  218. * @return The value of the Laguerre function of order n,
  219. * degree @f$ \alpha @f$, and argument x.
  220. */
  221. template<typename _Tpa, typename _Tp>
  222. _Tp
  223. __poly_laguerre(unsigned int __n, _Tpa __alpha1, _Tp __x)
  224. {
  225. if (__x < _Tp(0))
  226. std::__throw_domain_error(__N("Negative argument "
  227. "in __poly_laguerre."));
  228. // Return NaN on NaN input.
  229. else if (__isnan(__x))
  230. return std::numeric_limits<_Tp>::quiet_NaN();
  231. else if (__n == 0)
  232. return _Tp(1);
  233. else if (__n == 1)
  234. return _Tp(1) + _Tp(__alpha1) - __x;
  235. else if (__x == _Tp(0))
  236. {
  237. _Tp __prod = _Tp(__alpha1) + _Tp(1);
  238. for (unsigned int __k = 2; __k <= __n; ++__k)
  239. __prod *= (_Tp(__alpha1) + _Tp(__k)) / _Tp(__k);
  240. return __prod;
  241. }
  242. else if (__n > 10000000 && _Tp(__alpha1) > -_Tp(1)
  243. && __x < _Tp(2) * (_Tp(__alpha1) + _Tp(1)) + _Tp(4 * __n))
  244. return __poly_laguerre_large_n(__n, __alpha1, __x);
  245. else if (_Tp(__alpha1) >= _Tp(0)
  246. || (__x > _Tp(0) && _Tp(__alpha1) < -_Tp(__n + 1)))
  247. return __poly_laguerre_recursion(__n, __alpha1, __x);
  248. else
  249. return __poly_laguerre_hyperg(__n, __alpha1, __x);
  250. }
  251. /**
  252. * @brief This routine returns the associated Laguerre polynomial
  253. * of order n, degree m: @f$ L_n^m(x) @f$.
  254. *
  255. * The associated Laguerre polynomial is defined for integral
  256. * @f$ \alpha = m @f$ by:
  257. * @f[
  258. * L_n^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n + m}(x)
  259. * @f]
  260. * where the Laguerre polynomial is defined by:
  261. * @f[
  262. * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
  263. * @f]
  264. *
  265. * @param __n The order of the Laguerre polynomial.
  266. * @param __m The degree of the Laguerre polynomial.
  267. * @param __x The argument of the Laguerre polynomial.
  268. * @return The value of the associated Laguerre polynomial of order n,
  269. * degree m, and argument x.
  270. */
  271. template<typename _Tp>
  272. inline _Tp
  273. __assoc_laguerre(unsigned int __n, unsigned int __m, _Tp __x)
  274. { return __poly_laguerre<unsigned int, _Tp>(__n, __m, __x); }
  275. /**
  276. * @brief This routine returns the Laguerre polynomial
  277. * of order n: @f$ L_n(x) @f$.
  278. *
  279. * The Laguerre polynomial is defined by:
  280. * @f[
  281. * L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} (x^ne^{-x})
  282. * @f]
  283. *
  284. * @param __n The order of the Laguerre polynomial.
  285. * @param __x The argument of the Laguerre polynomial.
  286. * @return The value of the Laguerre polynomial of order n
  287. * and argument x.
  288. */
  289. template<typename _Tp>
  290. inline _Tp
  291. __laguerre(unsigned int __n, _Tp __x)
  292. { return __poly_laguerre<unsigned int, _Tp>(__n, 0, __x); }
  293. } // namespace __detail
  294. #undef _GLIBCXX_MATH_NS
  295. #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
  296. } // namespace tr1
  297. #endif
  298. _GLIBCXX_END_NAMESPACE_VERSION
  299. }
  300. #endif // _GLIBCXX_TR1_POLY_LAGUERRE_TCC