 |
NumCpp
2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
|
Go to the documentation of this file.
44 #include <type_traits>
56 template<
typename dtype>
60 STATIC_ASSERT_ARITHMETIC(dtype);
87 coefficients_.push_back(1);
88 for (
auto value : inValues)
96 for (
auto value : inValues)
98 coefficients_.push_back(value);
111 double area(
double a,
double b)
const
118 auto polyIntegral =
integ();
119 return polyIntegral(b) - polyIntegral(a);
128 template<
typename dtypeOut>
133 const auto function = [](dtype value) -> dtypeOut
135 return static_cast<dtypeOut
>(value);
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>());
250 const auto numMeasurements = xValues.
size();
252 if (yValues.
size() != numMeasurements)
257 if (weights.
size() != numMeasurements)
278 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
280 const auto xDouble =
static_cast<double>(xValues[measIdx]);
290 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
292 const auto weight =
static_cast<double>(weights[measIdx]);
294 yWeighted[measIdx] = yValues[measIdx] * weight;
297 aWeighted(measIdx,
order) = a(measIdx,
order) * weight;
311 aInv = aTaInv.
dot(aT);
314 auto x = aInv.
dot(yWeighted);
325 const auto numCoefficients =
static_cast<uint32>(coefficients_.size());
326 if (numCoefficients == 0)
332 integralCofficients[0] = 0.0;
334 for (
uint32 i = 0; i < numCoefficients; ++i)
336 integralCofficients[i + 1] =
static_cast<double>(coefficients_[i]) /
static_cast<double>(i + 1);
351 return static_cast<uint32>(coefficients_.size() - 1);
361 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)
431 for (
auto& coefficient : coefficients_)
464 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
466 for (
size_t i = 0; i < coefficients_.size(); ++i)
468 coefficients_[i] += inOtherPoly.coefficients_[i];
470 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
472 coefficients_.push_back(inOtherPoly.coefficients_[i]);
477 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
479 coefficients_[i] += inOtherPoly.coefficients_[i];
511 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
513 for (
size_t i = 0; i < coefficients_.size(); ++i)
515 coefficients_[i] -= inOtherPoly.coefficients_[i];
517 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
519 coefficients_.push_back(-inOtherPoly.coefficients_[i]);
524 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
526 coefficients_[i] -= inOtherPoly.coefficients_[i];
559 std::vector<dtype> coeffsA(finalCoefficientsSize, 0);
560 std::vector<dtype> coeffsB(finalCoefficientsSize, 0);
562 stl_algorithms::copy(inOtherPoly.coefficients_.cbegin(), inOtherPoly.coefficients_.cend(), coeffsB.begin());
565 std::vector<dtype> finalCoefficients(finalCoefficientsSize, 0);
566 for (
uint32 i = 0; i < finalCoefficientsSize; ++i)
568 for (
uint32 k = 0; k <= i; ++k)
570 finalCoefficients[i] += coeffsA[k] * coeffsB[i - k];
574 this->coefficients_ = finalCoefficients;
589 return Poly1d(*
this) ^= inPower;
605 coefficients_.clear();
606 coefficients_.push_back(1);
614 auto thisPoly(*
this);
634 inOStream << inPoly.
str() << std::endl;
639 std::vector<dtype> coefficients_{};
Poly1d< dtype > & operator*=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:556
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition: inv.hpp:54
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4356
double area(double a, double b) const
Definition: Poly1d.hpp:111
NdArray< dtype > dot(const NdArray< dtype > &inOtherArray) const
Definition: NdArrayCore.hpp:2661
Poly1d< double > integ() const
Definition: Poly1d.hpp:323
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:52
std::uint8_t uint8
Definition: Types.hpp:42
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
bool issquare() const noexcept
Definition: NdArrayCore.hpp:2961
Poly1d< dtype > operator^(uint32 inPower) const
Definition: Poly1d.hpp:587
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4652
Poly1d< dtype > operator+(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:448
Definition: Poly1d.hpp:57
Poly1d< dtype > & operator^=(uint32 inPower)
Definition: Poly1d.hpp:601
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:702
std::uint32_t uint32
Definition: Types.hpp:40
uint32 order() const noexcept
Definition: Poly1d.hpp:349
uint32 numCols() const noexcept
Definition: NdArrayCore.hpp:3475
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:427
Poly1d< dtype > & operator-=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:509
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 > deriv() const
Definition: Poly1d.hpp:161
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:95
bool isflat() const noexcept
Definition: NdArrayCore.hpp:2898
size_type size() const noexcept
Definition: NdArrayCore.hpp:4370
friend std::ostream & operator<<(std::ostream &inOStream, const Poly1d< dtype > &inPoly)
Definition: Poly1d.hpp:632
Definition: Coordinate.hpp:44
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
constexpr dtype power(dtype inValue, uint8 inExponent) noexcept
Definition: Functions/power.hpp:53
std::string str() const
Definition: Poly1d.hpp:371
Holds info about the dtype.
Definition: DtypeInfo.hpp:40
NdArray< dtype > coefficients() const
Definition: Poly1d.hpp:150
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Poly1d< dtype > operator*(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:542
dtype power(dtype inValue, uint8 inPower) noexcept
Definition: Utils/power.hpp:48
Poly1d< dtype > operator-(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:495
Poly1d< dtypeOut > astype() const
Definition: Poly1d.hpp:129
Poly1d< dtype > & operator+=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:462
void print() const
Definition: Poly1d.hpp:359