Author: Robin K. S. Hankin

# Print methods in the permutations package

#### Robin K. S. Hankin

To cite the permutations package in publications, please use Hankin (2020). The `permutations` package has a print method which includes a number of user-configurable options which are illustrated and motivated here.

Permutations have two natural representations: word form and cycle form. Internally, the package coerces a permutation from one form to another depending on what operations one does with it: group-theoretic products and inverses are carried out more easily in word form, while powers are more easily done using cycle form.

Partly as a result of a perceptive comment from a journal reviewer, the package coerces to cycle form for printing as this is generally more comprehensible than word form. However, the package print method is extensively customizable. This document covers some of the options. Low-level print functionality includes `print_word()` and `print_cycle()` which can be used explicitly to print in either word form or cycle form as desired:

    set.seed(0)
    x <- rperm(r = 9)
    print_word(x)
    #>      a b c d e f g h i
    #> [1]  i d g a b . c . e
    #> [2]  b c h a . . i g d
    #> [3]  g a i e f h d b c
    #> [4]  e . h f a d c i g
    #> [5]  c f b g d e h i a
    #> [6]  g f a . i c b . e
    #> [7]  c . f g h a e i d
    #> [8]  f a . g d b i . e
    #> [9]  d e a g i c f . b
    #> [10] f e d g a h i c b
    print_cycle(x)
    #>  [1] (aiebd)(cg)    (abchgid)      (agdefhb)(ci)  (ae)(chig)(df) (acbfedghi)
    #>  [6] (agbfc)(ei)    (acf)(dgehi)   (afb)(dgie)    (adgfc)(bei)   (afhcdgibe)

It is a matter of taste which one is preferable. For the example above, the majority of the symbols are moved: elements that map to themselves are shown with a dot in the word form, and these are in a minority. However, the difference between word form and cycle form becomes more pronounced if only a small number of elements move:

    x <- rperm(r = 9, moved = 3)
    print_word(x)
    #>      a b c d e f g h i
    #> [1]  g . . . . . i . a
    #> [2]  . i b . . . . . c
    #> [3]  . . . . . . . . .
    #> [4]  c . a . . . . . .
    #> [5]  . h . . b . . e .
    #> [6]  . . . f . d . . .
    #> [7]  . . . . . . . . .
    #> [8]  . . . h . . . d .
    #> [9]  f . a . . c . . .
    #> [10] . . . i . . . . d
    print_cycle(x)
    #>  [1] (agi) (bic) ()    (ac)  (bhe) (df)  ()    (dh)  (afc) (di)

Above we see the cycle form is arguably more compact.

### One-line notation for permutations

Sometimes it is desirable to present permutations in “one-line notation”, which is just the ordered list of the images of \(1,2,\ldots,n\); this is just a compact form of the two-line notation:

\[ \begin{pmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\\ 9 & 2 & 6 & 3 & 5 & 4 & 1 & 7 & 8 \end{pmatrix} \]

would just be (926354178). To do this, use `print_word(x, use_dot=TRUE)`:

    print_word(x, use_dot=FALSE)
    #>      a b c d e f g h i
    #> [1]  g b c d e f i h a
    #> [2]  a i b d e f g h c
    #> [3]  a b c d e f g h i
    #> [4]  c b a d e f g h i
    #> [5]  a h c d b f g e i
    #> [6]  a b c f e d g h i
    #> [7]  a b c d e f g h i
    #> [8]  a b c h e f g d i
    #> [9]  f b a d e c g h i
    #> [10] a b c i e f g h d

## Print word as cycle

In use, sometimes we work with objects in word form and sometimes objects in cycle form. However, the standard workflow would be to use R’s default system, which is to print values to the console:

    x <- rperm(n = 2)
    y <- as.cycle(x)
    unclass(x) # shows internal representation (x is in word form)
    #>      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
    #> [1,]    2    6    1    7    3    5    4
    #> [2,]    7    4    1    6    3    5    2
    unclass(y) # shows internal representation (x is in cycle form)
    #> [[1]]
    #> [[1]][[1]]
    #> [1] 1 2 6 5 3
    #>
    #> [[1]][[2]]
    #> [1] 4 7
    #>
    #>
    #> [[2]]
    #> [[2]][[1]]
    #> [1] 1 7 2 4 6 5 3
    x # default: print to console
    #> [1] (abfec)(dg) (agbdfec)
    #> [coerced from word form]
    y # default: print to console
    #> [1] (abfec)(dg) (agbdfec)

Above we see that, by default, both `x` and `y` are printed in cycle form, even though `x` is stored internally in word form. See how the print method tells us if its argument object is printed in coerced-to-cycle form. However, it is possible to suppress this coercion by setting option `print_word_as_cycle`:

    options(print_word_as_cycle = FALSE)
    x
    #>     a b c d e f g
    #> [1] b f a g c e d
    #> [2] g d a f c e b
    y
    #> [1] (abfec)(dg) (agbdfec)

We reinstate the default:

    options(print_word_as_cycle = TRUE) # restore default

Most people seem to prefer printing in cycle form.

### Commas

Above, we saw the print method for permutations of \(\left[9\right]\). If we have 10 or more elements then we need a comma. This is added by default:

    x <- rperm(r = 15, moved = 4)
    print_cycle(x)
    #>  [1] (a,d,b,c)  (j,m,o)    (c,n)      (i,j,l)    (e,o,g,i)  (f,n)(l,o)
    #>  [7] (f,h,i)    (h,l,k)    (a,d,e)    (e,f,h)

Above, the comma has been added to aid readability. However, it is possible to override this behaviour by setting option `comma`. The default value of `NULL` means to add commas iff \(r>9\) but Boolean values are respected:

    options("comma" = TRUE)
    rperm(3, r = 9) # commas printed irregardless
    #> [1] (a,b,h,c,f,i,e,d,g) (a,h,c,d,i,f,b,g,e) (a,i,b,g,h,e,d,f,c)
    #> [coerced from word form]

Above we see the commas making the output somewhat prolix, but not a disaster. However, if we are permuting \(>9\) objects commas are necessary to interpret strings like `124` which might be `1,2,4` or `12,4` or `1,24` or `124`:

    options("comma" = FALSE)
    x <- rperm(3, r = 20)
    x # commas not printed irregardless
    #> [1] (aideprbgscnkjm)(flho)(qt) (aqgnpfocdeltib)(hsrk)(jm)
    #> [3] (aeo)(bktdc)(gnhilj)(mqr)
    #> [coerced from word form]
    options("comma" = NULL) # restore default
    x # default for comparison
    #> [1] (a,i,d,e,p,r,b,g,s,c,n,k,j,m)(f,l,h,o)(q,t)
    #> [2] (a,q,g,n,p,f,o,c,d,e,l,t,i,b)(h,s,r,k)(j,m)
    #> [3] (a,e,o)(b,k,t,d,c)(g,n,h,i,l,j)(m,q,r)
    #> [coerced from word form]

Above, the absence of commas is somewhat confusing, default restored.

### Symbols

The `permutations` package considers permutations of a finite set. It is very convenient to identify the finite set with integers \(1,2,\ldots,n\) [chiefly because products and inverses are easy if one can use R’s 1-based vector indexing: products are just `a*b=b[a]` and the inverse of `W` is just `W[W] <- seq_along(W)`]. Because of this, the default print method echoes the internal representation of a permutation.

    set.seed(0)
    x <- rperm(n = 3)
    dput(x)
    #> structure(c(6L, 7L, 5L, 1L, 3L, 2L, 4L, 2L, 6L, 3L, 6L, 3L, 7L,
    #> 4L, 1L, 2L, 1L, 4L, 5L, 5L, 7L), dim = c(3L, 7L), class = c("permutation",
    #> "word"))
    x
    #> [1] (afb)(cd)(eg) (agedf)(bc)   (ae)(cfd)
    #> [coerced from word form]

We see the print method echoing the internal representation of the set \(\left[n\right]=\left\lbrace 1,2,\ldots,n\right\rbrace\). However, it is possible to use a different set, using option `perm_set`:

    options(perm_set = letters)
    rperm()
    #>  [1] (aegdb)(cf)  (abfec)      (acbf)(de)   (adfcbge)    (bdcge)
    #>  [6] (acfgd)      (afgdeb)     (ad)(bf)(eg) (bfeg)(cd)   (afcdgbe)
    #> [coerced from word form]

Above we see cyclic representation of permutations of the letters a-z. However, the use of commas is somewhat problematic, and by default the print method uses the \(>9\) criterion for including a comma:

    (xx <- rperm(n = 2, r = 26))
    #> [1] (a,f,r,h,k,l,c,i,j,w,z,v,y,o,u,d,g,p)(b,q,t,e,s,n)
    #> [2] (a,s,z,l,y,r,p,m,u,b,c,k,w,q,x,j,v,h,i,e,n,o,g,t,d)
    #> [coerced from word form]

Above we see that commas are included (because \(26>9\)) but it probably looks better without them:

    options(comma = FALSE)
    xx
    #> [1] (afrhklcijwzvyoudgp)(bqtesn) (aszlyrpmubckwqxjvhienogtd)
    #> [coerced from word form]

One side-effect of using a finite set of symbols is that the print method might run out of symbols:

    (z <- rperm(n = 2, r = 50))
    #> [1] (agNANAyNAepqNANAhbNAuNANAjNAlzNANAdxNAfsNAoNANANArkmNAvnNANANANANAwNANAi)(ct)
    #> [2] (aNANArNANANAi)(bNANAsNAlvzNAgNANAwNANANAoNANAjtnpNAyeNANAmNAfNAcNAq)(dhu)(kxNANA)
    #> [coerced from word form]

Above we see `NA` printed where the symbol’s index exceeds 26 (also the absence of commas does not help). Remember that the print method does not change the object itself, so this might not be an issue. It is possible to use symbols that have more than one character:

    options(perm_set = state.abb)
    options(comma = TRUE)
    z
    #> [1] (AL,CT,OK,WV,MO,NC,CA,KS,KY,PA,NE,DE,AK,WA,MA,NY,NJ,GA,RI,ID,MT,VA,SD,AR,MS,SC,CO,ME,NM,IA,VT,WI,NH,LA,HI,IL,ND,MI,IN,TX,TN,OR,OH,WY,MN,UT,NV,FL)(AZ,MD)
    #> [2] (AL,NE,SC,LA,WA,WV,ND,FL)(AK,UT,TN,ME,VA,ID,MI,MT,NV,CT,NH,VT,MN,NC,WY,OR,IA,NJ,NY,GA,MD,IN,KS,WI,MO,CA,OK,RI,IL,PA,CO,SD,AZ,NM,KY)(AR,DE,MA)(HI,MS,TX,OH)
    #> [coerced from word form]

## Cycles in length order

Consider the following:

    options(perm_set = NULL) # revert to numbers
    options(comma = FALSE) # supress comma
    x <- rgivenshape(30, 2:4)
    x
    #>  [1] (1938)(256)(47) (162)(3547)(89) (189)(24)(3567) (1523)(48)(697)
    #>  [5] (163)(25)(4978) (1263)(45)(789) (1829)(36)(457) (1764)(239)(58)
    #>  [9] (153)(28)(4976) (1783)(265)(49) (1829)(34)(576) (13)(2496)(587)
    #> [13] (1326)(498)(57) (1537)(286)(49) (17)(284)(3965) (1846)(237)(59)
    #> [17] (1597)(246)(38) (1392)(486)(57) (17)(268)(3549) (1972)(356)(48)
    #> [21] (198)(2647)(35) (14)(2853)(697) (19)(247)(3568) (1458)(29)(367)
    #> [25] (1249)(367)(58) (1794)(256)(38) (1394)(287)(56) (167)(2439)(58)
    #> [29] (15)(287)(3649) (1369)(285)(47)

We see 30 random permutations with shape \(\left(\cdot\,\cdot\right)\left(\cdot\cdot\cdot\right)\left(\cdot\cdot\cdot\right)\). However, because function `nicify_cyclist()` sorts each cycle so that the smallest element is first, then sorts the cycles by first element, it is not obvious that all the permutations above have the same shape. The print method is sensitive to experimental option `print_in_length_order` (via function `as.character_cyclist()`). If `TRUE`, permutations cycle form will be printed but with the cycles in increasing length order:

    options("print_in_length_order" = TRUE)
    x
    #>  [1] (47)(256)(1938) (89)(162)(3547) (24)(189)(3567) (48)(697)(1523)
    #>  [5] (25)(163)(4978) (45)(789)(1263) (36)(457)(1829) (58)(239)(1764)
    #>  [9] (28)(153)(4976) (49)(265)(1783) (34)(576)(1829) (13)(587)(2496)
    #> [13] (57)(498)(1326) (49)(286)(1537) (17)(284)(3965) (59)(237)(1846)
    #> [17] (38)(246)(1597) (57)(486)(1392) (17)(268)(3549) (48)(356)(1972)
    #> [21] (35)(198)(2647) (14)(697)(2853) (19)(247)(3568) (29)(367)(1458)
    #> [25] (58)(367)(1249) (38)(256)(1794) (56)(287)(1394) (58)(167)(2439)
    #> [29] (15)(287)(3649) (47)(285)(1369)

## Wrapup

Above we used state abbreviations (a builtin R dataset) and also direct the print method to use commas for readability. However, it is best to reset to the default, as the option persists between vignettes:

    options(perm_set = NULL)
    options(comma = NULL)
    options(print_word_as_cycle = FALSE)
    options("print_in_length_order" = FALSE)

## References

Hankin, R. K. S. 2020. “Introducing the Permutations R Package.” SoftwareX 11. https://doi.org/10.1016/j.softx.2020.100453.