24#ifndef RCPPARMADILLO__EXTENSIONS__SAMPLE_H
26#ifndef Rcpp__sugar__sample_h
27#define Rcpp__sugar__sample_h
64 stop(
"Probabilities must be finite and non-negative!");
71 stop(
"Too few positive probabilities!");
74 for (i = 0; i < n; i++) {
85 int i = 0, j = 0,
nm1 = n - 1;
95 for (i = 1; i < n; i++) {
99 for (i = 0; i < k; i++) {
101 for (j = 0; j <
nm1; j++) {
122 int i = 0, j = 0,
nm1 = n - 1;
124 for ( ; i < n; i++) {
130 for (i = 1; i < n; i++) {
134 for (i = 0; i < k; i++) {
136 for (j = 0; j <
nm1; j++) {
153 std::vector<double>
q(n);
156 std::vector<int>
HL(n);
157 std::vector<int>::iterator
H,
L;
161 H =
HL.begin() - 1;
L =
HL.begin() + n;
162 for (i = 0; i < n; i++) {
171 if (
H >=
HL.begin() &&
L <
HL.begin() + n) {
172 for (k = 0; k < n - 1; k++) {
180 if (
L >=
HL.begin() + n) {
186 for (i = 0; i < n; i++) {
190 for (i = 0; i <
nans; i++) {
191 rU = unif_rand() * n;
192 k =
static_cast<int>(
rU);
209 std::vector<double>
q(n);
212 std::vector<int>
HL(n);
213 std::vector<int>::iterator
H,
L;
215 H =
HL.begin() - 1;
L =
HL.begin() + n;
216 for (i = 0; i < n; i++) {
225 if (
H >=
HL.begin() &&
L <
HL.begin() + n) {
226 for (k = 0; k < n - 1; k++) {
234 if (
L >=
HL.begin() + n) {
240 for (i = 0; i < n; i++) {
244 for (i = 0; i <
nans; i++) {
245 rU = unif_rand() * n;
246 k =
static_cast<int>(
rU);
247 ans[i] = (
rU <
q[k]) ? ref[k] : ref[a[k]];
263 for (i = 0; i < n; i++) {
270 for (i = 0, n1 = n - 1; i <
nans; i++, n1--) {
274 for (j = 0; j < n1; j++) {
284 for (k = j; k < n1; k++) {
305 for (i = 0; i < n; i++) {
312 for (i = 0, n1 = n - 1; i <
nans; i++, n1--) {
316 for (j = 0; j < n1; j++) {
326 for (k = j; k < n1; k++) {
344 if (replace || size < 2) {
346 *
ians =
static_cast<int>(n * unif_rand() +
adj);
352 for (
int i = 0; i < n; i++) {
357 int j =
static_cast<int>(n * unif_rand());
374 if (replace || size < 2) {
376 *
ians = ref[
static_cast<int>(n * unif_rand())];
382 for (
int i = 0; i < n; i++) {
387 int j =
static_cast<int>(n * unif_rand());
403 if (
probs.isNotNull()) {
405 if (
static_cast<int>(p.
size()) != n) {
406 stop(
"probs.size() != n!");
413 for ( ; i < n; i++) {
414 nc += (n * p[i] > 0.1);
422 stop(
"Sample size must be <= n when not using replacement!");
428 if (!replace && size > n) {
429 stop(
"Sample size must be <= n when not using replacement!");
441 if (
probs.isNotNull()) {
443 if (
static_cast<int>(p.
size()) != n) {
444 stop(
"probs.size() != n!");
451 for ( ; i < n; i++) {
452 nc += (n * p[i] > 0.1);
460 stop(
"Sample size must be <= n when not using replacement!");
466 if (!replace && size > n) {
467 stop(
"Sample size must be <= n when not using replacement!");
traits::r_vector_iterator< RTYPE, StoragePolicy >::type iterator
void Normalize(Vector< REALSXP > &p, int require_k, bool replace)
Vector< INTSXP > SampleNoReplace(Vector< REALSXP > &p, int n, int nans, bool one_based)
Nullable< Vector< REALSXP > > probs_t
Vector< INTSXP > SampleReplace(Vector< REALSXP > &p, int n, int k, bool one_based)
Vector< INTSXP > EmpiricalSample(int n, int size, bool replace, bool one_based)
Vector< INTSXP > WalkerSample(const Vector< REALSXP > &p, int n, int nans, bool one_based)
void NORET stop(const std::string &message)
no_init_vector no_init(R_xlen_t size)
Vector< INTSXP > sample(int n, int size, bool replace=false, sugar::probs_t probs=R_NilValue, bool one_based=true)
sugar::Sum< INTSXP, NA, T > sum(const VectorBase< INTSXP, NA, T > &t)