jwood000/RcppAlgos

wrong nth permutation

randy3k opened this issue · 7 comments

RcppAlgos::permuteSample(100, 100, sampleVec=2)

It returns 1:100 instead of the second permutation. I assume it is due to round off error.

Hey @randy3k ,

You are correct. I thought I put a note in the documentation regarding the accuracy, but I forgot. I only mention this limitation in the Count functions (which the Sample functions call)

Accurate up to \eqn{2^{53} - 1}{2^53 - 1}.
I will update the documentation to include this limitation.

Also, extending this to larger cases has been on my todo list for a good bit now (among many other things) by making use of the gmp library. I have been working on making this implementation much simpler than what is currently implemented in the bigIntegerAlgos library, which makes use of a configure script and is a bit messy.

Anywho, I really do appreciate your input.

Joseph

I have given this a shot - arrangements now also generates nth results using gmp.

r$> library(arrangements)

r$> permutations(20, index = "314159265358979311")
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    3   13    2    7    5   17    1    6   19     4    16     8    11     9
     [,15] [,16] [,17] [,18] [,19] [,20]
[1,]    14    12    15    10    18    20

r$> combinations(50, 10, nsample = 2)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    8   10   12   22   38   42   43   46   47    49
[2,]    2    7   14   19   21   29   32   37   44    48

PS: It was so much fun to work with the nth results.

Hey @randy3k ,

Sorry for the delay, I've been incredibly busy.

Your work looks great. I'm nearly done implementing support for gmp, so I should be able to close this soon. All I have had to do thus far is simply translate the base algorithms into their gmp counterparts (mainly converting primitive types and operations into mpz_t's). It hasn't been that difficult as I have nearly memorized the gmp documentation from my work with bigIntegerAlgos.

I'm actually in the process of documenting these algorithms with a decent amount of rigor. Could you reference these once they are done?

I ask because I am fairly certain there isn't any official documentation readily available for these algorithms (especially the multiset and with-repetition portions) and it took me quite a while (over a month) to develop these from scratch.

I would greatly appreciate it and thanks again,
Joseph

Issue is now resolved as of version 2.1.0

RcppAlgos::permuteSample(100, 100, sampleVec=2)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17]
[1,]    1    2    3    4    5    6    7    8    9    10    11    12    13    14    15    16    17
     [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[1,]    18    19    20    21    22    23    24    25    26    27    28    29    30    31    32    33
     [,34] [,35] [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49]
[1,]    34    35    36    37    38    39    40    41    42    43    44    45    46    47    48    49
     [,50] [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62] [,63] [,64] [,65]
[1,]    50    51    52    53    54    55    56    57    58    59    60    61    62    63    64    65
     [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73] [,74] [,75] [,76] [,77] [,78] [,79] [,80] [,81]
[1,]    66    67    68    69    70    71    72    73    74    75    76    77    78    79    80    81
     [,82] [,83] [,84] [,85] [,86] [,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97]
[1,]    82    83    84    85    86    87    88    89    90    91    92    93    94    95    96    97
     [,98] [,99] [,100]
[1,]    98   100     99

Awesome. Just out of curiosity, why don’t you link with libgmpxx? I guess it would vastly simplify your code in c++.

To be honest @randy3k, it is a result of habit and comfort (a nice way of saying I was lazy). As I said earlier, I have nearly memorized the gmp manual from my work with bigIntegerAlgos. That statement should be qualified to be "I have nearly memorized the first 11 chapters of the gmp manual" (Chapter 12 is the chapter on the C++ interface).

But you are right, gmpxx is probably the way to go here.

Fair, I thought that you only need to write an Rcpp wrapper for the mpz_class. That will be the true laziness.

By the way, the new parallel feature looks awesome. (PS: don’t worry, I have no intend to replicate it, especially, it is difficult to do it with the C api)