This function will return all permutations of a multiset

allPerm(mcObj, nThreads = 1)

Arguments

mcObj

an object of class mc - usually generated by initMC

nThreads

the number of threads to use. Using more than one only helps for large results (at least 100,000 permutations; smaller results are always generated with one thread), and only for integer, double and logical multisets, and multisets of other types that are mapped from integer codes, such as Dates. Character and factor multisets always use one thread. The gain is limited by memory bandwidth, so it is usually well short of nThreads times faster.

Value

A matrix with each row being a different permutation of the multiset

Details

This function will return all permutations of a multiset. It makes no check to see if this is a sensible thing to do. Users are advised to check how many permutations are possible using the multinom function in this package.

Note

This function does not warn the user that the requested set of permutations may be very large. In addition, all working is handled entirely in memory, and so this may cause it to crash if the request is execeptionally large. nextPerm can step through the permutations in batches instead, without holding them all in memory.

See also

Author

James M. Curran

Examples


## a small numeric example with 6 permuations
x = c(1,1,2,2)
m = initMC(x)
allPerm(m)
#>      [,1] [,2] [,3] [,4]
#> [1,]    2    2    1    1
#> [2,]    1    2    2    1
#> [3,]    2    1    2    1
#> [4,]    1    2    1    2
#> [5,]    1    1    2    2
#> [6,]    2    1    1    2

## a large character example - 60 possibilities
x = rep(letters[1:3], 3:1)
multinom(x) ## calculate the number of permutations
#> [1] 60
m = initMC(x)
allPerm(m)
#>       [,1] [,2] [,3] [,4] [,5] [,6]
#>  [1,] "c"  "b"  "b"  "a"  "a"  "a" 
#>  [2,] "a"  "c"  "b"  "b"  "a"  "a" 
#>  [3,] "c"  "a"  "b"  "b"  "a"  "a" 
#>  [4,] "b"  "c"  "a"  "b"  "a"  "a" 
#>  [5,] "a"  "b"  "c"  "b"  "a"  "a" 
#>  [6,] "b"  "a"  "c"  "b"  "a"  "a" 
#>  [7,] "c"  "b"  "a"  "b"  "a"  "a" 
#>  [8,] "a"  "c"  "b"  "a"  "b"  "a" 
#>  [9,] "c"  "a"  "b"  "a"  "b"  "a" 
#> [10,] "a"  "c"  "a"  "b"  "b"  "a" 
#> [11,] "a"  "a"  "c"  "b"  "b"  "a" 
#> [12,] "c"  "a"  "a"  "b"  "b"  "a" 
#> [13,] "b"  "c"  "a"  "a"  "b"  "a" 
#> [14,] "a"  "b"  "c"  "a"  "b"  "a" 
#> [15,] "b"  "a"  "c"  "a"  "b"  "a" 
#> [16,] "a"  "b"  "a"  "c"  "b"  "a" 
#> [17,] "a"  "a"  "b"  "c"  "b"  "a" 
#> [18,] "b"  "a"  "a"  "c"  "b"  "a" 
#> [19,] "c"  "b"  "a"  "a"  "b"  "a" 
#> [20,] "a"  "c"  "b"  "a"  "a"  "b" 
#> [21,] "c"  "a"  "b"  "a"  "a"  "b" 
#> [22,] "a"  "c"  "a"  "b"  "a"  "b" 
#> [23,] "a"  "a"  "c"  "b"  "a"  "b" 
#> [24,] "c"  "a"  "a"  "b"  "a"  "b" 
#> [25,] "a"  "c"  "a"  "a"  "b"  "b" 
#> [26,] "a"  "a"  "c"  "a"  "b"  "b" 
#> [27,] "a"  "a"  "a"  "c"  "b"  "b" 
#> [28,] "c"  "a"  "a"  "a"  "b"  "b" 
#> [29,] "b"  "c"  "a"  "a"  "a"  "b" 
#> [30,] "a"  "b"  "c"  "a"  "a"  "b" 
#> [31,] "b"  "a"  "c"  "a"  "a"  "b" 
#> [32,] "a"  "b"  "a"  "c"  "a"  "b" 
#> [33,] "a"  "a"  "b"  "c"  "a"  "b" 
#> [34,] "b"  "a"  "a"  "c"  "a"  "b" 
#> [35,] "a"  "b"  "a"  "a"  "c"  "b" 
#> [36,] "a"  "a"  "b"  "a"  "c"  "b" 
#> [37,] "a"  "a"  "a"  "b"  "c"  "b" 
#> [38,] "b"  "a"  "a"  "a"  "c"  "b" 
#> [39,] "c"  "b"  "a"  "a"  "a"  "b" 
#> [40,] "b"  "c"  "b"  "a"  "a"  "a" 
#> [41,] "b"  "b"  "c"  "a"  "a"  "a" 
#> [42,] "a"  "b"  "b"  "c"  "a"  "a" 
#> [43,] "b"  "a"  "b"  "c"  "a"  "a" 
#> [44,] "b"  "b"  "a"  "c"  "a"  "a" 
#> [45,] "a"  "b"  "b"  "a"  "c"  "a" 
#> [46,] "b"  "a"  "b"  "a"  "c"  "a" 
#> [47,] "a"  "b"  "a"  "b"  "c"  "a" 
#> [48,] "a"  "a"  "b"  "b"  "c"  "a" 
#> [49,] "b"  "a"  "a"  "b"  "c"  "a" 
#> [50,] "b"  "b"  "a"  "a"  "c"  "a" 
#> [51,] "a"  "b"  "b"  "a"  "a"  "c" 
#> [52,] "b"  "a"  "b"  "a"  "a"  "c" 
#> [53,] "a"  "b"  "a"  "b"  "a"  "c" 
#> [54,] "a"  "a"  "b"  "b"  "a"  "c" 
#> [55,] "b"  "a"  "a"  "b"  "a"  "c" 
#> [56,] "a"  "b"  "a"  "a"  "b"  "c" 
#> [57,] "a"  "a"  "b"  "a"  "b"  "c" 
#> [58,] "a"  "a"  "a"  "b"  "b"  "c" 
#> [59,] "b"  "a"  "a"  "a"  "b"  "c" 
#> [60,] "b"  "b"  "a"  "a"  "a"  "c" 

## 362,880 permutations, using two threads
m = initMC(1:9)
p = allPerm(m, nThreads = 2)
dim(p)
#> [1] 362880      9