 |
NumCpp
1.0
A C++ implementation of the Python Numpy library
|
Go to the documentation of this file.
45 #include <type_traits>
57 template<
typename dtype>
61 STATIC_ASSERT_ARITHMETIC(dtype);
88 coefficients_.push_back(1);
89 for (
auto value : inValues)
97 for (
auto value : inValues)
99 coefficients_.push_back(value);
112 double area(
double a,
double b)
const
119 auto polyIntegral =
integ();
120 return polyIntegral(b) - polyIntegral(a);
129 template<
typename dtypeOut>
134 const auto function = [](dtype value) -> dtypeOut
136 return static_cast<dtypeOut
>(value);
153 auto coefficientsCopy = coefficients_;
164 const uint32 numCoefficients =
static_cast<uint32>(coefficients_.size());
165 if (numCoefficients == 0)
169 else if (numCoefficients == 1)
177 for (
uint32 i = 1; i < numCoefficients; ++i)
179 derivativeCofficients[counter++] = coefficients_[i] * i;
195 const auto numMeasurements = xValues.
size();
197 if (yValues.
size() != numMeasurements)
213 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
215 const auto xDouble =
static_cast<double>(xValues[measIdx]);
232 aInv = aTaInv.
dot(aT);
235 auto x = aInv.
dot(yValues.template astype<double>());
251 const auto numMeasurements = xValues.
size();
253 if (yValues.
size() != numMeasurements)
258 if (weights.
size() != numMeasurements)
279 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
281 const auto xDouble =
static_cast<double>(xValues[measIdx]);
291 for (
uint32 measIdx = 0; measIdx < numMeasurements; ++measIdx)
293 const auto weight =
static_cast<double>(weights[measIdx]);
295 yWeighted[measIdx] = yValues[measIdx] * weight;
298 aWeighted(measIdx,
order) = a(measIdx,
order) * weight;
312 aInv = aTaInv.
dot(aT);
315 auto x = aInv.
dot(yWeighted);
326 const uint32 numCoefficients =
static_cast<uint32>(coefficients_.size());
327 if (numCoefficients == 0)
333 integralCofficients[0] = 0.0;
335 for (
uint32 i = 0; i < numCoefficients; ++i)
337 integralCofficients[i + 1] =
static_cast<double>(coefficients_[i]) /
static_cast<double>(i + 1);
352 return static_cast<uint32>(coefficients_.size() - 1);
362 std::cout << *
this << std::endl;
374 const uint32 numCoeffients =
static_cast<uint32>(coefficients_.size());
376 std::string repr =
"Poly1d<";
378 for (
auto& coefficient : coefficients_)
382 if (coefficient == 0)
410 if (
power < numCoeffients)
432 for (
auto& coefficient : coefficients_)
465 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
467 for (
size_t i = 0; i < coefficients_.size(); ++i)
469 coefficients_[i] += inOtherPoly.coefficients_[i];
471 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
473 coefficients_.push_back(inOtherPoly.coefficients_[i]);
478 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
480 coefficients_[i] += inOtherPoly.coefficients_[i];
512 if (this->coefficients_.size() < inOtherPoly.coefficients_.size())
514 for (
size_t i = 0; i < coefficients_.size(); ++i)
516 coefficients_[i] -= inOtherPoly.coefficients_[i];
518 for (
size_t i = coefficients_.size(); i < inOtherPoly.coefficients_.size(); ++i)
520 coefficients_.push_back(-inOtherPoly.coefficients_[i]);
525 for (
size_t i = 0; i < inOtherPoly.coefficients_.size(); ++i)
527 coefficients_[i] -= inOtherPoly.coefficients_[i];
560 std::vector<dtype> coeffsA(finalCoefficientsSize, 0);
561 std::vector<dtype> coeffsB(finalCoefficientsSize, 0);
563 stl_algorithms::copy(inOtherPoly.coefficients_.cbegin(), inOtherPoly.coefficients_.cend(), coeffsB.begin());
566 std::vector<dtype> finalCoefficients(finalCoefficientsSize, 0);
567 for (
uint32 i = 0; i < finalCoefficientsSize; ++i)
569 for (
uint32 k = 0; k <= i; ++k)
571 finalCoefficients[i] += coeffsA[k] * coeffsB[i - k];
575 this->coefficients_ = finalCoefficients;
590 return Poly1d(*
this) ^= inPower;
606 coefficients_.clear();
607 coefficients_.push_back(1);
610 else if (inPower == 1)
615 auto thisPoly(*
this);
635 inOStream << inPoly.
str() << std::endl;
640 std::vector<dtype> coefficients_{};
Poly1d< dtype > & operator*=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:557
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition: inv.hpp:55
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
double area(double a, double b) const
Definition: Poly1d.hpp:112
NdArray< dtype > dot(const NdArray< dtype > &inOtherArray) const
Definition: NdArrayCore.hpp:2635
Poly1d< double > integ() const
Definition: Poly1d.hpp:324
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:53
std::uint8_t uint8
Definition: Types.hpp:43
std::string num2str(dtype inNumber)
Definition: num2str.hpp:47
bool issquare() const noexcept
Definition: NdArrayCore.hpp:2936
Poly1d< dtype > operator^(uint32 inPower) const
Definition: Poly1d.hpp:588
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4591
Poly1d< dtype > operator+(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:449
Definition: Poly1d.hpp:58
Poly1d< dtype > & operator^=(uint32 inPower)
Definition: Poly1d.hpp:602
std::uint32_t uint32
Definition: Types.hpp:41
uint32 order() const noexcept
Definition: Poly1d.hpp:350
uint32 numCols() const noexcept
Definition: NdArrayCore.hpp:3415
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, uint8 polyOrder)
Definition: Poly1d.hpp:193
dtype operator()(dtype inValue) const noexcept
Definition: Poly1d.hpp:428
Poly1d< dtype > & operator-=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:510
static Poly1d< double > fit(const NdArray< dtype > &xValues, const NdArray< dtype > &yValues, const NdArray< dtype > &weights, uint8 polyOrder)
Definition: Poly1d.hpp:248
Poly1d(const NdArray< dtype > &inValues, bool isRoots=false)
Definition: Poly1d.hpp:79
Poly1d< dtype > deriv() const
Definition: Poly1d.hpp:162
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:96
bool isflat() const noexcept
Definition: NdArrayCore.hpp:2874
size_type size() const noexcept
Definition: NdArrayCore.hpp:4310
friend std::ostream & operator<<(std::ostream &inOStream, const Poly1d< dtype > &inPoly)
Definition: Poly1d.hpp:633
Definition: Coordinate.hpp:45
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:43
constexpr dtype power(dtype inValue, uint8 inExponent) noexcept
Definition: Functions/power.hpp:54
std::string str() const
Definition: Poly1d.hpp:372
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
NdArray< dtype > coefficients() const
Definition: Poly1d.hpp:151
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
Poly1d< dtype > operator*(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:543
dtype power(dtype inValue, uint8 inPower) noexcept
Definition: Utils/power.hpp:49
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction) noexcept
Definition: StlAlgorithms.hpp:703
Poly1d< dtype > operator-(const Poly1d< dtype > &inOtherPoly) const
Definition: Poly1d.hpp:496
Poly1d< dtypeOut > astype() const
Definition: Poly1d.hpp:130
Poly1d< dtype > & operator+=(const Poly1d< dtype > &inOtherPoly)
Definition: Poly1d.hpp:463
void print() const
Definition: Poly1d.hpp:360