tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
kalman.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_KALMAN_H
2#define FUSED_ALGORITHMS_KALMAN_H
3
4#include "config.h"
6#include "matrix_traits.h"
9
52namespace matrix_algorithms
53{
54
56
69 template <typename T, my_size_t N, my_size_t M>
71 const FusedMatrix<T, N, N> &P,
72 const FusedMatrix<T, M, N> &H,
73 const FusedMatrix<T, M, M> &R)
74 {
75 static_assert(is_floating_point_v<T>,
76 "kalman_gain requires a floating-point scalar type");
77
78 // S = H·P·Hᵀ + R (M×M)
79 auto HP = FusedMatrix<T, M, N>::matmul(H, P);
80
83
84 // S⁻¹
85 auto S_inv_result = inverse(S);
86
87 if (!S_inv_result.has_value())
88 {
89 return Unexpected{S_inv_result.error()};
90 }
91
92 auto &S_inv = S_inv_result.value();
93
94 // K = P·Hᵀ·S⁻¹ (N×M)
96 auto K = FusedMatrix<T, N, M>::matmul(PHt, S_inv);
97
98 return move(K);
99 }
100
116 template <typename T, my_size_t N, my_size_t M>
118 const FusedMatrix<T, N, M> &K,
119 const FusedMatrix<T, M, N> &H,
120 const FusedMatrix<T, N, N> &P,
121 const FusedMatrix<T, M, M> &R)
122 {
123 static_assert(is_floating_point_v<T>,
124 "joseph_update requires a floating-point scalar type");
125
126 // IKH = I - K·H (N×N)
128 I.setIdentity();
129
130 auto KH = FusedMatrix<T, N, N>::matmul(K, H);
132 IKH = I - KH;
133
134 // (I-K·H)·P·(I-K·H)ᵀ
135 auto tmp = FusedMatrix<T, N, N>::matmul(IKH, P);
136 auto term1 = FusedMatrix<T, N, N>::matmul(tmp, IKH.transpose_view());
137
138 // K·R·Kᵀ
139 auto KR = FusedMatrix<T, N, M>::matmul(K, R);
140 auto term2 = FusedMatrix<T, N, N>::matmul(KR, K.transpose_view());
141
142 FusedMatrix<T, N, N> result(T(0));
143 result = term1 + term2;
144 return result;
145 }
146
147} // namespace matrix_algorithms
148
149#endif // FUSED_ALGORITHMS_KALMAN_H
A discriminated union holding either a success value or an error.
Definition expected.h:86
Definition fused_matrix.h:12
FusedMatrix & setIdentity(void)
Definition fused_matrix.h:234
static FusedMatrix< T, Rows, Cols > matmul(const BaseExpr< LeftExpr > &mat1, const BaseExpr< RightExpr > &mat2)
Definition fused_matrix.h:255
FORCE_INLINE auto transpose_view() const noexcept
Definition fused_tensor.h:304
Global configuration for the tesseract tensor library.
A minimal, STL-free expected/result type for failable operations.
Matrix inverse with compile-time dispatch.
Runtime property descriptors and error codes for matrices.
Definition cholesky.h:52
Expected< FusedMatrix< T, N, N >, MatrixStatus > inverse(const FusedMatrix< T, N, N > &A)
Compute the inverse of a square matrix.
Definition inverse.h:65
Expected< FusedMatrix< T, N, M >, MatrixStatus > kalman_gain(const FusedMatrix< T, N, N > &P, const FusedMatrix< T, M, N > &H, const FusedMatrix< T, M, M > &R)
Compute the Kalman gain K = P·Hᵀ·(H·P·Hᵀ + R)⁻¹.
Definition kalman.h:70
FusedMatrix< T, N, N > joseph_update(const FusedMatrix< T, N, M > &K, const FusedMatrix< T, M, N > &H, const FusedMatrix< T, N, N > &P, const FusedMatrix< T, M, M > &R)
Joseph form covariance update: P' = (I-K·H)·P·(I-K·H)ᵀ + K·R·Kᵀ.
Definition kalman.h:117
MatrixStatus
Error codes for matrix decomposition and solver algorithms.
Definition matrix_traits.h:33
constexpr remove_reference_t< T > && move(T &&t) noexcept
Cast to rvalue reference (replacement for std::move).
Definition simple_type_traits.h:178
Tag type for constructing an Expected in the error state.
Definition expected.h:30
E error
The error value.
Definition expected.h:31