0.2.0
Loading...
Searching...
No Matches
IncrementalLeastSquares.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
14
15#pragma once
16
17#include <cstddef>
18#include <vector>
19#include <cmath>
20#include "util/Assert.h"
21#include "util/Eigen.hpp"
22
23namespace NAV
24{
25
28template<typename Scalar>
30{
31 public:
34 explicit IncrementalLeastSquares(size_t polynomialDegree)
35 {
36 setPolynomialDegree(polynomialDegree);
37 }
38
41 void setPolynomialDegree(size_t polynomialDegree)
42 {
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);
46 }
47
51 void addDataPoint(const Scalar& x, const Scalar& y)
52 {
53 auto xpow = static_cast<Scalar>(1.0);
54 for (auto& sum : _summedPowersX)
55 {
56 sum += xpow;
57 xpow *= x;
58 }
59
60 xpow = static_cast<Scalar>(1.0);
61 for (auto& sum : _summedPowersXTimesY)
62 {
63 sum += xpow * y;
64 xpow *= x;
65 }
66 }
67
71 void removeDataPoint(const Scalar& x, const Scalar& y)
72 {
73 auto xpow = static_cast<Scalar>(1.0);
74 for (auto& sum : _summedPowersX)
75 {
76 sum -= xpow;
77 xpow *= x;
78 }
79
80 xpow = static_cast<Scalar>(1.0);
81 for (auto& sum : _summedPowersXTimesY)
82 {
83 sum -= xpow * y;
84 xpow *= x;
85 }
86 }
87
89 void reset()
90 {
91 _summedPowersX.setZero();
92 _summedPowersXTimesY.setZero();
93 }
94
96 [[nodiscard]] Eigen::VectorX<Scalar> calcCoefficients() const
97 {
98 if (_summedPowersX(0) == 0) { return {}; }
99
100 auto effectiveDegree = static_cast<Eigen::Index>(std::min(_polyDegree, static_cast<size_t>(_summedPowersX(0)) - 1));
101
102 // A^T * A matrix
103 Eigen::MatrixXd ATA(effectiveDegree + 1, effectiveDegree + 1);
104 for (Eigen::Index i = 0; i < effectiveDegree + 1; ++i)
105 {
106 ATA.row(i) = _summedPowersX.segment(i, effectiveDegree + 1);
107 }
108
109 return ATA.inverse().topRows(effectiveDegree + 1) * _summedPowersXTimesY.head(effectiveDegree + 1);
110 }
111
112 private:
113 size_t _polyDegree = 2;
114 Eigen::VectorX<Scalar> _summedPowersX;
115 Eigen::VectorX<Scalar> _summedPowersXTimesY;
116};
117
118} // namespace NAV
Assertion helpers.
Vector space operations.
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