INSTINCT Code Coverage Report


Directory: src/
File: Navigation/Math/internal/PolynomialRegressor/LeastSquares.hpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 1 1 100.0%
Branches: 17 32 53.1%

Line Branch Exec Source
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
9 /// @file LeastSquares.hpp
10 /// @brief Least Squares Curve Fit
11 /// @author T. Topp (topp@ins.uni-stuttgart.de)
12 /// @date 2023-11-06
13
14 #pragma once
15
16 #include <cstddef>
17 #include <vector>
18 #include <cmath>
19 #include "util/Assert.h"
20 #include "util/Eigen.hpp"
21 #include "util/Logger.hpp"
22
23 namespace NAV
24 {
25
26 /// @brief Least Squares Curve Fitting
27 /// @tparam Scalar Data type to store
28 template<typename Scalar>
29 class LeastSquares
30 {
31 public:
32 /// @brief Constructor
33 LeastSquares() = delete;
34
35 /// @brief Calculates the polynomial coefficients in order a0 + a1 * x + a2 * x^2 + ...
36 /// @param[in] x X data to fit
37 /// @param[in] y Y data to fit
38 /// @param[in] polynomialDegree Polynomial degree to fit
39 template<typename DerivedX, typename DerivedY>
40 23602 [[nodiscard]] static Eigen::VectorX<Scalar> calcCoefficients(const Eigen::MatrixBase<DerivedX>& x, const Eigen::MatrixBase<DerivedY>& y, size_t polynomialDegree = 2)
41 {
42 23602 auto effectiveDegree = static_cast<Eigen::Index>(std::min(polynomialDegree, static_cast<size_t>(x.rows()) - 1));
43
44 23602 auto n = x.size();
45
1/2
✓ Branch 1 taken 23602 times.
✗ Branch 2 not taken.
23602 Eigen::MatrixX<Scalar> H = Eigen::MatrixX<Scalar>(n, effectiveDegree + 1);
46
2/4
✓ Branch 2 taken 23602 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 23602 times.
✗ Branch 6 not taken.
23602 Eigen::ArrayX<Scalar> xpow = Eigen::ArrayX<Scalar>::Ones(x.rows());
47
2/2
✓ Branch 0 taken 70623 times.
✓ Branch 1 taken 23602 times.
94225 for (int i = 0; i < effectiveDegree + 1; i++)
48 {
49
3/6
✓ Branch 1 taken 70623 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 70623 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 70623 times.
✗ Branch 8 not taken.
70623 H.col(i) = xpow.matrix();
50
2/4
✓ Branch 1 taken 70623 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 70623 times.
✗ Branch 5 not taken.
70623 xpow *= x.array();
51 }
52
53
7/14
✓ Branch 1 taken 23602 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23602 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 23602 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 23602 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 23602 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 23602 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 23602 times.
✗ Branch 20 not taken.
47204 return (H.transpose() * H).ldlt().solve(H.transpose() * y);
54 23602 }
55 };
56
57 } // namespace NAV
58