Repository navigation
Expand file tree
/
Copy pathLinearTimeInvariant.hpp
More file actions
95 lines (85 loc) · 3.51 KB
/
Copy pathLinearTimeInvariant.hpp
File metadata and controls
95 lines (85 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC optimize("O3", "fast-math")
#endif
#include "numerical/math/CompilerOptimizations.hpp"
#include "numerical/math/Matrix.hpp"
#include "numerical/math/QNumber.hpp"
namespace math
{
// Discrete-time linear time-invariant state-space model:
// x_{k+1} = A x_k + B u_k
// y_k = C x_k + D u_k
//
// Template args: <T, StateSize=n, InputSize=m, OutputSize=p>
// OutputSize defaults to StateSize. All matrices are zero-initialised by default;
// use the WithFullStateOutput factory to get C = I.
template<typename T,
std::size_t StateSize,
std::size_t InputSize,
std::size_t OutputSize = StateSize>
struct LinearTimeInvariant
{
static_assert(detail::is_supported_type_v<T>,
"LinearTimeInvariant only supports float or QNumber types");
static_assert(detail::is_valid_dimensions_v<StateSize, StateSize>,
"StateSize must be positive");
static_assert(detail::is_valid_dimensions_v<InputSize, InputSize>,
"InputSize must be positive");
static_assert(detail::is_valid_dimensions_v<OutputSize, OutputSize>,
"OutputSize must be positive");
using StateMatrix = SquareMatrix<T, StateSize>;
using InputMatrix = Matrix<T, StateSize, InputSize>;
using OutputMatrix = Matrix<T, OutputSize, StateSize>;
using FeedthroughMatrix = Matrix<T, OutputSize, InputSize>;
using StateVector = Vector<T, StateSize>;
using InputVector = Vector<T, InputSize>;
using OutputVector = Vector<T, OutputSize>;
StateMatrix A{};
InputMatrix B{};
OutputMatrix C{};
FeedthroughMatrix D{};
// x_{k+1} = A x_k + B u_k
ALWAYS_INLINE_HOT StateVector Step(const StateVector& x, const InputVector& u) const
{
return A * x + B * u;
}
// y_k = C x_k + D u_k
ALWAYS_INLINE_HOT OutputVector Output(const StateVector& x, const InputVector& u) const
{
return C * x + D * u;
}
// Factory: C = I (OutputSize must equal StateSize), D = 0
static LinearTimeInvariant WithFullStateOutput(
const StateMatrix& stateTransition, const InputMatrix& inputMatrix)
requires(OutputSize == StateSize)
{
LinearTimeInvariant lti;
lti.A = stateTransition;
lti.B = inputMatrix;
lti.C = OutputMatrix::Identity();
return lti;
}
// Factory: autonomous system (B = 0, D = 0)
static LinearTimeInvariant Autonomous(
const StateMatrix& stateTransition, const OutputMatrix& outputMatrix)
{
LinearTimeInvariant lti;
lti.A = stateTransition;
lti.C = outputMatrix;
return lti;
}
};
#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD
extern template struct LinearTimeInvariant<float, 2, 1>;
extern template struct LinearTimeInvariant<float, 2, 1, 1>;
extern template struct LinearTimeInvariant<float, 3, 1>;
extern template struct LinearTimeInvariant<float, 4, 1>;
extern template struct LinearTimeInvariant<float, 4, 1, 1>;
extern template struct LinearTimeInvariant<float, 2, 2>;
extern template struct LinearTimeInvariant<math::Q15, 2, 1>;
extern template struct LinearTimeInvariant<math::Q15, 2, 1, 1>;
extern template struct LinearTimeInvariant<math::Q31, 2, 1>;
extern template struct LinearTimeInvariant<math::Q31, 2, 1, 1>;
#endif
}