123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #ifndef BOOST_MATH_TOOLS_ESTRIN_HPP
- #define BOOST_MATH_TOOLS_ESTRIN_HPP
- #include <array>
- #include <vector>
- #include <type_traits>
- #include <boost/math/tools/assert.hpp>
- namespace boost {
- namespace math {
- namespace tools {
- template <typename RandomAccessContainer1, typename RandomAccessContainer2, typename RealOrComplex>
- inline RealOrComplex evaluate_polynomial_estrin(RandomAccessContainer1 const &coeffs, RandomAccessContainer2 &scratch, RealOrComplex z) {
-
-
- static_assert(std::is_same<typename RandomAccessContainer2::value_type, RealOrComplex>::value,
- "The value type of the scratch space must be the same as the abscissa.");
- auto n = coeffs.size();
- BOOST_MATH_ASSERT_MSG(scratch.size() >= (n + 1) / 2, "The scratch space must be at least N+1/2");
- if (n == 0) {
- return static_cast<RealOrComplex>(0);
- }
- for (decltype(n) i = 0; i < n / 2; i++) {
- scratch[i] = coeffs[2 * i] + coeffs[2 * i + 1] * z;
- }
- if (n & 1) {
- scratch[n / 2] = coeffs[n - 1];
- }
- auto m = (n + 1) / 2;
- while (m != 1) {
- z = z * z;
- for (decltype(n) i = 0; i < m / 2; i++) {
- scratch[i] = scratch[2 * i] + scratch[2 * i + 1] * z;
- }
- if (m & 1) {
- scratch[m / 2] = scratch[m - 1];
- }
- m = (m + 1) / 2;
- }
- return scratch[0];
- }
- template <typename RealOrComplex1, size_t n, typename RealOrComplex2>
- inline RealOrComplex2 evaluate_polynomial_estrin(const std::array<RealOrComplex1, n> &coeffs, RealOrComplex2 z) {
- std::array<RealOrComplex2, (n + 1) / 2> ds;
- return evaluate_polynomial_estrin(coeffs, ds, z);
- }
- template <typename RandomAccessContainer, typename RealOrComplex>
- inline RealOrComplex evaluate_polynomial_estrin(const RandomAccessContainer &coeffs, RealOrComplex z) {
- auto n = coeffs.size();
-
-
-
- std::vector<RealOrComplex> ds((n + 1) / 2);
- return evaluate_polynomial_estrin(coeffs, ds, z);
- }
- }
- }
- }
- #endif
|