EmrysFilter
0.1.0
State estimation utilities in C++23 using Eigen
Toggle main menu visibility
Loading...
Searching...
No Matches
Time.h
Go to the documentation of this file.
1
#pragma once
2
8
9
#include <chrono>
10
#include <concepts>
11
#include <type_traits>
12
13
#include <Eigen/Core>
14
15
#include <
EmrysFilter/Core/Macros.h
>
16
17
namespace
Emrys
18
{
19
51
65
template
<
typename
Time>
66
struct
TimeTraits
67
{};
68
72
template
<std::
float
ing_po
int
Time>
73
struct
TimeTraits
<Time>
74
{
76
template
<
typename
Scalar>
77
[[nodiscard]]
static
Scalar
78
toScalar
(Time t)
79
{
80
return
Scalar(t);
81
}
82
};
83
87
template
<
typename
Rep,
typename
Period>
88
struct
TimeTraits
<std::chrono::duration<Rep, Period>>
89
{
94
template
<
typename
Scalar>
95
[[nodiscard]]
static
Scalar
96
toScalar
(std::chrono::duration<Rep, Period>
const
& t)
97
{
98
// Through the scalar's literal type, the plain number an autodiff scalar is built from, so
99
// that the integer's conversion is explicit there too.
100
using
Real =
typename
Eigen::NumTraits<Scalar>::Literal;
101
Scalar
const
count = Scalar(Real(t.count()));
102
Scalar
const
numerator = Scalar(Real(Period::num));
103
Scalar
const
denominator = Scalar(Real(Period::den));
104
if
constexpr
(Period::num == 1 && Period::den == 1)
105
{
106
return
count;
107
}
108
else
if
constexpr
(Period::num == 1)
109
{
110
return
Scalar(count / denominator);
111
}
112
else
if
constexpr
(Period::den == 1)
113
{
114
return
Scalar(count * numerator);
115
}
116
else
117
{
118
return
Scalar(Scalar(count * numerator) / denominator);
119
}
120
}
121
};
122
127
template
<
typename
Time,
typename
Scalar>
128
concept
TimeStep
= std::same_as<std::remove_cvref_t<Time>, Scalar> ||
requires
(std::remove_cvref_t<Time>
const
& t) {
129
{
TimeTraits<std::remove_cvref_t<Time>
>::template toScalar<Scalar>(t) } -> std::same_as<Scalar>;
130
};
131
135
struct
Steps
136
{
137
Eigen::Index
count
;
138
};
139
140
namespace
internal
141
{
142
147
template
<
typename
Scalar,
typename
Time>
148
requires
TimeStep<Time, Scalar>
149
[[nodiscard]] Scalar
150
toScalarTime(Time
const
& t)
151
{
152
Scalar
const
step = [&] {
153
if
constexpr
(std::same_as<Time, Scalar>)
154
{
155
return
t;
156
}
157
else
158
{
159
return
TimeTraits<Time>::template
toScalar<Scalar>(t);
160
}
161
}();
162
EMRYS_FILTER_ASSERT(step >= Scalar(0) &&
"a time step must be nonnegative"
);
163
return
step;
164
}
165
169
template
<
typename
... Args>
170
inline
constexpr
bool
leadsWithSteps =
false
;
171
173
template
<
typename
First,
typename
... Rest>
174
inline
constexpr
bool
leadsWithSteps<First, Rest...> = std::same_as<std::remove_cvref_t<First>, Steps>;
175
182
template
<
typename
... Args>
183
inline
constexpr
bool
leadsWithTimeStep =
false
;
184
186
template
<
typename
First,
typename
... Rest>
187
inline
constexpr
bool
leadsWithTimeStep<First, Rest...> =
188
std::is_arithmetic_v<std::remove_cvref_t<First>> || TimeStep<First, double> || TimeStep<First, float>;
189
193
template
<
typename
... Inputs>
194
consteval
void
195
rejectTimeStep()
196
{
197
static_assert
(
198
!leadsWithTimeStep<Inputs...>,
199
"a fixed-step filter takes its own step: call predict() for one step or predict(Steps{n}) for n, "
200
"and give an input by its tag, as sure<Tag>(value) or a distribution over the tag's space"
201
);
202
}
203
204
}
// namespace internal
205
206
}
// namespace Emrys
Macros.h
The macros every header includes first.
Emrys::TimeStep
A step of time that converts to Scalar: Scalar itself, or any type with a TimeTraits,...
Definition
Time.h:128
Emrys::Steps
A count of a fixed-step model's steps, for predict(Steps{n}).
Definition
Time.h:136
Emrys::Steps::count
Eigen::Index count
the number of steps, at least zero
Definition
Time.h:137
Emrys::TimeTraits< Time >::toScalar
static Scalar toScalar(Time t)
Definition
Time.h:78
Emrys::TimeTraits< std::chrono::duration< Rep, Period > >::toScalar
static Scalar toScalar(std::chrono::duration< Rep, Period > const &t)
Definition
Time.h:96
Emrys::TimeTraits
How a time type converts to a scalar, as a static member template toScalar<Scalar>(t) returning Scala...
Definition
Time.h:67
EmrysFilter
Core
Time.h
Generated by
1.18.0