hermite.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // Recurrence 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. using promoted_type = tools::promote_args_t<T1, T2, T3>;
  21. return (2 * promoted_type(x) * promoted_type(Hn) - 2 * n * promoted_type(Hnm1));
  22. }
  23. namespace detail{
  24. // Implement Hermite polynomials via recurrence:
  25. template <class T>
  26. T hermite_imp(unsigned n, T x)
  27. {
  28. T p0 = 1;
  29. T p1 = 2 * x;
  30. if(n == 0)
  31. return p0;
  32. unsigned c = 1;
  33. while(c < n)
  34. {
  35. std::swap(p0, p1);
  36. p1 = static_cast<T>(hermite_next(c, x, p0, p1));
  37. ++c;
  38. }
  39. return p1;
  40. }
  41. } // namespace detail
  42. template <class T, class Policy>
  43. inline typename tools::promote_args<T>::type
  44. hermite(unsigned n, T x, const Policy&)
  45. {
  46. typedef typename tools::promote_args<T>::type result_type;
  47. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  48. return policies::checked_narrowing_cast<result_type, Policy>(detail::hermite_imp(n, static_cast<value_type>(x)), "boost::math::hermite<%1%>(unsigned, %1%)");
  49. }
  50. template <class T>
  51. inline typename tools::promote_args<T>::type
  52. hermite(unsigned n, T x)
  53. {
  54. return boost::math::hermite(n, x, policies::policy<>());
  55. }
  56. } // namespace math
  57. } // namespace boost
  58. #endif // BOOST_MATH_SPECIAL_HERMITE_HPP