BlobofJello
BlobofJello

Reputation: 95

Is there exist a function in Sage that applies a permutation to a list?

I have the following problem in Sage:

I have a collection of permutation objects and a collection of lists, each of length 2. I was hoping there was a built-in function to apply a permutation to a list in the following way, e.g.:

result = (1,2,4)(3,5).apply_to([1,3])
print result
[2,5]

If not, any tips on how to write this function are appreciated. Thank you!

Upvotes: 0

Views: 688

Answers (2)

hiro protagonist
hiro protagonist

Reputation: 46901

you could try this using from_cycles:

sage: from sage.combinat import permutation
sage: perm = permutation.from_cycles(5, ((1,2,4), (3,5)))
sage: perm  # -> [2, 4, 5, 1, 3]
sage: res = [perm[i-1] for i in [1, 3]]
sage: res   # -> [2, 5]

the -1 in perm[i-1] is needed because your permutation starts at 1 and not at 0. there is a more elegant way to apply a permutation to a list: see John Palmieri's answer.

Upvotes: 1

John Palmieri
John Palmieri

Reputation: 1696

Similar to the answer from hiro protagonist, but perhaps more direct:

sage: a = Permutation('(1,2,4)(3,5)')
sage: result = [a(i) for i in [1,3]]
sage: result
[2, 5]

One point is that permutations in Sage can be called as functions, which is why the second line works.

Upvotes: 2

Related Questions