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.

163 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. /**
  20. A class representing a polynomial
  21. */
  22. template <typename FloatingType>
  23. class Polynomial
  24. {
  25. public:
  26. //==============================================================================
  27. /** Creates a new polynomial which will always evaluate to zero. */
  28. Polynomial()
  29. {
  30. coeffs.add (0);
  31. }
  32. /** Creates a new polynomial with given coefficients.
  33. @param numCoefficients The number of coefficients stored in coefficients.
  34. This is also the order of the returned polynomial.
  35. @param coefficients The coefficients which will be used by the newly
  36. created polynomial. The Polynomial class will keep
  37. a private copy of the coefficients.
  38. */
  39. Polynomial (const FloatingType* coefficients, int numCoefficients)
  40. : coeffs (coefficients, numCoefficients)
  41. {
  42. jassert (! coeffs.isEmpty());
  43. }
  44. /** Creates a copy of another polynomial. */
  45. Polynomial (const Polynomial&) = default;
  46. /** Creates a copy of another polynomial. */
  47. Polynomial (Polynomial&&) = default;
  48. /** Creates a copy of another polynomial. */
  49. Polynomial& operator= (const Polynomial&) = default;
  50. /** Creates a copy of another polynomial. */
  51. Polynomial& operator= (Polynomial&&) = default;
  52. #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS || defined(DOXYGEN)
  53. /** Creates a new polynomial with coefficients by a C++11 initializer list.
  54. This function can be used in the following way:
  55. Polynomial<float> p ({0.5f, -0.3f, 0.2f});
  56. */
  57. template <typename TypeToCreateFrom>
  58. Polynomial (const std::initializer_list<TypeToCreateFrom>& items) : coeffs (items)
  59. {
  60. jassert (! coeffs.isEmpty());
  61. }
  62. #endif
  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 = {};
  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. };