INSTINCT Code Coverage Report


Directory: src/
File: Navigation/Math/internal/PolynomialRegressor/HouseholderQr.hpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 1 1 100.0%
Branches: 13 24 54.2%

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 HouseholderQr.hpp
10 /// @brief Least Squares Curve Fit
11 /// @author T. Topp (topp@ins.uni-stuttgart.de)
12 /// @date 2023-10-26
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
22 namespace NAV
23 {
24
25 /// @brief Householder QR decomposition Curve Fitting
26 /// @tparam Scalar Data type to store
27 template<typename Scalar>
28 class HouseholderQr
29 {
30 public:
31 /// @brief Constructor
32 HouseholderQr() = delete;
33
34 /// @brief Calculates the polynomial coefficients in order a0 + a1 * x + a2 * x^2 + ...
35 /// @param[in] x X data to fit
36 /// @param[in] y Y data to fit
37 /// @param[in] polynomialDegree Polynomial degree to fit
38 template<typename DerivedX, typename DerivedY>
39 314275 [[nodiscard]] static Eigen::VectorX<Scalar> calcCoefficients(const Eigen::MatrixBase<DerivedX>& x, const Eigen::MatrixBase<DerivedY>& y, size_t polynomialDegree = 2)
40 {
41 314275 auto effectiveDegree = static_cast<Eigen::Index>(std::min(polynomialDegree, static_cast<size_t>(x.rows()) - 1));
42
43 314275 auto n = x.size();
44
1/2
✓ Branch 1 taken 314275 times.
✗ Branch 2 not taken.
314275 Eigen::MatrixX<Scalar> H = Eigen::MatrixX<Scalar>(n, effectiveDegree + 1);
45
2/4
✓ Branch 2 taken 314275 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 314275 times.
✗ Branch 6 not taken.
314275 Eigen::ArrayX<Scalar> xpow = Eigen::ArrayX<Scalar>::Ones(x.rows());
46
2/2
✓ Branch 0 taken 651969 times.
✓ Branch 1 taken 314275 times.
966244 for (int i = 0; i < effectiveDegree + 1; i++)
47 {
48
3/6
✓ Branch 1 taken 651969 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 651969 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 651969 times.
✗ Branch 8 not taken.
651969 H.col(i) = xpow.matrix();
49
2/4
✓ Branch 1 taken 651969 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 651969 times.
✗ Branch 5 not taken.
651969 xpow *= x.array();
50 }
51
52
3/6
✓ Branch 1 taken 314275 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 314275 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 314275 times.
✗ Branch 8 not taken.
628550 return H.householderQr().solve(y);
53 314275 }
54 };
55
56 } // namespace NAV
57