33 #include <type_traits>
55 template<
typename dtype>
59 STATIC_ASSERT_ARITHMETIC(dtype);
87 coefficients_.push_back(1);
88 for (
auto value : inValues)
96 coefficients_.resize(inValues.
size());
109 [[nodiscard]]
double area(
double a,
double b)
const
116 auto polyIntegral =
integ();
117 return polyIntegral(b) - polyIntegral(a);
126 template<
typename dtypeOut>
131 const auto function = [](dtype value) -> dtypeOut {
return static_cast<dtypeOut
>(value); };
146 auto coefficientsCopy = coefficients_;
157 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
158 if (numCoefficients == 0)
162 if (numCoefficients == 1)
170 for (
uint32 i = 1; i < numCoefficients; ++i)
172 derivativeCofficients[counter++] = coefficients_[i] * i;
188 const auto numMeasurements = xValues.
size();
190 if (yValues.
size() != numMeasurements)
206 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
208 const auto xDouble =
static_cast<double>(xValues[measIdx]);
225 aInv = aTaInv.
dot(aT);
228 auto x = aInv.
dot(yValues.template astype<double>());
246 const auto numMeasurements = xValues.
size();
248 if (yValues.
size() != numMeasurements)
253 if (weights.
size() != numMeasurements)
274 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
276 const auto xDouble =
static_cast<double>(xValues[measIdx]);
286 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
288 const auto weight =
static_cast<double>(weights[measIdx]);
290 yWeighted[measIdx] = yValues[measIdx] * weight;
293 aWeighted(measIdx,
order) = a(measIdx,
order) * weight;
307 aInv = aTaInv.
dot(aT);
310 auto x = aInv.
dot(yWeighted);
321 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
322 if (numCoefficients == 0)
328 integralCofficients[0] = 0.;
330 for (
uint32 i = 0; i < numCoefficients; ++i)
332 integralCofficients[i + 1] =
static_cast<double>(coefficients_[i]) /
static_cast<double>(i + 1);
346 return static_cast<uint32>(coefficients_.size() - 1);
356 std::cout << *
this << std::endl;
365 [[nodiscard]] std::string
str()
const
367 const auto numCoeffients =
static_cast<uint32>(coefficients_.size());
369 std::string repr =
"Poly1d<";
371 for (
auto& coefficient : coefficients_)
392 if (
power < numCoeffients)
411 return std::accumulate(coefficients_.begin(),
414 [&
power, inValue](dtype polyValue,
const auto& coefficient) noexcept -> dtype
415 { return polyValue + coefficient * utils::power(inValue, power++); });
439 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
441 for (
size_t i = 0; i < coefficients_.size(); ++i)
443 coefficients_[i] += inOtherPoly.coefficients_[i];
445 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
447 coefficients_.push_back(inOtherPoly.coefficients_[i]);
452 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
454 coefficients_[i] += inOtherPoly.coefficients_[i];
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];
526 std::vector<dtype> coeffsA(finalCoefficientsSize, 0);
527 std::vector<dtype> coeffsB(finalCoefficientsSize, 0);
529 stl_algorithms::copy(inOtherPoly.coefficients_.cbegin(), inOtherPoly.coefficients_.cend(), coeffsB.begin());
532 std::vector<dtype> finalCoefficients(finalCoefficientsSize, 0);
533 for (
uint32 i = 0; i < finalCoefficientsSize; ++i)
535 for (
uint32 k = 0; k <= i; ++k)
537 finalCoefficients[i] += coeffsA[k] * coeffsB[i - k];
541 this->coefficients_ = finalCoefficients;
554 return Poly1d(*
this) ^= inPower;
568 coefficients_.clear();
569 coefficients_.push_back(1);
577 auto thisPoly(*
this);
596 inOStream << inPoly.
str() << std::endl;
601 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:138
size_type size() const noexcept
Definition: NdArrayCore.hpp:4415
iterator end() noexcept
Definition: NdArrayCore.hpp:1566
self_type transpose() const
Definition: NdArrayCore.hpp:4775
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4402
bool issquare() const noexcept
Definition: NdArrayCore.hpp:2932
bool isflat() const noexcept
Definition: NdArrayCore.hpp:2868
self_type dot(const self_type &inOtherArray) const
Definition: NdArrayCore.hpp:2642
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3388
iterator begin() noexcept
Definition: NdArrayCore.hpp:1258
Definition: Poly1d.hpp:57
Poly1d< dtype > & operator*=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:523
Poly1d< dtype > deriv() const
Definition: Poly1d.hpp:155
Poly1d< dtypeOut > astype() const
Definition: Poly1d.hpp:127
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, const NdArray< dtype > &weights, uint8 polyOrder)
Definition: Poly1d.hpp:241
Poly1d(const NdArray< dtype > &inValues, bool isRoots=false)
Definition: Poly1d.hpp:77
Poly1d< dtype > & operator+=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:437
Poly1d< double > integ() const
Definition: Poly1d.hpp:319
Poly1d< dtype > operator^(uint32 inPower) const
Definition: Poly1d.hpp:552
friend std::ostream & operator<<(std::ostream &inOStream, const Poly1d< dtype > &inPoly)
Definition: Poly1d.hpp:594
Poly1d< dtype > operator+(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:425
Poly1d< dtype > & operator-=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:480
Poly1d< dtype > & operator^=(uint32 inPower)
Definition: Poly1d.hpp:564
std::string str() const
Definition: Poly1d.hpp:365
Poly1d< dtype > operator*(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:511
void print() const
Definition: Poly1d.hpp:354
uint32 order() const noexcept
Definition: Poly1d.hpp:344
NdArray< dtype > coefficients() const
Definition: Poly1d.hpp:144
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, uint8 polyOrder)
Definition: Poly1d.hpp:186
dtype operator()(dtype inValue) const noexcept
Definition: Poly1d.hpp:408
double area(double a, double b) const
Definition: Poly1d.hpp:109
Poly1d< dtype > operator-(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:468
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:48
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