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.

170 lines
5.2KB

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