The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

162 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace dsp
  16. {
  17. /**
  18. A class representing a polynomial
  19. @tags{DSP}
  20. */
  21. template <typename FloatingType>
  22. class Polynomial
  23. {
  24. public:
  25. //==============================================================================
  26. /** Creates a new polynomial which will always evaluate to zero. */
  27. Polynomial()
  28. {
  29. coeffs.add (0);
  30. }
  31. /** Creates a new polynomial with given coefficients.
  32. @param numCoefficients The number of coefficients stored in coefficients.
  33. This is also the order of the returned polynomial.
  34. @param coefficients The coefficients which will be used by the newly
  35. created polynomial. The Polynomial class will keep
  36. a private copy of the coefficients.
  37. */
  38. Polynomial (const FloatingType* coefficients, int numCoefficients)
  39. : coeffs (coefficients, numCoefficients)
  40. {
  41. jassert (! coeffs.isEmpty());
  42. }
  43. /** Creates a copy of another polynomial. */
  44. Polynomial (const Polynomial&) = default;
  45. /** Creates a copy of another polynomial. */
  46. Polynomial (Polynomial&&) = default;
  47. /** Creates a copy of another polynomial. */
  48. Polynomial& operator= (const Polynomial&) = default;
  49. /** Creates a copy of another polynomial. */
  50. Polynomial& operator= (Polynomial&&) = default;
  51. /** Creates a new polynomial with coefficients by a C++11 initializer list.
  52. This function can be used in the following way:
  53. Polynomial<float> p ({0.5f, -0.3f, 0.2f});
  54. */
  55. template <typename... Values>
  56. Polynomial (Values... items) : coeffs (items...)
  57. {
  58. jassert (! coeffs.isEmpty());
  59. }
  60. //==============================================================================
  61. /** Returns a single coefficient of the receiver for reading */
  62. FloatingType operator[] (int index) const noexcept { return coeffs.getUnchecked (index); }
  63. /** Returns a single coefficient of the receiver for modifying. */
  64. FloatingType& operator[] (int index) noexcept { return coeffs.getReference (index); }
  65. /** Evaluates the value of the polynomial at a single point x. */
  66. FloatingType operator() (FloatingType x) const noexcept
  67. {
  68. // Horner's method
  69. FloatingType y (0);
  70. for (int i = coeffs.size(); --i >= 0;)
  71. y = (x * y) + coeffs.getUnchecked(i);
  72. return y;
  73. }
  74. /** Returns the order of the polynomial. */
  75. int getOrder() noexcept
  76. {
  77. return coeffs.size() - 1;
  78. }
  79. //==============================================================================
  80. /** Returns the polynomial with all its coefficients multiplied with a gain factor */
  81. Polynomial<FloatingType> withGain (double gain) const
  82. {
  83. auto result = *this;
  84. for (auto& c : result.coeffs)
  85. c *= gain;
  86. return result;
  87. }
  88. /** Returns the sum of this polynomial with another */
  89. Polynomial<FloatingType> getSumWith (const Polynomial<FloatingType>& other) const
  90. {
  91. if (coeffs.size() < other.coeffs.size())
  92. return other.getSumWith (*this);
  93. auto result = *this;
  94. for (int i = 0; i < other.coeffs.size(); ++i)
  95. result[i] += other[i];
  96. return result;
  97. }
  98. /** computes the product of two polynomials and return the result */
  99. Polynomial<FloatingType> getProductWith (const Polynomial<FloatingType>& other) const
  100. {
  101. Polynomial<FloatingType> result;
  102. result.coeffs.clearQuick();
  103. auto N1 = coeffs.size();
  104. auto N2 = other.coeffs.size();
  105. auto Nmax = jmax (N1, N2);
  106. auto N = N1 + N2 - 1;
  107. for (int i = 0; i < N; ++i)
  108. {
  109. FloatingType value (0);
  110. for (int j = 0; j < Nmax; ++j)
  111. if (j >= 0 && j < N1 && i - j >= 0 && i - j < N2)
  112. value = value + (*this)[j] * other[i - j];
  113. result.coeffs.add (value);
  114. }
  115. return result;
  116. }
  117. private:
  118. //==============================================================================
  119. Array<FloatingType> coeffs;
  120. JUCE_LEAK_DETECTOR (Polynomial)
  121. };
  122. } // namespace dsp
  123. } // namespace juce