JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Polynomial.h
Go to the documentation of this file.
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
26namespace juce::dsp
27{
28
34template <typename FloatingType>
36{
37public:
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
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 //==============================================================================
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
158private:
159 //==============================================================================
160 Array<FloatingType> coeffs;
161
163};
164
165} // namespace juce::dsp
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:252
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
Definition juce_Array.h:222
void clearQuick()
Removes all elements from the array without freeing the array's allocated storage.
Definition juce_Array.h:198
int size() const noexcept
Returns the current number of elements in the array.
Definition juce_Array.h:215
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition juce_Array.h:418
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:267
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.
#define JUCE_LEAK_DETECTOR(OwnerClass)
This macro lets you embed a leak-detecting object inside a class.
#define jassert(expression)
Platform-independent assertion macro.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88