32 #ifndef VSMC_RESAMPLE_TRANSFORM_HPP 33 #define VSMC_RESAMPLE_TRANSFORM_HPP 42 template <
typename IntType,
typename U01SeqType>
44 const double *weight, U01SeqType &&u01seq, IntType *replication)
53 *replication =
static_cast<IntType
>(N);
57 std::memset(replication, 0,
sizeof(IntType) * M);
63 for (std::size_t i = 0; i != M - 1; ++i) {
65 while (j != N && u01seq[j] <= accw) {
70 replication[M - 1] =
static_cast<IntType
>(N - j);
75 template <
typename IntType,
typename U01SeqType>
77 const double *weight, U01SeqType &&u01seq, IntType *src_idx)
82 std::memset(src_idx, 0,
sizeof(IntType) * N);
88 for (std::size_t i = 0; i != M - 1; ++i) {
90 while (j != N && u01seq[j] <= accw)
91 src_idx[j++] =
static_cast<IntType
>(i);
94 src_idx[j++] =
static_cast<IntType
>(M - 1);
99 template <
typename IntType1,
typename IntType2>
101 const IntType1 *replication, IntType2 *src_idx)
103 if (M == 0 || N == 0)
108 for (std::size_t src = 0; src != M; ++src) {
109 const IntType1 rep = replication[src];
110 for (IntType1 r = 0; r != rep; ++r)
111 src_idx[dst++] = static_cast<IntType2>(src);
118 for (std::size_t dst = 0; dst != N; ++dst) {
119 if (replication[dst] != 0) {
120 src_idx[dst] =
static_cast<IntType2
>(dst);
123 if (replication[src] < time + 2) {
128 while (replication[src] < 2);
130 src_idx[dst] =
static_cast<IntType2
>(src);
138 template <
typename IntType1,
typename IntType2>
140 const IntType1 *src_idx, IntType2 *replication)
142 if (M == 0 || N == 0)
145 std::memset(replication, 0,
sizeof(IntType2) * M);
147 for (std::size_t i = 0; i != N; ++i)
148 ++replication[src_idx[i]];
154 template <
typename IntType>
156 const double *weight,
double *resid, IntType *integ)
159 const double coeff =
static_cast<double>(N);
160 for (std::size_t i = 0; i != M; ++i) {
161 resid[i] = std::modf(coeff * weight[i], &integral);
162 integ[i] =
static_cast<IntType
>(integral);
164 mul(M, 1 / std::accumulate(resid, resid + M, 0.0), resid, resid);
165 IntType R = std::accumulate(integ, integ + M, static_cast<IntType>(0));
167 return N -
static_cast<std::size_t
>(R);
172 #endif // VSMC_RESAMPLE_TRANSFORM_HPP
void resample_trans_u01_rep(std::size_t M, std::size_t N, const double *weight, U01SeqType &&u01seq, IntType *replication)
Transform uniform [0, 1] sequence into replication numbers.
void mul(std::size_t n, const float *a, const float *b, float *y)
void resample_trans_index_rep(std::size_t M, std::size_t N, const IntType1 *src_idx, IntType2 *replication)
Transform parent indices into replication numbers.
std::size_t resample_trans_residual(std::size_t M, std::size_t N, const double *weight, double *resid, IntType *integ)
Transform normalized weights to normalized residual and integrals, and return the number of remaining...
void resample_trans_rep_index(std::size_t M, std::size_t N, const IntType1 *replication, IntType2 *src_idx)
Transform replication numbers into parent indices.
void resample_trans_u01_index(std::size_t M, std::size_t N, const double *weight, U01SeqType &&u01seq, IntType *src_idx)
Transform uniform [0, 1] sequence into parent indices.