RobinHankin / partitions

R package for integer partitions
9 stars 5 forks source link

application in the permutations package #38

Open RobinHankin opened 1 year ago

RobinHankin commented 1 year ago

To generate all permutations of a particular shape, here a 1-cycle [null], a 2-cycle, and a 3-cycle:

library("partitions")
library("permutations")
#> 
#> Attaching package: 'permutations'
#> The following object is masked from 'package:stats':
#> 
#>     cycle
o <- 1:3
M <- multinomial(o)
a <- cumsum(o)
cycle(sapply(seq_len(ncol(M)),function(i){lapply(apply(rbind(c(0,1+a[-length(a)]),a),2,function(x){x[1]:x[2]}),function(x){M[,i][x]})},simplify=F))
#>  [1] (23)(456) (24)(356) (25)(346) (26)(345) (256)(34) (246)(35) (245)(36)
#>  [8] (236)(45) (235)(46) (234)(56) (13)(456) (14)(356) (15)(346) (16)(345)
#> [15] (12)(456) (12)(356) (12)(346) (12)(345) (14)(256) (15)(246) (16)(245)
#> [22] (13)(256) (13)(246) (13)(245) (15)(236) (16)(235) (14)(236) (14)(235)
#> [29] (16)(234) (15)(234) (156)(34) (146)(35) (145)(36) (136)(45) (135)(46)
#> [36] (134)(56) (156)(24) (146)(25) (145)(26) (156)(23) (146)(23) (145)(23)
#> [43] (136)(25) (135)(26) (136)(24) (135)(24) (134)(26) (134)(25) (126)(45)
#> [50] (125)(46) (124)(56) (126)(35) (125)(36) (126)(34) (125)(34) (124)(36)
#> [57] (124)(35) (123)(56) (123)(46) (123)(45)

Created on 2023-07-15 with reprex v2.0.2

RobinHankin commented 1 year ago

Above code is not right: it includes (23)(456) but not (23)(465) but I can "rescue" it:

cycle(sapply(seq_len(ncol(M)),function(i){lapply(apply(rbind(c(0,1+a[-length(a)]),a),2,function(x){x[1]:x[2]}),function(x){M[,i][x]})},simplify=F)) -> jj
c(jj,jj^5)
  [1] (23)(456) (24)(356) (25)(346) (26)(345) (256)(34) (246)(35) (245)(36) (236)(45) (235)(46) (234)(56) (13)(456) (14)(356) (15)(346)
 [14] (16)(345) (12)(456) (12)(356) (12)(346) (12)(345) (14)(256) (15)(246) (16)(245) (13)(256) (13)(246) (13)(245) (15)(236) (16)(235)
 [27] (14)(236) (14)(235) (16)(234) (15)(234) (156)(34) (146)(35) (145)(36) (136)(45) (135)(46) (134)(56) (156)(24) (146)(25) (145)(26)
 [40] (156)(23) (146)(23) (145)(23) (136)(25) (135)(26) (136)(24) (135)(24) (134)(26) (134)(25) (126)(45) (125)(46) (124)(56) (126)(35)
 [53] (125)(36) (126)(34) (125)(34) (124)(36) (124)(35) (123)(56) (123)(46) (123)(45) (23)(465) (24)(365) (25)(364) (26)(354) (265)(34)
 [66] (264)(35) (254)(36) (263)(45) (253)(46) (243)(56) (13)(465) (14)(365) (15)(364) (16)(354) (12)(465) (12)(365) (12)(364) (12)(354)
 [79] (14)(265) (15)(264) (16)(254) (13)(265) (13)(264) (13)(254) (15)(263) (16)(253) (14)(263) (14)(253) (16)(243) (15)(243) (165)(34)
 [92] (164)(35) (154)(36) (163)(45) (153)(46) (143)(56) (165)(24) (164)(25) (154)(26) (165)(23) (164)(23) (154)(23) (163)(25) (153)(26)
[105] (163)(24) (153)(24) (143)(26) (143)(25) (162)(45) (152)(46) (142)(56) (162)(35) (152)(36) (162)(34) (152)(34) (142)(36) (142)(35)
[118] (132)(56) (132)(46) (132)(45)
RobinHankin commented 1 year ago

comment: there is a bug in the above code; it does not work for c(1,2,2). To fix it we need to include a simplify=FALSE argument to the apply():

library("partitions")
library("permutations")
#> 
#> Attaching package: 'permutations'
#> The following object is masked from 'package:stats':
#> 
#>     cycle
allpermshape <- function(o){
   M <- multinomial(o)
   a <- cumsum(o)
   b <- rbind(c(0,1+a[-length(a)]),a)
   seqf <- function(x){seq(from=x[1],to=x[2])}
   f <- function(i){lapply(apply(b,2,seqf,simplify=FALSE),function(x){M[,i][x]})}
   cycle(sapply(seq_len(ncol(M)),f,simplify=FALSE))
}

allpermshape(c(1,2,2))
#>  [1] (23)(45) (24)(35) (25)(34) (25)(34) (24)(35) (23)(45) (13)(45) (14)(35)
#>  [9] (15)(34) (12)(45) (12)(35) (12)(34) (14)(25) (15)(24) (13)(25) (13)(24)
#> [17] (15)(23) (14)(23) (15)(34) (14)(35) (13)(45) (15)(24) (14)(25) (15)(23)
#> [25] (14)(23) (13)(25) (13)(24) (12)(45) (12)(35) (12)(34)
allpermshape(c(1,2,3))
#>  [1] (23)(456) (24)(356) (25)(346) (26)(345) (256)(34) (246)(35) (245)(36)
#>  [8] (236)(45) (235)(46) (234)(56) (13)(456) (14)(356) (15)(346) (16)(345)
#> [15] (12)(456) (12)(356) (12)(346) (12)(345) (14)(256) (15)(246) (16)(245)
#> [22] (13)(256) (13)(246) (13)(245) (15)(236) (16)(235) (14)(236) (14)(235)
#> [29] (16)(234) (15)(234) (156)(34) (146)(35) (145)(36) (136)(45) (135)(46)
#> [36] (134)(56) (156)(24) (146)(25) (145)(26) (156)(23) (146)(23) (145)(23)
#> [43] (136)(25) (135)(26) (136)(24) (135)(24) (134)(26) (134)(25) (126)(45)
#> [50] (125)(46) (124)(56) (126)(35) (125)(36) (126)(34) (125)(34) (124)(36)
#> [57] (124)(35) (123)(56) (123)(46) (123)(45)

Created on 2023-07-15 with reprex v2.0.2

NB: above in allpermshape(c(1,2,3)) we still have (23)(456) but not (23)(465). I don't think the "rescue" is easily generalized.

RobinHankin commented 1 year ago

the following construction might be useful:

library("partitions")
library("permutations")
#> 
#> Attaching package: 'permutations'
#> The following object is masked from 'package:stats':
#> 
#>     cycle
o <- as.cycle("(123)(45)(6789)")
lapply(unclass(o)[[1]],allcyc)
#> [[1]]
#> [1] (123) (132)
#> 
#> [[2]]
#> [1] (45)
#> 
#> [[3]]
#> [1] (6789) (6798) (6897) (6879) (6978) (6987)

Created on 2023-07-15 with reprex v2.0.2