OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Polynomial.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11  Agreement and JUCE Privacy Policy.
12 
13  End User License Agreement: www.juce.com/juce-7-licence
14  Privacy Policy: www.juce.com/juce-privacy-policy
15 
16  Or: You may also use this code under the terms of the GPL v3 (see
17  www.gnu.org/licenses).
18 
19  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21  DISCLAIMED.
22 
23  ==============================================================================
24 */
25 
26 namespace juce::dsp
27 {
28 
34 template <typename FloatingType>
36 {
37 public:
38  //==============================================================================
41  {
42  coeffs.add (0);
43  }
44 
53  Polynomial (const FloatingType* coefficients, int numCoefficients)
54  : coeffs (coefficients, numCoefficients)
55  {
56  jassert (! coeffs.isEmpty());
57  }
58 
60  Polynomial (const Polynomial&) = default;
61 
63  Polynomial (Polynomial&&) = default;
64 
66  Polynomial& operator= (const Polynomial&) = default;
67 
70 
75  template <typename... Values>
76  Polynomial (Values... items) : coeffs (items...)
77  {
78  jassert (! coeffs.isEmpty());
79  }
80 
81  //==============================================================================
83  FloatingType operator[] (int index) const noexcept { return coeffs.getUnchecked (index); }
84 
86  FloatingType& operator[] (int index) noexcept { return coeffs.getReference (index); }
87 
89  FloatingType operator() (FloatingType x) const noexcept
90  {
91  // Horner's method
92  FloatingType y (0);
93 
94  for (int i = coeffs.size(); --i >= 0;)
95  y = (x * y) + coeffs.getUnchecked (i);
96 
97  return y;
98  }
99 
101  int getOrder() noexcept
102  {
103  return coeffs.size() - 1;
104  }
105 
106  //==============================================================================
108  Polynomial<FloatingType> withGain (double gain) const
109  {
110  auto result = *this;
111 
112  for (auto& c : result.coeffs)
113  c *= gain;
114 
115  return result;
116  }
117 
120  {
121  if (coeffs.size() < other.coeffs.size())
122  return other.getSumWith (*this);
123 
124  auto result = *this;
125 
126  for (int i = 0; i < other.coeffs.size(); ++i)
127  result[i] += other[i];
128 
129  return result;
130  }
131 
134  {
136  result.coeffs.clearQuick();
137 
138  auto N1 = coeffs.size();
139  auto N2 = other.coeffs.size();
140  auto Nmax = jmax (N1, N2);
141 
142  auto N = N1 + N2 - 1;
143 
144  for (int i = 0; i < N; ++i)
145  {
146  FloatingType value (0);
147 
148  for (int j = 0; j < Nmax; ++j)
149  if (j >= 0 && j < N1 && i - j >= 0 && i - j < N2)
150  value = value + (*this)[j] * other[i - j];
151 
152  result.coeffs.add (value);
153  }
154 
155  return result;
156  }
157 
158 private:
159  //==============================================================================
160  Array<FloatingType> coeffs;
161 
162  JUCE_LEAK_DETECTOR (Polynomial)
163 };
164 
165 } // namespace juce::dsp
ElementType getUnchecked(int index) const
Definition: juce_Array.h:252
bool isEmpty() const noexcept
Definition: juce_Array.h:222
void clearQuick()
Definition: juce_Array.h:198
int size() const noexcept
Definition: juce_Array.h:215
void add(const ElementType &newElement)
Definition: juce_Array.h:418
ElementType & getReference(int index) noexcept
Definition: juce_Array.h:267
Polynomial< FloatingType > getProductWith(const Polynomial< FloatingType > &other) const
Polynomial< FloatingType > withGain(double gain) const
Polynomial(Values... items)
FloatingType operator[](int index) const noexcept
Polynomial(const Polynomial &)=default
Polynomial< FloatingType > getSumWith(const Polynomial< FloatingType > &other) const
Polynomial & operator=(const Polynomial &)=default
Polynomial(const FloatingType *coefficients, int numCoefficients)
Polynomial(Polynomial &&)=default
int getOrder() noexcept
FloatingType operator()(FloatingType x) const noexcept