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.

166 lines
5.2KB

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