Antkeeper  0.0.1
polynomial.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_MATH_POLYNOMIAL_HPP
21 #define ANTKEEPER_MATH_POLYNOMIAL_HPP
22 
23 #include <engine/math/numbers.hpp>
24 #include <engine/math/map.hpp>
25 
26 namespace math {
27 
29 namespace polynomial {
30 
40 template <class InputIt, class T>
41 [[nodiscard]] constexpr T horner(InputIt first, InputIt last, T x)
42 {
43  T y = *first;
44  for (++first; first != last; ++first)
45  {
46  y = y * x + *first;
47  }
48  return y;
49 }
50 
55 namespace chebyshev {
56 
65  template <class InputIt, class T>
66  [[nodiscard]] T evaluate(InputIt first, InputIt last, T x)
67  {
68  T y = *(first++);
69  y += *(first++) * x;
70 
71  T n2 = T{1};
72  T n1 = x;
73  T n0;
74 
75  x += x;
76 
77  for (; first != last; n2 = n1, n1 = n0)
78  {
79  n0 = x * n1 - n2;
80  y += *(first++) * n0;
81  }
82 
83  return y;
84  }
85 
95  template <class InputIt, class T>
96  [[nodiscard]] T evaluate(InputIt first, InputIt last, T min, T max, T x)
97  {
98  return evaluate<InputIt, T>(first, last, math::map<T>(x, min, max, T{-1}, T{1}));
99  }
100 
101 } // namespace chebyshev
102 
103 } // namespace polynomial
104 } // namespace math
105 
106 #endif // ANTKEEPER_MATH_POLYNOMIAL_HPP
T evaluate(InputIt first, InputIt last, T x)
Evaluates a Chebyshev polynomial.
Definition: polynomial.hpp:66
constexpr T horner(InputIt first, InputIt last, T x)
Evaluates a polynomial using Horner's method.
Definition: polynomial.hpp:41
Mathematical functions and data types.
Definition: angles.hpp:26
constexpr vector< T, N > max(const vector< T, N > &x, const vector< T, N > &y)
Returns a vector containing the maximum elements of two vectors.
Definition: vector.hpp:1328
constexpr vector< T, N > min(const vector< T, N > &x, const vector< T, N > &y)
Returns a vector containing the minimum elements of two vectors.
Definition: vector.hpp:1347