28template<
typename Scalar>
43 _polyDegree = polynomialDegree;
44 _summedPowersX = Eigen::VectorX<Scalar>::Zero((
static_cast<Eigen::Index
>(polynomialDegree) + 1) * 2 - 1);
45 _summedPowersXTimesY = Eigen::VectorX<Scalar>::Zero(
static_cast<Eigen::Index
>(polynomialDegree) + 1);
53 auto xpow =
static_cast<Scalar
>(1.0);
54 for (
auto& sum : _summedPowersX)
60 xpow =
static_cast<Scalar
>(1.0);
61 for (
auto& sum : _summedPowersXTimesY)
73 auto xpow =
static_cast<Scalar
>(1.0);
74 for (
auto& sum : _summedPowersX)
80 xpow =
static_cast<Scalar
>(1.0);
81 for (
auto& sum : _summedPowersXTimesY)
91 _summedPowersX.setZero();
92 _summedPowersXTimesY.setZero();
98 if (_summedPowersX(0) == 0) {
return {}; }
100 auto effectiveDegree =
static_cast<Eigen::Index
>(std::min(_polyDegree,
static_cast<size_t>(_summedPowersX(0)) - 1));
103 Eigen::MatrixXd ATA(effectiveDegree + 1, effectiveDegree + 1);
104 for (Eigen::Index i = 0; i < effectiveDegree + 1; ++i)
106 ATA.row(i) = _summedPowersX.segment(i, effectiveDegree + 1);
109 return ATA.inverse().topRows(effectiveDegree + 1) * _summedPowersXTimesY.head(effectiveDegree + 1);
113 size_t _polyDegree = 2;
114 Eigen::VectorX<Scalar> _summedPowersX;
115 Eigen::VectorX<Scalar> _summedPowersXTimesY;
Incremental Least Squares Curve Fitting.
Definition IncrementalLeastSquares.hpp:30
void reset()
Reset the saved data.
Definition IncrementalLeastSquares.hpp:89
void removeDataPoint(const Scalar &x, const Scalar &y)
Removes a data point from the polynomial fit.
Definition IncrementalLeastSquares.hpp:71
IncrementalLeastSquares(size_t polynomialDegree)
Constructor.
Definition IncrementalLeastSquares.hpp:34
Eigen::VectorX< Scalar > calcCoefficients() const
Calculates the polynomial coefficients in order a0 + a1 * x + a2 * x^2 + ...
Definition IncrementalLeastSquares.hpp:96
void setPolynomialDegree(size_t polynomialDegree)
Set the Polynomial Degree.
Definition IncrementalLeastSquares.hpp:41
void addDataPoint(const Scalar &x, const Scalar &y)
Add a data point to the polynomial.
Definition IncrementalLeastSquares.hpp:51