32 #ifndef VSMC_RESAMPLE_ALGORITHM_HPP 33 #define VSMC_RESAMPLE_ALGORITHM_HPP 39 #define VSMC_RUNTIME_ASSERT_RESAMPLE_ALGORITHM_EVAL \ 40 VSMC_RUNTIME_ASSERT( \ 41 eval_, "**ResampleEval::operator()** INVALID ALGORITHM OBJECT") 48 template <
typename U01SeqType,
bool Res
idual>
59 template <
typename RNGType,
typename InputIter,
typename OutputIter>
60 OutputIter
operator()(std::size_t N, std::size_t M, RNGType &rng,
61 InputIter weight, OutputIter replication)
const 63 return eval(N, M, rng, weight, replication,
64 std::integral_constant<bool, Residual>());
70 template <
typename RNGType,
typename InputIter,
typename OutputIter>
71 OutputIter eval(std::size_t N, std::size_t M, RNGType &rng,
72 InputIter weight, OutputIter replication, std::false_type)
const 74 using real_type =
typename std::iterator_traits<InputIter>::value_type;
77 u01seq_(rng, M, u01.data());
82 template <
typename RNGType,
typename InputIter,
typename OutputIter>
83 OutputIter eval(std::size_t N, std::size_t M, RNGType &rng,
84 InputIter weight, OutputIter replication, std::true_type)
const 86 using real_type =
typename std::iterator_traits<InputIter>::value_type;
87 using rep_type =
typename std::iterator_traits<OutputIter>::value_type;
95 u01seq_(rng, R, u01.data());
97 for (std::size_t i = 0; i != N; ++i, ++replication)
98 *replication += integ[i];
105 template <
typename T>
109 using eval_type = std::function<void(std::size_t, std::size_t,
121 return std::numeric_limits<double>::infinity();
126 return -std::numeric_limits<double>::infinity();
139 const std::size_t N =
static_cast<std::size_t
>(particle.
size());
142 eval_(N, N, particle.
rng(), particle.
weight().data(), rep.data());
144 particle.
state().select(N, idx.data());
145 particle.
weight().set_equal();
187 template <ResampleScheme>
246 template <ResampleScheme Scheme>
251 #endif // VSMC_RESAMPLE_ALGORITHM_HPP std::vector< T, Alloc > Vector
std::vector with Allocator as default allocator
ResampleAlgorithm< U01SequenceSorted, false > ResampleMultinomial
Multinomial resampling.
Particle class representing the whole particle set.
typename rng_set_type::rng_type rng_type
std::size_t operator()(std::size_t, Particle< T > &particle) const
Returns how many particles having non-zero replication number.
state_type & state()
Read and write access to the state collection object.
OutputIter resample_trans_rep_index(std::size_t N, std::size_t M, InputIter replication, OutputIter index)
Transform replication numbers into parent indices.
static constexpr double always()
weight_type & weight()
Read and write access to the weight collection object.
Stratified resampling on residuals.
void set_eval(const eval_type &new_eval)
Set a new evaluation object of type eval_type.
OutputIter operator()(std::size_t N, std::size_t M, RNGType &rng, InputIter weight, OutputIter replication) const
Generate replication numbers from normalized weights.
ResampleAlgorithm< U01SequenceStratified, false > ResampleStratified
Stratified resampling.
ResampleEval(const eval_type &eval)
Construct a Sampler::move_type object.
ResampleAlgorithm< U01SequenceSystematic, true > ResampleResidualSystematic
Residual systematic resampling.
Sampler<T>::eval_type subtype.
RealType u01(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
ResampleAlgorithm< U01SequenceStratified, true > ResampleResidualStratified
Residual stratified resampling.
Systematic resampling on residuals.
std::size_t resample_trans_residual(std::size_t N, std::size_t M, InputIter weight, OutputIterR resid, OutputIterI integ)
Transform normalized weights to normalized residual and integrals,.
OutputIter resample_trans_u01_rep(std::size_t N, std::size_t M, InputIter weight, U01SeqType &&u01seq, OutputIter replication)
Transform uniform [0, 1) sequence into replication numbers.
rng_type & rng(size_type id)
Get an (parallel) RNG stream for a given particle.
static constexpr double never()
#define VSMC_RUNTIME_ASSERT_RESAMPLE_ALGORITHM_EVAL
std::function< void(std::size_t, std::size_t, typename Particle< T >::rng_type &, const double *, typename Particle< T >::size_type *)> eval_type
size_type size() const
Number of particles.
Type trait of ResampleScheme parameter.
ResampleAlgorithm< U01SequenceSorted, true > ResampleResidual
Residual resampling.
typename ResampleTypeTrait< Scheme >::type ResampleType
Type of resample class corresponding to ResampleScheme parameter.
ResampleAlgorithm< U01SequenceSystematic, false > ResampleSystematic
Systematic resampling.