hermite.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // (C) Copyright John Maddock 2006.
  2. // (C) Copyright Matt Borland 2024.
  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. #ifndef BOOST_MATH_SPECIAL_HERMITE_HPP
  7. #define BOOST_MATH_SPECIAL_HERMITE_HPP
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #endif
  11. #include <boost/math/tools/config.hpp>
  12. #include <boost/math/tools/promotion.hpp>
  13. #include <boost/math/special_functions/math_fwd.hpp>
  14. #include <boost/math/policies/error_handling.hpp>
  15. namespace boost{
  16. namespace math{
  17. // Recurrence relation for Hermite polynomials:
  18. template <class T1, class T2, class T3>
  19. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2, T3>::type
  20. hermite_next(unsigned n, T1 x, T2 Hn, T3 Hnm1)
  21. {
  22. using promoted_type = tools::promote_args_t<T1, T2, T3>;
  23. return (2 * promoted_type(x) * promoted_type(Hn) - 2 * n * promoted_type(Hnm1));
  24. }
  25. namespace detail{
  26. // Implement Hermite polynomials via recurrence:
  27. template <class T>
  28. BOOST_MATH_GPU_ENABLED T hermite_imp(unsigned n, T x)
  29. {
  30. T p0 = 1;
  31. T p1 = 2 * x;
  32. if(n == 0)
  33. return p0;
  34. unsigned c = 1;
  35. while(c < n)
  36. {
  37. BOOST_MATH_GPU_SAFE_SWAP(p0, p1);
  38. p1 = static_cast<T>(hermite_next(c, x, p0, p1));
  39. ++c;
  40. }
  41. return p1;
  42. }
  43. } // namespace detail
  44. template <class T, class Policy>
  45. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type
  46. hermite(unsigned n, T x, const Policy&)
  47. {
  48. typedef typename tools::promote_args<T>::type result_type;
  49. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  50. return policies::checked_narrowing_cast<result_type, Policy>(detail::hermite_imp(n, static_cast<value_type>(x)), "boost::math::hermite<%1%>(unsigned, %1%)");
  51. }
  52. template <class T>
  53. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type
  54. hermite(unsigned n, T x)
  55. {
  56. return boost::math::hermite(n, x, policies::policy<>());
  57. }
  58. } // namespace math
  59. } // namespace boost
  60. #endif // BOOST_MATH_SPECIAL_HERMITE_HPP