44 #include <type_traits>
56 template<
typename dtype>
60 STATIC_ASSERT_ARITHMETIC(dtype);
87 coefficients_.push_back(1);
88 for (
auto value : inValues)
96 for (
auto value : inValues)
98 coefficients_.push_back(value);
111 double area(
double a,
double b)
const
118 auto polyIntegral =
integ();
119 return polyIntegral(b) - polyIntegral(a);
128 template<
typename dtypeOut>
133 const auto function = [](dtype value) -> dtypeOut
135 return static_cast<dtypeOut
>(value);
151 auto coefficientsCopy = coefficients_;
162 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
163 if (numCoefficients == 0)
167 if (numCoefficients == 1)
175 for (
uint32 i = 1; i < numCoefficients; ++i)
177 derivativeCofficients[counter++] = coefficients_[i] * i;
193 const auto numMeasurements = xValues.
size();
195 if (yValues.
size() != numMeasurements)
211 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
213 const auto xDouble =
static_cast<double>(xValues[measIdx]);
230 aInv = aTaInv.
dot(aT);
233 auto x = aInv.
dot(yValues.template astype<double>());
249 const auto numMeasurements = xValues.
size();
251 if (yValues.
size() != numMeasurements)
256 if (weights.
size() != numMeasurements)
277 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
279 const auto xDouble =
static_cast<double>(xValues[measIdx]);
289 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
291 const auto weight =
static_cast<double>(weights[measIdx]);
293 yWeighted[measIdx] = yValues[measIdx] * weight;
296 aWeighted(measIdx,
order) = a(measIdx,
order) * weight;
310 aInv = aTaInv.
dot(aT);
313 auto x = aInv.
dot(yWeighted);
324 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
325 if (numCoefficients == 0)
331 integralCofficients[0] = 0.0;
333 for (
uint32 i = 0; i < numCoefficients; ++i)
335 integralCofficients[i + 1] =
static_cast<double>(coefficients_[i]) /
static_cast<double>(i + 1);
349 return static_cast<uint32>(coefficients_.size() - 1);
359 std::cout << *
this << std::endl;
370 const auto numCoeffients =
static_cast<uint32>(coefficients_.size());
372 std::string repr =
"Poly1d<";
374 for (
auto& coefficient : coefficients_)
378 if (coefficient == 0)
406 if (
power < numCoeffients)
426 for (
auto& coefficient : coefficients_)
455 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
457 for (
size_t i = 0; i < coefficients_.size(); ++i)
459 coefficients_[i] += inOtherPoly.coefficients_[i];
461 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
463 coefficients_.push_back(inOtherPoly.coefficients_[i]);
468 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
470 coefficients_[i] += inOtherPoly.coefficients_[i];
498 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
500 for (
size_t i = 0; i < coefficients_.size(); ++i)
502 coefficients_[i] -= inOtherPoly.coefficients_[i];
504 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
506 coefficients_.push_back(-inOtherPoly.coefficients_[i]);
511 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
513 coefficients_[i] -= inOtherPoly.coefficients_[i];
542 std::vector<dtype> coeffsA(finalCoefficientsSize, 0);
543 std::vector<dtype> coeffsB(finalCoefficientsSize, 0);
545 stl_algorithms::copy(inOtherPoly.coefficients_.cbegin(), inOtherPoly.coefficients_.cend(), coeffsB.begin());
548 std::vector<dtype> finalCoefficients(finalCoefficientsSize, 0);
549 for (
uint32 i = 0; i < finalCoefficientsSize; ++i)
551 for (
uint32 k = 0; k <= i; ++k)
553 finalCoefficients[i] += coeffsA[k] * coeffsB[i - k];
557 this->coefficients_ = finalCoefficients;
570 return Poly1d(*
this) ^= inPower;
584 coefficients_.clear();
585 coefficients_.push_back(1);
593 auto thisPoly(*
this);
612 inOStream << inPoly.
str() << std::endl;
617 std::vector<dtype> coefficients_{};
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
uint32 numCols() const noexcept
Definition: NdArrayCore.hpp:3418
size_type size() const noexcept
Definition: NdArrayCore.hpp:4296
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
bool issquare() const noexcept
Definition: NdArrayCore.hpp:2918
bool isflat() const noexcept
Definition: NdArrayCore.hpp:2855
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4629
NdArray< dtype > dot(const NdArray< dtype > &inOtherArray) const
Definition: NdArrayCore.hpp:2633
Definition: Poly1d.hpp:58
Poly1d< dtype > & operator*=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:539
Poly1d< dtype > deriv() const
Definition: Poly1d.hpp:160
Poly1d< dtypeOut > astype() const
Definition: Poly1d.hpp:129
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, const NdArray< dtype > &weights, uint8 polyOrder)
Definition: Poly1d.hpp:246
Poly1d(const NdArray< dtype > &inValues, bool isRoots=false)
Definition: Poly1d.hpp:78
Poly1d< dtype > & operator+=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:453
Poly1d< double > integ() const
Definition: Poly1d.hpp:322
Poly1d< dtype > operator^(uint32 inPower) const
Definition: Poly1d.hpp:568
friend std::ostream & operator<<(std::ostream &inOStream, const Poly1d< dtype > &inPoly)
Definition: Poly1d.hpp:610
Poly1d< dtype > operator+(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:441
Poly1d< dtype > & operator-=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:496
Poly1d< dtype > & operator^=(uint32 inPower)
Definition: Poly1d.hpp:580
std::string str() const
Definition: Poly1d.hpp:368
Poly1d< dtype > operator*(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:527
void print() const
Definition: Poly1d.hpp:357
uint32 order() const noexcept
Definition: Poly1d.hpp:347
NdArray< dtype > coefficients() const
Definition: Poly1d.hpp:149
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, uint8 polyOrder)
Definition: Poly1d.hpp:191
dtype operator()(dtype inValue) const noexcept
Definition: Poly1d.hpp:422
double area(double a, double b) const
Definition: Poly1d.hpp:111
Poly1d< dtype > operator-(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:484
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition: inv.hpp:52
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:702
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:95
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
dtype power(dtype inValue, uint8 inPower) noexcept
Definition: Utils/power.hpp:48
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:52
Definition: Coordinate.hpp:45
constexpr dtype power(dtype inValue, uint8 inExponent) noexcept
Definition: Functions/power.hpp:52
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
std::uint8_t uint8
Definition: Types.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40