p1 <- c(1,3,5,2,4)
p1inv <- invperm(p1)            # 1 4 2 5 3
# Define two more functions for permutations
compoperm <- function(p, q) q[p]        # composition of permutations
compoperm(p1, p1inv)                    # 1 2 3 4 5
applyperm <- function(p, x) x[p]        # apply permutation to vector
p2 <- c(2,1,3,5,4)
x  <- c(11,12,13,14,15)
applyperm(compoperm(p2, p1), x) == applyperm(p2, applyperm(p1, x))  # all TRUERun the code above in your browser using DataLab