0.3.0
Loading...
Searching...
No Matches
AssociatedLegendre.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
13
14#pragma once
15
16#include <cmath>
17#include <array>
18#include <numbers>
20
21#include "util/Eigen.hpp"
22#include "util/Logger.hpp"
23
24namespace NAV::internal
25{
32template<typename T>
33[[nodiscard]] std::pair<Eigen::MatrixX<T>, Eigen::MatrixX<T>> associatedLegendre(const T& theta, size_t ndegree = 10)
34{
35 // Index needed for the calculation of all necessary 'Pd' entries up to 'ndegree' (--> Pd(n = ndegree, m = ndegree) is dependent on P(n = ndegree, m = ndegree + 1))
36 int N = static_cast<int>(ndegree + 2);
37
38 Eigen::MatrixX<T> P = Eigen::MatrixX<T>::Zero(N, N);
39 Eigen::MatrixX<T> Pd = Eigen::MatrixX<T>::Zero(N, N);
40
41#if defined(__GNUC__) || defined(__clang__)
42 #pragma GCC diagnostic push
43 #pragma GCC diagnostic ignored "-Wnull-dereference"
44#endif
45 // Recurrence relations for the normalized associated Legendre functions (see 'GUT User Guide' eq. 4.2.2 and eq. 4.2.3)
46 P(0, 0) = T(1.0);
47 P(1, 0) = std::numbers::sqrt3 * std::cos(theta);
48 P(1, 1) = std::numbers::sqrt3 * std::sin(theta);
49#if defined(__GNUC__) || defined(__clang__)
50 #pragma GCC diagnostic pop
51#endif
52
53 for (int n = 2; n <= N - 1; n++)
54 {
55 auto nd = static_cast<double>(n);
56 P(n, n) = std::sqrt((2.0 * nd + 1.0) / (2.0 * nd)) * std::sin(theta) * P(n - 1, n - 1);
57
58 for (int m = 0; m <= n; m++)
59 {
60 auto md = static_cast<double>(m);
61
62 if (n == m + 1)
63 {
64 P(n, m) = std::sqrt(((2.0 * nd + 1.0) * (2.0 * nd - 1.0)) / ((nd + md) * (nd - md))) * std::cos(theta) * P(n - 1, m);
65 }
66 else if (n > m + 1)
67 {
68 P(n, m) = std::sqrt(((2.0 * nd + 1.0) * (2.0 * nd - 1.0)) / ((nd + md) * (nd - md))) * std::cos(theta) * P(n - 1, m)
69 - std::sqrt(((2.0 * nd + 1.0) * (nd + md - 1.0) * (nd - md - 1.0)) / ((2.0 * nd - 3.0) * (nd + md) * (nd - md))) * std::cos(theta) * P(n - 2, m);
70 }
71 }
72 }
73
74 // Recurrence relations for the derivative of the normalized associated Legendre functions (see 'GUT User Guide' eq. 4.2.6)
75 Pd(0, 0) = T(0.0);
76 Pd(1, 0) = -std::numbers::sqrt3 * std::sin(theta);
77 Pd(1, 1) = std::numbers::sqrt3 * std::cos(theta);
78
79 for (int n = 2; n <= N - 1; n++) // 2nd for-loop is necessary, because for the calculation of 'Pd', all coefficients of 'P' must be available
80 {
81 auto nd = static_cast<double>(n);
82
83 Pd(n, 0) = -0.5 * std::sqrt(2.0 * nd * (nd + 1.0)) * P(n, 1);
84 Pd(n, 1) = 0.5 * (std::sqrt(2.0 * nd * (nd + 1.0)) * P(n, 0) - std::sqrt((nd - 1.0) * (nd + 2.0)) * std::pow(P(n, 2), 2.0));
85
86 for (int m = 2; m <= n - 1; m++)
87 {
88 auto md = static_cast<double>(m);
89
90 // else if ((m > 1) && (m < N - 1))
91 Pd(n, m) = 0.5 * (std::sqrt((nd + md) * (nd - md + 1.0)) * P(n, m - 1) - std::sqrt((nd - md) * (nd + md + 1.0)) * P(n, m + 1));
92 }
93 }
94
95 return std::make_pair(P, Pd);
96}
97
98} // namespace NAV::internal
Holds all Constants.
Vector space operations.
Utility class for logging to console and file.
EGM96 coefficients.
Definition GMFCoeffs.hpp:21
std::pair< Eigen::MatrixX< T >, Eigen::MatrixX< T > > associatedLegendre(const T &theta, size_t ndegree=10)
Calculates the associated Legendre Polynomial coefficients necessary for the EGM96.
Definition AssociatedLegendre.hpp:33