OpenShot Audio Library | OpenShotAudio  0.3.2
juce_Polynomial.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 namespace dsp
30 {
31 
37 template <typename FloatingType>
39 {
40 public:
41  //==============================================================================
44  {
45  coeffs.add (0);
46  }
47 
56  Polynomial (const FloatingType* coefficients, int numCoefficients)
57  : coeffs (coefficients, numCoefficients)
58  {
59  jassert (! coeffs.isEmpty());
60  }
61 
63  Polynomial (const Polynomial&) = default;
64 
66  Polynomial (Polynomial&&) = default;
67 
69  Polynomial& operator= (const Polynomial&) = default;
70 
73 
78  template <typename... Values>
79  Polynomial (Values... items) : coeffs (items...)
80  {
81  jassert (! coeffs.isEmpty());
82  }
83 
84  //==============================================================================
86  FloatingType operator[] (int index) const noexcept { return coeffs.getUnchecked (index); }
87 
89  FloatingType& operator[] (int index) noexcept { return coeffs.getReference (index); }
90 
92  FloatingType operator() (FloatingType x) const noexcept
93  {
94  // Horner's method
95  FloatingType y (0);
96 
97  for (int i = coeffs.size(); --i >= 0;)
98  y = (x * y) + coeffs.getUnchecked(i);
99 
100  return y;
101  }
102 
104  int getOrder() noexcept
105  {
106  return coeffs.size() - 1;
107  }
108 
109  //==============================================================================
111  Polynomial<FloatingType> withGain (double gain) const
112  {
113  auto result = *this;
114 
115  for (auto& c : result.coeffs)
116  c *= gain;
117 
118  return result;
119  }
120 
123  {
124  if (coeffs.size() < other.coeffs.size())
125  return other.getSumWith (*this);
126 
127  auto result = *this;
128 
129  for (int i = 0; i < other.coeffs.size(); ++i)
130  result[i] += other[i];
131 
132  return result;
133  }
134 
137  {
139  result.coeffs.clearQuick();
140 
141  auto N1 = coeffs.size();
142  auto N2 = other.coeffs.size();
143  auto Nmax = jmax (N1, N2);
144 
145  auto N = N1 + N2 - 1;
146 
147  for (int i = 0; i < N; ++i)
148  {
149  FloatingType value (0);
150 
151  for (int j = 0; j < Nmax; ++j)
152  if (j >= 0 && j < N1 && i - j >= 0 && i - j < N2)
153  value = value + (*this)[j] * other[i - j];
154 
155  result.coeffs.add (value);
156  }
157 
158  return result;
159  }
160 
161 private:
162  //==============================================================================
163  Array<FloatingType> coeffs;
164 
165  JUCE_LEAK_DETECTOR (Polynomial)
166 };
167 
168 } // namespace dsp
169 } // namespace juce
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