EmrysFilter 0.1.0
State estimation utilities in C++23 using Eigen
Loading...
Searching...
No Matches
Moments.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <type_traits>
10
17
18namespace Emrys
19{
20
25template <typename X>
26using ScalarOf = typename std::remove_cvref_t<X>::Scalar;
27
31enum class MomentsError
32{
34};
35
53template <Space Space_, RealScalar Scalar_ = double>
54class Moments
55{
56public:
57
58 using Scalar = Scalar_;
59 using Index = Eigen::Index;
60 using SpaceType = Space_;
61
64
67
70
73
77
78 Moments() = default;
79
87
89 explicit Moments(Space_ const & space)
90 : Moments{Emrys::valueDimension(space), Emrys::differenceDimension(space)}
91 {}
92
96 template <MatrixExpression Mean, CovarianceExpression Cov>
97 Moments(Mean && mean, Cov && cov)
98 : m_mean{std::forward<Mean>(mean)}
99 , m_cov{std::forward<Cov>(cov)}
100 {
101 EMRYS_FILTER_ASSERT(m_cov.rows() == m_cov.cols() && "the covariance must be square");
102 EMRYS_FILTER_ASSERT(
103 (!internal::hasEqualSizes<Space_> || m_cov.rows() == m_mean.size())
104 && "the covariance must have a row per coordinate of the mean"
105 );
106 }
107
109 [[nodiscard]] Index
111 {
112 return m_mean.size();
113 }
114
116 [[nodiscard]] Index
118 {
119 return m_cov.rows();
120 }
121
125 void
131
133 void
134 resize(Space_ const & space)
135 {
137 }
138
140 [[nodiscard]] VectorType const &
141 mean() const
142 {
143 return m_mean;
144 }
145
147 [[nodiscard]] MatrixType const &
149 {
150 return m_cov;
151 }
152
154 [[nodiscard]] VectorType &
156 {
157 return m_mean;
158 }
159
161 [[nodiscard]] MatrixType &
163 {
164 return m_cov;
165 }
166
171 template <typename Path>
172 [[nodiscard]] auto
173 mean() const
174 {
175 return m_mean(idx<Space_, Path>());
176 }
177
182 template <typename Path>
183 [[nodiscard]] auto
185 {
187 }
188
190 template <typename RowPath, typename ColPath>
191 [[nodiscard]] auto
193 {
195 }
196
198 template <typename Path>
199 [[nodiscard]] decltype(auto)
201 {
202 return m_mean(idx<Space_, Path>());
203 }
204
206 template <typename Path>
207 [[nodiscard]] decltype(auto)
209 {
211 }
212
213private:
214
215 VectorType m_mean;
216 MatrixType m_cov;
217};
218
219} // namespace Emrys
220
221#ifndef EMRYS_FILTER_NO_IO
222
223namespace Emrys
224{
225
226namespace internal
227{
228
230inline void
231describe(std::ostream & stream, MomentsError error, Indent indent)
232{
233 stream << indent;
234 switch (error)
235 {
237 stream << "the moments are infinite";
238 break;
239 }
240}
241
243template <typename Space_, typename Scalar_>
244void
245describe(std::ostream & stream, Moments<Space_, Scalar_> const & moments, Indent indent)
246{
247 stream << indent << "moments";
248 printDimension(stream, moments.valueDimension(), moments.differenceDimension());
249 printMoments(stream, indent.deeper(), moments);
250}
251
252} // namespace internal
253
254} // namespace Emrys
255
256#endif // EMRYS_FILTER_NO_IO
How a function that stores a matrix argument takes it (decisions.md [forwarded-matrices]): the concep...
The macros every header includes first.
Mathematical constants and scalar functions that work in every scalar type the library accepts,...
Printing: operator<< and std::formatter for every distribution, every filter (which prints its name a...
The Space concept and what every continuous space shares (decisions.md [sizes-and-dimensions]): the t...
Tag paths and the index utilities (decisions.md [tag-addressing], [index-names]): TagPath and TagPath...
decltype(auto) covarianceLower()
Definition Moments.h:208
Space_ SpaceType
Definition Moments.h:60
MatrixType const & covarianceLower() const
Definition Moments.h:148
Scalar_ Scalar
Definition Moments.h:58
Eigen::Index Index
Definition Moments.h:59
MatrixType & covarianceLower()
Definition Moments.h:162
DifferenceMatrix< Space_, Space_, Scalar > MatrixType
Definition Moments.h:76
DifferenceVector< Space_, Scalar > DifferenceVectorType
Definition Moments.h:75
Index differenceDimension() const
Definition Moments.h:117
Moments(Mean &&mean, Cov &&cov)
Constructs moments from mean and cov, moving either one passed as an rvalue of the stored type.
Definition Moments.h:97
ValueVector< Space_, Scalar > VectorType
Definition Moments.h:74
Moments(Index valueDimension, Index differenceDimension)
Constructs moments with valueDimension coordinates in the mean and differenceDimension in the covaria...
Definition Moments.h:83
auto covarianceLower() const
Definition Moments.h:184
Index valueDimension() const
Definition Moments.h:110
Moments(Space_ const &space)
Constructs moments sized for space, with uninitialized contents.
Definition Moments.h:89
VectorType & mean()
Definition Moments.h:155
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition Moments.h:72
VectorType const & mean() const
Definition Moments.h:141
decltype(auto) mean()
Definition Moments.h:200
void resize(Index valueDimension, Index differenceDimension)
Definition Moments.h:126
auto covarianceLower() const
Definition Moments.h:192
auto mean() const
Definition Moments.h:173
static constexpr int DifferenceDimensionAtCompileTime
Definition Moments.h:69
static constexpr int MaxValueDimensionAtCompileTime
Definition Moments.h:66
static constexpr int ValueDimensionAtCompileTime
Definition Moments.h:63
void resize(Space_ const &space)
Resizes the mean and covariance for space.
Definition Moments.h:134
MomentsError
Reasons a distribution's moments() can fail.
Definition Moments.h:32
typename std::remove_cvref_t< X >::Scalar ScalarOf
The scalar type of the Eigen object or distribution X, for writing functions generic in it.
Definition Moments.h:26
@ Infinite
the distribution has no finite mean or covariance, such as a Student-t with
Definition Moments.h:33
Eigen::Index valueDimension(S const &space)
Definition Space.h:272
constexpr auto diffIdx()
idx<S, Path>() of the differences of the subspace named by Path: its rows of a covariance.
Definition TagPath.h:669
ValueVector< DifferenceSpace< S >, Scalar > DifferenceVector
A difference in S as an Eigen column vector of Scalar, the type of minus() and of the offset plus() t...
Definition Space.h:650
Eigen::Index differenceDimension(S const &space)
Definition Space.h:284
constexpr int valueDimensionAtCompileTime
The value dimension of Space at compile time, or Eigen::Dynamic if it is resizable.
Definition Space.h:251
constexpr int maxValueDimensionAtCompileTime
The largest valid value dimension of Space, or Eigen::Dynamic if it is unbounded.
Definition Space.h:258
constexpr auto idx()
Definition TagPath.h:658
internal::MatrixOf< Scalar, valueDimensionAtCompileTime< DifferenceSpace< Rows > >, valueDimensionAtCompileTime< DifferenceSpace< Cols > >, maxValueDimensionAtCompileTime< DifferenceSpace< Rows > >, maxValueDimensionAtCompileTime< DifferenceSpace< Cols > > > DifferenceMatrix
An Eigen matrix of Scalar over the differences of Rows and Cols: a covariance, with one space for bot...
Definition Space.h:659
Eigen::Matrix< Scalar, valueDimensionAtCompileTime< S >, 1, Eigen::ColMajor, maxValueDimensionAtCompileTime< S >, 1 > ValueVector
A value in S as an Eigen column vector of Scalar: a mean, a sample, or a true state.
Definition Space.h:640