[英]Is there exist a function in Sage that applies a permutation to a list?
I have the following problem in Sage: 我在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, eg: 我有一个排列对象集合和一个列表集合,每个列表的长度为2。我希望有一个内置函数,可以通过以下方式将排列应用于列表,例如:
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!
谢谢!
Similar to the answer from hiro protagonist, but perhaps more direct: 与hiro主人公的答案类似,但也许更直接:
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. 有一点是,可以将Sage中的排列称为函数,这就是第二行起作用的原因。
you could try this using from_cycles
: 你可以使用
from_cycles
尝试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
. 需要
perm[i-1]
的-1
,因为您的排列从1
开始而不是从0
。 there is a more elegant way to apply a permutation to a list: see John Palmieri's answer . 有一种将排列应用于列表的更优雅的方法:请参见John Palmieri的答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.