I think this is what the OP is looking for:
out <- list(c("(1,5)","(3)","(4)","(6)"), c("(3,6)","(1)","(4)","(5)"),
c("(3,4)","(1)","(5)","(6)"),c("(3,5)","(1)","(4)","(6)"),
c("(4,6)","(1)","(3)","(5)"), c("(4,5)","(1)","(3)","(6)"),
c("(5,6)","(1)","(3)","(4)"))
library(RcppAlgos)
myPerms <- lapply(out, function(x) {
unlist(permuteGeneral(x, length(x), FUN = function(y) {
paste0(c("{", y, "}"), collapse = "")
}))
})
We use permuteGeneral
from RcppAlgos
(I am the author) as we can utilize the FUN
argument to pass a custom function that will be applied to each permutation (i.e. paste0(c("{", y, "}"), collapse = "")
).
Here is the output of the 5th element:
myPerms[[5]]
[1] "{(4,6)(1)(3)(5)}" "{(4,6)(1)(5)(3)}" "{(4,6)(3)(1)(5)}"
[4] "{(4,6)(3)(5)(1)}" "{(4,6)(5)(1)(3)}" "{(4,6)(5)(3)(1)}"
[7] "{(1)(4,6)(3)(5)}" "{(1)(4,6)(5)(3)}" "{(1)(3)(4,6)(5)}"
[10] "{(1)(3)(5)(4,6)}" "{(1)(5)(4,6)(3)}" "{(1)(5)(3)(4,6)}"
[13] "{(3)(4,6)(1)(5)}" "{(3)(4,6)(5)(1)}" "{(3)(1)(4,6)(5)}"
[16] "{(3)(1)(5)(4,6)}" "{(3)(5)(4,6)(1)}" "{(3)(5)(1)(4,6)}"
[19] "{(5)(4,6)(1)(3)}" "{(5)(4,6)(3)(1)}" "{(5)(1)(4,6)(3)}"
[22] "{(5)(1)(3)(4,6)}" "{(5)(3)(4,6)(1)}" "{(5)(3)(1)(4,6)}"
If you really want permutations with repetition, simply set repetition = TRUE
in permuteGeneral
. Of course, if you want a more useable output, we can drop the custom FUN
altogether.
UPDATE
After learning more about how the OP obtained out
above, we can better attack the problem. First we find out that the OP uses listParts
from the library partitions
. Looking at the source code we have:
listParts
function (x)
{
f <- function(pp) {
out <- split(seq_along(pp), pp)
class(out) <- c(class(out), "equivalence")
out
}
apply(setparts(x), 2, f)
}
<bytecode: 0x10d7b09f8>
<environment: namespace:partitions>
We can alter this to obtain all permutations:
permListParts <- function (x)
{
f <- function(pp) {
out <- split(seq_along(pp), pp)
myPerms <- perms(length(out))
apply(myPerms, 2, function(x) {
temp <- out[x]
class(temp) <- c(class(temp), "equivalence")
temp
})
}
apply(setparts(x), 2, f)
}
We note that we are going against the intent of listParts
... quoting the documentation:
"Note that (12)(3)(4) is the same partition as, for example, (3)(4)(21) as the equivalence relation is the same."
Oh well... here is the output for permutations of length 3:
permListParts(3)
[[1]]
[[1]][[1]]
[1] (1,2,3)
[[2]]
[[2]][[1]]
[1] (1,3)(2)
[[2]][[2]]
[1] (2)(1,3)
[[3]]
[[3]][[1]]
[1] (1,2)(3)
[[3]][[2]]
[1] (3)(1,2)
[[4]]
[[4]][[1]]
[1] (2,3)(1)
[[4]][[2]]
[1] (1)(2,3)
[[5]]
[[5]][[1]]
[1] (1)(2)(3)
[[5]][[2]]
[1] (1)(3)(2)
[[5]][[3]]
[1] (2)(1)(3)
[[5]][[4]]
[1] (2)(3)(1)
[[5]][[5]]
[1] (3)(1)(2)
[[5]][[6]]
[1] (3)(2)(1)