32 #include <type_traits>
56 template<
typename dtype>
60 STATIC_ASSERT_ARITHMETIC(dtype);
88 coefficients_.push_back(1);
89 for (
auto value : inValues)
97 for (
auto value : inValues)
99 coefficients_.push_back(value);
112 double area(
double a,
double b)
const
119 auto polyIntegral =
integ();
120 return polyIntegral(b) - polyIntegral(a);
129 template<
typename dtypeOut>
134 const auto function = [](dtype value) -> dtypeOut {
return static_cast<dtypeOut
>(value); };
138 newCoefficients.begin(),
152 auto coefficientsCopy = coefficients_;
163 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
164 if (numCoefficients == 0)
168 if (numCoefficients == 1)
176 for (
uint32 i = 1; i < numCoefficients; ++i)
178 derivativeCofficients[counter++] = coefficients_[i] * i;
194 const auto numMeasurements = xValues.
size();
196 if (yValues.
size() != numMeasurements)
212 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
214 const auto xDouble =
static_cast<double>(xValues[measIdx]);
231 aInv = aTaInv.
dot(aT);
234 auto x = aInv.
dot(yValues.template astype<double>());
252 const auto numMeasurements = xValues.
size();
254 if (yValues.
size() != numMeasurements)
259 if (weights.
size() != numMeasurements)
280 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
282 const auto xDouble =
static_cast<double>(xValues[measIdx]);
292 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
294 const auto weight =
static_cast<double>(weights[measIdx]);
296 yWeighted[measIdx] = yValues[measIdx] * weight;
299 aWeighted(measIdx,
order) = a(measIdx,
order) * weight;
313 aInv = aTaInv.
dot(aT);
316 auto x = aInv.
dot(yWeighted);
327 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
328 if (numCoefficients == 0)
334 integralCofficients[0] = 0.;
336 for (
uint32 i = 0; i < numCoefficients; ++i)
338 integralCofficients[i + 1] =
static_cast<double>(coefficients_[i]) /
static_cast<double>(i + 1);
352 return static_cast<uint32>(coefficients_.size() - 1);
362 std::cout << *
this << std::endl;
373 const auto numCoeffients =
static_cast<uint32>(coefficients_.size());
375 std::string repr =
"Poly1d<";
377 for (
auto& coefficient : coefficients_)
381 if (coefficient == 0)
409 if (
power < numCoeffients)
429 for (
auto& coefficient : coefficients_)
458 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
460 for (
size_t i = 0; i < coefficients_.size(); ++i)
462 coefficients_[i] += inOtherPoly.coefficients_[i];
464 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
466 coefficients_.push_back(inOtherPoly.coefficients_[i]);
471 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
473 coefficients_[i] += inOtherPoly.coefficients_[i];
501 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
503 for (
size_t i = 0; i < coefficients_.size(); ++i)
505 coefficients_[i] -= inOtherPoly.coefficients_[i];
507 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
509 coefficients_.push_back(-inOtherPoly.coefficients_[i]);
514 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
516 coefficients_[i] -= inOtherPoly.coefficients_[i];
545 std::vector<dtype> coeffsA(finalCoefficientsSize, 0);
546 std::vector<dtype> coeffsB(finalCoefficientsSize, 0);
549 inOtherPoly.coefficients_.cend(),
553 std::vector<dtype> finalCoefficients(finalCoefficientsSize, 0);
554 for (
uint32 i = 0; i < finalCoefficientsSize; ++i)
556 for (
uint32 k = 0; k <= i; ++k)
558 finalCoefficients[i] += coeffsA[k] * coeffsB[i - k];
562 this->coefficients_ = finalCoefficients;
575 return Poly1d(*
this) ^= inPower;
589 coefficients_.clear();
590 coefficients_.push_back(1);
598 auto thisPoly(*
this);
617 inOStream << inPoly.
str() << std::endl;
622 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:3252
size_type size() const noexcept
Definition: NdArrayCore.hpp:4105
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4092
bool issquare() const noexcept
Definition: NdArrayCore.hpp:2796
bool isflat() const noexcept
Definition: NdArrayCore.hpp:2744
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4457
NdArray< dtype > dot(const NdArray< dtype > &inOtherArray) const
Definition: NdArrayCore.hpp:2520
Definition: Poly1d.hpp:58
Poly1d< dtype > & operator*=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:542
Poly1d< dtype > deriv() const
Definition: Poly1d.hpp:161
Poly1d< dtypeOut > astype() const
Definition: Poly1d.hpp:130
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, const NdArray< dtype > &weights, uint8 polyOrder)
Definition: Poly1d.hpp:247
Poly1d(const NdArray< dtype > &inValues, bool isRoots=false)
Definition: Poly1d.hpp:78
Poly1d< dtype > & operator+=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:456
Poly1d< double > integ() const
Definition: Poly1d.hpp:325
Poly1d< dtype > operator^(uint32 inPower) const
Definition: Poly1d.hpp:573
friend std::ostream & operator<<(std::ostream &inOStream, const Poly1d< dtype > &inPoly)
Definition: Poly1d.hpp:615
Poly1d< dtype > operator+(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:444
Poly1d< dtype > & operator-=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:499
Poly1d< dtype > & operator^=(uint32 inPower)
Definition: Poly1d.hpp:585
std::string str() const
Definition: Poly1d.hpp:371
Poly1d< dtype > operator*(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:530
void print() const
Definition: Poly1d.hpp:360
uint32 order() const noexcept
Definition: Poly1d.hpp:350
NdArray< dtype > coefficients() const
Definition: Poly1d.hpp:150
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, uint8 polyOrder)
Definition: Poly1d.hpp:192
dtype operator()(dtype inValue) const noexcept
Definition: Poly1d.hpp:425
double area(double a, double b) const
Definition: Poly1d.hpp:112
Poly1d< dtype > operator-(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:487
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition: inv.hpp:56
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:784
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:99
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:51
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