RobinHankin / permutations

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

outer() not implemented #44

Closed RobinHankin closed 1 year ago

RobinHankin commented 1 year ago

Given two vectors x and y of permutations, it would be nice to use outer() to multiply each element of x with each element of y. Function cayley() has some of this functionality but is rather obscurely written: the intent is to produce nice tables of (closed) subgroups of the symmetric group $S_n$, rather than a general outer product calculation.

 set.seed(0)
 (x <- rperm(3))
[1] (162)(34)(57) (17546)(23)   (15)(364)    
[coerced from word form]
 (y <- rperm(5))
[1] (15742)(36) (12653)     (1326)(45)  (1463275)   (24375)    
[coerced from word form]
 do.call("c",apply(expand.grid(seq_along(x),seq_along(y)),1,function(v){x[v[1]] * y[v[2]]},simplify=FALSE))
 [1] (132546)    (143)(265)  (17462)     (15734)     (17362)(45) (135264)    (23574)     (174)(36)   (142653)    (1367)(24)  (156437)   
[12] (2754)      (16472)     (15346)(27) (12475)(36)
[coerced from word form]
 x[1] * y[1]
[1] (132546)
[coerced from word form]
RobinHankin commented 1 year ago

It is possible to replace the binary operator * with + if you are sure that the cycles of x and y are distinct:

 set.seed(0)
 x <- rperm(5,r=4)
 y <- capply(rperm(3,r=4),function(x){x+100})
 do.call("c",apply(expand.grid(seq_along(x),seq_along(y)),1,function(v){x[v[1]] + y[v[2]]},simplify=FALSE))
 [1] (1,2)(3,4)(101,103,104,102) (3,4)(101,103,104,102)      (1,3,4)(101,103,104,102)    (1,3,4,2)(101,103,104,102)  (1,2,4)(101,103,104,102)   
 [6] (1,2)(3,4)(102,104)         (3,4)(102,104)              (1,3,4)(102,104)            (1,3,4,2)(102,104)          (1,2,4)(102,104)           
[11] (1,2)(3,4)(102,104,103)     (3,4)(102,104,103)          (1,3,4)(102,104,103)        (1,3,4,2)(102,104,103)      (1,2,4)(102,104,103)       

Or even

 do.call("c",apply(expand.grid(seq_along(x),seq_along(y)),1,function(v){x[v[1]] %~% y[v[2]]},simplify=FALSE))
 [1] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE

(although in that case it might be better to return a matrix).

RobinHankin commented 1 year ago

fixed in df16a5b