💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.6 KiB

  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. #ifndef ANTKEEPER_MATH_POLYNOMIAL_HPP
  20. #define ANTKEEPER_MATH_POLYNOMIAL_HPP
  21. #include "math/numbers.hpp"
  22. #include "math/map.hpp"
  23. namespace math {
  24. /// Polynomial functions.
  25. namespace polynomial {
  26. /**
  27. * Evaluates a polynomial using Horner's method.
  28. *
  29. * @param first,last Range of polynomial coefficients, in descending order of degree.
  30. * @param x Variable value.
  31. * @return Evaluation of P(x).
  32. *
  33. * @see https://en.wikipedia.org/wiki/Horner%27s_method
  34. */
  35. template <class InputIt, class T>
  36. [[nodiscard]] constexpr T horner(InputIt first, InputIt last, T x)
  37. {
  38. T y = *first;
  39. for (++first; first != last; ++first)
  40. y = y * x + *first;
  41. return y;
  42. }
  43. /** Chebychev polynomials.
  44. *
  45. * @see https://en.wikipedia.org/wiki/Chebyshev_polynomials
  46. */
  47. namespace chebyshev {
  48. /**
  49. * Evaluates a Chebyshev polynomial.
  50. *
  51. * @param[in] first,last Range of Chebychev polynomial coefficients.
  52. * @param[in] x Value on the interval `[-1, 1]`.
  53. *
  54. * @return Evaluated value.
  55. */
  56. template <class InputIt, class T>
  57. [[nodiscard]] T evaluate(InputIt first, InputIt last, T x)
  58. {
  59. T y = *(first++);
  60. y += *(first++) * x;
  61. T n2 = T(1), n1 = x, n0;
  62. x *= T(2);
  63. for (; first != last; n2 = n1, n1 = n0)
  64. {
  65. n0 = x * n1 - n2;
  66. y += *(first++) * n0;
  67. }
  68. return y;
  69. }
  70. /**
  71. * Evaluates a Chebyshev polynomial.
  72. *
  73. * @param first,last Range of Chebychev polynomial coefficients.
  74. * @param min,max Domain of the approximated function.
  75. * @param x Value on the interval `[min, max]`.
  76. *
  77. * @return Evaluated value.
  78. */
  79. template <class InputIt, class T>
  80. [[nodiscard]] T evaluate(InputIt first, InputIt last, T min, T max, T x)
  81. {
  82. return evaluate<InputIt, T>(first, last, math::map<T>(x, min, max, T(-1), T(1)));
  83. }
  84. } // namespace chebyshev
  85. } // namespace polynomial
  86. } // namespace math
  87. #endif // ANTKEEPER_MATH_POLYNOMIAL_HPP