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;
185 dtype
eval(dtype xValue)
const noexcept
212 const auto numMeasurements = xValues.
size();
214 if (yValues.
size() != numMeasurements)
230 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
232 const auto xDouble =
static_cast<double>(xValues[measIdx]);
249 aInv = aTaInv.
dot(aT);
252 auto x = aInv.
dot(yValues.template astype<double>());
270 const auto numMeasurements = xValues.
size();
272 if (yValues.
size() != numMeasurements)
277 if (weights.
size() != numMeasurements)
298 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
300 const auto xDouble =
static_cast<double>(xValues[measIdx]);
310 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
312 const auto weight =
static_cast<double>(weights[measIdx]);
314 yWeighted[measIdx] = yValues[measIdx] * weight;
317 aWeighted(measIdx,
order) = a(measIdx,
order) * weight;
331 aInv = aTaInv.
dot(aT);
334 auto x = aInv.
dot(yWeighted);
345 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
346 if (numCoefficients == 0)
352 integralCofficients[0] = 0.;
354 for (
uint32 i = 0; i < numCoefficients; ++i)
356 integralCofficients[i + 1] =
static_cast<double>(coefficients_[i]) /
static_cast<double>(i + 1);
370 return static_cast<uint32>(coefficients_.size() - 1);
380 std::cout << *
this << std::endl;
389 [[nodiscard]] std::string
str()
const
391 const auto numCoeffients =
static_cast<uint32>(coefficients_.size());
393 std::string repr =
"Poly1d<";
395 for (
auto& coefficient : coefficients_)
416 if (
power < numCoeffients)
435 return std::accumulate(coefficients_.begin(),
438 [&
power, inValue](dtype polyValue,
const auto& coefficient) noexcept -> dtype
439 { return polyValue + coefficient * utils::power(inValue, power++); });
456 [
this](
const auto xValue) { return this->operator()(xValue); });
481 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
483 for (
size_t i = 0; i < coefficients_.size(); ++i)
485 coefficients_[i] += inOtherPoly.coefficients_[i];
487 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
489 coefficients_.push_back(inOtherPoly.coefficients_[i]);
494 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
496 coefficients_[i] += inOtherPoly.coefficients_[i];
524 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
526 for (
size_t i = 0; i < coefficients_.size(); ++i)
528 coefficients_[i] -= inOtherPoly.coefficients_[i];
530 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
532 coefficients_.push_back(-inOtherPoly.coefficients_[i]);
537 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
539 coefficients_[i] -= inOtherPoly.coefficients_[i];
568 std::vector<dtype> coeffsA(finalCoefficientsSize, 0);
569 std::vector<dtype> coeffsB(finalCoefficientsSize, 0);
571 stl_algorithms::copy(inOtherPoly.coefficients_.cbegin(), inOtherPoly.coefficients_.cend(), coeffsB.begin());
574 std::vector<dtype> finalCoefficients(finalCoefficientsSize, 0);
575 for (
uint32 i = 0; i < finalCoefficientsSize; ++i)
577 for (
uint32 k = 0; k <= i; ++k)
579 finalCoefficients[i] += coeffsA[k] * coeffsB[i - k];
583 this->coefficients_ = finalCoefficients;
596 return Poly1d(*
this) ^= inPower;
610 coefficients_.clear();
611 coefficients_.push_back(1);
619 auto thisPoly(*
this);
638 inOStream << inPoly.
str() << std::endl;
643 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:4477
iterator end() noexcept
Definition: NdArrayCore.hpp:1576
self_type transpose() const
Definition: NdArrayCore.hpp:4837
bool issquare() const noexcept
Definition: NdArrayCore.hpp:2962
bool isflat() const noexcept
Definition: NdArrayCore.hpp:2898
self_type dot(const self_type &inOtherArray) const
Definition: NdArrayCore.hpp:2672
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3418
iterator begin() noexcept
Definition: NdArrayCore.hpp:1268
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4464
Definition: Poly1d.hpp:57
Poly1d< dtype > & operator*=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:565
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:265
dtype eval(dtype xValue) const noexcept
Definition: Poly1d.hpp:185
Poly1d(const NdArray< dtype > &inValues, bool isRoots=false)
Definition: Poly1d.hpp:77
Poly1d< dtype > & operator+=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:479
Poly1d< double > integ() const
Definition: Poly1d.hpp:343
Poly1d< dtype > operator^(uint32 inPower) const
Definition: Poly1d.hpp:594
friend std::ostream & operator<<(std::ostream &inOStream, const Poly1d< dtype > &inPoly)
Definition: Poly1d.hpp:636
Poly1d< dtype > operator+(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:467
NdArray< dtype > operator()(const NdArray< dtype > &xValues) const noexcept
Definition: Poly1d.hpp:449
Poly1d< dtype > & operator-=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:522
Poly1d< dtype > & operator^=(uint32 inPower)
Definition: Poly1d.hpp:606
std::string str() const
Definition: Poly1d.hpp:389
Poly1d< dtype > operator*(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:553
void print() const
Definition: Poly1d.hpp:378
uint32 order() const noexcept
Definition: Poly1d.hpp:368
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:210
NdArray< dtype > eval(const NdArray< dtype > &xValues) const noexcept
Definition: Poly1d.hpp:197
dtype operator()(dtype inValue) const noexcept
Definition: Poly1d.hpp:432
double area(double a, double b) const
Definition: Poly1d.hpp:109
Poly1d< dtype > operator-(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:510
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