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 *index)
82 std::memset(index, 0,
sizeof(IntType) * N);
88 for (std::size_t i = 0; i != M - 1; ++i) {
90 while (j != N && u01seq[j] <= accw)
91 index[j++] =
static_cast<IntType
>(i);
94 index[j++] =
static_cast<IntType
>(M - 1);
99 template <
typename IntType1,
typename IntType2>
101 std::size_t M, std::size_t N,
const IntType1 *replication, IntType2 *index)
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 index[dst++] = static_cast<IntType2>(src);
118 for (std::size_t dst = 0; dst != N; ++dst) {
119 if (replication[dst] != 0) {
120 index[dst] =
static_cast<IntType2
>(dst);
123 if (replication[src] < time + 2) {
128 while (replication[src] < 2);
130 index[dst] =
static_cast<IntType2
>(src);
138 template <
typename IntType1,
typename IntType2>
140 std::size_t M, std::size_t N,
const IntType1 *index, 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[index[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_rep_index(std::size_t M, std::size_t N, const IntType1 *replication, IntType2 *index)
Transform replication numbers into parent indices.
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_u01_index(std::size_t M, std::size_t N, const double *weight, U01SeqType &&u01seq, IntType *index)
Transform uniform [0, 1] sequence into parent indices.
void resample_trans_index_rep(std::size_t M, std::size_t N, const IntType1 *index, IntType2 *replication)
Transform parent indices into replication numbers.