In my previous answer there were two short events:
- He calculated only permutations
1:r. - The number of permutations was wrong, as I used
n! / r!instead n! / (n - r)!.
n = 15 r = 8 8 8 . : , .
, - combn(). std::next_permutation ++:
src1 <- '
IntegerMatrix permute_combinations(const IntegerMatrix& combs) {
size_t numComb(combs.cols());
size_t r(combs.rows());
size_t numPermPerComb(1);
for(size_t i = 1; i <= r; ++i) numPermPerComb *= i;
size_t numPerm = numComb * numPermPerComb;
IntegerMatrix perms(numPerm, r);
for(size_t i = 0; i < numComb; ++i) {
IntegerVector v = combs(_, i);
for (size_t j = 0; j < numPermPerComb; ++j) {
perms(i * numPermPerComb + j, _) = v;
std::next_permutation(v.begin(), v.end());
}
}
return perms;
}
'
Rcpp::cppFunction(src1)
system.time(perms <- permute_combinations(combn(15, 8)))
dim(perms)
object.size(perms)
head(perms)
tail(perms)
1 , gtools . ++ Rcpp:
src <- 'IntegerMatrix permutations(int n, int r) {
size_t numPerm(1);
for(int i = n; i > r; --i) {
numPerm *= i;
}
IntegerMatrix result(numPerm, r);
IntegerVector v(r);
std::iota (std::begin(v), std::end(v), 1);
for (size_t i = 0; i < numPerm; ++i) {
result(i, _) = v;
std::next_permutation(v.begin(), v.end());
}
return result;
}'
Rcpp::cppFunction(src)
system.time(perms <- permutations(15, 8))
dim(perms)
object.size(perms)
head(perms)