RobinHankin / permutations

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

print method extensions #6

Closed RobinHankin closed 1 year ago

RobinHankin commented 6 years ago

It might be nice to extend the print method analogously to the freegroup package, in which it is possible to name the generators arbitrarily. Thus a cycle which is currently printed as (123)(45) might appear as (abc)(de). This would allow one to print permutations of up to 26 objects without using commas, which might be desirable.

In freegroup we could have

> options(symbols=state.abb)
> rfree(1,10)
[1] AZ^4.AK^-8.CT^-1.CA.AZ^2.AR^-10.CA^-6.AK^8.AZ^5

and something like this might be desirable in the context of (eg) the megaminx which has objects corresponding to stickers numbered 10-129. But it would be better to use more descriptive names. Thus 118 means position 8 on the pink pentagon (number 11), and it might be better to refer to this as PI8 rather than the unhelpful 118.

But the colour names have different lengths ("Dark Yellow" vs "white", for example) and the abbreviated colours also have different lengths ("DY" vs "W") and indeed capitalizations ("Pi" and "Pu").

I think commas should be included. (W8,Pi9,LG0) looks a lot better than (W8Pi9LG0).

RobinHankin commented 5 years ago

This is what it looks like:

> rperm(6,26)
[1] (ayhjifwosrptzguveqn)(cklmxd)        (aceoikryhxg)(bfdmquv)(lwptsn)      
[3] (ay)(blkpe)(cwjoxgnmqtfuszv)(dr)(hi) (aemuipklqwjv)(bhgncs)(dtozx)(fry)  
[5] (aesvzgiflqdcwrxhbm)(kytn)(pu)       (anjfmzkb)(cyuotpx)(dslvhqwg)       
> 
RobinHankin commented 2 years ago

It would also be possible to print permutations in word form using letters. This might be achievable:

> a
[1] (196)(2735)    (162834)(597)  (25384769)     (138)(496)(57) (1697425)(38) 
[6] (18473962)    
[coerced from word form]

> print_word(a)
    {1} {2} {3} {4} {5} {6} {7} {8} {9}
[1] 9   7   5   .   2   1   3   .   6  
[2] 6   8   4   1   9   2   5   3   7  
[3] .   5   8   7   3   9   6   4   2  
[4] 3   .   8   9   7   4   5   1   6  
[5] 6   5   8   2   1   9   4   3   7  
[6] 8   1   9   7   .   2   3   4   6  
> 

> options(word_symbols = letters)
> a
[1] (aif)(bgce)    (afbhcd)(eig)  (bechdgfi)     (ach)(dif)(eg) (afigdbe)(ch) 
[6] (ahdgcifb)    

> print_word(a)
    {a} {b} {c} {d} {e} {f} {g} {h} {i}
[1] i   g   e   .   b   a   c   .   f  
[2] f   h   d   a   i   b   e   c   g  
[3] .   e   h   g   c   i   f   d   b  
[4] c   .   h   i   g   d   e   a   f  
[5] f   e   h   b   a   i   d   c   g  
[6] h   a   i   g   .   b   c   d   f  
> 
RobinHankin commented 2 years ago

Actually we need a little more than just modifying the print method, because it should be possible to coerce strings to a permutation using the symbols from the print method. For example:

> as.cycle("(123)(56)")  # works fine
[1] (123)(56)
> as.cycle("(abc)(ef)")  # we would like this to work too, given the  `options()` above...
Error in if (any(x < 0)) { : missing value where TRUE/FALSE needed  # ... but it doesn't
RobinHankin commented 2 years ago

I have just realised that there is an essential difference between using 1,2,3,... or a,b,c,... on the one hand, and AL,AK,AZ,... (state abbreviations) on the other. The numbers and letters are ordered and must appear in their actual order when using print_word() or, indirectly, print_cycle() [via nicify_cyclist()]. But the states are not inherently ordered. So a three-cycle (AL,AK,AZ) could just as easily appear as (AK,AZ,AL) or (AZ,AL,AK). Less obviously, the word form might be either of these two forms:

    {AL} {AK} {AZ}
[1] AK   AZ   AL

    {AL} {AZ} {AK}
[1] AK   AL   AZ

In both cases we see the cycle Alabama -> Alaska -> Arizona-> Alabama -> ... But in the second case we have (for whatever reason) put Arizona second and Alaska third; and this is just as valid an ordering as the alphabetical ordering given in state.abb.