OpenKalman
WeightedParticleDistribution.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 OPENKALMAN_WEIGHTEDPARTICLEDISTRIBUTION_HPP
12 #define OPENKALMAN_WEIGHTEDPARTICLEDISTRIBUTION_HPP
13 
14 #include <vector>
15 #include <tuple>
16 #include <numeric>
17 #include <algorithm>
18 #include "distributions/ParticleDistribution.hpp"
19 
20 namespace OpenKalman {
21 
22  template<typename WeightScalar = double, typename... OtherProperties>
23  struct WeightedParticleDistribution: ParticleDistribution<WeightScalar, OtherProperties...>
24  {
25  protected:
26  using Parent = ParticleDistribution<WeightScalar, OtherProperties...>
27  using Parent::Properties;
28 
29  public:
30  const auto normalizeWeights()
31  {
32  auto sum = std::accumulate(
33  begin(),
34  end(),
35  WeightScalar {0},
36  [](const WeightScalar& a, const Properties& p) -> WeightScalar {
37  return a + p.template get<0>();
38  }
39  );
40 
41  std::for_each(begin(), end(), [](const Properties& p) { p.template get<0>() /= sum; });
42  return *this;
43  }
44 
45  };
46 
47 }
48 
49 #endif //OPENKALMAN_WEIGHTEDPARTICLEDISTRIBUTION_HPP
Distribution of particles.
Definition: ParticleDistribution.hpp:27
The root namespace for OpenKalman.
Definition: basics.hpp:34
Definition: WeightedParticleDistribution.hpp:23
decltype(auto) constexpr sum(Ts &&...ts)
Element-by-element sum of one or more objects.
Definition: sum.hpp:112