16 #ifndef OPENKALMAN_KALMANFILTER_HPP 17 #define OPENKALMAN_KALMANFILTER_HPP 25 template<
typename...Transform>
33 template<
typename Transform>
39 template<
typename XDistribution,
typename YDistribution,
typename CrossCovariance,
typename Measurement>
41 update_step(
const XDistribution& Nx,
const YDistribution& Ny,
const CrossCovariance& P_xy,
const Measurement& z)
43 static_assert(gaussian_distribution<XDistribution>);
44 static_assert(gaussian_distribution<YDistribution>);
45 static_assert(typed_matrix<CrossCovariance>);
46 static_assert(typed_matrix<Measurement> and vector<Measurement>);
48 typename DistributionTraits<YDistribution>::StaticDescriptor>);
50 typename DistributionTraits<XDistribution>::StaticDescriptor>);
52 typename DistributionTraits<YDistribution>::StaticDescriptor>);
54 const auto y = mean_of(Ny);
55 const auto P_yy = covariance_of(Ny);
60 using re =
typename DistributionTraits<XDistribution>::random_number_engine;
62 if constexpr (cholesky_form<YDistribution>)
66 auto out_x_cov = covariance_of(Nx) - square(
LQ_decomposition(K * square_root(P_yy)));
67 return make_GaussianDistribution<re>(std::move(out_x_mean), std::move(out_x_cov));
73 auto out_x_cov = covariance_of(Nx) -
Covariance(P_xy * K_adj);
74 return make_GaussianDistribution<re>(std::move(out_x_mean), std::move(out_x_cov));
86 template<
typename...ProcessTransformArguments>
88 predict(
const ProcessTransformArguments&...args)
90 return transform(args...);
96 template<
typename Measurement,
typename State,
typename...MeasurementTransformArguments>
98 update(
const Measurement& z,
const State& x,
const MeasurementTransformArguments&...args)
100 const auto [y, P_xy] = transform.transform_with_cross_covariance(x, args...);
101 return update_step(x, y, P_xy, z);
109 typename Measurement,
111 typename...ProcessTransformArgs,
112 typename...MeasurementTransformArgs>
115 const Measurement& z,
117 const std::tuple<ProcessTransformArgs...>& proc_args = std::tuple {},
118 const std::tuple<MeasurementTransformArgs...>& meas_args = std::tuple {})
120 const auto&& [y_, P_xy, y] = std::apply(
121 transform.transform_with_cross_covariance,
122 std::tuple_cat(std::forward_as_tuple(x), proc_args, meas_args));
123 return update_step(y, y_, P_xy, z);
134 template<
typename ProcessTransform,
typename MeasurementTransform>
139 using Base::transform;
140 MeasurementTransform measurement_transform;
142 using Base::update_step;
145 KalmanFilter(
const ProcessTransform& p_transform,
const MeasurementTransform& m_transform)
146 :
Base(p_transform), measurement_transform(m_transform)
152 template<
typename Measurement,
typename State,
typename...MeasurementTransformArguments>
154 update(
const Measurement& z,
const State& x,
const MeasurementTransformArguments&...args)
156 const auto [y, P_xy] = measurement_transform.transform_with_cross_covariance(x, args...);
157 return update_step(x, y, P_xy, z);
165 typename Measurement,
167 typename...ProcessTransformArgs,
168 typename...MeasurementTransformArgs>
171 const Measurement& z,
173 const std::tuple<ProcessTransformArgs...>& proc_args = std::tuple {},
174 const std::tuple<MeasurementTransformArgs...>& meas_args = std::tuple {})
176 const auto y = std::apply(transform, std::tuple_cat(std::forward_as_tuple(x), proc_args));
177 const auto [y_, P_xy] = std::apply(
178 measurement_transform.transform_with_cross_covariance,
179 std::tuple_cat(std::forward_as_tuple(y), meas_args));
180 return update_step(y, y_, P_xy, z);
193 template<
typename P,
typename M>
198 #endif //OPENKALMAN_KALMANFILTER_HPP decltype(auto) constexpr contract(A &&a, B &&b)
Matrix multiplication of A * B.
Definition: contract.hpp:54
A set of one or more column vectors, each representing a statistical mean.
Definition: forward-class-declarations.hpp:477
A Kalman filter, using one or more statistical transforms.
Definition: KalmanFilter.hpp:26
The root namespace for OpenKalman.
Definition: basics.hpp:34
constexpr auto solve(A &&a, B &&b)
Solve the equation AX = B for X, which may or may not be a unique solution.
Definition: solve.hpp:87
decltype(auto) constexpr adjoint(Arg &&arg)
Take the adjoint of a matrix.
Definition: adjoint.hpp:33
typename vector_space_descriptor_of< T, N >::type vector_space_descriptor_of_t
helper template for vector_space_descriptor_of.
Definition: vector_space_descriptor_of.hpp:56
decltype(auto) constexpr sum(Ts &&...ts)
Element-by-element sum of one or more objects.
Definition: sum.hpp:112
Covariance(M &&) -> Covariance< Dimensions< index_dimension_of_v< M, 0 >>, passable_t< M >>
Deduce Covariance type from a covariance_nestable.
decltype(auto) constexpr LQ_decomposition(A &&a)
Perform an LQ decomposition of matrix A=[L,0]Q, L is a lower-triangular matrix, and Q is orthogonal...
Definition: LQ_decomposition.hpp:33
constexpr detail::update_adaptor update
a std::ranges::range_adaptor_closure associated with update_view.
Definition: update.hpp:403