estrin.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright Thomas Dybdahl Ahle, Nick Thompson, Matt Borland, John Maddock, 2023
  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_TOOLS_ESTRIN_HPP
  8. #define BOOST_MATH_TOOLS_ESTRIN_HPP
  9. #include <array>
  10. #include <vector>
  11. #include <type_traits>
  12. #include <boost/math/tools/assert.hpp>
  13. namespace boost {
  14. namespace math {
  15. namespace tools {
  16. template <typename RandomAccessContainer1, typename RandomAccessContainer2, typename RealOrComplex>
  17. inline RealOrComplex evaluate_polynomial_estrin(RandomAccessContainer1 const &coeffs, RandomAccessContainer2 &scratch, RealOrComplex z) {
  18. // Does anyone care about the complex coefficients, real argument case?
  19. // I've never seen it used, and this static assert makes the error messages much better:
  20. static_assert(std::is_same<typename RandomAccessContainer2::value_type, RealOrComplex>::value,
  21. "The value type of the scratch space must be the same as the abscissa.");
  22. auto n = coeffs.size();
  23. BOOST_MATH_ASSERT_MSG(scratch.size() >= (n + 1) / 2, "The scratch space must be at least N+1/2");
  24. if (n == 0) {
  25. return static_cast<RealOrComplex>(0);
  26. }
  27. for (decltype(n) i = 0; i < n / 2; i++) {
  28. scratch[i] = coeffs[2 * i] + coeffs[2 * i + 1] * z;
  29. }
  30. if (n & 1) {
  31. scratch[n / 2] = coeffs[n - 1];
  32. }
  33. auto m = (n + 1) / 2;
  34. while (m != 1) {
  35. z = z * z;
  36. for (decltype(n) i = 0; i < m / 2; i++) {
  37. scratch[i] = scratch[2 * i] + scratch[2 * i + 1] * z;
  38. }
  39. if (m & 1) {
  40. scratch[m / 2] = scratch[m - 1];
  41. }
  42. m = (m + 1) / 2;
  43. }
  44. return scratch[0];
  45. }
  46. // The std::array template specialization doesn't need to allocate:
  47. template <typename RealOrComplex1, size_t n, typename RealOrComplex2>
  48. inline RealOrComplex2 evaluate_polynomial_estrin(const std::array<RealOrComplex1, n> &coeffs, RealOrComplex2 z) {
  49. std::array<RealOrComplex2, (n + 1) / 2> ds;
  50. return evaluate_polynomial_estrin(coeffs, ds, z);
  51. }
  52. template <typename RandomAccessContainer, typename RealOrComplex>
  53. inline RealOrComplex evaluate_polynomial_estrin(const RandomAccessContainer &coeffs, RealOrComplex z) {
  54. auto n = coeffs.size();
  55. // Normally, I'd make `ds` a RandomAccessContainer, but its value type needs to be RealOrComplex,
  56. // and the value_type of the passed RandomAccessContainer can just be Real.
  57. // Allocation of the std::vector is not ideal, but I have no other ideas at the moment:
  58. std::vector<RealOrComplex> ds((n + 1) / 2);
  59. return evaluate_polynomial_estrin(coeffs, ds, z);
  60. }
  61. } // namespace tools
  62. } // namespace math
  63. } // namespace boost
  64. #endif