Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nice check of plainperms() #17

Open
RobinHankin opened this issue Jul 5, 2019 · 3 comments
Open

nice check of plainperms() #17

RobinHankin opened this issue Jul 5, 2019 · 3 comments

Comments

@RobinHankin
Copy link
Owner

Knuth points out that the plain permutation algorithm produces alternating odd and even permutations and verifying this has nice R idiom:

> apply(plainperms(5),2,function(x){is.even(as.cycle(as.word(x)))})
  [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 [13]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 [25]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 [37]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 [49]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 [61]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 [73]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 [85]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
 [97]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
[109]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
@RobinHankin
Copy link
Owner Author

but including this in the tests would require the partitions package to be added to the Depends list

@RobinHankin
Copy link
Owner Author

Observing here that we are using partitions::plainperms(); also there is no need to coerce to cycle form:

apply(plainperms(5),2,function(x){is.even(as.word(x))})

@RobinHankin
Copy link
Owner Author

A nice way to test the above would be to use standard recycling, or compare each element with the next:

library("partitions")
suppressMessages(library("permutations"))
x <- apply(plainperms(5),2,function(x){is.even(as.word(x))})
all(x == c(TRUE,FALSE))
#> [1] TRUE
all(x[-1] != x[-length(x)])
#> [1] TRUE

Above, I think the first test is "cuter" but is defective because it relies on: (1), the vector starting with TRUE, and (2), the vector is of even length. Neither of these assumptions is necessarily true. The second test just uses the fact that each element is the negation of the previous. Actually, maybe the second test is cuter. I am clearly overthinking this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant