OpenShot Library | OpenShotAudio 0.2.2
juce_Polynomial.h
1
2/** @weakgroup juce_dsp-maths
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 By using JUCE, you agree to the terms of both the JUCE 5 End-User License
15 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
16 27th April 2017).
17
18 End User License Agreement: www.juce.com/juce-5-licence
19 Privacy Policy: www.juce.com/juce-5-privacy-policy
20
21 Or: You may also use this code under the terms of the GPL v3 (see
22 www.gnu.org/licenses).
23
24 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
25 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
26 DISCLAIMED.
27
28 ==============================================================================
29*/
30
31namespace juce
32{
33namespace dsp
34{
35
36/**
37 A class representing a polynomial
38
39 @tags{DSP}
40*/
41template <typename FloatingType>
43{
44public:
45 //==============================================================================
46 /** Creates a new polynomial which will always evaluate to zero. */
48 {
49 coeffs.add (0);
50 }
51
52 /** Creates a new polynomial with given coefficients.
53
54 @param numCoefficients The number of coefficients stored in coefficients.
55 This is also the order of the returned polynomial.
56 @param coefficients The coefficients which will be used by the newly
57 created polynomial. The Polynomial class will keep
58 a private copy of the coefficients.
59 */
60 Polynomial (const FloatingType* coefficients, int numCoefficients)
61 : coeffs (coefficients, numCoefficients)
62 {
63 jassert (! coeffs.isEmpty());
64 }
65
66 /** Creates a copy of another polynomial. */
67 Polynomial (const Polynomial&) = default;
68
69 /** Creates a copy of another polynomial. */
70 Polynomial (Polynomial&&) = default;
71
72 /** Creates a copy of another polynomial. */
73 Polynomial& operator= (const Polynomial&) = default;
74
75 /** Creates a copy of another polynomial. */
77
78 /** Creates a new polynomial with coefficients by a C++11 initializer list.
79 This function can be used in the following way:
80 Polynomial<float> p ({0.5f, -0.3f, 0.2f});
81 */
82 template <typename... Values>
83 Polynomial (Values... items) : coeffs (items...)
84 {
85 jassert (! coeffs.isEmpty());
86 }
87
88 //==============================================================================
89 /** Returns a single coefficient of the receiver for reading */
90 FloatingType operator[] (int index) const noexcept { return coeffs.getUnchecked (index); }
91
92 /** Returns a single coefficient of the receiver for modifying. */
93 FloatingType& operator[] (int index) noexcept { return coeffs.getReference (index); }
94
95 /** Evaluates the value of the polynomial at a single point x. */
96 FloatingType operator() (FloatingType x) const noexcept
97 {
98 // Horner's method
99 FloatingType y (0);
100
101 for (int i = coeffs.size(); --i >= 0;)
102 y = (x * y) + coeffs.getUnchecked(i);
103
104 return y;
105 }
106
107 /** Returns the order of the polynomial. */
108 int getOrder() noexcept
109 {
110 return coeffs.size() - 1;
111 }
112
113 //==============================================================================
114 /** Returns the polynomial with all its coefficients multiplied with a gain factor */
116 {
117 auto result = *this;
118
119 for (auto& c : result.coeffs)
120 c *= gain;
121
122 return result;
123 }
124
125 /** Returns the sum of this polynomial with another */
127 {
128 if (coeffs.size() < other.coeffs.size())
129 return other.getSumWith (*this);
130
131 auto result = *this;
132
133 for (int i = 0; i < other.coeffs.size(); ++i)
134 result[i] += other[i];
135
136 return result;
137 }
138
139 /** computes the product of two polynomials and return the result */
141 {
143 result.coeffs.clearQuick();
144
145 auto N1 = coeffs.size();
146 auto N2 = other.coeffs.size();
147 auto Nmax = jmax (N1, N2);
148
149 auto N = N1 + N2 - 1;
150
151 for (int i = 0; i < N; ++i)
152 {
153 FloatingType value (0);
154
155 for (int j = 0; j < Nmax; ++j)
156 if (j >= 0 && j < N1 && i - j >= 0 && i - j < N2)
157 value = value + (*this)[j] * other[i - j];
158
159 result.coeffs.add (value);
160 }
161
162 return result;
163 }
164
165private:
166 //==============================================================================
167 Array<FloatingType> coeffs;
168
169 JUCE_LEAK_DETECTOR (Polynomial)
170};
171
172} // namespace dsp
173} // namespace juce
174
175/** @}*/
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
Definition: juce_Array.h:256
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
Definition: juce_Array.h:226
void clearQuick()
Removes all elements from the array without freeing the array's allocated storage.
Definition: juce_Array.h:202
int size() const noexcept
Returns the current number of elements in the array.
Definition: juce_Array.h:219
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition: juce_Array.h:422
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Definition: juce_Array.h:271
A class representing a polynomial.
Polynomial & operator=(const Polynomial &)=default
Creates a copy of another polynomial.
Polynomial(Values... items)
Creates a new polynomial with coefficients by a C++11 initializer list.
Polynomial()
Creates a new polynomial which will always evaluate to zero.
FloatingType operator[](int index) const noexcept
Returns a single coefficient of the receiver for reading.
Polynomial(const Polynomial &)=default
Creates a copy of another polynomial.
Polynomial< FloatingType > getSumWith(const Polynomial< FloatingType > &other) const
Returns the sum of this polynomial with another.
Polynomial< FloatingType > withGain(double gain) const
Returns the polynomial with all its coefficients multiplied with a gain factor.
Polynomial(const FloatingType *coefficients, int numCoefficients)
Creates a new polynomial with given coefficients.
Polynomial< FloatingType > getProductWith(const Polynomial< FloatingType > &other) const
computes the product of two polynomials and return the result
Polynomial(Polynomial &&)=default
Creates a copy of another polynomial.
int getOrder() noexcept
Returns the order of the polynomial.
FloatingType operator()(FloatingType x) const noexcept
Evaluates the value of the polynomial at a single point x.