hermite.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_HERMITE_HPP
  6. #define BOOST_MATH_SPECIAL_HERMITE_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 Hermite polynomials:
  16. template <class T1, class T2, class T3>
  17. inline typename tools::promote_args<T1, T2, T3>::type
  18. hermite_next(unsigned n, T1 x, T2 Hn, T3 Hnm1)
  19. {
  20. return (2 * x * Hn - 2 * n * Hnm1);
  21. }
  22. namespace detail{
  23. // Implement Hermite polynomials via recurrance:
  24. template <class T>
  25. T hermite_imp(unsigned n, T x)
  26. {
  27. T p0 = 1;
  28. T p1 = 2 * x;
  29. if(n == 0)
  30. return p0;
  31. unsigned c = 1;
  32. while(c < n)
  33. {
  34. std::swap(p0, p1);
  35. p1 = hermite_next(c, x, p0, p1);
  36. ++c;
  37. }
  38. return p1;
  39. }
  40. } // namespace detail
  41. template <class T, class Policy>
  42. inline typename tools::promote_args<T>::type
  43. hermite(unsigned n, T x, const Policy&)
  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::hermite_imp(n, static_cast<value_type>(x)), "boost::math::hermite<%1%>(unsigned, %1%)");
  48. }
  49. template <class T>
  50. inline typename tools::promote_args<T>::type
  51. hermite(unsigned n, T x)
  52. {
  53. return boost::math::hermite(n, x, policies::policy<>());
  54. }
  55. } // namespace math
  56. } // namespace boost
  57. #endif // BOOST_MATH_SPECIAL_HERMITE_HPP