54 template<
typename dtype>
86 coefficients_.push_back(1);
87 for (
auto value : inValues)
95 coefficients_.resize(inValues.
size());
108 [[nodiscard]]
double area(
double a,
double b)
const
115 auto polyIntegral =
integ();
116 return polyIntegral(b) - polyIntegral(a);
125 template<
typename dtypeOut>
130 const auto function = [](dtype value) -> dtypeOut {
return static_cast<dtypeOut
>(value); };
145 auto coefficientsCopy = coefficients_;
156 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
157 if (numCoefficients == 0)
161 if (numCoefficients == 1)
169 for (
uint32 i = 1; i < numCoefficients; ++i)
171 derivativeCofficients[counter++] = coefficients_[i] * i;
184 dtype
eval(dtype xValue)
const noexcept
211 const auto numMeasurements = xValues.
size();
213 if (yValues.
size() != numMeasurements)
229 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
231 const auto xDouble =
static_cast<double>(xValues[measIdx]);
248 aInv = aTaInv.
dot(aT);
269 const auto numMeasurements = xValues.
size();
271 if (yValues.
size() != numMeasurements)
276 if (weights.
size() != numMeasurements)
297 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
299 const auto xDouble =
static_cast<double>(xValues[measIdx]);
309 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
311 const auto weight =
static_cast<double>(weights[measIdx]);
313 yWeighted[measIdx] = yValues[measIdx] * weight;
316 aWeighted(measIdx,
order) = a(measIdx,
order) * weight;
330 aInv = aTaInv.
dot(aT);
333 auto x = aInv.
dot(yWeighted);
344 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
345 if (numCoefficients == 0)
351 integralCofficients[0] = 0.;
353 for (
uint32 i = 0; i < numCoefficients; ++i)
355 integralCofficients[i + 1] =
static_cast<double>(coefficients_[i]) /
static_cast<double>(i + 1);
369 return static_cast<uint32>(coefficients_.size() - 1);
379 std::cout << *
this << std::endl;
388 [[nodiscard]] std::string
str()
const
390 const auto numCoeffients =
static_cast<uint32>(coefficients_.size());
392 std::string repr =
"Poly1d<";
394 for (
auto& coefficient : coefficients_)
415 if (
power < numCoeffients)
434 return std::accumulate(coefficients_.begin(),
437 [&
power, inValue](dtype polyValue,
const auto& coefficient) noexcept -> dtype
438 { return polyValue + coefficient * utils::power(inValue, power++); });
455 [
this](
const auto xValue) { return this->operator()(xValue); });
480 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
482 for (
size_t i = 0; i < coefficients_.size(); ++i)
484 coefficients_[i] += inOtherPoly.coefficients_[i];
486 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
488 coefficients_.push_back(inOtherPoly.coefficients_[i]);
493 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
495 coefficients_[i] += inOtherPoly.coefficients_[i];
523 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
525 for (
size_t i = 0; i < coefficients_.size(); ++i)
527 coefficients_[i] -= inOtherPoly.coefficients_[i];
529 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
531 coefficients_.push_back(-inOtherPoly.coefficients_[i]);
536 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
538 coefficients_[i] -= inOtherPoly.coefficients_[i];
567 std::vector<dtype> coeffsA(finalCoefficientsSize, 0);
568 std::vector<dtype> coeffsB(finalCoefficientsSize, 0);
570 stl_algorithms::copy(inOtherPoly.coefficients_.cbegin(), inOtherPoly.coefficients_.cend(), coeffsB.begin());
573 std::vector<dtype> finalCoefficients(finalCoefficientsSize, 0);
574 for (
uint32 i = 0; i < finalCoefficientsSize; ++i)
576 for (
uint32 k = 0; k <= i; ++k)
578 finalCoefficients[i] += coeffsA[k] * coeffsB[i - k];
582 this->coefficients_ = finalCoefficients;
595 return Poly1d(*
this) ^= inPower;
609 coefficients_.clear();
610 coefficients_.push_back(1);
618 auto thisPoly(*
this);
637 inOStream << inPoly.
str() << std::endl;
642 std::vector<dtype> coefficients_{};
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
static constexpr dtype max() noexcept
Definition DtypeInfo.hpp:110
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
iterator end() noexcept
Definition NdArrayCore.hpp:1619
self_type transpose() const
Definition NdArrayCore.hpp:4959
bool issquare() const noexcept
Definition NdArrayCore.hpp:3085
bool isflat() const noexcept
Definition NdArrayCore.hpp:3021
self_type dot(const self_type &inOtherArray) const
Definition NdArrayCore.hpp:2791
size_type numCols() const noexcept
Definition NdArrayCore.hpp:3541
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
Poly1d< dtype > operator-(const Poly1d< dtype > &inOtherPoly) const
Definition Poly1d.hpp:509
Poly1d< dtype > operator*(const Poly1d< dtype > &inOtherPoly) const
Definition Poly1d.hpp:552
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, const NdArray< dtype > &weights, uint8 polyOrder)
Definition Poly1d.hpp:264
Poly1d< dtype > & operator^=(uint32 inPower)
Definition Poly1d.hpp:605
Poly1d< dtype > operator+(const Poly1d< dtype > &inOtherPoly) const
Definition Poly1d.hpp:466
dtype eval(dtype xValue) const noexcept
Definition Poly1d.hpp:184
Poly1d< double > integ() const
Definition Poly1d.hpp:342
Poly1d< dtype > & operator*=(const Poly1d< dtype > &inOtherPoly)
Definition Poly1d.hpp:564
Poly1d(const NdArray< dtype > &inValues, IsRoots isRoots=IsRoots::NO)
Definition Poly1d.hpp:76
Poly1d< dtype > & operator-=(const Poly1d< dtype > &inOtherPoly)
Definition Poly1d.hpp:521
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, uint8 polyOrder)
Definition Poly1d.hpp:209
Poly1d< dtypeOut > astype() const
Definition Poly1d.hpp:126
NdArray< dtype > eval(const NdArray< dtype > &xValues) const noexcept
Definition Poly1d.hpp:196
Poly1d< dtype > & operator+=(const Poly1d< dtype > &inOtherPoly)
Definition Poly1d.hpp:478
NdArray< dtype > operator()(const NdArray< dtype > &xValues) const noexcept
Definition Poly1d.hpp:448
std::string str() const
Definition Poly1d.hpp:388
void print() const
Definition Poly1d.hpp:377
Poly1d< dtype > operator^(uint32 inPower) const
Definition Poly1d.hpp:593
uint32 order() const noexcept
Definition Poly1d.hpp:367
dtype operator()(dtype inValue) const noexcept
Definition Poly1d.hpp:431
double area(double a, double b) const
Definition Poly1d.hpp:108
friend std::ostream & operator<<(std::ostream &inOStream, const Poly1d< dtype > &inPoly)
Definition Poly1d.hpp:635
NdArray< dtype > coefficients() const
Definition Poly1d.hpp:143
Poly1d< dtype > deriv() const
Definition Poly1d.hpp:154
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition inv.hpp:50
Definition chebyshev_t.hpp:39
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition StlAlgorithms.hpp:776
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition StlAlgorithms.hpp:98
std::string num2str(dtype inNumber)
Definition num2str.hpp:44
dtype power(dtype inValue, uint8 inPower) noexcept
Definition Utils/power.hpp:44
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:47
constexpr dtype power(dtype inValue, uint8 inExponent) noexcept
Definition Functions/power.hpp:49
std::uint8_t uint8
Definition Types.hpp:42
IsRoots
Is Roots boolean.
Definition Enums.hpp:92
@ YES
Definition Enums.hpp:93
@ NO
Definition Enums.hpp:94
std::uint32_t uint32
Definition Types.hpp:40