56 template<
typename dtype>
60 STATIC_ASSERT_ARITHMETIC(dtype);
88 coefficients_.push_back(1);
89 for (
auto value : inValues)
97 coefficients_.resize(inValues.
size());
110 [[nodiscard]]
double area(
double a,
double b)
const
117 auto polyIntegral =
integ();
118 return polyIntegral(b) - polyIntegral(a);
127 template<
typename dtypeOut>
132 const auto function = [](dtype value) -> dtypeOut {
return static_cast<dtypeOut
>(value); };
147 auto coefficientsCopy = coefficients_;
158 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
159 if (numCoefficients == 0)
163 if (numCoefficients == 1)
171 for (
uint32 i = 1; i < numCoefficients; ++i)
173 derivativeCofficients[counter++] = coefficients_[i] * i;
186 dtype
eval(dtype xValue)
const noexcept
213 const auto numMeasurements = xValues.
size();
215 if (yValues.
size() != numMeasurements)
231 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
233 const auto xDouble =
static_cast<double>(xValues[measIdx]);
250 aInv = aTaInv.
dot(aT);
253 auto x = aInv.
dot(yValues.template astype<double>());
271 const auto numMeasurements = xValues.
size();
273 if (yValues.
size() != numMeasurements)
278 if (weights.
size() != numMeasurements)
299 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
301 const auto xDouble =
static_cast<double>(xValues[measIdx]);
311 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
313 const auto weight =
static_cast<double>(weights[measIdx]);
315 yWeighted[measIdx] = yValues[measIdx] * weight;
318 aWeighted(measIdx,
order) = a(measIdx,
order) * weight;
332 aInv = aTaInv.
dot(aT);
335 auto x = aInv.
dot(yWeighted);
346 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
347 if (numCoefficients == 0)
353 integralCofficients[0] = 0.;
355 for (
uint32 i = 0; i < numCoefficients; ++i)
357 integralCofficients[i + 1] =
static_cast<double>(coefficients_[i]) /
static_cast<double>(i + 1);
371 return static_cast<uint32>(coefficients_.size() - 1);
381 std::cout << *
this << std::endl;
390 [[nodiscard]] std::string
str()
const
392 const auto numCoeffients =
static_cast<uint32>(coefficients_.size());
394 std::string repr =
"Poly1d<";
396 for (
auto& coefficient : coefficients_)
417 if (
power < numCoeffients)
436 return std::accumulate(coefficients_.begin(),
439 [&
power, inValue](dtype polyValue,
const auto& coefficient) noexcept -> dtype
440 { return polyValue + coefficient * utils::power(inValue, power++); });
457 [
this](
const auto xValue) { return this->operator()(xValue); });
482 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
484 for (
size_t i = 0; i < coefficients_.size(); ++i)
486 coefficients_[i] += inOtherPoly.coefficients_[i];
488 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
490 coefficients_.push_back(inOtherPoly.coefficients_[i]);
495 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
497 coefficients_[i] += inOtherPoly.coefficients_[i];
525 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
527 for (
size_t i = 0; i < coefficients_.size(); ++i)
529 coefficients_[i] -= inOtherPoly.coefficients_[i];
531 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
533 coefficients_.push_back(-inOtherPoly.coefficients_[i]);
538 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
540 coefficients_[i] -= inOtherPoly.coefficients_[i];
569 std::vector<dtype> coeffsA(finalCoefficientsSize, 0);
570 std::vector<dtype> coeffsB(finalCoefficientsSize, 0);
572 stl_algorithms::copy(inOtherPoly.coefficients_.cbegin(), inOtherPoly.coefficients_.cend(), coeffsB.begin());
575 std::vector<dtype> finalCoefficients(finalCoefficientsSize, 0);
576 for (
uint32 i = 0; i < finalCoefficientsSize; ++i)
578 for (
uint32 k = 0; k <= i; ++k)
580 finalCoefficients[i] += coeffsA[k] * coeffsB[i - k];
584 this->coefficients_ = finalCoefficients;
597 return Poly1d(*
this) ^= inPower;
611 coefficients_.clear();
612 coefficients_.push_back(1);
620 auto thisPoly(*
this);
639 inOStream << inPoly.
str() << std::endl;
644 std::vector<dtype> coefficients_{};
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
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:139
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
self_type transpose() const
Definition: NdArrayCore.hpp:4882
bool issquare() const noexcept
Definition: NdArrayCore.hpp:3009
bool isflat() const noexcept
Definition: NdArrayCore.hpp:2945
self_type dot(const self_type &inOtherArray) const
Definition: NdArrayCore.hpp:2719
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3465
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
Definition: Poly1d.hpp:58
Poly1d< dtype > operator-(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:511
Poly1d< dtype > operator*(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:554
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, const NdArray< dtype > &weights, uint8 polyOrder)
Definition: Poly1d.hpp:266
Poly1d< dtype > & operator^=(uint32 inPower)
Definition: Poly1d.hpp:607
Poly1d< dtype > operator+(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:468
dtype eval(dtype xValue) const noexcept
Definition: Poly1d.hpp:186
Poly1d< double > integ() const
Definition: Poly1d.hpp:344
Poly1d< dtype > & operator*=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:566
Poly1d(const NdArray< dtype > &inValues, IsRoots isRoots=IsRoots::NO)
Definition: Poly1d.hpp:78
Poly1d< dtype > & operator-=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:523
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, uint8 polyOrder)
Definition: Poly1d.hpp:211
Poly1d< dtypeOut > astype() const
Definition: Poly1d.hpp:128
NdArray< dtype > eval(const NdArray< dtype > &xValues) const noexcept
Definition: Poly1d.hpp:198
Poly1d< dtype > & operator+=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:480
NdArray< dtype > operator()(const NdArray< dtype > &xValues) const noexcept
Definition: Poly1d.hpp:450
std::string str() const
Definition: Poly1d.hpp:390
void print() const
Definition: Poly1d.hpp:379
Poly1d< dtype > operator^(uint32 inPower) const
Definition: Poly1d.hpp:595
uint32 order() const noexcept
Definition: Poly1d.hpp:369
dtype operator()(dtype inValue) const noexcept
Definition: Poly1d.hpp:433
double area(double a, double b) const
Definition: Poly1d.hpp:110
friend std::ostream & operator<<(std::ostream &inOStream, const Poly1d< dtype > &inPoly)
Definition: Poly1d.hpp:637
NdArray< dtype > coefficients() const
Definition: Poly1d.hpp:145
Poly1d< dtype > deriv() const
Definition: Poly1d.hpp:156
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition: inv.hpp:54
Definition: chebyshev_t.hpp:39
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:775
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:97
std::string num2str(dtype inNumber)
Definition: num2str.hpp:44
dtype power(dtype inValue, uint8 inPower) noexcept
Definition: Utils/power.hpp:46
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:49
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
IsRoots
Is Roots boolean.
Definition: Enums.hpp:92
std::uint8_t uint8
Definition: Types.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40