Coerce a permutation to a function
as.function.permutation.Rd
Coerce a permutation to an executable function with domain \(\left\lbrace 1,\ldots,n\right\rbrace\).
The resulting function is vectorised.
Usage
# S3 method for class 'permutation'
as.function(x, ...)
Details
This functionality is sometimes known as group action. Formally, if
\(\alpha\colon X\times G\longrightarrow X\) is a function,
it is a group action if \(\alpha(x,e)=e\) and
\(\alpha(\alpha(x,h),h)=\alpha(x,gh)\). Writing
\(x\cdot g\) for \(\alpha(x,g)\) we have \(x\cdot
e=x\) and \((x\cdot g)\cdot h=x\cdot(gh)\).
In package idiom, given permutation g
[considered as an element
of the symmetric group \(S_n\)], we return the function with domain
\(\left[n\right]=\left\lbrace 1,\ldots,n\right\rbrace\)
mapping \(x\in[n]\) to \(\alpha(g,x)\). For example, if
\(g=(172)(45)\) then \(\alpha(g,7)=2\) and
\(\alpha(g,4)=5\). Package idiom allows one to explicitly coerce
g
to a function, or to use the overloaded caret:
(g <- as.cycle("(172)(45)"))
#> [1] (172)(45)
as.function(g)(7)
#> [1] 2
7^g
#> [1] 2
Note
Multiplication of permutations loses associativity when using functional notation; see examples.
Also, note that the coerced function will not take an argument greater than the size (qv) of the permutation.
Examples
x <- cyc_len(3)
y <- cyc_len(5)
xfun <- as.function(x)
yfun <- as.function(y)
stopifnot(xfun(yfun(2)) == as.function(y*x)(2)) # note transposition of x & y
# postfix notation has the very appealing result x(fg) == (xf)g
# Carets are good too, in that x^(fg) == (x^f)^g.
a <- rperm()
b <- rperm()
stopifnot(2^(a*b) == (2^a)^b)
# it's fully vectorized:
as.function(rperm(10,9))(1)
#> [1] 7 7 9 1 7 5 6 3 8 4
as.function(as.cycle(1:9))(sample(9))
#> [1] 2 7 3 8 6 1 9 4 5
as.function(allcyc(5:8))(1:6)
#> [1] 1 2 3 4 8 5