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