32 #ifndef VSMC_RESAMPLE_TRANSFORM_HPP 33 #define VSMC_RESAMPLE_TRANSFORM_HPP 49 template <
typename InputIter,
typename OutputIterR,
typename OutputIterI>
51 InputIter weight, OutputIterR resid, OutputIterI integ)
53 using resid_type =
typename std::iterator_traits<OutputIterR>::value_type;
54 using integ_type =
typename std::iterator_traits<OutputIterI>::value_type;
56 static_assert(std::is_floating_point<resid_type>::value,
57 "**resample_trans_residual** OUTPUT resid IS NOT OF FLOATING POINT " 60 resid_type sum_resid = 0;
61 integ_type sum_integ = 0;
62 OutputIterR resid_i = resid;
63 OutputIterI integ_i = integ;
64 const resid_type coeff =
static_cast<resid_type
>(M);
65 for (std::size_t i = 0; i != N; ++i, ++weight, ++resid_i, ++integ_i) {
66 resid_type w = coeff *
static_cast<resid_type
>(*weight);
69 *integ_i =
static_cast<integ_type
>(integral);
70 sum_resid += *resid_i;
71 sum_integ += *integ_i;
74 const resid_type mul_resid = 1 / sum_resid;
75 for (std::size_t i = 0; i != N; ++i, ++resid)
78 return M -
static_cast<std::size_t
>(sum_integ);
89 template <
typename InputIter,
typename OutputIter,
typename U01SeqType>
91 InputIter weight, U01SeqType &&u01seq, OutputIter replication)
93 using real_type =
typename std::iterator_traits<InputIter>::value_type;
94 using rep_type =
typename std::iterator_traits<OutputIter>::value_type;
100 *replication++ =
static_cast<rep_type
>(M);
104 OutputIter rep = std::fill_n(replication, N, static_cast<rep_type>(0));
110 for (std::size_t i = 0; i != N - 1; ++i, ++weight, ++replication) {
112 while (j != M && static_cast<real_type>(u01seq[j]) < accw) {
117 *replication++ =
static_cast<rep_type
>(M - j);
129 template <
typename InputIter,
typename OutputIter>
131 std::size_t N, std::size_t M, InputIter replication, OutputIter index)
133 using rep_type =
typename std::iterator_traits<InputIter>::value_type;
134 using idx_type =
typename std::iterator_traits<OutputIter>::value_type;
136 if (N == 0 || M == 0)
139 const std::size_t K = std::min(N, M);
142 InputIter rep = replication;
144 auto seek = [N, K, &time, &src, &rep]() {
145 if (src < K && *rep < time + 2) {
150 }
while (src < K && *rep < time + 2);
152 if (src >= K && *rep < time + 1) {
157 }
while (src < N && *rep < time + 1);
161 for (std::size_t dst = 0; dst != K; ++dst, ++replication, ++index) {
162 if (*replication > 0) {
163 *index =
static_cast<idx_type
>(dst);
166 *index =
static_cast<idx_type
>(src);
171 for (std::size_t dst = K; dst < M; ++dst, ++index) {
173 *index =
static_cast<idx_type
>(src);
182 #endif // VSMC_RESAMPLE_TRANSFORM_HPP
OutputIter resample_trans_rep_index(std::size_t N, std::size_t M, InputIter replication, OutputIter index)
Transform replication numbers into parent indices.
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.
void modf(std::size_t n, const float *a, float *y, float *z)