RobinHankin / permutations

https://robinhankin.github.io/permutations/
5 stars 3 forks source link

generalize `allcyc()` for different sets #42

Closed RobinHankin closed 1 year ago

RobinHankin commented 1 year ago

Function allcyc() generates all cycles of the set $\left\lbrace 1,2,\ldots,n\right\rbrace$. But it would be nice to generalize this to give all cycles of any set. Look:

 allcyc(5)
 [1] (12345) (12354) (12453) (12435) (12534) (12543) (13452) (13542) (13245)
[10] (13524) (13425) (13254) (14523) (14235) (14532) (14352) (14253) (14325)
[19] (15234) (15423) (15324) (15243) (15342) (15432)
[coerced from word form]
> 
 cycle(lapply(as.cycle(allcyc(5)),function(x){list(c(6,22,55,33,7)[x[[1]]])}))
 [1] (6,22,55,33,7) (6,22,55,7,33) (6,22,33,7,55) (6,22,33,55,7) (6,22,7,55,33)
 [6] (6,22,7,33,55) (6,55,33,7,22) (6,55,7,33,22) (6,55,22,33,7) (6,55,7,22,33)
[11] (6,55,33,22,7) (6,55,22,7,33) (6,33,7,22,55) (6,33,22,55,7) (6,33,7,55,22)
[16] (6,33,55,7,22) (6,33,22,7,55) (6,33,55,22,7) (6,7,22,55,33) (6,7,33,22,55)
[21] (6,7,55,22,33) (6,7,22,33,55) (6,7,55,33,22) (6,7,33,55,22)
> 
RobinHankin commented 1 year ago

Spent a bit of time trying to leverage the partitions package and came up with this:

do.call("c",apply(matrix(c(6,22,55,33,7)[perms(5)],nrow=5),2,as.cycle))
  [1] (6,22,55,33,7) (6,22,55,7,33) (6,22,33,55,7) (6,22,33,7,55) (6,22,7,55,33)
  [6] (6,22,7,33,55) (6,55,22,33,7) (6,55,22,7,33) (6,55,33,22,7) (6,55,33,7,22)
 [11] (6,55,7,22,33) (6,55,7,33,22) (6,33,22,55,7) (6,33,22,7,55) (6,33,55,22,7)
[snip]

(this is not desired functionality because it includes permutations that are not a single cycle). The partitions package does not have a way to generate all cyclic permutations of $\lbrace 1,2,\ldots,n\rbrace$. I will raise an issue in the partitions repo for this.

RobinHankin commented 1 year ago

repopening, df16a5b should have closed issue #44, not issue #42