排列#
- class sympy.combinatorics.permutations.Permutation(*args, size=None, **kwargs)[源代码]#
A permutation, alternatively known as an 'arrangement number' or 'ordering' is an arrangement of the elements of an ordered list into a one-to-one mapping with itself. The permutation of a given arrangement is given by indicating the positions of the elements after re-arrangement [R80]. For example, if one started with elements
[x, y, a, b]
(in that order) and they were reordered as[x, y, b, a]
then the permutation would be[0, 1, 3, 2]
. Notice that (in SymPy) the first element is always referred to as 0 and the permutation uses the indices of the elements in the original ordering, not the elements(a, b, ...)
themselves.>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False)
排列符号
置换通常以不相交的循环或数组形式表示。
数组表示法和双线形式
在双线形式中,元素及其最终位置显示为包含两行的矩阵:
[0 1 2 ... n-1] [p(0) p(1) p(2) ... p(n-1)]
Since the first line is always
range(n)
, where n is the size of p, it is sufficient to represent the permutation by the second line, referred to as the "array form" of the permutation. This is entered in brackets as the argument to the Permutation class:>>> p = Permutation([0, 2, 1]); p Permutation([0, 2, 1])
给定范围内的i(p.size),置换将i映射到i^p
>>> [i^p for i in range(p.size)] [0, 2, 1]
两个置换的合成p q意味着首先应用p,然后应用q,所以i ^(p q) =(i^p)^q,根据Python优先规则,它是i^p^q:
>>> q = Permutation([2, 1, 0]) >>> [i^p^q for i in range(3)] [2, 0, 1] >>> [i^(p*q) for i in range(3)] [2, 0, 1]
我们也可以使用符号p(i)=i^p,但是组成规则是(p*q)(i)=q(p(i)),而不是p(q(i)):
>>> [(p*q)(i) for i in range(p.size)] [2, 0, 1] >>> [q(p(i)) for i in range(p.size)] [2, 0, 1] >>> [p(q(i)) for i in range(p.size)] [1, 2, 0]
不相交循环符号
In disjoint cycle notation, only the elements that have shifted are indicated.
For example, [1, 3, 2, 0] can be represented as (0, 1, 3)(2). This can be understood from the 2 line format of the given permutation. In the 2-line form, [0 1 2 3] [1 3 2 0]
The element in the 0th position is 1, so 0 -> 1. The element in the 1st position is three, so 1 -> 3. And the element in the third position is again 0, so 3 -> 0. Thus, 0 -> 1 -> 3 -> 0, and 2 -> 2. Thus, this can be represented as 2 cycles: (0, 1, 3)(2). In common notation, singular cycles are not explicitly written as they can be inferred implicitly.
只有循环中元素的相对顺序:
>>> Permutation(1,2,3) == Permutation(2,3,1) == Permutation(3,1,2) True
不相交循环表示法在表示包含多个循环的置换时非常方便:
>>> Permutation(1, 2)(3, 5) == Permutation([[1, 2], [3, 5]]) True
当计算用不相交循环符号表示的置换积时,它还提供了一些经济性:
>>> Permutation(1, 2)(1, 3)(2, 3) Permutation([0, 3, 2, 1]) >>> _ == Permutation([[1, 2]])*Permutation([[1, 3]])*Permutation([[2, 3]]) True
Caution: when the cycles have common elements between them then the order in which the permutations are applied matters. This module applies the permutations from left to right.
>>> Permutation(1, 2)(2, 3) == Permutation([(1, 2), (2, 3)]) True >>> Permutation(1, 2)(2, 3).list() [0, 3, 1, 2]
In the above case, (1,2) is computed before (2,3). As 0 -> 0, 0 -> 0, element in position 0 is 0. As 1 -> 2, 2 -> 3, element in position 1 is 3. As 2 -> 1, 1 -> 1, element in position 2 is 1. As 3 -> 3, 3 -> 2, element in position 3 is 2.
If the first and second elements had been swapped first, followed by the swapping of the second and third, the result would have been [0, 2, 3, 1]. If, you want to apply the cycles in the conventional right to left order, call the function with arguments in reverse order as demonstrated below:
>>> Permutation([(1, 2), (2, 3)][::-1]).list() [0, 2, 3, 1]
在排列中输入一个单例是表示排列大小的一种方法。这个
size
关键字也可以使用。数组窗体项:
>>> Permutation([[1, 2], [9]]) Permutation([0, 2, 1], size=10) >>> Permutation([[1, 2]], size=10) Permutation([0, 2, 1], size=10)
循环表单输入:
>>> Permutation(1, 2, size=10) Permutation([0, 2, 1], size=10) >>> Permutation(9)(1, 2) Permutation([0, 2, 1], size=10)
Caution: no singleton containing an element larger than the largest in any previous cycle can be entered. This is an important difference in how Permutation and Cycle handle the
__call__
syntax. A singleton argument at the start of a Permutation performs instantiation of the Permutation and is permitted:>>> Permutation(5) Permutation([], size=6)
实例化后输入的singleton是对置换的调用——函数调用——如果参数超出范围,它将触发错误。因此,最好从单例开始循环:
以下操作失败,因为没有元素3:
>>> Permutation(1, 2)(3) Traceback (most recent call last): ... IndexError: list index out of range
这是可以的:只禁止调用超出范围的单例对象;否则排列将自动调整大小:
>>> Permutation(3)(1, 2) Permutation([0, 2, 1, 3]) >>> Permutation(1, 2)(3, 4) == Permutation(3, 4)(1, 2) True
相等性检验
数组形式必须相同才能使排列相等:
>>> Permutation([1, 0, 2, 3]) == Permutation([1, 0]) False
身份置换
同一置换是一种没有元素错位的置换。它可以通过多种方式输入。以下所有操作将创建大小为4的标识置换:
>>> I = Permutation([0, 1, 2, 3]) >>> all(p == I for p in [ ... Permutation(3), ... Permutation(range(4)), ... Permutation([], size=4), ... Permutation(size=4)]) True
小心进入射程 里面 一组括号(即循环符号):
>>> I == Permutation([range(4)]) False
置换印刷
关于如何打印排列,有几点需要注意。
自 1.6 版本弃用: Configuring Permutation printing by setting
Permutation.print_cyclic
is deprecated. Users should use theperm_cyclic
flag to the printers, as described below.1) 如果您更喜欢一种形式(数组或循环),可以设置
init_printing
与perm_cyclic
旗帜。>>> from sympy import init_printing >>> p = Permutation(1, 2)(4, 5)(3, 4) >>> p Permutation([0, 2, 1, 4, 5, 3])
>>> init_printing(perm_cyclic=True, pretty_print=False) >>> p (1 2)(3 4 5)
2) 无论设置如何,都可以获得循环形式数组中的元素列表,并且可以复制其中任何一个元素并将其作为置换的参数提供:
>>> p.array_form [0, 2, 1, 4, 5, 3] >>> p.cyclic_form [[1, 2], [3, 4, 5]] >>> Permutation(_) == p True
3) 打印是经济的,因为尽可能少地打印,同时保留有关排列大小的所有信息:
>>> init_printing(perm_cyclic=False, pretty_print=False) >>> Permutation([1, 0, 2, 3]) Permutation([1, 0, 2, 3]) >>> Permutation([1, 0, 2, 3], size=20) Permutation([1, 0], size=20) >>> Permutation([1, 0, 2, 4, 3, 5, 6], size=20) Permutation([1, 0, 2, 4, 3], size=20)
>>> p = Permutation([1, 0, 2, 3]) >>> init_printing(perm_cyclic=True, pretty_print=False) >>> p (3)(0 1) >>> init_printing(perm_cyclic=False, pretty_print=False)
2没有打印出来,但是它仍然存在,这可以通过array_form和size方法看到:
>>> p.array_form [1, 0, 2, 3] >>> p.size 4
其他方法简介
置换可以作为一个双目标函数,告诉什么元素位于给定的位置
>>> q = Permutation([5, 2, 3, 4, 1, 0]) >>> q.array_form[1] # the hard way 2 >>> q(1) # the easy way 2 >>> {i: q(i) for i in range(q.size)} # showing the bijection {0: 5, 1: 2, 2: 3, 3: 4, 4: 1, 5: 0}
完整的循环形式(包括单原子)可以得到:
>>> p.full_cyclic_form [[0, 1], [2], [3]]
任何排列都可以分解成成对元素的换位:
>>> Permutation([[1, 2], [3, 4, 5]]).transpositions() [(1, 2), (3, 5), (3, 4)] >>> Permutation.rmul(*[Permutation([ti], size=6) for ti in _]).cyclic_form [[1, 2], [3, 4, 5]]
n个元素集合上的置换数由n给出!被称为基数。
>>> p.size 4 >>> p.cardinality 24
一个给定的排列在同一元素的所有可能的排列中有一个秩,但是这个秩是多少取决于排列是如何被枚举的。(有许多不同的方法可以这样做。)字典序秩是由秩方法给出的,这个秩用于通过加法/减法来增加置换:
>>> p.rank() 6 >>> p + 1 Permutation([1, 0, 3, 2]) >>> p.next_lex() Permutation([1, 0, 3, 2]) >>> _.rank() 7 >>> p.unrank_lex(p.size, rank=7) Permutation([1, 0, 3, 2])
The product of two permutations p and q is defined as their composition as functions, (p*q)(i) = q(p(i)) [R84].
>>> p = Permutation([1, 0, 2, 3]) >>> q = Permutation([2, 3, 1, 0]) >>> list(q*p) [2, 3, 0, 1] >>> list(p*q) [3, 2, 1, 0] >>> [q(p(i)) for i in range(p.size)] [3, 2, 1, 0]
排列可以“应用”到任何类似列表的对象,而不仅仅是排列:
>>> p(['zero', 'one', 'four', 'two']) ['one', 'zero', 'four', 'two'] >>> p('zo42') ['o', 'z', '4', '2']
如果您有任意元素的列表,则可以使用from_sequence方法找到相应的排列:
>>> Permutation.from_sequence('SymPy') Permutation([1, 3, 2, 0, 4])
Checking If A Permutation Is Contained In A Group
Generally if you have a group of permutations G on n symbols, and you're checking if a permutation on less than n symbols is part of that group, the check will fail.
Here is an example for n=5 and we check if the cycle (1,2,3) is in G:
>>> from sympy import init_printing >>> init_printing(perm_cyclic=True, pretty_print=False) >>> from sympy.combinatorics import Cycle, Permutation >>> from sympy.combinatorics.perm_groups import PermutationGroup >>> G = PermutationGroup(Cycle(2, 3)(4, 5), Cycle(1, 2, 3, 4, 5)) >>> p1 = Permutation(Cycle(2, 5, 3)) >>> p2 = Permutation(Cycle(1, 2, 3)) >>> a1 = Permutation(Cycle(1, 2, 3).list(6)) >>> a2 = Permutation(Cycle(1, 2, 3)(5)) >>> a3 = Permutation(Cycle(1, 2, 3),size=6) >>> for p in [p1,p2,a1,a2,a3]: p, G.contains(p) ((2 5 3), True) ((1 2 3), False) ((5)(1 2 3), True) ((5)(1 2 3), True) ((5)(1 2 3), True)
The check for p2 above will fail.
Checking if p1 is in G works because SymPy knows G is a group on 5 symbols, and p1 is also on 5 symbols (its largest element is 5).
For
a1
, the.list(6)
call will extend the permutation to 5 symbols, so the test will work as well. In the case ofa2
the permutation is being extended to 5 symbols by using a singleton, and in the case ofa3
it's extended through the constructor argumentsize=6
.There is another way to do this, which is to tell the
contains
method that the number of symbols the group is on does not need to match perfectly the number of symbols for the permutation:>>> G.contains(p2,strict=False) True
This can be via the
strict
argument to thecontains
method, and SymPy will try to extend the permutation on its own and then perform the containment check.参见
工具书类
[R79]用Mathematica实现离散数学组合学和图论的1.1。雷丁,MA:Addison-Wesley,第3-16页,1990年。
[R81]温迪·迈尔沃德和弗兰克·罗斯基。2001线性时间中排列排列和取消排列。信息处理。利特。79、6(2001年9月),第281-284页。DOI=10.1016/S0020-0190(01)00141-7
[R82]D、 L.Kreher,D.R.Stinson“组合算法”,CRC出版社,1999年
[R83]Graham,R.L.;Knuth,D.E.;和Patashnik,O.《混凝土数学:计算机科学基础》,第2版,阅读,MA:Addison-Wesley,1994。
- apply(i)[源代码]#
对表达式应用置换。
- 参数:
i :表达式
它应该是一个介于\(0\)和\(n-1\)之间的整数,其中\(n\)是置换的大小。
如果它是可以具有整数值的符号或符号表达式,则
AppliedPermutation
对象将返回,该对象可以表示未赋值的函数。
笔记
Any permutation can be defined as a bijective function \(\sigma : \{ 0, 1, \dots, n-1 \} \rightarrow \{ 0, 1, \dots, n-1 \}\) where \(n\) denotes the size of the permutation.
该定义甚至可以扩展到具有不同元素的任何集合,使得置换甚至可以应用于实数或诸如此类,但是,由于计算的原因和与群论模块的完整性,目前还没有实现该定义。
This function is similar to the
__call__
magic, however,__call__
magic already has some other applications like permuting an array or attaching new cycles, which would not always be mathematically consistent.这也保证了返回类型是SymPy integer,这保证了使用假设的安全性。
- property array_form#
返回属性_array_form Examples的副本========
>>> from sympy.combinatorics import Permutation >>> p = Permutation([[2, 0], [3, 1]]) >>> p.array_form [2, 3, 0, 1] >>> Permutation([[2, 0, 3, 1]]).array_form [3, 2, 0, 1] >>> Permutation([2, 0, 3, 1]).array_form [2, 0, 3, 1] >>> Permutation([[1, 2], [4, 5]]).array_form [0, 2, 1, 3, 5, 4]
- ascents()[源代码]#
置换的位置,即返回p的位置 [i] <p [i+1]
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([4, 0, 1, 3, 2]) >>> p.ascents() [1, 2]
参见
descents
,inversions
,min
,max
- atoms()[源代码]#
返回置换的所有元素
实例
>>> from sympy.combinatorics import Permutation >>> Permutation([0, 1, 2, 3, 4, 5]).atoms() {0, 1, 2, 3, 4, 5} >>> Permutation([[0, 1], [2, 3], [4, 5]]).atoms() {0, 1, 2, 3, 4, 5}
- property cardinality#
返回所有可能的置换数。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.cardinality 24
- commutator(x)[源代码]#
返回的换向器
self
和x
:~x*~self*x*self
如果f和g是群g的一部分,那么f和g的交换子是群恒等式,如果f和g通勤,即fg==gf。
实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([0, 2, 3, 1]) >>> x = Permutation([2, 0, 3, 1]) >>> c = p.commutator(x); c Permutation([2, 1, 3, 0]) >>> c == ~x*~p*x*p True
>>> I = Permutation(3) >>> p = [I + i for i in range(6)] >>> for i in range(len(p)): ... for j in range(len(p)): ... c = p[i].commutator(p[j]) ... if p[i]*p[j] == p[j]*p[i]: ... assert c == I ... else: ... assert c != I ...
工具书类
- commutes_with(other)[源代码]#
检查元素是否正在交换。
实例
>>> from sympy.combinatorics import Permutation >>> a = Permutation([1, 4, 3, 0, 2, 5]) >>> b = Permutation([0, 1, 2, 3, 4, 5]) >>> a.commutes_with(b) True >>> b = Permutation([2, 3, 5, 4, 1, 0]) >>> a.commutes_with(b) False
- property cycle_structure#
返回置换的循环结构作为字典,指示每个循环长度的多重性。
实例
>>> from sympy.combinatorics import Permutation >>> Permutation(3).cycle_structure {1: 4} >>> Permutation(0, 4, 3)(1, 2)(5, 6).cycle_structure {2: 2, 3: 1}
- property cycles#
返回置换中包含的循环数(包括单例)。
实例
>>> from sympy.combinatorics import Permutation >>> Permutation([0, 1, 2]).cycles 3 >>> Permutation([0, 1, 2]).full_cyclic_form [[0], [1], [2]] >>> Permutation(0, 1)(2, 3).cycles 2
- property cyclic_form#
这用于从规范表示法转换为循环表示法。单例省略。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 3, 1, 2]) >>> p.cyclic_form [[1, 3, 2]] >>> Permutation([1, 0, 2, 4, 3, 5]).cyclic_form [[0, 1], [3, 4]]
- descents()[源代码]#
返回置换中降序的位置,即p [i] >p [i+1]
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([4, 0, 1, 3, 2]) >>> p.descents() [0, 3]
参见
ascents
,inversions
,min
,max
- classmethod from_inversion_vector(inversion)[源代码]#
从反演向量计算排列。
实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> Permutation.from_inversion_vector([3, 2, 1, 0, 0]) Permutation([3, 2, 1, 0, 4, 5])
- classmethod from_sequence(i, key=None)[源代码]#
返回获取所需的排列
i
从i
. 如果需要自定义排序,可以指定一个键。实例
>>> from sympy.combinatorics import Permutation
>>> Permutation.from_sequence('SymPy') (4)(0 1 3) >>> _(sorted("SymPy")) ['S', 'y', 'm', 'P', 'y'] >>> Permutation.from_sequence('SymPy', key=lambda x: x.lower()) (4)(0 2)(1 3)
- property full_cyclic_form#
循环形式的返回置换,包括单粒子。
实例
>>> from sympy.combinatorics import Permutation >>> Permutation([0, 2, 1]).full_cyclic_form [[0], [1, 2]]
- get_adjacency_distance(other)[源代码]#
计算两个置换之间的邻接距离。
解释
此度量计算一对i,j作业在p和p'中相邻的次数。如果n_adj是这个量,那么邻接距离是n-n_adj-1 [1]
[1] Reeves,Colin R.《景观、操作员和启发式搜索》,《运筹学年鉴》,86,473-490页。(1999年)
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 3, 1, 2, 4]) >>> q = Permutation.josephus(4, 5, 2) >>> p.get_adjacency_distance(q) 3 >>> r = Permutation([0, 2, 1, 4, 3]) >>> p.get_adjacency_distance(r) 4
- get_adjacency_matrix()[源代码]#
计算置换的邻接矩阵。
解释
如果在置换p中job i与job j相邻,则设m [i, j] =1,其中m是p的邻接矩阵。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation.josephus(3, 6, 1) >>> p.get_adjacency_matrix() Matrix([ [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]]) >>> q = Permutation([0, 1, 2, 3]) >>> q.get_adjacency_matrix() Matrix([ [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]])
- get_positional_distance(other)[源代码]#
计算两个位置置换之间的距离。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 3, 1, 2, 4]) >>> q = Permutation.josephus(4, 5, 2) >>> r = Permutation([3, 1, 4, 0, 2]) >>> p.get_positional_distance(q) 12 >>> p.get_positional_distance(r) 12
- get_precedence_distance(other)[源代码]#
计算两个置换之间的优先距离。
解释
假设p和p'代表n个作业。优先级度量计算p和p'中作业j前面跟作业i的次数。这个度量是可交换的。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([2, 0, 4, 3, 1]) >>> q = Permutation([3, 1, 2, 4, 0]) >>> p.get_precedence_distance(q) 7 >>> q.get_precedence_distance(p) 7
- get_precedence_matrix()[源代码]#
获取优先矩阵。这是用来计算两个排列之间的距离。
实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation.josephus(3, 6, 1) >>> p Permutation([2, 5, 3, 1, 4, 0]) >>> p.get_precedence_matrix() Matrix([ [0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0], [1, 1, 0, 1, 1, 1], [1, 1, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0], [1, 1, 0, 1, 1, 0]])
- index()[源代码]#
返回置换的索引。
置换的指数是所有下标j的和,使得p [j] 大于p [j+1] .
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([3, 0, 2, 1, 4]) >>> p.index() 2
- inversion_vector()[源代码]#
返回置换的反转向量。
反演向量由元素组成,这些元素的值表示置换中元素的数量,这些元素的数量小于置换向量,并且位于其右侧。
反转向量与置换的Lehmer编码相同。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([4, 8, 0, 7, 1, 5, 3, 6, 2]) >>> p.inversion_vector() [4, 7, 0, 5, 0, 2, 1, 1] >>> p = Permutation([3, 2, 1, 0]) >>> p.inversion_vector() [3, 2, 1]
逆向量随置换的秩按字典顺序增加,第i个元素循环通过0..i。
>>> p = Permutation(2) >>> while p: ... print('%s %s %s' % (p, p.inversion_vector(), p.rank())) ... p = p.next_lex() (2) [0, 0] 0 (1 2) [0, 1] 1 (2)(0 1) [1, 0] 2 (0 1 2) [1, 1] 3 (0 2 1) [2, 0] 4 (0 2) [2, 1] 5
- inversions()[源代码]#
计算置换的倒数。
解释
反转是指i>j但是p [i] <p [j] .
对于较小长度的p,它迭代所有i和j值并计算反转次数。对于大长度的p,它使用merge-sort的变异来计算反转次数。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3, 4, 5]) >>> p.inversions() 0 >>> Permutation([3, 2, 1, 0]).inversions() 6
工具书类
- property is_Empty#
检查置换是否为零元素集
实例
>>> from sympy.combinatorics import Permutation >>> Permutation([]).is_Empty True >>> Permutation([0]).is_Empty False
参见
- property is_Identity#
如果置换是标识置换,则返回True。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([]) >>> p.is_Identity True >>> p = Permutation([[0], [1], [2]]) >>> p.is_Identity True >>> p = Permutation([0, 1, 2]) >>> p.is_Identity True >>> p = Permutation([0, 2, 1]) >>> p.is_Identity False
参见
- property is_Singleton#
检查置换是否仅包含一个数字,因此是这组数字的唯一可能排列
实例
>>> from sympy.combinatorics import Permutation >>> Permutation([0]).is_Singleton True >>> Permutation([0, 1]).is_Singleton False
参见
- property is_even#
检查置换是否为偶数。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.is_even True >>> p = Permutation([3, 2, 1, 0]) >>> p.is_even True
参见
- property is_odd#
检查置换是否为奇数。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.is_odd False >>> p = Permutation([3, 2, 0, 1]) >>> p.is_odd True
参见
- classmethod josephus(m, n, s=1)[源代码]#
使用Josephus方案将范围(n)的洗牌作为置换返回,在该方案中,每个m个项目被选中,直到所有项目都被选中。返回的排列按元素的选择顺序列出元素。
参数
s
如果存在,则停止选择过程s
剩余项,这些项通过继续选择进行选择,按1计数,而不是按m
.考虑从第6项中选择第3项,直到只剩下2项:
choices chosen ======== ====== 012345 01 345 2 01 34 25 01 4 253 0 4 2531 0 25314 253140
实例
>>> from sympy.combinatorics import Permutation >>> Permutation.josephus(3, 6, 2).array_form [2, 5, 3, 1, 4, 0]
工具书类
- length()[源代码]#
返回置换移动的整数数。
实例
>>> from sympy.combinatorics import Permutation >>> Permutation([0, 3, 2, 1]).length() 2 >>> Permutation([[0, 1], [2, 3]]).length() 4
- list(size=None)[源代码]#
以显式列表的形式返回置换,如果大小小于置换中的最大元素,则可能修剪未移动的元素;如果需要,则设置
size=-1
保证这样的修剪。实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation(2, 3)(4, 5) >>> p.list() [0, 1, 3, 2, 5, 4] >>> p.list(10) [0, 1, 3, 2, 5, 4, 6, 7, 8, 9]
传递太小的长度将修剪排列中尾随的、不变的元素:
>>> Permutation(2, 4)(1, 2, 4).list(-1) [0, 2, 1] >>> Permutation(3).list(-1) []
- max()[源代码]#
排列移动的最大元素。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([1, 0, 2, 3, 4]) >>> p.max() 1
参见
- min()[源代码]#
排列移动的最小元素。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 4, 3, 2]) >>> p.min() 2
参见
- next_lex()[源代码]#
按字典顺序返回下一个置换。如果self是字典顺序中的最后一个置换,则返回None。看到了吗 [4] 第2.4条。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([2, 3, 1, 0]) >>> p = Permutation([2, 3, 1, 0]); p.rank() 17 >>> p = p.next_lex(); p.rank() 18
参见
- next_nonlex()[源代码]#
以非lex顺序返回下一个置换 [3] . 如果self是此顺序中的最后一个置换,则返回None。
实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([2, 0, 3, 1]); p.rank_nonlex() 5 >>> p = p.next_nonlex(); p Permutation([3, 0, 1, 2]) >>> p.rank_nonlex() 6
- next_trotterjohnson()[源代码]#
以Trotter-Johnson顺序返回下一个置换。如果self是最后一个置换,则返回None。看到了吗 [4] 第2.4条。如果希望生成所有这样的排列,则可以使用
generate_bell
功能。实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([3, 0, 2, 1]) >>> p.rank_trotterjohnson() 4 >>> p = p.next_trotterjohnson(); p Permutation([0, 3, 2, 1]) >>> p.rank_trotterjohnson() 5
- order()[源代码]#
计算排列的顺序。
当置换被提升到它的阶次幂时,它等于同一置换。
实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([3, 1, 5, 2, 4, 0]) >>> p.order() 4 >>> (p**(p.order())) Permutation([], size=6)
参见
- parity()[源代码]#
计算置换的奇偶性。
解释
一个置换的奇偶性反映了该置换中反转数的奇偶性,即x和y对的奇偶性,使得
x > y
但是p[x] < p[y]
.实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.parity() 0 >>> p = Permutation([3, 2, 0, 1]) >>> p.parity() 1
参见
- classmethod random(n)[源代码]#
生成长度的随机排列
n
.使用底层Python伪随机数生成器。
实例
>>> from sympy.combinatorics import Permutation >>> Permutation.random(2) in (Permutation([1, 0]), Permutation([0, 1])) True
- rank()[源代码]#
返回置换的字典排序。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.rank() 0 >>> p = Permutation([3, 2, 1, 0]) >>> p.rank() 23
参见
- rank_nonlex(inv_perm=None)[源代码]#
这是一个线性时间排序算法,不强制字典顺序 [3] .
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.rank_nonlex() 23
- rank_trotterjohnson()[源代码]#
返回Trotter-Johnson秩,它是从最小变化算法中得到的。看到了吗 [4] 第2.4条。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.rank_trotterjohnson() 0 >>> p = Permutation([0, 2, 1, 3]) >>> p.rank_trotterjohnson() 7
- resize(n)[源代码]#
将排列调整为新大小
n
.- 参数:
n :内景
排列的新大小。
- 加薪:
ValueError
如果排列的大小不能调整到给定的大小。只有当调整到比原始尺寸更小的尺寸时,才会发生这种情况。
实例
>>> from sympy.combinatorics import Permutation
增加排列的大小:
>>> p = Permutation(0, 1, 2) >>> p = p.resize(5) >>> p (4)(0 1 2)
减小排列的大小:
>>> p = p.resize(4) >>> p (3)(0 1 2)
如果调整到特定大小会中断循环:
>>> p.resize(2) Traceback (most recent call last): ... ValueError: The permutation cannot be resized to 2 because the cycle (0, 1, 2) may break.
- static rmul(*args)[源代码]#
置换的返回积 [a、 b,c。。。] 作为第i个值为a(b(c(i)))的置换。
a、 b,c。。。可以是置换对象或元组。
实例
>>> from sympy.combinatorics import Permutation
>>> a, b = [1, 0, 2], [0, 2, 1] >>> a = Permutation(a); b = Permutation(b) >>> list(Permutation.rmul(a, b)) [1, 2, 0] >>> [a(b(i)) for i in range(3)] [1, 2, 0]
它处理操作数的顺序与
*
操作员:>>> a = Permutation(a); b = Permutation(b) >>> list(a*b) [2, 0, 1] >>> [b(a(i)) for i in range(3)] [2, 0, 1]
笔记
只要第一项是置换,序列中的所有项都将通过置换进行解析:
>>> Permutation.rmul(a, [0, 2, 1]) == Permutation.rmul(a, b) True
参数的相反顺序将引发TypeError。
- runs()[源代码]#
返回排列的运行次数。
排列中的升序序列称为运行 [5] .
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([2, 5, 7, 3, 6, 0, 1, 4, 8]) >>> p.runs() [[2, 5, 7], [3, 6], [0, 1, 4, 8]] >>> q = Permutation([1,3,2,0]) >>> q.runs() [[1, 3], [2], [0]]
- signature()[源代码]#
给出将置换元素按规范顺序排列所需的置换签名。
签名计算为(-1)^<number of inversions>
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2]) >>> p.inversions() 0 >>> p.signature() 1 >>> q = Permutation([0,2,1]) >>> q.inversions() 1 >>> q.signature() -1
参见
- property size#
返回置换中的元素数。
实例
>>> from sympy.combinatorics import Permutation >>> Permutation([[3, 2], [0, 1]]).size 4
参见
- support()[源代码]#
返回置换中的元素P,其中P [i] !=i。
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([[3, 2], [0, 1], [4]]) >>> p.array_form [1, 0, 3, 2, 4] >>> p.support() [0, 1, 2, 3]
- transpositions()[源代码]#
返回分解为换位列表的置换。
解释
置换总是可以表示为换位的产物,参见 [1]
实例
>>> from sympy.combinatorics import Permutation >>> p = Permutation([[1, 2, 3], [0, 4, 5, 6, 7]]) >>> t = p.transpositions() >>> t [(0, 7), (0, 6), (0, 5), (0, 4), (1, 3), (1, 2)] >>> print(''.join(str(c) for c in t)) (0, 7)(0, 6)(0, 5)(0, 4)(1, 3)(1, 2) >>> Permutation.rmul(*[Permutation([ti], size=p.size) for ti in t]) == p True
工具书类
- classmethod unrank_lex(size, rank)[源代码]#
字典排列取消排列。
实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> a = Permutation.unrank_lex(5, 10) >>> a.rank() 10 >>> a Permutation([0, 2, 4, 1, 3])
- classmethod unrank_nonlex(n, r)[源代码]#
这是一个不考虑字典序的线性时间解列算法 [3] .
实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> Permutation.unrank_nonlex(4, 5) Permutation([2, 0, 3, 1]) >>> Permutation.unrank_nonlex(4, -1) Permutation([0, 1, 2, 3])
- classmethod unrank_trotterjohnson(size, rank)[源代码]#
Trotter Johnson排列取消排列。看到了吗 [4] 第2.4条。
实例
>>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> Permutation.unrank_trotterjohnson(5, 10) Permutation([0, 3, 1, 2, 4])
- class sympy.combinatorics.permutations.Cycle(*args)[源代码]#
提供不相交循环功能的dict包装器。
解释
循环显示用于移动元素子集以获得排列的规则。Cycle类比置换更灵活,因为1)为了研究多个循环如何按顺序作用,不需要存在所有元素;2)它可以包含单个元素:
>>> from sympy.combinatorics.permutations import Perm, Cycle
循环将自动解析rhs上以元组形式给出的循环:
>>> Cycle(1, 2)(2, 3) (1 3 2)
identity循环cycle()可用于启动产品:
>>> Cycle()(1, 2)(2, 3) (1 3 2)
一个循环的数组形式可以通过调用list方法(或传递给list函数)得到,0中的所有元素都将显示:
>>> a = Cycle(1, 2) >>> a.list() [0, 2, 1] >>> list(a) [0, 2, 1]
如果需要更大(或更小)的范围,请使用list方法并提供所需的大小,但不能将循环截断为小于不合适的最大元素的大小:
>>> b = Cycle(2, 4)(1, 2)(3, 1, 4)(1, 3) >>> b.list() [0, 2, 1, 3, 4] >>> b.list(b.size + 1) [0, 2, 1, 3, 4, 5] >>> b.list(-1) [0, 2, 1]
打印时不显示单个元素,但有一个例外:始终显示最大的元素--如有必要,显示为单个元素:
>>> Cycle(1, 4, 10)(4, 5) (1 5 4 10) >>> Cycle(1, 2)(4)(5)(10) (1 2)(10)
数组形式可用于实例化置换,因此可以研究置换的其他属性:
>>> Perm(Cycle(1, 2)(3, 4).list()).transpositions() [(1, 2), (3, 4)]
笔记
循环的基本结构是一个字典,尽管 __iter__ 方法已被重新定义为提供循环的数组形式,基础字典项仍然可用items()等方法:
>>> list(Cycle(1, 2).items()) [(1, 2), (2, 1)]
参见
- list(size=None)[源代码]#
以显式列表形式返回周期,从0开始,直到循环和大小中最大值中的较大值。
当大小小于循环中的最大元素时,将截断尾部未移动的项目;如果需要,请设置
size=-1
保证这样的修剪。实例
>>> from sympy.combinatorics import Cycle >>> p = Cycle(2, 3)(4, 5) >>> p.list() [0, 1, 3, 2, 5, 4] >>> p.list(10) [0, 1, 3, 2, 5, 4, 6, 7, 8, 9]
传递太小的长度将修剪排列中尾随的、不变的元素:
>>> Cycle(2, 4)(1, 2, 4).list(-1) [0, 2, 1]
- sympy.combinatorics.permutations._af_parity(pi)[源代码]#
以数组形式计算置换的奇偶性。
解释
一个置换的奇偶性反映了该置换中逆数的奇偶性,即x和y的对数,使得x>y而p [x] <p [y] .
实例
>>> from sympy.combinatorics.permutations import _af_parity >>> _af_parity([0, 1, 2, 3]) 0 >>> _af_parity([3, 2, 0, 1]) 1
参见
生成器#
- generators.symmetric()[源代码]#
生成n,Sn阶对称群。
实例
>>> from sympy.combinatorics.generators import symmetric >>> list(symmetric(3)) [(2), (1 2), (2)(0 1), (0 1 2), (0 2 1), (0 2)]
- generators.cyclic()[源代码]#
生成n,Cn阶的循环群。
实例
>>> from sympy.combinatorics.generators import cyclic >>> list(cyclic(5)) [(4), (0 1 2 3 4), (0 2 4 1 3), (0 3 1 4 2), (0 4 3 2 1)]
参见