OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
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>
38 class Polynomial
39{
40 public:
41 //==============================================================================
43 Polynomial()
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
72 Polynomial& operator= (Polynomial&&) = default;
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
122 Polynomial<FloatingType> getSumWith (const Polynomial<FloatingType>& other) const
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
136 Polynomial<FloatingType> getProductWith (const Polynomial<FloatingType>& other) const
137 {
138 Polynomial<FloatingType> result;
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
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:39
juce::dsp::Polynomial::operator=
Polynomial & operator=(const Polynomial &)=default
juce::dsp::Polynomial::Polynomial
Polynomial(Values... items)
Definition: juce_Polynomial.h:79
juce::dsp::Polynomial::Polynomial
Polynomial()
Definition: juce_Polynomial.h:43
juce::dsp::Polynomial::operator[]
FloatingType operator[](int index) const noexcept
Definition: juce_Polynomial.h:86
juce::dsp::Polynomial::Polynomial
Polynomial(const Polynomial &)=default
juce::dsp::Polynomial::getSumWith
Polynomial< FloatingType > getSumWith(const Polynomial< FloatingType > &other) const
Definition: juce_Polynomial.h:122
juce::dsp::Polynomial::withGain
Polynomial< FloatingType > withGain(double gain) const
Definition: juce_Polynomial.h:111
juce::dsp::Polynomial::Polynomial
Polynomial(const FloatingType *coefficients, int numCoefficients)
Definition: juce_Polynomial.h:56
juce::dsp::Polynomial::getProductWith
Polynomial< FloatingType > getProductWith(const Polynomial< FloatingType > &other) const
Definition: juce_Polynomial.h:136
juce::dsp::Polynomial::Polynomial
Polynomial(Polynomial &&)=default
juce::dsp::Polynomial::getOrder
int getOrder() noexcept
Definition: juce_Polynomial.h:104
juce::dsp::Polynomial::operator()
FloatingType operator()(FloatingType x) const noexcept
Definition: juce_Polynomial.h:92