OpenKalman
ContinuousPropertyParticleDistribution.hpp
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2017-2021 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
11 #ifndef CONTINUOUSPROPERTYPARTICLEDISTRIBUTION_HPP
12 #define CONTINUOUSPROPERTYPARTICLEDISTRIBUTION_HPP
13 
14 #include <vector>
15 #include <tuple>
16 #include <numeric>
17 #include <algorithm>
18 #include <Eigen/Dense>
19 #include "distributions/WeightedParticleDistribution.hpp"
21 
22 namespace OpenKalman {
23 
24  template<template<int, typename> typename ContinuousDistribution,
25  int continuous_dimensions,
26  typename Scalar = double,
27  typename... OtherProperties>
30  Scalar, // Weights for each particle
31  eigen_matrix_t<Scalar, continuous_dimensions, 1>,
32  OtherProperties...>
33  {
34  public:
35  using Mean = eigen_matrix_t<Scalar, continuous_dimensions, 1>;
36  using Covariance = eigen_matrix_t<Scalar, continuous_dimensions, continuous_dimensions>;
37 
38  protected:
40  using Parent::Properties;
41 
42  public:
43  const auto getDistribution() const
44  {
45  using Acc = std::tuple<Mean, Mean>;
46  auto acc = std::accumulate(
47  begin(),
48  end(),
49  Acc {Mean::Zero(), Covariance::Zero()},
50  [](const Acc& a, const Properties& p) -> Acc {
51  Mean x = p.template get<0>() * p.template get<1>(); // x == weighted value
52  return {a.template get<0>() + x, a.template get<1>() + x * x.transpose()};
53  }
54  );
55  // assume weights are normalized
56  Mean mean = acc.template get<0>;
57  Covariance covariance = acc.template get<1> - mean * mean.transpose();
58  return ContinuousDistribution<continuous_dimensions, Scalar>(GaussianDistribution(mean, covariance));
59  }
60 
61 
62  };
63 }
64 
65 #endif //CONTINUOUSPROPERTYPARTICLEDISTRIBUTION_HPP
A meta-header file including all the headers relating to OpenKalman distributions.
A Gaussian distribution, defined in terms of a Mean and a Covariance.
Definition: GaussianDistribution.hpp:42
The root namespace for OpenKalman.
Definition: basics.hpp:34
Definition: WeightedParticleDistribution.hpp:23
constexpr bool covariance
T is a specialization of either Covariance or SquareRootCovariance.
Definition: object-types.hpp:161
Definition: ContinuousPropertyParticleDistribution.hpp:28
constexpr bool mean
Specifies that T is a mean (i.e., is a specialization of the class Mean).
Definition: object-types.hpp:40