Loading...
Searching...
No Matches
juce_Polynomial.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce::dsp
{
template <typename FloatingType>
class Polynomial
{
public:
//==============================================================================
Polynomial()
{
coeffs.add (0);
}
Polynomial (const FloatingType* coefficients, int numCoefficients)
: coeffs (coefficients, numCoefficients)
{
jassert (! coeffs.isEmpty());
}
Polynomial (const Polynomial&) = default;
Polynomial (Polynomial&&) = default;
Polynomial& operator= (const Polynomial&) = default;
Polynomial& operator= (Polynomial&&) = default;
template <typename... Values>
Polynomial (Values... items) : coeffs (items...)
{
jassert (! coeffs.isEmpty());
}
//==============================================================================
FloatingType operator[] (int index) const noexcept { return coeffs.getUnchecked (index); }
FloatingType& operator[] (int index) noexcept { return coeffs.getReference (index); }
FloatingType operator() (FloatingType x) const noexcept
{
// Horner's method
FloatingType y (0);
for (int i = coeffs.size(); --i >= 0;)
y = (x * y) + coeffs.getUnchecked (i);
return y;
}
int getOrder() noexcept
{
return coeffs.size() - 1;
}
//==============================================================================
Polynomial<FloatingType> withGain (double gain) const
{
auto result = *this;
for (auto& c : result.coeffs)
c *= gain;
return result;
}
Polynomial<FloatingType> getSumWith (const Polynomial<FloatingType>& other) const
{
if (coeffs.size() < other.coeffs.size())
return other.getSumWith (*this);
auto result = *this;
for (int i = 0; i < other.coeffs.size(); ++i)
result[i] += other[i];
return result;
}
Polynomial<FloatingType> getProductWith (const Polynomial<FloatingType>& other) const
{
Polynomial<FloatingType> result;
result.coeffs.clearQuick();
auto N1 = coeffs.size();
auto N2 = other.coeffs.size();
auto Nmax = jmax (N1, N2);
auto N = N1 + N2 - 1;
for (int i = 0; i < N; ++i)
{
FloatingType value (0);
for (int j = 0; j < Nmax; ++j)
if (j >= 0 && j < N1 && i - j >= 0 && i - j < N2)
value = value + (*this)[j] * other[i - j];
result.coeffs.add (value);
}
return result;
}
private:
//==============================================================================
Array<FloatingType> coeffs;
JUCE_LEAK_DETECTOR (Polynomial)
};
} // namespace juce::dsp
juce::Array
Definition juce_Array.h:56
juce::Array::clearQuick
void clearQuick()
Definition juce_Array.h:198
juce::Array::add
void add(const ElementType &newElement)
Definition juce_Array.h:418
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