laguerre.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_SPECIAL_LAGUERRE_HPP
  6. #define BOOST_MATH_SPECIAL_LAGUERRE_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/tools/config.hpp>
  12. #include <boost/math/policies/error_handling.hpp>
  13. namespace boost{
  14. namespace math{
  15. // Recurrance relation for Laguerre polynomials:
  16. template <class T1, class T2, class T3>
  17. inline typename tools::promote_args<T1, T2, T3>::type
  18. laguerre_next(unsigned n, T1 x, T2 Ln, T3 Lnm1)
  19. {
  20. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  21. return ((2 * n + 1 - result_type(x)) * result_type(Ln) - n * result_type(Lnm1)) / (n + 1);
  22. }
  23. namespace detail{
  24. // Implement Laguerre polynomials via recurrance:
  25. template <class T>
  26. T laguerre_imp(unsigned n, T x)
  27. {
  28. T p0 = 1;
  29. T p1 = 1 - x;
  30. if(n == 0)
  31. return p0;
  32. unsigned c = 1;
  33. while(c < n)
  34. {
  35. std::swap(p0, p1);
  36. p1 = laguerre_next(c, x, p0, p1);
  37. ++c;
  38. }
  39. return p1;
  40. }
  41. template <class T, class Policy>
  42. inline typename tools::promote_args<T>::type
  43. laguerre(unsigned n, T x, const Policy&, const mpl::true_&)
  44. {
  45. typedef typename tools::promote_args<T>::type result_type;
  46. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  47. return policies::checked_narrowing_cast<result_type, Policy>(detail::laguerre_imp(n, static_cast<value_type>(x)), "boost::math::laguerre<%1%>(unsigned, %1%)");
  48. }
  49. template <class T>
  50. inline typename tools::promote_args<T>::type
  51. laguerre(unsigned n, unsigned m, T x, const mpl::false_&)
  52. {
  53. return boost::math::laguerre(n, m, x, policies::policy<>());
  54. }
  55. } // namespace detail
  56. template <class T>
  57. inline typename tools::promote_args<T>::type
  58. laguerre(unsigned n, T x)
  59. {
  60. return laguerre(n, x, policies::policy<>());
  61. }
  62. // Recurrence for associated polynomials:
  63. template <class T1, class T2, class T3>
  64. inline typename tools::promote_args<T1, T2, T3>::type
  65. laguerre_next(unsigned n, unsigned l, T1 x, T2 Pl, T3 Plm1)
  66. {
  67. typedef typename tools::promote_args<T1, T2, T3>::type result_type;
  68. return ((2 * n + l + 1 - result_type(x)) * result_type(Pl) - (n + l) * result_type(Plm1)) / (n+1);
  69. }
  70. namespace detail{
  71. // Laguerre Associated Polynomial:
  72. template <class T, class Policy>
  73. T laguerre_imp(unsigned n, unsigned m, T x, const Policy& pol)
  74. {
  75. // Special cases:
  76. if(m == 0)
  77. return boost::math::laguerre(n, x, pol);
  78. T p0 = 1;
  79. if(n == 0)
  80. return p0;
  81. T p1 = m + 1 - x;
  82. unsigned c = 1;
  83. while(c < n)
  84. {
  85. std::swap(p0, p1);
  86. p1 = laguerre_next(c, m, x, p0, p1);
  87. ++c;
  88. }
  89. return p1;
  90. }
  91. }
  92. template <class T, class Policy>
  93. inline typename tools::promote_args<T>::type
  94. laguerre(unsigned n, unsigned m, T x, const Policy& pol)
  95. {
  96. typedef typename tools::promote_args<T>::type result_type;
  97. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  98. return policies::checked_narrowing_cast<result_type, Policy>(detail::laguerre_imp(n, m, static_cast<value_type>(x), pol), "boost::math::laguerre<%1%>(unsigned, unsigned, %1%)");
  99. }
  100. template <class T1, class T2>
  101. inline typename laguerre_result<T1, T2>::type
  102. laguerre(unsigned n, T1 m, T2 x)
  103. {
  104. typedef typename policies::is_policy<T2>::type tag_type;
  105. return detail::laguerre(n, m, x, tag_type());
  106. }
  107. } // namespace math
  108. } // namespace boost
  109. #endif // BOOST_MATH_SPECIAL_LAGUERRE_HPP