ssmkit  master-68aed98
base.hpp
Go to the documentation of this file.
1 
11 #ifndef SSMPACK_FILTER_RESAMPLER_BASE
12 #define SSMPACK_FILTER_RESAMPLER_BASE
13 
14 #include <armadillo>
15 
16 namespace ssmkit {
17 namespace filter {
18 namespace resampler {
19 
22 template<class T>
24 
25 template <template <class> class Method, class Criterion>
26 class BaseResampler<Method<Criterion>> {
27  protected:
28  Criterion criterion_;
29 
30  public:
31  BaseResampler(Criterion criterion) : criterion_(std::move(criterion)) {}
32 
33  template <class Particles, class Weights>
34  void operator()(Particles &pars, Weights &w) {
35  // return if resampling criterion is false
36  if (!criterion_(w))
37  return;
38 
39  // generate ordered numbers
40  auto u = static_cast<Method<Criterion> *>(this)
41  ->generateOrderedNumbers(w.n_rows);
42 
43  auto ws = arma::cumsum(w);
44  auto u_it = u.begin();
45 
46  Particles old_pars = pars;
47 
48  pars.each_col([&u_it, &old_pars,
49  &ws](arma::Col<typename Particles::elem_type> &col) {
50  col = old_pars.col(
51  static_cast<arma::uvec>(arma::find(ws > *u_it++, 1, "first"))(0));
52  });
53 
54  w.fill(1.0 / w.n_rows);
55  }
56 };
57 
58 } // namespace resampler
59 } // namespace filter
60 } // namespace ssmkit
61 #endif //SSMPACK_FILTER_RESAMPLER_IDENTITY
Base resampling class.
Definition: base.hpp:23