OpenShot Audio Library | OpenShotAudio: juce_Polynomial.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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
26namespace juce::dsp
27{
28
34template <typename FloatingType>
35 class Polynomial
36{
37public:
38 //==============================================================================
40 Polynomial()
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
69 Polynomial& operator= (Polynomial&&) = default;
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
119 Polynomial<FloatingType> getSumWith (const Polynomial<FloatingType>& other) const
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
133 Polynomial<FloatingType> getProductWith (const Polynomial<FloatingType>& other) const
134 {
135 Polynomial<FloatingType> result;
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
162 JUCE_LEAK_DETECTOR (Polynomial)
163};
164
165} // namespace juce::dsp
juce::Array
Definition juce_Array.h:56
juce::Array::getUnchecked
ElementType getUnchecked(int index) const
Definition juce_Array.h:252
juce::Array::isEmpty
bool isEmpty() const noexcept
Definition juce_Array.h:222
juce::Array::clearQuick
void clearQuick()
Definition juce_Array.h:198
juce::Array::size
int size() const noexcept
Definition juce_Array.h:215
juce::Array::add
void add(const ElementType &newElement)
Definition juce_Array.h:418
juce::Array::getReference
ElementType & getReference(int index) noexcept
Definition juce_Array.h:267
juce::dsp::Polynomial
Definition juce_Polynomial.h:36
juce::dsp::Polynomial::operator=
Polynomial & operator=(const Polynomial &)=default
juce::dsp::Polynomial::Polynomial
Polynomial(Values... items)
Definition juce_Polynomial.h:76
juce::dsp::Polynomial::Polynomial
Polynomial()
Definition juce_Polynomial.h:40
juce::dsp::Polynomial::operator[]
FloatingType operator[](int index) const noexcept
Definition juce_Polynomial.h:83
juce::dsp::Polynomial::Polynomial
Polynomial(const Polynomial &)=default
juce::dsp::Polynomial::getSumWith
Polynomial< FloatingType > getSumWith(const Polynomial< FloatingType > &other) const
Definition juce_Polynomial.h:119
juce::dsp::Polynomial::withGain
Polynomial< FloatingType > withGain(double gain) const
Definition juce_Polynomial.h:108
juce::dsp::Polynomial::Polynomial
Polynomial(const FloatingType *coefficients, int numCoefficients)
Definition juce_Polynomial.h:53
juce::dsp::Polynomial::getProductWith
Polynomial< FloatingType > getProductWith(const Polynomial< FloatingType > &other) const
Definition juce_Polynomial.h:133
juce::dsp::Polynomial::Polynomial
Polynomial(Polynomial &&)=default
juce::dsp::Polynomial::getOrder
int getOrder() noexcept
Definition juce_Polynomial.h:101
juce::dsp::Polynomial::operator()
FloatingType operator()(FloatingType x) const noexcept
Definition juce_Polynomial.h:89