Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- ckpts/universal/global_step80/zero/21.mlp.dense_h_to_4h_swiglu.weight/fp32.pt +3 -0
- ckpts/universal/global_step80/zero/22.attention.query_key_value.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/22.attention.query_key_value.weight/exp_avg_sq.pt +3 -0
- ckpts/universal/global_step80/zero/22.attention.query_key_value.weight/fp32.pt +3 -0
- ckpts/universal/global_step80/zero/8.input_layernorm.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/8.input_layernorm.weight/exp_avg_sq.pt +3 -0
- ckpts/universal/global_step80/zero/8.input_layernorm.weight/fp32.pt +3 -0
- venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/abstract_nodes.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/cutils.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/fnodes.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/pynodes.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/rewriting.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/__init__.py +43 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/coset_table.py +1255 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/fp_groups.py +1348 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/free_groups.py +1354 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/galois.py +611 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/generators.py +302 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/graycode.py +430 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/group_constructs.py +61 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/group_numbers.py +118 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/homomorphisms.py +549 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/named_groups.py +332 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/partitions.py +745 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/pc_groups.py +709 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/perm_groups.py +0 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/permutations.py +3112 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/polyhedron.py +1019 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/prufer.py +436 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/rewritingsystem.py +453 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/rewritingsystem_fsm.py +60 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/schur_number.py +160 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/subsets.py +619 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/tensor_can.py +1190 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/tests/test_testutil.py +55 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/testutil.py +358 -0
- venv/lib/python3.10/site-packages/sympy/combinatorics/util.py +536 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__init__.py +45 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/curve.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/ellipse.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/entity.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/exceptions.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/line.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/parabola.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/plane.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/point.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/polygon.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/util.cpython-310.pyc +0 -0
ckpts/universal/global_step80/zero/21.mlp.dense_h_to_4h_swiglu.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:159724b7828578b4eb373c6553a52d183313ef9a2b514d06ab1f8ab06531f2bb
|
3 |
+
size 33555533
|
ckpts/universal/global_step80/zero/22.attention.query_key_value.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:22cc0bd93ed78d475bdc48622a3832bd6b305b33e1ff79c6afc4bdb1d03b09dc
|
3 |
+
size 50332828
|
ckpts/universal/global_step80/zero/22.attention.query_key_value.weight/exp_avg_sq.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9a5f0bdc2008facf3aaa5f62c3689b67448d8f2b5c45299fe3431999880cdc5d
|
3 |
+
size 50332843
|
ckpts/universal/global_step80/zero/22.attention.query_key_value.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:68668ab7546b207fb5a6667700e7333e5ce252f5b3fd2afd0305ed7839c6f14c
|
3 |
+
size 50332749
|
ckpts/universal/global_step80/zero/8.input_layernorm.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4d7a847fab5d3b3c84f21e4e0ae3d274134a56e4a1b7c22bd75aafb71aa00aac
|
3 |
+
size 9372
|
ckpts/universal/global_step80/zero/8.input_layernorm.weight/exp_avg_sq.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b38db52162fdbfdf9d71d1e42148a0982766be85368ca6df71ff9b925101bb18
|
3 |
+
size 9387
|
ckpts/universal/global_step80/zero/8.input_layernorm.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6d753f975b01bc787c35c93a87ef16f5daf1dcc6f58ad3bf8b92cac38091908c
|
3 |
+
size 9293
|
venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.09 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/abstract_nodes.cpython-310.pyc
ADDED
Binary file (953 Bytes). View file
|
|
venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/cutils.cpython-310.pyc
ADDED
Binary file (720 Bytes). View file
|
|
venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/fnodes.cpython-310.pyc
ADDED
Binary file (21.2 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/pynodes.cpython-310.pyc
ADDED
Binary file (672 Bytes). View file
|
|
venv/lib/python3.10/site-packages/sympy/codegen/__pycache__/rewriting.cpython-310.pyc
ADDED
Binary file (13.5 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/combinatorics/__init__.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics.permutations import Permutation, Cycle
|
2 |
+
from sympy.combinatorics.prufer import Prufer
|
3 |
+
from sympy.combinatorics.generators import cyclic, alternating, symmetric, dihedral
|
4 |
+
from sympy.combinatorics.subsets import Subset
|
5 |
+
from sympy.combinatorics.partitions import (Partition, IntegerPartition,
|
6 |
+
RGS_rank, RGS_unrank, RGS_enum)
|
7 |
+
from sympy.combinatorics.polyhedron import (Polyhedron, tetrahedron, cube,
|
8 |
+
octahedron, dodecahedron, icosahedron)
|
9 |
+
from sympy.combinatorics.perm_groups import PermutationGroup, Coset, SymmetricPermutationGroup
|
10 |
+
from sympy.combinatorics.group_constructs import DirectProduct
|
11 |
+
from sympy.combinatorics.graycode import GrayCode
|
12 |
+
from sympy.combinatorics.named_groups import (SymmetricGroup, DihedralGroup,
|
13 |
+
CyclicGroup, AlternatingGroup, AbelianGroup, RubikGroup)
|
14 |
+
from sympy.combinatorics.pc_groups import PolycyclicGroup, Collector
|
15 |
+
from sympy.combinatorics.free_groups import free_group
|
16 |
+
|
17 |
+
__all__ = [
|
18 |
+
'Permutation', 'Cycle',
|
19 |
+
|
20 |
+
'Prufer',
|
21 |
+
|
22 |
+
'cyclic', 'alternating', 'symmetric', 'dihedral',
|
23 |
+
|
24 |
+
'Subset',
|
25 |
+
|
26 |
+
'Partition', 'IntegerPartition', 'RGS_rank', 'RGS_unrank', 'RGS_enum',
|
27 |
+
|
28 |
+
'Polyhedron', 'tetrahedron', 'cube', 'octahedron', 'dodecahedron',
|
29 |
+
'icosahedron',
|
30 |
+
|
31 |
+
'PermutationGroup', 'Coset', 'SymmetricPermutationGroup',
|
32 |
+
|
33 |
+
'DirectProduct',
|
34 |
+
|
35 |
+
'GrayCode',
|
36 |
+
|
37 |
+
'SymmetricGroup', 'DihedralGroup', 'CyclicGroup', 'AlternatingGroup',
|
38 |
+
'AbelianGroup', 'RubikGroup',
|
39 |
+
|
40 |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|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics.free_groups import free_group
|
2 |
+
from sympy.printing.defaults import DefaultPrinting
|
3 |
+
|
4 |
+
from itertools import chain, product
|
5 |
+
from bisect import bisect_left
|
6 |
+
|
7 |
+
|
8 |
+
###############################################################################
|
9 |
+
# COSET TABLE #
|
10 |
+
###############################################################################
|
11 |
+
|
12 |
+
class CosetTable(DefaultPrinting):
|
13 |
+
# coset_table: Mathematically a coset table
|
14 |
+
# represented using a list of lists
|
15 |
+
# alpha: Mathematically a coset (precisely, a live coset)
|
16 |
+
# represented by an integer between i with 1 <= i <= n
|
17 |
+
# alpha in c
|
18 |
+
# x: Mathematically an element of "A" (set of generators and
|
19 |
+
# their inverses), represented using "FpGroupElement"
|
20 |
+
# fp_grp: Finitely Presented Group with < X|R > as presentation.
|
21 |
+
# H: subgroup of fp_grp.
|
22 |
+
# NOTE: We start with H as being only a list of words in generators
|
23 |
+
# of "fp_grp". Since `.subgroup` method has not been implemented.
|
24 |
+
|
25 |
+
r"""
|
26 |
+
|
27 |
+
Properties
|
28 |
+
==========
|
29 |
+
|
30 |
+
[1] `0 \in \Omega` and `\tau(1) = \epsilon`
|
31 |
+
[2] `\alpha^x = \beta \Leftrightarrow \beta^{x^{-1}} = \alpha`
|
32 |
+
[3] If `\alpha^x = \beta`, then `H \tau(\alpha)x = H \tau(\beta)`
|
33 |
+
[4] `\forall \alpha \in \Omega, 1^{\tau(\alpha)} = \alpha`
|
34 |
+
|
35 |
+
References
|
36 |
+
==========
|
37 |
+
|
38 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.
|
39 |
+
"Handbook of Computational Group Theory"
|
40 |
+
|
41 |
+
.. [2] John J. Cannon; Lucien A. Dimino; George Havas; Jane M. Watson
|
42 |
+
Mathematics of Computation, Vol. 27, No. 123. (Jul., 1973), pp. 463-490.
|
43 |
+
"Implementation and Analysis of the Todd-Coxeter Algorithm"
|
44 |
+
|
45 |
+
"""
|
46 |
+
# default limit for the number of cosets allowed in a
|
47 |
+
# coset enumeration.
|
48 |
+
coset_table_max_limit = 4096000
|
49 |
+
# limit for the current instance
|
50 |
+
coset_table_limit = None
|
51 |
+
# maximum size of deduction stack above or equal to
|
52 |
+
# which it is emptied
|
53 |
+
max_stack_size = 100
|
54 |
+
|
55 |
+
def __init__(self, fp_grp, subgroup, max_cosets=None):
|
56 |
+
if not max_cosets:
|
57 |
+
max_cosets = CosetTable.coset_table_max_limit
|
58 |
+
self.fp_group = fp_grp
|
59 |
+
self.subgroup = subgroup
|
60 |
+
self.coset_table_limit = max_cosets
|
61 |
+
# "p" is setup independent of Omega and n
|
62 |
+
self.p = [0]
|
63 |
+
# a list of the form `[gen_1, gen_1^{-1}, ... , gen_k, gen_k^{-1}]`
|
64 |
+
self.A = list(chain.from_iterable((gen, gen**-1) \
|
65 |
+
for gen in self.fp_group.generators))
|
66 |
+
#P[alpha, x] Only defined when alpha^x is defined.
|
67 |
+
self.P = [[None]*len(self.A)]
|
68 |
+
# the mathematical coset table which is a list of lists
|
69 |
+
self.table = [[None]*len(self.A)]
|
70 |
+
self.A_dict = {x: self.A.index(x) for x in self.A}
|
71 |
+
self.A_dict_inv = {}
|
72 |
+
for x, index in self.A_dict.items():
|
73 |
+
if index % 2 == 0:
|
74 |
+
self.A_dict_inv[x] = self.A_dict[x] + 1
|
75 |
+
else:
|
76 |
+
self.A_dict_inv[x] = self.A_dict[x] - 1
|
77 |
+
# used in the coset-table based method of coset enumeration. Each of
|
78 |
+
# the element is called a "deduction" which is the form (alpha, x) whenever
|
79 |
+
# a value is assigned to alpha^x during a definition or "deduction process"
|
80 |
+
self.deduction_stack = []
|
81 |
+
# Attributes for modified methods.
|
82 |
+
H = self.subgroup
|
83 |
+
self._grp = free_group(', ' .join(["a_%d" % i for i in range(len(H))]))[0]
|
84 |
+
self.P = [[None]*len(self.A)]
|
85 |
+
self.p_p = {}
|
86 |
+
|
87 |
+
@property
|
88 |
+
def omega(self):
|
89 |
+
"""Set of live cosets. """
|
90 |
+
return [coset for coset in range(len(self.p)) if self.p[coset] == coset]
|
91 |
+
|
92 |
+
def copy(self):
|
93 |
+
"""
|
94 |
+
Return a shallow copy of Coset Table instance ``self``.
|
95 |
+
|
96 |
+
"""
|
97 |
+
self_copy = self.__class__(self.fp_group, self.subgroup)
|
98 |
+
self_copy.table = [list(perm_rep) for perm_rep in self.table]
|
99 |
+
self_copy.p = list(self.p)
|
100 |
+
self_copy.deduction_stack = list(self.deduction_stack)
|
101 |
+
return self_copy
|
102 |
+
|
103 |
+
def __str__(self):
|
104 |
+
return "Coset Table on %s with %s as subgroup generators" \
|
105 |
+
% (self.fp_group, self.subgroup)
|
106 |
+
|
107 |
+
__repr__ = __str__
|
108 |
+
|
109 |
+
@property
|
110 |
+
def n(self):
|
111 |
+
"""The number `n` represents the length of the sublist containing the
|
112 |
+
live cosets.
|
113 |
+
|
114 |
+
"""
|
115 |
+
if not self.table:
|
116 |
+
return 0
|
117 |
+
return max(self.omega) + 1
|
118 |
+
|
119 |
+
# Pg. 152 [1]
|
120 |
+
def is_complete(self):
|
121 |
+
r"""
|
122 |
+
The coset table is called complete if it has no undefined entries
|
123 |
+
on the live cosets; that is, `\alpha^x` is defined for all
|
124 |
+
`\alpha \in \Omega` and `x \in A`.
|
125 |
+
|
126 |
+
"""
|
127 |
+
return not any(None in self.table[coset] for coset in self.omega)
|
128 |
+
|
129 |
+
# Pg. 153 [1]
|
130 |
+
def define(self, alpha, x, modified=False):
|
131 |
+
r"""
|
132 |
+
This routine is used in the relator-based strategy of Todd-Coxeter
|
133 |
+
algorithm if some `\alpha^x` is undefined. We check whether there is
|
134 |
+
space available for defining a new coset. If there is enough space
|
135 |
+
then we remedy this by adjoining a new coset `\beta` to `\Omega`
|
136 |
+
(i.e to set of live cosets) and put that equal to `\alpha^x`, then
|
137 |
+
make an assignment satisfying Property[1]. If there is not enough space
|
138 |
+
then we halt the Coset Table creation. The maximum amount of space that
|
139 |
+
can be used by Coset Table can be manipulated using the class variable
|
140 |
+
``CosetTable.coset_table_max_limit``.
|
141 |
+
|
142 |
+
See Also
|
143 |
+
========
|
144 |
+
|
145 |
+
define_c
|
146 |
+
|
147 |
+
"""
|
148 |
+
A = self.A
|
149 |
+
table = self.table
|
150 |
+
len_table = len(table)
|
151 |
+
if len_table >= self.coset_table_limit:
|
152 |
+
# abort the further generation of cosets
|
153 |
+
raise ValueError("the coset enumeration has defined more than "
|
154 |
+
"%s cosets. Try with a greater value max number of cosets "
|
155 |
+
% self.coset_table_limit)
|
156 |
+
table.append([None]*len(A))
|
157 |
+
self.P.append([None]*len(self.A))
|
158 |
+
# beta is the new coset generated
|
159 |
+
beta = len_table
|
160 |
+
self.p.append(beta)
|
161 |
+
table[alpha][self.A_dict[x]] = beta
|
162 |
+
table[beta][self.A_dict_inv[x]] = alpha
|
163 |
+
# P[alpha][x] = epsilon, P[beta][x**-1] = epsilon
|
164 |
+
if modified:
|
165 |
+
self.P[alpha][self.A_dict[x]] = self._grp.identity
|
166 |
+
self.P[beta][self.A_dict_inv[x]] = self._grp.identity
|
167 |
+
self.p_p[beta] = self._grp.identity
|
168 |
+
|
169 |
+
def define_c(self, alpha, x):
|
170 |
+
r"""
|
171 |
+
A variation of ``define`` routine, described on Pg. 165 [1], used in
|
172 |
+
the coset table-based strategy of Todd-Coxeter algorithm. It differs
|
173 |
+
from ``define`` routine in that for each definition it also adds the
|
174 |
+
tuple `(\alpha, x)` to the deduction stack.
|
175 |
+
|
176 |
+
See Also
|
177 |
+
========
|
178 |
+
|
179 |
+
define
|
180 |
+
|
181 |
+
"""
|
182 |
+
A = self.A
|
183 |
+
table = self.table
|
184 |
+
len_table = len(table)
|
185 |
+
if len_table >= self.coset_table_limit:
|
186 |
+
# abort the further generation of cosets
|
187 |
+
raise ValueError("the coset enumeration has defined more than "
|
188 |
+
"%s cosets. Try with a greater value max number of cosets "
|
189 |
+
% self.coset_table_limit)
|
190 |
+
table.append([None]*len(A))
|
191 |
+
# beta is the new coset generated
|
192 |
+
beta = len_table
|
193 |
+
self.p.append(beta)
|
194 |
+
table[alpha][self.A_dict[x]] = beta
|
195 |
+
table[beta][self.A_dict_inv[x]] = alpha
|
196 |
+
# append to deduction stack
|
197 |
+
self.deduction_stack.append((alpha, x))
|
198 |
+
|
199 |
+
def scan_c(self, alpha, word):
|
200 |
+
"""
|
201 |
+
A variation of ``scan`` routine, described on pg. 165 of [1], which
|
202 |
+
puts at tuple, whenever a deduction occurs, to deduction stack.
|
203 |
+
|
204 |
+
See Also
|
205 |
+
========
|
206 |
+
|
207 |
+
scan, scan_check, scan_and_fill, scan_and_fill_c
|
208 |
+
|
209 |
+
"""
|
210 |
+
# alpha is an integer representing a "coset"
|
211 |
+
# since scanning can be in two cases
|
212 |
+
# 1. for alpha=0 and w in Y (i.e generating set of H)
|
213 |
+
# 2. alpha in Omega (set of live cosets), w in R (relators)
|
214 |
+
A_dict = self.A_dict
|
215 |
+
A_dict_inv = self.A_dict_inv
|
216 |
+
table = self.table
|
217 |
+
f = alpha
|
218 |
+
i = 0
|
219 |
+
r = len(word)
|
220 |
+
b = alpha
|
221 |
+
j = r - 1
|
222 |
+
# list of union of generators and their inverses
|
223 |
+
while i <= j and table[f][A_dict[word[i]]] is not None:
|
224 |
+
f = table[f][A_dict[word[i]]]
|
225 |
+
i += 1
|
226 |
+
if i > j:
|
227 |
+
if f != b:
|
228 |
+
self.coincidence_c(f, b)
|
229 |
+
return
|
230 |
+
while j >= i and table[b][A_dict_inv[word[j]]] is not None:
|
231 |
+
b = table[b][A_dict_inv[word[j]]]
|
232 |
+
j -= 1
|
233 |
+
if j < i:
|
234 |
+
# we have an incorrect completed scan with coincidence f ~ b
|
235 |
+
# run the "coincidence" routine
|
236 |
+
self.coincidence_c(f, b)
|
237 |
+
elif j == i:
|
238 |
+
# deduction process
|
239 |
+
table[f][A_dict[word[i]]] = b
|
240 |
+
table[b][A_dict_inv[word[i]]] = f
|
241 |
+
self.deduction_stack.append((f, word[i]))
|
242 |
+
# otherwise scan is incomplete and yields no information
|
243 |
+
|
244 |
+
# alpha, beta coincide, i.e. alpha, beta represent the pair of cosets where
|
245 |
+
# coincidence occurs
|
246 |
+
def coincidence_c(self, alpha, beta):
|
247 |
+
"""
|
248 |
+
A variation of ``coincidence`` routine used in the coset-table based
|
249 |
+
method of coset enumeration. The only difference being on addition of
|
250 |
+
a new coset in coset table(i.e new coset introduction), then it is
|
251 |
+
appended to ``deduction_stack``.
|
252 |
+
|
253 |
+
See Also
|
254 |
+
========
|
255 |
+
|
256 |
+
coincidence
|
257 |
+
|
258 |
+
"""
|
259 |
+
A_dict = self.A_dict
|
260 |
+
A_dict_inv = self.A_dict_inv
|
261 |
+
table = self.table
|
262 |
+
# behaves as a queue
|
263 |
+
q = []
|
264 |
+
self.merge(alpha, beta, q)
|
265 |
+
while len(q) > 0:
|
266 |
+
gamma = q.pop(0)
|
267 |
+
for x in A_dict:
|
268 |
+
delta = table[gamma][A_dict[x]]
|
269 |
+
if delta is not None:
|
270 |
+
table[delta][A_dict_inv[x]] = None
|
271 |
+
# only line of difference from ``coincidence`` routine
|
272 |
+
self.deduction_stack.append((delta, x**-1))
|
273 |
+
mu = self.rep(gamma)
|
274 |
+
nu = self.rep(delta)
|
275 |
+
if table[mu][A_dict[x]] is not None:
|
276 |
+
self.merge(nu, table[mu][A_dict[x]], q)
|
277 |
+
elif table[nu][A_dict_inv[x]] is not None:
|
278 |
+
self.merge(mu, table[nu][A_dict_inv[x]], q)
|
279 |
+
else:
|
280 |
+
table[mu][A_dict[x]] = nu
|
281 |
+
table[nu][A_dict_inv[x]] = mu
|
282 |
+
|
283 |
+
def scan(self, alpha, word, y=None, fill=False, modified=False):
|
284 |
+
r"""
|
285 |
+
``scan`` performs a scanning process on the input ``word``.
|
286 |
+
It first locates the largest prefix ``s`` of ``word`` for which
|
287 |
+
`\alpha^s` is defined (i.e is not ``None``), ``s`` may be empty. Let
|
288 |
+
``word=sv``, let ``t`` be the longest suffix of ``v`` for which
|
289 |
+
`\alpha^{t^{-1}}` is defined, and let ``v=ut``. Then three
|
290 |
+
possibilities are there:
|
291 |
+
|
292 |
+
1. If ``t=v``, then we say that the scan completes, and if, in addition
|
293 |
+
`\alpha^s = \alpha^{t^{-1}}`, then we say that the scan completes
|
294 |
+
correctly.
|
295 |
+
|
296 |
+
2. It can also happen that scan does not complete, but `|u|=1`; that
|
297 |
+
is, the word ``u`` consists of a single generator `x \in A`. In that
|
298 |
+
case, if `\alpha^s = \beta` and `\alpha^{t^{-1}} = \gamma`, then we can
|
299 |
+
set `\beta^x = \gamma` and `\gamma^{x^{-1}} = \beta`. These assignments
|
300 |
+
are known as deductions and enable the scan to complete correctly.
|
301 |
+
|
302 |
+
3. See ``coicidence`` routine for explanation of third condition.
|
303 |
+
|
304 |
+
Notes
|
305 |
+
=====
|
306 |
+
|
307 |
+
The code for the procedure of scanning `\alpha \in \Omega`
|
308 |
+
under `w \in A*` is defined on pg. 155 [1]
|
309 |
+
|
310 |
+
See Also
|
311 |
+
========
|
312 |
+
|
313 |
+
scan_c, scan_check, scan_and_fill, scan_and_fill_c
|
314 |
+
|
315 |
+
Scan and Fill
|
316 |
+
=============
|
317 |
+
|
318 |
+
Performed when the default argument fill=True.
|
319 |
+
|
320 |
+
Modified Scan
|
321 |
+
=============
|
322 |
+
|
323 |
+
Performed when the default argument modified=True
|
324 |
+
|
325 |
+
"""
|
326 |
+
# alpha is an integer representing a "coset"
|
327 |
+
# since scanning can be in two cases
|
328 |
+
# 1. for alpha=0 and w in Y (i.e generating set of H)
|
329 |
+
# 2. alpha in Omega (set of live cosets), w in R (relators)
|
330 |
+
A_dict = self.A_dict
|
331 |
+
A_dict_inv = self.A_dict_inv
|
332 |
+
table = self.table
|
333 |
+
f = alpha
|
334 |
+
i = 0
|
335 |
+
r = len(word)
|
336 |
+
b = alpha
|
337 |
+
j = r - 1
|
338 |
+
b_p = y
|
339 |
+
if modified:
|
340 |
+
f_p = self._grp.identity
|
341 |
+
flag = 0
|
342 |
+
while fill or flag == 0:
|
343 |
+
flag = 1
|
344 |
+
while i <= j and table[f][A_dict[word[i]]] is not None:
|
345 |
+
if modified:
|
346 |
+
f_p = f_p*self.P[f][A_dict[word[i]]]
|
347 |
+
f = table[f][A_dict[word[i]]]
|
348 |
+
i += 1
|
349 |
+
if i > j:
|
350 |
+
if f != b:
|
351 |
+
if modified:
|
352 |
+
self.modified_coincidence(f, b, f_p**-1*y)
|
353 |
+
else:
|
354 |
+
self.coincidence(f, b)
|
355 |
+
return
|
356 |
+
while j >= i and table[b][A_dict_inv[word[j]]] is not None:
|
357 |
+
if modified:
|
358 |
+
b_p = b_p*self.P[b][self.A_dict_inv[word[j]]]
|
359 |
+
b = table[b][A_dict_inv[word[j]]]
|
360 |
+
j -= 1
|
361 |
+
if j < i:
|
362 |
+
# we have an incorrect completed scan with coincidence f ~ b
|
363 |
+
# run the "coincidence" routine
|
364 |
+
if modified:
|
365 |
+
self.modified_coincidence(f, b, f_p**-1*b_p)
|
366 |
+
else:
|
367 |
+
self.coincidence(f, b)
|
368 |
+
elif j == i:
|
369 |
+
# deduction process
|
370 |
+
table[f][A_dict[word[i]]] = b
|
371 |
+
table[b][A_dict_inv[word[i]]] = f
|
372 |
+
if modified:
|
373 |
+
self.P[f][self.A_dict[word[i]]] = f_p**-1*b_p
|
374 |
+
self.P[b][self.A_dict_inv[word[i]]] = b_p**-1*f_p
|
375 |
+
return
|
376 |
+
elif fill:
|
377 |
+
self.define(f, word[i], modified=modified)
|
378 |
+
# otherwise scan is incomplete and yields no information
|
379 |
+
|
380 |
+
# used in the low-index subgroups algorithm
|
381 |
+
def scan_check(self, alpha, word):
|
382 |
+
r"""
|
383 |
+
Another version of ``scan`` routine, described on, it checks whether
|
384 |
+
`\alpha` scans correctly under `word`, it is a straightforward
|
385 |
+
modification of ``scan``. ``scan_check`` returns ``False`` (rather than
|
386 |
+
calling ``coincidence``) if the scan completes incorrectly; otherwise
|
387 |
+
it returns ``True``.
|
388 |
+
|
389 |
+
See Also
|
390 |
+
========
|
391 |
+
|
392 |
+
scan, scan_c, scan_and_fill, scan_and_fill_c
|
393 |
+
|
394 |
+
"""
|
395 |
+
# alpha is an integer representing a "coset"
|
396 |
+
# since scanning can be in two cases
|
397 |
+
# 1. for alpha=0 and w in Y (i.e generating set of H)
|
398 |
+
# 2. alpha in Omega (set of live cosets), w in R (relators)
|
399 |
+
A_dict = self.A_dict
|
400 |
+
A_dict_inv = self.A_dict_inv
|
401 |
+
table = self.table
|
402 |
+
f = alpha
|
403 |
+
i = 0
|
404 |
+
r = len(word)
|
405 |
+
b = alpha
|
406 |
+
j = r - 1
|
407 |
+
while i <= j and table[f][A_dict[word[i]]] is not None:
|
408 |
+
f = table[f][A_dict[word[i]]]
|
409 |
+
i += 1
|
410 |
+
if i > j:
|
411 |
+
return f == b
|
412 |
+
while j >= i and table[b][A_dict_inv[word[j]]] is not None:
|
413 |
+
b = table[b][A_dict_inv[word[j]]]
|
414 |
+
j -= 1
|
415 |
+
if j < i:
|
416 |
+
# we have an incorrect completed scan with coincidence f ~ b
|
417 |
+
# return False, instead of calling coincidence routine
|
418 |
+
return False
|
419 |
+
elif j == i:
|
420 |
+
# deduction process
|
421 |
+
table[f][A_dict[word[i]]] = b
|
422 |
+
table[b][A_dict_inv[word[i]]] = f
|
423 |
+
return True
|
424 |
+
|
425 |
+
def merge(self, k, lamda, q, w=None, modified=False):
|
426 |
+
"""
|
427 |
+
Merge two classes with representatives ``k`` and ``lamda``, described
|
428 |
+
on Pg. 157 [1] (for pseudocode), start by putting ``p[k] = lamda``.
|
429 |
+
It is more efficient to choose the new representative from the larger
|
430 |
+
of the two classes being merged, i.e larger among ``k`` and ``lamda``.
|
431 |
+
procedure ``merge`` performs the merging operation, adds the deleted
|
432 |
+
class representative to the queue ``q``.
|
433 |
+
|
434 |
+
Parameters
|
435 |
+
==========
|
436 |
+
|
437 |
+
'k', 'lamda' being the two class representatives to be merged.
|
438 |
+
|
439 |
+
Notes
|
440 |
+
=====
|
441 |
+
|
442 |
+
Pg. 86-87 [1] contains a description of this method.
|
443 |
+
|
444 |
+
See Also
|
445 |
+
========
|
446 |
+
|
447 |
+
coincidence, rep
|
448 |
+
|
449 |
+
"""
|
450 |
+
p = self.p
|
451 |
+
rep = self.rep
|
452 |
+
phi = rep(k, modified=modified)
|
453 |
+
psi = rep(lamda, modified=modified)
|
454 |
+
if phi != psi:
|
455 |
+
mu = min(phi, psi)
|
456 |
+
v = max(phi, psi)
|
457 |
+
p[v] = mu
|
458 |
+
if modified:
|
459 |
+
if v == phi:
|
460 |
+
self.p_p[phi] = self.p_p[k]**-1*w*self.p_p[lamda]
|
461 |
+
else:
|
462 |
+
self.p_p[psi] = self.p_p[lamda]**-1*w**-1*self.p_p[k]
|
463 |
+
q.append(v)
|
464 |
+
|
465 |
+
def rep(self, k, modified=False):
|
466 |
+
r"""
|
467 |
+
Parameters
|
468 |
+
==========
|
469 |
+
|
470 |
+
`k \in [0 \ldots n-1]`, as for ``self`` only array ``p`` is used
|
471 |
+
|
472 |
+
Returns
|
473 |
+
=======
|
474 |
+
|
475 |
+
Representative of the class containing ``k``.
|
476 |
+
|
477 |
+
Returns the representative of `\sim` class containing ``k``, it also
|
478 |
+
makes some modification to array ``p`` of ``self`` to ease further
|
479 |
+
computations, described on Pg. 157 [1].
|
480 |
+
|
481 |
+
The information on classes under `\sim` is stored in array `p` of
|
482 |
+
``self`` argument, which will always satisfy the property:
|
483 |
+
|
484 |
+
`p[\alpha] \sim \alpha` and `p[\alpha]=\alpha \iff \alpha=rep(\alpha)`
|
485 |
+
`\forall \in [0 \ldots n-1]`.
|
486 |
+
|
487 |
+
So, for `\alpha \in [0 \ldots n-1]`, we find `rep(self, \alpha)` by
|
488 |
+
continually replacing `\alpha` by `p[\alpha]` until it becomes
|
489 |
+
constant (i.e satisfies `p[\alpha] = \alpha`):w
|
490 |
+
|
491 |
+
To increase the efficiency of later ``rep`` calculations, whenever we
|
492 |
+
find `rep(self, \alpha)=\beta`, we set
|
493 |
+
`p[\gamma] = \beta \forall \gamma \in p-chain` from `\alpha` to `\beta`
|
494 |
+
|
495 |
+
Notes
|
496 |
+
=====
|
497 |
+
|
498 |
+
``rep`` routine is also described on Pg. 85-87 [1] in Atkinson's
|
499 |
+
algorithm, this results from the fact that ``coincidence`` routine
|
500 |
+
introduces functionality similar to that introduced by the
|
501 |
+
``minimal_block`` routine on Pg. 85-87 [1].
|
502 |
+
|
503 |
+
See Also
|
504 |
+
========
|
505 |
+
|
506 |
+
coincidence, merge
|
507 |
+
|
508 |
+
"""
|
509 |
+
p = self.p
|
510 |
+
lamda = k
|
511 |
+
rho = p[lamda]
|
512 |
+
if modified:
|
513 |
+
s = p[:]
|
514 |
+
while rho != lamda:
|
515 |
+
if modified:
|
516 |
+
s[rho] = lamda
|
517 |
+
lamda = rho
|
518 |
+
rho = p[lamda]
|
519 |
+
if modified:
|
520 |
+
rho = s[lamda]
|
521 |
+
while rho != k:
|
522 |
+
mu = rho
|
523 |
+
rho = s[mu]
|
524 |
+
p[rho] = lamda
|
525 |
+
self.p_p[rho] = self.p_p[rho]*self.p_p[mu]
|
526 |
+
else:
|
527 |
+
mu = k
|
528 |
+
rho = p[mu]
|
529 |
+
while rho != lamda:
|
530 |
+
p[mu] = lamda
|
531 |
+
mu = rho
|
532 |
+
rho = p[mu]
|
533 |
+
return lamda
|
534 |
+
|
535 |
+
# alpha, beta coincide, i.e. alpha, beta represent the pair of cosets
|
536 |
+
# where coincidence occurs
|
537 |
+
def coincidence(self, alpha, beta, w=None, modified=False):
|
538 |
+
r"""
|
539 |
+
The third situation described in ``scan`` routine is handled by this
|
540 |
+
routine, described on Pg. 156-161 [1].
|
541 |
+
|
542 |
+
The unfortunate situation when the scan completes but not correctly,
|
543 |
+
then ``coincidence`` routine is run. i.e when for some `i` with
|
544 |
+
`1 \le i \le r+1`, we have `w=st` with `s = x_1 x_2 \dots x_{i-1}`,
|
545 |
+
`t = x_i x_{i+1} \dots x_r`, and `\beta = \alpha^s` and
|
546 |
+
`\gamma = \alpha^{t-1}` are defined but unequal. This means that
|
547 |
+
`\beta` and `\gamma` represent the same coset of `H` in `G`. Described
|
548 |
+
on Pg. 156 [1]. ``rep``
|
549 |
+
|
550 |
+
See Also
|
551 |
+
========
|
552 |
+
|
553 |
+
scan
|
554 |
+
|
555 |
+
"""
|
556 |
+
A_dict = self.A_dict
|
557 |
+
A_dict_inv = self.A_dict_inv
|
558 |
+
table = self.table
|
559 |
+
# behaves as a queue
|
560 |
+
q = []
|
561 |
+
if modified:
|
562 |
+
self.modified_merge(alpha, beta, w, q)
|
563 |
+
else:
|
564 |
+
self.merge(alpha, beta, q)
|
565 |
+
while len(q) > 0:
|
566 |
+
gamma = q.pop(0)
|
567 |
+
for x in A_dict:
|
568 |
+
delta = table[gamma][A_dict[x]]
|
569 |
+
if delta is not None:
|
570 |
+
table[delta][A_dict_inv[x]] = None
|
571 |
+
mu = self.rep(gamma, modified=modified)
|
572 |
+
nu = self.rep(delta, modified=modified)
|
573 |
+
if table[mu][A_dict[x]] is not None:
|
574 |
+
if modified:
|
575 |
+
v = self.p_p[delta]**-1*self.P[gamma][self.A_dict[x]]**-1
|
576 |
+
v = v*self.p_p[gamma]*self.P[mu][self.A_dict[x]]
|
577 |
+
self.modified_merge(nu, table[mu][self.A_dict[x]], v, q)
|
578 |
+
else:
|
579 |
+
self.merge(nu, table[mu][A_dict[x]], q)
|
580 |
+
elif table[nu][A_dict_inv[x]] is not None:
|
581 |
+
if modified:
|
582 |
+
v = self.p_p[gamma]**-1*self.P[gamma][self.A_dict[x]]
|
583 |
+
v = v*self.p_p[delta]*self.P[mu][self.A_dict_inv[x]]
|
584 |
+
self.modified_merge(mu, table[nu][self.A_dict_inv[x]], v, q)
|
585 |
+
else:
|
586 |
+
self.merge(mu, table[nu][A_dict_inv[x]], q)
|
587 |
+
else:
|
588 |
+
table[mu][A_dict[x]] = nu
|
589 |
+
table[nu][A_dict_inv[x]] = mu
|
590 |
+
if modified:
|
591 |
+
v = self.p_p[gamma]**-1*self.P[gamma][self.A_dict[x]]*self.p_p[delta]
|
592 |
+
self.P[mu][self.A_dict[x]] = v
|
593 |
+
self.P[nu][self.A_dict_inv[x]] = v**-1
|
594 |
+
|
595 |
+
# method used in the HLT strategy
|
596 |
+
def scan_and_fill(self, alpha, word):
|
597 |
+
"""
|
598 |
+
A modified version of ``scan`` routine used in the relator-based
|
599 |
+
method of coset enumeration, described on pg. 162-163 [1], which
|
600 |
+
follows the idea that whenever the procedure is called and the scan
|
601 |
+
is incomplete then it makes new definitions to enable the scan to
|
602 |
+
complete; i.e it fills in the gaps in the scan of the relator or
|
603 |
+
subgroup generator.
|
604 |
+
|
605 |
+
"""
|
606 |
+
self.scan(alpha, word, fill=True)
|
607 |
+
|
608 |
+
def scan_and_fill_c(self, alpha, word):
|
609 |
+
"""
|
610 |
+
A modified version of ``scan`` routine, described on Pg. 165 second
|
611 |
+
para. [1], with modification similar to that of ``scan_anf_fill`` the
|
612 |
+
only difference being it calls the coincidence procedure used in the
|
613 |
+
coset-table based method i.e. the routine ``coincidence_c`` is used.
|
614 |
+
|
615 |
+
See Also
|
616 |
+
========
|
617 |
+
|
618 |
+
scan, scan_and_fill
|
619 |
+
|
620 |
+
"""
|
621 |
+
A_dict = self.A_dict
|
622 |
+
A_dict_inv = self.A_dict_inv
|
623 |
+
table = self.table
|
624 |
+
r = len(word)
|
625 |
+
f = alpha
|
626 |
+
i = 0
|
627 |
+
b = alpha
|
628 |
+
j = r - 1
|
629 |
+
# loop until it has filled the alpha row in the table.
|
630 |
+
while True:
|
631 |
+
# do the forward scanning
|
632 |
+
while i <= j and table[f][A_dict[word[i]]] is not None:
|
633 |
+
f = table[f][A_dict[word[i]]]
|
634 |
+
i += 1
|
635 |
+
if i > j:
|
636 |
+
if f != b:
|
637 |
+
self.coincidence_c(f, b)
|
638 |
+
return
|
639 |
+
# forward scan was incomplete, scan backwards
|
640 |
+
while j >= i and table[b][A_dict_inv[word[j]]] is not None:
|
641 |
+
b = table[b][A_dict_inv[word[j]]]
|
642 |
+
j -= 1
|
643 |
+
if j < i:
|
644 |
+
self.coincidence_c(f, b)
|
645 |
+
elif j == i:
|
646 |
+
table[f][A_dict[word[i]]] = b
|
647 |
+
table[b][A_dict_inv[word[i]]] = f
|
648 |
+
self.deduction_stack.append((f, word[i]))
|
649 |
+
else:
|
650 |
+
self.define_c(f, word[i])
|
651 |
+
|
652 |
+
# method used in the HLT strategy
|
653 |
+
def look_ahead(self):
|
654 |
+
"""
|
655 |
+
When combined with the HLT method this is known as HLT+Lookahead
|
656 |
+
method of coset enumeration, described on pg. 164 [1]. Whenever
|
657 |
+
``define`` aborts due to lack of space available this procedure is
|
658 |
+
executed. This routine helps in recovering space resulting from
|
659 |
+
"coincidence" of cosets.
|
660 |
+
|
661 |
+
"""
|
662 |
+
R = self.fp_group.relators
|
663 |
+
p = self.p
|
664 |
+
# complete scan all relators under all cosets(obviously live)
|
665 |
+
# without making new definitions
|
666 |
+
for beta in self.omega:
|
667 |
+
for w in R:
|
668 |
+
self.scan(beta, w)
|
669 |
+
if p[beta] < beta:
|
670 |
+
break
|
671 |
+
|
672 |
+
# Pg. 166
|
673 |
+
def process_deductions(self, R_c_x, R_c_x_inv):
|
674 |
+
"""
|
675 |
+
Processes the deductions that have been pushed onto ``deduction_stack``,
|
676 |
+
described on Pg. 166 [1] and is used in coset-table based enumeration.
|
677 |
+
|
678 |
+
See Also
|
679 |
+
========
|
680 |
+
|
681 |
+
deduction_stack
|
682 |
+
|
683 |
+
"""
|
684 |
+
p = self.p
|
685 |
+
table = self.table
|
686 |
+
while len(self.deduction_stack) > 0:
|
687 |
+
if len(self.deduction_stack) >= CosetTable.max_stack_size:
|
688 |
+
self.look_ahead()
|
689 |
+
del self.deduction_stack[:]
|
690 |
+
continue
|
691 |
+
else:
|
692 |
+
alpha, x = self.deduction_stack.pop()
|
693 |
+
if p[alpha] == alpha:
|
694 |
+
for w in R_c_x:
|
695 |
+
self.scan_c(alpha, w)
|
696 |
+
if p[alpha] < alpha:
|
697 |
+
break
|
698 |
+
beta = table[alpha][self.A_dict[x]]
|
699 |
+
if beta is not None and p[beta] == beta:
|
700 |
+
for w in R_c_x_inv:
|
701 |
+
self.scan_c(beta, w)
|
702 |
+
if p[beta] < beta:
|
703 |
+
break
|
704 |
+
|
705 |
+
def process_deductions_check(self, R_c_x, R_c_x_inv):
|
706 |
+
"""
|
707 |
+
A variation of ``process_deductions``, this calls ``scan_check``
|
708 |
+
wherever ``process_deductions`` calls ``scan``, described on Pg. [1].
|
709 |
+
|
710 |
+
See Also
|
711 |
+
========
|
712 |
+
|
713 |
+
process_deductions
|
714 |
+
|
715 |
+
"""
|
716 |
+
table = self.table
|
717 |
+
while len(self.deduction_stack) > 0:
|
718 |
+
alpha, x = self.deduction_stack.pop()
|
719 |
+
for w in R_c_x:
|
720 |
+
if not self.scan_check(alpha, w):
|
721 |
+
return False
|
722 |
+
beta = table[alpha][self.A_dict[x]]
|
723 |
+
if beta is not None:
|
724 |
+
for w in R_c_x_inv:
|
725 |
+
if not self.scan_check(beta, w):
|
726 |
+
return False
|
727 |
+
return True
|
728 |
+
|
729 |
+
def switch(self, beta, gamma):
|
730 |
+
r"""Switch the elements `\beta, \gamma \in \Omega` of ``self``, used
|
731 |
+
by the ``standardize`` procedure, described on Pg. 167 [1].
|
732 |
+
|
733 |
+
See Also
|
734 |
+
========
|
735 |
+
|
736 |
+
standardize
|
737 |
+
|
738 |
+
"""
|
739 |
+
A = self.A
|
740 |
+
A_dict = self.A_dict
|
741 |
+
table = self.table
|
742 |
+
for x in A:
|
743 |
+
z = table[gamma][A_dict[x]]
|
744 |
+
table[gamma][A_dict[x]] = table[beta][A_dict[x]]
|
745 |
+
table[beta][A_dict[x]] = z
|
746 |
+
for alpha in range(len(self.p)):
|
747 |
+
if self.p[alpha] == alpha:
|
748 |
+
if table[alpha][A_dict[x]] == beta:
|
749 |
+
table[alpha][A_dict[x]] = gamma
|
750 |
+
elif table[alpha][A_dict[x]] == gamma:
|
751 |
+
table[alpha][A_dict[x]] = beta
|
752 |
+
|
753 |
+
def standardize(self):
|
754 |
+
r"""
|
755 |
+
A coset table is standardized if when running through the cosets and
|
756 |
+
within each coset through the generator images (ignoring generator
|
757 |
+
inverses), the cosets appear in order of the integers
|
758 |
+
`0, 1, \dots, n`. "Standardize" reorders the elements of `\Omega`
|
759 |
+
such that, if we scan the coset table first by elements of `\Omega`
|
760 |
+
and then by elements of A, then the cosets occur in ascending order.
|
761 |
+
``standardize()`` is used at the end of an enumeration to permute the
|
762 |
+
cosets so that they occur in some sort of standard order.
|
763 |
+
|
764 |
+
Notes
|
765 |
+
=====
|
766 |
+
|
767 |
+
procedure is described on pg. 167-168 [1], it also makes use of the
|
768 |
+
``switch`` routine to replace by smaller integer value.
|
769 |
+
|
770 |
+
Examples
|
771 |
+
========
|
772 |
+
|
773 |
+
>>> from sympy.combinatorics import free_group
|
774 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r
|
775 |
+
>>> F, x, y = free_group("x, y")
|
776 |
+
|
777 |
+
# Example 5.3 from [1]
|
778 |
+
>>> f = FpGroup(F, [x**2*y**2, x**3*y**5])
|
779 |
+
>>> C = coset_enumeration_r(f, [])
|
780 |
+
>>> C.compress()
|
781 |
+
>>> C.table
|
782 |
+
[[1, 3, 1, 3], [2, 0, 2, 0], [3, 1, 3, 1], [0, 2, 0, 2]]
|
783 |
+
>>> C.standardize()
|
784 |
+
>>> C.table
|
785 |
+
[[1, 2, 1, 2], [3, 0, 3, 0], [0, 3, 0, 3], [2, 1, 2, 1]]
|
786 |
+
|
787 |
+
"""
|
788 |
+
A = self.A
|
789 |
+
A_dict = self.A_dict
|
790 |
+
gamma = 1
|
791 |
+
for alpha, x in product(range(self.n), A):
|
792 |
+
beta = self.table[alpha][A_dict[x]]
|
793 |
+
if beta >= gamma:
|
794 |
+
if beta > gamma:
|
795 |
+
self.switch(gamma, beta)
|
796 |
+
gamma += 1
|
797 |
+
if gamma == self.n:
|
798 |
+
return
|
799 |
+
|
800 |
+
# Compression of a Coset Table
|
801 |
+
def compress(self):
|
802 |
+
"""Removes the non-live cosets from the coset table, described on
|
803 |
+
pg. 167 [1].
|
804 |
+
|
805 |
+
"""
|
806 |
+
gamma = -1
|
807 |
+
A = self.A
|
808 |
+
A_dict = self.A_dict
|
809 |
+
A_dict_inv = self.A_dict_inv
|
810 |
+
table = self.table
|
811 |
+
chi = tuple([i for i in range(len(self.p)) if self.p[i] != i])
|
812 |
+
for alpha in self.omega:
|
813 |
+
gamma += 1
|
814 |
+
if gamma != alpha:
|
815 |
+
# replace alpha by gamma in coset table
|
816 |
+
for x in A:
|
817 |
+
beta = table[alpha][A_dict[x]]
|
818 |
+
table[gamma][A_dict[x]] = beta
|
819 |
+
table[beta][A_dict_inv[x]] == gamma
|
820 |
+
# all the cosets in the table are live cosets
|
821 |
+
self.p = list(range(gamma + 1))
|
822 |
+
# delete the useless columns
|
823 |
+
del table[len(self.p):]
|
824 |
+
# re-define values
|
825 |
+
for row in table:
|
826 |
+
for j in range(len(self.A)):
|
827 |
+
row[j] -= bisect_left(chi, row[j])
|
828 |
+
|
829 |
+
def conjugates(self, R):
|
830 |
+
R_c = list(chain.from_iterable((rel.cyclic_conjugates(), \
|
831 |
+
(rel**-1).cyclic_conjugates()) for rel in R))
|
832 |
+
R_set = set()
|
833 |
+
for conjugate in R_c:
|
834 |
+
R_set = R_set.union(conjugate)
|
835 |
+
R_c_list = []
|
836 |
+
for x in self.A:
|
837 |
+
r = {word for word in R_set if word[0] == x}
|
838 |
+
R_c_list.append(r)
|
839 |
+
R_set.difference_update(r)
|
840 |
+
return R_c_list
|
841 |
+
|
842 |
+
def coset_representative(self, coset):
|
843 |
+
'''
|
844 |
+
Compute the coset representative of a given coset.
|
845 |
+
|
846 |
+
Examples
|
847 |
+
========
|
848 |
+
|
849 |
+
>>> from sympy.combinatorics import free_group
|
850 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r
|
851 |
+
>>> F, x, y = free_group("x, y")
|
852 |
+
>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
|
853 |
+
>>> C = coset_enumeration_r(f, [x])
|
854 |
+
>>> C.compress()
|
855 |
+
>>> C.table
|
856 |
+
[[0, 0, 1, 2], [1, 1, 2, 0], [2, 2, 0, 1]]
|
857 |
+
>>> C.coset_representative(0)
|
858 |
+
<identity>
|
859 |
+
>>> C.coset_representative(1)
|
860 |
+
y
|
861 |
+
>>> C.coset_representative(2)
|
862 |
+
y**-1
|
863 |
+
|
864 |
+
'''
|
865 |
+
for x in self.A:
|
866 |
+
gamma = self.table[coset][self.A_dict[x]]
|
867 |
+
if coset == 0:
|
868 |
+
return self.fp_group.identity
|
869 |
+
if gamma < coset:
|
870 |
+
return self.coset_representative(gamma)*x**-1
|
871 |
+
|
872 |
+
##############################
|
873 |
+
# Modified Methods #
|
874 |
+
##############################
|
875 |
+
|
876 |
+
def modified_define(self, alpha, x):
|
877 |
+
r"""
|
878 |
+
Define a function p_p from from [1..n] to A* as
|
879 |
+
an additional component of the modified coset table.
|
880 |
+
|
881 |
+
Parameters
|
882 |
+
==========
|
883 |
+
|
884 |
+
\alpha \in \Omega
|
885 |
+
x \in A*
|
886 |
+
|
887 |
+
See Also
|
888 |
+
========
|
889 |
+
|
890 |
+
define
|
891 |
+
|
892 |
+
"""
|
893 |
+
self.define(alpha, x, modified=True)
|
894 |
+
|
895 |
+
def modified_scan(self, alpha, w, y, fill=False):
|
896 |
+
r"""
|
897 |
+
Parameters
|
898 |
+
==========
|
899 |
+
\alpha \in \Omega
|
900 |
+
w \in A*
|
901 |
+
y \in (YUY^-1)
|
902 |
+
fill -- `modified_scan_and_fill` when set to True.
|
903 |
+
|
904 |
+
See Also
|
905 |
+
========
|
906 |
+
|
907 |
+
scan
|
908 |
+
"""
|
909 |
+
self.scan(alpha, w, y=y, fill=fill, modified=True)
|
910 |
+
|
911 |
+
def modified_scan_and_fill(self, alpha, w, y):
|
912 |
+
self.modified_scan(alpha, w, y, fill=True)
|
913 |
+
|
914 |
+
def modified_merge(self, k, lamda, w, q):
|
915 |
+
r"""
|
916 |
+
Parameters
|
917 |
+
==========
|
918 |
+
|
919 |
+
'k', 'lamda' -- the two class representatives to be merged.
|
920 |
+
q -- queue of length l of elements to be deleted from `\Omega` *.
|
921 |
+
w -- Word in (YUY^-1)
|
922 |
+
|
923 |
+
See Also
|
924 |
+
========
|
925 |
+
|
926 |
+
merge
|
927 |
+
"""
|
928 |
+
self.merge(k, lamda, q, w=w, modified=True)
|
929 |
+
|
930 |
+
def modified_rep(self, k):
|
931 |
+
r"""
|
932 |
+
Parameters
|
933 |
+
==========
|
934 |
+
|
935 |
+
`k \in [0 \ldots n-1]`
|
936 |
+
|
937 |
+
See Also
|
938 |
+
========
|
939 |
+
|
940 |
+
rep
|
941 |
+
"""
|
942 |
+
self.rep(k, modified=True)
|
943 |
+
|
944 |
+
def modified_coincidence(self, alpha, beta, w):
|
945 |
+
r"""
|
946 |
+
Parameters
|
947 |
+
==========
|
948 |
+
|
949 |
+
A coincident pair `\alpha, \beta \in \Omega, w \in Y \cup Y^{-1}`
|
950 |
+
|
951 |
+
See Also
|
952 |
+
========
|
953 |
+
|
954 |
+
coincidence
|
955 |
+
|
956 |
+
"""
|
957 |
+
self.coincidence(alpha, beta, w=w, modified=True)
|
958 |
+
|
959 |
+
###############################################################################
|
960 |
+
# COSET ENUMERATION #
|
961 |
+
###############################################################################
|
962 |
+
|
963 |
+
# relator-based method
|
964 |
+
def coset_enumeration_r(fp_grp, Y, max_cosets=None, draft=None,
|
965 |
+
incomplete=False, modified=False):
|
966 |
+
"""
|
967 |
+
This is easier of the two implemented methods of coset enumeration.
|
968 |
+
and is often called the HLT method, after Hazelgrove, Leech, Trotter
|
969 |
+
The idea is that we make use of ``scan_and_fill`` makes new definitions
|
970 |
+
whenever the scan is incomplete to enable the scan to complete; this way
|
971 |
+
we fill in the gaps in the scan of the relator or subgroup generator,
|
972 |
+
that's why the name relator-based method.
|
973 |
+
|
974 |
+
An instance of `CosetTable` for `fp_grp` can be passed as the keyword
|
975 |
+
argument `draft` in which case the coset enumeration will start with
|
976 |
+
that instance and attempt to complete it.
|
977 |
+
|
978 |
+
When `incomplete` is `True` and the function is unable to complete for
|
979 |
+
some reason, the partially complete table will be returned.
|
980 |
+
|
981 |
+
# TODO: complete the docstring
|
982 |
+
|
983 |
+
See Also
|
984 |
+
========
|
985 |
+
|
986 |
+
scan_and_fill,
|
987 |
+
|
988 |
+
Examples
|
989 |
+
========
|
990 |
+
|
991 |
+
>>> from sympy.combinatorics.free_groups import free_group
|
992 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r
|
993 |
+
>>> F, x, y = free_group("x, y")
|
994 |
+
|
995 |
+
# Example 5.1 from [1]
|
996 |
+
>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
|
997 |
+
>>> C = coset_enumeration_r(f, [x])
|
998 |
+
>>> for i in range(len(C.p)):
|
999 |
+
... if C.p[i] == i:
|
1000 |
+
... print(C.table[i])
|
1001 |
+
[0, 0, 1, 2]
|
1002 |
+
[1, 1, 2, 0]
|
1003 |
+
[2, 2, 0, 1]
|
1004 |
+
>>> C.p
|
1005 |
+
[0, 1, 2, 1, 1]
|
1006 |
+
|
1007 |
+
# Example from exercises Q2 [1]
|
1008 |
+
>>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3])
|
1009 |
+
>>> C = coset_enumeration_r(f, [])
|
1010 |
+
>>> C.compress(); C.standardize()
|
1011 |
+
>>> C.table
|
1012 |
+
[[1, 2, 3, 4],
|
1013 |
+
[5, 0, 6, 7],
|
1014 |
+
[0, 5, 7, 6],
|
1015 |
+
[7, 6, 5, 0],
|
1016 |
+
[6, 7, 0, 5],
|
1017 |
+
[2, 1, 4, 3],
|
1018 |
+
[3, 4, 2, 1],
|
1019 |
+
[4, 3, 1, 2]]
|
1020 |
+
|
1021 |
+
# Example 5.2
|
1022 |
+
>>> f = FpGroup(F, [x**2, y**3, (x*y)**3])
|
1023 |
+
>>> Y = [x*y]
|
1024 |
+
>>> C = coset_enumeration_r(f, Y)
|
1025 |
+
>>> for i in range(len(C.p)):
|
1026 |
+
... if C.p[i] == i:
|
1027 |
+
... print(C.table[i])
|
1028 |
+
[1, 1, 2, 1]
|
1029 |
+
[0, 0, 0, 2]
|
1030 |
+
[3, 3, 1, 0]
|
1031 |
+
[2, 2, 3, 3]
|
1032 |
+
|
1033 |
+
# Example 5.3
|
1034 |
+
>>> f = FpGroup(F, [x**2*y**2, x**3*y**5])
|
1035 |
+
>>> Y = []
|
1036 |
+
>>> C = coset_enumeration_r(f, Y)
|
1037 |
+
>>> for i in range(len(C.p)):
|
1038 |
+
... if C.p[i] == i:
|
1039 |
+
... print(C.table[i])
|
1040 |
+
[1, 3, 1, 3]
|
1041 |
+
[2, 0, 2, 0]
|
1042 |
+
[3, 1, 3, 1]
|
1043 |
+
[0, 2, 0, 2]
|
1044 |
+
|
1045 |
+
# Example 5.4
|
1046 |
+
>>> F, a, b, c, d, e = free_group("a, b, c, d, e")
|
1047 |
+
>>> f = FpGroup(F, [a*b*c**-1, b*c*d**-1, c*d*e**-1, d*e*a**-1, e*a*b**-1])
|
1048 |
+
>>> Y = [a]
|
1049 |
+
>>> C = coset_enumeration_r(f, Y)
|
1050 |
+
>>> for i in range(len(C.p)):
|
1051 |
+
... if C.p[i] == i:
|
1052 |
+
... print(C.table[i])
|
1053 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
1054 |
+
|
1055 |
+
# example of "compress" method
|
1056 |
+
>>> C.compress()
|
1057 |
+
>>> C.table
|
1058 |
+
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
|
1059 |
+
|
1060 |
+
# Exercises Pg. 161, Q2.
|
1061 |
+
>>> F, x, y = free_group("x, y")
|
1062 |
+
>>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3])
|
1063 |
+
>>> Y = []
|
1064 |
+
>>> C = coset_enumeration_r(f, Y)
|
1065 |
+
>>> C.compress()
|
1066 |
+
>>> C.standardize()
|
1067 |
+
>>> C.table
|
1068 |
+
[[1, 2, 3, 4],
|
1069 |
+
[5, 0, 6, 7],
|
1070 |
+
[0, 5, 7, 6],
|
1071 |
+
[7, 6, 5, 0],
|
1072 |
+
[6, 7, 0, 5],
|
1073 |
+
[2, 1, 4, 3],
|
1074 |
+
[3, 4, 2, 1],
|
1075 |
+
[4, 3, 1, 2]]
|
1076 |
+
|
1077 |
+
# John J. Cannon; Lucien A. Dimino; George Havas; Jane M. Watson
|
1078 |
+
# Mathematics of Computation, Vol. 27, No. 123. (Jul., 1973), pp. 463-490
|
1079 |
+
# from 1973chwd.pdf
|
1080 |
+
# Table 1. Ex. 1
|
1081 |
+
>>> F, r, s, t = free_group("r, s, t")
|
1082 |
+
>>> E1 = FpGroup(F, [t**-1*r*t*r**-2, r**-1*s*r*s**-2, s**-1*t*s*t**-2])
|
1083 |
+
>>> C = coset_enumeration_r(E1, [r])
|
1084 |
+
>>> for i in range(len(C.p)):
|
1085 |
+
... if C.p[i] == i:
|
1086 |
+
... print(C.table[i])
|
1087 |
+
[0, 0, 0, 0, 0, 0]
|
1088 |
+
|
1089 |
+
Ex. 2
|
1090 |
+
>>> F, a, b = free_group("a, b")
|
1091 |
+
>>> Cox = FpGroup(F, [a**6, b**6, (a*b)**2, (a**2*b**2)**2, (a**3*b**3)**5])
|
1092 |
+
>>> C = coset_enumeration_r(Cox, [a])
|
1093 |
+
>>> index = 0
|
1094 |
+
>>> for i in range(len(C.p)):
|
1095 |
+
... if C.p[i] == i:
|
1096 |
+
... index += 1
|
1097 |
+
>>> index
|
1098 |
+
500
|
1099 |
+
|
1100 |
+
# Ex. 3
|
1101 |
+
>>> F, a, b = free_group("a, b")
|
1102 |
+
>>> B_2_4 = FpGroup(F, [a**4, b**4, (a*b)**4, (a**-1*b)**4, (a**2*b)**4, \
|
1103 |
+
(a*b**2)**4, (a**2*b**2)**4, (a**-1*b*a*b)**4, (a*b**-1*a*b)**4])
|
1104 |
+
>>> C = coset_enumeration_r(B_2_4, [a])
|
1105 |
+
>>> index = 0
|
1106 |
+
>>> for i in range(len(C.p)):
|
1107 |
+
... if C.p[i] == i:
|
1108 |
+
... index += 1
|
1109 |
+
>>> index
|
1110 |
+
1024
|
1111 |
+
|
1112 |
+
References
|
1113 |
+
==========
|
1114 |
+
|
1115 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.
|
1116 |
+
"Handbook of computational group theory"
|
1117 |
+
|
1118 |
+
"""
|
1119 |
+
# 1. Initialize a coset table C for < X|R >
|
1120 |
+
C = CosetTable(fp_grp, Y, max_cosets=max_cosets)
|
1121 |
+
# Define coset table methods.
|
1122 |
+
if modified:
|
1123 |
+
_scan_and_fill = C.modified_scan_and_fill
|
1124 |
+
_define = C.modified_define
|
1125 |
+
else:
|
1126 |
+
_scan_and_fill = C.scan_and_fill
|
1127 |
+
_define = C.define
|
1128 |
+
if draft:
|
1129 |
+
C.table = draft.table[:]
|
1130 |
+
C.p = draft.p[:]
|
1131 |
+
R = fp_grp.relators
|
1132 |
+
A_dict = C.A_dict
|
1133 |
+
p = C.p
|
1134 |
+
for i in range(len(Y)):
|
1135 |
+
if modified:
|
1136 |
+
_scan_and_fill(0, Y[i], C._grp.generators[i])
|
1137 |
+
else:
|
1138 |
+
_scan_and_fill(0, Y[i])
|
1139 |
+
alpha = 0
|
1140 |
+
while alpha < C.n:
|
1141 |
+
if p[alpha] == alpha:
|
1142 |
+
try:
|
1143 |
+
for w in R:
|
1144 |
+
if modified:
|
1145 |
+
_scan_and_fill(alpha, w, C._grp.identity)
|
1146 |
+
else:
|
1147 |
+
_scan_and_fill(alpha, w)
|
1148 |
+
# if alpha was eliminated during the scan then break
|
1149 |
+
if p[alpha] < alpha:
|
1150 |
+
break
|
1151 |
+
if p[alpha] == alpha:
|
1152 |
+
for x in A_dict:
|
1153 |
+
if C.table[alpha][A_dict[x]] is None:
|
1154 |
+
_define(alpha, x)
|
1155 |
+
except ValueError as e:
|
1156 |
+
if incomplete:
|
1157 |
+
return C
|
1158 |
+
raise e
|
1159 |
+
alpha += 1
|
1160 |
+
return C
|
1161 |
+
|
1162 |
+
def modified_coset_enumeration_r(fp_grp, Y, max_cosets=None, draft=None,
|
1163 |
+
incomplete=False):
|
1164 |
+
r"""
|
1165 |
+
Introduce a new set of symbols y \in Y that correspond to the
|
1166 |
+
generators of the subgroup. Store the elements of Y as a
|
1167 |
+
word P[\alpha, x] and compute the coset table similar to that of
|
1168 |
+
the regular coset enumeration methods.
|
1169 |
+
|
1170 |
+
Examples
|
1171 |
+
========
|
1172 |
+
|
1173 |
+
>>> from sympy.combinatorics.free_groups import free_group
|
1174 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup
|
1175 |
+
>>> from sympy.combinatorics.coset_table import modified_coset_enumeration_r
|
1176 |
+
>>> F, x, y = free_group("x, y")
|
1177 |
+
>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
|
1178 |
+
>>> C = modified_coset_enumeration_r(f, [x])
|
1179 |
+
>>> C.table
|
1180 |
+
[[0, 0, 1, 2], [1, 1, 2, 0], [2, 2, 0, 1], [None, 1, None, None], [1, 3, None, None]]
|
1181 |
+
|
1182 |
+
See Also
|
1183 |
+
========
|
1184 |
+
|
1185 |
+
coset_enumertation_r
|
1186 |
+
|
1187 |
+
References
|
1188 |
+
==========
|
1189 |
+
|
1190 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.,
|
1191 |
+
"Handbook of Computational Group Theory",
|
1192 |
+
Section 5.3.2
|
1193 |
+
"""
|
1194 |
+
return coset_enumeration_r(fp_grp, Y, max_cosets=max_cosets, draft=draft,
|
1195 |
+
incomplete=incomplete, modified=True)
|
1196 |
+
|
1197 |
+
# Pg. 166
|
1198 |
+
# coset-table based method
|
1199 |
+
def coset_enumeration_c(fp_grp, Y, max_cosets=None, draft=None,
|
1200 |
+
incomplete=False):
|
1201 |
+
"""
|
1202 |
+
>>> from sympy.combinatorics.free_groups import free_group
|
1203 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_c
|
1204 |
+
>>> F, x, y = free_group("x, y")
|
1205 |
+
>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
|
1206 |
+
>>> C = coset_enumeration_c(f, [x])
|
1207 |
+
>>> C.table
|
1208 |
+
[[0, 0, 1, 2], [1, 1, 2, 0], [2, 2, 0, 1]]
|
1209 |
+
|
1210 |
+
"""
|
1211 |
+
# Initialize a coset table C for < X|R >
|
1212 |
+
X = fp_grp.generators
|
1213 |
+
R = fp_grp.relators
|
1214 |
+
C = CosetTable(fp_grp, Y, max_cosets=max_cosets)
|
1215 |
+
if draft:
|
1216 |
+
C.table = draft.table[:]
|
1217 |
+
C.p = draft.p[:]
|
1218 |
+
C.deduction_stack = draft.deduction_stack
|
1219 |
+
for alpha, x in product(range(len(C.table)), X):
|
1220 |
+
if C.table[alpha][C.A_dict[x]] is not None:
|
1221 |
+
C.deduction_stack.append((alpha, x))
|
1222 |
+
A = C.A
|
1223 |
+
# replace all the elements by cyclic reductions
|
1224 |
+
R_cyc_red = [rel.identity_cyclic_reduction() for rel in R]
|
1225 |
+
R_c = list(chain.from_iterable((rel.cyclic_conjugates(), (rel**-1).cyclic_conjugates()) \
|
1226 |
+
for rel in R_cyc_red))
|
1227 |
+
R_set = set()
|
1228 |
+
for conjugate in R_c:
|
1229 |
+
R_set = R_set.union(conjugate)
|
1230 |
+
# a list of subsets of R_c whose words start with "x".
|
1231 |
+
R_c_list = []
|
1232 |
+
for x in C.A:
|
1233 |
+
r = {word for word in R_set if word[0] == x}
|
1234 |
+
R_c_list.append(r)
|
1235 |
+
R_set.difference_update(r)
|
1236 |
+
for w in Y:
|
1237 |
+
C.scan_and_fill_c(0, w)
|
1238 |
+
for x in A:
|
1239 |
+
C.process_deductions(R_c_list[C.A_dict[x]], R_c_list[C.A_dict_inv[x]])
|
1240 |
+
alpha = 0
|
1241 |
+
while alpha < len(C.table):
|
1242 |
+
if C.p[alpha] == alpha:
|
1243 |
+
try:
|
1244 |
+
for x in C.A:
|
1245 |
+
if C.p[alpha] != alpha:
|
1246 |
+
break
|
1247 |
+
if C.table[alpha][C.A_dict[x]] is None:
|
1248 |
+
C.define_c(alpha, x)
|
1249 |
+
C.process_deductions(R_c_list[C.A_dict[x]], R_c_list[C.A_dict_inv[x]])
|
1250 |
+
except ValueError as e:
|
1251 |
+
if incomplete:
|
1252 |
+
return C
|
1253 |
+
raise e
|
1254 |
+
alpha += 1
|
1255 |
+
return C
|
venv/lib/python3.10/site-packages/sympy/combinatorics/fp_groups.py
ADDED
@@ -0,0 +1,1348 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Finitely Presented Groups and its algorithms. """
|
2 |
+
|
3 |
+
from sympy.core.singleton import S
|
4 |
+
from sympy.core.symbol import symbols
|
5 |
+
from sympy.combinatorics.free_groups import (FreeGroup, FreeGroupElement,
|
6 |
+
free_group)
|
7 |
+
from sympy.combinatorics.rewritingsystem import RewritingSystem
|
8 |
+
from sympy.combinatorics.coset_table import (CosetTable,
|
9 |
+
coset_enumeration_r,
|
10 |
+
coset_enumeration_c)
|
11 |
+
from sympy.combinatorics import PermutationGroup
|
12 |
+
from sympy.matrices.normalforms import invariant_factors
|
13 |
+
from sympy.matrices import Matrix
|
14 |
+
from sympy.polys.polytools import gcd
|
15 |
+
from sympy.printing.defaults import DefaultPrinting
|
16 |
+
from sympy.utilities import public
|
17 |
+
from sympy.utilities.magic import pollute
|
18 |
+
|
19 |
+
from itertools import product
|
20 |
+
|
21 |
+
|
22 |
+
@public
|
23 |
+
def fp_group(fr_grp, relators=()):
|
24 |
+
_fp_group = FpGroup(fr_grp, relators)
|
25 |
+
return (_fp_group,) + tuple(_fp_group._generators)
|
26 |
+
|
27 |
+
@public
|
28 |
+
def xfp_group(fr_grp, relators=()):
|
29 |
+
_fp_group = FpGroup(fr_grp, relators)
|
30 |
+
return (_fp_group, _fp_group._generators)
|
31 |
+
|
32 |
+
# Does not work. Both symbols and pollute are undefined. Never tested.
|
33 |
+
@public
|
34 |
+
def vfp_group(fr_grpm, relators):
|
35 |
+
_fp_group = FpGroup(symbols, relators)
|
36 |
+
pollute([sym.name for sym in _fp_group.symbols], _fp_group.generators)
|
37 |
+
return _fp_group
|
38 |
+
|
39 |
+
|
40 |
+
def _parse_relators(rels):
|
41 |
+
"""Parse the passed relators."""
|
42 |
+
return rels
|
43 |
+
|
44 |
+
|
45 |
+
###############################################################################
|
46 |
+
# FINITELY PRESENTED GROUPS #
|
47 |
+
###############################################################################
|
48 |
+
|
49 |
+
|
50 |
+
class FpGroup(DefaultPrinting):
|
51 |
+
"""
|
52 |
+
The FpGroup would take a FreeGroup and a list/tuple of relators, the
|
53 |
+
relators would be specified in such a way that each of them be equal to the
|
54 |
+
identity of the provided free group.
|
55 |
+
|
56 |
+
"""
|
57 |
+
is_group = True
|
58 |
+
is_FpGroup = True
|
59 |
+
is_PermutationGroup = False
|
60 |
+
|
61 |
+
def __init__(self, fr_grp, relators):
|
62 |
+
relators = _parse_relators(relators)
|
63 |
+
self.free_group = fr_grp
|
64 |
+
self.relators = relators
|
65 |
+
self.generators = self._generators()
|
66 |
+
self.dtype = type("FpGroupElement", (FpGroupElement,), {"group": self})
|
67 |
+
|
68 |
+
# CosetTable instance on identity subgroup
|
69 |
+
self._coset_table = None
|
70 |
+
# returns whether coset table on identity subgroup
|
71 |
+
# has been standardized
|
72 |
+
self._is_standardized = False
|
73 |
+
|
74 |
+
self._order = None
|
75 |
+
self._center = None
|
76 |
+
|
77 |
+
self._rewriting_system = RewritingSystem(self)
|
78 |
+
self._perm_isomorphism = None
|
79 |
+
return
|
80 |
+
|
81 |
+
def _generators(self):
|
82 |
+
return self.free_group.generators
|
83 |
+
|
84 |
+
def make_confluent(self):
|
85 |
+
'''
|
86 |
+
Try to make the group's rewriting system confluent
|
87 |
+
|
88 |
+
'''
|
89 |
+
self._rewriting_system.make_confluent()
|
90 |
+
return
|
91 |
+
|
92 |
+
def reduce(self, word):
|
93 |
+
'''
|
94 |
+
Return the reduced form of `word` in `self` according to the group's
|
95 |
+
rewriting system. If it's confluent, the reduced form is the unique normal
|
96 |
+
form of the word in the group.
|
97 |
+
|
98 |
+
'''
|
99 |
+
return self._rewriting_system.reduce(word)
|
100 |
+
|
101 |
+
def equals(self, word1, word2):
|
102 |
+
'''
|
103 |
+
Compare `word1` and `word2` for equality in the group
|
104 |
+
using the group's rewriting system. If the system is
|
105 |
+
confluent, the returned answer is necessarily correct.
|
106 |
+
(If it is not, `False` could be returned in some cases
|
107 |
+
where in fact `word1 == word2`)
|
108 |
+
|
109 |
+
'''
|
110 |
+
if self.reduce(word1*word2**-1) == self.identity:
|
111 |
+
return True
|
112 |
+
elif self._rewriting_system.is_confluent:
|
113 |
+
return False
|
114 |
+
return None
|
115 |
+
|
116 |
+
@property
|
117 |
+
def identity(self):
|
118 |
+
return self.free_group.identity
|
119 |
+
|
120 |
+
def __contains__(self, g):
|
121 |
+
return g in self.free_group
|
122 |
+
|
123 |
+
def subgroup(self, gens, C=None, homomorphism=False):
|
124 |
+
'''
|
125 |
+
Return the subgroup generated by `gens` using the
|
126 |
+
Reidemeister-Schreier algorithm
|
127 |
+
homomorphism -- When set to True, return a dictionary containing the images
|
128 |
+
of the presentation generators in the original group.
|
129 |
+
|
130 |
+
Examples
|
131 |
+
========
|
132 |
+
|
133 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup
|
134 |
+
>>> from sympy.combinatorics import free_group
|
135 |
+
>>> F, x, y = free_group("x, y")
|
136 |
+
>>> f = FpGroup(F, [x**3, y**5, (x*y)**2])
|
137 |
+
>>> H = [x*y, x**-1*y**-1*x*y*x]
|
138 |
+
>>> K, T = f.subgroup(H, homomorphism=True)
|
139 |
+
>>> T(K.generators)
|
140 |
+
[x*y, x**-1*y**2*x**-1]
|
141 |
+
|
142 |
+
'''
|
143 |
+
|
144 |
+
if not all(isinstance(g, FreeGroupElement) for g in gens):
|
145 |
+
raise ValueError("Generators must be `FreeGroupElement`s")
|
146 |
+
if not all(g.group == self.free_group for g in gens):
|
147 |
+
raise ValueError("Given generators are not members of the group")
|
148 |
+
if homomorphism:
|
149 |
+
g, rels, _gens = reidemeister_presentation(self, gens, C=C, homomorphism=True)
|
150 |
+
else:
|
151 |
+
g, rels = reidemeister_presentation(self, gens, C=C)
|
152 |
+
if g:
|
153 |
+
g = FpGroup(g[0].group, rels)
|
154 |
+
else:
|
155 |
+
g = FpGroup(free_group('')[0], [])
|
156 |
+
if homomorphism:
|
157 |
+
from sympy.combinatorics.homomorphisms import homomorphism
|
158 |
+
return g, homomorphism(g, self, g.generators, _gens, check=False)
|
159 |
+
return g
|
160 |
+
|
161 |
+
def coset_enumeration(self, H, strategy="relator_based", max_cosets=None,
|
162 |
+
draft=None, incomplete=False):
|
163 |
+
"""
|
164 |
+
Return an instance of ``coset table``, when Todd-Coxeter algorithm is
|
165 |
+
run over the ``self`` with ``H`` as subgroup, using ``strategy``
|
166 |
+
argument as strategy. The returned coset table is compressed but not
|
167 |
+
standardized.
|
168 |
+
|
169 |
+
An instance of `CosetTable` for `fp_grp` can be passed as the keyword
|
170 |
+
argument `draft` in which case the coset enumeration will start with
|
171 |
+
that instance and attempt to complete it.
|
172 |
+
|
173 |
+
When `incomplete` is `True` and the function is unable to complete for
|
174 |
+
some reason, the partially complete table will be returned.
|
175 |
+
|
176 |
+
"""
|
177 |
+
if not max_cosets:
|
178 |
+
max_cosets = CosetTable.coset_table_max_limit
|
179 |
+
if strategy == 'relator_based':
|
180 |
+
C = coset_enumeration_r(self, H, max_cosets=max_cosets,
|
181 |
+
draft=draft, incomplete=incomplete)
|
182 |
+
else:
|
183 |
+
C = coset_enumeration_c(self, H, max_cosets=max_cosets,
|
184 |
+
draft=draft, incomplete=incomplete)
|
185 |
+
if C.is_complete():
|
186 |
+
C.compress()
|
187 |
+
return C
|
188 |
+
|
189 |
+
def standardize_coset_table(self):
|
190 |
+
"""
|
191 |
+
Standardized the coset table ``self`` and makes the internal variable
|
192 |
+
``_is_standardized`` equal to ``True``.
|
193 |
+
|
194 |
+
"""
|
195 |
+
self._coset_table.standardize()
|
196 |
+
self._is_standardized = True
|
197 |
+
|
198 |
+
def coset_table(self, H, strategy="relator_based", max_cosets=None,
|
199 |
+
draft=None, incomplete=False):
|
200 |
+
"""
|
201 |
+
Return the mathematical coset table of ``self`` in ``H``.
|
202 |
+
|
203 |
+
"""
|
204 |
+
if not H:
|
205 |
+
if self._coset_table is not None:
|
206 |
+
if not self._is_standardized:
|
207 |
+
self.standardize_coset_table()
|
208 |
+
else:
|
209 |
+
C = self.coset_enumeration([], strategy, max_cosets=max_cosets,
|
210 |
+
draft=draft, incomplete=incomplete)
|
211 |
+
self._coset_table = C
|
212 |
+
self.standardize_coset_table()
|
213 |
+
return self._coset_table.table
|
214 |
+
else:
|
215 |
+
C = self.coset_enumeration(H, strategy, max_cosets=max_cosets,
|
216 |
+
draft=draft, incomplete=incomplete)
|
217 |
+
C.standardize()
|
218 |
+
return C.table
|
219 |
+
|
220 |
+
def order(self, strategy="relator_based"):
|
221 |
+
"""
|
222 |
+
Returns the order of the finitely presented group ``self``. It uses
|
223 |
+
the coset enumeration with identity group as subgroup, i.e ``H=[]``.
|
224 |
+
|
225 |
+
Examples
|
226 |
+
========
|
227 |
+
|
228 |
+
>>> from sympy.combinatorics import free_group
|
229 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup
|
230 |
+
>>> F, x, y = free_group("x, y")
|
231 |
+
>>> f = FpGroup(F, [x, y**2])
|
232 |
+
>>> f.order(strategy="coset_table_based")
|
233 |
+
2
|
234 |
+
|
235 |
+
"""
|
236 |
+
if self._order is not None:
|
237 |
+
return self._order
|
238 |
+
if self._coset_table is not None:
|
239 |
+
self._order = len(self._coset_table.table)
|
240 |
+
elif len(self.relators) == 0:
|
241 |
+
self._order = self.free_group.order()
|
242 |
+
elif len(self.generators) == 1:
|
243 |
+
self._order = abs(gcd([r.array_form[0][1] for r in self.relators]))
|
244 |
+
elif self._is_infinite():
|
245 |
+
self._order = S.Infinity
|
246 |
+
else:
|
247 |
+
gens, C = self._finite_index_subgroup()
|
248 |
+
if C:
|
249 |
+
ind = len(C.table)
|
250 |
+
self._order = ind*self.subgroup(gens, C=C).order()
|
251 |
+
else:
|
252 |
+
self._order = self.index([])
|
253 |
+
return self._order
|
254 |
+
|
255 |
+
def _is_infinite(self):
|
256 |
+
'''
|
257 |
+
Test if the group is infinite. Return `True` if the test succeeds
|
258 |
+
and `None` otherwise
|
259 |
+
|
260 |
+
'''
|
261 |
+
used_gens = set()
|
262 |
+
for r in self.relators:
|
263 |
+
used_gens.update(r.contains_generators())
|
264 |
+
if not set(self.generators) <= used_gens:
|
265 |
+
return True
|
266 |
+
# Abelianisation test: check is the abelianisation is infinite
|
267 |
+
abelian_rels = []
|
268 |
+
for rel in self.relators:
|
269 |
+
abelian_rels.append([rel.exponent_sum(g) for g in self.generators])
|
270 |
+
m = Matrix(Matrix(abelian_rels))
|
271 |
+
if 0 in invariant_factors(m):
|
272 |
+
return True
|
273 |
+
else:
|
274 |
+
return None
|
275 |
+
|
276 |
+
|
277 |
+
def _finite_index_subgroup(self, s=None):
|
278 |
+
'''
|
279 |
+
Find the elements of `self` that generate a finite index subgroup
|
280 |
+
and, if found, return the list of elements and the coset table of `self` by
|
281 |
+
the subgroup, otherwise return `(None, None)`
|
282 |
+
|
283 |
+
'''
|
284 |
+
gen = self.most_frequent_generator()
|
285 |
+
rels = list(self.generators)
|
286 |
+
rels.extend(self.relators)
|
287 |
+
if not s:
|
288 |
+
if len(self.generators) == 2:
|
289 |
+
s = [gen] + [g for g in self.generators if g != gen]
|
290 |
+
else:
|
291 |
+
rand = self.free_group.identity
|
292 |
+
i = 0
|
293 |
+
while ((rand in rels or rand**-1 in rels or rand.is_identity)
|
294 |
+
and i<10):
|
295 |
+
rand = self.random()
|
296 |
+
i += 1
|
297 |
+
s = [gen, rand] + [g for g in self.generators if g != gen]
|
298 |
+
mid = (len(s)+1)//2
|
299 |
+
half1 = s[:mid]
|
300 |
+
half2 = s[mid:]
|
301 |
+
draft1 = None
|
302 |
+
draft2 = None
|
303 |
+
m = 200
|
304 |
+
C = None
|
305 |
+
while not C and (m/2 < CosetTable.coset_table_max_limit):
|
306 |
+
m = min(m, CosetTable.coset_table_max_limit)
|
307 |
+
draft1 = self.coset_enumeration(half1, max_cosets=m,
|
308 |
+
draft=draft1, incomplete=True)
|
309 |
+
if draft1.is_complete():
|
310 |
+
C = draft1
|
311 |
+
half = half1
|
312 |
+
else:
|
313 |
+
draft2 = self.coset_enumeration(half2, max_cosets=m,
|
314 |
+
draft=draft2, incomplete=True)
|
315 |
+
if draft2.is_complete():
|
316 |
+
C = draft2
|
317 |
+
half = half2
|
318 |
+
if not C:
|
319 |
+
m *= 2
|
320 |
+
if not C:
|
321 |
+
return None, None
|
322 |
+
C.compress()
|
323 |
+
return half, C
|
324 |
+
|
325 |
+
def most_frequent_generator(self):
|
326 |
+
gens = self.generators
|
327 |
+
rels = self.relators
|
328 |
+
freqs = [sum([r.generator_count(g) for r in rels]) for g in gens]
|
329 |
+
return gens[freqs.index(max(freqs))]
|
330 |
+
|
331 |
+
def random(self):
|
332 |
+
import random
|
333 |
+
r = self.free_group.identity
|
334 |
+
for i in range(random.randint(2,3)):
|
335 |
+
r = r*random.choice(self.generators)**random.choice([1,-1])
|
336 |
+
return r
|
337 |
+
|
338 |
+
def index(self, H, strategy="relator_based"):
|
339 |
+
"""
|
340 |
+
Return the index of subgroup ``H`` in group ``self``.
|
341 |
+
|
342 |
+
Examples
|
343 |
+
========
|
344 |
+
|
345 |
+
>>> from sympy.combinatorics import free_group
|
346 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup
|
347 |
+
>>> F, x, y = free_group("x, y")
|
348 |
+
>>> f = FpGroup(F, [x**5, y**4, y*x*y**3*x**3])
|
349 |
+
>>> f.index([x])
|
350 |
+
4
|
351 |
+
|
352 |
+
"""
|
353 |
+
# TODO: use |G:H| = |G|/|H| (currently H can't be made into a group)
|
354 |
+
# when we know |G| and |H|
|
355 |
+
|
356 |
+
if H == []:
|
357 |
+
return self.order()
|
358 |
+
else:
|
359 |
+
C = self.coset_enumeration(H, strategy)
|
360 |
+
return len(C.table)
|
361 |
+
|
362 |
+
def __str__(self):
|
363 |
+
if self.free_group.rank > 30:
|
364 |
+
str_form = "<fp group with %s generators>" % self.free_group.rank
|
365 |
+
else:
|
366 |
+
str_form = "<fp group on the generators %s>" % str(self.generators)
|
367 |
+
return str_form
|
368 |
+
|
369 |
+
__repr__ = __str__
|
370 |
+
|
371 |
+
#==============================================================================
|
372 |
+
# PERMUTATION GROUP METHODS
|
373 |
+
#==============================================================================
|
374 |
+
|
375 |
+
def _to_perm_group(self):
|
376 |
+
'''
|
377 |
+
Return an isomorphic permutation group and the isomorphism.
|
378 |
+
The implementation is dependent on coset enumeration so
|
379 |
+
will only terminate for finite groups.
|
380 |
+
|
381 |
+
'''
|
382 |
+
from sympy.combinatorics import Permutation
|
383 |
+
from sympy.combinatorics.homomorphisms import homomorphism
|
384 |
+
if self.order() is S.Infinity:
|
385 |
+
raise NotImplementedError("Permutation presentation of infinite "
|
386 |
+
"groups is not implemented")
|
387 |
+
if self._perm_isomorphism:
|
388 |
+
T = self._perm_isomorphism
|
389 |
+
P = T.image()
|
390 |
+
else:
|
391 |
+
C = self.coset_table([])
|
392 |
+
gens = self.generators
|
393 |
+
images = [[C[i][2*gens.index(g)] for i in range(len(C))] for g in gens]
|
394 |
+
images = [Permutation(i) for i in images]
|
395 |
+
P = PermutationGroup(images)
|
396 |
+
T = homomorphism(self, P, gens, images, check=False)
|
397 |
+
self._perm_isomorphism = T
|
398 |
+
return P, T
|
399 |
+
|
400 |
+
def _perm_group_list(self, method_name, *args):
|
401 |
+
'''
|
402 |
+
Given the name of a `PermutationGroup` method (returning a subgroup
|
403 |
+
or a list of subgroups) and (optionally) additional arguments it takes,
|
404 |
+
return a list or a list of lists containing the generators of this (or
|
405 |
+
these) subgroups in terms of the generators of `self`.
|
406 |
+
|
407 |
+
'''
|
408 |
+
P, T = self._to_perm_group()
|
409 |
+
perm_result = getattr(P, method_name)(*args)
|
410 |
+
single = False
|
411 |
+
if isinstance(perm_result, PermutationGroup):
|
412 |
+
perm_result, single = [perm_result], True
|
413 |
+
result = []
|
414 |
+
for group in perm_result:
|
415 |
+
gens = group.generators
|
416 |
+
result.append(T.invert(gens))
|
417 |
+
return result[0] if single else result
|
418 |
+
|
419 |
+
def derived_series(self):
|
420 |
+
'''
|
421 |
+
Return the list of lists containing the generators
|
422 |
+
of the subgroups in the derived series of `self`.
|
423 |
+
|
424 |
+
'''
|
425 |
+
return self._perm_group_list('derived_series')
|
426 |
+
|
427 |
+
def lower_central_series(self):
|
428 |
+
'''
|
429 |
+
Return the list of lists containing the generators
|
430 |
+
of the subgroups in the lower central series of `self`.
|
431 |
+
|
432 |
+
'''
|
433 |
+
return self._perm_group_list('lower_central_series')
|
434 |
+
|
435 |
+
def center(self):
|
436 |
+
'''
|
437 |
+
Return the list of generators of the center of `self`.
|
438 |
+
|
439 |
+
'''
|
440 |
+
return self._perm_group_list('center')
|
441 |
+
|
442 |
+
|
443 |
+
def derived_subgroup(self):
|
444 |
+
'''
|
445 |
+
Return the list of generators of the derived subgroup of `self`.
|
446 |
+
|
447 |
+
'''
|
448 |
+
return self._perm_group_list('derived_subgroup')
|
449 |
+
|
450 |
+
|
451 |
+
def centralizer(self, other):
|
452 |
+
'''
|
453 |
+
Return the list of generators of the centralizer of `other`
|
454 |
+
(a list of elements of `self`) in `self`.
|
455 |
+
|
456 |
+
'''
|
457 |
+
T = self._to_perm_group()[1]
|
458 |
+
other = T(other)
|
459 |
+
return self._perm_group_list('centralizer', other)
|
460 |
+
|
461 |
+
def normal_closure(self, other):
|
462 |
+
'''
|
463 |
+
Return the list of generators of the normal closure of `other`
|
464 |
+
(a list of elements of `self`) in `self`.
|
465 |
+
|
466 |
+
'''
|
467 |
+
T = self._to_perm_group()[1]
|
468 |
+
other = T(other)
|
469 |
+
return self._perm_group_list('normal_closure', other)
|
470 |
+
|
471 |
+
def _perm_property(self, attr):
|
472 |
+
'''
|
473 |
+
Given an attribute of a `PermutationGroup`, return
|
474 |
+
its value for a permutation group isomorphic to `self`.
|
475 |
+
|
476 |
+
'''
|
477 |
+
P = self._to_perm_group()[0]
|
478 |
+
return getattr(P, attr)
|
479 |
+
|
480 |
+
@property
|
481 |
+
def is_abelian(self):
|
482 |
+
'''
|
483 |
+
Check if `self` is abelian.
|
484 |
+
|
485 |
+
'''
|
486 |
+
return self._perm_property("is_abelian")
|
487 |
+
|
488 |
+
@property
|
489 |
+
def is_nilpotent(self):
|
490 |
+
'''
|
491 |
+
Check if `self` is nilpotent.
|
492 |
+
|
493 |
+
'''
|
494 |
+
return self._perm_property("is_nilpotent")
|
495 |
+
|
496 |
+
@property
|
497 |
+
def is_solvable(self):
|
498 |
+
'''
|
499 |
+
Check if `self` is solvable.
|
500 |
+
|
501 |
+
'''
|
502 |
+
return self._perm_property("is_solvable")
|
503 |
+
|
504 |
+
@property
|
505 |
+
def elements(self):
|
506 |
+
'''
|
507 |
+
List the elements of `self`.
|
508 |
+
|
509 |
+
'''
|
510 |
+
P, T = self._to_perm_group()
|
511 |
+
return T.invert(P._elements)
|
512 |
+
|
513 |
+
@property
|
514 |
+
def is_cyclic(self):
|
515 |
+
"""
|
516 |
+
Return ``True`` if group is Cyclic.
|
517 |
+
|
518 |
+
"""
|
519 |
+
if len(self.generators) <= 1:
|
520 |
+
return True
|
521 |
+
try:
|
522 |
+
P, T = self._to_perm_group()
|
523 |
+
except NotImplementedError:
|
524 |
+
raise NotImplementedError("Check for infinite Cyclic group "
|
525 |
+
"is not implemented")
|
526 |
+
return P.is_cyclic
|
527 |
+
|
528 |
+
def abelian_invariants(self):
|
529 |
+
"""
|
530 |
+
Return Abelian Invariants of a group.
|
531 |
+
"""
|
532 |
+
try:
|
533 |
+
P, T = self._to_perm_group()
|
534 |
+
except NotImplementedError:
|
535 |
+
raise NotImplementedError("abelian invariants is not implemented"
|
536 |
+
"for infinite group")
|
537 |
+
return P.abelian_invariants()
|
538 |
+
|
539 |
+
def composition_series(self):
|
540 |
+
"""
|
541 |
+
Return subnormal series of maximum length for a group.
|
542 |
+
"""
|
543 |
+
try:
|
544 |
+
P, T = self._to_perm_group()
|
545 |
+
except NotImplementedError:
|
546 |
+
raise NotImplementedError("composition series is not implemented"
|
547 |
+
"for infinite group")
|
548 |
+
return P.composition_series()
|
549 |
+
|
550 |
+
|
551 |
+
class FpSubgroup(DefaultPrinting):
|
552 |
+
'''
|
553 |
+
The class implementing a subgroup of an FpGroup or a FreeGroup
|
554 |
+
(only finite index subgroups are supported at this point). This
|
555 |
+
is to be used if one wishes to check if an element of the original
|
556 |
+
group belongs to the subgroup
|
557 |
+
|
558 |
+
'''
|
559 |
+
def __init__(self, G, gens, normal=False):
|
560 |
+
super().__init__()
|
561 |
+
self.parent = G
|
562 |
+
self.generators = list({g for g in gens if g != G.identity})
|
563 |
+
self._min_words = None #for use in __contains__
|
564 |
+
self.C = None
|
565 |
+
self.normal = normal
|
566 |
+
|
567 |
+
def __contains__(self, g):
|
568 |
+
|
569 |
+
if isinstance(self.parent, FreeGroup):
|
570 |
+
if self._min_words is None:
|
571 |
+
# make _min_words - a list of subwords such that
|
572 |
+
# g is in the subgroup if and only if it can be
|
573 |
+
# partitioned into these subwords. Infinite families of
|
574 |
+
# subwords are presented by tuples, e.g. (r, w)
|
575 |
+
# stands for the family of subwords r*w**n*r**-1
|
576 |
+
|
577 |
+
def _process(w):
|
578 |
+
# this is to be used before adding new words
|
579 |
+
# into _min_words; if the word w is not cyclically
|
580 |
+
# reduced, it will generate an infinite family of
|
581 |
+
# subwords so should be written as a tuple;
|
582 |
+
# if it is, w**-1 should be added to the list
|
583 |
+
# as well
|
584 |
+
p, r = w.cyclic_reduction(removed=True)
|
585 |
+
if not r.is_identity:
|
586 |
+
return [(r, p)]
|
587 |
+
else:
|
588 |
+
return [w, w**-1]
|
589 |
+
|
590 |
+
# make the initial list
|
591 |
+
gens = []
|
592 |
+
for w in self.generators:
|
593 |
+
if self.normal:
|
594 |
+
w = w.cyclic_reduction()
|
595 |
+
gens.extend(_process(w))
|
596 |
+
|
597 |
+
for w1 in gens:
|
598 |
+
for w2 in gens:
|
599 |
+
# if w1 and w2 are equal or are inverses, continue
|
600 |
+
if w1 == w2 or (not isinstance(w1, tuple)
|
601 |
+
and w1**-1 == w2):
|
602 |
+
continue
|
603 |
+
|
604 |
+
# if the start of one word is the inverse of the
|
605 |
+
# end of the other, their multiple should be added
|
606 |
+
# to _min_words because of cancellation
|
607 |
+
if isinstance(w1, tuple):
|
608 |
+
# start, end
|
609 |
+
s1, s2 = w1[0][0], w1[0][0]**-1
|
610 |
+
else:
|
611 |
+
s1, s2 = w1[0], w1[len(w1)-1]
|
612 |
+
|
613 |
+
if isinstance(w2, tuple):
|
614 |
+
# start, end
|
615 |
+
r1, r2 = w2[0][0], w2[0][0]**-1
|
616 |
+
else:
|
617 |
+
r1, r2 = w2[0], w2[len(w1)-1]
|
618 |
+
|
619 |
+
# p1 and p2 are w1 and w2 or, in case when
|
620 |
+
# w1 or w2 is an infinite family, a representative
|
621 |
+
p1, p2 = w1, w2
|
622 |
+
if isinstance(w1, tuple):
|
623 |
+
p1 = w1[0]*w1[1]*w1[0]**-1
|
624 |
+
if isinstance(w2, tuple):
|
625 |
+
p2 = w2[0]*w2[1]*w2[0]**-1
|
626 |
+
|
627 |
+
# add the product of the words to the list is necessary
|
628 |
+
if r1**-1 == s2 and not (p1*p2).is_identity:
|
629 |
+
new = _process(p1*p2)
|
630 |
+
if new not in gens:
|
631 |
+
gens.extend(new)
|
632 |
+
|
633 |
+
if r2**-1 == s1 and not (p2*p1).is_identity:
|
634 |
+
new = _process(p2*p1)
|
635 |
+
if new not in gens:
|
636 |
+
gens.extend(new)
|
637 |
+
|
638 |
+
self._min_words = gens
|
639 |
+
|
640 |
+
min_words = self._min_words
|
641 |
+
|
642 |
+
def _is_subword(w):
|
643 |
+
# check if w is a word in _min_words or one of
|
644 |
+
# the infinite families in it
|
645 |
+
w, r = w.cyclic_reduction(removed=True)
|
646 |
+
if r.is_identity or self.normal:
|
647 |
+
return w in min_words
|
648 |
+
else:
|
649 |
+
t = [s[1] for s in min_words if isinstance(s, tuple)
|
650 |
+
and s[0] == r]
|
651 |
+
return [s for s in t if w.power_of(s)] != []
|
652 |
+
|
653 |
+
# store the solution of words for which the result of
|
654 |
+
# _word_break (below) is known
|
655 |
+
known = {}
|
656 |
+
|
657 |
+
def _word_break(w):
|
658 |
+
# check if w can be written as a product of words
|
659 |
+
# in min_words
|
660 |
+
if len(w) == 0:
|
661 |
+
return True
|
662 |
+
i = 0
|
663 |
+
while i < len(w):
|
664 |
+
i += 1
|
665 |
+
prefix = w.subword(0, i)
|
666 |
+
if not _is_subword(prefix):
|
667 |
+
continue
|
668 |
+
rest = w.subword(i, len(w))
|
669 |
+
if rest not in known:
|
670 |
+
known[rest] = _word_break(rest)
|
671 |
+
if known[rest]:
|
672 |
+
return True
|
673 |
+
return False
|
674 |
+
|
675 |
+
if self.normal:
|
676 |
+
g = g.cyclic_reduction()
|
677 |
+
return _word_break(g)
|
678 |
+
else:
|
679 |
+
if self.C is None:
|
680 |
+
C = self.parent.coset_enumeration(self.generators)
|
681 |
+
self.C = C
|
682 |
+
i = 0
|
683 |
+
C = self.C
|
684 |
+
for j in range(len(g)):
|
685 |
+
i = C.table[i][C.A_dict[g[j]]]
|
686 |
+
return i == 0
|
687 |
+
|
688 |
+
def order(self):
|
689 |
+
if not self.generators:
|
690 |
+
return S.One
|
691 |
+
if isinstance(self.parent, FreeGroup):
|
692 |
+
return S.Infinity
|
693 |
+
if self.C is None:
|
694 |
+
C = self.parent.coset_enumeration(self.generators)
|
695 |
+
self.C = C
|
696 |
+
# This is valid because `len(self.C.table)` (the index of the subgroup)
|
697 |
+
# will always be finite - otherwise coset enumeration doesn't terminate
|
698 |
+
return self.parent.order()/len(self.C.table)
|
699 |
+
|
700 |
+
def to_FpGroup(self):
|
701 |
+
if isinstance(self.parent, FreeGroup):
|
702 |
+
gen_syms = [('x_%d'%i) for i in range(len(self.generators))]
|
703 |
+
return free_group(', '.join(gen_syms))[0]
|
704 |
+
return self.parent.subgroup(C=self.C)
|
705 |
+
|
706 |
+
def __str__(self):
|
707 |
+
if len(self.generators) > 30:
|
708 |
+
str_form = "<fp subgroup with %s generators>" % len(self.generators)
|
709 |
+
else:
|
710 |
+
str_form = "<fp subgroup on the generators %s>" % str(self.generators)
|
711 |
+
return str_form
|
712 |
+
|
713 |
+
__repr__ = __str__
|
714 |
+
|
715 |
+
|
716 |
+
###############################################################################
|
717 |
+
# LOW INDEX SUBGROUPS #
|
718 |
+
###############################################################################
|
719 |
+
|
720 |
+
def low_index_subgroups(G, N, Y=()):
|
721 |
+
"""
|
722 |
+
Implements the Low Index Subgroups algorithm, i.e find all subgroups of
|
723 |
+
``G`` upto a given index ``N``. This implements the method described in
|
724 |
+
[Sim94]. This procedure involves a backtrack search over incomplete Coset
|
725 |
+
Tables, rather than over forced coincidences.
|
726 |
+
|
727 |
+
Parameters
|
728 |
+
==========
|
729 |
+
|
730 |
+
G: An FpGroup < X|R >
|
731 |
+
N: positive integer, representing the maximum index value for subgroups
|
732 |
+
Y: (an optional argument) specifying a list of subgroup generators, such
|
733 |
+
that each of the resulting subgroup contains the subgroup generated by Y.
|
734 |
+
|
735 |
+
Examples
|
736 |
+
========
|
737 |
+
|
738 |
+
>>> from sympy.combinatorics import free_group
|
739 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, low_index_subgroups
|
740 |
+
>>> F, x, y = free_group("x, y")
|
741 |
+
>>> f = FpGroup(F, [x**2, y**3, (x*y)**4])
|
742 |
+
>>> L = low_index_subgroups(f, 4)
|
743 |
+
>>> for coset_table in L:
|
744 |
+
... print(coset_table.table)
|
745 |
+
[[0, 0, 0, 0]]
|
746 |
+
[[0, 0, 1, 2], [1, 1, 2, 0], [3, 3, 0, 1], [2, 2, 3, 3]]
|
747 |
+
[[0, 0, 1, 2], [2, 2, 2, 0], [1, 1, 0, 1]]
|
748 |
+
[[1, 1, 0, 0], [0, 0, 1, 1]]
|
749 |
+
|
750 |
+
References
|
751 |
+
==========
|
752 |
+
|
753 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.
|
754 |
+
"Handbook of Computational Group Theory"
|
755 |
+
Section 5.4
|
756 |
+
|
757 |
+
.. [2] Marston Conder and Peter Dobcsanyi
|
758 |
+
"Applications and Adaptions of the Low Index Subgroups Procedure"
|
759 |
+
|
760 |
+
"""
|
761 |
+
C = CosetTable(G, [])
|
762 |
+
R = G.relators
|
763 |
+
# length chosen for the length of the short relators
|
764 |
+
len_short_rel = 5
|
765 |
+
# elements of R2 only checked at the last step for complete
|
766 |
+
# coset tables
|
767 |
+
R2 = {rel for rel in R if len(rel) > len_short_rel}
|
768 |
+
# elements of R1 are used in inner parts of the process to prune
|
769 |
+
# branches of the search tree,
|
770 |
+
R1 = {rel.identity_cyclic_reduction() for rel in set(R) - R2}
|
771 |
+
R1_c_list = C.conjugates(R1)
|
772 |
+
S = []
|
773 |
+
descendant_subgroups(S, C, R1_c_list, C.A[0], R2, N, Y)
|
774 |
+
return S
|
775 |
+
|
776 |
+
|
777 |
+
def descendant_subgroups(S, C, R1_c_list, x, R2, N, Y):
|
778 |
+
A_dict = C.A_dict
|
779 |
+
A_dict_inv = C.A_dict_inv
|
780 |
+
if C.is_complete():
|
781 |
+
# if C is complete then it only needs to test
|
782 |
+
# whether the relators in R2 are satisfied
|
783 |
+
for w, alpha in product(R2, C.omega):
|
784 |
+
if not C.scan_check(alpha, w):
|
785 |
+
return
|
786 |
+
# relators in R2 are satisfied, append the table to list
|
787 |
+
S.append(C)
|
788 |
+
else:
|
789 |
+
# find the first undefined entry in Coset Table
|
790 |
+
for alpha, x in product(range(len(C.table)), C.A):
|
791 |
+
if C.table[alpha][A_dict[x]] is None:
|
792 |
+
# this is "x" in pseudo-code (using "y" makes it clear)
|
793 |
+
undefined_coset, undefined_gen = alpha, x
|
794 |
+
break
|
795 |
+
# for filling up the undefine entry we try all possible values
|
796 |
+
# of beta in Omega or beta = n where beta^(undefined_gen^-1) is undefined
|
797 |
+
reach = C.omega + [C.n]
|
798 |
+
for beta in reach:
|
799 |
+
if beta < N:
|
800 |
+
if beta == C.n or C.table[beta][A_dict_inv[undefined_gen]] is None:
|
801 |
+
try_descendant(S, C, R1_c_list, R2, N, undefined_coset, \
|
802 |
+
undefined_gen, beta, Y)
|
803 |
+
|
804 |
+
|
805 |
+
def try_descendant(S, C, R1_c_list, R2, N, alpha, x, beta, Y):
|
806 |
+
r"""
|
807 |
+
Solves the problem of trying out each individual possibility
|
808 |
+
for `\alpha^x.
|
809 |
+
|
810 |
+
"""
|
811 |
+
D = C.copy()
|
812 |
+
if beta == D.n and beta < N:
|
813 |
+
D.table.append([None]*len(D.A))
|
814 |
+
D.p.append(beta)
|
815 |
+
D.table[alpha][D.A_dict[x]] = beta
|
816 |
+
D.table[beta][D.A_dict_inv[x]] = alpha
|
817 |
+
D.deduction_stack.append((alpha, x))
|
818 |
+
if not D.process_deductions_check(R1_c_list[D.A_dict[x]], \
|
819 |
+
R1_c_list[D.A_dict_inv[x]]):
|
820 |
+
return
|
821 |
+
for w in Y:
|
822 |
+
if not D.scan_check(0, w):
|
823 |
+
return
|
824 |
+
if first_in_class(D, Y):
|
825 |
+
descendant_subgroups(S, D, R1_c_list, x, R2, N, Y)
|
826 |
+
|
827 |
+
|
828 |
+
def first_in_class(C, Y=()):
|
829 |
+
"""
|
830 |
+
Checks whether the subgroup ``H=G1`` corresponding to the Coset Table
|
831 |
+
could possibly be the canonical representative of its conjugacy class.
|
832 |
+
|
833 |
+
Parameters
|
834 |
+
==========
|
835 |
+
|
836 |
+
C: CosetTable
|
837 |
+
|
838 |
+
Returns
|
839 |
+
=======
|
840 |
+
|
841 |
+
bool: True/False
|
842 |
+
|
843 |
+
If this returns False, then no descendant of C can have that property, and
|
844 |
+
so we can abandon C. If it returns True, then we need to process further
|
845 |
+
the node of the search tree corresponding to C, and so we call
|
846 |
+
``descendant_subgroups`` recursively on C.
|
847 |
+
|
848 |
+
Examples
|
849 |
+
========
|
850 |
+
|
851 |
+
>>> from sympy.combinatorics import free_group
|
852 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, CosetTable, first_in_class
|
853 |
+
>>> F, x, y = free_group("x, y")
|
854 |
+
>>> f = FpGroup(F, [x**2, y**3, (x*y)**4])
|
855 |
+
>>> C = CosetTable(f, [])
|
856 |
+
>>> C.table = [[0, 0, None, None]]
|
857 |
+
>>> first_in_class(C)
|
858 |
+
True
|
859 |
+
>>> C.table = [[1, 1, 1, None], [0, 0, None, 1]]; C.p = [0, 1]
|
860 |
+
>>> first_in_class(C)
|
861 |
+
True
|
862 |
+
>>> C.table = [[1, 1, 2, 1], [0, 0, 0, None], [None, None, None, 0]]
|
863 |
+
>>> C.p = [0, 1, 2]
|
864 |
+
>>> first_in_class(C)
|
865 |
+
False
|
866 |
+
>>> C.table = [[1, 1, 1, 2], [0, 0, 2, 0], [2, None, 0, 1]]
|
867 |
+
>>> first_in_class(C)
|
868 |
+
False
|
869 |
+
|
870 |
+
# TODO:: Sims points out in [Sim94] that performance can be improved by
|
871 |
+
# remembering some of the information computed by ``first_in_class``. If
|
872 |
+
# the ``continue alpha`` statement is executed at line 14, then the same thing
|
873 |
+
# will happen for that value of alpha in any descendant of the table C, and so
|
874 |
+
# the values the values of alpha for which this occurs could profitably be
|
875 |
+
# stored and passed through to the descendants of C. Of course this would
|
876 |
+
# make the code more complicated.
|
877 |
+
|
878 |
+
# The code below is taken directly from the function on page 208 of [Sim94]
|
879 |
+
# nu[alpha]
|
880 |
+
|
881 |
+
"""
|
882 |
+
n = C.n
|
883 |
+
# lamda is the largest numbered point in Omega_c_alpha which is currently defined
|
884 |
+
lamda = -1
|
885 |
+
# for alpha in Omega_c, nu[alpha] is the point in Omega_c_alpha corresponding to alpha
|
886 |
+
nu = [None]*n
|
887 |
+
# for alpha in Omega_c_alpha, mu[alpha] is the point in Omega_c corresponding to alpha
|
888 |
+
mu = [None]*n
|
889 |
+
# mutually nu and mu are the mutually-inverse equivalence maps between
|
890 |
+
# Omega_c_alpha and Omega_c
|
891 |
+
next_alpha = False
|
892 |
+
# For each 0!=alpha in [0 .. nc-1], we start by constructing the equivalent
|
893 |
+
# standardized coset table C_alpha corresponding to H_alpha
|
894 |
+
for alpha in range(1, n):
|
895 |
+
# reset nu to "None" after previous value of alpha
|
896 |
+
for beta in range(lamda+1):
|
897 |
+
nu[mu[beta]] = None
|
898 |
+
# we only want to reject our current table in favour of a preceding
|
899 |
+
# table in the ordering in which 1 is replaced by alpha, if the subgroup
|
900 |
+
# G_alpha corresponding to this preceding table definitely contains the
|
901 |
+
# given subgroup
|
902 |
+
for w in Y:
|
903 |
+
# TODO: this should support input of a list of general words
|
904 |
+
# not just the words which are in "A" (i.e gen and gen^-1)
|
905 |
+
if C.table[alpha][C.A_dict[w]] != alpha:
|
906 |
+
# continue with alpha
|
907 |
+
next_alpha = True
|
908 |
+
break
|
909 |
+
if next_alpha:
|
910 |
+
next_alpha = False
|
911 |
+
continue
|
912 |
+
# try alpha as the new point 0 in Omega_C_alpha
|
913 |
+
mu[0] = alpha
|
914 |
+
nu[alpha] = 0
|
915 |
+
# compare corresponding entries in C and C_alpha
|
916 |
+
lamda = 0
|
917 |
+
for beta in range(n):
|
918 |
+
for x in C.A:
|
919 |
+
gamma = C.table[beta][C.A_dict[x]]
|
920 |
+
delta = C.table[mu[beta]][C.A_dict[x]]
|
921 |
+
# if either of the entries is undefined,
|
922 |
+
# we move with next alpha
|
923 |
+
if gamma is None or delta is None:
|
924 |
+
# continue with alpha
|
925 |
+
next_alpha = True
|
926 |
+
break
|
927 |
+
if nu[delta] is None:
|
928 |
+
# delta becomes the next point in Omega_C_alpha
|
929 |
+
lamda += 1
|
930 |
+
nu[delta] = lamda
|
931 |
+
mu[lamda] = delta
|
932 |
+
if nu[delta] < gamma:
|
933 |
+
return False
|
934 |
+
if nu[delta] > gamma:
|
935 |
+
# continue with alpha
|
936 |
+
next_alpha = True
|
937 |
+
break
|
938 |
+
if next_alpha:
|
939 |
+
next_alpha = False
|
940 |
+
break
|
941 |
+
return True
|
942 |
+
|
943 |
+
#========================================================================
|
944 |
+
# Simplifying Presentation
|
945 |
+
#========================================================================
|
946 |
+
|
947 |
+
def simplify_presentation(*args, change_gens=False):
|
948 |
+
'''
|
949 |
+
For an instance of `FpGroup`, return a simplified isomorphic copy of
|
950 |
+
the group (e.g. remove redundant generators or relators). Alternatively,
|
951 |
+
a list of generators and relators can be passed in which case the
|
952 |
+
simplified lists will be returned.
|
953 |
+
|
954 |
+
By default, the generators of the group are unchanged. If you would
|
955 |
+
like to remove redundant generators, set the keyword argument
|
956 |
+
`change_gens = True`.
|
957 |
+
|
958 |
+
'''
|
959 |
+
if len(args) == 1:
|
960 |
+
if not isinstance(args[0], FpGroup):
|
961 |
+
raise TypeError("The argument must be an instance of FpGroup")
|
962 |
+
G = args[0]
|
963 |
+
gens, rels = simplify_presentation(G.generators, G.relators,
|
964 |
+
change_gens=change_gens)
|
965 |
+
if gens:
|
966 |
+
return FpGroup(gens[0].group, rels)
|
967 |
+
return FpGroup(FreeGroup([]), [])
|
968 |
+
elif len(args) == 2:
|
969 |
+
gens, rels = args[0][:], args[1][:]
|
970 |
+
if not gens:
|
971 |
+
return gens, rels
|
972 |
+
identity = gens[0].group.identity
|
973 |
+
else:
|
974 |
+
if len(args) == 0:
|
975 |
+
m = "Not enough arguments"
|
976 |
+
else:
|
977 |
+
m = "Too many arguments"
|
978 |
+
raise RuntimeError(m)
|
979 |
+
|
980 |
+
prev_gens = []
|
981 |
+
prev_rels = []
|
982 |
+
while not set(prev_rels) == set(rels):
|
983 |
+
prev_rels = rels
|
984 |
+
while change_gens and not set(prev_gens) == set(gens):
|
985 |
+
prev_gens = gens
|
986 |
+
gens, rels = elimination_technique_1(gens, rels, identity)
|
987 |
+
rels = _simplify_relators(rels, identity)
|
988 |
+
|
989 |
+
if change_gens:
|
990 |
+
syms = [g.array_form[0][0] for g in gens]
|
991 |
+
F = free_group(syms)[0]
|
992 |
+
identity = F.identity
|
993 |
+
gens = F.generators
|
994 |
+
subs = dict(zip(syms, gens))
|
995 |
+
for j, r in enumerate(rels):
|
996 |
+
a = r.array_form
|
997 |
+
rel = identity
|
998 |
+
for sym, p in a:
|
999 |
+
rel = rel*subs[sym]**p
|
1000 |
+
rels[j] = rel
|
1001 |
+
return gens, rels
|
1002 |
+
|
1003 |
+
def _simplify_relators(rels, identity):
|
1004 |
+
"""Relies upon ``_simplification_technique_1`` for its functioning. """
|
1005 |
+
rels = rels[:]
|
1006 |
+
|
1007 |
+
rels = list(set(_simplification_technique_1(rels)))
|
1008 |
+
rels.sort()
|
1009 |
+
rels = [r.identity_cyclic_reduction() for r in rels]
|
1010 |
+
try:
|
1011 |
+
rels.remove(identity)
|
1012 |
+
except ValueError:
|
1013 |
+
pass
|
1014 |
+
return rels
|
1015 |
+
|
1016 |
+
# Pg 350, section 2.5.1 from [2]
|
1017 |
+
def elimination_technique_1(gens, rels, identity):
|
1018 |
+
rels = rels[:]
|
1019 |
+
# the shorter relators are examined first so that generators selected for
|
1020 |
+
# elimination will have shorter strings as equivalent
|
1021 |
+
rels.sort()
|
1022 |
+
gens = gens[:]
|
1023 |
+
redundant_gens = {}
|
1024 |
+
redundant_rels = []
|
1025 |
+
used_gens = set()
|
1026 |
+
# examine each relator in relator list for any generator occurring exactly
|
1027 |
+
# once
|
1028 |
+
for rel in rels:
|
1029 |
+
# don't look for a redundant generator in a relator which
|
1030 |
+
# depends on previously found ones
|
1031 |
+
contained_gens = rel.contains_generators()
|
1032 |
+
if any(g in contained_gens for g in redundant_gens):
|
1033 |
+
continue
|
1034 |
+
contained_gens = list(contained_gens)
|
1035 |
+
contained_gens.sort(reverse = True)
|
1036 |
+
for gen in contained_gens:
|
1037 |
+
if rel.generator_count(gen) == 1 and gen not in used_gens:
|
1038 |
+
k = rel.exponent_sum(gen)
|
1039 |
+
gen_index = rel.index(gen**k)
|
1040 |
+
bk = rel.subword(gen_index + 1, len(rel))
|
1041 |
+
fw = rel.subword(0, gen_index)
|
1042 |
+
chi = bk*fw
|
1043 |
+
redundant_gens[gen] = chi**(-1*k)
|
1044 |
+
used_gens.update(chi.contains_generators())
|
1045 |
+
redundant_rels.append(rel)
|
1046 |
+
break
|
1047 |
+
rels = [r for r in rels if r not in redundant_rels]
|
1048 |
+
# eliminate the redundant generators from remaining relators
|
1049 |
+
rels = [r.eliminate_words(redundant_gens, _all = True).identity_cyclic_reduction() for r in rels]
|
1050 |
+
rels = list(set(rels))
|
1051 |
+
try:
|
1052 |
+
rels.remove(identity)
|
1053 |
+
except ValueError:
|
1054 |
+
pass
|
1055 |
+
gens = [g for g in gens if g not in redundant_gens]
|
1056 |
+
return gens, rels
|
1057 |
+
|
1058 |
+
def _simplification_technique_1(rels):
|
1059 |
+
"""
|
1060 |
+
All relators are checked to see if they are of the form `gen^n`. If any
|
1061 |
+
such relators are found then all other relators are processed for strings
|
1062 |
+
in the `gen` known order.
|
1063 |
+
|
1064 |
+
Examples
|
1065 |
+
========
|
1066 |
+
|
1067 |
+
>>> from sympy.combinatorics import free_group
|
1068 |
+
>>> from sympy.combinatorics.fp_groups import _simplification_technique_1
|
1069 |
+
>>> F, x, y = free_group("x, y")
|
1070 |
+
>>> w1 = [x**2*y**4, x**3]
|
1071 |
+
>>> _simplification_technique_1(w1)
|
1072 |
+
[x**-1*y**4, x**3]
|
1073 |
+
|
1074 |
+
>>> w2 = [x**2*y**-4*x**5, x**3, x**2*y**8, y**5]
|
1075 |
+
>>> _simplification_technique_1(w2)
|
1076 |
+
[x**-1*y*x**-1, x**3, x**-1*y**-2, y**5]
|
1077 |
+
|
1078 |
+
>>> w3 = [x**6*y**4, x**4]
|
1079 |
+
>>> _simplification_technique_1(w3)
|
1080 |
+
[x**2*y**4, x**4]
|
1081 |
+
|
1082 |
+
"""
|
1083 |
+
rels = rels[:]
|
1084 |
+
# dictionary with "gen: n" where gen^n is one of the relators
|
1085 |
+
exps = {}
|
1086 |
+
for i in range(len(rels)):
|
1087 |
+
rel = rels[i]
|
1088 |
+
if rel.number_syllables() == 1:
|
1089 |
+
g = rel[0]
|
1090 |
+
exp = abs(rel.array_form[0][1])
|
1091 |
+
if rel.array_form[0][1] < 0:
|
1092 |
+
rels[i] = rels[i]**-1
|
1093 |
+
g = g**-1
|
1094 |
+
if g in exps:
|
1095 |
+
exp = gcd(exp, exps[g].array_form[0][1])
|
1096 |
+
exps[g] = g**exp
|
1097 |
+
|
1098 |
+
one_syllables_words = exps.values()
|
1099 |
+
# decrease some of the exponents in relators, making use of the single
|
1100 |
+
# syllable relators
|
1101 |
+
for i in range(len(rels)):
|
1102 |
+
rel = rels[i]
|
1103 |
+
if rel in one_syllables_words:
|
1104 |
+
continue
|
1105 |
+
rel = rel.eliminate_words(one_syllables_words, _all = True)
|
1106 |
+
# if rels[i] contains g**n where abs(n) is greater than half of the power p
|
1107 |
+
# of g in exps, g**n can be replaced by g**(n-p) (or g**(p-n) if n<0)
|
1108 |
+
for g in rel.contains_generators():
|
1109 |
+
if g in exps:
|
1110 |
+
exp = exps[g].array_form[0][1]
|
1111 |
+
max_exp = (exp + 1)//2
|
1112 |
+
rel = rel.eliminate_word(g**(max_exp), g**(max_exp-exp), _all = True)
|
1113 |
+
rel = rel.eliminate_word(g**(-max_exp), g**(-(max_exp-exp)), _all = True)
|
1114 |
+
rels[i] = rel
|
1115 |
+
rels = [r.identity_cyclic_reduction() for r in rels]
|
1116 |
+
return rels
|
1117 |
+
|
1118 |
+
|
1119 |
+
###############################################################################
|
1120 |
+
# SUBGROUP PRESENTATIONS #
|
1121 |
+
###############################################################################
|
1122 |
+
|
1123 |
+
# Pg 175 [1]
|
1124 |
+
def define_schreier_generators(C, homomorphism=False):
|
1125 |
+
'''
|
1126 |
+
Parameters
|
1127 |
+
==========
|
1128 |
+
|
1129 |
+
C -- Coset table.
|
1130 |
+
homomorphism -- When set to True, return a dictionary containing the images
|
1131 |
+
of the presentation generators in the original group.
|
1132 |
+
'''
|
1133 |
+
y = []
|
1134 |
+
gamma = 1
|
1135 |
+
f = C.fp_group
|
1136 |
+
X = f.generators
|
1137 |
+
if homomorphism:
|
1138 |
+
# `_gens` stores the elements of the parent group to
|
1139 |
+
# to which the schreier generators correspond to.
|
1140 |
+
_gens = {}
|
1141 |
+
# compute the schreier Traversal
|
1142 |
+
tau = {}
|
1143 |
+
tau[0] = f.identity
|
1144 |
+
C.P = [[None]*len(C.A) for i in range(C.n)]
|
1145 |
+
for alpha, x in product(C.omega, C.A):
|
1146 |
+
beta = C.table[alpha][C.A_dict[x]]
|
1147 |
+
if beta == gamma:
|
1148 |
+
C.P[alpha][C.A_dict[x]] = "<identity>"
|
1149 |
+
C.P[beta][C.A_dict_inv[x]] = "<identity>"
|
1150 |
+
gamma += 1
|
1151 |
+
if homomorphism:
|
1152 |
+
tau[beta] = tau[alpha]*x
|
1153 |
+
elif x in X and C.P[alpha][C.A_dict[x]] is None:
|
1154 |
+
y_alpha_x = '%s_%s' % (x, alpha)
|
1155 |
+
y.append(y_alpha_x)
|
1156 |
+
C.P[alpha][C.A_dict[x]] = y_alpha_x
|
1157 |
+
if homomorphism:
|
1158 |
+
_gens[y_alpha_x] = tau[alpha]*x*tau[beta]**-1
|
1159 |
+
grp_gens = list(free_group(', '.join(y)))
|
1160 |
+
C._schreier_free_group = grp_gens.pop(0)
|
1161 |
+
C._schreier_generators = grp_gens
|
1162 |
+
if homomorphism:
|
1163 |
+
C._schreier_gen_elem = _gens
|
1164 |
+
# replace all elements of P by, free group elements
|
1165 |
+
for i, j in product(range(len(C.P)), range(len(C.A))):
|
1166 |
+
# if equals "<identity>", replace by identity element
|
1167 |
+
if C.P[i][j] == "<identity>":
|
1168 |
+
C.P[i][j] = C._schreier_free_group.identity
|
1169 |
+
elif isinstance(C.P[i][j], str):
|
1170 |
+
r = C._schreier_generators[y.index(C.P[i][j])]
|
1171 |
+
C.P[i][j] = r
|
1172 |
+
beta = C.table[i][j]
|
1173 |
+
C.P[beta][j + 1] = r**-1
|
1174 |
+
|
1175 |
+
def reidemeister_relators(C):
|
1176 |
+
R = C.fp_group.relators
|
1177 |
+
rels = [rewrite(C, coset, word) for word in R for coset in range(C.n)]
|
1178 |
+
order_1_gens = {i for i in rels if len(i) == 1}
|
1179 |
+
|
1180 |
+
# remove all the order 1 generators from relators
|
1181 |
+
rels = list(filter(lambda rel: rel not in order_1_gens, rels))
|
1182 |
+
|
1183 |
+
# replace order 1 generators by identity element in reidemeister relators
|
1184 |
+
for i in range(len(rels)):
|
1185 |
+
w = rels[i]
|
1186 |
+
w = w.eliminate_words(order_1_gens, _all=True)
|
1187 |
+
rels[i] = w
|
1188 |
+
|
1189 |
+
C._schreier_generators = [i for i in C._schreier_generators
|
1190 |
+
if not (i in order_1_gens or i**-1 in order_1_gens)]
|
1191 |
+
|
1192 |
+
# Tietze transformation 1 i.e TT_1
|
1193 |
+
# remove cyclic conjugate elements from relators
|
1194 |
+
i = 0
|
1195 |
+
while i < len(rels):
|
1196 |
+
w = rels[i]
|
1197 |
+
j = i + 1
|
1198 |
+
while j < len(rels):
|
1199 |
+
if w.is_cyclic_conjugate(rels[j]):
|
1200 |
+
del rels[j]
|
1201 |
+
else:
|
1202 |
+
j += 1
|
1203 |
+
i += 1
|
1204 |
+
|
1205 |
+
C._reidemeister_relators = rels
|
1206 |
+
|
1207 |
+
|
1208 |
+
def rewrite(C, alpha, w):
|
1209 |
+
"""
|
1210 |
+
Parameters
|
1211 |
+
==========
|
1212 |
+
|
1213 |
+
C: CosetTable
|
1214 |
+
alpha: A live coset
|
1215 |
+
w: A word in `A*`
|
1216 |
+
|
1217 |
+
Returns
|
1218 |
+
=======
|
1219 |
+
|
1220 |
+
rho(tau(alpha), w)
|
1221 |
+
|
1222 |
+
Examples
|
1223 |
+
========
|
1224 |
+
|
1225 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, CosetTable, define_schreier_generators, rewrite
|
1226 |
+
>>> from sympy.combinatorics import free_group
|
1227 |
+
>>> F, x, y = free_group("x, y")
|
1228 |
+
>>> f = FpGroup(F, [x**2, y**3, (x*y)**6])
|
1229 |
+
>>> C = CosetTable(f, [])
|
1230 |
+
>>> C.table = [[1, 1, 2, 3], [0, 0, 4, 5], [4, 4, 3, 0], [5, 5, 0, 2], [2, 2, 5, 1], [3, 3, 1, 4]]
|
1231 |
+
>>> C.p = [0, 1, 2, 3, 4, 5]
|
1232 |
+
>>> define_schreier_generators(C)
|
1233 |
+
>>> rewrite(C, 0, (x*y)**6)
|
1234 |
+
x_4*y_2*x_3*x_1*x_2*y_4*x_5
|
1235 |
+
|
1236 |
+
"""
|
1237 |
+
v = C._schreier_free_group.identity
|
1238 |
+
for i in range(len(w)):
|
1239 |
+
x_i = w[i]
|
1240 |
+
v = v*C.P[alpha][C.A_dict[x_i]]
|
1241 |
+
alpha = C.table[alpha][C.A_dict[x_i]]
|
1242 |
+
return v
|
1243 |
+
|
1244 |
+
# Pg 350, section 2.5.2 from [2]
|
1245 |
+
def elimination_technique_2(C):
|
1246 |
+
"""
|
1247 |
+
This technique eliminates one generator at a time. Heuristically this
|
1248 |
+
seems superior in that we may select for elimination the generator with
|
1249 |
+
shortest equivalent string at each stage.
|
1250 |
+
|
1251 |
+
>>> from sympy.combinatorics import free_group
|
1252 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r, \
|
1253 |
+
reidemeister_relators, define_schreier_generators, elimination_technique_2
|
1254 |
+
>>> F, x, y = free_group("x, y")
|
1255 |
+
>>> f = FpGroup(F, [x**3, y**5, (x*y)**2]); H = [x*y, x**-1*y**-1*x*y*x]
|
1256 |
+
>>> C = coset_enumeration_r(f, H)
|
1257 |
+
>>> C.compress(); C.standardize()
|
1258 |
+
>>> define_schreier_generators(C)
|
1259 |
+
>>> reidemeister_relators(C)
|
1260 |
+
>>> elimination_technique_2(C)
|
1261 |
+
([y_1, y_2], [y_2**-3, y_2*y_1*y_2*y_1*y_2*y_1, y_1**2])
|
1262 |
+
|
1263 |
+
"""
|
1264 |
+
rels = C._reidemeister_relators
|
1265 |
+
rels.sort(reverse=True)
|
1266 |
+
gens = C._schreier_generators
|
1267 |
+
for i in range(len(gens) - 1, -1, -1):
|
1268 |
+
rel = rels[i]
|
1269 |
+
for j in range(len(gens) - 1, -1, -1):
|
1270 |
+
gen = gens[j]
|
1271 |
+
if rel.generator_count(gen) == 1:
|
1272 |
+
k = rel.exponent_sum(gen)
|
1273 |
+
gen_index = rel.index(gen**k)
|
1274 |
+
bk = rel.subword(gen_index + 1, len(rel))
|
1275 |
+
fw = rel.subword(0, gen_index)
|
1276 |
+
rep_by = (bk*fw)**(-1*k)
|
1277 |
+
del rels[i]; del gens[j]
|
1278 |
+
for l in range(len(rels)):
|
1279 |
+
rels[l] = rels[l].eliminate_word(gen, rep_by)
|
1280 |
+
break
|
1281 |
+
C._reidemeister_relators = rels
|
1282 |
+
C._schreier_generators = gens
|
1283 |
+
return C._schreier_generators, C._reidemeister_relators
|
1284 |
+
|
1285 |
+
def reidemeister_presentation(fp_grp, H, C=None, homomorphism=False):
|
1286 |
+
"""
|
1287 |
+
Parameters
|
1288 |
+
==========
|
1289 |
+
|
1290 |
+
fp_group: A finitely presented group, an instance of FpGroup
|
1291 |
+
H: A subgroup whose presentation is to be found, given as a list
|
1292 |
+
of words in generators of `fp_grp`
|
1293 |
+
homomorphism: When set to True, return a homomorphism from the subgroup
|
1294 |
+
to the parent group
|
1295 |
+
|
1296 |
+
Examples
|
1297 |
+
========
|
1298 |
+
|
1299 |
+
>>> from sympy.combinatorics import free_group
|
1300 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup, reidemeister_presentation
|
1301 |
+
>>> F, x, y = free_group("x, y")
|
1302 |
+
|
1303 |
+
Example 5.6 Pg. 177 from [1]
|
1304 |
+
>>> f = FpGroup(F, [x**3, y**5, (x*y)**2])
|
1305 |
+
>>> H = [x*y, x**-1*y**-1*x*y*x]
|
1306 |
+
>>> reidemeister_presentation(f, H)
|
1307 |
+
((y_1, y_2), (y_1**2, y_2**3, y_2*y_1*y_2*y_1*y_2*y_1))
|
1308 |
+
|
1309 |
+
Example 5.8 Pg. 183 from [1]
|
1310 |
+
>>> f = FpGroup(F, [x**3, y**3, (x*y)**3])
|
1311 |
+
>>> H = [x*y, x*y**-1]
|
1312 |
+
>>> reidemeister_presentation(f, H)
|
1313 |
+
((x_0, y_0), (x_0**3, y_0**3, x_0*y_0*x_0*y_0*x_0*y_0))
|
1314 |
+
|
1315 |
+
Exercises Q2. Pg 187 from [1]
|
1316 |
+
>>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3])
|
1317 |
+
>>> H = [x]
|
1318 |
+
>>> reidemeister_presentation(f, H)
|
1319 |
+
((x_0,), (x_0**4,))
|
1320 |
+
|
1321 |
+
Example 5.9 Pg. 183 from [1]
|
1322 |
+
>>> f = FpGroup(F, [x**3*y**-3, (x*y)**3, (x*y**-1)**2])
|
1323 |
+
>>> H = [x]
|
1324 |
+
>>> reidemeister_presentation(f, H)
|
1325 |
+
((x_0,), (x_0**6,))
|
1326 |
+
|
1327 |
+
"""
|
1328 |
+
if not C:
|
1329 |
+
C = coset_enumeration_r(fp_grp, H)
|
1330 |
+
C.compress(); C.standardize()
|
1331 |
+
define_schreier_generators(C, homomorphism=homomorphism)
|
1332 |
+
reidemeister_relators(C)
|
1333 |
+
gens, rels = C._schreier_generators, C._reidemeister_relators
|
1334 |
+
gens, rels = simplify_presentation(gens, rels, change_gens=True)
|
1335 |
+
|
1336 |
+
C.schreier_generators = tuple(gens)
|
1337 |
+
C.reidemeister_relators = tuple(rels)
|
1338 |
+
|
1339 |
+
if homomorphism:
|
1340 |
+
_gens = []
|
1341 |
+
for gen in gens:
|
1342 |
+
_gens.append(C._schreier_gen_elem[str(gen)])
|
1343 |
+
return C.schreier_generators, C.reidemeister_relators, _gens
|
1344 |
+
|
1345 |
+
return C.schreier_generators, C.reidemeister_relators
|
1346 |
+
|
1347 |
+
|
1348 |
+
FpGroupElement = FreeGroupElement
|
venv/lib/python3.10/site-packages/sympy/combinatorics/free_groups.py
ADDED
@@ -0,0 +1,1354 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
from sympy.core import S
|
4 |
+
from sympy.core.expr import Expr
|
5 |
+
from sympy.core.symbol import Symbol, symbols as _symbols
|
6 |
+
from sympy.core.sympify import CantSympify
|
7 |
+
from sympy.printing.defaults import DefaultPrinting
|
8 |
+
from sympy.utilities import public
|
9 |
+
from sympy.utilities.iterables import flatten, is_sequence
|
10 |
+
from sympy.utilities.magic import pollute
|
11 |
+
from sympy.utilities.misc import as_int
|
12 |
+
|
13 |
+
|
14 |
+
@public
|
15 |
+
def free_group(symbols):
|
16 |
+
"""Construct a free group returning ``(FreeGroup, (f_0, f_1, ..., f_(n-1))``.
|
17 |
+
|
18 |
+
Parameters
|
19 |
+
==========
|
20 |
+
|
21 |
+
symbols : str, Symbol/Expr or sequence of str, Symbol/Expr (may be empty)
|
22 |
+
|
23 |
+
Examples
|
24 |
+
========
|
25 |
+
|
26 |
+
>>> from sympy.combinatorics import free_group
|
27 |
+
>>> F, x, y, z = free_group("x, y, z")
|
28 |
+
>>> F
|
29 |
+
<free group on the generators (x, y, z)>
|
30 |
+
>>> x**2*y**-1
|
31 |
+
x**2*y**-1
|
32 |
+
>>> type(_)
|
33 |
+
<class 'sympy.combinatorics.free_groups.FreeGroupElement'>
|
34 |
+
|
35 |
+
"""
|
36 |
+
_free_group = FreeGroup(symbols)
|
37 |
+
return (_free_group,) + tuple(_free_group.generators)
|
38 |
+
|
39 |
+
@public
|
40 |
+
def xfree_group(symbols):
|
41 |
+
"""Construct a free group returning ``(FreeGroup, (f_0, f_1, ..., f_(n-1)))``.
|
42 |
+
|
43 |
+
Parameters
|
44 |
+
==========
|
45 |
+
|
46 |
+
symbols : str, Symbol/Expr or sequence of str, Symbol/Expr (may be empty)
|
47 |
+
|
48 |
+
Examples
|
49 |
+
========
|
50 |
+
|
51 |
+
>>> from sympy.combinatorics.free_groups import xfree_group
|
52 |
+
>>> F, (x, y, z) = xfree_group("x, y, z")
|
53 |
+
>>> F
|
54 |
+
<free group on the generators (x, y, z)>
|
55 |
+
>>> y**2*x**-2*z**-1
|
56 |
+
y**2*x**-2*z**-1
|
57 |
+
>>> type(_)
|
58 |
+
<class 'sympy.combinatorics.free_groups.FreeGroupElement'>
|
59 |
+
|
60 |
+
"""
|
61 |
+
_free_group = FreeGroup(symbols)
|
62 |
+
return (_free_group, _free_group.generators)
|
63 |
+
|
64 |
+
@public
|
65 |
+
def vfree_group(symbols):
|
66 |
+
"""Construct a free group and inject ``f_0, f_1, ..., f_(n-1)`` as symbols
|
67 |
+
into the global namespace.
|
68 |
+
|
69 |
+
Parameters
|
70 |
+
==========
|
71 |
+
|
72 |
+
symbols : str, Symbol/Expr or sequence of str, Symbol/Expr (may be empty)
|
73 |
+
|
74 |
+
Examples
|
75 |
+
========
|
76 |
+
|
77 |
+
>>> from sympy.combinatorics.free_groups import vfree_group
|
78 |
+
>>> vfree_group("x, y, z")
|
79 |
+
<free group on the generators (x, y, z)>
|
80 |
+
>>> x**2*y**-2*z # noqa: F821
|
81 |
+
x**2*y**-2*z
|
82 |
+
>>> type(_)
|
83 |
+
<class 'sympy.combinatorics.free_groups.FreeGroupElement'>
|
84 |
+
|
85 |
+
"""
|
86 |
+
_free_group = FreeGroup(symbols)
|
87 |
+
pollute([sym.name for sym in _free_group.symbols], _free_group.generators)
|
88 |
+
return _free_group
|
89 |
+
|
90 |
+
|
91 |
+
def _parse_symbols(symbols):
|
92 |
+
if not symbols:
|
93 |
+
return ()
|
94 |
+
if isinstance(symbols, str):
|
95 |
+
return _symbols(symbols, seq=True)
|
96 |
+
elif isinstance(symbols, (Expr, FreeGroupElement)):
|
97 |
+
return (symbols,)
|
98 |
+
elif is_sequence(symbols):
|
99 |
+
if all(isinstance(s, str) for s in symbols):
|
100 |
+
return _symbols(symbols)
|
101 |
+
elif all(isinstance(s, Expr) for s in symbols):
|
102 |
+
return symbols
|
103 |
+
raise ValueError("The type of `symbols` must be one of the following: "
|
104 |
+
"a str, Symbol/Expr or a sequence of "
|
105 |
+
"one of these types")
|
106 |
+
|
107 |
+
|
108 |
+
##############################################################################
|
109 |
+
# FREE GROUP #
|
110 |
+
##############################################################################
|
111 |
+
|
112 |
+
_free_group_cache: dict[int, FreeGroup] = {}
|
113 |
+
|
114 |
+
class FreeGroup(DefaultPrinting):
|
115 |
+
"""
|
116 |
+
Free group with finite or infinite number of generators. Its input API
|
117 |
+
is that of a str, Symbol/Expr or a sequence of one of
|
118 |
+
these types (which may be empty)
|
119 |
+
|
120 |
+
See Also
|
121 |
+
========
|
122 |
+
|
123 |
+
sympy.polys.rings.PolyRing
|
124 |
+
|
125 |
+
References
|
126 |
+
==========
|
127 |
+
|
128 |
+
.. [1] https://www.gap-system.org/Manuals/doc/ref/chap37.html
|
129 |
+
|
130 |
+
.. [2] https://en.wikipedia.org/wiki/Free_group
|
131 |
+
|
132 |
+
"""
|
133 |
+
is_associative = True
|
134 |
+
is_group = True
|
135 |
+
is_FreeGroup = True
|
136 |
+
is_PermutationGroup = False
|
137 |
+
relators: list[Expr] = []
|
138 |
+
|
139 |
+
def __new__(cls, symbols):
|
140 |
+
symbols = tuple(_parse_symbols(symbols))
|
141 |
+
rank = len(symbols)
|
142 |
+
_hash = hash((cls.__name__, symbols, rank))
|
143 |
+
obj = _free_group_cache.get(_hash)
|
144 |
+
|
145 |
+
if obj is None:
|
146 |
+
obj = object.__new__(cls)
|
147 |
+
obj._hash = _hash
|
148 |
+
obj._rank = rank
|
149 |
+
# dtype method is used to create new instances of FreeGroupElement
|
150 |
+
obj.dtype = type("FreeGroupElement", (FreeGroupElement,), {"group": obj})
|
151 |
+
obj.symbols = symbols
|
152 |
+
obj.generators = obj._generators()
|
153 |
+
obj._gens_set = set(obj.generators)
|
154 |
+
for symbol, generator in zip(obj.symbols, obj.generators):
|
155 |
+
if isinstance(symbol, Symbol):
|
156 |
+
name = symbol.name
|
157 |
+
if hasattr(obj, name):
|
158 |
+
setattr(obj, name, generator)
|
159 |
+
|
160 |
+
_free_group_cache[_hash] = obj
|
161 |
+
|
162 |
+
return obj
|
163 |
+
|
164 |
+
def _generators(group):
|
165 |
+
"""Returns the generators of the FreeGroup.
|
166 |
+
|
167 |
+
Examples
|
168 |
+
========
|
169 |
+
|
170 |
+
>>> from sympy.combinatorics import free_group
|
171 |
+
>>> F, x, y, z = free_group("x, y, z")
|
172 |
+
>>> F.generators
|
173 |
+
(x, y, z)
|
174 |
+
|
175 |
+
"""
|
176 |
+
gens = []
|
177 |
+
for sym in group.symbols:
|
178 |
+
elm = ((sym, 1),)
|
179 |
+
gens.append(group.dtype(elm))
|
180 |
+
return tuple(gens)
|
181 |
+
|
182 |
+
def clone(self, symbols=None):
|
183 |
+
return self.__class__(symbols or self.symbols)
|
184 |
+
|
185 |
+
def __contains__(self, i):
|
186 |
+
"""Return True if ``i`` is contained in FreeGroup."""
|
187 |
+
if not isinstance(i, FreeGroupElement):
|
188 |
+
return False
|
189 |
+
group = i.group
|
190 |
+
return self == group
|
191 |
+
|
192 |
+
def __hash__(self):
|
193 |
+
return self._hash
|
194 |
+
|
195 |
+
def __len__(self):
|
196 |
+
return self.rank
|
197 |
+
|
198 |
+
def __str__(self):
|
199 |
+
if self.rank > 30:
|
200 |
+
str_form = "<free group with %s generators>" % self.rank
|
201 |
+
else:
|
202 |
+
str_form = "<free group on the generators "
|
203 |
+
gens = self.generators
|
204 |
+
str_form += str(gens) + ">"
|
205 |
+
return str_form
|
206 |
+
|
207 |
+
__repr__ = __str__
|
208 |
+
|
209 |
+
def __getitem__(self, index):
|
210 |
+
symbols = self.symbols[index]
|
211 |
+
return self.clone(symbols=symbols)
|
212 |
+
|
213 |
+
def __eq__(self, other):
|
214 |
+
"""No ``FreeGroup`` is equal to any "other" ``FreeGroup``.
|
215 |
+
"""
|
216 |
+
return self is other
|
217 |
+
|
218 |
+
def index(self, gen):
|
219 |
+
"""Return the index of the generator `gen` from ``(f_0, ..., f_(n-1))``.
|
220 |
+
|
221 |
+
Examples
|
222 |
+
========
|
223 |
+
|
224 |
+
>>> from sympy.combinatorics import free_group
|
225 |
+
>>> F, x, y = free_group("x, y")
|
226 |
+
>>> F.index(y)
|
227 |
+
1
|
228 |
+
>>> F.index(x)
|
229 |
+
0
|
230 |
+
|
231 |
+
"""
|
232 |
+
if isinstance(gen, self.dtype):
|
233 |
+
return self.generators.index(gen)
|
234 |
+
else:
|
235 |
+
raise ValueError("expected a generator of Free Group %s, got %s" % (self, gen))
|
236 |
+
|
237 |
+
def order(self):
|
238 |
+
"""Return the order of the free group.
|
239 |
+
|
240 |
+
Examples
|
241 |
+
========
|
242 |
+
|
243 |
+
>>> from sympy.combinatorics import free_group
|
244 |
+
>>> F, x, y = free_group("x, y")
|
245 |
+
>>> F.order()
|
246 |
+
oo
|
247 |
+
|
248 |
+
>>> free_group("")[0].order()
|
249 |
+
1
|
250 |
+
|
251 |
+
"""
|
252 |
+
if self.rank == 0:
|
253 |
+
return S.One
|
254 |
+
else:
|
255 |
+
return S.Infinity
|
256 |
+
|
257 |
+
@property
|
258 |
+
def elements(self):
|
259 |
+
"""
|
260 |
+
Return the elements of the free group.
|
261 |
+
|
262 |
+
Examples
|
263 |
+
========
|
264 |
+
|
265 |
+
>>> from sympy.combinatorics import free_group
|
266 |
+
>>> (z,) = free_group("")
|
267 |
+
>>> z.elements
|
268 |
+
{<identity>}
|
269 |
+
|
270 |
+
"""
|
271 |
+
if self.rank == 0:
|
272 |
+
# A set containing Identity element of `FreeGroup` self is returned
|
273 |
+
return {self.identity}
|
274 |
+
else:
|
275 |
+
raise ValueError("Group contains infinitely many elements"
|
276 |
+
", hence cannot be represented")
|
277 |
+
|
278 |
+
@property
|
279 |
+
def rank(self):
|
280 |
+
r"""
|
281 |
+
In group theory, the `rank` of a group `G`, denoted `G.rank`,
|
282 |
+
can refer to the smallest cardinality of a generating set
|
283 |
+
for G, that is
|
284 |
+
|
285 |
+
\operatorname{rank}(G)=\min\{ |X|: X\subseteq G, \left\langle X\right\rangle =G\}.
|
286 |
+
|
287 |
+
"""
|
288 |
+
return self._rank
|
289 |
+
|
290 |
+
@property
|
291 |
+
def is_abelian(self):
|
292 |
+
"""Returns if the group is Abelian.
|
293 |
+
|
294 |
+
Examples
|
295 |
+
========
|
296 |
+
|
297 |
+
>>> from sympy.combinatorics import free_group
|
298 |
+
>>> f, x, y, z = free_group("x y z")
|
299 |
+
>>> f.is_abelian
|
300 |
+
False
|
301 |
+
|
302 |
+
"""
|
303 |
+
return self.rank in (0, 1)
|
304 |
+
|
305 |
+
@property
|
306 |
+
def identity(self):
|
307 |
+
"""Returns the identity element of free group."""
|
308 |
+
return self.dtype()
|
309 |
+
|
310 |
+
def contains(self, g):
|
311 |
+
"""Tests if Free Group element ``g`` belong to self, ``G``.
|
312 |
+
|
313 |
+
In mathematical terms any linear combination of generators
|
314 |
+
of a Free Group is contained in it.
|
315 |
+
|
316 |
+
Examples
|
317 |
+
========
|
318 |
+
|
319 |
+
>>> from sympy.combinatorics import free_group
|
320 |
+
>>> f, x, y, z = free_group("x y z")
|
321 |
+
>>> f.contains(x**3*y**2)
|
322 |
+
True
|
323 |
+
|
324 |
+
"""
|
325 |
+
if not isinstance(g, FreeGroupElement):
|
326 |
+
return False
|
327 |
+
elif self != g.group:
|
328 |
+
return False
|
329 |
+
else:
|
330 |
+
return True
|
331 |
+
|
332 |
+
def center(self):
|
333 |
+
"""Returns the center of the free group `self`."""
|
334 |
+
return {self.identity}
|
335 |
+
|
336 |
+
|
337 |
+
############################################################################
|
338 |
+
# FreeGroupElement #
|
339 |
+
############################################################################
|
340 |
+
|
341 |
+
|
342 |
+
class FreeGroupElement(CantSympify, DefaultPrinting, tuple):
|
343 |
+
"""Used to create elements of FreeGroup. It cannot be used directly to
|
344 |
+
create a free group element. It is called by the `dtype` method of the
|
345 |
+
`FreeGroup` class.
|
346 |
+
|
347 |
+
"""
|
348 |
+
is_assoc_word = True
|
349 |
+
|
350 |
+
def new(self, init):
|
351 |
+
return self.__class__(init)
|
352 |
+
|
353 |
+
_hash = None
|
354 |
+
|
355 |
+
def __hash__(self):
|
356 |
+
_hash = self._hash
|
357 |
+
if _hash is None:
|
358 |
+
self._hash = _hash = hash((self.group, frozenset(tuple(self))))
|
359 |
+
return _hash
|
360 |
+
|
361 |
+
def copy(self):
|
362 |
+
return self.new(self)
|
363 |
+
|
364 |
+
@property
|
365 |
+
def is_identity(self):
|
366 |
+
if self.array_form == ():
|
367 |
+
return True
|
368 |
+
else:
|
369 |
+
return False
|
370 |
+
|
371 |
+
@property
|
372 |
+
def array_form(self):
|
373 |
+
"""
|
374 |
+
SymPy provides two different internal kinds of representation
|
375 |
+
of associative words. The first one is called the `array_form`
|
376 |
+
which is a tuple containing `tuples` as its elements, where the
|
377 |
+
size of each tuple is two. At the first position the tuple
|
378 |
+
contains the `symbol-generator`, while at the second position
|
379 |
+
of tuple contains the exponent of that generator at the position.
|
380 |
+
Since elements (i.e. words) do not commute, the indexing of tuple
|
381 |
+
makes that property to stay.
|
382 |
+
|
383 |
+
The structure in ``array_form`` of ``FreeGroupElement`` is of form:
|
384 |
+
|
385 |
+
``( ( symbol_of_gen, exponent ), ( , ), ... ( , ) )``
|
386 |
+
|
387 |
+
Examples
|
388 |
+
========
|
389 |
+
|
390 |
+
>>> from sympy.combinatorics import free_group
|
391 |
+
>>> f, x, y, z = free_group("x y z")
|
392 |
+
>>> (x*z).array_form
|
393 |
+
((x, 1), (z, 1))
|
394 |
+
>>> (x**2*z*y*x**2).array_form
|
395 |
+
((x, 2), (z, 1), (y, 1), (x, 2))
|
396 |
+
|
397 |
+
See Also
|
398 |
+
========
|
399 |
+
|
400 |
+
letter_repr
|
401 |
+
|
402 |
+
"""
|
403 |
+
return tuple(self)
|
404 |
+
|
405 |
+
@property
|
406 |
+
def letter_form(self):
|
407 |
+
"""
|
408 |
+
The letter representation of a ``FreeGroupElement`` is a tuple
|
409 |
+
of generator symbols, with each entry corresponding to a group
|
410 |
+
generator. Inverses of the generators are represented by
|
411 |
+
negative generator symbols.
|
412 |
+
|
413 |
+
Examples
|
414 |
+
========
|
415 |
+
|
416 |
+
>>> from sympy.combinatorics import free_group
|
417 |
+
>>> f, a, b, c, d = free_group("a b c d")
|
418 |
+
>>> (a**3).letter_form
|
419 |
+
(a, a, a)
|
420 |
+
>>> (a**2*d**-2*a*b**-4).letter_form
|
421 |
+
(a, a, -d, -d, a, -b, -b, -b, -b)
|
422 |
+
>>> (a**-2*b**3*d).letter_form
|
423 |
+
(-a, -a, b, b, b, d)
|
424 |
+
|
425 |
+
See Also
|
426 |
+
========
|
427 |
+
|
428 |
+
array_form
|
429 |
+
|
430 |
+
"""
|
431 |
+
return tuple(flatten([(i,)*j if j > 0 else (-i,)*(-j)
|
432 |
+
for i, j in self.array_form]))
|
433 |
+
|
434 |
+
def __getitem__(self, i):
|
435 |
+
group = self.group
|
436 |
+
r = self.letter_form[i]
|
437 |
+
if r.is_Symbol:
|
438 |
+
return group.dtype(((r, 1),))
|
439 |
+
else:
|
440 |
+
return group.dtype(((-r, -1),))
|
441 |
+
|
442 |
+
def index(self, gen):
|
443 |
+
if len(gen) != 1:
|
444 |
+
raise ValueError()
|
445 |
+
return (self.letter_form).index(gen.letter_form[0])
|
446 |
+
|
447 |
+
@property
|
448 |
+
def letter_form_elm(self):
|
449 |
+
"""
|
450 |
+
"""
|
451 |
+
group = self.group
|
452 |
+
r = self.letter_form
|
453 |
+
return [group.dtype(((elm,1),)) if elm.is_Symbol \
|
454 |
+
else group.dtype(((-elm,-1),)) for elm in r]
|
455 |
+
|
456 |
+
@property
|
457 |
+
def ext_rep(self):
|
458 |
+
"""This is called the External Representation of ``FreeGroupElement``
|
459 |
+
"""
|
460 |
+
return tuple(flatten(self.array_form))
|
461 |
+
|
462 |
+
def __contains__(self, gen):
|
463 |
+
return gen.array_form[0][0] in tuple([r[0] for r in self.array_form])
|
464 |
+
|
465 |
+
def __str__(self):
|
466 |
+
if self.is_identity:
|
467 |
+
return "<identity>"
|
468 |
+
|
469 |
+
str_form = ""
|
470 |
+
array_form = self.array_form
|
471 |
+
for i in range(len(array_form)):
|
472 |
+
if i == len(array_form) - 1:
|
473 |
+
if array_form[i][1] == 1:
|
474 |
+
str_form += str(array_form[i][0])
|
475 |
+
else:
|
476 |
+
str_form += str(array_form[i][0]) + \
|
477 |
+
"**" + str(array_form[i][1])
|
478 |
+
else:
|
479 |
+
if array_form[i][1] == 1:
|
480 |
+
str_form += str(array_form[i][0]) + "*"
|
481 |
+
else:
|
482 |
+
str_form += str(array_form[i][0]) + \
|
483 |
+
"**" + str(array_form[i][1]) + "*"
|
484 |
+
return str_form
|
485 |
+
|
486 |
+
__repr__ = __str__
|
487 |
+
|
488 |
+
def __pow__(self, n):
|
489 |
+
n = as_int(n)
|
490 |
+
group = self.group
|
491 |
+
if n == 0:
|
492 |
+
return group.identity
|
493 |
+
|
494 |
+
if n < 0:
|
495 |
+
n = -n
|
496 |
+
return (self.inverse())**n
|
497 |
+
|
498 |
+
result = self
|
499 |
+
for i in range(n - 1):
|
500 |
+
result = result*self
|
501 |
+
# this method can be improved instead of just returning the
|
502 |
+
# multiplication of elements
|
503 |
+
return result
|
504 |
+
|
505 |
+
def __mul__(self, other):
|
506 |
+
"""Returns the product of elements belonging to the same ``FreeGroup``.
|
507 |
+
|
508 |
+
Examples
|
509 |
+
========
|
510 |
+
|
511 |
+
>>> from sympy.combinatorics import free_group
|
512 |
+
>>> f, x, y, z = free_group("x y z")
|
513 |
+
>>> x*y**2*y**-4
|
514 |
+
x*y**-2
|
515 |
+
>>> z*y**-2
|
516 |
+
z*y**-2
|
517 |
+
>>> x**2*y*y**-1*x**-2
|
518 |
+
<identity>
|
519 |
+
|
520 |
+
"""
|
521 |
+
group = self.group
|
522 |
+
if not isinstance(other, group.dtype):
|
523 |
+
raise TypeError("only FreeGroup elements of same FreeGroup can "
|
524 |
+
"be multiplied")
|
525 |
+
if self.is_identity:
|
526 |
+
return other
|
527 |
+
if other.is_identity:
|
528 |
+
return self
|
529 |
+
r = list(self.array_form + other.array_form)
|
530 |
+
zero_mul_simp(r, len(self.array_form) - 1)
|
531 |
+
return group.dtype(tuple(r))
|
532 |
+
|
533 |
+
def __truediv__(self, other):
|
534 |
+
group = self.group
|
535 |
+
if not isinstance(other, group.dtype):
|
536 |
+
raise TypeError("only FreeGroup elements of same FreeGroup can "
|
537 |
+
"be multiplied")
|
538 |
+
return self*(other.inverse())
|
539 |
+
|
540 |
+
def __rtruediv__(self, other):
|
541 |
+
group = self.group
|
542 |
+
if not isinstance(other, group.dtype):
|
543 |
+
raise TypeError("only FreeGroup elements of same FreeGroup can "
|
544 |
+
"be multiplied")
|
545 |
+
return other*(self.inverse())
|
546 |
+
|
547 |
+
def __add__(self, other):
|
548 |
+
return NotImplemented
|
549 |
+
|
550 |
+
def inverse(self):
|
551 |
+
"""
|
552 |
+
Returns the inverse of a ``FreeGroupElement`` element
|
553 |
+
|
554 |
+
Examples
|
555 |
+
========
|
556 |
+
|
557 |
+
>>> from sympy.combinatorics import free_group
|
558 |
+
>>> f, x, y, z = free_group("x y z")
|
559 |
+
>>> x.inverse()
|
560 |
+
x**-1
|
561 |
+
>>> (x*y).inverse()
|
562 |
+
y**-1*x**-1
|
563 |
+
|
564 |
+
"""
|
565 |
+
group = self.group
|
566 |
+
r = tuple([(i, -j) for i, j in self.array_form[::-1]])
|
567 |
+
return group.dtype(r)
|
568 |
+
|
569 |
+
def order(self):
|
570 |
+
"""Find the order of a ``FreeGroupElement``.
|
571 |
+
|
572 |
+
Examples
|
573 |
+
========
|
574 |
+
|
575 |
+
>>> from sympy.combinatorics import free_group
|
576 |
+
>>> f, x, y = free_group("x y")
|
577 |
+
>>> (x**2*y*y**-1*x**-2).order()
|
578 |
+
1
|
579 |
+
|
580 |
+
"""
|
581 |
+
if self.is_identity:
|
582 |
+
return S.One
|
583 |
+
else:
|
584 |
+
return S.Infinity
|
585 |
+
|
586 |
+
def commutator(self, other):
|
587 |
+
"""
|
588 |
+
Return the commutator of `self` and `x`: ``~x*~self*x*self``
|
589 |
+
|
590 |
+
"""
|
591 |
+
group = self.group
|
592 |
+
if not isinstance(other, group.dtype):
|
593 |
+
raise ValueError("commutator of only FreeGroupElement of the same "
|
594 |
+
"FreeGroup exists")
|
595 |
+
else:
|
596 |
+
return self.inverse()*other.inverse()*self*other
|
597 |
+
|
598 |
+
def eliminate_words(self, words, _all=False, inverse=True):
|
599 |
+
'''
|
600 |
+
Replace each subword from the dictionary `words` by words[subword].
|
601 |
+
If words is a list, replace the words by the identity.
|
602 |
+
|
603 |
+
'''
|
604 |
+
again = True
|
605 |
+
new = self
|
606 |
+
if isinstance(words, dict):
|
607 |
+
while again:
|
608 |
+
again = False
|
609 |
+
for sub in words:
|
610 |
+
prev = new
|
611 |
+
new = new.eliminate_word(sub, words[sub], _all=_all, inverse=inverse)
|
612 |
+
if new != prev:
|
613 |
+
again = True
|
614 |
+
else:
|
615 |
+
while again:
|
616 |
+
again = False
|
617 |
+
for sub in words:
|
618 |
+
prev = new
|
619 |
+
new = new.eliminate_word(sub, _all=_all, inverse=inverse)
|
620 |
+
if new != prev:
|
621 |
+
again = True
|
622 |
+
return new
|
623 |
+
|
624 |
+
def eliminate_word(self, gen, by=None, _all=False, inverse=True):
|
625 |
+
"""
|
626 |
+
For an associative word `self`, a subword `gen`, and an associative
|
627 |
+
word `by` (identity by default), return the associative word obtained by
|
628 |
+
replacing each occurrence of `gen` in `self` by `by`. If `_all = True`,
|
629 |
+
the occurrences of `gen` that may appear after the first substitution will
|
630 |
+
also be replaced and so on until no occurrences are found. This might not
|
631 |
+
always terminate (e.g. `(x).eliminate_word(x, x**2, _all=True)`).
|
632 |
+
|
633 |
+
Examples
|
634 |
+
========
|
635 |
+
|
636 |
+
>>> from sympy.combinatorics import free_group
|
637 |
+
>>> f, x, y = free_group("x y")
|
638 |
+
>>> w = x**5*y*x**2*y**-4*x
|
639 |
+
>>> w.eliminate_word( x, x**2 )
|
640 |
+
x**10*y*x**4*y**-4*x**2
|
641 |
+
>>> w.eliminate_word( x, y**-1 )
|
642 |
+
y**-11
|
643 |
+
>>> w.eliminate_word(x**5)
|
644 |
+
y*x**2*y**-4*x
|
645 |
+
>>> w.eliminate_word(x*y, y)
|
646 |
+
x**4*y*x**2*y**-4*x
|
647 |
+
|
648 |
+
See Also
|
649 |
+
========
|
650 |
+
substituted_word
|
651 |
+
|
652 |
+
"""
|
653 |
+
if by is None:
|
654 |
+
by = self.group.identity
|
655 |
+
if self.is_independent(gen) or gen == by:
|
656 |
+
return self
|
657 |
+
if gen == self:
|
658 |
+
return by
|
659 |
+
if gen**-1 == by:
|
660 |
+
_all = False
|
661 |
+
word = self
|
662 |
+
l = len(gen)
|
663 |
+
|
664 |
+
try:
|
665 |
+
i = word.subword_index(gen)
|
666 |
+
k = 1
|
667 |
+
except ValueError:
|
668 |
+
if not inverse:
|
669 |
+
return word
|
670 |
+
try:
|
671 |
+
i = word.subword_index(gen**-1)
|
672 |
+
k = -1
|
673 |
+
except ValueError:
|
674 |
+
return word
|
675 |
+
|
676 |
+
word = word.subword(0, i)*by**k*word.subword(i+l, len(word)).eliminate_word(gen, by)
|
677 |
+
|
678 |
+
if _all:
|
679 |
+
return word.eliminate_word(gen, by, _all=True, inverse=inverse)
|
680 |
+
else:
|
681 |
+
return word
|
682 |
+
|
683 |
+
def __len__(self):
|
684 |
+
"""
|
685 |
+
For an associative word `self`, returns the number of letters in it.
|
686 |
+
|
687 |
+
Examples
|
688 |
+
========
|
689 |
+
|
690 |
+
>>> from sympy.combinatorics import free_group
|
691 |
+
>>> f, a, b = free_group("a b")
|
692 |
+
>>> w = a**5*b*a**2*b**-4*a
|
693 |
+
>>> len(w)
|
694 |
+
13
|
695 |
+
>>> len(a**17)
|
696 |
+
17
|
697 |
+
>>> len(w**0)
|
698 |
+
0
|
699 |
+
|
700 |
+
"""
|
701 |
+
return sum(abs(j) for (i, j) in self)
|
702 |
+
|
703 |
+
def __eq__(self, other):
|
704 |
+
"""
|
705 |
+
Two associative words are equal if they are words over the
|
706 |
+
same alphabet and if they are sequences of the same letters.
|
707 |
+
This is equivalent to saying that the external representations
|
708 |
+
of the words are equal.
|
709 |
+
There is no "universal" empty word, every alphabet has its own
|
710 |
+
empty word.
|
711 |
+
|
712 |
+
Examples
|
713 |
+
========
|
714 |
+
|
715 |
+
>>> from sympy.combinatorics import free_group
|
716 |
+
>>> f, swapnil0, swapnil1 = free_group("swapnil0 swapnil1")
|
717 |
+
>>> f
|
718 |
+
<free group on the generators (swapnil0, swapnil1)>
|
719 |
+
>>> g, swap0, swap1 = free_group("swap0 swap1")
|
720 |
+
>>> g
|
721 |
+
<free group on the generators (swap0, swap1)>
|
722 |
+
|
723 |
+
>>> swapnil0 == swapnil1
|
724 |
+
False
|
725 |
+
>>> swapnil0*swapnil1 == swapnil1/swapnil1*swapnil0*swapnil1
|
726 |
+
True
|
727 |
+
>>> swapnil0*swapnil1 == swapnil1*swapnil0
|
728 |
+
False
|
729 |
+
>>> swapnil1**0 == swap0**0
|
730 |
+
False
|
731 |
+
|
732 |
+
"""
|
733 |
+
group = self.group
|
734 |
+
if not isinstance(other, group.dtype):
|
735 |
+
return False
|
736 |
+
return tuple.__eq__(self, other)
|
737 |
+
|
738 |
+
def __lt__(self, other):
|
739 |
+
"""
|
740 |
+
The ordering of associative words is defined by length and
|
741 |
+
lexicography (this ordering is called short-lex ordering), that
|
742 |
+
is, shorter words are smaller than longer words, and words of the
|
743 |
+
same length are compared w.r.t. the lexicographical ordering induced
|
744 |
+
by the ordering of generators. Generators are sorted according
|
745 |
+
to the order in which they were created. If the generators are
|
746 |
+
invertible then each generator `g` is larger than its inverse `g^{-1}`,
|
747 |
+
and `g^{-1}` is larger than every generator that is smaller than `g`.
|
748 |
+
|
749 |
+
Examples
|
750 |
+
========
|
751 |
+
|
752 |
+
>>> from sympy.combinatorics import free_group
|
753 |
+
>>> f, a, b = free_group("a b")
|
754 |
+
>>> b < a
|
755 |
+
False
|
756 |
+
>>> a < a.inverse()
|
757 |
+
False
|
758 |
+
|
759 |
+
"""
|
760 |
+
group = self.group
|
761 |
+
if not isinstance(other, group.dtype):
|
762 |
+
raise TypeError("only FreeGroup elements of same FreeGroup can "
|
763 |
+
"be compared")
|
764 |
+
l = len(self)
|
765 |
+
m = len(other)
|
766 |
+
# implement lenlex order
|
767 |
+
if l < m:
|
768 |
+
return True
|
769 |
+
elif l > m:
|
770 |
+
return False
|
771 |
+
for i in range(l):
|
772 |
+
a = self[i].array_form[0]
|
773 |
+
b = other[i].array_form[0]
|
774 |
+
p = group.symbols.index(a[0])
|
775 |
+
q = group.symbols.index(b[0])
|
776 |
+
if p < q:
|
777 |
+
return True
|
778 |
+
elif p > q:
|
779 |
+
return False
|
780 |
+
elif a[1] < b[1]:
|
781 |
+
return True
|
782 |
+
elif a[1] > b[1]:
|
783 |
+
return False
|
784 |
+
return False
|
785 |
+
|
786 |
+
def __le__(self, other):
|
787 |
+
return (self == other or self < other)
|
788 |
+
|
789 |
+
def __gt__(self, other):
|
790 |
+
"""
|
791 |
+
|
792 |
+
Examples
|
793 |
+
========
|
794 |
+
|
795 |
+
>>> from sympy.combinatorics import free_group
|
796 |
+
>>> f, x, y, z = free_group("x y z")
|
797 |
+
>>> y**2 > x**2
|
798 |
+
True
|
799 |
+
>>> y*z > z*y
|
800 |
+
False
|
801 |
+
>>> x > x.inverse()
|
802 |
+
True
|
803 |
+
|
804 |
+
"""
|
805 |
+
group = self.group
|
806 |
+
if not isinstance(other, group.dtype):
|
807 |
+
raise TypeError("only FreeGroup elements of same FreeGroup can "
|
808 |
+
"be compared")
|
809 |
+
return not self <= other
|
810 |
+
|
811 |
+
def __ge__(self, other):
|
812 |
+
return not self < other
|
813 |
+
|
814 |
+
def exponent_sum(self, gen):
|
815 |
+
"""
|
816 |
+
For an associative word `self` and a generator or inverse of generator
|
817 |
+
`gen`, ``exponent_sum`` returns the number of times `gen` appears in
|
818 |
+
`self` minus the number of times its inverse appears in `self`. If
|
819 |
+
neither `gen` nor its inverse occur in `self` then 0 is returned.
|
820 |
+
|
821 |
+
Examples
|
822 |
+
========
|
823 |
+
|
824 |
+
>>> from sympy.combinatorics import free_group
|
825 |
+
>>> F, x, y = free_group("x, y")
|
826 |
+
>>> w = x**2*y**3
|
827 |
+
>>> w.exponent_sum(x)
|
828 |
+
2
|
829 |
+
>>> w.exponent_sum(x**-1)
|
830 |
+
-2
|
831 |
+
>>> w = x**2*y**4*x**-3
|
832 |
+
>>> w.exponent_sum(x)
|
833 |
+
-1
|
834 |
+
|
835 |
+
See Also
|
836 |
+
========
|
837 |
+
|
838 |
+
generator_count
|
839 |
+
|
840 |
+
"""
|
841 |
+
if len(gen) != 1:
|
842 |
+
raise ValueError("gen must be a generator or inverse of a generator")
|
843 |
+
s = gen.array_form[0]
|
844 |
+
return s[1]*sum([i[1] for i in self.array_form if i[0] == s[0]])
|
845 |
+
|
846 |
+
def generator_count(self, gen):
|
847 |
+
"""
|
848 |
+
For an associative word `self` and a generator `gen`,
|
849 |
+
``generator_count`` returns the multiplicity of generator
|
850 |
+
`gen` in `self`.
|
851 |
+
|
852 |
+
Examples
|
853 |
+
========
|
854 |
+
|
855 |
+
>>> from sympy.combinatorics import free_group
|
856 |
+
>>> F, x, y = free_group("x, y")
|
857 |
+
>>> w = x**2*y**3
|
858 |
+
>>> w.generator_count(x)
|
859 |
+
2
|
860 |
+
>>> w = x**2*y**4*x**-3
|
861 |
+
>>> w.generator_count(x)
|
862 |
+
5
|
863 |
+
|
864 |
+
See Also
|
865 |
+
========
|
866 |
+
|
867 |
+
exponent_sum
|
868 |
+
|
869 |
+
"""
|
870 |
+
if len(gen) != 1 or gen.array_form[0][1] < 0:
|
871 |
+
raise ValueError("gen must be a generator")
|
872 |
+
s = gen.array_form[0]
|
873 |
+
return s[1]*sum([abs(i[1]) for i in self.array_form if i[0] == s[0]])
|
874 |
+
|
875 |
+
def subword(self, from_i, to_j, strict=True):
|
876 |
+
"""
|
877 |
+
For an associative word `self` and two positive integers `from_i` and
|
878 |
+
`to_j`, `subword` returns the subword of `self` that begins at position
|
879 |
+
`from_i` and ends at `to_j - 1`, indexing is done with origin 0.
|
880 |
+
|
881 |
+
Examples
|
882 |
+
========
|
883 |
+
|
884 |
+
>>> from sympy.combinatorics import free_group
|
885 |
+
>>> f, a, b = free_group("a b")
|
886 |
+
>>> w = a**5*b*a**2*b**-4*a
|
887 |
+
>>> w.subword(2, 6)
|
888 |
+
a**3*b
|
889 |
+
|
890 |
+
"""
|
891 |
+
group = self.group
|
892 |
+
if not strict:
|
893 |
+
from_i = max(from_i, 0)
|
894 |
+
to_j = min(len(self), to_j)
|
895 |
+
if from_i < 0 or to_j > len(self):
|
896 |
+
raise ValueError("`from_i`, `to_j` must be positive and no greater than "
|
897 |
+
"the length of associative word")
|
898 |
+
if to_j <= from_i:
|
899 |
+
return group.identity
|
900 |
+
else:
|
901 |
+
letter_form = self.letter_form[from_i: to_j]
|
902 |
+
array_form = letter_form_to_array_form(letter_form, group)
|
903 |
+
return group.dtype(array_form)
|
904 |
+
|
905 |
+
def subword_index(self, word, start = 0):
|
906 |
+
'''
|
907 |
+
Find the index of `word` in `self`.
|
908 |
+
|
909 |
+
Examples
|
910 |
+
========
|
911 |
+
|
912 |
+
>>> from sympy.combinatorics import free_group
|
913 |
+
>>> f, a, b = free_group("a b")
|
914 |
+
>>> w = a**2*b*a*b**3
|
915 |
+
>>> w.subword_index(a*b*a*b)
|
916 |
+
1
|
917 |
+
|
918 |
+
'''
|
919 |
+
l = len(word)
|
920 |
+
self_lf = self.letter_form
|
921 |
+
word_lf = word.letter_form
|
922 |
+
index = None
|
923 |
+
for i in range(start,len(self_lf)-l+1):
|
924 |
+
if self_lf[i:i+l] == word_lf:
|
925 |
+
index = i
|
926 |
+
break
|
927 |
+
if index is not None:
|
928 |
+
return index
|
929 |
+
else:
|
930 |
+
raise ValueError("The given word is not a subword of self")
|
931 |
+
|
932 |
+
def is_dependent(self, word):
|
933 |
+
"""
|
934 |
+
Examples
|
935 |
+
========
|
936 |
+
|
937 |
+
>>> from sympy.combinatorics import free_group
|
938 |
+
>>> F, x, y = free_group("x, y")
|
939 |
+
>>> (x**4*y**-3).is_dependent(x**4*y**-2)
|
940 |
+
True
|
941 |
+
>>> (x**2*y**-1).is_dependent(x*y)
|
942 |
+
False
|
943 |
+
>>> (x*y**2*x*y**2).is_dependent(x*y**2)
|
944 |
+
True
|
945 |
+
>>> (x**12).is_dependent(x**-4)
|
946 |
+
True
|
947 |
+
|
948 |
+
See Also
|
949 |
+
========
|
950 |
+
|
951 |
+
is_independent
|
952 |
+
|
953 |
+
"""
|
954 |
+
try:
|
955 |
+
return self.subword_index(word) is not None
|
956 |
+
except ValueError:
|
957 |
+
pass
|
958 |
+
try:
|
959 |
+
return self.subword_index(word**-1) is not None
|
960 |
+
except ValueError:
|
961 |
+
return False
|
962 |
+
|
963 |
+
def is_independent(self, word):
|
964 |
+
"""
|
965 |
+
|
966 |
+
See Also
|
967 |
+
========
|
968 |
+
|
969 |
+
is_dependent
|
970 |
+
|
971 |
+
"""
|
972 |
+
return not self.is_dependent(word)
|
973 |
+
|
974 |
+
def contains_generators(self):
|
975 |
+
"""
|
976 |
+
Examples
|
977 |
+
========
|
978 |
+
|
979 |
+
>>> from sympy.combinatorics import free_group
|
980 |
+
>>> F, x, y, z = free_group("x, y, z")
|
981 |
+
>>> (x**2*y**-1).contains_generators()
|
982 |
+
{x, y}
|
983 |
+
>>> (x**3*z).contains_generators()
|
984 |
+
{x, z}
|
985 |
+
|
986 |
+
"""
|
987 |
+
group = self.group
|
988 |
+
gens = set()
|
989 |
+
for syllable in self.array_form:
|
990 |
+
gens.add(group.dtype(((syllable[0], 1),)))
|
991 |
+
return set(gens)
|
992 |
+
|
993 |
+
def cyclic_subword(self, from_i, to_j):
|
994 |
+
group = self.group
|
995 |
+
l = len(self)
|
996 |
+
letter_form = self.letter_form
|
997 |
+
period1 = int(from_i/l)
|
998 |
+
if from_i >= l:
|
999 |
+
from_i -= l*period1
|
1000 |
+
to_j -= l*period1
|
1001 |
+
diff = to_j - from_i
|
1002 |
+
word = letter_form[from_i: to_j]
|
1003 |
+
period2 = int(to_j/l) - 1
|
1004 |
+
word += letter_form*period2 + letter_form[:diff-l+from_i-l*period2]
|
1005 |
+
word = letter_form_to_array_form(word, group)
|
1006 |
+
return group.dtype(word)
|
1007 |
+
|
1008 |
+
def cyclic_conjugates(self):
|
1009 |
+
"""Returns a words which are cyclic to the word `self`.
|
1010 |
+
|
1011 |
+
Examples
|
1012 |
+
========
|
1013 |
+
|
1014 |
+
>>> from sympy.combinatorics import free_group
|
1015 |
+
>>> F, x, y = free_group("x, y")
|
1016 |
+
>>> w = x*y*x*y*x
|
1017 |
+
>>> w.cyclic_conjugates()
|
1018 |
+
{x*y*x**2*y, x**2*y*x*y, y*x*y*x**2, y*x**2*y*x, x*y*x*y*x}
|
1019 |
+
>>> s = x*y*x**2*y*x
|
1020 |
+
>>> s.cyclic_conjugates()
|
1021 |
+
{x**2*y*x**2*y, y*x**2*y*x**2, x*y*x**2*y*x}
|
1022 |
+
|
1023 |
+
References
|
1024 |
+
==========
|
1025 |
+
|
1026 |
+
.. [1] https://planetmath.org/cyclicpermutation
|
1027 |
+
|
1028 |
+
"""
|
1029 |
+
return {self.cyclic_subword(i, i+len(self)) for i in range(len(self))}
|
1030 |
+
|
1031 |
+
def is_cyclic_conjugate(self, w):
|
1032 |
+
"""
|
1033 |
+
Checks whether words ``self``, ``w`` are cyclic conjugates.
|
1034 |
+
|
1035 |
+
Examples
|
1036 |
+
========
|
1037 |
+
|
1038 |
+
>>> from sympy.combinatorics import free_group
|
1039 |
+
>>> F, x, y = free_group("x, y")
|
1040 |
+
>>> w1 = x**2*y**5
|
1041 |
+
>>> w2 = x*y**5*x
|
1042 |
+
>>> w1.is_cyclic_conjugate(w2)
|
1043 |
+
True
|
1044 |
+
>>> w3 = x**-1*y**5*x**-1
|
1045 |
+
>>> w3.is_cyclic_conjugate(w2)
|
1046 |
+
False
|
1047 |
+
|
1048 |
+
"""
|
1049 |
+
l1 = len(self)
|
1050 |
+
l2 = len(w)
|
1051 |
+
if l1 != l2:
|
1052 |
+
return False
|
1053 |
+
w1 = self.identity_cyclic_reduction()
|
1054 |
+
w2 = w.identity_cyclic_reduction()
|
1055 |
+
letter1 = w1.letter_form
|
1056 |
+
letter2 = w2.letter_form
|
1057 |
+
str1 = ' '.join(map(str, letter1))
|
1058 |
+
str2 = ' '.join(map(str, letter2))
|
1059 |
+
if len(str1) != len(str2):
|
1060 |
+
return False
|
1061 |
+
|
1062 |
+
return str1 in str2 + ' ' + str2
|
1063 |
+
|
1064 |
+
def number_syllables(self):
|
1065 |
+
"""Returns the number of syllables of the associative word `self`.
|
1066 |
+
|
1067 |
+
Examples
|
1068 |
+
========
|
1069 |
+
|
1070 |
+
>>> from sympy.combinatorics import free_group
|
1071 |
+
>>> f, swapnil0, swapnil1 = free_group("swapnil0 swapnil1")
|
1072 |
+
>>> (swapnil1**3*swapnil0*swapnil1**-1).number_syllables()
|
1073 |
+
3
|
1074 |
+
|
1075 |
+
"""
|
1076 |
+
return len(self.array_form)
|
1077 |
+
|
1078 |
+
def exponent_syllable(self, i):
|
1079 |
+
"""
|
1080 |
+
Returns the exponent of the `i`-th syllable of the associative word
|
1081 |
+
`self`.
|
1082 |
+
|
1083 |
+
Examples
|
1084 |
+
========
|
1085 |
+
|
1086 |
+
>>> from sympy.combinatorics import free_group
|
1087 |
+
>>> f, a, b = free_group("a b")
|
1088 |
+
>>> w = a**5*b*a**2*b**-4*a
|
1089 |
+
>>> w.exponent_syllable( 2 )
|
1090 |
+
2
|
1091 |
+
|
1092 |
+
"""
|
1093 |
+
return self.array_form[i][1]
|
1094 |
+
|
1095 |
+
def generator_syllable(self, i):
|
1096 |
+
"""
|
1097 |
+
Returns the symbol of the generator that is involved in the
|
1098 |
+
i-th syllable of the associative word `self`.
|
1099 |
+
|
1100 |
+
Examples
|
1101 |
+
========
|
1102 |
+
|
1103 |
+
>>> from sympy.combinatorics import free_group
|
1104 |
+
>>> f, a, b = free_group("a b")
|
1105 |
+
>>> w = a**5*b*a**2*b**-4*a
|
1106 |
+
>>> w.generator_syllable( 3 )
|
1107 |
+
b
|
1108 |
+
|
1109 |
+
"""
|
1110 |
+
return self.array_form[i][0]
|
1111 |
+
|
1112 |
+
def sub_syllables(self, from_i, to_j):
|
1113 |
+
"""
|
1114 |
+
`sub_syllables` returns the subword of the associative word `self` that
|
1115 |
+
consists of syllables from positions `from_to` to `to_j`, where
|
1116 |
+
`from_to` and `to_j` must be positive integers and indexing is done
|
1117 |
+
with origin 0.
|
1118 |
+
|
1119 |
+
Examples
|
1120 |
+
========
|
1121 |
+
|
1122 |
+
>>> from sympy.combinatorics import free_group
|
1123 |
+
>>> f, a, b = free_group("a, b")
|
1124 |
+
>>> w = a**5*b*a**2*b**-4*a
|
1125 |
+
>>> w.sub_syllables(1, 2)
|
1126 |
+
b
|
1127 |
+
>>> w.sub_syllables(3, 3)
|
1128 |
+
<identity>
|
1129 |
+
|
1130 |
+
"""
|
1131 |
+
if not isinstance(from_i, int) or not isinstance(to_j, int):
|
1132 |
+
raise ValueError("both arguments should be integers")
|
1133 |
+
group = self.group
|
1134 |
+
if to_j <= from_i:
|
1135 |
+
return group.identity
|
1136 |
+
else:
|
1137 |
+
r = tuple(self.array_form[from_i: to_j])
|
1138 |
+
return group.dtype(r)
|
1139 |
+
|
1140 |
+
def substituted_word(self, from_i, to_j, by):
|
1141 |
+
"""
|
1142 |
+
Returns the associative word obtained by replacing the subword of
|
1143 |
+
`self` that begins at position `from_i` and ends at position `to_j - 1`
|
1144 |
+
by the associative word `by`. `from_i` and `to_j` must be positive
|
1145 |
+
integers, indexing is done with origin 0. In other words,
|
1146 |
+
`w.substituted_word(w, from_i, to_j, by)` is the product of the three
|
1147 |
+
words: `w.subword(0, from_i)`, `by`, and
|
1148 |
+
`w.subword(to_j len(w))`.
|
1149 |
+
|
1150 |
+
See Also
|
1151 |
+
========
|
1152 |
+
|
1153 |
+
eliminate_word
|
1154 |
+
|
1155 |
+
"""
|
1156 |
+
lw = len(self)
|
1157 |
+
if from_i >= to_j or from_i > lw or to_j > lw:
|
1158 |
+
raise ValueError("values should be within bounds")
|
1159 |
+
|
1160 |
+
# otherwise there are four possibilities
|
1161 |
+
|
1162 |
+
# first if from=1 and to=lw then
|
1163 |
+
if from_i == 0 and to_j == lw:
|
1164 |
+
return by
|
1165 |
+
elif from_i == 0: # second if from_i=1 (and to_j < lw) then
|
1166 |
+
return by*self.subword(to_j, lw)
|
1167 |
+
elif to_j == lw: # third if to_j=1 (and from_i > 1) then
|
1168 |
+
return self.subword(0, from_i)*by
|
1169 |
+
else: # finally
|
1170 |
+
return self.subword(0, from_i)*by*self.subword(to_j, lw)
|
1171 |
+
|
1172 |
+
def is_cyclically_reduced(self):
|
1173 |
+
r"""Returns whether the word is cyclically reduced or not.
|
1174 |
+
A word is cyclically reduced if by forming the cycle of the
|
1175 |
+
word, the word is not reduced, i.e a word w = `a_1 ... a_n`
|
1176 |
+
is called cyclically reduced if `a_1 \ne a_n^{-1}`.
|
1177 |
+
|
1178 |
+
Examples
|
1179 |
+
========
|
1180 |
+
|
1181 |
+
>>> from sympy.combinatorics import free_group
|
1182 |
+
>>> F, x, y = free_group("x, y")
|
1183 |
+
>>> (x**2*y**-1*x**-1).is_cyclically_reduced()
|
1184 |
+
False
|
1185 |
+
>>> (y*x**2*y**2).is_cyclically_reduced()
|
1186 |
+
True
|
1187 |
+
|
1188 |
+
"""
|
1189 |
+
if not self:
|
1190 |
+
return True
|
1191 |
+
return self[0] != self[-1]**-1
|
1192 |
+
|
1193 |
+
def identity_cyclic_reduction(self):
|
1194 |
+
"""Return a unique cyclically reduced version of the word.
|
1195 |
+
|
1196 |
+
Examples
|
1197 |
+
========
|
1198 |
+
|
1199 |
+
>>> from sympy.combinatorics import free_group
|
1200 |
+
>>> F, x, y = free_group("x, y")
|
1201 |
+
>>> (x**2*y**2*x**-1).identity_cyclic_reduction()
|
1202 |
+
x*y**2
|
1203 |
+
>>> (x**-3*y**-1*x**5).identity_cyclic_reduction()
|
1204 |
+
x**2*y**-1
|
1205 |
+
|
1206 |
+
References
|
1207 |
+
==========
|
1208 |
+
|
1209 |
+
.. [1] https://planetmath.org/cyclicallyreduced
|
1210 |
+
|
1211 |
+
"""
|
1212 |
+
word = self.copy()
|
1213 |
+
group = self.group
|
1214 |
+
while not word.is_cyclically_reduced():
|
1215 |
+
exp1 = word.exponent_syllable(0)
|
1216 |
+
exp2 = word.exponent_syllable(-1)
|
1217 |
+
r = exp1 + exp2
|
1218 |
+
if r == 0:
|
1219 |
+
rep = word.array_form[1: word.number_syllables() - 1]
|
1220 |
+
else:
|
1221 |
+
rep = ((word.generator_syllable(0), exp1 + exp2),) + \
|
1222 |
+
word.array_form[1: word.number_syllables() - 1]
|
1223 |
+
word = group.dtype(rep)
|
1224 |
+
return word
|
1225 |
+
|
1226 |
+
def cyclic_reduction(self, removed=False):
|
1227 |
+
"""Return a cyclically reduced version of the word. Unlike
|
1228 |
+
`identity_cyclic_reduction`, this will not cyclically permute
|
1229 |
+
the reduced word - just remove the "unreduced" bits on either
|
1230 |
+
side of it. Compare the examples with those of
|
1231 |
+
`identity_cyclic_reduction`.
|
1232 |
+
|
1233 |
+
When `removed` is `True`, return a tuple `(word, r)` where
|
1234 |
+
self `r` is such that before the reduction the word was either
|
1235 |
+
`r*word*r**-1`.
|
1236 |
+
|
1237 |
+
Examples
|
1238 |
+
========
|
1239 |
+
|
1240 |
+
>>> from sympy.combinatorics import free_group
|
1241 |
+
>>> F, x, y = free_group("x, y")
|
1242 |
+
>>> (x**2*y**2*x**-1).cyclic_reduction()
|
1243 |
+
x*y**2
|
1244 |
+
>>> (x**-3*y**-1*x**5).cyclic_reduction()
|
1245 |
+
y**-1*x**2
|
1246 |
+
>>> (x**-3*y**-1*x**5).cyclic_reduction(removed=True)
|
1247 |
+
(y**-1*x**2, x**-3)
|
1248 |
+
|
1249 |
+
"""
|
1250 |
+
word = self.copy()
|
1251 |
+
g = self.group.identity
|
1252 |
+
while not word.is_cyclically_reduced():
|
1253 |
+
exp1 = abs(word.exponent_syllable(0))
|
1254 |
+
exp2 = abs(word.exponent_syllable(-1))
|
1255 |
+
exp = min(exp1, exp2)
|
1256 |
+
start = word[0]**abs(exp)
|
1257 |
+
end = word[-1]**abs(exp)
|
1258 |
+
word = start**-1*word*end**-1
|
1259 |
+
g = g*start
|
1260 |
+
if removed:
|
1261 |
+
return word, g
|
1262 |
+
return word
|
1263 |
+
|
1264 |
+
def power_of(self, other):
|
1265 |
+
'''
|
1266 |
+
Check if `self == other**n` for some integer n.
|
1267 |
+
|
1268 |
+
Examples
|
1269 |
+
========
|
1270 |
+
|
1271 |
+
>>> from sympy.combinatorics import free_group
|
1272 |
+
>>> F, x, y = free_group("x, y")
|
1273 |
+
>>> ((x*y)**2).power_of(x*y)
|
1274 |
+
True
|
1275 |
+
>>> (x**-3*y**-2*x**3).power_of(x**-3*y*x**3)
|
1276 |
+
True
|
1277 |
+
|
1278 |
+
'''
|
1279 |
+
if self.is_identity:
|
1280 |
+
return True
|
1281 |
+
|
1282 |
+
l = len(other)
|
1283 |
+
if l == 1:
|
1284 |
+
# self has to be a power of one generator
|
1285 |
+
gens = self.contains_generators()
|
1286 |
+
s = other in gens or other**-1 in gens
|
1287 |
+
return len(gens) == 1 and s
|
1288 |
+
|
1289 |
+
# if self is not cyclically reduced and it is a power of other,
|
1290 |
+
# other isn't cyclically reduced and the parts removed during
|
1291 |
+
# their reduction must be equal
|
1292 |
+
reduced, r1 = self.cyclic_reduction(removed=True)
|
1293 |
+
if not r1.is_identity:
|
1294 |
+
other, r2 = other.cyclic_reduction(removed=True)
|
1295 |
+
if r1 == r2:
|
1296 |
+
return reduced.power_of(other)
|
1297 |
+
return False
|
1298 |
+
|
1299 |
+
if len(self) < l or len(self) % l:
|
1300 |
+
return False
|
1301 |
+
|
1302 |
+
prefix = self.subword(0, l)
|
1303 |
+
if prefix == other or prefix**-1 == other:
|
1304 |
+
rest = self.subword(l, len(self))
|
1305 |
+
return rest.power_of(other)
|
1306 |
+
return False
|
1307 |
+
|
1308 |
+
|
1309 |
+
def letter_form_to_array_form(array_form, group):
|
1310 |
+
"""
|
1311 |
+
This method converts a list given with possible repetitions of elements in
|
1312 |
+
it. It returns a new list such that repetitions of consecutive elements is
|
1313 |
+
removed and replace with a tuple element of size two such that the first
|
1314 |
+
index contains `value` and the second index contains the number of
|
1315 |
+
consecutive repetitions of `value`.
|
1316 |
+
|
1317 |
+
"""
|
1318 |
+
a = list(array_form[:])
|
1319 |
+
new_array = []
|
1320 |
+
n = 1
|
1321 |
+
symbols = group.symbols
|
1322 |
+
for i in range(len(a)):
|
1323 |
+
if i == len(a) - 1:
|
1324 |
+
if a[i] == a[i - 1]:
|
1325 |
+
if (-a[i]) in symbols:
|
1326 |
+
new_array.append((-a[i], -n))
|
1327 |
+
else:
|
1328 |
+
new_array.append((a[i], n))
|
1329 |
+
else:
|
1330 |
+
if (-a[i]) in symbols:
|
1331 |
+
new_array.append((-a[i], -1))
|
1332 |
+
else:
|
1333 |
+
new_array.append((a[i], 1))
|
1334 |
+
return new_array
|
1335 |
+
elif a[i] == a[i + 1]:
|
1336 |
+
n += 1
|
1337 |
+
else:
|
1338 |
+
if (-a[i]) in symbols:
|
1339 |
+
new_array.append((-a[i], -n))
|
1340 |
+
else:
|
1341 |
+
new_array.append((a[i], n))
|
1342 |
+
n = 1
|
1343 |
+
|
1344 |
+
|
1345 |
+
def zero_mul_simp(l, index):
|
1346 |
+
"""Used to combine two reduced words."""
|
1347 |
+
while index >=0 and index < len(l) - 1 and l[index][0] == l[index + 1][0]:
|
1348 |
+
exp = l[index][1] + l[index + 1][1]
|
1349 |
+
base = l[index][0]
|
1350 |
+
l[index] = (base, exp)
|
1351 |
+
del l[index + 1]
|
1352 |
+
if l[index][1] == 0:
|
1353 |
+
del l[index]
|
1354 |
+
index -= 1
|
venv/lib/python3.10/site-packages/sympy/combinatorics/galois.py
ADDED
@@ -0,0 +1,611 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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Galois theory.
|
3 |
+
|
4 |
+
Besides constructing instances of the :py:class:`~.PermutationGroup` class to
|
5 |
+
represent the transitive subgroups of $S_n$ for small $n$, this module provides
|
6 |
+
*names* for these groups.
|
7 |
+
|
8 |
+
In some applications, it may be preferable to know the name of a group,
|
9 |
+
rather than receive an instance of the :py:class:`~.PermutationGroup`
|
10 |
+
class, and then have to do extra work to determine which group it is, by
|
11 |
+
checking various properties.
|
12 |
+
|
13 |
+
Names are instances of ``Enum`` classes defined in this module. With a name in
|
14 |
+
hand, the name's ``get_perm_group`` method can then be used to retrieve a
|
15 |
+
:py:class:`~.PermutationGroup`.
|
16 |
+
|
17 |
+
The names used for groups in this module are taken from [1].
|
18 |
+
|
19 |
+
References
|
20 |
+
==========
|
21 |
+
|
22 |
+
.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory*.
|
23 |
+
|
24 |
+
"""
|
25 |
+
|
26 |
+
from collections import defaultdict
|
27 |
+
from enum import Enum
|
28 |
+
import itertools
|
29 |
+
|
30 |
+
from sympy.combinatorics.named_groups import (
|
31 |
+
SymmetricGroup, AlternatingGroup, CyclicGroup, DihedralGroup,
|
32 |
+
set_symmetric_group_properties, set_alternating_group_properties,
|
33 |
+
)
|
34 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
35 |
+
from sympy.combinatorics.permutations import Permutation
|
36 |
+
|
37 |
+
|
38 |
+
class S1TransitiveSubgroups(Enum):
|
39 |
+
"""
|
40 |
+
Names for the transitive subgroups of S1.
|
41 |
+
"""
|
42 |
+
S1 = "S1"
|
43 |
+
|
44 |
+
def get_perm_group(self):
|
45 |
+
return SymmetricGroup(1)
|
46 |
+
|
47 |
+
|
48 |
+
class S2TransitiveSubgroups(Enum):
|
49 |
+
"""
|
50 |
+
Names for the transitive subgroups of S2.
|
51 |
+
"""
|
52 |
+
S2 = "S2"
|
53 |
+
|
54 |
+
def get_perm_group(self):
|
55 |
+
return SymmetricGroup(2)
|
56 |
+
|
57 |
+
|
58 |
+
class S3TransitiveSubgroups(Enum):
|
59 |
+
"""
|
60 |
+
Names for the transitive subgroups of S3.
|
61 |
+
"""
|
62 |
+
A3 = "A3"
|
63 |
+
S3 = "S3"
|
64 |
+
|
65 |
+
def get_perm_group(self):
|
66 |
+
if self == S3TransitiveSubgroups.A3:
|
67 |
+
return AlternatingGroup(3)
|
68 |
+
elif self == S3TransitiveSubgroups.S3:
|
69 |
+
return SymmetricGroup(3)
|
70 |
+
|
71 |
+
|
72 |
+
class S4TransitiveSubgroups(Enum):
|
73 |
+
"""
|
74 |
+
Names for the transitive subgroups of S4.
|
75 |
+
"""
|
76 |
+
C4 = "C4"
|
77 |
+
V = "V"
|
78 |
+
D4 = "D4"
|
79 |
+
A4 = "A4"
|
80 |
+
S4 = "S4"
|
81 |
+
|
82 |
+
def get_perm_group(self):
|
83 |
+
if self == S4TransitiveSubgroups.C4:
|
84 |
+
return CyclicGroup(4)
|
85 |
+
elif self == S4TransitiveSubgroups.V:
|
86 |
+
return four_group()
|
87 |
+
elif self == S4TransitiveSubgroups.D4:
|
88 |
+
return DihedralGroup(4)
|
89 |
+
elif self == S4TransitiveSubgroups.A4:
|
90 |
+
return AlternatingGroup(4)
|
91 |
+
elif self == S4TransitiveSubgroups.S4:
|
92 |
+
return SymmetricGroup(4)
|
93 |
+
|
94 |
+
|
95 |
+
class S5TransitiveSubgroups(Enum):
|
96 |
+
"""
|
97 |
+
Names for the transitive subgroups of S5.
|
98 |
+
"""
|
99 |
+
C5 = "C5"
|
100 |
+
D5 = "D5"
|
101 |
+
M20 = "M20"
|
102 |
+
A5 = "A5"
|
103 |
+
S5 = "S5"
|
104 |
+
|
105 |
+
def get_perm_group(self):
|
106 |
+
if self == S5TransitiveSubgroups.C5:
|
107 |
+
return CyclicGroup(5)
|
108 |
+
elif self == S5TransitiveSubgroups.D5:
|
109 |
+
return DihedralGroup(5)
|
110 |
+
elif self == S5TransitiveSubgroups.M20:
|
111 |
+
return M20()
|
112 |
+
elif self == S5TransitiveSubgroups.A5:
|
113 |
+
return AlternatingGroup(5)
|
114 |
+
elif self == S5TransitiveSubgroups.S5:
|
115 |
+
return SymmetricGroup(5)
|
116 |
+
|
117 |
+
|
118 |
+
class S6TransitiveSubgroups(Enum):
|
119 |
+
"""
|
120 |
+
Names for the transitive subgroups of S6.
|
121 |
+
"""
|
122 |
+
C6 = "C6"
|
123 |
+
S3 = "S3"
|
124 |
+
D6 = "D6"
|
125 |
+
A4 = "A4"
|
126 |
+
G18 = "G18"
|
127 |
+
A4xC2 = "A4 x C2"
|
128 |
+
S4m = "S4-"
|
129 |
+
S4p = "S4+"
|
130 |
+
G36m = "G36-"
|
131 |
+
G36p = "G36+"
|
132 |
+
S4xC2 = "S4 x C2"
|
133 |
+
PSL2F5 = "PSL2(F5)"
|
134 |
+
G72 = "G72"
|
135 |
+
PGL2F5 = "PGL2(F5)"
|
136 |
+
A6 = "A6"
|
137 |
+
S6 = "S6"
|
138 |
+
|
139 |
+
def get_perm_group(self):
|
140 |
+
if self == S6TransitiveSubgroups.C6:
|
141 |
+
return CyclicGroup(6)
|
142 |
+
elif self == S6TransitiveSubgroups.S3:
|
143 |
+
return S3_in_S6()
|
144 |
+
elif self == S6TransitiveSubgroups.D6:
|
145 |
+
return DihedralGroup(6)
|
146 |
+
elif self == S6TransitiveSubgroups.A4:
|
147 |
+
return A4_in_S6()
|
148 |
+
elif self == S6TransitiveSubgroups.G18:
|
149 |
+
return G18()
|
150 |
+
elif self == S6TransitiveSubgroups.A4xC2:
|
151 |
+
return A4xC2()
|
152 |
+
elif self == S6TransitiveSubgroups.S4m:
|
153 |
+
return S4m()
|
154 |
+
elif self == S6TransitiveSubgroups.S4p:
|
155 |
+
return S4p()
|
156 |
+
elif self == S6TransitiveSubgroups.G36m:
|
157 |
+
return G36m()
|
158 |
+
elif self == S6TransitiveSubgroups.G36p:
|
159 |
+
return G36p()
|
160 |
+
elif self == S6TransitiveSubgroups.S4xC2:
|
161 |
+
return S4xC2()
|
162 |
+
elif self == S6TransitiveSubgroups.PSL2F5:
|
163 |
+
return PSL2F5()
|
164 |
+
elif self == S6TransitiveSubgroups.G72:
|
165 |
+
return G72()
|
166 |
+
elif self == S6TransitiveSubgroups.PGL2F5:
|
167 |
+
return PGL2F5()
|
168 |
+
elif self == S6TransitiveSubgroups.A6:
|
169 |
+
return AlternatingGroup(6)
|
170 |
+
elif self == S6TransitiveSubgroups.S6:
|
171 |
+
return SymmetricGroup(6)
|
172 |
+
|
173 |
+
|
174 |
+
def four_group():
|
175 |
+
"""
|
176 |
+
Return a representation of the Klein four-group as a transitive subgroup
|
177 |
+
of S4.
|
178 |
+
"""
|
179 |
+
return PermutationGroup(
|
180 |
+
Permutation(0, 1)(2, 3),
|
181 |
+
Permutation(0, 2)(1, 3)
|
182 |
+
)
|
183 |
+
|
184 |
+
|
185 |
+
def M20():
|
186 |
+
"""
|
187 |
+
Return a representation of the metacyclic group M20, a transitive subgroup
|
188 |
+
of S5 that is one of the possible Galois groups for polys of degree 5.
|
189 |
+
|
190 |
+
Notes
|
191 |
+
=====
|
192 |
+
|
193 |
+
See [1], Page 323.
|
194 |
+
|
195 |
+
"""
|
196 |
+
G = PermutationGroup(Permutation(0, 1, 2, 3, 4), Permutation(1, 2, 4, 3))
|
197 |
+
G._degree = 5
|
198 |
+
G._order = 20
|
199 |
+
G._is_transitive = True
|
200 |
+
G._is_sym = False
|
201 |
+
G._is_alt = False
|
202 |
+
G._is_cyclic = False
|
203 |
+
G._is_dihedral = False
|
204 |
+
return G
|
205 |
+
|
206 |
+
|
207 |
+
def S3_in_S6():
|
208 |
+
"""
|
209 |
+
Return a representation of S3 as a transitive subgroup of S6.
|
210 |
+
|
211 |
+
Notes
|
212 |
+
=====
|
213 |
+
|
214 |
+
The representation is found by viewing the group as the symmetries of a
|
215 |
+
triangular prism.
|
216 |
+
|
217 |
+
"""
|
218 |
+
G = PermutationGroup(Permutation(0, 1, 2)(3, 4, 5), Permutation(0, 3)(2, 4)(1, 5))
|
219 |
+
set_symmetric_group_properties(G, 3, 6)
|
220 |
+
return G
|
221 |
+
|
222 |
+
|
223 |
+
def A4_in_S6():
|
224 |
+
"""
|
225 |
+
Return a representation of A4 as a transitive subgroup of S6.
|
226 |
+
|
227 |
+
Notes
|
228 |
+
=====
|
229 |
+
|
230 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
231 |
+
|
232 |
+
"""
|
233 |
+
G = PermutationGroup(Permutation(0, 4, 5)(1, 3, 2), Permutation(0, 1, 2)(3, 5, 4))
|
234 |
+
set_alternating_group_properties(G, 4, 6)
|
235 |
+
return G
|
236 |
+
|
237 |
+
|
238 |
+
def S4m():
|
239 |
+
"""
|
240 |
+
Return a representation of the S4- transitive subgroup of S6.
|
241 |
+
|
242 |
+
Notes
|
243 |
+
=====
|
244 |
+
|
245 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
246 |
+
|
247 |
+
"""
|
248 |
+
G = PermutationGroup(Permutation(1, 4, 5, 3), Permutation(0, 4)(1, 5)(2, 3))
|
249 |
+
set_symmetric_group_properties(G, 4, 6)
|
250 |
+
return G
|
251 |
+
|
252 |
+
|
253 |
+
def S4p():
|
254 |
+
"""
|
255 |
+
Return a representation of the S4+ transitive subgroup of S6.
|
256 |
+
|
257 |
+
Notes
|
258 |
+
=====
|
259 |
+
|
260 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
261 |
+
|
262 |
+
"""
|
263 |
+
G = PermutationGroup(Permutation(0, 2, 4, 1)(3, 5), Permutation(0, 3)(4, 5))
|
264 |
+
set_symmetric_group_properties(G, 4, 6)
|
265 |
+
return G
|
266 |
+
|
267 |
+
|
268 |
+
def A4xC2():
|
269 |
+
"""
|
270 |
+
Return a representation of the (A4 x C2) transitive subgroup of S6.
|
271 |
+
|
272 |
+
Notes
|
273 |
+
=====
|
274 |
+
|
275 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
276 |
+
|
277 |
+
"""
|
278 |
+
return PermutationGroup(
|
279 |
+
Permutation(0, 4, 5)(1, 3, 2), Permutation(0, 1, 2)(3, 5, 4),
|
280 |
+
Permutation(5)(2, 4))
|
281 |
+
|
282 |
+
|
283 |
+
def S4xC2():
|
284 |
+
"""
|
285 |
+
Return a representation of the (S4 x C2) transitive subgroup of S6.
|
286 |
+
|
287 |
+
Notes
|
288 |
+
=====
|
289 |
+
|
290 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
291 |
+
|
292 |
+
"""
|
293 |
+
return PermutationGroup(
|
294 |
+
Permutation(1, 4, 5, 3), Permutation(0, 4)(1, 5)(2, 3),
|
295 |
+
Permutation(1, 4)(3, 5))
|
296 |
+
|
297 |
+
|
298 |
+
def G18():
|
299 |
+
"""
|
300 |
+
Return a representation of the group G18, a transitive subgroup of S6
|
301 |
+
isomorphic to the semidirect product of C3^2 with C2.
|
302 |
+
|
303 |
+
Notes
|
304 |
+
=====
|
305 |
+
|
306 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
307 |
+
|
308 |
+
"""
|
309 |
+
return PermutationGroup(
|
310 |
+
Permutation(5)(0, 1, 2), Permutation(3, 4, 5),
|
311 |
+
Permutation(0, 4)(1, 5)(2, 3))
|
312 |
+
|
313 |
+
|
314 |
+
def G36m():
|
315 |
+
"""
|
316 |
+
Return a representation of the group G36-, a transitive subgroup of S6
|
317 |
+
isomorphic to the semidirect product of C3^2 with C2^2.
|
318 |
+
|
319 |
+
Notes
|
320 |
+
=====
|
321 |
+
|
322 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
323 |
+
|
324 |
+
"""
|
325 |
+
return PermutationGroup(
|
326 |
+
Permutation(5)(0, 1, 2), Permutation(3, 4, 5),
|
327 |
+
Permutation(1, 2)(3, 5), Permutation(0, 4)(1, 5)(2, 3))
|
328 |
+
|
329 |
+
|
330 |
+
def G36p():
|
331 |
+
"""
|
332 |
+
Return a representation of the group G36+, a transitive subgroup of S6
|
333 |
+
isomorphic to the semidirect product of C3^2 with C4.
|
334 |
+
|
335 |
+
Notes
|
336 |
+
=====
|
337 |
+
|
338 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
339 |
+
|
340 |
+
"""
|
341 |
+
return PermutationGroup(
|
342 |
+
Permutation(5)(0, 1, 2), Permutation(3, 4, 5),
|
343 |
+
Permutation(0, 5, 2, 3)(1, 4))
|
344 |
+
|
345 |
+
|
346 |
+
def G72():
|
347 |
+
"""
|
348 |
+
Return a representation of the group G72, a transitive subgroup of S6
|
349 |
+
isomorphic to the semidirect product of C3^2 with D4.
|
350 |
+
|
351 |
+
Notes
|
352 |
+
=====
|
353 |
+
|
354 |
+
See [1], Page 325.
|
355 |
+
|
356 |
+
"""
|
357 |
+
return PermutationGroup(
|
358 |
+
Permutation(5)(0, 1, 2),
|
359 |
+
Permutation(0, 4, 1, 3)(2, 5), Permutation(0, 3)(1, 4)(2, 5))
|
360 |
+
|
361 |
+
|
362 |
+
def PSL2F5():
|
363 |
+
r"""
|
364 |
+
Return a representation of the group $PSL_2(\mathbb{F}_5)$, as a transitive
|
365 |
+
subgroup of S6, isomorphic to $A_5$.
|
366 |
+
|
367 |
+
Notes
|
368 |
+
=====
|
369 |
+
|
370 |
+
This was computed using :py:func:`~.find_transitive_subgroups_of_S6`.
|
371 |
+
|
372 |
+
"""
|
373 |
+
G = PermutationGroup(
|
374 |
+
Permutation(0, 4, 5)(1, 3, 2), Permutation(0, 4, 3, 1, 5))
|
375 |
+
set_alternating_group_properties(G, 5, 6)
|
376 |
+
return G
|
377 |
+
|
378 |
+
|
379 |
+
def PGL2F5():
|
380 |
+
r"""
|
381 |
+
Return a representation of the group $PGL_2(\mathbb{F}_5)$, as a transitive
|
382 |
+
subgroup of S6, isomorphic to $S_5$.
|
383 |
+
|
384 |
+
Notes
|
385 |
+
=====
|
386 |
+
|
387 |
+
See [1], Page 325.
|
388 |
+
|
389 |
+
"""
|
390 |
+
G = PermutationGroup(
|
391 |
+
Permutation(0, 1, 2, 3, 4), Permutation(0, 5)(1, 2)(3, 4))
|
392 |
+
set_symmetric_group_properties(G, 5, 6)
|
393 |
+
return G
|
394 |
+
|
395 |
+
|
396 |
+
def find_transitive_subgroups_of_S6(*targets, print_report=False):
|
397 |
+
r"""
|
398 |
+
Search for certain transitive subgroups of $S_6$.
|
399 |
+
|
400 |
+
The symmetric group $S_6$ has 16 different transitive subgroups, up to
|
401 |
+
conjugacy. Some are more easily constructed than others. For example, the
|
402 |
+
dihedral group $D_6$ is immediately found, but it is not at all obvious how
|
403 |
+
to realize $S_4$ or $S_5$ *transitively* within $S_6$.
|
404 |
+
|
405 |
+
In some cases there are well-known constructions that can be used. For
|
406 |
+
example, $S_5$ is isomorphic to $PGL_2(\mathbb{F}_5)$, which acts in a
|
407 |
+
natural way on the projective line $P^1(\mathbb{F}_5)$, a set of order 6.
|
408 |
+
|
409 |
+
In absence of such special constructions however, we can simply search for
|
410 |
+
generators. For example, transitive instances of $A_4$ and $S_4$ can be
|
411 |
+
found within $S_6$ in this way.
|
412 |
+
|
413 |
+
Once we are engaged in such searches, it may then be easier (if less
|
414 |
+
elegant) to find even those groups like $S_5$ that do have special
|
415 |
+
constructions, by mere search.
|
416 |
+
|
417 |
+
This function locates generators for transitive instances in $S_6$ of the
|
418 |
+
following subgroups:
|
419 |
+
|
420 |
+
* $A_4$
|
421 |
+
* $S_4^-$ ($S_4$ not contained within $A_6$)
|
422 |
+
* $S_4^+$ ($S_4$ contained within $A_6$)
|
423 |
+
* $A_4 \times C_2$
|
424 |
+
* $S_4 \times C_2$
|
425 |
+
* $G_{18} = C_3^2 \rtimes C_2$
|
426 |
+
* $G_{36}^- = C_3^2 \rtimes C_2^2$
|
427 |
+
* $G_{36}^+ = C_3^2 \rtimes C_4$
|
428 |
+
* $G_{72} = C_3^2 \rtimes D_4$
|
429 |
+
* $A_5$
|
430 |
+
* $S_5$
|
431 |
+
|
432 |
+
Note: Each of these groups also has a dedicated function in this module
|
433 |
+
that returns the group immediately, using generators that were found by
|
434 |
+
this search procedure.
|
435 |
+
|
436 |
+
The search procedure serves as a record of how these generators were
|
437 |
+
found. Also, due to randomness in the generation of the elements of
|
438 |
+
permutation groups, it can be called again, in order to (probably) get
|
439 |
+
different generators for the same groups.
|
440 |
+
|
441 |
+
Parameters
|
442 |
+
==========
|
443 |
+
|
444 |
+
targets : list of :py:class:`~.S6TransitiveSubgroups` values
|
445 |
+
The groups you want to find.
|
446 |
+
|
447 |
+
print_report : bool (default False)
|
448 |
+
If True, print to stdout the generators found for each group.
|
449 |
+
|
450 |
+
Returns
|
451 |
+
=======
|
452 |
+
|
453 |
+
dict
|
454 |
+
mapping each name in *targets* to the :py:class:`~.PermutationGroup`
|
455 |
+
that was found
|
456 |
+
|
457 |
+
References
|
458 |
+
==========
|
459 |
+
|
460 |
+
.. [2] https://en.wikipedia.org/wiki/Projective_linear_group#Exceptional_isomorphisms
|
461 |
+
.. [3] https://en.wikipedia.org/wiki/Automorphisms_of_the_symmetric_and_alternating_groups#PGL(2,5)
|
462 |
+
|
463 |
+
"""
|
464 |
+
def elts_by_order(G):
|
465 |
+
"""Sort the elements of a group by their order. """
|
466 |
+
elts = defaultdict(list)
|
467 |
+
for g in G.elements:
|
468 |
+
elts[g.order()].append(g)
|
469 |
+
return elts
|
470 |
+
|
471 |
+
def order_profile(G, name=None):
|
472 |
+
"""Determine how many elements a group has, of each order. """
|
473 |
+
elts = elts_by_order(G)
|
474 |
+
profile = {o:len(e) for o, e in elts.items()}
|
475 |
+
if name:
|
476 |
+
print(f'{name}: ' + ' '.join(f'{len(profile[r])}@{r}' for r in sorted(profile.keys())))
|
477 |
+
return profile
|
478 |
+
|
479 |
+
S6 = SymmetricGroup(6)
|
480 |
+
A6 = AlternatingGroup(6)
|
481 |
+
S6_by_order = elts_by_order(S6)
|
482 |
+
|
483 |
+
def search(existing_gens, needed_gen_orders, order, alt=None, profile=None, anti_profile=None):
|
484 |
+
"""
|
485 |
+
Find a transitive subgroup of S6.
|
486 |
+
|
487 |
+
Parameters
|
488 |
+
==========
|
489 |
+
|
490 |
+
existing_gens : list of Permutation
|
491 |
+
Optionally empty list of generators that must be in the group.
|
492 |
+
|
493 |
+
needed_gen_orders : list of positive int
|
494 |
+
Nonempty list of the orders of the additional generators that are
|
495 |
+
to be found.
|
496 |
+
|
497 |
+
order: int
|
498 |
+
The order of the group being sought.
|
499 |
+
|
500 |
+
alt: bool, None
|
501 |
+
If True, require the group to be contained in A6.
|
502 |
+
If False, require the group not to be contained in A6.
|
503 |
+
|
504 |
+
profile : dict
|
505 |
+
If given, the group's order profile must equal this.
|
506 |
+
|
507 |
+
anti_profile : dict
|
508 |
+
If given, the group's order profile must *not* equal this.
|
509 |
+
|
510 |
+
"""
|
511 |
+
for gens in itertools.product(*[S6_by_order[n] for n in needed_gen_orders]):
|
512 |
+
if len(set(gens)) < len(gens):
|
513 |
+
continue
|
514 |
+
G = PermutationGroup(existing_gens + list(gens))
|
515 |
+
if G.order() == order and G.is_transitive():
|
516 |
+
if alt is not None and G.is_subgroup(A6) != alt:
|
517 |
+
continue
|
518 |
+
if profile and order_profile(G) != profile:
|
519 |
+
continue
|
520 |
+
if anti_profile and order_profile(G) == anti_profile:
|
521 |
+
continue
|
522 |
+
return G
|
523 |
+
|
524 |
+
def match_known_group(G, alt=None):
|
525 |
+
needed = [g.order() for g in G.generators]
|
526 |
+
return search([], needed, G.order(), alt=alt, profile=order_profile(G))
|
527 |
+
|
528 |
+
found = {}
|
529 |
+
|
530 |
+
def finish_up(name, G):
|
531 |
+
found[name] = G
|
532 |
+
if print_report:
|
533 |
+
print("=" * 40)
|
534 |
+
print(f"{name}:")
|
535 |
+
print(G.generators)
|
536 |
+
|
537 |
+
if S6TransitiveSubgroups.A4 in targets or S6TransitiveSubgroups.A4xC2 in targets:
|
538 |
+
A4_in_S6 = match_known_group(AlternatingGroup(4))
|
539 |
+
finish_up(S6TransitiveSubgroups.A4, A4_in_S6)
|
540 |
+
|
541 |
+
if S6TransitiveSubgroups.S4m in targets or S6TransitiveSubgroups.S4xC2 in targets:
|
542 |
+
S4m_in_S6 = match_known_group(SymmetricGroup(4), alt=False)
|
543 |
+
finish_up(S6TransitiveSubgroups.S4m, S4m_in_S6)
|
544 |
+
|
545 |
+
if S6TransitiveSubgroups.S4p in targets:
|
546 |
+
S4p_in_S6 = match_known_group(SymmetricGroup(4), alt=True)
|
547 |
+
finish_up(S6TransitiveSubgroups.S4p, S4p_in_S6)
|
548 |
+
|
549 |
+
if S6TransitiveSubgroups.A4xC2 in targets:
|
550 |
+
A4xC2_in_S6 = search(A4_in_S6.generators, [2], 24, anti_profile=order_profile(SymmetricGroup(4)))
|
551 |
+
finish_up(S6TransitiveSubgroups.A4xC2, A4xC2_in_S6)
|
552 |
+
|
553 |
+
if S6TransitiveSubgroups.S4xC2 in targets:
|
554 |
+
S4xC2_in_S6 = search(S4m_in_S6.generators, [2], 48)
|
555 |
+
finish_up(S6TransitiveSubgroups.S4xC2, S4xC2_in_S6)
|
556 |
+
|
557 |
+
# For the normal factor N = C3^2 in any of the G_n subgroups, we take one
|
558 |
+
# obvious instance of C3^2 in S6:
|
559 |
+
N_gens = [Permutation(5)(0, 1, 2), Permutation(5)(3, 4, 5)]
|
560 |
+
|
561 |
+
if S6TransitiveSubgroups.G18 in targets:
|
562 |
+
G18_in_S6 = search(N_gens, [2], 18)
|
563 |
+
finish_up(S6TransitiveSubgroups.G18, G18_in_S6)
|
564 |
+
|
565 |
+
if S6TransitiveSubgroups.G36m in targets:
|
566 |
+
G36m_in_S6 = search(N_gens, [2, 2], 36, alt=False)
|
567 |
+
finish_up(S6TransitiveSubgroups.G36m, G36m_in_S6)
|
568 |
+
|
569 |
+
if S6TransitiveSubgroups.G36p in targets:
|
570 |
+
G36p_in_S6 = search(N_gens, [4], 36, alt=True)
|
571 |
+
finish_up(S6TransitiveSubgroups.G36p, G36p_in_S6)
|
572 |
+
|
573 |
+
if S6TransitiveSubgroups.G72 in targets:
|
574 |
+
G72_in_S6 = search(N_gens, [4, 2], 72)
|
575 |
+
finish_up(S6TransitiveSubgroups.G72, G72_in_S6)
|
576 |
+
|
577 |
+
# The PSL2(F5) and PGL2(F5) subgroups are isomorphic to A5 and S5, resp.
|
578 |
+
|
579 |
+
if S6TransitiveSubgroups.PSL2F5 in targets:
|
580 |
+
PSL2F5_in_S6 = match_known_group(AlternatingGroup(5))
|
581 |
+
finish_up(S6TransitiveSubgroups.PSL2F5, PSL2F5_in_S6)
|
582 |
+
|
583 |
+
if S6TransitiveSubgroups.PGL2F5 in targets:
|
584 |
+
PGL2F5_in_S6 = match_known_group(SymmetricGroup(5))
|
585 |
+
finish_up(S6TransitiveSubgroups.PGL2F5, PGL2F5_in_S6)
|
586 |
+
|
587 |
+
# There is little need to "search" for any of the groups C6, S3, D6, A6,
|
588 |
+
# or S6, since they all have obvious realizations within S6. However, we
|
589 |
+
# support them here just in case a random representation is desired.
|
590 |
+
|
591 |
+
if S6TransitiveSubgroups.C6 in targets:
|
592 |
+
C6 = match_known_group(CyclicGroup(6))
|
593 |
+
finish_up(S6TransitiveSubgroups.C6, C6)
|
594 |
+
|
595 |
+
if S6TransitiveSubgroups.S3 in targets:
|
596 |
+
S3 = match_known_group(SymmetricGroup(3))
|
597 |
+
finish_up(S6TransitiveSubgroups.S3, S3)
|
598 |
+
|
599 |
+
if S6TransitiveSubgroups.D6 in targets:
|
600 |
+
D6 = match_known_group(DihedralGroup(6))
|
601 |
+
finish_up(S6TransitiveSubgroups.D6, D6)
|
602 |
+
|
603 |
+
if S6TransitiveSubgroups.A6 in targets:
|
604 |
+
A6 = match_known_group(A6)
|
605 |
+
finish_up(S6TransitiveSubgroups.A6, A6)
|
606 |
+
|
607 |
+
if S6TransitiveSubgroups.S6 in targets:
|
608 |
+
S6 = match_known_group(S6)
|
609 |
+
finish_up(S6TransitiveSubgroups.S6, S6)
|
610 |
+
|
611 |
+
return found
|
venv/lib/python3.10/site-packages/sympy/combinatorics/generators.py
ADDED
@@ -0,0 +1,302 @@
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics.permutations import Permutation
|
2 |
+
from sympy.core.symbol import symbols
|
3 |
+
from sympy.matrices import Matrix
|
4 |
+
from sympy.utilities.iterables import variations, rotate_left
|
5 |
+
|
6 |
+
|
7 |
+
def symmetric(n):
|
8 |
+
"""
|
9 |
+
Generates the symmetric group of order n, Sn.
|
10 |
+
|
11 |
+
Examples
|
12 |
+
========
|
13 |
+
|
14 |
+
>>> from sympy.combinatorics.generators import symmetric
|
15 |
+
>>> list(symmetric(3))
|
16 |
+
[(2), (1 2), (2)(0 1), (0 1 2), (0 2 1), (0 2)]
|
17 |
+
"""
|
18 |
+
for perm in variations(range(n), n):
|
19 |
+
yield Permutation(perm)
|
20 |
+
|
21 |
+
|
22 |
+
def cyclic(n):
|
23 |
+
"""
|
24 |
+
Generates the cyclic group of order n, Cn.
|
25 |
+
|
26 |
+
Examples
|
27 |
+
========
|
28 |
+
|
29 |
+
>>> from sympy.combinatorics.generators import cyclic
|
30 |
+
>>> list(cyclic(5))
|
31 |
+
[(4), (0 1 2 3 4), (0 2 4 1 3),
|
32 |
+
(0 3 1 4 2), (0 4 3 2 1)]
|
33 |
+
|
34 |
+
See Also
|
35 |
+
========
|
36 |
+
|
37 |
+
dihedral
|
38 |
+
"""
|
39 |
+
gen = list(range(n))
|
40 |
+
for i in range(n):
|
41 |
+
yield Permutation(gen)
|
42 |
+
gen = rotate_left(gen, 1)
|
43 |
+
|
44 |
+
|
45 |
+
def alternating(n):
|
46 |
+
"""
|
47 |
+
Generates the alternating group of order n, An.
|
48 |
+
|
49 |
+
Examples
|
50 |
+
========
|
51 |
+
|
52 |
+
>>> from sympy.combinatorics.generators import alternating
|
53 |
+
>>> list(alternating(3))
|
54 |
+
[(2), (0 1 2), (0 2 1)]
|
55 |
+
"""
|
56 |
+
for perm in variations(range(n), n):
|
57 |
+
p = Permutation(perm)
|
58 |
+
if p.is_even:
|
59 |
+
yield p
|
60 |
+
|
61 |
+
|
62 |
+
def dihedral(n):
|
63 |
+
"""
|
64 |
+
Generates the dihedral group of order 2n, Dn.
|
65 |
+
|
66 |
+
The result is given as a subgroup of Sn, except for the special cases n=1
|
67 |
+
(the group S2) and n=2 (the Klein 4-group) where that's not possible
|
68 |
+
and embeddings in S2 and S4 respectively are given.
|
69 |
+
|
70 |
+
Examples
|
71 |
+
========
|
72 |
+
|
73 |
+
>>> from sympy.combinatorics.generators import dihedral
|
74 |
+
>>> list(dihedral(3))
|
75 |
+
[(2), (0 2), (0 1 2), (1 2), (0 2 1), (2)(0 1)]
|
76 |
+
|
77 |
+
See Also
|
78 |
+
========
|
79 |
+
|
80 |
+
cyclic
|
81 |
+
"""
|
82 |
+
if n == 1:
|
83 |
+
yield Permutation([0, 1])
|
84 |
+
yield Permutation([1, 0])
|
85 |
+
elif n == 2:
|
86 |
+
yield Permutation([0, 1, 2, 3])
|
87 |
+
yield Permutation([1, 0, 3, 2])
|
88 |
+
yield Permutation([2, 3, 0, 1])
|
89 |
+
yield Permutation([3, 2, 1, 0])
|
90 |
+
else:
|
91 |
+
gen = list(range(n))
|
92 |
+
for i in range(n):
|
93 |
+
yield Permutation(gen)
|
94 |
+
yield Permutation(gen[::-1])
|
95 |
+
gen = rotate_left(gen, 1)
|
96 |
+
|
97 |
+
|
98 |
+
def rubik_cube_generators():
|
99 |
+
"""Return the permutations of the 3x3 Rubik's cube, see
|
100 |
+
https://www.gap-system.org/Doc/Examples/rubik.html
|
101 |
+
"""
|
102 |
+
a = [
|
103 |
+
[(1, 3, 8, 6), (2, 5, 7, 4), (9, 33, 25, 17), (10, 34, 26, 18),
|
104 |
+
(11, 35, 27, 19)],
|
105 |
+
[(9, 11, 16, 14), (10, 13, 15, 12), (1, 17, 41, 40), (4, 20, 44, 37),
|
106 |
+
(6, 22, 46, 35)],
|
107 |
+
[(17, 19, 24, 22), (18, 21, 23, 20), (6, 25, 43, 16), (7, 28, 42, 13),
|
108 |
+
(8, 30, 41, 11)],
|
109 |
+
[(25, 27, 32, 30), (26, 29, 31, 28), (3, 38, 43, 19), (5, 36, 45, 21),
|
110 |
+
(8, 33, 48, 24)],
|
111 |
+
[(33, 35, 40, 38), (34, 37, 39, 36), (3, 9, 46, 32), (2, 12, 47, 29),
|
112 |
+
(1, 14, 48, 27)],
|
113 |
+
[(41, 43, 48, 46), (42, 45, 47, 44), (14, 22, 30, 38),
|
114 |
+
(15, 23, 31, 39), (16, 24, 32, 40)]
|
115 |
+
]
|
116 |
+
return [Permutation([[i - 1 for i in xi] for xi in x], size=48) for x in a]
|
117 |
+
|
118 |
+
|
119 |
+
def rubik(n):
|
120 |
+
"""Return permutations for an nxn Rubik's cube.
|
121 |
+
|
122 |
+
Permutations returned are for rotation of each of the slice
|
123 |
+
from the face up to the last face for each of the 3 sides (in this order):
|
124 |
+
front, right and bottom. Hence, the first n - 1 permutations are for the
|
125 |
+
slices from the front.
|
126 |
+
"""
|
127 |
+
|
128 |
+
if n < 2:
|
129 |
+
raise ValueError('dimension of cube must be > 1')
|
130 |
+
|
131 |
+
# 1-based reference to rows and columns in Matrix
|
132 |
+
def getr(f, i):
|
133 |
+
return faces[f].col(n - i)
|
134 |
+
|
135 |
+
def getl(f, i):
|
136 |
+
return faces[f].col(i - 1)
|
137 |
+
|
138 |
+
def getu(f, i):
|
139 |
+
return faces[f].row(i - 1)
|
140 |
+
|
141 |
+
def getd(f, i):
|
142 |
+
return faces[f].row(n - i)
|
143 |
+
|
144 |
+
def setr(f, i, s):
|
145 |
+
faces[f][:, n - i] = Matrix(n, 1, s)
|
146 |
+
|
147 |
+
def setl(f, i, s):
|
148 |
+
faces[f][:, i - 1] = Matrix(n, 1, s)
|
149 |
+
|
150 |
+
def setu(f, i, s):
|
151 |
+
faces[f][i - 1, :] = Matrix(1, n, s)
|
152 |
+
|
153 |
+
def setd(f, i, s):
|
154 |
+
faces[f][n - i, :] = Matrix(1, n, s)
|
155 |
+
|
156 |
+
# motion of a single face
|
157 |
+
def cw(F, r=1):
|
158 |
+
for _ in range(r):
|
159 |
+
face = faces[F]
|
160 |
+
rv = []
|
161 |
+
for c in range(n):
|
162 |
+
for r in range(n - 1, -1, -1):
|
163 |
+
rv.append(face[r, c])
|
164 |
+
faces[F] = Matrix(n, n, rv)
|
165 |
+
|
166 |
+
def ccw(F):
|
167 |
+
cw(F, 3)
|
168 |
+
|
169 |
+
# motion of plane i from the F side;
|
170 |
+
# fcw(0) moves the F face, fcw(1) moves the plane
|
171 |
+
# just behind the front face, etc...
|
172 |
+
def fcw(i, r=1):
|
173 |
+
for _ in range(r):
|
174 |
+
if i == 0:
|
175 |
+
cw(F)
|
176 |
+
i += 1
|
177 |
+
temp = getr(L, i)
|
178 |
+
setr(L, i, list(getu(D, i)))
|
179 |
+
setu(D, i, list(reversed(getl(R, i))))
|
180 |
+
setl(R, i, list(getd(U, i)))
|
181 |
+
setd(U, i, list(reversed(temp)))
|
182 |
+
i -= 1
|
183 |
+
|
184 |
+
def fccw(i):
|
185 |
+
fcw(i, 3)
|
186 |
+
|
187 |
+
# motion of the entire cube from the F side
|
188 |
+
def FCW(r=1):
|
189 |
+
for _ in range(r):
|
190 |
+
cw(F)
|
191 |
+
ccw(B)
|
192 |
+
cw(U)
|
193 |
+
t = faces[U]
|
194 |
+
cw(L)
|
195 |
+
faces[U] = faces[L]
|
196 |
+
cw(D)
|
197 |
+
faces[L] = faces[D]
|
198 |
+
cw(R)
|
199 |
+
faces[D] = faces[R]
|
200 |
+
faces[R] = t
|
201 |
+
|
202 |
+
def FCCW():
|
203 |
+
FCW(3)
|
204 |
+
|
205 |
+
# motion of the entire cube from the U side
|
206 |
+
def UCW(r=1):
|
207 |
+
for _ in range(r):
|
208 |
+
cw(U)
|
209 |
+
ccw(D)
|
210 |
+
t = faces[F]
|
211 |
+
faces[F] = faces[R]
|
212 |
+
faces[R] = faces[B]
|
213 |
+
faces[B] = faces[L]
|
214 |
+
faces[L] = t
|
215 |
+
|
216 |
+
def UCCW():
|
217 |
+
UCW(3)
|
218 |
+
|
219 |
+
# defining the permutations for the cube
|
220 |
+
|
221 |
+
U, F, R, B, L, D = names = symbols('U, F, R, B, L, D')
|
222 |
+
|
223 |
+
# the faces are represented by nxn matrices
|
224 |
+
faces = {}
|
225 |
+
count = 0
|
226 |
+
for fi in range(6):
|
227 |
+
f = []
|
228 |
+
for a in range(n**2):
|
229 |
+
f.append(count)
|
230 |
+
count += 1
|
231 |
+
faces[names[fi]] = Matrix(n, n, f)
|
232 |
+
|
233 |
+
# this will either return the value of the current permutation
|
234 |
+
# (show != 1) or else append the permutation to the group, g
|
235 |
+
def perm(show=0):
|
236 |
+
# add perm to the list of perms
|
237 |
+
p = []
|
238 |
+
for f in names:
|
239 |
+
p.extend(faces[f])
|
240 |
+
if show:
|
241 |
+
return p
|
242 |
+
g.append(Permutation(p))
|
243 |
+
|
244 |
+
g = [] # container for the group's permutations
|
245 |
+
I = list(range(6*n**2)) # the identity permutation used for checking
|
246 |
+
|
247 |
+
# define permutations corresponding to cw rotations of the planes
|
248 |
+
# up TO the last plane from that direction; by not including the
|
249 |
+
# last plane, the orientation of the cube is maintained.
|
250 |
+
|
251 |
+
# F slices
|
252 |
+
for i in range(n - 1):
|
253 |
+
fcw(i)
|
254 |
+
perm()
|
255 |
+
fccw(i) # restore
|
256 |
+
assert perm(1) == I
|
257 |
+
|
258 |
+
# R slices
|
259 |
+
# bring R to front
|
260 |
+
UCW()
|
261 |
+
for i in range(n - 1):
|
262 |
+
fcw(i)
|
263 |
+
# put it back in place
|
264 |
+
UCCW()
|
265 |
+
# record
|
266 |
+
perm()
|
267 |
+
# restore
|
268 |
+
# bring face to front
|
269 |
+
UCW()
|
270 |
+
fccw(i)
|
271 |
+
# restore
|
272 |
+
UCCW()
|
273 |
+
assert perm(1) == I
|
274 |
+
|
275 |
+
# D slices
|
276 |
+
# bring up bottom
|
277 |
+
FCW()
|
278 |
+
UCCW()
|
279 |
+
FCCW()
|
280 |
+
for i in range(n - 1):
|
281 |
+
# turn strip
|
282 |
+
fcw(i)
|
283 |
+
# put bottom back on the bottom
|
284 |
+
FCW()
|
285 |
+
UCW()
|
286 |
+
FCCW()
|
287 |
+
# record
|
288 |
+
perm()
|
289 |
+
# restore
|
290 |
+
# bring up bottom
|
291 |
+
FCW()
|
292 |
+
UCCW()
|
293 |
+
FCCW()
|
294 |
+
# turn strip
|
295 |
+
fccw(i)
|
296 |
+
# put bottom back on the bottom
|
297 |
+
FCW()
|
298 |
+
UCW()
|
299 |
+
FCCW()
|
300 |
+
assert perm(1) == I
|
301 |
+
|
302 |
+
return g
|
venv/lib/python3.10/site-packages/sympy/combinatorics/graycode.py
ADDED
@@ -0,0 +1,430 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core import Basic, Integer
|
2 |
+
|
3 |
+
import random
|
4 |
+
|
5 |
+
|
6 |
+
class GrayCode(Basic):
|
7 |
+
"""
|
8 |
+
A Gray code is essentially a Hamiltonian walk on
|
9 |
+
a n-dimensional cube with edge length of one.
|
10 |
+
The vertices of the cube are represented by vectors
|
11 |
+
whose values are binary. The Hamilton walk visits
|
12 |
+
each vertex exactly once. The Gray code for a 3d
|
13 |
+
cube is ['000','100','110','010','011','111','101',
|
14 |
+
'001'].
|
15 |
+
|
16 |
+
A Gray code solves the problem of sequentially
|
17 |
+
generating all possible subsets of n objects in such
|
18 |
+
a way that each subset is obtained from the previous
|
19 |
+
one by either deleting or adding a single object.
|
20 |
+
In the above example, 1 indicates that the object is
|
21 |
+
present, and 0 indicates that its absent.
|
22 |
+
|
23 |
+
Gray codes have applications in statistics as well when
|
24 |
+
we want to compute various statistics related to subsets
|
25 |
+
in an efficient manner.
|
26 |
+
|
27 |
+
Examples
|
28 |
+
========
|
29 |
+
|
30 |
+
>>> from sympy.combinatorics import GrayCode
|
31 |
+
>>> a = GrayCode(3)
|
32 |
+
>>> list(a.generate_gray())
|
33 |
+
['000', '001', '011', '010', '110', '111', '101', '100']
|
34 |
+
>>> a = GrayCode(4)
|
35 |
+
>>> list(a.generate_gray())
|
36 |
+
['0000', '0001', '0011', '0010', '0110', '0111', '0101', '0100', \
|
37 |
+
'1100', '1101', '1111', '1110', '1010', '1011', '1001', '1000']
|
38 |
+
|
39 |
+
References
|
40 |
+
==========
|
41 |
+
|
42 |
+
.. [1] Nijenhuis,A. and Wilf,H.S.(1978).
|
43 |
+
Combinatorial Algorithms. Academic Press.
|
44 |
+
.. [2] Knuth, D. (2011). The Art of Computer Programming, Vol 4
|
45 |
+
Addison Wesley
|
46 |
+
|
47 |
+
|
48 |
+
"""
|
49 |
+
|
50 |
+
_skip = False
|
51 |
+
_current = 0
|
52 |
+
_rank = None
|
53 |
+
|
54 |
+
def __new__(cls, n, *args, **kw_args):
|
55 |
+
"""
|
56 |
+
Default constructor.
|
57 |
+
|
58 |
+
It takes a single argument ``n`` which gives the dimension of the Gray
|
59 |
+
code. The starting Gray code string (``start``) or the starting ``rank``
|
60 |
+
may also be given; the default is to start at rank = 0 ('0...0').
|
61 |
+
|
62 |
+
Examples
|
63 |
+
========
|
64 |
+
|
65 |
+
>>> from sympy.combinatorics import GrayCode
|
66 |
+
>>> a = GrayCode(3)
|
67 |
+
>>> a
|
68 |
+
GrayCode(3)
|
69 |
+
>>> a.n
|
70 |
+
3
|
71 |
+
|
72 |
+
>>> a = GrayCode(3, start='100')
|
73 |
+
>>> a.current
|
74 |
+
'100'
|
75 |
+
|
76 |
+
>>> a = GrayCode(4, rank=4)
|
77 |
+
>>> a.current
|
78 |
+
'0110'
|
79 |
+
>>> a.rank
|
80 |
+
4
|
81 |
+
|
82 |
+
"""
|
83 |
+
if n < 1 or int(n) != n:
|
84 |
+
raise ValueError(
|
85 |
+
'Gray code dimension must be a positive integer, not %i' % n)
|
86 |
+
n = Integer(n)
|
87 |
+
args = (n,) + args
|
88 |
+
obj = Basic.__new__(cls, *args)
|
89 |
+
if 'start' in kw_args:
|
90 |
+
obj._current = kw_args["start"]
|
91 |
+
if len(obj._current) > n:
|
92 |
+
raise ValueError('Gray code start has length %i but '
|
93 |
+
'should not be greater than %i' % (len(obj._current), n))
|
94 |
+
elif 'rank' in kw_args:
|
95 |
+
if int(kw_args["rank"]) != kw_args["rank"]:
|
96 |
+
raise ValueError('Gray code rank must be a positive integer, '
|
97 |
+
'not %i' % kw_args["rank"])
|
98 |
+
obj._rank = int(kw_args["rank"]) % obj.selections
|
99 |
+
obj._current = obj.unrank(n, obj._rank)
|
100 |
+
return obj
|
101 |
+
|
102 |
+
def next(self, delta=1):
|
103 |
+
"""
|
104 |
+
Returns the Gray code a distance ``delta`` (default = 1) from the
|
105 |
+
current value in canonical order.
|
106 |
+
|
107 |
+
|
108 |
+
Examples
|
109 |
+
========
|
110 |
+
|
111 |
+
>>> from sympy.combinatorics import GrayCode
|
112 |
+
>>> a = GrayCode(3, start='110')
|
113 |
+
>>> a.next().current
|
114 |
+
'111'
|
115 |
+
>>> a.next(-1).current
|
116 |
+
'010'
|
117 |
+
"""
|
118 |
+
return GrayCode(self.n, rank=(self.rank + delta) % self.selections)
|
119 |
+
|
120 |
+
@property
|
121 |
+
def selections(self):
|
122 |
+
"""
|
123 |
+
Returns the number of bit vectors in the Gray code.
|
124 |
+
|
125 |
+
Examples
|
126 |
+
========
|
127 |
+
|
128 |
+
>>> from sympy.combinatorics import GrayCode
|
129 |
+
>>> a = GrayCode(3)
|
130 |
+
>>> a.selections
|
131 |
+
8
|
132 |
+
"""
|
133 |
+
return 2**self.n
|
134 |
+
|
135 |
+
@property
|
136 |
+
def n(self):
|
137 |
+
"""
|
138 |
+
Returns the dimension of the Gray code.
|
139 |
+
|
140 |
+
Examples
|
141 |
+
========
|
142 |
+
|
143 |
+
>>> from sympy.combinatorics import GrayCode
|
144 |
+
>>> a = GrayCode(5)
|
145 |
+
>>> a.n
|
146 |
+
5
|
147 |
+
"""
|
148 |
+
return self.args[0]
|
149 |
+
|
150 |
+
def generate_gray(self, **hints):
|
151 |
+
"""
|
152 |
+
Generates the sequence of bit vectors of a Gray Code.
|
153 |
+
|
154 |
+
Examples
|
155 |
+
========
|
156 |
+
|
157 |
+
>>> from sympy.combinatorics import GrayCode
|
158 |
+
>>> a = GrayCode(3)
|
159 |
+
>>> list(a.generate_gray())
|
160 |
+
['000', '001', '011', '010', '110', '111', '101', '100']
|
161 |
+
>>> list(a.generate_gray(start='011'))
|
162 |
+
['011', '010', '110', '111', '101', '100']
|
163 |
+
>>> list(a.generate_gray(rank=4))
|
164 |
+
['110', '111', '101', '100']
|
165 |
+
|
166 |
+
See Also
|
167 |
+
========
|
168 |
+
|
169 |
+
skip
|
170 |
+
|
171 |
+
References
|
172 |
+
==========
|
173 |
+
|
174 |
+
.. [1] Knuth, D. (2011). The Art of Computer Programming,
|
175 |
+
Vol 4, Addison Wesley
|
176 |
+
|
177 |
+
"""
|
178 |
+
bits = self.n
|
179 |
+
start = None
|
180 |
+
if "start" in hints:
|
181 |
+
start = hints["start"]
|
182 |
+
elif "rank" in hints:
|
183 |
+
start = GrayCode.unrank(self.n, hints["rank"])
|
184 |
+
if start is not None:
|
185 |
+
self._current = start
|
186 |
+
current = self.current
|
187 |
+
graycode_bin = gray_to_bin(current)
|
188 |
+
if len(graycode_bin) > self.n:
|
189 |
+
raise ValueError('Gray code start has length %i but should '
|
190 |
+
'not be greater than %i' % (len(graycode_bin), bits))
|
191 |
+
self._current = int(current, 2)
|
192 |
+
graycode_int = int(''.join(graycode_bin), 2)
|
193 |
+
for i in range(graycode_int, 1 << bits):
|
194 |
+
if self._skip:
|
195 |
+
self._skip = False
|
196 |
+
else:
|
197 |
+
yield self.current
|
198 |
+
bbtc = (i ^ (i + 1))
|
199 |
+
gbtc = (bbtc ^ (bbtc >> 1))
|
200 |
+
self._current = (self._current ^ gbtc)
|
201 |
+
self._current = 0
|
202 |
+
|
203 |
+
def skip(self):
|
204 |
+
"""
|
205 |
+
Skips the bit generation.
|
206 |
+
|
207 |
+
Examples
|
208 |
+
========
|
209 |
+
|
210 |
+
>>> from sympy.combinatorics import GrayCode
|
211 |
+
>>> a = GrayCode(3)
|
212 |
+
>>> for i in a.generate_gray():
|
213 |
+
... if i == '010':
|
214 |
+
... a.skip()
|
215 |
+
... print(i)
|
216 |
+
...
|
217 |
+
000
|
218 |
+
001
|
219 |
+
011
|
220 |
+
010
|
221 |
+
111
|
222 |
+
101
|
223 |
+
100
|
224 |
+
|
225 |
+
See Also
|
226 |
+
========
|
227 |
+
|
228 |
+
generate_gray
|
229 |
+
"""
|
230 |
+
self._skip = True
|
231 |
+
|
232 |
+
@property
|
233 |
+
def rank(self):
|
234 |
+
"""
|
235 |
+
Ranks the Gray code.
|
236 |
+
|
237 |
+
A ranking algorithm determines the position (or rank)
|
238 |
+
of a combinatorial object among all the objects w.r.t.
|
239 |
+
a given order. For example, the 4 bit binary reflected
|
240 |
+
Gray code (BRGC) '0101' has a rank of 6 as it appears in
|
241 |
+
the 6th position in the canonical ordering of the family
|
242 |
+
of 4 bit Gray codes.
|
243 |
+
|
244 |
+
Examples
|
245 |
+
========
|
246 |
+
|
247 |
+
>>> from sympy.combinatorics import GrayCode
|
248 |
+
>>> a = GrayCode(3)
|
249 |
+
>>> list(a.generate_gray())
|
250 |
+
['000', '001', '011', '010', '110', '111', '101', '100']
|
251 |
+
>>> GrayCode(3, start='100').rank
|
252 |
+
7
|
253 |
+
>>> GrayCode(3, rank=7).current
|
254 |
+
'100'
|
255 |
+
|
256 |
+
See Also
|
257 |
+
========
|
258 |
+
|
259 |
+
unrank
|
260 |
+
|
261 |
+
References
|
262 |
+
==========
|
263 |
+
|
264 |
+
.. [1] https://web.archive.org/web/20200224064753/http://statweb.stanford.edu/~susan/courses/s208/node12.html
|
265 |
+
|
266 |
+
"""
|
267 |
+
if self._rank is None:
|
268 |
+
self._rank = int(gray_to_bin(self.current), 2)
|
269 |
+
return self._rank
|
270 |
+
|
271 |
+
@property
|
272 |
+
def current(self):
|
273 |
+
"""
|
274 |
+
Returns the currently referenced Gray code as a bit string.
|
275 |
+
|
276 |
+
Examples
|
277 |
+
========
|
278 |
+
|
279 |
+
>>> from sympy.combinatorics import GrayCode
|
280 |
+
>>> GrayCode(3, start='100').current
|
281 |
+
'100'
|
282 |
+
"""
|
283 |
+
rv = self._current or '0'
|
284 |
+
if not isinstance(rv, str):
|
285 |
+
rv = bin(rv)[2:]
|
286 |
+
return rv.rjust(self.n, '0')
|
287 |
+
|
288 |
+
@classmethod
|
289 |
+
def unrank(self, n, rank):
|
290 |
+
"""
|
291 |
+
Unranks an n-bit sized Gray code of rank k. This method exists
|
292 |
+
so that a derivative GrayCode class can define its own code of
|
293 |
+
a given rank.
|
294 |
+
|
295 |
+
The string here is generated in reverse order to allow for tail-call
|
296 |
+
optimization.
|
297 |
+
|
298 |
+
Examples
|
299 |
+
========
|
300 |
+
|
301 |
+
>>> from sympy.combinatorics import GrayCode
|
302 |
+
>>> GrayCode(5, rank=3).current
|
303 |
+
'00010'
|
304 |
+
>>> GrayCode.unrank(5, 3)
|
305 |
+
'00010'
|
306 |
+
|
307 |
+
See Also
|
308 |
+
========
|
309 |
+
|
310 |
+
rank
|
311 |
+
"""
|
312 |
+
def _unrank(k, n):
|
313 |
+
if n == 1:
|
314 |
+
return str(k % 2)
|
315 |
+
m = 2**(n - 1)
|
316 |
+
if k < m:
|
317 |
+
return '0' + _unrank(k, n - 1)
|
318 |
+
return '1' + _unrank(m - (k % m) - 1, n - 1)
|
319 |
+
return _unrank(rank, n)
|
320 |
+
|
321 |
+
|
322 |
+
def random_bitstring(n):
|
323 |
+
"""
|
324 |
+
Generates a random bitlist of length n.
|
325 |
+
|
326 |
+
Examples
|
327 |
+
========
|
328 |
+
|
329 |
+
>>> from sympy.combinatorics.graycode import random_bitstring
|
330 |
+
>>> random_bitstring(3) # doctest: +SKIP
|
331 |
+
100
|
332 |
+
"""
|
333 |
+
return ''.join([random.choice('01') for i in range(n)])
|
334 |
+
|
335 |
+
|
336 |
+
def gray_to_bin(bin_list):
|
337 |
+
"""
|
338 |
+
Convert from Gray coding to binary coding.
|
339 |
+
|
340 |
+
We assume big endian encoding.
|
341 |
+
|
342 |
+
Examples
|
343 |
+
========
|
344 |
+
|
345 |
+
>>> from sympy.combinatorics.graycode import gray_to_bin
|
346 |
+
>>> gray_to_bin('100')
|
347 |
+
'111'
|
348 |
+
|
349 |
+
See Also
|
350 |
+
========
|
351 |
+
|
352 |
+
bin_to_gray
|
353 |
+
"""
|
354 |
+
b = [bin_list[0]]
|
355 |
+
for i in range(1, len(bin_list)):
|
356 |
+
b += str(int(b[i - 1] != bin_list[i]))
|
357 |
+
return ''.join(b)
|
358 |
+
|
359 |
+
|
360 |
+
def bin_to_gray(bin_list):
|
361 |
+
"""
|
362 |
+
Convert from binary coding to gray coding.
|
363 |
+
|
364 |
+
We assume big endian encoding.
|
365 |
+
|
366 |
+
Examples
|
367 |
+
========
|
368 |
+
|
369 |
+
>>> from sympy.combinatorics.graycode import bin_to_gray
|
370 |
+
>>> bin_to_gray('111')
|
371 |
+
'100'
|
372 |
+
|
373 |
+
See Also
|
374 |
+
========
|
375 |
+
|
376 |
+
gray_to_bin
|
377 |
+
"""
|
378 |
+
b = [bin_list[0]]
|
379 |
+
for i in range(1, len(bin_list)):
|
380 |
+
b += str(int(bin_list[i]) ^ int(bin_list[i - 1]))
|
381 |
+
return ''.join(b)
|
382 |
+
|
383 |
+
|
384 |
+
def get_subset_from_bitstring(super_set, bitstring):
|
385 |
+
"""
|
386 |
+
Gets the subset defined by the bitstring.
|
387 |
+
|
388 |
+
Examples
|
389 |
+
========
|
390 |
+
|
391 |
+
>>> from sympy.combinatorics.graycode import get_subset_from_bitstring
|
392 |
+
>>> get_subset_from_bitstring(['a', 'b', 'c', 'd'], '0011')
|
393 |
+
['c', 'd']
|
394 |
+
>>> get_subset_from_bitstring(['c', 'a', 'c', 'c'], '1100')
|
395 |
+
['c', 'a']
|
396 |
+
|
397 |
+
See Also
|
398 |
+
========
|
399 |
+
|
400 |
+
graycode_subsets
|
401 |
+
"""
|
402 |
+
if len(super_set) != len(bitstring):
|
403 |
+
raise ValueError("The sizes of the lists are not equal")
|
404 |
+
return [super_set[i] for i, j in enumerate(bitstring)
|
405 |
+
if bitstring[i] == '1']
|
406 |
+
|
407 |
+
|
408 |
+
def graycode_subsets(gray_code_set):
|
409 |
+
"""
|
410 |
+
Generates the subsets as enumerated by a Gray code.
|
411 |
+
|
412 |
+
Examples
|
413 |
+
========
|
414 |
+
|
415 |
+
>>> from sympy.combinatorics.graycode import graycode_subsets
|
416 |
+
>>> list(graycode_subsets(['a', 'b', 'c']))
|
417 |
+
[[], ['c'], ['b', 'c'], ['b'], ['a', 'b'], ['a', 'b', 'c'], \
|
418 |
+
['a', 'c'], ['a']]
|
419 |
+
>>> list(graycode_subsets(['a', 'b', 'c', 'c']))
|
420 |
+
[[], ['c'], ['c', 'c'], ['c'], ['b', 'c'], ['b', 'c', 'c'], \
|
421 |
+
['b', 'c'], ['b'], ['a', 'b'], ['a', 'b', 'c'], ['a', 'b', 'c', 'c'], \
|
422 |
+
['a', 'b', 'c'], ['a', 'c'], ['a', 'c', 'c'], ['a', 'c'], ['a']]
|
423 |
+
|
424 |
+
See Also
|
425 |
+
========
|
426 |
+
|
427 |
+
get_subset_from_bitstring
|
428 |
+
"""
|
429 |
+
for bitstring in list(GrayCode(len(gray_code_set)).generate_gray()):
|
430 |
+
yield get_subset_from_bitstring(gray_code_set, bitstring)
|
venv/lib/python3.10/site-packages/sympy/combinatorics/group_constructs.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
2 |
+
from sympy.combinatorics.permutations import Permutation
|
3 |
+
from sympy.utilities.iterables import uniq
|
4 |
+
|
5 |
+
_af_new = Permutation._af_new
|
6 |
+
|
7 |
+
|
8 |
+
def DirectProduct(*groups):
|
9 |
+
"""
|
10 |
+
Returns the direct product of several groups as a permutation group.
|
11 |
+
|
12 |
+
Explanation
|
13 |
+
===========
|
14 |
+
|
15 |
+
This is implemented much like the __mul__ procedure for taking the direct
|
16 |
+
product of two permutation groups, but the idea of shifting the
|
17 |
+
generators is realized in the case of an arbitrary number of groups.
|
18 |
+
A call to DirectProduct(G1, G2, ..., Gn) is generally expected to be faster
|
19 |
+
than a call to G1*G2*...*Gn (and thus the need for this algorithm).
|
20 |
+
|
21 |
+
Examples
|
22 |
+
========
|
23 |
+
|
24 |
+
>>> from sympy.combinatorics.group_constructs import DirectProduct
|
25 |
+
>>> from sympy.combinatorics.named_groups import CyclicGroup
|
26 |
+
>>> C = CyclicGroup(4)
|
27 |
+
>>> G = DirectProduct(C, C, C)
|
28 |
+
>>> G.order()
|
29 |
+
64
|
30 |
+
|
31 |
+
See Also
|
32 |
+
========
|
33 |
+
|
34 |
+
sympy.combinatorics.perm_groups.PermutationGroup.__mul__
|
35 |
+
|
36 |
+
"""
|
37 |
+
degrees = []
|
38 |
+
gens_count = []
|
39 |
+
total_degree = 0
|
40 |
+
total_gens = 0
|
41 |
+
for group in groups:
|
42 |
+
current_deg = group.degree
|
43 |
+
current_num_gens = len(group.generators)
|
44 |
+
degrees.append(current_deg)
|
45 |
+
total_degree += current_deg
|
46 |
+
gens_count.append(current_num_gens)
|
47 |
+
total_gens += current_num_gens
|
48 |
+
array_gens = []
|
49 |
+
for i in range(total_gens):
|
50 |
+
array_gens.append(list(range(total_degree)))
|
51 |
+
current_gen = 0
|
52 |
+
current_deg = 0
|
53 |
+
for i in range(len(gens_count)):
|
54 |
+
for j in range(current_gen, current_gen + gens_count[i]):
|
55 |
+
gen = ((groups[i].generators)[j - current_gen]).array_form
|
56 |
+
array_gens[j][current_deg:current_deg + degrees[i]] = \
|
57 |
+
[x + current_deg for x in gen]
|
58 |
+
current_gen += gens_count[i]
|
59 |
+
current_deg += degrees[i]
|
60 |
+
perm_gens = list(uniq([_af_new(list(a)) for a in array_gens]))
|
61 |
+
return PermutationGroup(perm_gens, dups=False)
|
venv/lib/python3.10/site-packages/sympy/combinatorics/group_numbers.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core import Integer, Pow, Mod
|
2 |
+
from sympy import factorint
|
3 |
+
|
4 |
+
|
5 |
+
def is_nilpotent_number(n):
|
6 |
+
"""
|
7 |
+
Check whether `n` is a nilpotent number. A number `n` is said to be
|
8 |
+
nilpotent if and only if every finite group of order `n` is nilpotent.
|
9 |
+
For more information see [1]_.
|
10 |
+
|
11 |
+
Examples
|
12 |
+
========
|
13 |
+
|
14 |
+
>>> from sympy.combinatorics.group_numbers import is_nilpotent_number
|
15 |
+
>>> from sympy import randprime
|
16 |
+
>>> is_nilpotent_number(21)
|
17 |
+
False
|
18 |
+
>>> is_nilpotent_number(randprime(1, 30)**12)
|
19 |
+
True
|
20 |
+
|
21 |
+
References
|
22 |
+
==========
|
23 |
+
|
24 |
+
.. [1] Pakianathan, J., Shankar, K., *Nilpotent Numbers*,
|
25 |
+
The American Mathematical Monthly, 107(7), 631-634.
|
26 |
+
|
27 |
+
|
28 |
+
"""
|
29 |
+
if n <= 0 or int(n) != n:
|
30 |
+
raise ValueError("n must be a positive integer, not %i" % n)
|
31 |
+
|
32 |
+
n = Integer(n)
|
33 |
+
prime_factors = list(factorint(n).items())
|
34 |
+
is_nilpotent = True
|
35 |
+
for p_j, a_j in prime_factors:
|
36 |
+
for p_i, a_i in prime_factors:
|
37 |
+
if any([Mod(Pow(p_i, k), p_j) == 1 for k in range(1, a_i + 1)]):
|
38 |
+
is_nilpotent = False
|
39 |
+
break
|
40 |
+
if not is_nilpotent:
|
41 |
+
break
|
42 |
+
|
43 |
+
return is_nilpotent
|
44 |
+
|
45 |
+
|
46 |
+
def is_abelian_number(n):
|
47 |
+
"""
|
48 |
+
Check whether `n` is an abelian number. A number `n` is said to be abelian
|
49 |
+
if and only if every finite group of order `n` is abelian. For more
|
50 |
+
information see [1]_.
|
51 |
+
|
52 |
+
Examples
|
53 |
+
========
|
54 |
+
|
55 |
+
>>> from sympy.combinatorics.group_numbers import is_abelian_number
|
56 |
+
>>> from sympy import randprime
|
57 |
+
>>> is_abelian_number(4)
|
58 |
+
True
|
59 |
+
>>> is_abelian_number(randprime(1, 2000)**2)
|
60 |
+
True
|
61 |
+
>>> is_abelian_number(60)
|
62 |
+
False
|
63 |
+
|
64 |
+
References
|
65 |
+
==========
|
66 |
+
|
67 |
+
.. [1] Pakianathan, J., Shankar, K., *Nilpotent Numbers*,
|
68 |
+
The American Mathematical Monthly, 107(7), 631-634.
|
69 |
+
|
70 |
+
|
71 |
+
"""
|
72 |
+
if n <= 0 or int(n) != n:
|
73 |
+
raise ValueError("n must be a positive integer, not %i" % n)
|
74 |
+
|
75 |
+
n = Integer(n)
|
76 |
+
if not is_nilpotent_number(n):
|
77 |
+
return False
|
78 |
+
|
79 |
+
prime_factors = list(factorint(n).items())
|
80 |
+
is_abelian = all(a_i < 3 for p_i, a_i in prime_factors)
|
81 |
+
return is_abelian
|
82 |
+
|
83 |
+
|
84 |
+
def is_cyclic_number(n):
|
85 |
+
"""
|
86 |
+
Check whether `n` is a cyclic number. A number `n` is said to be cyclic
|
87 |
+
if and only if every finite group of order `n` is cyclic. For more
|
88 |
+
information see [1]_.
|
89 |
+
|
90 |
+
Examples
|
91 |
+
========
|
92 |
+
|
93 |
+
>>> from sympy.combinatorics.group_numbers import is_cyclic_number
|
94 |
+
>>> from sympy import randprime
|
95 |
+
>>> is_cyclic_number(15)
|
96 |
+
True
|
97 |
+
>>> is_cyclic_number(randprime(1, 2000)**2)
|
98 |
+
False
|
99 |
+
>>> is_cyclic_number(4)
|
100 |
+
False
|
101 |
+
|
102 |
+
References
|
103 |
+
==========
|
104 |
+
|
105 |
+
.. [1] Pakianathan, J., Shankar, K., *Nilpotent Numbers*,
|
106 |
+
The American Mathematical Monthly, 107(7), 631-634.
|
107 |
+
|
108 |
+
"""
|
109 |
+
if n <= 0 or int(n) != n:
|
110 |
+
raise ValueError("n must be a positive integer, not %i" % n)
|
111 |
+
|
112 |
+
n = Integer(n)
|
113 |
+
if not is_nilpotent_number(n):
|
114 |
+
return False
|
115 |
+
|
116 |
+
prime_factors = list(factorint(n).items())
|
117 |
+
is_cyclic = all(a_i < 2 for p_i, a_i in prime_factors)
|
118 |
+
return is_cyclic
|
venv/lib/python3.10/site-packages/sympy/combinatorics/homomorphisms.py
ADDED
@@ -0,0 +1,549 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import itertools
|
2 |
+
from sympy.combinatorics.fp_groups import FpGroup, FpSubgroup, simplify_presentation
|
3 |
+
from sympy.combinatorics.free_groups import FreeGroup
|
4 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
5 |
+
from sympy.core.numbers import igcd
|
6 |
+
from sympy.ntheory.factor_ import totient
|
7 |
+
from sympy.core.singleton import S
|
8 |
+
|
9 |
+
class GroupHomomorphism:
|
10 |
+
'''
|
11 |
+
A class representing group homomorphisms. Instantiate using `homomorphism()`.
|
12 |
+
|
13 |
+
References
|
14 |
+
==========
|
15 |
+
|
16 |
+
.. [1] Holt, D., Eick, B. and O'Brien, E. (2005). Handbook of computational group theory.
|
17 |
+
|
18 |
+
'''
|
19 |
+
|
20 |
+
def __init__(self, domain, codomain, images):
|
21 |
+
self.domain = domain
|
22 |
+
self.codomain = codomain
|
23 |
+
self.images = images
|
24 |
+
self._inverses = None
|
25 |
+
self._kernel = None
|
26 |
+
self._image = None
|
27 |
+
|
28 |
+
def _invs(self):
|
29 |
+
'''
|
30 |
+
Return a dictionary with `{gen: inverse}` where `gen` is a rewriting
|
31 |
+
generator of `codomain` (e.g. strong generator for permutation groups)
|
32 |
+
and `inverse` is an element of its preimage
|
33 |
+
|
34 |
+
'''
|
35 |
+
image = self.image()
|
36 |
+
inverses = {}
|
37 |
+
for k in list(self.images.keys()):
|
38 |
+
v = self.images[k]
|
39 |
+
if not (v in inverses
|
40 |
+
or v.is_identity):
|
41 |
+
inverses[v] = k
|
42 |
+
if isinstance(self.codomain, PermutationGroup):
|
43 |
+
gens = image.strong_gens
|
44 |
+
else:
|
45 |
+
gens = image.generators
|
46 |
+
for g in gens:
|
47 |
+
if g in inverses or g.is_identity:
|
48 |
+
continue
|
49 |
+
w = self.domain.identity
|
50 |
+
if isinstance(self.codomain, PermutationGroup):
|
51 |
+
parts = image._strong_gens_slp[g][::-1]
|
52 |
+
else:
|
53 |
+
parts = g
|
54 |
+
for s in parts:
|
55 |
+
if s in inverses:
|
56 |
+
w = w*inverses[s]
|
57 |
+
else:
|
58 |
+
w = w*inverses[s**-1]**-1
|
59 |
+
inverses[g] = w
|
60 |
+
|
61 |
+
return inverses
|
62 |
+
|
63 |
+
def invert(self, g):
|
64 |
+
'''
|
65 |
+
Return an element of the preimage of ``g`` or of each element
|
66 |
+
of ``g`` if ``g`` is a list.
|
67 |
+
|
68 |
+
Explanation
|
69 |
+
===========
|
70 |
+
|
71 |
+
If the codomain is an FpGroup, the inverse for equal
|
72 |
+
elements might not always be the same unless the FpGroup's
|
73 |
+
rewriting system is confluent. However, making a system
|
74 |
+
confluent can be time-consuming. If it's important, try
|
75 |
+
`self.codomain.make_confluent()` first.
|
76 |
+
|
77 |
+
'''
|
78 |
+
from sympy.combinatorics import Permutation
|
79 |
+
from sympy.combinatorics.free_groups import FreeGroupElement
|
80 |
+
if isinstance(g, (Permutation, FreeGroupElement)):
|
81 |
+
if isinstance(self.codomain, FpGroup):
|
82 |
+
g = self.codomain.reduce(g)
|
83 |
+
if self._inverses is None:
|
84 |
+
self._inverses = self._invs()
|
85 |
+
image = self.image()
|
86 |
+
w = self.domain.identity
|
87 |
+
if isinstance(self.codomain, PermutationGroup):
|
88 |
+
gens = image.generator_product(g)[::-1]
|
89 |
+
else:
|
90 |
+
gens = g
|
91 |
+
# the following can't be "for s in gens:"
|
92 |
+
# because that would be equivalent to
|
93 |
+
# "for s in gens.array_form:" when g is
|
94 |
+
# a FreeGroupElement. On the other hand,
|
95 |
+
# when you call gens by index, the generator
|
96 |
+
# (or inverse) at position i is returned.
|
97 |
+
for i in range(len(gens)):
|
98 |
+
s = gens[i]
|
99 |
+
if s.is_identity:
|
100 |
+
continue
|
101 |
+
if s in self._inverses:
|
102 |
+
w = w*self._inverses[s]
|
103 |
+
else:
|
104 |
+
w = w*self._inverses[s**-1]**-1
|
105 |
+
return w
|
106 |
+
elif isinstance(g, list):
|
107 |
+
return [self.invert(e) for e in g]
|
108 |
+
|
109 |
+
def kernel(self):
|
110 |
+
'''
|
111 |
+
Compute the kernel of `self`.
|
112 |
+
|
113 |
+
'''
|
114 |
+
if self._kernel is None:
|
115 |
+
self._kernel = self._compute_kernel()
|
116 |
+
return self._kernel
|
117 |
+
|
118 |
+
def _compute_kernel(self):
|
119 |
+
G = self.domain
|
120 |
+
G_order = G.order()
|
121 |
+
if G_order is S.Infinity:
|
122 |
+
raise NotImplementedError(
|
123 |
+
"Kernel computation is not implemented for infinite groups")
|
124 |
+
gens = []
|
125 |
+
if isinstance(G, PermutationGroup):
|
126 |
+
K = PermutationGroup(G.identity)
|
127 |
+
else:
|
128 |
+
K = FpSubgroup(G, gens, normal=True)
|
129 |
+
i = self.image().order()
|
130 |
+
while K.order()*i != G_order:
|
131 |
+
r = G.random()
|
132 |
+
k = r*self.invert(self(r))**-1
|
133 |
+
if k not in K:
|
134 |
+
gens.append(k)
|
135 |
+
if isinstance(G, PermutationGroup):
|
136 |
+
K = PermutationGroup(gens)
|
137 |
+
else:
|
138 |
+
K = FpSubgroup(G, gens, normal=True)
|
139 |
+
return K
|
140 |
+
|
141 |
+
def image(self):
|
142 |
+
'''
|
143 |
+
Compute the image of `self`.
|
144 |
+
|
145 |
+
'''
|
146 |
+
if self._image is None:
|
147 |
+
values = list(set(self.images.values()))
|
148 |
+
if isinstance(self.codomain, PermutationGroup):
|
149 |
+
self._image = self.codomain.subgroup(values)
|
150 |
+
else:
|
151 |
+
self._image = FpSubgroup(self.codomain, values)
|
152 |
+
return self._image
|
153 |
+
|
154 |
+
def _apply(self, elem):
|
155 |
+
'''
|
156 |
+
Apply `self` to `elem`.
|
157 |
+
|
158 |
+
'''
|
159 |
+
if elem not in self.domain:
|
160 |
+
if isinstance(elem, (list, tuple)):
|
161 |
+
return [self._apply(e) for e in elem]
|
162 |
+
raise ValueError("The supplied element does not belong to the domain")
|
163 |
+
if elem.is_identity:
|
164 |
+
return self.codomain.identity
|
165 |
+
else:
|
166 |
+
images = self.images
|
167 |
+
value = self.codomain.identity
|
168 |
+
if isinstance(self.domain, PermutationGroup):
|
169 |
+
gens = self.domain.generator_product(elem, original=True)
|
170 |
+
for g in gens:
|
171 |
+
if g in self.images:
|
172 |
+
value = images[g]*value
|
173 |
+
else:
|
174 |
+
value = images[g**-1]**-1*value
|
175 |
+
else:
|
176 |
+
i = 0
|
177 |
+
for _, p in elem.array_form:
|
178 |
+
if p < 0:
|
179 |
+
g = elem[i]**-1
|
180 |
+
else:
|
181 |
+
g = elem[i]
|
182 |
+
value = value*images[g]**p
|
183 |
+
i += abs(p)
|
184 |
+
return value
|
185 |
+
|
186 |
+
def __call__(self, elem):
|
187 |
+
return self._apply(elem)
|
188 |
+
|
189 |
+
def is_injective(self):
|
190 |
+
'''
|
191 |
+
Check if the homomorphism is injective
|
192 |
+
|
193 |
+
'''
|
194 |
+
return self.kernel().order() == 1
|
195 |
+
|
196 |
+
def is_surjective(self):
|
197 |
+
'''
|
198 |
+
Check if the homomorphism is surjective
|
199 |
+
|
200 |
+
'''
|
201 |
+
im = self.image().order()
|
202 |
+
oth = self.codomain.order()
|
203 |
+
if im is S.Infinity and oth is S.Infinity:
|
204 |
+
return None
|
205 |
+
else:
|
206 |
+
return im == oth
|
207 |
+
|
208 |
+
def is_isomorphism(self):
|
209 |
+
'''
|
210 |
+
Check if `self` is an isomorphism.
|
211 |
+
|
212 |
+
'''
|
213 |
+
return self.is_injective() and self.is_surjective()
|
214 |
+
|
215 |
+
def is_trivial(self):
|
216 |
+
'''
|
217 |
+
Check is `self` is a trivial homomorphism, i.e. all elements
|
218 |
+
are mapped to the identity.
|
219 |
+
|
220 |
+
'''
|
221 |
+
return self.image().order() == 1
|
222 |
+
|
223 |
+
def compose(self, other):
|
224 |
+
'''
|
225 |
+
Return the composition of `self` and `other`, i.e.
|
226 |
+
the homomorphism phi such that for all g in the domain
|
227 |
+
of `other`, phi(g) = self(other(g))
|
228 |
+
|
229 |
+
'''
|
230 |
+
if not other.image().is_subgroup(self.domain):
|
231 |
+
raise ValueError("The image of `other` must be a subgroup of "
|
232 |
+
"the domain of `self`")
|
233 |
+
images = {g: self(other(g)) for g in other.images}
|
234 |
+
return GroupHomomorphism(other.domain, self.codomain, images)
|
235 |
+
|
236 |
+
def restrict_to(self, H):
|
237 |
+
'''
|
238 |
+
Return the restriction of the homomorphism to the subgroup `H`
|
239 |
+
of the domain.
|
240 |
+
|
241 |
+
'''
|
242 |
+
if not isinstance(H, PermutationGroup) or not H.is_subgroup(self.domain):
|
243 |
+
raise ValueError("Given H is not a subgroup of the domain")
|
244 |
+
domain = H
|
245 |
+
images = {g: self(g) for g in H.generators}
|
246 |
+
return GroupHomomorphism(domain, self.codomain, images)
|
247 |
+
|
248 |
+
def invert_subgroup(self, H):
|
249 |
+
'''
|
250 |
+
Return the subgroup of the domain that is the inverse image
|
251 |
+
of the subgroup ``H`` of the homomorphism image
|
252 |
+
|
253 |
+
'''
|
254 |
+
if not H.is_subgroup(self.image()):
|
255 |
+
raise ValueError("Given H is not a subgroup of the image")
|
256 |
+
gens = []
|
257 |
+
P = PermutationGroup(self.image().identity)
|
258 |
+
for h in H.generators:
|
259 |
+
h_i = self.invert(h)
|
260 |
+
if h_i not in P:
|
261 |
+
gens.append(h_i)
|
262 |
+
P = PermutationGroup(gens)
|
263 |
+
for k in self.kernel().generators:
|
264 |
+
if k*h_i not in P:
|
265 |
+
gens.append(k*h_i)
|
266 |
+
P = PermutationGroup(gens)
|
267 |
+
return P
|
268 |
+
|
269 |
+
def homomorphism(domain, codomain, gens, images=(), check=True):
|
270 |
+
'''
|
271 |
+
Create (if possible) a group homomorphism from the group ``domain``
|
272 |
+
to the group ``codomain`` defined by the images of the domain's
|
273 |
+
generators ``gens``. ``gens`` and ``images`` can be either lists or tuples
|
274 |
+
of equal sizes. If ``gens`` is a proper subset of the group's generators,
|
275 |
+
the unspecified generators will be mapped to the identity. If the
|
276 |
+
images are not specified, a trivial homomorphism will be created.
|
277 |
+
|
278 |
+
If the given images of the generators do not define a homomorphism,
|
279 |
+
an exception is raised.
|
280 |
+
|
281 |
+
If ``check`` is ``False``, do not check whether the given images actually
|
282 |
+
define a homomorphism.
|
283 |
+
|
284 |
+
'''
|
285 |
+
if not isinstance(domain, (PermutationGroup, FpGroup, FreeGroup)):
|
286 |
+
raise TypeError("The domain must be a group")
|
287 |
+
if not isinstance(codomain, (PermutationGroup, FpGroup, FreeGroup)):
|
288 |
+
raise TypeError("The codomain must be a group")
|
289 |
+
|
290 |
+
generators = domain.generators
|
291 |
+
if not all(g in generators for g in gens):
|
292 |
+
raise ValueError("The supplied generators must be a subset of the domain's generators")
|
293 |
+
if not all(g in codomain for g in images):
|
294 |
+
raise ValueError("The images must be elements of the codomain")
|
295 |
+
|
296 |
+
if images and len(images) != len(gens):
|
297 |
+
raise ValueError("The number of images must be equal to the number of generators")
|
298 |
+
|
299 |
+
gens = list(gens)
|
300 |
+
images = list(images)
|
301 |
+
|
302 |
+
images.extend([codomain.identity]*(len(generators)-len(images)))
|
303 |
+
gens.extend([g for g in generators if g not in gens])
|
304 |
+
images = dict(zip(gens,images))
|
305 |
+
|
306 |
+
if check and not _check_homomorphism(domain, codomain, images):
|
307 |
+
raise ValueError("The given images do not define a homomorphism")
|
308 |
+
return GroupHomomorphism(domain, codomain, images)
|
309 |
+
|
310 |
+
def _check_homomorphism(domain, codomain, images):
|
311 |
+
"""
|
312 |
+
Check that a given mapping of generators to images defines a homomorphism.
|
313 |
+
|
314 |
+
Parameters
|
315 |
+
==========
|
316 |
+
domain : PermutationGroup, FpGroup, FreeGroup
|
317 |
+
codomain : PermutationGroup, FpGroup, FreeGroup
|
318 |
+
images : dict
|
319 |
+
The set of keys must be equal to domain.generators.
|
320 |
+
The values must be elements of the codomain.
|
321 |
+
|
322 |
+
"""
|
323 |
+
pres = domain if hasattr(domain, 'relators') else domain.presentation()
|
324 |
+
rels = pres.relators
|
325 |
+
gens = pres.generators
|
326 |
+
symbols = [g.ext_rep[0] for g in gens]
|
327 |
+
symbols_to_domain_generators = dict(zip(symbols, domain.generators))
|
328 |
+
identity = codomain.identity
|
329 |
+
|
330 |
+
def _image(r):
|
331 |
+
w = identity
|
332 |
+
for symbol, power in r.array_form:
|
333 |
+
g = symbols_to_domain_generators[symbol]
|
334 |
+
w *= images[g]**power
|
335 |
+
return w
|
336 |
+
|
337 |
+
for r in rels:
|
338 |
+
if isinstance(codomain, FpGroup):
|
339 |
+
s = codomain.equals(_image(r), identity)
|
340 |
+
if s is None:
|
341 |
+
# only try to make the rewriting system
|
342 |
+
# confluent when it can't determine the
|
343 |
+
# truth of equality otherwise
|
344 |
+
success = codomain.make_confluent()
|
345 |
+
s = codomain.equals(_image(r), identity)
|
346 |
+
if s is None and not success:
|
347 |
+
raise RuntimeError("Can't determine if the images "
|
348 |
+
"define a homomorphism. Try increasing "
|
349 |
+
"the maximum number of rewriting rules "
|
350 |
+
"(group._rewriting_system.set_max(new_value); "
|
351 |
+
"the current value is stored in group._rewriting"
|
352 |
+
"_system.maxeqns)")
|
353 |
+
else:
|
354 |
+
s = _image(r).is_identity
|
355 |
+
if not s:
|
356 |
+
return False
|
357 |
+
return True
|
358 |
+
|
359 |
+
def orbit_homomorphism(group, omega):
|
360 |
+
'''
|
361 |
+
Return the homomorphism induced by the action of the permutation
|
362 |
+
group ``group`` on the set ``omega`` that is closed under the action.
|
363 |
+
|
364 |
+
'''
|
365 |
+
from sympy.combinatorics import Permutation
|
366 |
+
from sympy.combinatorics.named_groups import SymmetricGroup
|
367 |
+
codomain = SymmetricGroup(len(omega))
|
368 |
+
identity = codomain.identity
|
369 |
+
omega = list(omega)
|
370 |
+
images = {g: identity*Permutation([omega.index(o^g) for o in omega]) for g in group.generators}
|
371 |
+
group._schreier_sims(base=omega)
|
372 |
+
H = GroupHomomorphism(group, codomain, images)
|
373 |
+
if len(group.basic_stabilizers) > len(omega):
|
374 |
+
H._kernel = group.basic_stabilizers[len(omega)]
|
375 |
+
else:
|
376 |
+
H._kernel = PermutationGroup([group.identity])
|
377 |
+
return H
|
378 |
+
|
379 |
+
def block_homomorphism(group, blocks):
|
380 |
+
'''
|
381 |
+
Return the homomorphism induced by the action of the permutation
|
382 |
+
group ``group`` on the block system ``blocks``. The latter should be
|
383 |
+
of the same form as returned by the ``minimal_block`` method for
|
384 |
+
permutation groups, namely a list of length ``group.degree`` where
|
385 |
+
the i-th entry is a representative of the block i belongs to.
|
386 |
+
|
387 |
+
'''
|
388 |
+
from sympy.combinatorics import Permutation
|
389 |
+
from sympy.combinatorics.named_groups import SymmetricGroup
|
390 |
+
|
391 |
+
n = len(blocks)
|
392 |
+
|
393 |
+
# number the blocks; m is the total number,
|
394 |
+
# b is such that b[i] is the number of the block i belongs to,
|
395 |
+
# p is the list of length m such that p[i] is the representative
|
396 |
+
# of the i-th block
|
397 |
+
m = 0
|
398 |
+
p = []
|
399 |
+
b = [None]*n
|
400 |
+
for i in range(n):
|
401 |
+
if blocks[i] == i:
|
402 |
+
p.append(i)
|
403 |
+
b[i] = m
|
404 |
+
m += 1
|
405 |
+
for i in range(n):
|
406 |
+
b[i] = b[blocks[i]]
|
407 |
+
|
408 |
+
codomain = SymmetricGroup(m)
|
409 |
+
# the list corresponding to the identity permutation in codomain
|
410 |
+
identity = range(m)
|
411 |
+
images = {g: Permutation([b[p[i]^g] for i in identity]) for g in group.generators}
|
412 |
+
H = GroupHomomorphism(group, codomain, images)
|
413 |
+
return H
|
414 |
+
|
415 |
+
def group_isomorphism(G, H, isomorphism=True):
|
416 |
+
'''
|
417 |
+
Compute an isomorphism between 2 given groups.
|
418 |
+
|
419 |
+
Parameters
|
420 |
+
==========
|
421 |
+
|
422 |
+
G : A finite ``FpGroup`` or a ``PermutationGroup``.
|
423 |
+
First group.
|
424 |
+
|
425 |
+
H : A finite ``FpGroup`` or a ``PermutationGroup``
|
426 |
+
Second group.
|
427 |
+
|
428 |
+
isomorphism : bool
|
429 |
+
This is used to avoid the computation of homomorphism
|
430 |
+
when the user only wants to check if there exists
|
431 |
+
an isomorphism between the groups.
|
432 |
+
|
433 |
+
Returns
|
434 |
+
=======
|
435 |
+
|
436 |
+
If isomorphism = False -- Returns a boolean.
|
437 |
+
If isomorphism = True -- Returns a boolean and an isomorphism between `G` and `H`.
|
438 |
+
|
439 |
+
Examples
|
440 |
+
========
|
441 |
+
|
442 |
+
>>> from sympy.combinatorics import free_group, Permutation
|
443 |
+
>>> from sympy.combinatorics.perm_groups import PermutationGroup
|
444 |
+
>>> from sympy.combinatorics.fp_groups import FpGroup
|
445 |
+
>>> from sympy.combinatorics.homomorphisms import group_isomorphism
|
446 |
+
>>> from sympy.combinatorics.named_groups import DihedralGroup, AlternatingGroup
|
447 |
+
|
448 |
+
>>> D = DihedralGroup(8)
|
449 |
+
>>> p = Permutation(0, 1, 2, 3, 4, 5, 6, 7)
|
450 |
+
>>> P = PermutationGroup(p)
|
451 |
+
>>> group_isomorphism(D, P)
|
452 |
+
(False, None)
|
453 |
+
|
454 |
+
>>> F, a, b = free_group("a, b")
|
455 |
+
>>> G = FpGroup(F, [a**3, b**3, (a*b)**2])
|
456 |
+
>>> H = AlternatingGroup(4)
|
457 |
+
>>> (check, T) = group_isomorphism(G, H)
|
458 |
+
>>> check
|
459 |
+
True
|
460 |
+
>>> T(b*a*b**-1*a**-1*b**-1)
|
461 |
+
(0 2 3)
|
462 |
+
|
463 |
+
Notes
|
464 |
+
=====
|
465 |
+
|
466 |
+
Uses the approach suggested by Robert Tarjan to compute the isomorphism between two groups.
|
467 |
+
First, the generators of ``G`` are mapped to the elements of ``H`` and
|
468 |
+
we check if the mapping induces an isomorphism.
|
469 |
+
|
470 |
+
'''
|
471 |
+
if not isinstance(G, (PermutationGroup, FpGroup)):
|
472 |
+
raise TypeError("The group must be a PermutationGroup or an FpGroup")
|
473 |
+
if not isinstance(H, (PermutationGroup, FpGroup)):
|
474 |
+
raise TypeError("The group must be a PermutationGroup or an FpGroup")
|
475 |
+
|
476 |
+
if isinstance(G, FpGroup) and isinstance(H, FpGroup):
|
477 |
+
G = simplify_presentation(G)
|
478 |
+
H = simplify_presentation(H)
|
479 |
+
# Two infinite FpGroups with the same generators are isomorphic
|
480 |
+
# when the relators are same but are ordered differently.
|
481 |
+
if G.generators == H.generators and (G.relators).sort() == (H.relators).sort():
|
482 |
+
if not isomorphism:
|
483 |
+
return True
|
484 |
+
return (True, homomorphism(G, H, G.generators, H.generators))
|
485 |
+
|
486 |
+
# `_H` is the permutation group isomorphic to `H`.
|
487 |
+
_H = H
|
488 |
+
g_order = G.order()
|
489 |
+
h_order = H.order()
|
490 |
+
|
491 |
+
if g_order is S.Infinity:
|
492 |
+
raise NotImplementedError("Isomorphism methods are not implemented for infinite groups.")
|
493 |
+
|
494 |
+
if isinstance(H, FpGroup):
|
495 |
+
if h_order is S.Infinity:
|
496 |
+
raise NotImplementedError("Isomorphism methods are not implemented for infinite groups.")
|
497 |
+
_H, h_isomorphism = H._to_perm_group()
|
498 |
+
|
499 |
+
if (g_order != h_order) or (G.is_abelian != H.is_abelian):
|
500 |
+
if not isomorphism:
|
501 |
+
return False
|
502 |
+
return (False, None)
|
503 |
+
|
504 |
+
if not isomorphism:
|
505 |
+
# Two groups of the same cyclic numbered order
|
506 |
+
# are isomorphic to each other.
|
507 |
+
n = g_order
|
508 |
+
if (igcd(n, totient(n))) == 1:
|
509 |
+
return True
|
510 |
+
|
511 |
+
# Match the generators of `G` with subsets of `_H`
|
512 |
+
gens = list(G.generators)
|
513 |
+
for subset in itertools.permutations(_H, len(gens)):
|
514 |
+
images = list(subset)
|
515 |
+
images.extend([_H.identity]*(len(G.generators)-len(images)))
|
516 |
+
_images = dict(zip(gens,images))
|
517 |
+
if _check_homomorphism(G, _H, _images):
|
518 |
+
if isinstance(H, FpGroup):
|
519 |
+
images = h_isomorphism.invert(images)
|
520 |
+
T = homomorphism(G, H, G.generators, images, check=False)
|
521 |
+
if T.is_isomorphism():
|
522 |
+
# It is a valid isomorphism
|
523 |
+
if not isomorphism:
|
524 |
+
return True
|
525 |
+
return (True, T)
|
526 |
+
|
527 |
+
if not isomorphism:
|
528 |
+
return False
|
529 |
+
return (False, None)
|
530 |
+
|
531 |
+
def is_isomorphic(G, H):
|
532 |
+
'''
|
533 |
+
Check if the groups are isomorphic to each other
|
534 |
+
|
535 |
+
Parameters
|
536 |
+
==========
|
537 |
+
|
538 |
+
G : A finite ``FpGroup`` or a ``PermutationGroup``
|
539 |
+
First group.
|
540 |
+
|
541 |
+
H : A finite ``FpGroup`` or a ``PermutationGroup``
|
542 |
+
Second group.
|
543 |
+
|
544 |
+
Returns
|
545 |
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics.group_constructs import DirectProduct
|
2 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
3 |
+
from sympy.combinatorics.permutations import Permutation
|
4 |
+
|
5 |
+
_af_new = Permutation._af_new
|
6 |
+
|
7 |
+
|
8 |
+
def AbelianGroup(*cyclic_orders):
|
9 |
+
"""
|
10 |
+
Returns the direct product of cyclic groups with the given orders.
|
11 |
+
|
12 |
+
Explanation
|
13 |
+
===========
|
14 |
+
|
15 |
+
According to the structure theorem for finite abelian groups ([1]),
|
16 |
+
every finite abelian group can be written as the direct product of
|
17 |
+
finitely many cyclic groups.
|
18 |
+
|
19 |
+
Examples
|
20 |
+
========
|
21 |
+
|
22 |
+
>>> from sympy.combinatorics.named_groups import AbelianGroup
|
23 |
+
>>> AbelianGroup(3, 4)
|
24 |
+
PermutationGroup([
|
25 |
+
(6)(0 1 2),
|
26 |
+
(3 4 5 6)])
|
27 |
+
>>> _.is_group
|
28 |
+
True
|
29 |
+
|
30 |
+
See Also
|
31 |
+
========
|
32 |
+
|
33 |
+
DirectProduct
|
34 |
+
|
35 |
+
References
|
36 |
+
==========
|
37 |
+
|
38 |
+
.. [1] https://groupprops.subwiki.org/wiki/Structure_theorem_for_finitely_generated_abelian_groups
|
39 |
+
|
40 |
+
"""
|
41 |
+
groups = []
|
42 |
+
degree = 0
|
43 |
+
order = 1
|
44 |
+
for size in cyclic_orders:
|
45 |
+
degree += size
|
46 |
+
order *= size
|
47 |
+
groups.append(CyclicGroup(size))
|
48 |
+
G = DirectProduct(*groups)
|
49 |
+
G._is_abelian = True
|
50 |
+
G._degree = degree
|
51 |
+
G._order = order
|
52 |
+
|
53 |
+
return G
|
54 |
+
|
55 |
+
|
56 |
+
def AlternatingGroup(n):
|
57 |
+
"""
|
58 |
+
Generates the alternating group on ``n`` elements as a permutation group.
|
59 |
+
|
60 |
+
Explanation
|
61 |
+
===========
|
62 |
+
|
63 |
+
For ``n > 2``, the generators taken are ``(0 1 2), (0 1 2 ... n-1)`` for
|
64 |
+
``n`` odd
|
65 |
+
and ``(0 1 2), (1 2 ... n-1)`` for ``n`` even (See [1], p.31, ex.6.9.).
|
66 |
+
After the group is generated, some of its basic properties are set.
|
67 |
+
The cases ``n = 1, 2`` are handled separately.
|
68 |
+
|
69 |
+
Examples
|
70 |
+
========
|
71 |
+
|
72 |
+
>>> from sympy.combinatorics.named_groups import AlternatingGroup
|
73 |
+
>>> G = AlternatingGroup(4)
|
74 |
+
>>> G.is_group
|
75 |
+
True
|
76 |
+
>>> a = list(G.generate_dimino())
|
77 |
+
>>> len(a)
|
78 |
+
12
|
79 |
+
>>> all(perm.is_even for perm in a)
|
80 |
+
True
|
81 |
+
|
82 |
+
See Also
|
83 |
+
========
|
84 |
+
|
85 |
+
SymmetricGroup, CyclicGroup, DihedralGroup
|
86 |
+
|
87 |
+
References
|
88 |
+
==========
|
89 |
+
|
90 |
+
.. [1] Armstrong, M. "Groups and Symmetry"
|
91 |
+
|
92 |
+
"""
|
93 |
+
# small cases are special
|
94 |
+
if n in (1, 2):
|
95 |
+
return PermutationGroup([Permutation([0])])
|
96 |
+
|
97 |
+
a = list(range(n))
|
98 |
+
a[0], a[1], a[2] = a[1], a[2], a[0]
|
99 |
+
gen1 = a
|
100 |
+
if n % 2:
|
101 |
+
a = list(range(1, n))
|
102 |
+
a.append(0)
|
103 |
+
gen2 = a
|
104 |
+
else:
|
105 |
+
a = list(range(2, n))
|
106 |
+
a.append(1)
|
107 |
+
a.insert(0, 0)
|
108 |
+
gen2 = a
|
109 |
+
gens = [gen1, gen2]
|
110 |
+
if gen1 == gen2:
|
111 |
+
gens = gens[:1]
|
112 |
+
G = PermutationGroup([_af_new(a) for a in gens], dups=False)
|
113 |
+
|
114 |
+
set_alternating_group_properties(G, n, n)
|
115 |
+
G._is_alt = True
|
116 |
+
return G
|
117 |
+
|
118 |
+
|
119 |
+
def set_alternating_group_properties(G, n, degree):
|
120 |
+
"""Set known properties of an alternating group. """
|
121 |
+
if n < 4:
|
122 |
+
G._is_abelian = True
|
123 |
+
G._is_nilpotent = True
|
124 |
+
else:
|
125 |
+
G._is_abelian = False
|
126 |
+
G._is_nilpotent = False
|
127 |
+
if n < 5:
|
128 |
+
G._is_solvable = True
|
129 |
+
else:
|
130 |
+
G._is_solvable = False
|
131 |
+
G._degree = degree
|
132 |
+
G._is_transitive = True
|
133 |
+
G._is_dihedral = False
|
134 |
+
|
135 |
+
|
136 |
+
def CyclicGroup(n):
|
137 |
+
"""
|
138 |
+
Generates the cyclic group of order ``n`` as a permutation group.
|
139 |
+
|
140 |
+
Explanation
|
141 |
+
===========
|
142 |
+
|
143 |
+
The generator taken is the ``n``-cycle ``(0 1 2 ... n-1)``
|
144 |
+
(in cycle notation). After the group is generated, some of its basic
|
145 |
+
properties are set.
|
146 |
+
|
147 |
+
Examples
|
148 |
+
========
|
149 |
+
|
150 |
+
>>> from sympy.combinatorics.named_groups import CyclicGroup
|
151 |
+
>>> G = CyclicGroup(6)
|
152 |
+
>>> G.is_group
|
153 |
+
True
|
154 |
+
>>> G.order()
|
155 |
+
6
|
156 |
+
>>> list(G.generate_schreier_sims(af=True))
|
157 |
+
[[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 0], [2, 3, 4, 5, 0, 1],
|
158 |
+
[3, 4, 5, 0, 1, 2], [4, 5, 0, 1, 2, 3], [5, 0, 1, 2, 3, 4]]
|
159 |
+
|
160 |
+
See Also
|
161 |
+
========
|
162 |
+
|
163 |
+
SymmetricGroup, DihedralGroup, AlternatingGroup
|
164 |
+
|
165 |
+
"""
|
166 |
+
a = list(range(1, n))
|
167 |
+
a.append(0)
|
168 |
+
gen = _af_new(a)
|
169 |
+
G = PermutationGroup([gen])
|
170 |
+
|
171 |
+
G._is_abelian = True
|
172 |
+
G._is_nilpotent = True
|
173 |
+
G._is_solvable = True
|
174 |
+
G._degree = n
|
175 |
+
G._is_transitive = True
|
176 |
+
G._order = n
|
177 |
+
G._is_dihedral = (n == 2)
|
178 |
+
return G
|
179 |
+
|
180 |
+
|
181 |
+
def DihedralGroup(n):
|
182 |
+
r"""
|
183 |
+
Generates the dihedral group `D_n` as a permutation group.
|
184 |
+
|
185 |
+
Explanation
|
186 |
+
===========
|
187 |
+
|
188 |
+
The dihedral group `D_n` is the group of symmetries of the regular
|
189 |
+
``n``-gon. The generators taken are the ``n``-cycle ``a = (0 1 2 ... n-1)``
|
190 |
+
(a rotation of the ``n``-gon) and ``b = (0 n-1)(1 n-2)...``
|
191 |
+
(a reflection of the ``n``-gon) in cycle rotation. It is easy to see that
|
192 |
+
these satisfy ``a**n = b**2 = 1`` and ``bab = ~a`` so they indeed generate
|
193 |
+
`D_n` (See [1]). After the group is generated, some of its basic properties
|
194 |
+
are set.
|
195 |
+
|
196 |
+
Examples
|
197 |
+
========
|
198 |
+
|
199 |
+
>>> from sympy.combinatorics.named_groups import DihedralGroup
|
200 |
+
>>> G = DihedralGroup(5)
|
201 |
+
>>> G.is_group
|
202 |
+
True
|
203 |
+
>>> a = list(G.generate_dimino())
|
204 |
+
>>> [perm.cyclic_form for perm in a]
|
205 |
+
[[], [[0, 1, 2, 3, 4]], [[0, 2, 4, 1, 3]],
|
206 |
+
[[0, 3, 1, 4, 2]], [[0, 4, 3, 2, 1]], [[0, 4], [1, 3]],
|
207 |
+
[[1, 4], [2, 3]], [[0, 1], [2, 4]], [[0, 2], [3, 4]],
|
208 |
+
[[0, 3], [1, 2]]]
|
209 |
+
|
210 |
+
See Also
|
211 |
+
========
|
212 |
+
|
213 |
+
SymmetricGroup, CyclicGroup, AlternatingGroup
|
214 |
+
|
215 |
+
References
|
216 |
+
==========
|
217 |
+
|
218 |
+
.. [1] https://en.wikipedia.org/wiki/Dihedral_group
|
219 |
+
|
220 |
+
"""
|
221 |
+
# small cases are special
|
222 |
+
if n == 1:
|
223 |
+
return PermutationGroup([Permutation([1, 0])])
|
224 |
+
if n == 2:
|
225 |
+
return PermutationGroup([Permutation([1, 0, 3, 2]),
|
226 |
+
Permutation([2, 3, 0, 1]), Permutation([3, 2, 1, 0])])
|
227 |
+
|
228 |
+
a = list(range(1, n))
|
229 |
+
a.append(0)
|
230 |
+
gen1 = _af_new(a)
|
231 |
+
a = list(range(n))
|
232 |
+
a.reverse()
|
233 |
+
gen2 = _af_new(a)
|
234 |
+
G = PermutationGroup([gen1, gen2])
|
235 |
+
# if n is a power of 2, group is nilpotent
|
236 |
+
if n & (n-1) == 0:
|
237 |
+
G._is_nilpotent = True
|
238 |
+
else:
|
239 |
+
G._is_nilpotent = False
|
240 |
+
G._is_dihedral = True
|
241 |
+
G._is_abelian = False
|
242 |
+
G._is_solvable = True
|
243 |
+
G._degree = n
|
244 |
+
G._is_transitive = True
|
245 |
+
G._order = 2*n
|
246 |
+
return G
|
247 |
+
|
248 |
+
|
249 |
+
def SymmetricGroup(n):
|
250 |
+
"""
|
251 |
+
Generates the symmetric group on ``n`` elements as a permutation group.
|
252 |
+
|
253 |
+
Explanation
|
254 |
+
===========
|
255 |
+
|
256 |
+
The generators taken are the ``n``-cycle
|
257 |
+
``(0 1 2 ... n-1)`` and the transposition ``(0 1)`` (in cycle notation).
|
258 |
+
(See [1]). After the group is generated, some of its basic properties
|
259 |
+
are set.
|
260 |
+
|
261 |
+
Examples
|
262 |
+
========
|
263 |
+
|
264 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
265 |
+
>>> G = SymmetricGroup(4)
|
266 |
+
>>> G.is_group
|
267 |
+
True
|
268 |
+
>>> G.order()
|
269 |
+
24
|
270 |
+
>>> list(G.generate_schreier_sims(af=True))
|
271 |
+
[[0, 1, 2, 3], [1, 2, 3, 0], [2, 3, 0, 1], [3, 1, 2, 0], [0, 2, 3, 1],
|
272 |
+
[1, 3, 0, 2], [2, 0, 1, 3], [3, 2, 0, 1], [0, 3, 1, 2], [1, 0, 2, 3],
|
273 |
+
[2, 1, 3, 0], [3, 0, 1, 2], [0, 1, 3, 2], [1, 2, 0, 3], [2, 3, 1, 0],
|
274 |
+
[3, 1, 0, 2], [0, 2, 1, 3], [1, 3, 2, 0], [2, 0, 3, 1], [3, 2, 1, 0],
|
275 |
+
[0, 3, 2, 1], [1, 0, 3, 2], [2, 1, 0, 3], [3, 0, 2, 1]]
|
276 |
+
|
277 |
+
See Also
|
278 |
+
========
|
279 |
+
|
280 |
+
CyclicGroup, DihedralGroup, AlternatingGroup
|
281 |
+
|
282 |
+
References
|
283 |
+
==========
|
284 |
+
|
285 |
+
.. [1] https://en.wikipedia.org/wiki/Symmetric_group#Generators_and_relations
|
286 |
+
|
287 |
+
"""
|
288 |
+
if n == 1:
|
289 |
+
G = PermutationGroup([Permutation([0])])
|
290 |
+
elif n == 2:
|
291 |
+
G = PermutationGroup([Permutation([1, 0])])
|
292 |
+
else:
|
293 |
+
a = list(range(1, n))
|
294 |
+
a.append(0)
|
295 |
+
gen1 = _af_new(a)
|
296 |
+
a = list(range(n))
|
297 |
+
a[0], a[1] = a[1], a[0]
|
298 |
+
gen2 = _af_new(a)
|
299 |
+
G = PermutationGroup([gen1, gen2])
|
300 |
+
set_symmetric_group_properties(G, n, n)
|
301 |
+
G._is_sym = True
|
302 |
+
return G
|
303 |
+
|
304 |
+
|
305 |
+
def set_symmetric_group_properties(G, n, degree):
|
306 |
+
"""Set known properties of a symmetric group. """
|
307 |
+
if n < 3:
|
308 |
+
G._is_abelian = True
|
309 |
+
G._is_nilpotent = True
|
310 |
+
else:
|
311 |
+
G._is_abelian = False
|
312 |
+
G._is_nilpotent = False
|
313 |
+
if n < 5:
|
314 |
+
G._is_solvable = True
|
315 |
+
else:
|
316 |
+
G._is_solvable = False
|
317 |
+
G._degree = degree
|
318 |
+
G._is_transitive = True
|
319 |
+
G._is_dihedral = (n in [2, 3]) # cf Landau's func and Stirling's approx
|
320 |
+
|
321 |
+
|
322 |
+
def RubikGroup(n):
|
323 |
+
"""Return a group of Rubik's cube generators
|
324 |
+
|
325 |
+
>>> from sympy.combinatorics.named_groups import RubikGroup
|
326 |
+
>>> RubikGroup(2).is_group
|
327 |
+
True
|
328 |
+
"""
|
329 |
+
from sympy.combinatorics.generators import rubik
|
330 |
+
if n <= 1:
|
331 |
+
raise ValueError("Invalid cube. n has to be greater than 1")
|
332 |
+
return PermutationGroup(rubik(n))
|
venv/lib/python3.10/site-packages/sympy/combinatorics/partitions.py
ADDED
@@ -0,0 +1,745 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core import Basic, Dict, sympify, Tuple
|
2 |
+
from sympy.core.numbers import Integer
|
3 |
+
from sympy.core.sorting import default_sort_key
|
4 |
+
from sympy.core.sympify import _sympify
|
5 |
+
from sympy.functions.combinatorial.numbers import bell
|
6 |
+
from sympy.matrices import zeros
|
7 |
+
from sympy.sets.sets import FiniteSet, Union
|
8 |
+
from sympy.utilities.iterables import flatten, group
|
9 |
+
from sympy.utilities.misc import as_int
|
10 |
+
|
11 |
+
|
12 |
+
from collections import defaultdict
|
13 |
+
|
14 |
+
|
15 |
+
class Partition(FiniteSet):
|
16 |
+
"""
|
17 |
+
This class represents an abstract partition.
|
18 |
+
|
19 |
+
A partition is a set of disjoint sets whose union equals a given set.
|
20 |
+
|
21 |
+
See Also
|
22 |
+
========
|
23 |
+
|
24 |
+
sympy.utilities.iterables.partitions,
|
25 |
+
sympy.utilities.iterables.multiset_partitions
|
26 |
+
"""
|
27 |
+
|
28 |
+
_rank = None
|
29 |
+
_partition = None
|
30 |
+
|
31 |
+
def __new__(cls, *partition):
|
32 |
+
"""
|
33 |
+
Generates a new partition object.
|
34 |
+
|
35 |
+
This method also verifies if the arguments passed are
|
36 |
+
valid and raises a ValueError if they are not.
|
37 |
+
|
38 |
+
Examples
|
39 |
+
========
|
40 |
+
|
41 |
+
Creating Partition from Python lists:
|
42 |
+
|
43 |
+
>>> from sympy.combinatorics import Partition
|
44 |
+
>>> a = Partition([1, 2], [3])
|
45 |
+
>>> a
|
46 |
+
Partition({3}, {1, 2})
|
47 |
+
>>> a.partition
|
48 |
+
[[1, 2], [3]]
|
49 |
+
>>> len(a)
|
50 |
+
2
|
51 |
+
>>> a.members
|
52 |
+
(1, 2, 3)
|
53 |
+
|
54 |
+
Creating Partition from Python sets:
|
55 |
+
|
56 |
+
>>> Partition({1, 2, 3}, {4, 5})
|
57 |
+
Partition({4, 5}, {1, 2, 3})
|
58 |
+
|
59 |
+
Creating Partition from SymPy finite sets:
|
60 |
+
|
61 |
+
>>> from sympy import FiniteSet
|
62 |
+
>>> a = FiniteSet(1, 2, 3)
|
63 |
+
>>> b = FiniteSet(4, 5)
|
64 |
+
>>> Partition(a, b)
|
65 |
+
Partition({4, 5}, {1, 2, 3})
|
66 |
+
"""
|
67 |
+
args = []
|
68 |
+
dups = False
|
69 |
+
for arg in partition:
|
70 |
+
if isinstance(arg, list):
|
71 |
+
as_set = set(arg)
|
72 |
+
if len(as_set) < len(arg):
|
73 |
+
dups = True
|
74 |
+
break # error below
|
75 |
+
arg = as_set
|
76 |
+
args.append(_sympify(arg))
|
77 |
+
|
78 |
+
if not all(isinstance(part, FiniteSet) for part in args):
|
79 |
+
raise ValueError(
|
80 |
+
"Each argument to Partition should be " \
|
81 |
+
"a list, set, or a FiniteSet")
|
82 |
+
|
83 |
+
# sort so we have a canonical reference for RGS
|
84 |
+
U = Union(*args)
|
85 |
+
if dups or len(U) < sum(len(arg) for arg in args):
|
86 |
+
raise ValueError("Partition contained duplicate elements.")
|
87 |
+
|
88 |
+
obj = FiniteSet.__new__(cls, *args)
|
89 |
+
obj.members = tuple(U)
|
90 |
+
obj.size = len(U)
|
91 |
+
return obj
|
92 |
+
|
93 |
+
def sort_key(self, order=None):
|
94 |
+
"""Return a canonical key that can be used for sorting.
|
95 |
+
|
96 |
+
Ordering is based on the size and sorted elements of the partition
|
97 |
+
and ties are broken with the rank.
|
98 |
+
|
99 |
+
Examples
|
100 |
+
========
|
101 |
+
|
102 |
+
>>> from sympy import default_sort_key
|
103 |
+
>>> from sympy.combinatorics import Partition
|
104 |
+
>>> from sympy.abc import x
|
105 |
+
>>> a = Partition([1, 2])
|
106 |
+
>>> b = Partition([3, 4])
|
107 |
+
>>> c = Partition([1, x])
|
108 |
+
>>> d = Partition(list(range(4)))
|
109 |
+
>>> l = [d, b, a + 1, a, c]
|
110 |
+
>>> l.sort(key=default_sort_key); l
|
111 |
+
[Partition({1, 2}), Partition({1}, {2}), Partition({1, x}), Partition({3, 4}), Partition({0, 1, 2, 3})]
|
112 |
+
"""
|
113 |
+
if order is None:
|
114 |
+
members = self.members
|
115 |
+
else:
|
116 |
+
members = tuple(sorted(self.members,
|
117 |
+
key=lambda w: default_sort_key(w, order)))
|
118 |
+
return tuple(map(default_sort_key, (self.size, members, self.rank)))
|
119 |
+
|
120 |
+
@property
|
121 |
+
def partition(self):
|
122 |
+
"""Return partition as a sorted list of lists.
|
123 |
+
|
124 |
+
Examples
|
125 |
+
========
|
126 |
+
|
127 |
+
>>> from sympy.combinatorics import Partition
|
128 |
+
>>> Partition([1], [2, 3]).partition
|
129 |
+
[[1], [2, 3]]
|
130 |
+
"""
|
131 |
+
if self._partition is None:
|
132 |
+
self._partition = sorted([sorted(p, key=default_sort_key)
|
133 |
+
for p in self.args])
|
134 |
+
return self._partition
|
135 |
+
|
136 |
+
def __add__(self, other):
|
137 |
+
"""
|
138 |
+
Return permutation whose rank is ``other`` greater than current rank,
|
139 |
+
(mod the maximum rank for the set).
|
140 |
+
|
141 |
+
Examples
|
142 |
+
========
|
143 |
+
|
144 |
+
>>> from sympy.combinatorics import Partition
|
145 |
+
>>> a = Partition([1, 2], [3])
|
146 |
+
>>> a.rank
|
147 |
+
1
|
148 |
+
>>> (a + 1).rank
|
149 |
+
2
|
150 |
+
>>> (a + 100).rank
|
151 |
+
1
|
152 |
+
"""
|
153 |
+
other = as_int(other)
|
154 |
+
offset = self.rank + other
|
155 |
+
result = RGS_unrank((offset) %
|
156 |
+
RGS_enum(self.size),
|
157 |
+
self.size)
|
158 |
+
return Partition.from_rgs(result, self.members)
|
159 |
+
|
160 |
+
def __sub__(self, other):
|
161 |
+
"""
|
162 |
+
Return permutation whose rank is ``other`` less than current rank,
|
163 |
+
(mod the maximum rank for the set).
|
164 |
+
|
165 |
+
Examples
|
166 |
+
========
|
167 |
+
|
168 |
+
>>> from sympy.combinatorics import Partition
|
169 |
+
>>> a = Partition([1, 2], [3])
|
170 |
+
>>> a.rank
|
171 |
+
1
|
172 |
+
>>> (a - 1).rank
|
173 |
+
0
|
174 |
+
>>> (a - 100).rank
|
175 |
+
1
|
176 |
+
"""
|
177 |
+
return self.__add__(-other)
|
178 |
+
|
179 |
+
def __le__(self, other):
|
180 |
+
"""
|
181 |
+
Checks if a partition is less than or equal to
|
182 |
+
the other based on rank.
|
183 |
+
|
184 |
+
Examples
|
185 |
+
========
|
186 |
+
|
187 |
+
>>> from sympy.combinatorics import Partition
|
188 |
+
>>> a = Partition([1, 2], [3, 4, 5])
|
189 |
+
>>> b = Partition([1], [2, 3], [4], [5])
|
190 |
+
>>> a.rank, b.rank
|
191 |
+
(9, 34)
|
192 |
+
>>> a <= a
|
193 |
+
True
|
194 |
+
>>> a <= b
|
195 |
+
True
|
196 |
+
"""
|
197 |
+
return self.sort_key() <= sympify(other).sort_key()
|
198 |
+
|
199 |
+
def __lt__(self, other):
|
200 |
+
"""
|
201 |
+
Checks if a partition is less than the other.
|
202 |
+
|
203 |
+
Examples
|
204 |
+
========
|
205 |
+
|
206 |
+
>>> from sympy.combinatorics import Partition
|
207 |
+
>>> a = Partition([1, 2], [3, 4, 5])
|
208 |
+
>>> b = Partition([1], [2, 3], [4], [5])
|
209 |
+
>>> a.rank, b.rank
|
210 |
+
(9, 34)
|
211 |
+
>>> a < b
|
212 |
+
True
|
213 |
+
"""
|
214 |
+
return self.sort_key() < sympify(other).sort_key()
|
215 |
+
|
216 |
+
@property
|
217 |
+
def rank(self):
|
218 |
+
"""
|
219 |
+
Gets the rank of a partition.
|
220 |
+
|
221 |
+
Examples
|
222 |
+
========
|
223 |
+
|
224 |
+
>>> from sympy.combinatorics import Partition
|
225 |
+
>>> a = Partition([1, 2], [3], [4, 5])
|
226 |
+
>>> a.rank
|
227 |
+
13
|
228 |
+
"""
|
229 |
+
if self._rank is not None:
|
230 |
+
return self._rank
|
231 |
+
self._rank = RGS_rank(self.RGS)
|
232 |
+
return self._rank
|
233 |
+
|
234 |
+
@property
|
235 |
+
def RGS(self):
|
236 |
+
"""
|
237 |
+
Returns the "restricted growth string" of the partition.
|
238 |
+
|
239 |
+
Explanation
|
240 |
+
===========
|
241 |
+
|
242 |
+
The RGS is returned as a list of indices, L, where L[i] indicates
|
243 |
+
the block in which element i appears. For example, in a partition
|
244 |
+
of 3 elements (a, b, c) into 2 blocks ([c], [a, b]) the RGS is
|
245 |
+
[1, 1, 0]: "a" is in block 1, "b" is in block 1 and "c" is in block 0.
|
246 |
+
|
247 |
+
Examples
|
248 |
+
========
|
249 |
+
|
250 |
+
>>> from sympy.combinatorics import Partition
|
251 |
+
>>> a = Partition([1, 2], [3], [4, 5])
|
252 |
+
>>> a.members
|
253 |
+
(1, 2, 3, 4, 5)
|
254 |
+
>>> a.RGS
|
255 |
+
(0, 0, 1, 2, 2)
|
256 |
+
>>> a + 1
|
257 |
+
Partition({3}, {4}, {5}, {1, 2})
|
258 |
+
>>> _.RGS
|
259 |
+
(0, 0, 1, 2, 3)
|
260 |
+
"""
|
261 |
+
rgs = {}
|
262 |
+
partition = self.partition
|
263 |
+
for i, part in enumerate(partition):
|
264 |
+
for j in part:
|
265 |
+
rgs[j] = i
|
266 |
+
return tuple([rgs[i] for i in sorted(
|
267 |
+
[i for p in partition for i in p], key=default_sort_key)])
|
268 |
+
|
269 |
+
@classmethod
|
270 |
+
def from_rgs(self, rgs, elements):
|
271 |
+
"""
|
272 |
+
Creates a set partition from a restricted growth string.
|
273 |
+
|
274 |
+
Explanation
|
275 |
+
===========
|
276 |
+
|
277 |
+
The indices given in rgs are assumed to be the index
|
278 |
+
of the element as given in elements *as provided* (the
|
279 |
+
elements are not sorted by this routine). Block numbering
|
280 |
+
starts from 0. If any block was not referenced in ``rgs``
|
281 |
+
an error will be raised.
|
282 |
+
|
283 |
+
Examples
|
284 |
+
========
|
285 |
+
|
286 |
+
>>> from sympy.combinatorics import Partition
|
287 |
+
>>> Partition.from_rgs([0, 1, 2, 0, 1], list('abcde'))
|
288 |
+
Partition({c}, {a, d}, {b, e})
|
289 |
+
>>> Partition.from_rgs([0, 1, 2, 0, 1], list('cbead'))
|
290 |
+
Partition({e}, {a, c}, {b, d})
|
291 |
+
>>> a = Partition([1, 4], [2], [3, 5])
|
292 |
+
>>> Partition.from_rgs(a.RGS, a.members)
|
293 |
+
Partition({2}, {1, 4}, {3, 5})
|
294 |
+
"""
|
295 |
+
if len(rgs) != len(elements):
|
296 |
+
raise ValueError('mismatch in rgs and element lengths')
|
297 |
+
max_elem = max(rgs) + 1
|
298 |
+
partition = [[] for i in range(max_elem)]
|
299 |
+
j = 0
|
300 |
+
for i in rgs:
|
301 |
+
partition[i].append(elements[j])
|
302 |
+
j += 1
|
303 |
+
if not all(p for p in partition):
|
304 |
+
raise ValueError('some blocks of the partition were empty.')
|
305 |
+
return Partition(*partition)
|
306 |
+
|
307 |
+
|
308 |
+
class IntegerPartition(Basic):
|
309 |
+
"""
|
310 |
+
This class represents an integer partition.
|
311 |
+
|
312 |
+
Explanation
|
313 |
+
===========
|
314 |
+
|
315 |
+
In number theory and combinatorics, a partition of a positive integer,
|
316 |
+
``n``, also called an integer partition, is a way of writing ``n`` as a
|
317 |
+
list of positive integers that sum to n. Two partitions that differ only
|
318 |
+
in the order of summands are considered to be the same partition; if order
|
319 |
+
matters then the partitions are referred to as compositions. For example,
|
320 |
+
4 has five partitions: [4], [3, 1], [2, 2], [2, 1, 1], and [1, 1, 1, 1];
|
321 |
+
the compositions [1, 2, 1] and [1, 1, 2] are the same as partition
|
322 |
+
[2, 1, 1].
|
323 |
+
|
324 |
+
See Also
|
325 |
+
========
|
326 |
+
|
327 |
+
sympy.utilities.iterables.partitions,
|
328 |
+
sympy.utilities.iterables.multiset_partitions
|
329 |
+
|
330 |
+
References
|
331 |
+
==========
|
332 |
+
|
333 |
+
.. [1] https://en.wikipedia.org/wiki/Partition_%28number_theory%29
|
334 |
+
"""
|
335 |
+
|
336 |
+
_dict = None
|
337 |
+
_keys = None
|
338 |
+
|
339 |
+
def __new__(cls, partition, integer=None):
|
340 |
+
"""
|
341 |
+
Generates a new IntegerPartition object from a list or dictionary.
|
342 |
+
|
343 |
+
Explanation
|
344 |
+
===========
|
345 |
+
|
346 |
+
The partition can be given as a list of positive integers or a
|
347 |
+
dictionary of (integer, multiplicity) items. If the partition is
|
348 |
+
preceded by an integer an error will be raised if the partition
|
349 |
+
does not sum to that given integer.
|
350 |
+
|
351 |
+
Examples
|
352 |
+
========
|
353 |
+
|
354 |
+
>>> from sympy.combinatorics.partitions import IntegerPartition
|
355 |
+
>>> a = IntegerPartition([5, 4, 3, 1, 1])
|
356 |
+
>>> a
|
357 |
+
IntegerPartition(14, (5, 4, 3, 1, 1))
|
358 |
+
>>> print(a)
|
359 |
+
[5, 4, 3, 1, 1]
|
360 |
+
>>> IntegerPartition({1:3, 2:1})
|
361 |
+
IntegerPartition(5, (2, 1, 1, 1))
|
362 |
+
|
363 |
+
If the value that the partition should sum to is given first, a check
|
364 |
+
will be made to see n error will be raised if there is a discrepancy:
|
365 |
+
|
366 |
+
>>> IntegerPartition(10, [5, 4, 3, 1])
|
367 |
+
Traceback (most recent call last):
|
368 |
+
...
|
369 |
+
ValueError: The partition is not valid
|
370 |
+
|
371 |
+
"""
|
372 |
+
if integer is not None:
|
373 |
+
integer, partition = partition, integer
|
374 |
+
if isinstance(partition, (dict, Dict)):
|
375 |
+
_ = []
|
376 |
+
for k, v in sorted(partition.items(), reverse=True):
|
377 |
+
if not v:
|
378 |
+
continue
|
379 |
+
k, v = as_int(k), as_int(v)
|
380 |
+
_.extend([k]*v)
|
381 |
+
partition = tuple(_)
|
382 |
+
else:
|
383 |
+
partition = tuple(sorted(map(as_int, partition), reverse=True))
|
384 |
+
sum_ok = False
|
385 |
+
if integer is None:
|
386 |
+
integer = sum(partition)
|
387 |
+
sum_ok = True
|
388 |
+
else:
|
389 |
+
integer = as_int(integer)
|
390 |
+
|
391 |
+
if not sum_ok and sum(partition) != integer:
|
392 |
+
raise ValueError("Partition did not add to %s" % integer)
|
393 |
+
if any(i < 1 for i in partition):
|
394 |
+
raise ValueError("All integer summands must be greater than one")
|
395 |
+
|
396 |
+
obj = Basic.__new__(cls, Integer(integer), Tuple(*partition))
|
397 |
+
obj.partition = list(partition)
|
398 |
+
obj.integer = integer
|
399 |
+
return obj
|
400 |
+
|
401 |
+
def prev_lex(self):
|
402 |
+
"""Return the previous partition of the integer, n, in lexical order,
|
403 |
+
wrapping around to [1, ..., 1] if the partition is [n].
|
404 |
+
|
405 |
+
Examples
|
406 |
+
========
|
407 |
+
|
408 |
+
>>> from sympy.combinatorics.partitions import IntegerPartition
|
409 |
+
>>> p = IntegerPartition([4])
|
410 |
+
>>> print(p.prev_lex())
|
411 |
+
[3, 1]
|
412 |
+
>>> p.partition > p.prev_lex().partition
|
413 |
+
True
|
414 |
+
"""
|
415 |
+
d = defaultdict(int)
|
416 |
+
d.update(self.as_dict())
|
417 |
+
keys = self._keys
|
418 |
+
if keys == [1]:
|
419 |
+
return IntegerPartition({self.integer: 1})
|
420 |
+
if keys[-1] != 1:
|
421 |
+
d[keys[-1]] -= 1
|
422 |
+
if keys[-1] == 2:
|
423 |
+
d[1] = 2
|
424 |
+
else:
|
425 |
+
d[keys[-1] - 1] = d[1] = 1
|
426 |
+
else:
|
427 |
+
d[keys[-2]] -= 1
|
428 |
+
left = d[1] + keys[-2]
|
429 |
+
new = keys[-2]
|
430 |
+
d[1] = 0
|
431 |
+
while left:
|
432 |
+
new -= 1
|
433 |
+
if left - new >= 0:
|
434 |
+
d[new] += left//new
|
435 |
+
left -= d[new]*new
|
436 |
+
return IntegerPartition(self.integer, d)
|
437 |
+
|
438 |
+
def next_lex(self):
|
439 |
+
"""Return the next partition of the integer, n, in lexical order,
|
440 |
+
wrapping around to [n] if the partition is [1, ..., 1].
|
441 |
+
|
442 |
+
Examples
|
443 |
+
========
|
444 |
+
|
445 |
+
>>> from sympy.combinatorics.partitions import IntegerPartition
|
446 |
+
>>> p = IntegerPartition([3, 1])
|
447 |
+
>>> print(p.next_lex())
|
448 |
+
[4]
|
449 |
+
>>> p.partition < p.next_lex().partition
|
450 |
+
True
|
451 |
+
"""
|
452 |
+
d = defaultdict(int)
|
453 |
+
d.update(self.as_dict())
|
454 |
+
key = self._keys
|
455 |
+
a = key[-1]
|
456 |
+
if a == self.integer:
|
457 |
+
d.clear()
|
458 |
+
d[1] = self.integer
|
459 |
+
elif a == 1:
|
460 |
+
if d[a] > 1:
|
461 |
+
d[a + 1] += 1
|
462 |
+
d[a] -= 2
|
463 |
+
else:
|
464 |
+
b = key[-2]
|
465 |
+
d[b + 1] += 1
|
466 |
+
d[1] = (d[b] - 1)*b
|
467 |
+
d[b] = 0
|
468 |
+
else:
|
469 |
+
if d[a] > 1:
|
470 |
+
if len(key) == 1:
|
471 |
+
d.clear()
|
472 |
+
d[a + 1] = 1
|
473 |
+
d[1] = self.integer - a - 1
|
474 |
+
else:
|
475 |
+
a1 = a + 1
|
476 |
+
d[a1] += 1
|
477 |
+
d[1] = d[a]*a - a1
|
478 |
+
d[a] = 0
|
479 |
+
else:
|
480 |
+
b = key[-2]
|
481 |
+
b1 = b + 1
|
482 |
+
d[b1] += 1
|
483 |
+
need = d[b]*b + d[a]*a - b1
|
484 |
+
d[a] = d[b] = 0
|
485 |
+
d[1] = need
|
486 |
+
return IntegerPartition(self.integer, d)
|
487 |
+
|
488 |
+
def as_dict(self):
|
489 |
+
"""Return the partition as a dictionary whose keys are the
|
490 |
+
partition integers and the values are the multiplicity of that
|
491 |
+
integer.
|
492 |
+
|
493 |
+
Examples
|
494 |
+
========
|
495 |
+
|
496 |
+
>>> from sympy.combinatorics.partitions import IntegerPartition
|
497 |
+
>>> IntegerPartition([1]*3 + [2] + [3]*4).as_dict()
|
498 |
+
{1: 3, 2: 1, 3: 4}
|
499 |
+
"""
|
500 |
+
if self._dict is None:
|
501 |
+
groups = group(self.partition, multiple=False)
|
502 |
+
self._keys = [g[0] for g in groups]
|
503 |
+
self._dict = dict(groups)
|
504 |
+
return self._dict
|
505 |
+
|
506 |
+
@property
|
507 |
+
def conjugate(self):
|
508 |
+
"""
|
509 |
+
Computes the conjugate partition of itself.
|
510 |
+
|
511 |
+
Examples
|
512 |
+
========
|
513 |
+
|
514 |
+
>>> from sympy.combinatorics.partitions import IntegerPartition
|
515 |
+
>>> a = IntegerPartition([6, 3, 3, 2, 1])
|
516 |
+
>>> a.conjugate
|
517 |
+
[5, 4, 3, 1, 1, 1]
|
518 |
+
"""
|
519 |
+
j = 1
|
520 |
+
temp_arr = list(self.partition) + [0]
|
521 |
+
k = temp_arr[0]
|
522 |
+
b = [0]*k
|
523 |
+
while k > 0:
|
524 |
+
while k > temp_arr[j]:
|
525 |
+
b[k - 1] = j
|
526 |
+
k -= 1
|
527 |
+
j += 1
|
528 |
+
return b
|
529 |
+
|
530 |
+
def __lt__(self, other):
|
531 |
+
"""Return True if self is less than other when the partition
|
532 |
+
is listed from smallest to biggest.
|
533 |
+
|
534 |
+
Examples
|
535 |
+
========
|
536 |
+
|
537 |
+
>>> from sympy.combinatorics.partitions import IntegerPartition
|
538 |
+
>>> a = IntegerPartition([3, 1])
|
539 |
+
>>> a < a
|
540 |
+
False
|
541 |
+
>>> b = a.next_lex()
|
542 |
+
>>> a < b
|
543 |
+
True
|
544 |
+
>>> a == b
|
545 |
+
False
|
546 |
+
"""
|
547 |
+
return list(reversed(self.partition)) < list(reversed(other.partition))
|
548 |
+
|
549 |
+
def __le__(self, other):
|
550 |
+
"""Return True if self is less than other when the partition
|
551 |
+
is listed from smallest to biggest.
|
552 |
+
|
553 |
+
Examples
|
554 |
+
========
|
555 |
+
|
556 |
+
>>> from sympy.combinatorics.partitions import IntegerPartition
|
557 |
+
>>> a = IntegerPartition([4])
|
558 |
+
>>> a <= a
|
559 |
+
True
|
560 |
+
"""
|
561 |
+
return list(reversed(self.partition)) <= list(reversed(other.partition))
|
562 |
+
|
563 |
+
def as_ferrers(self, char='#'):
|
564 |
+
"""
|
565 |
+
Prints the ferrer diagram of a partition.
|
566 |
+
|
567 |
+
Examples
|
568 |
+
========
|
569 |
+
|
570 |
+
>>> from sympy.combinatorics.partitions import IntegerPartition
|
571 |
+
>>> print(IntegerPartition([1, 1, 5]).as_ferrers())
|
572 |
+
#####
|
573 |
+
#
|
574 |
+
#
|
575 |
+
"""
|
576 |
+
return "\n".join([char*i for i in self.partition])
|
577 |
+
|
578 |
+
def __str__(self):
|
579 |
+
return str(list(self.partition))
|
580 |
+
|
581 |
+
|
582 |
+
def random_integer_partition(n, seed=None):
|
583 |
+
"""
|
584 |
+
Generates a random integer partition summing to ``n`` as a list
|
585 |
+
of reverse-sorted integers.
|
586 |
+
|
587 |
+
Examples
|
588 |
+
========
|
589 |
+
|
590 |
+
>>> from sympy.combinatorics.partitions import random_integer_partition
|
591 |
+
|
592 |
+
For the following, a seed is given so a known value can be shown; in
|
593 |
+
practice, the seed would not be given.
|
594 |
+
|
595 |
+
>>> random_integer_partition(100, seed=[1, 1, 12, 1, 2, 1, 85, 1])
|
596 |
+
[85, 12, 2, 1]
|
597 |
+
>>> random_integer_partition(10, seed=[1, 2, 3, 1, 5, 1])
|
598 |
+
[5, 3, 1, 1]
|
599 |
+
>>> random_integer_partition(1)
|
600 |
+
[1]
|
601 |
+
"""
|
602 |
+
from sympy.core.random import _randint
|
603 |
+
|
604 |
+
n = as_int(n)
|
605 |
+
if n < 1:
|
606 |
+
raise ValueError('n must be a positive integer')
|
607 |
+
|
608 |
+
randint = _randint(seed)
|
609 |
+
|
610 |
+
partition = []
|
611 |
+
while (n > 0):
|
612 |
+
k = randint(1, n)
|
613 |
+
mult = randint(1, n//k)
|
614 |
+
partition.append((k, mult))
|
615 |
+
n -= k*mult
|
616 |
+
partition.sort(reverse=True)
|
617 |
+
partition = flatten([[k]*m for k, m in partition])
|
618 |
+
return partition
|
619 |
+
|
620 |
+
|
621 |
+
def RGS_generalized(m):
|
622 |
+
"""
|
623 |
+
Computes the m + 1 generalized unrestricted growth strings
|
624 |
+
and returns them as rows in matrix.
|
625 |
+
|
626 |
+
Examples
|
627 |
+
========
|
628 |
+
|
629 |
+
>>> from sympy.combinatorics.partitions import RGS_generalized
|
630 |
+
>>> RGS_generalized(6)
|
631 |
+
Matrix([
|
632 |
+
[ 1, 1, 1, 1, 1, 1, 1],
|
633 |
+
[ 1, 2, 3, 4, 5, 6, 0],
|
634 |
+
[ 2, 5, 10, 17, 26, 0, 0],
|
635 |
+
[ 5, 15, 37, 77, 0, 0, 0],
|
636 |
+
[ 15, 52, 151, 0, 0, 0, 0],
|
637 |
+
[ 52, 203, 0, 0, 0, 0, 0],
|
638 |
+
[203, 0, 0, 0, 0, 0, 0]])
|
639 |
+
"""
|
640 |
+
d = zeros(m + 1)
|
641 |
+
for i in range(m + 1):
|
642 |
+
d[0, i] = 1
|
643 |
+
|
644 |
+
for i in range(1, m + 1):
|
645 |
+
for j in range(m):
|
646 |
+
if j <= m - i:
|
647 |
+
d[i, j] = j * d[i - 1, j] + d[i - 1, j + 1]
|
648 |
+
else:
|
649 |
+
d[i, j] = 0
|
650 |
+
return d
|
651 |
+
|
652 |
+
|
653 |
+
def RGS_enum(m):
|
654 |
+
"""
|
655 |
+
RGS_enum computes the total number of restricted growth strings
|
656 |
+
possible for a superset of size m.
|
657 |
+
|
658 |
+
Examples
|
659 |
+
========
|
660 |
+
|
661 |
+
>>> from sympy.combinatorics.partitions import RGS_enum
|
662 |
+
>>> from sympy.combinatorics import Partition
|
663 |
+
>>> RGS_enum(4)
|
664 |
+
15
|
665 |
+
>>> RGS_enum(5)
|
666 |
+
52
|
667 |
+
>>> RGS_enum(6)
|
668 |
+
203
|
669 |
+
|
670 |
+
We can check that the enumeration is correct by actually generating
|
671 |
+
the partitions. Here, the 15 partitions of 4 items are generated:
|
672 |
+
|
673 |
+
>>> a = Partition(list(range(4)))
|
674 |
+
>>> s = set()
|
675 |
+
>>> for i in range(20):
|
676 |
+
... s.add(a)
|
677 |
+
... a += 1
|
678 |
+
...
|
679 |
+
>>> assert len(s) == 15
|
680 |
+
|
681 |
+
"""
|
682 |
+
if (m < 1):
|
683 |
+
return 0
|
684 |
+
elif (m == 1):
|
685 |
+
return 1
|
686 |
+
else:
|
687 |
+
return bell(m)
|
688 |
+
|
689 |
+
|
690 |
+
def RGS_unrank(rank, m):
|
691 |
+
"""
|
692 |
+
Gives the unranked restricted growth string for a given
|
693 |
+
superset size.
|
694 |
+
|
695 |
+
Examples
|
696 |
+
========
|
697 |
+
|
698 |
+
>>> from sympy.combinatorics.partitions import RGS_unrank
|
699 |
+
>>> RGS_unrank(14, 4)
|
700 |
+
[0, 1, 2, 3]
|
701 |
+
>>> RGS_unrank(0, 4)
|
702 |
+
[0, 0, 0, 0]
|
703 |
+
"""
|
704 |
+
if m < 1:
|
705 |
+
raise ValueError("The superset size must be >= 1")
|
706 |
+
if rank < 0 or RGS_enum(m) <= rank:
|
707 |
+
raise ValueError("Invalid arguments")
|
708 |
+
|
709 |
+
L = [1] * (m + 1)
|
710 |
+
j = 1
|
711 |
+
D = RGS_generalized(m)
|
712 |
+
for i in range(2, m + 1):
|
713 |
+
v = D[m - i, j]
|
714 |
+
cr = j*v
|
715 |
+
if cr <= rank:
|
716 |
+
L[i] = j + 1
|
717 |
+
rank -= cr
|
718 |
+
j += 1
|
719 |
+
else:
|
720 |
+
L[i] = int(rank / v + 1)
|
721 |
+
rank %= v
|
722 |
+
return [x - 1 for x in L[1:]]
|
723 |
+
|
724 |
+
|
725 |
+
def RGS_rank(rgs):
|
726 |
+
"""
|
727 |
+
Computes the rank of a restricted growth string.
|
728 |
+
|
729 |
+
Examples
|
730 |
+
========
|
731 |
+
|
732 |
+
>>> from sympy.combinatorics.partitions import RGS_rank, RGS_unrank
|
733 |
+
>>> RGS_rank([0, 1, 2, 1, 3])
|
734 |
+
42
|
735 |
+
>>> RGS_rank(RGS_unrank(4, 7))
|
736 |
+
4
|
737 |
+
"""
|
738 |
+
rgs_size = len(rgs)
|
739 |
+
rank = 0
|
740 |
+
D = RGS_generalized(rgs_size)
|
741 |
+
for i in range(1, rgs_size):
|
742 |
+
n = len(rgs[(i + 1):])
|
743 |
+
m = max(rgs[0:i])
|
744 |
+
rank += D[n, m + 1] * rgs[i]
|
745 |
+
return rank
|
venv/lib/python3.10/site-packages/sympy/combinatorics/pc_groups.py
ADDED
@@ -0,0 +1,709 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.ntheory.primetest import isprime
|
2 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
3 |
+
from sympy.printing.defaults import DefaultPrinting
|
4 |
+
from sympy.combinatorics.free_groups import free_group
|
5 |
+
|
6 |
+
|
7 |
+
class PolycyclicGroup(DefaultPrinting):
|
8 |
+
|
9 |
+
is_group = True
|
10 |
+
is_solvable = True
|
11 |
+
|
12 |
+
def __init__(self, pc_sequence, pc_series, relative_order, collector=None):
|
13 |
+
"""
|
14 |
+
|
15 |
+
Parameters
|
16 |
+
==========
|
17 |
+
|
18 |
+
pc_sequence : list
|
19 |
+
A sequence of elements whose classes generate the cyclic factor
|
20 |
+
groups of pc_series.
|
21 |
+
pc_series : list
|
22 |
+
A subnormal sequence of subgroups where each factor group is cyclic.
|
23 |
+
relative_order : list
|
24 |
+
The orders of factor groups of pc_series.
|
25 |
+
collector : Collector
|
26 |
+
By default, it is None. Collector class provides the
|
27 |
+
polycyclic presentation with various other functionalities.
|
28 |
+
|
29 |
+
"""
|
30 |
+
self.pcgs = pc_sequence
|
31 |
+
self.pc_series = pc_series
|
32 |
+
self.relative_order = relative_order
|
33 |
+
self.collector = Collector(self.pcgs, pc_series, relative_order) if not collector else collector
|
34 |
+
|
35 |
+
def is_prime_order(self):
|
36 |
+
return all(isprime(order) for order in self.relative_order)
|
37 |
+
|
38 |
+
def length(self):
|
39 |
+
return len(self.pcgs)
|
40 |
+
|
41 |
+
|
42 |
+
class Collector(DefaultPrinting):
|
43 |
+
|
44 |
+
"""
|
45 |
+
References
|
46 |
+
==========
|
47 |
+
|
48 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.
|
49 |
+
"Handbook of Computational Group Theory"
|
50 |
+
Section 8.1.3
|
51 |
+
"""
|
52 |
+
|
53 |
+
def __init__(self, pcgs, pc_series, relative_order, free_group_=None, pc_presentation=None):
|
54 |
+
"""
|
55 |
+
|
56 |
+
Most of the parameters for the Collector class are the same as for PolycyclicGroup.
|
57 |
+
Others are described below.
|
58 |
+
|
59 |
+
Parameters
|
60 |
+
==========
|
61 |
+
|
62 |
+
free_group_ : tuple
|
63 |
+
free_group_ provides the mapping of polycyclic generating
|
64 |
+
sequence with the free group elements.
|
65 |
+
pc_presentation : dict
|
66 |
+
Provides the presentation of polycyclic groups with the
|
67 |
+
help of power and conjugate relators.
|
68 |
+
|
69 |
+
See Also
|
70 |
+
========
|
71 |
+
|
72 |
+
PolycyclicGroup
|
73 |
+
|
74 |
+
"""
|
75 |
+
self.pcgs = pcgs
|
76 |
+
self.pc_series = pc_series
|
77 |
+
self.relative_order = relative_order
|
78 |
+
self.free_group = free_group('x:{}'.format(len(pcgs)))[0] if not free_group_ else free_group_
|
79 |
+
self.index = {s: i for i, s in enumerate(self.free_group.symbols)}
|
80 |
+
self.pc_presentation = self.pc_relators()
|
81 |
+
|
82 |
+
def minimal_uncollected_subword(self, word):
|
83 |
+
r"""
|
84 |
+
Returns the minimal uncollected subwords.
|
85 |
+
|
86 |
+
Explanation
|
87 |
+
===========
|
88 |
+
|
89 |
+
A word ``v`` defined on generators in ``X`` is a minimal
|
90 |
+
uncollected subword of the word ``w`` if ``v`` is a subword
|
91 |
+
of ``w`` and it has one of the following form
|
92 |
+
|
93 |
+
* `v = {x_{i+1}}^{a_j}x_i`
|
94 |
+
|
95 |
+
* `v = {x_{i+1}}^{a_j}{x_i}^{-1}`
|
96 |
+
|
97 |
+
* `v = {x_i}^{a_j}`
|
98 |
+
|
99 |
+
for `a_j` not in `\{1, \ldots, s-1\}`. Where, ``s`` is the power
|
100 |
+
exponent of the corresponding generator.
|
101 |
+
|
102 |
+
Examples
|
103 |
+
========
|
104 |
+
|
105 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
106 |
+
>>> from sympy.combinatorics import free_group
|
107 |
+
>>> G = SymmetricGroup(4)
|
108 |
+
>>> PcGroup = G.polycyclic_group()
|
109 |
+
>>> collector = PcGroup.collector
|
110 |
+
>>> F, x1, x2 = free_group("x1, x2")
|
111 |
+
>>> word = x2**2*x1**7
|
112 |
+
>>> collector.minimal_uncollected_subword(word)
|
113 |
+
((x2, 2),)
|
114 |
+
|
115 |
+
"""
|
116 |
+
# To handle the case word = <identity>
|
117 |
+
if not word:
|
118 |
+
return None
|
119 |
+
|
120 |
+
array = word.array_form
|
121 |
+
re = self.relative_order
|
122 |
+
index = self.index
|
123 |
+
|
124 |
+
for i in range(len(array)):
|
125 |
+
s1, e1 = array[i]
|
126 |
+
|
127 |
+
if re[index[s1]] and (e1 < 0 or e1 > re[index[s1]]-1):
|
128 |
+
return ((s1, e1), )
|
129 |
+
|
130 |
+
for i in range(len(array)-1):
|
131 |
+
s1, e1 = array[i]
|
132 |
+
s2, e2 = array[i+1]
|
133 |
+
|
134 |
+
if index[s1] > index[s2]:
|
135 |
+
e = 1 if e2 > 0 else -1
|
136 |
+
return ((s1, e1), (s2, e))
|
137 |
+
|
138 |
+
return None
|
139 |
+
|
140 |
+
def relations(self):
|
141 |
+
"""
|
142 |
+
Separates the given relators of pc presentation in power and
|
143 |
+
conjugate relations.
|
144 |
+
|
145 |
+
Returns
|
146 |
+
=======
|
147 |
+
|
148 |
+
(power_rel, conj_rel)
|
149 |
+
Separates pc presentation into power and conjugate relations.
|
150 |
+
|
151 |
+
Examples
|
152 |
+
========
|
153 |
+
|
154 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
155 |
+
>>> G = SymmetricGroup(3)
|
156 |
+
>>> PcGroup = G.polycyclic_group()
|
157 |
+
>>> collector = PcGroup.collector
|
158 |
+
>>> power_rel, conj_rel = collector.relations()
|
159 |
+
>>> power_rel
|
160 |
+
{x0**2: (), x1**3: ()}
|
161 |
+
>>> conj_rel
|
162 |
+
{x0**-1*x1*x0: x1**2}
|
163 |
+
|
164 |
+
See Also
|
165 |
+
========
|
166 |
+
|
167 |
+
pc_relators
|
168 |
+
|
169 |
+
"""
|
170 |
+
power_relators = {}
|
171 |
+
conjugate_relators = {}
|
172 |
+
for key, value in self.pc_presentation.items():
|
173 |
+
if len(key.array_form) == 1:
|
174 |
+
power_relators[key] = value
|
175 |
+
else:
|
176 |
+
conjugate_relators[key] = value
|
177 |
+
return power_relators, conjugate_relators
|
178 |
+
|
179 |
+
def subword_index(self, word, w):
|
180 |
+
"""
|
181 |
+
Returns the start and ending index of a given
|
182 |
+
subword in a word.
|
183 |
+
|
184 |
+
Parameters
|
185 |
+
==========
|
186 |
+
|
187 |
+
word : FreeGroupElement
|
188 |
+
word defined on free group elements for a
|
189 |
+
polycyclic group.
|
190 |
+
w : FreeGroupElement
|
191 |
+
subword of a given word, whose starting and
|
192 |
+
ending index to be computed.
|
193 |
+
|
194 |
+
Returns
|
195 |
+
=======
|
196 |
+
|
197 |
+
(i, j)
|
198 |
+
A tuple containing starting and ending index of ``w``
|
199 |
+
in the given word.
|
200 |
+
|
201 |
+
Examples
|
202 |
+
========
|
203 |
+
|
204 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
205 |
+
>>> from sympy.combinatorics import free_group
|
206 |
+
>>> G = SymmetricGroup(4)
|
207 |
+
>>> PcGroup = G.polycyclic_group()
|
208 |
+
>>> collector = PcGroup.collector
|
209 |
+
>>> F, x1, x2 = free_group("x1, x2")
|
210 |
+
>>> word = x2**2*x1**7
|
211 |
+
>>> w = x2**2*x1
|
212 |
+
>>> collector.subword_index(word, w)
|
213 |
+
(0, 3)
|
214 |
+
>>> w = x1**7
|
215 |
+
>>> collector.subword_index(word, w)
|
216 |
+
(2, 9)
|
217 |
+
|
218 |
+
"""
|
219 |
+
low = -1
|
220 |
+
high = -1
|
221 |
+
for i in range(len(word)-len(w)+1):
|
222 |
+
if word.subword(i, i+len(w)) == w:
|
223 |
+
low = i
|
224 |
+
high = i+len(w)
|
225 |
+
break
|
226 |
+
if low == high == -1:
|
227 |
+
return -1, -1
|
228 |
+
return low, high
|
229 |
+
|
230 |
+
def map_relation(self, w):
|
231 |
+
"""
|
232 |
+
Return a conjugate relation.
|
233 |
+
|
234 |
+
Explanation
|
235 |
+
===========
|
236 |
+
|
237 |
+
Given a word formed by two free group elements, the
|
238 |
+
corresponding conjugate relation with those free
|
239 |
+
group elements is formed and mapped with the collected
|
240 |
+
word in the polycyclic presentation.
|
241 |
+
|
242 |
+
Examples
|
243 |
+
========
|
244 |
+
|
245 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
246 |
+
>>> from sympy.combinatorics import free_group
|
247 |
+
>>> G = SymmetricGroup(3)
|
248 |
+
>>> PcGroup = G.polycyclic_group()
|
249 |
+
>>> collector = PcGroup.collector
|
250 |
+
>>> F, x0, x1 = free_group("x0, x1")
|
251 |
+
>>> w = x1*x0
|
252 |
+
>>> collector.map_relation(w)
|
253 |
+
x1**2
|
254 |
+
|
255 |
+
See Also
|
256 |
+
========
|
257 |
+
|
258 |
+
pc_presentation
|
259 |
+
|
260 |
+
"""
|
261 |
+
array = w.array_form
|
262 |
+
s1 = array[0][0]
|
263 |
+
s2 = array[1][0]
|
264 |
+
key = ((s2, -1), (s1, 1), (s2, 1))
|
265 |
+
key = self.free_group.dtype(key)
|
266 |
+
return self.pc_presentation[key]
|
267 |
+
|
268 |
+
|
269 |
+
def collected_word(self, word):
|
270 |
+
r"""
|
271 |
+
Return the collected form of a word.
|
272 |
+
|
273 |
+
Explanation
|
274 |
+
===========
|
275 |
+
|
276 |
+
A word ``w`` is called collected, if `w = {x_{i_1}}^{a_1} * \ldots *
|
277 |
+
{x_{i_r}}^{a_r}` with `i_1 < i_2< \ldots < i_r` and `a_j` is in
|
278 |
+
`\{1, \ldots, {s_j}-1\}`.
|
279 |
+
|
280 |
+
Otherwise w is uncollected.
|
281 |
+
|
282 |
+
Parameters
|
283 |
+
==========
|
284 |
+
|
285 |
+
word : FreeGroupElement
|
286 |
+
An uncollected word.
|
287 |
+
|
288 |
+
Returns
|
289 |
+
=======
|
290 |
+
|
291 |
+
word
|
292 |
+
A collected word of form `w = {x_{i_1}}^{a_1}, \ldots,
|
293 |
+
{x_{i_r}}^{a_r}` with `i_1, i_2, \ldots, i_r` and `a_j \in
|
294 |
+
\{1, \ldots, {s_j}-1\}`.
|
295 |
+
|
296 |
+
Examples
|
297 |
+
========
|
298 |
+
|
299 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
300 |
+
>>> from sympy.combinatorics.perm_groups import PermutationGroup
|
301 |
+
>>> from sympy.combinatorics import free_group
|
302 |
+
>>> G = SymmetricGroup(4)
|
303 |
+
>>> PcGroup = G.polycyclic_group()
|
304 |
+
>>> collector = PcGroup.collector
|
305 |
+
>>> F, x0, x1, x2, x3 = free_group("x0, x1, x2, x3")
|
306 |
+
>>> word = x3*x2*x1*x0
|
307 |
+
>>> collected_word = collector.collected_word(word)
|
308 |
+
>>> free_to_perm = {}
|
309 |
+
>>> free_group = collector.free_group
|
310 |
+
>>> for sym, gen in zip(free_group.symbols, collector.pcgs):
|
311 |
+
... free_to_perm[sym] = gen
|
312 |
+
>>> G1 = PermutationGroup()
|
313 |
+
>>> for w in word:
|
314 |
+
... sym = w[0]
|
315 |
+
... perm = free_to_perm[sym]
|
316 |
+
... G1 = PermutationGroup([perm] + G1.generators)
|
317 |
+
>>> G2 = PermutationGroup()
|
318 |
+
>>> for w in collected_word:
|
319 |
+
... sym = w[0]
|
320 |
+
... perm = free_to_perm[sym]
|
321 |
+
... G2 = PermutationGroup([perm] + G2.generators)
|
322 |
+
|
323 |
+
The two are not identical, but they are equivalent:
|
324 |
+
|
325 |
+
>>> G1.equals(G2), G1 == G2
|
326 |
+
(True, False)
|
327 |
+
|
328 |
+
See Also
|
329 |
+
========
|
330 |
+
|
331 |
+
minimal_uncollected_subword
|
332 |
+
|
333 |
+
"""
|
334 |
+
free_group = self.free_group
|
335 |
+
while True:
|
336 |
+
w = self.minimal_uncollected_subword(word)
|
337 |
+
if not w:
|
338 |
+
break
|
339 |
+
|
340 |
+
low, high = self.subword_index(word, free_group.dtype(w))
|
341 |
+
if low == -1:
|
342 |
+
continue
|
343 |
+
|
344 |
+
s1, e1 = w[0]
|
345 |
+
if len(w) == 1:
|
346 |
+
re = self.relative_order[self.index[s1]]
|
347 |
+
q = e1 // re
|
348 |
+
r = e1-q*re
|
349 |
+
|
350 |
+
key = ((w[0][0], re), )
|
351 |
+
key = free_group.dtype(key)
|
352 |
+
if self.pc_presentation[key]:
|
353 |
+
presentation = self.pc_presentation[key].array_form
|
354 |
+
sym, exp = presentation[0]
|
355 |
+
word_ = ((w[0][0], r), (sym, q*exp))
|
356 |
+
word_ = free_group.dtype(word_)
|
357 |
+
else:
|
358 |
+
if r != 0:
|
359 |
+
word_ = ((w[0][0], r), )
|
360 |
+
word_ = free_group.dtype(word_)
|
361 |
+
else:
|
362 |
+
word_ = None
|
363 |
+
word = word.eliminate_word(free_group.dtype(w), word_)
|
364 |
+
|
365 |
+
if len(w) == 2 and w[1][1] > 0:
|
366 |
+
s2, e2 = w[1]
|
367 |
+
s2 = ((s2, 1), )
|
368 |
+
s2 = free_group.dtype(s2)
|
369 |
+
word_ = self.map_relation(free_group.dtype(w))
|
370 |
+
word_ = s2*word_**e1
|
371 |
+
word_ = free_group.dtype(word_)
|
372 |
+
word = word.substituted_word(low, high, word_)
|
373 |
+
|
374 |
+
elif len(w) == 2 and w[1][1] < 0:
|
375 |
+
s2, e2 = w[1]
|
376 |
+
s2 = ((s2, 1), )
|
377 |
+
s2 = free_group.dtype(s2)
|
378 |
+
word_ = self.map_relation(free_group.dtype(w))
|
379 |
+
word_ = s2**-1*word_**e1
|
380 |
+
word_ = free_group.dtype(word_)
|
381 |
+
word = word.substituted_word(low, high, word_)
|
382 |
+
|
383 |
+
return word
|
384 |
+
|
385 |
+
|
386 |
+
def pc_relators(self):
|
387 |
+
r"""
|
388 |
+
Return the polycyclic presentation.
|
389 |
+
|
390 |
+
Explanation
|
391 |
+
===========
|
392 |
+
|
393 |
+
There are two types of relations used in polycyclic
|
394 |
+
presentation.
|
395 |
+
|
396 |
+
* Power relations : Power relators are of the form `x_i^{re_i}`,
|
397 |
+
where `i \in \{0, \ldots, \mathrm{len(pcgs)}\}`, ``x`` represents polycyclic
|
398 |
+
generator and ``re`` is the corresponding relative order.
|
399 |
+
|
400 |
+
* Conjugate relations : Conjugate relators are of the form `x_j^-1x_ix_j`,
|
401 |
+
where `j < i \in \{0, \ldots, \mathrm{len(pcgs)}\}`.
|
402 |
+
|
403 |
+
Returns
|
404 |
+
=======
|
405 |
+
|
406 |
+
A dictionary with power and conjugate relations as key and
|
407 |
+
their collected form as corresponding values.
|
408 |
+
|
409 |
+
Notes
|
410 |
+
=====
|
411 |
+
|
412 |
+
Identity Permutation is mapped with empty ``()``.
|
413 |
+
|
414 |
+
Examples
|
415 |
+
========
|
416 |
+
|
417 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
418 |
+
>>> from sympy.combinatorics.permutations import Permutation
|
419 |
+
>>> S = SymmetricGroup(49).sylow_subgroup(7)
|
420 |
+
>>> der = S.derived_series()
|
421 |
+
>>> G = der[len(der)-2]
|
422 |
+
>>> PcGroup = G.polycyclic_group()
|
423 |
+
>>> collector = PcGroup.collector
|
424 |
+
>>> pcgs = PcGroup.pcgs
|
425 |
+
>>> len(pcgs)
|
426 |
+
6
|
427 |
+
>>> free_group = collector.free_group
|
428 |
+
>>> pc_resentation = collector.pc_presentation
|
429 |
+
>>> free_to_perm = {}
|
430 |
+
>>> for s, g in zip(free_group.symbols, pcgs):
|
431 |
+
... free_to_perm[s] = g
|
432 |
+
|
433 |
+
>>> for k, v in pc_resentation.items():
|
434 |
+
... k_array = k.array_form
|
435 |
+
... if v != ():
|
436 |
+
... v_array = v.array_form
|
437 |
+
... lhs = Permutation()
|
438 |
+
... for gen in k_array:
|
439 |
+
... s = gen[0]
|
440 |
+
... e = gen[1]
|
441 |
+
... lhs = lhs*free_to_perm[s]**e
|
442 |
+
... if v == ():
|
443 |
+
... assert lhs.is_identity
|
444 |
+
... continue
|
445 |
+
... rhs = Permutation()
|
446 |
+
... for gen in v_array:
|
447 |
+
... s = gen[0]
|
448 |
+
... e = gen[1]
|
449 |
+
... rhs = rhs*free_to_perm[s]**e
|
450 |
+
... assert lhs == rhs
|
451 |
+
|
452 |
+
"""
|
453 |
+
free_group = self.free_group
|
454 |
+
rel_order = self.relative_order
|
455 |
+
pc_relators = {}
|
456 |
+
perm_to_free = {}
|
457 |
+
pcgs = self.pcgs
|
458 |
+
|
459 |
+
for gen, s in zip(pcgs, free_group.generators):
|
460 |
+
perm_to_free[gen**-1] = s**-1
|
461 |
+
perm_to_free[gen] = s
|
462 |
+
|
463 |
+
pcgs = pcgs[::-1]
|
464 |
+
series = self.pc_series[::-1]
|
465 |
+
rel_order = rel_order[::-1]
|
466 |
+
collected_gens = []
|
467 |
+
|
468 |
+
for i, gen in enumerate(pcgs):
|
469 |
+
re = rel_order[i]
|
470 |
+
relation = perm_to_free[gen]**re
|
471 |
+
G = series[i]
|
472 |
+
|
473 |
+
l = G.generator_product(gen**re, original = True)
|
474 |
+
l.reverse()
|
475 |
+
|
476 |
+
word = free_group.identity
|
477 |
+
for g in l:
|
478 |
+
word = word*perm_to_free[g]
|
479 |
+
|
480 |
+
word = self.collected_word(word)
|
481 |
+
pc_relators[relation] = word if word else ()
|
482 |
+
self.pc_presentation = pc_relators
|
483 |
+
|
484 |
+
collected_gens.append(gen)
|
485 |
+
if len(collected_gens) > 1:
|
486 |
+
conj = collected_gens[len(collected_gens)-1]
|
487 |
+
conjugator = perm_to_free[conj]
|
488 |
+
|
489 |
+
for j in range(len(collected_gens)-1):
|
490 |
+
conjugated = perm_to_free[collected_gens[j]]
|
491 |
+
|
492 |
+
relation = conjugator**-1*conjugated*conjugator
|
493 |
+
gens = conj**-1*collected_gens[j]*conj
|
494 |
+
|
495 |
+
l = G.generator_product(gens, original = True)
|
496 |
+
l.reverse()
|
497 |
+
word = free_group.identity
|
498 |
+
for g in l:
|
499 |
+
word = word*perm_to_free[g]
|
500 |
+
|
501 |
+
word = self.collected_word(word)
|
502 |
+
pc_relators[relation] = word if word else ()
|
503 |
+
self.pc_presentation = pc_relators
|
504 |
+
|
505 |
+
return pc_relators
|
506 |
+
|
507 |
+
def exponent_vector(self, element):
|
508 |
+
r"""
|
509 |
+
Return the exponent vector of length equal to the
|
510 |
+
length of polycyclic generating sequence.
|
511 |
+
|
512 |
+
Explanation
|
513 |
+
===========
|
514 |
+
|
515 |
+
For a given generator/element ``g`` of the polycyclic group,
|
516 |
+
it can be represented as `g = {x_1}^{e_1}, \ldots, {x_n}^{e_n}`,
|
517 |
+
where `x_i` represents polycyclic generators and ``n`` is
|
518 |
+
the number of generators in the free_group equal to the length
|
519 |
+
of pcgs.
|
520 |
+
|
521 |
+
Parameters
|
522 |
+
==========
|
523 |
+
|
524 |
+
element : Permutation
|
525 |
+
Generator of a polycyclic group.
|
526 |
+
|
527 |
+
Examples
|
528 |
+
========
|
529 |
+
|
530 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
531 |
+
>>> from sympy.combinatorics.permutations import Permutation
|
532 |
+
>>> G = SymmetricGroup(4)
|
533 |
+
>>> PcGroup = G.polycyclic_group()
|
534 |
+
>>> collector = PcGroup.collector
|
535 |
+
>>> pcgs = PcGroup.pcgs
|
536 |
+
>>> collector.exponent_vector(G[0])
|
537 |
+
[1, 0, 0, 0]
|
538 |
+
>>> exp = collector.exponent_vector(G[1])
|
539 |
+
>>> g = Permutation()
|
540 |
+
>>> for i in range(len(exp)):
|
541 |
+
... g = g*pcgs[i]**exp[i] if exp[i] else g
|
542 |
+
>>> assert g == G[1]
|
543 |
+
|
544 |
+
References
|
545 |
+
==========
|
546 |
+
|
547 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.
|
548 |
+
"Handbook of Computational Group Theory"
|
549 |
+
Section 8.1.1, Definition 8.4
|
550 |
+
|
551 |
+
"""
|
552 |
+
free_group = self.free_group
|
553 |
+
G = PermutationGroup()
|
554 |
+
for g in self.pcgs:
|
555 |
+
G = PermutationGroup([g] + G.generators)
|
556 |
+
gens = G.generator_product(element, original = True)
|
557 |
+
gens.reverse()
|
558 |
+
|
559 |
+
perm_to_free = {}
|
560 |
+
for sym, g in zip(free_group.generators, self.pcgs):
|
561 |
+
perm_to_free[g**-1] = sym**-1
|
562 |
+
perm_to_free[g] = sym
|
563 |
+
w = free_group.identity
|
564 |
+
for g in gens:
|
565 |
+
w = w*perm_to_free[g]
|
566 |
+
|
567 |
+
word = self.collected_word(w)
|
568 |
+
|
569 |
+
index = self.index
|
570 |
+
exp_vector = [0]*len(free_group)
|
571 |
+
word = word.array_form
|
572 |
+
for t in word:
|
573 |
+
exp_vector[index[t[0]]] = t[1]
|
574 |
+
return exp_vector
|
575 |
+
|
576 |
+
def depth(self, element):
|
577 |
+
r"""
|
578 |
+
Return the depth of a given element.
|
579 |
+
|
580 |
+
Explanation
|
581 |
+
===========
|
582 |
+
|
583 |
+
The depth of a given element ``g`` is defined by
|
584 |
+
`\mathrm{dep}[g] = i` if `e_1 = e_2 = \ldots = e_{i-1} = 0`
|
585 |
+
and `e_i != 0`, where ``e`` represents the exponent-vector.
|
586 |
+
|
587 |
+
Examples
|
588 |
+
========
|
589 |
+
|
590 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
591 |
+
>>> G = SymmetricGroup(3)
|
592 |
+
>>> PcGroup = G.polycyclic_group()
|
593 |
+
>>> collector = PcGroup.collector
|
594 |
+
>>> collector.depth(G[0])
|
595 |
+
2
|
596 |
+
>>> collector.depth(G[1])
|
597 |
+
1
|
598 |
+
|
599 |
+
References
|
600 |
+
==========
|
601 |
+
|
602 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.
|
603 |
+
"Handbook of Computational Group Theory"
|
604 |
+
Section 8.1.1, Definition 8.5
|
605 |
+
|
606 |
+
"""
|
607 |
+
exp_vector = self.exponent_vector(element)
|
608 |
+
return next((i+1 for i, x in enumerate(exp_vector) if x), len(self.pcgs)+1)
|
609 |
+
|
610 |
+
def leading_exponent(self, element):
|
611 |
+
r"""
|
612 |
+
Return the leading non-zero exponent.
|
613 |
+
|
614 |
+
Explanation
|
615 |
+
===========
|
616 |
+
|
617 |
+
The leading exponent for a given element `g` is defined
|
618 |
+
by `\mathrm{leading\_exponent}[g]` `= e_i`, if `\mathrm{depth}[g] = i`.
|
619 |
+
|
620 |
+
Examples
|
621 |
+
========
|
622 |
+
|
623 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
624 |
+
>>> G = SymmetricGroup(3)
|
625 |
+
>>> PcGroup = G.polycyclic_group()
|
626 |
+
>>> collector = PcGroup.collector
|
627 |
+
>>> collector.leading_exponent(G[1])
|
628 |
+
1
|
629 |
+
|
630 |
+
"""
|
631 |
+
exp_vector = self.exponent_vector(element)
|
632 |
+
depth = self.depth(element)
|
633 |
+
if depth != len(self.pcgs)+1:
|
634 |
+
return exp_vector[depth-1]
|
635 |
+
return None
|
636 |
+
|
637 |
+
def _sift(self, z, g):
|
638 |
+
h = g
|
639 |
+
d = self.depth(h)
|
640 |
+
while d < len(self.pcgs) and z[d-1] != 1:
|
641 |
+
k = z[d-1]
|
642 |
+
e = self.leading_exponent(h)*(self.leading_exponent(k))**-1
|
643 |
+
e = e % self.relative_order[d-1]
|
644 |
+
h = k**-e*h
|
645 |
+
d = self.depth(h)
|
646 |
+
return h
|
647 |
+
|
648 |
+
def induced_pcgs(self, gens):
|
649 |
+
"""
|
650 |
+
|
651 |
+
Parameters
|
652 |
+
==========
|
653 |
+
|
654 |
+
gens : list
|
655 |
+
A list of generators on which polycyclic subgroup
|
656 |
+
is to be defined.
|
657 |
+
|
658 |
+
Examples
|
659 |
+
========
|
660 |
+
|
661 |
+
>>> from sympy.combinatorics.named_groups import SymmetricGroup
|
662 |
+
>>> S = SymmetricGroup(8)
|
663 |
+
>>> G = S.sylow_subgroup(2)
|
664 |
+
>>> PcGroup = G.polycyclic_group()
|
665 |
+
>>> collector = PcGroup.collector
|
666 |
+
>>> gens = [G[0], G[1]]
|
667 |
+
>>> ipcgs = collector.induced_pcgs(gens)
|
668 |
+
>>> [gen.order() for gen in ipcgs]
|
669 |
+
[2, 2, 2]
|
670 |
+
>>> G = S.sylow_subgroup(3)
|
671 |
+
>>> PcGroup = G.polycyclic_group()
|
672 |
+
>>> collector = PcGroup.collector
|
673 |
+
>>> gens = [G[0], G[1]]
|
674 |
+
>>> ipcgs = collector.induced_pcgs(gens)
|
675 |
+
>>> [gen.order() for gen in ipcgs]
|
676 |
+
[3]
|
677 |
+
|
678 |
+
"""
|
679 |
+
z = [1]*len(self.pcgs)
|
680 |
+
G = gens
|
681 |
+
while G:
|
682 |
+
g = G.pop(0)
|
683 |
+
h = self._sift(z, g)
|
684 |
+
d = self.depth(h)
|
685 |
+
if d < len(self.pcgs):
|
686 |
+
for gen in z:
|
687 |
+
if gen != 1:
|
688 |
+
G.append(h**-1*gen**-1*h*gen)
|
689 |
+
z[d-1] = h;
|
690 |
+
z = [gen for gen in z if gen != 1]
|
691 |
+
return z
|
692 |
+
|
693 |
+
def constructive_membership_test(self, ipcgs, g):
|
694 |
+
"""
|
695 |
+
Return the exponent vector for induced pcgs.
|
696 |
+
"""
|
697 |
+
e = [0]*len(ipcgs)
|
698 |
+
h = g
|
699 |
+
d = self.depth(h)
|
700 |
+
for i, gen in enumerate(ipcgs):
|
701 |
+
while self.depth(gen) == d:
|
702 |
+
f = self.leading_exponent(h)*self.leading_exponent(gen)
|
703 |
+
f = f % self.relative_order[d-1]
|
704 |
+
h = gen**(-f)*h
|
705 |
+
e[i] = f
|
706 |
+
d = self.depth(h)
|
707 |
+
if h == 1:
|
708 |
+
return e
|
709 |
+
return False
|
venv/lib/python3.10/site-packages/sympy/combinatorics/perm_groups.py
ADDED
The diff for this file is too large to render.
See raw diff
|
|
venv/lib/python3.10/site-packages/sympy/combinatorics/permutations.py
ADDED
@@ -0,0 +1,3112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
from collections import defaultdict
|
3 |
+
from collections.abc import Iterable
|
4 |
+
from functools import reduce
|
5 |
+
|
6 |
+
from sympy.core.parameters import global_parameters
|
7 |
+
from sympy.core.basic import Atom
|
8 |
+
from sympy.core.expr import Expr
|
9 |
+
from sympy.core.numbers import Integer
|
10 |
+
from sympy.core.sympify import _sympify
|
11 |
+
from sympy.matrices import zeros
|
12 |
+
from sympy.polys.polytools import lcm
|
13 |
+
from sympy.printing.repr import srepr
|
14 |
+
from sympy.utilities.iterables import (flatten, has_variety, minlex,
|
15 |
+
has_dups, runs, is_sequence)
|
16 |
+
from sympy.utilities.misc import as_int
|
17 |
+
from mpmath.libmp.libintmath import ifac
|
18 |
+
from sympy.multipledispatch import dispatch
|
19 |
+
|
20 |
+
def _af_rmul(a, b):
|
21 |
+
"""
|
22 |
+
Return the product b*a; input and output are array forms. The ith value
|
23 |
+
is a[b[i]].
|
24 |
+
|
25 |
+
Examples
|
26 |
+
========
|
27 |
+
|
28 |
+
>>> from sympy.combinatorics.permutations import _af_rmul, Permutation
|
29 |
+
|
30 |
+
>>> a, b = [1, 0, 2], [0, 2, 1]
|
31 |
+
>>> _af_rmul(a, b)
|
32 |
+
[1, 2, 0]
|
33 |
+
>>> [a[b[i]] for i in range(3)]
|
34 |
+
[1, 2, 0]
|
35 |
+
|
36 |
+
This handles the operands in reverse order compared to the ``*`` operator:
|
37 |
+
|
38 |
+
>>> a = Permutation(a)
|
39 |
+
>>> b = Permutation(b)
|
40 |
+
>>> list(a*b)
|
41 |
+
[2, 0, 1]
|
42 |
+
>>> [b(a(i)) for i in range(3)]
|
43 |
+
[2, 0, 1]
|
44 |
+
|
45 |
+
See Also
|
46 |
+
========
|
47 |
+
|
48 |
+
rmul, _af_rmuln
|
49 |
+
"""
|
50 |
+
return [a[i] for i in b]
|
51 |
+
|
52 |
+
|
53 |
+
def _af_rmuln(*abc):
|
54 |
+
"""
|
55 |
+
Given [a, b, c, ...] return the product of ...*c*b*a using array forms.
|
56 |
+
The ith value is a[b[c[i]]].
|
57 |
+
|
58 |
+
Examples
|
59 |
+
========
|
60 |
+
|
61 |
+
>>> from sympy.combinatorics.permutations import _af_rmul, Permutation
|
62 |
+
|
63 |
+
>>> a, b = [1, 0, 2], [0, 2, 1]
|
64 |
+
>>> _af_rmul(a, b)
|
65 |
+
[1, 2, 0]
|
66 |
+
>>> [a[b[i]] for i in range(3)]
|
67 |
+
[1, 2, 0]
|
68 |
+
|
69 |
+
This handles the operands in reverse order compared to the ``*`` operator:
|
70 |
+
|
71 |
+
>>> a = Permutation(a); b = Permutation(b)
|
72 |
+
>>> list(a*b)
|
73 |
+
[2, 0, 1]
|
74 |
+
>>> [b(a(i)) for i in range(3)]
|
75 |
+
[2, 0, 1]
|
76 |
+
|
77 |
+
See Also
|
78 |
+
========
|
79 |
+
|
80 |
+
rmul, _af_rmul
|
81 |
+
"""
|
82 |
+
a = abc
|
83 |
+
m = len(a)
|
84 |
+
if m == 3:
|
85 |
+
p0, p1, p2 = a
|
86 |
+
return [p0[p1[i]] for i in p2]
|
87 |
+
if m == 4:
|
88 |
+
p0, p1, p2, p3 = a
|
89 |
+
return [p0[p1[p2[i]]] for i in p3]
|
90 |
+
if m == 5:
|
91 |
+
p0, p1, p2, p3, p4 = a
|
92 |
+
return [p0[p1[p2[p3[i]]]] for i in p4]
|
93 |
+
if m == 6:
|
94 |
+
p0, p1, p2, p3, p4, p5 = a
|
95 |
+
return [p0[p1[p2[p3[p4[i]]]]] for i in p5]
|
96 |
+
if m == 7:
|
97 |
+
p0, p1, p2, p3, p4, p5, p6 = a
|
98 |
+
return [p0[p1[p2[p3[p4[p5[i]]]]]] for i in p6]
|
99 |
+
if m == 8:
|
100 |
+
p0, p1, p2, p3, p4, p5, p6, p7 = a
|
101 |
+
return [p0[p1[p2[p3[p4[p5[p6[i]]]]]]] for i in p7]
|
102 |
+
if m == 1:
|
103 |
+
return a[0][:]
|
104 |
+
if m == 2:
|
105 |
+
a, b = a
|
106 |
+
return [a[i] for i in b]
|
107 |
+
if m == 0:
|
108 |
+
raise ValueError("String must not be empty")
|
109 |
+
p0 = _af_rmuln(*a[:m//2])
|
110 |
+
p1 = _af_rmuln(*a[m//2:])
|
111 |
+
return [p0[i] for i in p1]
|
112 |
+
|
113 |
+
|
114 |
+
def _af_parity(pi):
|
115 |
+
"""
|
116 |
+
Computes the parity of a permutation in array form.
|
117 |
+
|
118 |
+
Explanation
|
119 |
+
===========
|
120 |
+
|
121 |
+
The parity of a permutation reflects the parity of the
|
122 |
+
number of inversions in the permutation, i.e., the
|
123 |
+
number of pairs of x and y such that x > y but p[x] < p[y].
|
124 |
+
|
125 |
+
Examples
|
126 |
+
========
|
127 |
+
|
128 |
+
>>> from sympy.combinatorics.permutations import _af_parity
|
129 |
+
>>> _af_parity([0, 1, 2, 3])
|
130 |
+
0
|
131 |
+
>>> _af_parity([3, 2, 0, 1])
|
132 |
+
1
|
133 |
+
|
134 |
+
See Also
|
135 |
+
========
|
136 |
+
|
137 |
+
Permutation
|
138 |
+
"""
|
139 |
+
n = len(pi)
|
140 |
+
a = [0] * n
|
141 |
+
c = 0
|
142 |
+
for j in range(n):
|
143 |
+
if a[j] == 0:
|
144 |
+
c += 1
|
145 |
+
a[j] = 1
|
146 |
+
i = j
|
147 |
+
while pi[i] != j:
|
148 |
+
i = pi[i]
|
149 |
+
a[i] = 1
|
150 |
+
return (n - c) % 2
|
151 |
+
|
152 |
+
|
153 |
+
def _af_invert(a):
|
154 |
+
"""
|
155 |
+
Finds the inverse, ~A, of a permutation, A, given in array form.
|
156 |
+
|
157 |
+
Examples
|
158 |
+
========
|
159 |
+
|
160 |
+
>>> from sympy.combinatorics.permutations import _af_invert, _af_rmul
|
161 |
+
>>> A = [1, 2, 0, 3]
|
162 |
+
>>> _af_invert(A)
|
163 |
+
[2, 0, 1, 3]
|
164 |
+
>>> _af_rmul(_, A)
|
165 |
+
[0, 1, 2, 3]
|
166 |
+
|
167 |
+
See Also
|
168 |
+
========
|
169 |
+
|
170 |
+
Permutation, __invert__
|
171 |
+
"""
|
172 |
+
inv_form = [0] * len(a)
|
173 |
+
for i, ai in enumerate(a):
|
174 |
+
inv_form[ai] = i
|
175 |
+
return inv_form
|
176 |
+
|
177 |
+
|
178 |
+
def _af_pow(a, n):
|
179 |
+
"""
|
180 |
+
Routine for finding powers of a permutation.
|
181 |
+
|
182 |
+
Examples
|
183 |
+
========
|
184 |
+
|
185 |
+
>>> from sympy.combinatorics import Permutation
|
186 |
+
>>> from sympy.combinatorics.permutations import _af_pow
|
187 |
+
>>> p = Permutation([2, 0, 3, 1])
|
188 |
+
>>> p.order()
|
189 |
+
4
|
190 |
+
>>> _af_pow(p._array_form, 4)
|
191 |
+
[0, 1, 2, 3]
|
192 |
+
"""
|
193 |
+
if n == 0:
|
194 |
+
return list(range(len(a)))
|
195 |
+
if n < 0:
|
196 |
+
return _af_pow(_af_invert(a), -n)
|
197 |
+
if n == 1:
|
198 |
+
return a[:]
|
199 |
+
elif n == 2:
|
200 |
+
b = [a[i] for i in a]
|
201 |
+
elif n == 3:
|
202 |
+
b = [a[a[i]] for i in a]
|
203 |
+
elif n == 4:
|
204 |
+
b = [a[a[a[i]]] for i in a]
|
205 |
+
else:
|
206 |
+
# use binary multiplication
|
207 |
+
b = list(range(len(a)))
|
208 |
+
while 1:
|
209 |
+
if n & 1:
|
210 |
+
b = [b[i] for i in a]
|
211 |
+
n -= 1
|
212 |
+
if not n:
|
213 |
+
break
|
214 |
+
if n % 4 == 0:
|
215 |
+
a = [a[a[a[i]]] for i in a]
|
216 |
+
n = n // 4
|
217 |
+
elif n % 2 == 0:
|
218 |
+
a = [a[i] for i in a]
|
219 |
+
n = n // 2
|
220 |
+
return b
|
221 |
+
|
222 |
+
|
223 |
+
def _af_commutes_with(a, b):
|
224 |
+
"""
|
225 |
+
Checks if the two permutations with array forms
|
226 |
+
given by ``a`` and ``b`` commute.
|
227 |
+
|
228 |
+
Examples
|
229 |
+
========
|
230 |
+
|
231 |
+
>>> from sympy.combinatorics.permutations import _af_commutes_with
|
232 |
+
>>> _af_commutes_with([1, 2, 0], [0, 2, 1])
|
233 |
+
False
|
234 |
+
|
235 |
+
See Also
|
236 |
+
========
|
237 |
+
|
238 |
+
Permutation, commutes_with
|
239 |
+
"""
|
240 |
+
return not any(a[b[i]] != b[a[i]] for i in range(len(a) - 1))
|
241 |
+
|
242 |
+
|
243 |
+
class Cycle(dict):
|
244 |
+
"""
|
245 |
+
Wrapper around dict which provides the functionality of a disjoint cycle.
|
246 |
+
|
247 |
+
Explanation
|
248 |
+
===========
|
249 |
+
|
250 |
+
A cycle shows the rule to use to move subsets of elements to obtain
|
251 |
+
a permutation. The Cycle class is more flexible than Permutation in
|
252 |
+
that 1) all elements need not be present in order to investigate how
|
253 |
+
multiple cycles act in sequence and 2) it can contain singletons:
|
254 |
+
|
255 |
+
>>> from sympy.combinatorics.permutations import Perm, Cycle
|
256 |
+
|
257 |
+
A Cycle will automatically parse a cycle given as a tuple on the rhs:
|
258 |
+
|
259 |
+
>>> Cycle(1, 2)(2, 3)
|
260 |
+
(1 3 2)
|
261 |
+
|
262 |
+
The identity cycle, Cycle(), can be used to start a product:
|
263 |
+
|
264 |
+
>>> Cycle()(1, 2)(2, 3)
|
265 |
+
(1 3 2)
|
266 |
+
|
267 |
+
The array form of a Cycle can be obtained by calling the list
|
268 |
+
method (or passing it to the list function) and all elements from
|
269 |
+
0 will be shown:
|
270 |
+
|
271 |
+
>>> a = Cycle(1, 2)
|
272 |
+
>>> a.list()
|
273 |
+
[0, 2, 1]
|
274 |
+
>>> list(a)
|
275 |
+
[0, 2, 1]
|
276 |
+
|
277 |
+
If a larger (or smaller) range is desired use the list method and
|
278 |
+
provide the desired size -- but the Cycle cannot be truncated to
|
279 |
+
a size smaller than the largest element that is out of place:
|
280 |
+
|
281 |
+
>>> b = Cycle(2, 4)(1, 2)(3, 1, 4)(1, 3)
|
282 |
+
>>> b.list()
|
283 |
+
[0, 2, 1, 3, 4]
|
284 |
+
>>> b.list(b.size + 1)
|
285 |
+
[0, 2, 1, 3, 4, 5]
|
286 |
+
>>> b.list(-1)
|
287 |
+
[0, 2, 1]
|
288 |
+
|
289 |
+
Singletons are not shown when printing with one exception: the largest
|
290 |
+
element is always shown -- as a singleton if necessary:
|
291 |
+
|
292 |
+
>>> Cycle(1, 4, 10)(4, 5)
|
293 |
+
(1 5 4 10)
|
294 |
+
>>> Cycle(1, 2)(4)(5)(10)
|
295 |
+
(1 2)(10)
|
296 |
+
|
297 |
+
The array form can be used to instantiate a Permutation so other
|
298 |
+
properties of the permutation can be investigated:
|
299 |
+
|
300 |
+
>>> Perm(Cycle(1, 2)(3, 4).list()).transpositions()
|
301 |
+
[(1, 2), (3, 4)]
|
302 |
+
|
303 |
+
Notes
|
304 |
+
=====
|
305 |
+
|
306 |
+
The underlying structure of the Cycle is a dictionary and although
|
307 |
+
the __iter__ method has been redefined to give the array form of the
|
308 |
+
cycle, the underlying dictionary items are still available with the
|
309 |
+
such methods as items():
|
310 |
+
|
311 |
+
>>> list(Cycle(1, 2).items())
|
312 |
+
[(1, 2), (2, 1)]
|
313 |
+
|
314 |
+
See Also
|
315 |
+
========
|
316 |
+
|
317 |
+
Permutation
|
318 |
+
"""
|
319 |
+
def __missing__(self, arg):
|
320 |
+
"""Enter arg into dictionary and return arg."""
|
321 |
+
return as_int(arg)
|
322 |
+
|
323 |
+
def __iter__(self):
|
324 |
+
yield from self.list()
|
325 |
+
|
326 |
+
def __call__(self, *other):
|
327 |
+
"""Return product of cycles processed from R to L.
|
328 |
+
|
329 |
+
Examples
|
330 |
+
========
|
331 |
+
|
332 |
+
>>> from sympy.combinatorics import Cycle
|
333 |
+
>>> Cycle(1, 2)(2, 3)
|
334 |
+
(1 3 2)
|
335 |
+
|
336 |
+
An instance of a Cycle will automatically parse list-like
|
337 |
+
objects and Permutations that are on the right. It is more
|
338 |
+
flexible than the Permutation in that all elements need not
|
339 |
+
be present:
|
340 |
+
|
341 |
+
>>> a = Cycle(1, 2)
|
342 |
+
>>> a(2, 3)
|
343 |
+
(1 3 2)
|
344 |
+
>>> a(2, 3)(4, 5)
|
345 |
+
(1 3 2)(4 5)
|
346 |
+
|
347 |
+
"""
|
348 |
+
rv = Cycle(*other)
|
349 |
+
for k, v in zip(list(self.keys()), [rv[self[k]] for k in self.keys()]):
|
350 |
+
rv[k] = v
|
351 |
+
return rv
|
352 |
+
|
353 |
+
def list(self, size=None):
|
354 |
+
"""Return the cycles as an explicit list starting from 0 up
|
355 |
+
to the greater of the largest value in the cycles and size.
|
356 |
+
|
357 |
+
Truncation of trailing unmoved items will occur when size
|
358 |
+
is less than the maximum element in the cycle; if this is
|
359 |
+
desired, setting ``size=-1`` will guarantee such trimming.
|
360 |
+
|
361 |
+
Examples
|
362 |
+
========
|
363 |
+
|
364 |
+
>>> from sympy.combinatorics import Cycle
|
365 |
+
>>> p = Cycle(2, 3)(4, 5)
|
366 |
+
>>> p.list()
|
367 |
+
[0, 1, 3, 2, 5, 4]
|
368 |
+
>>> p.list(10)
|
369 |
+
[0, 1, 3, 2, 5, 4, 6, 7, 8, 9]
|
370 |
+
|
371 |
+
Passing a length too small will trim trailing, unchanged elements
|
372 |
+
in the permutation:
|
373 |
+
|
374 |
+
>>> Cycle(2, 4)(1, 2, 4).list(-1)
|
375 |
+
[0, 2, 1]
|
376 |
+
"""
|
377 |
+
if not self and size is None:
|
378 |
+
raise ValueError('must give size for empty Cycle')
|
379 |
+
if size is not None:
|
380 |
+
big = max([i for i in self.keys() if self[i] != i] + [0])
|
381 |
+
size = max(size, big + 1)
|
382 |
+
else:
|
383 |
+
size = self.size
|
384 |
+
return [self[i] for i in range(size)]
|
385 |
+
|
386 |
+
def __repr__(self):
|
387 |
+
"""We want it to print as a Cycle, not as a dict.
|
388 |
+
|
389 |
+
Examples
|
390 |
+
========
|
391 |
+
|
392 |
+
>>> from sympy.combinatorics import Cycle
|
393 |
+
>>> Cycle(1, 2)
|
394 |
+
(1 2)
|
395 |
+
>>> print(_)
|
396 |
+
(1 2)
|
397 |
+
>>> list(Cycle(1, 2).items())
|
398 |
+
[(1, 2), (2, 1)]
|
399 |
+
"""
|
400 |
+
if not self:
|
401 |
+
return 'Cycle()'
|
402 |
+
cycles = Permutation(self).cyclic_form
|
403 |
+
s = ''.join(str(tuple(c)) for c in cycles)
|
404 |
+
big = self.size - 1
|
405 |
+
if not any(i == big for c in cycles for i in c):
|
406 |
+
s += '(%s)' % big
|
407 |
+
return 'Cycle%s' % s
|
408 |
+
|
409 |
+
def __str__(self):
|
410 |
+
"""We want it to be printed in a Cycle notation with no
|
411 |
+
comma in-between.
|
412 |
+
|
413 |
+
Examples
|
414 |
+
========
|
415 |
+
|
416 |
+
>>> from sympy.combinatorics import Cycle
|
417 |
+
>>> Cycle(1, 2)
|
418 |
+
(1 2)
|
419 |
+
>>> Cycle(1, 2, 4)(5, 6)
|
420 |
+
(1 2 4)(5 6)
|
421 |
+
"""
|
422 |
+
if not self:
|
423 |
+
return '()'
|
424 |
+
cycles = Permutation(self).cyclic_form
|
425 |
+
s = ''.join(str(tuple(c)) for c in cycles)
|
426 |
+
big = self.size - 1
|
427 |
+
if not any(i == big for c in cycles for i in c):
|
428 |
+
s += '(%s)' % big
|
429 |
+
s = s.replace(',', '')
|
430 |
+
return s
|
431 |
+
|
432 |
+
def __init__(self, *args):
|
433 |
+
"""Load up a Cycle instance with the values for the cycle.
|
434 |
+
|
435 |
+
Examples
|
436 |
+
========
|
437 |
+
|
438 |
+
>>> from sympy.combinatorics import Cycle
|
439 |
+
>>> Cycle(1, 2, 6)
|
440 |
+
(1 2 6)
|
441 |
+
"""
|
442 |
+
|
443 |
+
if not args:
|
444 |
+
return
|
445 |
+
if len(args) == 1:
|
446 |
+
if isinstance(args[0], Permutation):
|
447 |
+
for c in args[0].cyclic_form:
|
448 |
+
self.update(self(*c))
|
449 |
+
return
|
450 |
+
elif isinstance(args[0], Cycle):
|
451 |
+
for k, v in args[0].items():
|
452 |
+
self[k] = v
|
453 |
+
return
|
454 |
+
args = [as_int(a) for a in args]
|
455 |
+
if any(i < 0 for i in args):
|
456 |
+
raise ValueError('negative integers are not allowed in a cycle.')
|
457 |
+
if has_dups(args):
|
458 |
+
raise ValueError('All elements must be unique in a cycle.')
|
459 |
+
for i in range(-len(args), 0):
|
460 |
+
self[args[i]] = args[i + 1]
|
461 |
+
|
462 |
+
@property
|
463 |
+
def size(self):
|
464 |
+
if not self:
|
465 |
+
return 0
|
466 |
+
return max(self.keys()) + 1
|
467 |
+
|
468 |
+
def copy(self):
|
469 |
+
return Cycle(self)
|
470 |
+
|
471 |
+
|
472 |
+
class Permutation(Atom):
|
473 |
+
r"""
|
474 |
+
A permutation, alternatively known as an 'arrangement number' or 'ordering'
|
475 |
+
is an arrangement of the elements of an ordered list into a one-to-one
|
476 |
+
mapping with itself. The permutation of a given arrangement is given by
|
477 |
+
indicating the positions of the elements after re-arrangement [2]_. For
|
478 |
+
example, if one started with elements ``[x, y, a, b]`` (in that order) and
|
479 |
+
they were reordered as ``[x, y, b, a]`` then the permutation would be
|
480 |
+
``[0, 1, 3, 2]``. Notice that (in SymPy) the first element is always referred
|
481 |
+
to as 0 and the permutation uses the indices of the elements in the
|
482 |
+
original ordering, not the elements ``(a, b, ...)`` themselves.
|
483 |
+
|
484 |
+
>>> from sympy.combinatorics import Permutation
|
485 |
+
>>> from sympy import init_printing
|
486 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
487 |
+
|
488 |
+
Permutations Notation
|
489 |
+
=====================
|
490 |
+
|
491 |
+
Permutations are commonly represented in disjoint cycle or array forms.
|
492 |
+
|
493 |
+
Array Notation and 2-line Form
|
494 |
+
------------------------------------
|
495 |
+
|
496 |
+
In the 2-line form, the elements and their final positions are shown
|
497 |
+
as a matrix with 2 rows:
|
498 |
+
|
499 |
+
[0 1 2 ... n-1]
|
500 |
+
[p(0) p(1) p(2) ... p(n-1)]
|
501 |
+
|
502 |
+
Since the first line is always ``range(n)``, where n is the size of p,
|
503 |
+
it is sufficient to represent the permutation by the second line,
|
504 |
+
referred to as the "array form" of the permutation. This is entered
|
505 |
+
in brackets as the argument to the Permutation class:
|
506 |
+
|
507 |
+
>>> p = Permutation([0, 2, 1]); p
|
508 |
+
Permutation([0, 2, 1])
|
509 |
+
|
510 |
+
Given i in range(p.size), the permutation maps i to i^p
|
511 |
+
|
512 |
+
>>> [i^p for i in range(p.size)]
|
513 |
+
[0, 2, 1]
|
514 |
+
|
515 |
+
The composite of two permutations p*q means first apply p, then q, so
|
516 |
+
i^(p*q) = (i^p)^q which is i^p^q according to Python precedence rules:
|
517 |
+
|
518 |
+
>>> q = Permutation([2, 1, 0])
|
519 |
+
>>> [i^p^q for i in range(3)]
|
520 |
+
[2, 0, 1]
|
521 |
+
>>> [i^(p*q) for i in range(3)]
|
522 |
+
[2, 0, 1]
|
523 |
+
|
524 |
+
One can use also the notation p(i) = i^p, but then the composition
|
525 |
+
rule is (p*q)(i) = q(p(i)), not p(q(i)):
|
526 |
+
|
527 |
+
>>> [(p*q)(i) for i in range(p.size)]
|
528 |
+
[2, 0, 1]
|
529 |
+
>>> [q(p(i)) for i in range(p.size)]
|
530 |
+
[2, 0, 1]
|
531 |
+
>>> [p(q(i)) for i in range(p.size)]
|
532 |
+
[1, 2, 0]
|
533 |
+
|
534 |
+
Disjoint Cycle Notation
|
535 |
+
-----------------------
|
536 |
+
|
537 |
+
In disjoint cycle notation, only the elements that have shifted are
|
538 |
+
indicated.
|
539 |
+
|
540 |
+
For example, [1, 3, 2, 0] can be represented as (0, 1, 3)(2).
|
541 |
+
This can be understood from the 2 line format of the given permutation.
|
542 |
+
In the 2-line form,
|
543 |
+
[0 1 2 3]
|
544 |
+
[1 3 2 0]
|
545 |
+
|
546 |
+
The element in the 0th position is 1, so 0 -> 1. The element in the 1st
|
547 |
+
position is three, so 1 -> 3. And the element in the third position is again
|
548 |
+
0, so 3 -> 0. Thus, 0 -> 1 -> 3 -> 0, and 2 -> 2. Thus, this can be represented
|
549 |
+
as 2 cycles: (0, 1, 3)(2).
|
550 |
+
In common notation, singular cycles are not explicitly written as they can be
|
551 |
+
inferred implicitly.
|
552 |
+
|
553 |
+
Only the relative ordering of elements in a cycle matter:
|
554 |
+
|
555 |
+
>>> Permutation(1,2,3) == Permutation(2,3,1) == Permutation(3,1,2)
|
556 |
+
True
|
557 |
+
|
558 |
+
The disjoint cycle notation is convenient when representing
|
559 |
+
permutations that have several cycles in them:
|
560 |
+
|
561 |
+
>>> Permutation(1, 2)(3, 5) == Permutation([[1, 2], [3, 5]])
|
562 |
+
True
|
563 |
+
|
564 |
+
It also provides some economy in entry when computing products of
|
565 |
+
permutations that are written in disjoint cycle notation:
|
566 |
+
|
567 |
+
>>> Permutation(1, 2)(1, 3)(2, 3)
|
568 |
+
Permutation([0, 3, 2, 1])
|
569 |
+
>>> _ == Permutation([[1, 2]])*Permutation([[1, 3]])*Permutation([[2, 3]])
|
570 |
+
True
|
571 |
+
|
572 |
+
Caution: when the cycles have common elements between them then the order
|
573 |
+
in which the permutations are applied matters. This module applies
|
574 |
+
the permutations from *left to right*.
|
575 |
+
|
576 |
+
>>> Permutation(1, 2)(2, 3) == Permutation([(1, 2), (2, 3)])
|
577 |
+
True
|
578 |
+
>>> Permutation(1, 2)(2, 3).list()
|
579 |
+
[0, 3, 1, 2]
|
580 |
+
|
581 |
+
In the above case, (1,2) is computed before (2,3).
|
582 |
+
As 0 -> 0, 0 -> 0, element in position 0 is 0.
|
583 |
+
As 1 -> 2, 2 -> 3, element in position 1 is 3.
|
584 |
+
As 2 -> 1, 1 -> 1, element in position 2 is 1.
|
585 |
+
As 3 -> 3, 3 -> 2, element in position 3 is 2.
|
586 |
+
|
587 |
+
If the first and second elements had been
|
588 |
+
swapped first, followed by the swapping of the second
|
589 |
+
and third, the result would have been [0, 2, 3, 1].
|
590 |
+
If, you want to apply the cycles in the conventional
|
591 |
+
right to left order, call the function with arguments in reverse order
|
592 |
+
as demonstrated below:
|
593 |
+
|
594 |
+
>>> Permutation([(1, 2), (2, 3)][::-1]).list()
|
595 |
+
[0, 2, 3, 1]
|
596 |
+
|
597 |
+
Entering a singleton in a permutation is a way to indicate the size of the
|
598 |
+
permutation. The ``size`` keyword can also be used.
|
599 |
+
|
600 |
+
Array-form entry:
|
601 |
+
|
602 |
+
>>> Permutation([[1, 2], [9]])
|
603 |
+
Permutation([0, 2, 1], size=10)
|
604 |
+
>>> Permutation([[1, 2]], size=10)
|
605 |
+
Permutation([0, 2, 1], size=10)
|
606 |
+
|
607 |
+
Cyclic-form entry:
|
608 |
+
|
609 |
+
>>> Permutation(1, 2, size=10)
|
610 |
+
Permutation([0, 2, 1], size=10)
|
611 |
+
>>> Permutation(9)(1, 2)
|
612 |
+
Permutation([0, 2, 1], size=10)
|
613 |
+
|
614 |
+
Caution: no singleton containing an element larger than the largest
|
615 |
+
in any previous cycle can be entered. This is an important difference
|
616 |
+
in how Permutation and Cycle handle the ``__call__`` syntax. A singleton
|
617 |
+
argument at the start of a Permutation performs instantiation of the
|
618 |
+
Permutation and is permitted:
|
619 |
+
|
620 |
+
>>> Permutation(5)
|
621 |
+
Permutation([], size=6)
|
622 |
+
|
623 |
+
A singleton entered after instantiation is a call to the permutation
|
624 |
+
-- a function call -- and if the argument is out of range it will
|
625 |
+
trigger an error. For this reason, it is better to start the cycle
|
626 |
+
with the singleton:
|
627 |
+
|
628 |
+
The following fails because there is no element 3:
|
629 |
+
|
630 |
+
>>> Permutation(1, 2)(3)
|
631 |
+
Traceback (most recent call last):
|
632 |
+
...
|
633 |
+
IndexError: list index out of range
|
634 |
+
|
635 |
+
This is ok: only the call to an out of range singleton is prohibited;
|
636 |
+
otherwise the permutation autosizes:
|
637 |
+
|
638 |
+
>>> Permutation(3)(1, 2)
|
639 |
+
Permutation([0, 2, 1, 3])
|
640 |
+
>>> Permutation(1, 2)(3, 4) == Permutation(3, 4)(1, 2)
|
641 |
+
True
|
642 |
+
|
643 |
+
|
644 |
+
Equality testing
|
645 |
+
----------------
|
646 |
+
|
647 |
+
The array forms must be the same in order for permutations to be equal:
|
648 |
+
|
649 |
+
>>> Permutation([1, 0, 2, 3]) == Permutation([1, 0])
|
650 |
+
False
|
651 |
+
|
652 |
+
|
653 |
+
Identity Permutation
|
654 |
+
--------------------
|
655 |
+
|
656 |
+
The identity permutation is a permutation in which no element is out of
|
657 |
+
place. It can be entered in a variety of ways. All the following create
|
658 |
+
an identity permutation of size 4:
|
659 |
+
|
660 |
+
>>> I = Permutation([0, 1, 2, 3])
|
661 |
+
>>> all(p == I for p in [
|
662 |
+
... Permutation(3),
|
663 |
+
... Permutation(range(4)),
|
664 |
+
... Permutation([], size=4),
|
665 |
+
... Permutation(size=4)])
|
666 |
+
True
|
667 |
+
|
668 |
+
Watch out for entering the range *inside* a set of brackets (which is
|
669 |
+
cycle notation):
|
670 |
+
|
671 |
+
>>> I == Permutation([range(4)])
|
672 |
+
False
|
673 |
+
|
674 |
+
|
675 |
+
Permutation Printing
|
676 |
+
====================
|
677 |
+
|
678 |
+
There are a few things to note about how Permutations are printed.
|
679 |
+
|
680 |
+
.. deprecated:: 1.6
|
681 |
+
|
682 |
+
Configuring Permutation printing by setting
|
683 |
+
``Permutation.print_cyclic`` is deprecated. Users should use the
|
684 |
+
``perm_cyclic`` flag to the printers, as described below.
|
685 |
+
|
686 |
+
1) If you prefer one form (array or cycle) over another, you can set
|
687 |
+
``init_printing`` with the ``perm_cyclic`` flag.
|
688 |
+
|
689 |
+
>>> from sympy import init_printing
|
690 |
+
>>> p = Permutation(1, 2)(4, 5)(3, 4)
|
691 |
+
>>> p
|
692 |
+
Permutation([0, 2, 1, 4, 5, 3])
|
693 |
+
|
694 |
+
>>> init_printing(perm_cyclic=True, pretty_print=False)
|
695 |
+
>>> p
|
696 |
+
(1 2)(3 4 5)
|
697 |
+
|
698 |
+
2) Regardless of the setting, a list of elements in the array for cyclic
|
699 |
+
form can be obtained and either of those can be copied and supplied as
|
700 |
+
the argument to Permutation:
|
701 |
+
|
702 |
+
>>> p.array_form
|
703 |
+
[0, 2, 1, 4, 5, 3]
|
704 |
+
>>> p.cyclic_form
|
705 |
+
[[1, 2], [3, 4, 5]]
|
706 |
+
>>> Permutation(_) == p
|
707 |
+
True
|
708 |
+
|
709 |
+
3) Printing is economical in that as little as possible is printed while
|
710 |
+
retaining all information about the size of the permutation:
|
711 |
+
|
712 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
713 |
+
>>> Permutation([1, 0, 2, 3])
|
714 |
+
Permutation([1, 0, 2, 3])
|
715 |
+
>>> Permutation([1, 0, 2, 3], size=20)
|
716 |
+
Permutation([1, 0], size=20)
|
717 |
+
>>> Permutation([1, 0, 2, 4, 3, 5, 6], size=20)
|
718 |
+
Permutation([1, 0, 2, 4, 3], size=20)
|
719 |
+
|
720 |
+
>>> p = Permutation([1, 0, 2, 3])
|
721 |
+
>>> init_printing(perm_cyclic=True, pretty_print=False)
|
722 |
+
>>> p
|
723 |
+
(3)(0 1)
|
724 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
725 |
+
|
726 |
+
The 2 was not printed but it is still there as can be seen with the
|
727 |
+
array_form and size methods:
|
728 |
+
|
729 |
+
>>> p.array_form
|
730 |
+
[1, 0, 2, 3]
|
731 |
+
>>> p.size
|
732 |
+
4
|
733 |
+
|
734 |
+
Short introduction to other methods
|
735 |
+
===================================
|
736 |
+
|
737 |
+
The permutation can act as a bijective function, telling what element is
|
738 |
+
located at a given position
|
739 |
+
|
740 |
+
>>> q = Permutation([5, 2, 3, 4, 1, 0])
|
741 |
+
>>> q.array_form[1] # the hard way
|
742 |
+
2
|
743 |
+
>>> q(1) # the easy way
|
744 |
+
2
|
745 |
+
>>> {i: q(i) for i in range(q.size)} # showing the bijection
|
746 |
+
{0: 5, 1: 2, 2: 3, 3: 4, 4: 1, 5: 0}
|
747 |
+
|
748 |
+
The full cyclic form (including singletons) can be obtained:
|
749 |
+
|
750 |
+
>>> p.full_cyclic_form
|
751 |
+
[[0, 1], [2], [3]]
|
752 |
+
|
753 |
+
Any permutation can be factored into transpositions of pairs of elements:
|
754 |
+
|
755 |
+
>>> Permutation([[1, 2], [3, 4, 5]]).transpositions()
|
756 |
+
[(1, 2), (3, 5), (3, 4)]
|
757 |
+
>>> Permutation.rmul(*[Permutation([ti], size=6) for ti in _]).cyclic_form
|
758 |
+
[[1, 2], [3, 4, 5]]
|
759 |
+
|
760 |
+
The number of permutations on a set of n elements is given by n! and is
|
761 |
+
called the cardinality.
|
762 |
+
|
763 |
+
>>> p.size
|
764 |
+
4
|
765 |
+
>>> p.cardinality
|
766 |
+
24
|
767 |
+
|
768 |
+
A given permutation has a rank among all the possible permutations of the
|
769 |
+
same elements, but what that rank is depends on how the permutations are
|
770 |
+
enumerated. (There are a number of different methods of doing so.) The
|
771 |
+
lexicographic rank is given by the rank method and this rank is used to
|
772 |
+
increment a permutation with addition/subtraction:
|
773 |
+
|
774 |
+
>>> p.rank()
|
775 |
+
6
|
776 |
+
>>> p + 1
|
777 |
+
Permutation([1, 0, 3, 2])
|
778 |
+
>>> p.next_lex()
|
779 |
+
Permutation([1, 0, 3, 2])
|
780 |
+
>>> _.rank()
|
781 |
+
7
|
782 |
+
>>> p.unrank_lex(p.size, rank=7)
|
783 |
+
Permutation([1, 0, 3, 2])
|
784 |
+
|
785 |
+
The product of two permutations p and q is defined as their composition as
|
786 |
+
functions, (p*q)(i) = q(p(i)) [6]_.
|
787 |
+
|
788 |
+
>>> p = Permutation([1, 0, 2, 3])
|
789 |
+
>>> q = Permutation([2, 3, 1, 0])
|
790 |
+
>>> list(q*p)
|
791 |
+
[2, 3, 0, 1]
|
792 |
+
>>> list(p*q)
|
793 |
+
[3, 2, 1, 0]
|
794 |
+
>>> [q(p(i)) for i in range(p.size)]
|
795 |
+
[3, 2, 1, 0]
|
796 |
+
|
797 |
+
The permutation can be 'applied' to any list-like object, not only
|
798 |
+
Permutations:
|
799 |
+
|
800 |
+
>>> p(['zero', 'one', 'four', 'two'])
|
801 |
+
['one', 'zero', 'four', 'two']
|
802 |
+
>>> p('zo42')
|
803 |
+
['o', 'z', '4', '2']
|
804 |
+
|
805 |
+
If you have a list of arbitrary elements, the corresponding permutation
|
806 |
+
can be found with the from_sequence method:
|
807 |
+
|
808 |
+
>>> Permutation.from_sequence('SymPy')
|
809 |
+
Permutation([1, 3, 2, 0, 4])
|
810 |
+
|
811 |
+
Checking if a Permutation is contained in a Group
|
812 |
+
=================================================
|
813 |
+
|
814 |
+
Generally if you have a group of permutations G on n symbols, and
|
815 |
+
you're checking if a permutation on less than n symbols is part
|
816 |
+
of that group, the check will fail.
|
817 |
+
|
818 |
+
Here is an example for n=5 and we check if the cycle
|
819 |
+
(1,2,3) is in G:
|
820 |
+
|
821 |
+
>>> from sympy import init_printing
|
822 |
+
>>> init_printing(perm_cyclic=True, pretty_print=False)
|
823 |
+
>>> from sympy.combinatorics import Cycle, Permutation
|
824 |
+
>>> from sympy.combinatorics.perm_groups import PermutationGroup
|
825 |
+
>>> G = PermutationGroup(Cycle(2, 3)(4, 5), Cycle(1, 2, 3, 4, 5))
|
826 |
+
>>> p1 = Permutation(Cycle(2, 5, 3))
|
827 |
+
>>> p2 = Permutation(Cycle(1, 2, 3))
|
828 |
+
>>> a1 = Permutation(Cycle(1, 2, 3).list(6))
|
829 |
+
>>> a2 = Permutation(Cycle(1, 2, 3)(5))
|
830 |
+
>>> a3 = Permutation(Cycle(1, 2, 3),size=6)
|
831 |
+
>>> for p in [p1,p2,a1,a2,a3]: p, G.contains(p)
|
832 |
+
((2 5 3), True)
|
833 |
+
((1 2 3), False)
|
834 |
+
((5)(1 2 3), True)
|
835 |
+
((5)(1 2 3), True)
|
836 |
+
((5)(1 2 3), True)
|
837 |
+
|
838 |
+
The check for p2 above will fail.
|
839 |
+
|
840 |
+
Checking if p1 is in G works because SymPy knows
|
841 |
+
G is a group on 5 symbols, and p1 is also on 5 symbols
|
842 |
+
(its largest element is 5).
|
843 |
+
|
844 |
+
For ``a1``, the ``.list(6)`` call will extend the permutation to 5
|
845 |
+
symbols, so the test will work as well. In the case of ``a2`` the
|
846 |
+
permutation is being extended to 5 symbols by using a singleton,
|
847 |
+
and in the case of ``a3`` it's extended through the constructor
|
848 |
+
argument ``size=6``.
|
849 |
+
|
850 |
+
There is another way to do this, which is to tell the ``contains``
|
851 |
+
method that the number of symbols the group is on does not need to
|
852 |
+
match perfectly the number of symbols for the permutation:
|
853 |
+
|
854 |
+
>>> G.contains(p2,strict=False)
|
855 |
+
True
|
856 |
+
|
857 |
+
This can be via the ``strict`` argument to the ``contains`` method,
|
858 |
+
and SymPy will try to extend the permutation on its own and then
|
859 |
+
perform the containment check.
|
860 |
+
|
861 |
+
See Also
|
862 |
+
========
|
863 |
+
|
864 |
+
Cycle
|
865 |
+
|
866 |
+
References
|
867 |
+
==========
|
868 |
+
|
869 |
+
.. [1] Skiena, S. 'Permutations.' 1.1 in Implementing Discrete Mathematics
|
870 |
+
Combinatorics and Graph Theory with Mathematica. Reading, MA:
|
871 |
+
Addison-Wesley, pp. 3-16, 1990.
|
872 |
+
|
873 |
+
.. [2] Knuth, D. E. The Art of Computer Programming, Vol. 4: Combinatorial
|
874 |
+
Algorithms, 1st ed. Reading, MA: Addison-Wesley, 2011.
|
875 |
+
|
876 |
+
.. [3] Wendy Myrvold and Frank Ruskey. 2001. Ranking and unranking
|
877 |
+
permutations in linear time. Inf. Process. Lett. 79, 6 (September 2001),
|
878 |
+
281-284. DOI=10.1016/S0020-0190(01)00141-7
|
879 |
+
|
880 |
+
.. [4] D. L. Kreher, D. R. Stinson 'Combinatorial Algorithms'
|
881 |
+
CRC Press, 1999
|
882 |
+
|
883 |
+
.. [5] Graham, R. L.; Knuth, D. E.; and Patashnik, O.
|
884 |
+
Concrete Mathematics: A Foundation for Computer Science, 2nd ed.
|
885 |
+
Reading, MA: Addison-Wesley, 1994.
|
886 |
+
|
887 |
+
.. [6] https://en.wikipedia.org/w/index.php?oldid=499948155#Product_and_inverse
|
888 |
+
|
889 |
+
.. [7] https://en.wikipedia.org/wiki/Lehmer_code
|
890 |
+
|
891 |
+
"""
|
892 |
+
|
893 |
+
is_Permutation = True
|
894 |
+
|
895 |
+
_array_form = None
|
896 |
+
_cyclic_form = None
|
897 |
+
_cycle_structure = None
|
898 |
+
_size = None
|
899 |
+
_rank = None
|
900 |
+
|
901 |
+
def __new__(cls, *args, size=None, **kwargs):
|
902 |
+
"""
|
903 |
+
Constructor for the Permutation object from a list or a
|
904 |
+
list of lists in which all elements of the permutation may
|
905 |
+
appear only once.
|
906 |
+
|
907 |
+
Examples
|
908 |
+
========
|
909 |
+
|
910 |
+
>>> from sympy.combinatorics import Permutation
|
911 |
+
>>> from sympy import init_printing
|
912 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
913 |
+
|
914 |
+
Permutations entered in array-form are left unaltered:
|
915 |
+
|
916 |
+
>>> Permutation([0, 2, 1])
|
917 |
+
Permutation([0, 2, 1])
|
918 |
+
|
919 |
+
Permutations entered in cyclic form are converted to array form;
|
920 |
+
singletons need not be entered, but can be entered to indicate the
|
921 |
+
largest element:
|
922 |
+
|
923 |
+
>>> Permutation([[4, 5, 6], [0, 1]])
|
924 |
+
Permutation([1, 0, 2, 3, 5, 6, 4])
|
925 |
+
>>> Permutation([[4, 5, 6], [0, 1], [19]])
|
926 |
+
Permutation([1, 0, 2, 3, 5, 6, 4], size=20)
|
927 |
+
|
928 |
+
All manipulation of permutations assumes that the smallest element
|
929 |
+
is 0 (in keeping with 0-based indexing in Python) so if the 0 is
|
930 |
+
missing when entering a permutation in array form, an error will be
|
931 |
+
raised:
|
932 |
+
|
933 |
+
>>> Permutation([2, 1])
|
934 |
+
Traceback (most recent call last):
|
935 |
+
...
|
936 |
+
ValueError: Integers 0 through 2 must be present.
|
937 |
+
|
938 |
+
If a permutation is entered in cyclic form, it can be entered without
|
939 |
+
singletons and the ``size`` specified so those values can be filled
|
940 |
+
in, otherwise the array form will only extend to the maximum value
|
941 |
+
in the cycles:
|
942 |
+
|
943 |
+
>>> Permutation([[1, 4], [3, 5, 2]], size=10)
|
944 |
+
Permutation([0, 4, 3, 5, 1, 2], size=10)
|
945 |
+
>>> _.array_form
|
946 |
+
[0, 4, 3, 5, 1, 2, 6, 7, 8, 9]
|
947 |
+
"""
|
948 |
+
if size is not None:
|
949 |
+
size = int(size)
|
950 |
+
|
951 |
+
#a) ()
|
952 |
+
#b) (1) = identity
|
953 |
+
#c) (1, 2) = cycle
|
954 |
+
#d) ([1, 2, 3]) = array form
|
955 |
+
#e) ([[1, 2]]) = cyclic form
|
956 |
+
#f) (Cycle) = conversion to permutation
|
957 |
+
#g) (Permutation) = adjust size or return copy
|
958 |
+
ok = True
|
959 |
+
if not args: # a
|
960 |
+
return cls._af_new(list(range(size or 0)))
|
961 |
+
elif len(args) > 1: # c
|
962 |
+
return cls._af_new(Cycle(*args).list(size))
|
963 |
+
if len(args) == 1:
|
964 |
+
a = args[0]
|
965 |
+
if isinstance(a, cls): # g
|
966 |
+
if size is None or size == a.size:
|
967 |
+
return a
|
968 |
+
return cls(a.array_form, size=size)
|
969 |
+
if isinstance(a, Cycle): # f
|
970 |
+
return cls._af_new(a.list(size))
|
971 |
+
if not is_sequence(a): # b
|
972 |
+
if size is not None and a + 1 > size:
|
973 |
+
raise ValueError('size is too small when max is %s' % a)
|
974 |
+
return cls._af_new(list(range(a + 1)))
|
975 |
+
if has_variety(is_sequence(ai) for ai in a):
|
976 |
+
ok = False
|
977 |
+
else:
|
978 |
+
ok = False
|
979 |
+
if not ok:
|
980 |
+
raise ValueError("Permutation argument must be a list of ints, "
|
981 |
+
"a list of lists, Permutation or Cycle.")
|
982 |
+
|
983 |
+
# safe to assume args are valid; this also makes a copy
|
984 |
+
# of the args
|
985 |
+
args = list(args[0])
|
986 |
+
|
987 |
+
is_cycle = args and is_sequence(args[0])
|
988 |
+
if is_cycle: # e
|
989 |
+
args = [[int(i) for i in c] for c in args]
|
990 |
+
else: # d
|
991 |
+
args = [int(i) for i in args]
|
992 |
+
|
993 |
+
# if there are n elements present, 0, 1, ..., n-1 should be present
|
994 |
+
# unless a cycle notation has been provided. A 0 will be added
|
995 |
+
# for convenience in case one wants to enter permutations where
|
996 |
+
# counting starts from 1.
|
997 |
+
|
998 |
+
temp = flatten(args)
|
999 |
+
if has_dups(temp) and not is_cycle:
|
1000 |
+
raise ValueError('there were repeated elements.')
|
1001 |
+
temp = set(temp)
|
1002 |
+
|
1003 |
+
if not is_cycle:
|
1004 |
+
if temp != set(range(len(temp))):
|
1005 |
+
raise ValueError('Integers 0 through %s must be present.' %
|
1006 |
+
max(temp))
|
1007 |
+
if size is not None and temp and max(temp) + 1 > size:
|
1008 |
+
raise ValueError('max element should not exceed %s' % (size - 1))
|
1009 |
+
|
1010 |
+
if is_cycle:
|
1011 |
+
# it's not necessarily canonical so we won't store
|
1012 |
+
# it -- use the array form instead
|
1013 |
+
c = Cycle()
|
1014 |
+
for ci in args:
|
1015 |
+
c = c(*ci)
|
1016 |
+
aform = c.list()
|
1017 |
+
else:
|
1018 |
+
aform = list(args)
|
1019 |
+
if size and size > len(aform):
|
1020 |
+
# don't allow for truncation of permutation which
|
1021 |
+
# might split a cycle and lead to an invalid aform
|
1022 |
+
# but do allow the permutation size to be increased
|
1023 |
+
aform.extend(list(range(len(aform), size)))
|
1024 |
+
|
1025 |
+
return cls._af_new(aform)
|
1026 |
+
|
1027 |
+
@classmethod
|
1028 |
+
def _af_new(cls, perm):
|
1029 |
+
"""A method to produce a Permutation object from a list;
|
1030 |
+
the list is bound to the _array_form attribute, so it must
|
1031 |
+
not be modified; this method is meant for internal use only;
|
1032 |
+
the list ``a`` is supposed to be generated as a temporary value
|
1033 |
+
in a method, so p = Perm._af_new(a) is the only object
|
1034 |
+
to hold a reference to ``a``::
|
1035 |
+
|
1036 |
+
Examples
|
1037 |
+
========
|
1038 |
+
|
1039 |
+
>>> from sympy.combinatorics.permutations import Perm
|
1040 |
+
>>> from sympy import init_printing
|
1041 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
1042 |
+
>>> a = [2, 1, 3, 0]
|
1043 |
+
>>> p = Perm._af_new(a)
|
1044 |
+
>>> p
|
1045 |
+
Permutation([2, 1, 3, 0])
|
1046 |
+
|
1047 |
+
"""
|
1048 |
+
p = super().__new__(cls)
|
1049 |
+
p._array_form = perm
|
1050 |
+
p._size = len(perm)
|
1051 |
+
return p
|
1052 |
+
|
1053 |
+
def _hashable_content(self):
|
1054 |
+
# the array_form (a list) is the Permutation arg, so we need to
|
1055 |
+
# return a tuple, instead
|
1056 |
+
return tuple(self.array_form)
|
1057 |
+
|
1058 |
+
@property
|
1059 |
+
def array_form(self):
|
1060 |
+
"""
|
1061 |
+
Return a copy of the attribute _array_form
|
1062 |
+
Examples
|
1063 |
+
========
|
1064 |
+
|
1065 |
+
>>> from sympy.combinatorics import Permutation
|
1066 |
+
>>> p = Permutation([[2, 0], [3, 1]])
|
1067 |
+
>>> p.array_form
|
1068 |
+
[2, 3, 0, 1]
|
1069 |
+
>>> Permutation([[2, 0, 3, 1]]).array_form
|
1070 |
+
[3, 2, 0, 1]
|
1071 |
+
>>> Permutation([2, 0, 3, 1]).array_form
|
1072 |
+
[2, 0, 3, 1]
|
1073 |
+
>>> Permutation([[1, 2], [4, 5]]).array_form
|
1074 |
+
[0, 2, 1, 3, 5, 4]
|
1075 |
+
"""
|
1076 |
+
return self._array_form[:]
|
1077 |
+
|
1078 |
+
def list(self, size=None):
|
1079 |
+
"""Return the permutation as an explicit list, possibly
|
1080 |
+
trimming unmoved elements if size is less than the maximum
|
1081 |
+
element in the permutation; if this is desired, setting
|
1082 |
+
``size=-1`` will guarantee such trimming.
|
1083 |
+
|
1084 |
+
Examples
|
1085 |
+
========
|
1086 |
+
|
1087 |
+
>>> from sympy.combinatorics import Permutation
|
1088 |
+
>>> p = Permutation(2, 3)(4, 5)
|
1089 |
+
>>> p.list()
|
1090 |
+
[0, 1, 3, 2, 5, 4]
|
1091 |
+
>>> p.list(10)
|
1092 |
+
[0, 1, 3, 2, 5, 4, 6, 7, 8, 9]
|
1093 |
+
|
1094 |
+
Passing a length too small will trim trailing, unchanged elements
|
1095 |
+
in the permutation:
|
1096 |
+
|
1097 |
+
>>> Permutation(2, 4)(1, 2, 4).list(-1)
|
1098 |
+
[0, 2, 1]
|
1099 |
+
>>> Permutation(3).list(-1)
|
1100 |
+
[]
|
1101 |
+
"""
|
1102 |
+
if not self and size is None:
|
1103 |
+
raise ValueError('must give size for empty Cycle')
|
1104 |
+
rv = self.array_form
|
1105 |
+
if size is not None:
|
1106 |
+
if size > self.size:
|
1107 |
+
rv.extend(list(range(self.size, size)))
|
1108 |
+
else:
|
1109 |
+
# find first value from rhs where rv[i] != i
|
1110 |
+
i = self.size - 1
|
1111 |
+
while rv:
|
1112 |
+
if rv[-1] != i:
|
1113 |
+
break
|
1114 |
+
rv.pop()
|
1115 |
+
i -= 1
|
1116 |
+
return rv
|
1117 |
+
|
1118 |
+
@property
|
1119 |
+
def cyclic_form(self):
|
1120 |
+
"""
|
1121 |
+
This is used to convert to the cyclic notation
|
1122 |
+
from the canonical notation. Singletons are omitted.
|
1123 |
+
|
1124 |
+
Examples
|
1125 |
+
========
|
1126 |
+
|
1127 |
+
>>> from sympy.combinatorics import Permutation
|
1128 |
+
>>> p = Permutation([0, 3, 1, 2])
|
1129 |
+
>>> p.cyclic_form
|
1130 |
+
[[1, 3, 2]]
|
1131 |
+
>>> Permutation([1, 0, 2, 4, 3, 5]).cyclic_form
|
1132 |
+
[[0, 1], [3, 4]]
|
1133 |
+
|
1134 |
+
See Also
|
1135 |
+
========
|
1136 |
+
|
1137 |
+
array_form, full_cyclic_form
|
1138 |
+
"""
|
1139 |
+
if self._cyclic_form is not None:
|
1140 |
+
return list(self._cyclic_form)
|
1141 |
+
array_form = self.array_form
|
1142 |
+
unchecked = [True] * len(array_form)
|
1143 |
+
cyclic_form = []
|
1144 |
+
for i in range(len(array_form)):
|
1145 |
+
if unchecked[i]:
|
1146 |
+
cycle = []
|
1147 |
+
cycle.append(i)
|
1148 |
+
unchecked[i] = False
|
1149 |
+
j = i
|
1150 |
+
while unchecked[array_form[j]]:
|
1151 |
+
j = array_form[j]
|
1152 |
+
cycle.append(j)
|
1153 |
+
unchecked[j] = False
|
1154 |
+
if len(cycle) > 1:
|
1155 |
+
cyclic_form.append(cycle)
|
1156 |
+
assert cycle == list(minlex(cycle))
|
1157 |
+
cyclic_form.sort()
|
1158 |
+
self._cyclic_form = cyclic_form[:]
|
1159 |
+
return cyclic_form
|
1160 |
+
|
1161 |
+
@property
|
1162 |
+
def full_cyclic_form(self):
|
1163 |
+
"""Return permutation in cyclic form including singletons.
|
1164 |
+
|
1165 |
+
Examples
|
1166 |
+
========
|
1167 |
+
|
1168 |
+
>>> from sympy.combinatorics import Permutation
|
1169 |
+
>>> Permutation([0, 2, 1]).full_cyclic_form
|
1170 |
+
[[0], [1, 2]]
|
1171 |
+
"""
|
1172 |
+
need = set(range(self.size)) - set(flatten(self.cyclic_form))
|
1173 |
+
rv = self.cyclic_form + [[i] for i in need]
|
1174 |
+
rv.sort()
|
1175 |
+
return rv
|
1176 |
+
|
1177 |
+
@property
|
1178 |
+
def size(self):
|
1179 |
+
"""
|
1180 |
+
Returns the number of elements in the permutation.
|
1181 |
+
|
1182 |
+
Examples
|
1183 |
+
========
|
1184 |
+
|
1185 |
+
>>> from sympy.combinatorics import Permutation
|
1186 |
+
>>> Permutation([[3, 2], [0, 1]]).size
|
1187 |
+
4
|
1188 |
+
|
1189 |
+
See Also
|
1190 |
+
========
|
1191 |
+
|
1192 |
+
cardinality, length, order, rank
|
1193 |
+
"""
|
1194 |
+
return self._size
|
1195 |
+
|
1196 |
+
def support(self):
|
1197 |
+
"""Return the elements in permutation, P, for which P[i] != i.
|
1198 |
+
|
1199 |
+
Examples
|
1200 |
+
========
|
1201 |
+
|
1202 |
+
>>> from sympy.combinatorics import Permutation
|
1203 |
+
>>> p = Permutation([[3, 2], [0, 1], [4]])
|
1204 |
+
>>> p.array_form
|
1205 |
+
[1, 0, 3, 2, 4]
|
1206 |
+
>>> p.support()
|
1207 |
+
[0, 1, 2, 3]
|
1208 |
+
"""
|
1209 |
+
a = self.array_form
|
1210 |
+
return [i for i, e in enumerate(a) if a[i] != i]
|
1211 |
+
|
1212 |
+
def __add__(self, other):
|
1213 |
+
"""Return permutation that is other higher in rank than self.
|
1214 |
+
|
1215 |
+
The rank is the lexicographical rank, with the identity permutation
|
1216 |
+
having rank of 0.
|
1217 |
+
|
1218 |
+
Examples
|
1219 |
+
========
|
1220 |
+
|
1221 |
+
>>> from sympy.combinatorics import Permutation
|
1222 |
+
>>> I = Permutation([0, 1, 2, 3])
|
1223 |
+
>>> a = Permutation([2, 1, 3, 0])
|
1224 |
+
>>> I + a.rank() == a
|
1225 |
+
True
|
1226 |
+
|
1227 |
+
See Also
|
1228 |
+
========
|
1229 |
+
|
1230 |
+
__sub__, inversion_vector
|
1231 |
+
|
1232 |
+
"""
|
1233 |
+
rank = (self.rank() + other) % self.cardinality
|
1234 |
+
rv = self.unrank_lex(self.size, rank)
|
1235 |
+
rv._rank = rank
|
1236 |
+
return rv
|
1237 |
+
|
1238 |
+
def __sub__(self, other):
|
1239 |
+
"""Return the permutation that is other lower in rank than self.
|
1240 |
+
|
1241 |
+
See Also
|
1242 |
+
========
|
1243 |
+
|
1244 |
+
__add__
|
1245 |
+
"""
|
1246 |
+
return self.__add__(-other)
|
1247 |
+
|
1248 |
+
@staticmethod
|
1249 |
+
def rmul(*args):
|
1250 |
+
"""
|
1251 |
+
Return product of Permutations [a, b, c, ...] as the Permutation whose
|
1252 |
+
ith value is a(b(c(i))).
|
1253 |
+
|
1254 |
+
a, b, c, ... can be Permutation objects or tuples.
|
1255 |
+
|
1256 |
+
Examples
|
1257 |
+
========
|
1258 |
+
|
1259 |
+
>>> from sympy.combinatorics import Permutation
|
1260 |
+
|
1261 |
+
>>> a, b = [1, 0, 2], [0, 2, 1]
|
1262 |
+
>>> a = Permutation(a); b = Permutation(b)
|
1263 |
+
>>> list(Permutation.rmul(a, b))
|
1264 |
+
[1, 2, 0]
|
1265 |
+
>>> [a(b(i)) for i in range(3)]
|
1266 |
+
[1, 2, 0]
|
1267 |
+
|
1268 |
+
This handles the operands in reverse order compared to the ``*`` operator:
|
1269 |
+
|
1270 |
+
>>> a = Permutation(a); b = Permutation(b)
|
1271 |
+
>>> list(a*b)
|
1272 |
+
[2, 0, 1]
|
1273 |
+
>>> [b(a(i)) for i in range(3)]
|
1274 |
+
[2, 0, 1]
|
1275 |
+
|
1276 |
+
Notes
|
1277 |
+
=====
|
1278 |
+
|
1279 |
+
All items in the sequence will be parsed by Permutation as
|
1280 |
+
necessary as long as the first item is a Permutation:
|
1281 |
+
|
1282 |
+
>>> Permutation.rmul(a, [0, 2, 1]) == Permutation.rmul(a, b)
|
1283 |
+
True
|
1284 |
+
|
1285 |
+
The reverse order of arguments will raise a TypeError.
|
1286 |
+
|
1287 |
+
"""
|
1288 |
+
rv = args[0]
|
1289 |
+
for i in range(1, len(args)):
|
1290 |
+
rv = args[i]*rv
|
1291 |
+
return rv
|
1292 |
+
|
1293 |
+
@classmethod
|
1294 |
+
def rmul_with_af(cls, *args):
|
1295 |
+
"""
|
1296 |
+
same as rmul, but the elements of args are Permutation objects
|
1297 |
+
which have _array_form
|
1298 |
+
"""
|
1299 |
+
a = [x._array_form for x in args]
|
1300 |
+
rv = cls._af_new(_af_rmuln(*a))
|
1301 |
+
return rv
|
1302 |
+
|
1303 |
+
def mul_inv(self, other):
|
1304 |
+
"""
|
1305 |
+
other*~self, self and other have _array_form
|
1306 |
+
"""
|
1307 |
+
a = _af_invert(self._array_form)
|
1308 |
+
b = other._array_form
|
1309 |
+
return self._af_new(_af_rmul(a, b))
|
1310 |
+
|
1311 |
+
def __rmul__(self, other):
|
1312 |
+
"""This is needed to coerce other to Permutation in rmul."""
|
1313 |
+
cls = type(self)
|
1314 |
+
return cls(other)*self
|
1315 |
+
|
1316 |
+
def __mul__(self, other):
|
1317 |
+
"""
|
1318 |
+
Return the product a*b as a Permutation; the ith value is b(a(i)).
|
1319 |
+
|
1320 |
+
Examples
|
1321 |
+
========
|
1322 |
+
|
1323 |
+
>>> from sympy.combinatorics.permutations import _af_rmul, Permutation
|
1324 |
+
|
1325 |
+
>>> a, b = [1, 0, 2], [0, 2, 1]
|
1326 |
+
>>> a = Permutation(a); b = Permutation(b)
|
1327 |
+
>>> list(a*b)
|
1328 |
+
[2, 0, 1]
|
1329 |
+
>>> [b(a(i)) for i in range(3)]
|
1330 |
+
[2, 0, 1]
|
1331 |
+
|
1332 |
+
This handles operands in reverse order compared to _af_rmul and rmul:
|
1333 |
+
|
1334 |
+
>>> al = list(a); bl = list(b)
|
1335 |
+
>>> _af_rmul(al, bl)
|
1336 |
+
[1, 2, 0]
|
1337 |
+
>>> [al[bl[i]] for i in range(3)]
|
1338 |
+
[1, 2, 0]
|
1339 |
+
|
1340 |
+
It is acceptable for the arrays to have different lengths; the shorter
|
1341 |
+
one will be padded to match the longer one:
|
1342 |
+
|
1343 |
+
>>> from sympy import init_printing
|
1344 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
1345 |
+
>>> b*Permutation([1, 0])
|
1346 |
+
Permutation([1, 2, 0])
|
1347 |
+
>>> Permutation([1, 0])*b
|
1348 |
+
Permutation([2, 0, 1])
|
1349 |
+
|
1350 |
+
It is also acceptable to allow coercion to handle conversion of a
|
1351 |
+
single list to the left of a Permutation:
|
1352 |
+
|
1353 |
+
>>> [0, 1]*a # no change: 2-element identity
|
1354 |
+
Permutation([1, 0, 2])
|
1355 |
+
>>> [[0, 1]]*a # exchange first two elements
|
1356 |
+
Permutation([0, 1, 2])
|
1357 |
+
|
1358 |
+
You cannot use more than 1 cycle notation in a product of cycles
|
1359 |
+
since coercion can only handle one argument to the left. To handle
|
1360 |
+
multiple cycles it is convenient to use Cycle instead of Permutation:
|
1361 |
+
|
1362 |
+
>>> [[1, 2]]*[[2, 3]]*Permutation([]) # doctest: +SKIP
|
1363 |
+
>>> from sympy.combinatorics.permutations import Cycle
|
1364 |
+
>>> Cycle(1, 2)(2, 3)
|
1365 |
+
(1 3 2)
|
1366 |
+
|
1367 |
+
"""
|
1368 |
+
from sympy.combinatorics.perm_groups import PermutationGroup, Coset
|
1369 |
+
if isinstance(other, PermutationGroup):
|
1370 |
+
return Coset(self, other, dir='-')
|
1371 |
+
a = self.array_form
|
1372 |
+
# __rmul__ makes sure the other is a Permutation
|
1373 |
+
b = other.array_form
|
1374 |
+
if not b:
|
1375 |
+
perm = a
|
1376 |
+
else:
|
1377 |
+
b.extend(list(range(len(b), len(a))))
|
1378 |
+
perm = [b[i] for i in a] + b[len(a):]
|
1379 |
+
return self._af_new(perm)
|
1380 |
+
|
1381 |
+
def commutes_with(self, other):
|
1382 |
+
"""
|
1383 |
+
Checks if the elements are commuting.
|
1384 |
+
|
1385 |
+
Examples
|
1386 |
+
========
|
1387 |
+
|
1388 |
+
>>> from sympy.combinatorics import Permutation
|
1389 |
+
>>> a = Permutation([1, 4, 3, 0, 2, 5])
|
1390 |
+
>>> b = Permutation([0, 1, 2, 3, 4, 5])
|
1391 |
+
>>> a.commutes_with(b)
|
1392 |
+
True
|
1393 |
+
>>> b = Permutation([2, 3, 5, 4, 1, 0])
|
1394 |
+
>>> a.commutes_with(b)
|
1395 |
+
False
|
1396 |
+
"""
|
1397 |
+
a = self.array_form
|
1398 |
+
b = other.array_form
|
1399 |
+
return _af_commutes_with(a, b)
|
1400 |
+
|
1401 |
+
def __pow__(self, n):
|
1402 |
+
"""
|
1403 |
+
Routine for finding powers of a permutation.
|
1404 |
+
|
1405 |
+
Examples
|
1406 |
+
========
|
1407 |
+
|
1408 |
+
>>> from sympy.combinatorics import Permutation
|
1409 |
+
>>> from sympy import init_printing
|
1410 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
1411 |
+
>>> p = Permutation([2, 0, 3, 1])
|
1412 |
+
>>> p.order()
|
1413 |
+
4
|
1414 |
+
>>> p**4
|
1415 |
+
Permutation([0, 1, 2, 3])
|
1416 |
+
"""
|
1417 |
+
if isinstance(n, Permutation):
|
1418 |
+
raise NotImplementedError(
|
1419 |
+
'p**p is not defined; do you mean p^p (conjugate)?')
|
1420 |
+
n = int(n)
|
1421 |
+
return self._af_new(_af_pow(self.array_form, n))
|
1422 |
+
|
1423 |
+
def __rxor__(self, i):
|
1424 |
+
"""Return self(i) when ``i`` is an int.
|
1425 |
+
|
1426 |
+
Examples
|
1427 |
+
========
|
1428 |
+
|
1429 |
+
>>> from sympy.combinatorics import Permutation
|
1430 |
+
>>> p = Permutation(1, 2, 9)
|
1431 |
+
>>> 2^p == p(2) == 9
|
1432 |
+
True
|
1433 |
+
"""
|
1434 |
+
if int(i) == i:
|
1435 |
+
return self(i)
|
1436 |
+
else:
|
1437 |
+
raise NotImplementedError(
|
1438 |
+
"i^p = p(i) when i is an integer, not %s." % i)
|
1439 |
+
|
1440 |
+
def __xor__(self, h):
|
1441 |
+
"""Return the conjugate permutation ``~h*self*h` `.
|
1442 |
+
|
1443 |
+
Explanation
|
1444 |
+
===========
|
1445 |
+
|
1446 |
+
If ``a`` and ``b`` are conjugates, ``a = h*b*~h`` and
|
1447 |
+
``b = ~h*a*h`` and both have the same cycle structure.
|
1448 |
+
|
1449 |
+
Examples
|
1450 |
+
========
|
1451 |
+
|
1452 |
+
>>> from sympy.combinatorics import Permutation
|
1453 |
+
>>> p = Permutation(1, 2, 9)
|
1454 |
+
>>> q = Permutation(6, 9, 8)
|
1455 |
+
>>> p*q != q*p
|
1456 |
+
True
|
1457 |
+
|
1458 |
+
Calculate and check properties of the conjugate:
|
1459 |
+
|
1460 |
+
>>> c = p^q
|
1461 |
+
>>> c == ~q*p*q and p == q*c*~q
|
1462 |
+
True
|
1463 |
+
|
1464 |
+
The expression q^p^r is equivalent to q^(p*r):
|
1465 |
+
|
1466 |
+
>>> r = Permutation(9)(4, 6, 8)
|
1467 |
+
>>> q^p^r == q^(p*r)
|
1468 |
+
True
|
1469 |
+
|
1470 |
+
If the term to the left of the conjugate operator, i, is an integer
|
1471 |
+
then this is interpreted as selecting the ith element from the
|
1472 |
+
permutation to the right:
|
1473 |
+
|
1474 |
+
>>> all(i^p == p(i) for i in range(p.size))
|
1475 |
+
True
|
1476 |
+
|
1477 |
+
Note that the * operator as higher precedence than the ^ operator:
|
1478 |
+
|
1479 |
+
>>> q^r*p^r == q^(r*p)^r == Permutation(9)(1, 6, 4)
|
1480 |
+
True
|
1481 |
+
|
1482 |
+
Notes
|
1483 |
+
=====
|
1484 |
+
|
1485 |
+
In Python the precedence rule is p^q^r = (p^q)^r which differs
|
1486 |
+
in general from p^(q^r)
|
1487 |
+
|
1488 |
+
>>> q^p^r
|
1489 |
+
(9)(1 4 8)
|
1490 |
+
>>> q^(p^r)
|
1491 |
+
(9)(1 8 6)
|
1492 |
+
|
1493 |
+
For a given r and p, both of the following are conjugates of p:
|
1494 |
+
~r*p*r and r*p*~r. But these are not necessarily the same:
|
1495 |
+
|
1496 |
+
>>> ~r*p*r == r*p*~r
|
1497 |
+
True
|
1498 |
+
|
1499 |
+
>>> p = Permutation(1, 2, 9)(5, 6)
|
1500 |
+
>>> ~r*p*r == r*p*~r
|
1501 |
+
False
|
1502 |
+
|
1503 |
+
The conjugate ~r*p*r was chosen so that ``p^q^r`` would be equivalent
|
1504 |
+
to ``p^(q*r)`` rather than ``p^(r*q)``. To obtain r*p*~r, pass ~r to
|
1505 |
+
this method:
|
1506 |
+
|
1507 |
+
>>> p^~r == r*p*~r
|
1508 |
+
True
|
1509 |
+
"""
|
1510 |
+
|
1511 |
+
if self.size != h.size:
|
1512 |
+
raise ValueError("The permutations must be of equal size.")
|
1513 |
+
a = [None]*self.size
|
1514 |
+
h = h._array_form
|
1515 |
+
p = self._array_form
|
1516 |
+
for i in range(self.size):
|
1517 |
+
a[h[i]] = h[p[i]]
|
1518 |
+
return self._af_new(a)
|
1519 |
+
|
1520 |
+
def transpositions(self):
|
1521 |
+
"""
|
1522 |
+
Return the permutation decomposed into a list of transpositions.
|
1523 |
+
|
1524 |
+
Explanation
|
1525 |
+
===========
|
1526 |
+
|
1527 |
+
It is always possible to express a permutation as the product of
|
1528 |
+
transpositions, see [1]
|
1529 |
+
|
1530 |
+
Examples
|
1531 |
+
========
|
1532 |
+
|
1533 |
+
>>> from sympy.combinatorics import Permutation
|
1534 |
+
>>> p = Permutation([[1, 2, 3], [0, 4, 5, 6, 7]])
|
1535 |
+
>>> t = p.transpositions()
|
1536 |
+
>>> t
|
1537 |
+
[(0, 7), (0, 6), (0, 5), (0, 4), (1, 3), (1, 2)]
|
1538 |
+
>>> print(''.join(str(c) for c in t))
|
1539 |
+
(0, 7)(0, 6)(0, 5)(0, 4)(1, 3)(1, 2)
|
1540 |
+
>>> Permutation.rmul(*[Permutation([ti], size=p.size) for ti in t]) == p
|
1541 |
+
True
|
1542 |
+
|
1543 |
+
References
|
1544 |
+
==========
|
1545 |
+
|
1546 |
+
.. [1] https://en.wikipedia.org/wiki/Transposition_%28mathematics%29#Properties
|
1547 |
+
|
1548 |
+
"""
|
1549 |
+
a = self.cyclic_form
|
1550 |
+
res = []
|
1551 |
+
for x in a:
|
1552 |
+
nx = len(x)
|
1553 |
+
if nx == 2:
|
1554 |
+
res.append(tuple(x))
|
1555 |
+
elif nx > 2:
|
1556 |
+
first = x[0]
|
1557 |
+
for y in x[nx - 1:0:-1]:
|
1558 |
+
res.append((first, y))
|
1559 |
+
return res
|
1560 |
+
|
1561 |
+
@classmethod
|
1562 |
+
def from_sequence(self, i, key=None):
|
1563 |
+
"""Return the permutation needed to obtain ``i`` from the sorted
|
1564 |
+
elements of ``i``. If custom sorting is desired, a key can be given.
|
1565 |
+
|
1566 |
+
Examples
|
1567 |
+
========
|
1568 |
+
|
1569 |
+
>>> from sympy.combinatorics import Permutation
|
1570 |
+
|
1571 |
+
>>> Permutation.from_sequence('SymPy')
|
1572 |
+
(4)(0 1 3)
|
1573 |
+
>>> _(sorted("SymPy"))
|
1574 |
+
['S', 'y', 'm', 'P', 'y']
|
1575 |
+
>>> Permutation.from_sequence('SymPy', key=lambda x: x.lower())
|
1576 |
+
(4)(0 2)(1 3)
|
1577 |
+
"""
|
1578 |
+
ic = list(zip(i, list(range(len(i)))))
|
1579 |
+
if key:
|
1580 |
+
ic.sort(key=lambda x: key(x[0]))
|
1581 |
+
else:
|
1582 |
+
ic.sort()
|
1583 |
+
return ~Permutation([i[1] for i in ic])
|
1584 |
+
|
1585 |
+
def __invert__(self):
|
1586 |
+
"""
|
1587 |
+
Return the inverse of the permutation.
|
1588 |
+
|
1589 |
+
A permutation multiplied by its inverse is the identity permutation.
|
1590 |
+
|
1591 |
+
Examples
|
1592 |
+
========
|
1593 |
+
|
1594 |
+
>>> from sympy.combinatorics import Permutation
|
1595 |
+
>>> from sympy import init_printing
|
1596 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
1597 |
+
>>> p = Permutation([[2, 0], [3, 1]])
|
1598 |
+
>>> ~p
|
1599 |
+
Permutation([2, 3, 0, 1])
|
1600 |
+
>>> _ == p**-1
|
1601 |
+
True
|
1602 |
+
>>> p*~p == ~p*p == Permutation([0, 1, 2, 3])
|
1603 |
+
True
|
1604 |
+
"""
|
1605 |
+
return self._af_new(_af_invert(self._array_form))
|
1606 |
+
|
1607 |
+
def __iter__(self):
|
1608 |
+
"""Yield elements from array form.
|
1609 |
+
|
1610 |
+
Examples
|
1611 |
+
========
|
1612 |
+
|
1613 |
+
>>> from sympy.combinatorics import Permutation
|
1614 |
+
>>> list(Permutation(range(3)))
|
1615 |
+
[0, 1, 2]
|
1616 |
+
"""
|
1617 |
+
yield from self.array_form
|
1618 |
+
|
1619 |
+
def __repr__(self):
|
1620 |
+
return srepr(self)
|
1621 |
+
|
1622 |
+
def __call__(self, *i):
|
1623 |
+
"""
|
1624 |
+
Allows applying a permutation instance as a bijective function.
|
1625 |
+
|
1626 |
+
Examples
|
1627 |
+
========
|
1628 |
+
|
1629 |
+
>>> from sympy.combinatorics import Permutation
|
1630 |
+
>>> p = Permutation([[2, 0], [3, 1]])
|
1631 |
+
>>> p.array_form
|
1632 |
+
[2, 3, 0, 1]
|
1633 |
+
>>> [p(i) for i in range(4)]
|
1634 |
+
[2, 3, 0, 1]
|
1635 |
+
|
1636 |
+
If an array is given then the permutation selects the items
|
1637 |
+
from the array (i.e. the permutation is applied to the array):
|
1638 |
+
|
1639 |
+
>>> from sympy.abc import x
|
1640 |
+
>>> p([x, 1, 0, x**2])
|
1641 |
+
[0, x**2, x, 1]
|
1642 |
+
"""
|
1643 |
+
# list indices can be Integer or int; leave this
|
1644 |
+
# as it is (don't test or convert it) because this
|
1645 |
+
# gets called a lot and should be fast
|
1646 |
+
if len(i) == 1:
|
1647 |
+
i = i[0]
|
1648 |
+
if not isinstance(i, Iterable):
|
1649 |
+
i = as_int(i)
|
1650 |
+
if i < 0 or i > self.size:
|
1651 |
+
raise TypeError(
|
1652 |
+
"{} should be an integer between 0 and {}"
|
1653 |
+
.format(i, self.size-1))
|
1654 |
+
return self._array_form[i]
|
1655 |
+
# P([a, b, c])
|
1656 |
+
if len(i) != self.size:
|
1657 |
+
raise TypeError(
|
1658 |
+
"{} should have the length {}.".format(i, self.size))
|
1659 |
+
return [i[j] for j in self._array_form]
|
1660 |
+
# P(1, 2, 3)
|
1661 |
+
return self*Permutation(Cycle(*i), size=self.size)
|
1662 |
+
|
1663 |
+
def atoms(self):
|
1664 |
+
"""
|
1665 |
+
Returns all the elements of a permutation
|
1666 |
+
|
1667 |
+
Examples
|
1668 |
+
========
|
1669 |
+
|
1670 |
+
>>> from sympy.combinatorics import Permutation
|
1671 |
+
>>> Permutation([0, 1, 2, 3, 4, 5]).atoms()
|
1672 |
+
{0, 1, 2, 3, 4, 5}
|
1673 |
+
>>> Permutation([[0, 1], [2, 3], [4, 5]]).atoms()
|
1674 |
+
{0, 1, 2, 3, 4, 5}
|
1675 |
+
"""
|
1676 |
+
return set(self.array_form)
|
1677 |
+
|
1678 |
+
def apply(self, i):
|
1679 |
+
r"""Apply the permutation to an expression.
|
1680 |
+
|
1681 |
+
Parameters
|
1682 |
+
==========
|
1683 |
+
|
1684 |
+
i : Expr
|
1685 |
+
It should be an integer between $0$ and $n-1$ where $n$
|
1686 |
+
is the size of the permutation.
|
1687 |
+
|
1688 |
+
If it is a symbol or a symbolic expression that can
|
1689 |
+
have integer values, an ``AppliedPermutation`` object
|
1690 |
+
will be returned which can represent an unevaluated
|
1691 |
+
function.
|
1692 |
+
|
1693 |
+
Notes
|
1694 |
+
=====
|
1695 |
+
|
1696 |
+
Any permutation can be defined as a bijective function
|
1697 |
+
$\sigma : \{ 0, 1, \dots, n-1 \} \rightarrow \{ 0, 1, \dots, n-1 \}$
|
1698 |
+
where $n$ denotes the size of the permutation.
|
1699 |
+
|
1700 |
+
The definition may even be extended for any set with distinctive
|
1701 |
+
elements, such that the permutation can even be applied for
|
1702 |
+
real numbers or such, however, it is not implemented for now for
|
1703 |
+
computational reasons and the integrity with the group theory
|
1704 |
+
module.
|
1705 |
+
|
1706 |
+
This function is similar to the ``__call__`` magic, however,
|
1707 |
+
``__call__`` magic already has some other applications like
|
1708 |
+
permuting an array or attaching new cycles, which would
|
1709 |
+
not always be mathematically consistent.
|
1710 |
+
|
1711 |
+
This also guarantees that the return type is a SymPy integer,
|
1712 |
+
which guarantees the safety to use assumptions.
|
1713 |
+
"""
|
1714 |
+
i = _sympify(i)
|
1715 |
+
if i.is_integer is False:
|
1716 |
+
raise NotImplementedError("{} should be an integer.".format(i))
|
1717 |
+
|
1718 |
+
n = self.size
|
1719 |
+
if (i < 0) == True or (i >= n) == True:
|
1720 |
+
raise NotImplementedError(
|
1721 |
+
"{} should be an integer between 0 and {}".format(i, n-1))
|
1722 |
+
|
1723 |
+
if i.is_Integer:
|
1724 |
+
return Integer(self._array_form[i])
|
1725 |
+
return AppliedPermutation(self, i)
|
1726 |
+
|
1727 |
+
def next_lex(self):
|
1728 |
+
"""
|
1729 |
+
Returns the next permutation in lexicographical order.
|
1730 |
+
If self is the last permutation in lexicographical order
|
1731 |
+
it returns None.
|
1732 |
+
See [4] section 2.4.
|
1733 |
+
|
1734 |
+
|
1735 |
+
Examples
|
1736 |
+
========
|
1737 |
+
|
1738 |
+
>>> from sympy.combinatorics import Permutation
|
1739 |
+
>>> p = Permutation([2, 3, 1, 0])
|
1740 |
+
>>> p = Permutation([2, 3, 1, 0]); p.rank()
|
1741 |
+
17
|
1742 |
+
>>> p = p.next_lex(); p.rank()
|
1743 |
+
18
|
1744 |
+
|
1745 |
+
See Also
|
1746 |
+
========
|
1747 |
+
|
1748 |
+
rank, unrank_lex
|
1749 |
+
"""
|
1750 |
+
perm = self.array_form[:]
|
1751 |
+
n = len(perm)
|
1752 |
+
i = n - 2
|
1753 |
+
while perm[i + 1] < perm[i]:
|
1754 |
+
i -= 1
|
1755 |
+
if i == -1:
|
1756 |
+
return None
|
1757 |
+
else:
|
1758 |
+
j = n - 1
|
1759 |
+
while perm[j] < perm[i]:
|
1760 |
+
j -= 1
|
1761 |
+
perm[j], perm[i] = perm[i], perm[j]
|
1762 |
+
i += 1
|
1763 |
+
j = n - 1
|
1764 |
+
while i < j:
|
1765 |
+
perm[j], perm[i] = perm[i], perm[j]
|
1766 |
+
i += 1
|
1767 |
+
j -= 1
|
1768 |
+
return self._af_new(perm)
|
1769 |
+
|
1770 |
+
@classmethod
|
1771 |
+
def unrank_nonlex(self, n, r):
|
1772 |
+
"""
|
1773 |
+
This is a linear time unranking algorithm that does not
|
1774 |
+
respect lexicographic order [3].
|
1775 |
+
|
1776 |
+
Examples
|
1777 |
+
========
|
1778 |
+
|
1779 |
+
>>> from sympy.combinatorics import Permutation
|
1780 |
+
>>> from sympy import init_printing
|
1781 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
1782 |
+
>>> Permutation.unrank_nonlex(4, 5)
|
1783 |
+
Permutation([2, 0, 3, 1])
|
1784 |
+
>>> Permutation.unrank_nonlex(4, -1)
|
1785 |
+
Permutation([0, 1, 2, 3])
|
1786 |
+
|
1787 |
+
See Also
|
1788 |
+
========
|
1789 |
+
|
1790 |
+
next_nonlex, rank_nonlex
|
1791 |
+
"""
|
1792 |
+
def _unrank1(n, r, a):
|
1793 |
+
if n > 0:
|
1794 |
+
a[n - 1], a[r % n] = a[r % n], a[n - 1]
|
1795 |
+
_unrank1(n - 1, r//n, a)
|
1796 |
+
|
1797 |
+
id_perm = list(range(n))
|
1798 |
+
n = int(n)
|
1799 |
+
r = r % ifac(n)
|
1800 |
+
_unrank1(n, r, id_perm)
|
1801 |
+
return self._af_new(id_perm)
|
1802 |
+
|
1803 |
+
def rank_nonlex(self, inv_perm=None):
|
1804 |
+
"""
|
1805 |
+
This is a linear time ranking algorithm that does not
|
1806 |
+
enforce lexicographic order [3].
|
1807 |
+
|
1808 |
+
|
1809 |
+
Examples
|
1810 |
+
========
|
1811 |
+
|
1812 |
+
>>> from sympy.combinatorics import Permutation
|
1813 |
+
>>> p = Permutation([0, 1, 2, 3])
|
1814 |
+
>>> p.rank_nonlex()
|
1815 |
+
23
|
1816 |
+
|
1817 |
+
See Also
|
1818 |
+
========
|
1819 |
+
|
1820 |
+
next_nonlex, unrank_nonlex
|
1821 |
+
"""
|
1822 |
+
def _rank1(n, perm, inv_perm):
|
1823 |
+
if n == 1:
|
1824 |
+
return 0
|
1825 |
+
s = perm[n - 1]
|
1826 |
+
t = inv_perm[n - 1]
|
1827 |
+
perm[n - 1], perm[t] = perm[t], s
|
1828 |
+
inv_perm[n - 1], inv_perm[s] = inv_perm[s], t
|
1829 |
+
return s + n*_rank1(n - 1, perm, inv_perm)
|
1830 |
+
|
1831 |
+
if inv_perm is None:
|
1832 |
+
inv_perm = (~self).array_form
|
1833 |
+
if not inv_perm:
|
1834 |
+
return 0
|
1835 |
+
perm = self.array_form[:]
|
1836 |
+
r = _rank1(len(perm), perm, inv_perm)
|
1837 |
+
return r
|
1838 |
+
|
1839 |
+
def next_nonlex(self):
|
1840 |
+
"""
|
1841 |
+
Returns the next permutation in nonlex order [3].
|
1842 |
+
If self is the last permutation in this order it returns None.
|
1843 |
+
|
1844 |
+
Examples
|
1845 |
+
========
|
1846 |
+
|
1847 |
+
>>> from sympy.combinatorics import Permutation
|
1848 |
+
>>> from sympy import init_printing
|
1849 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
1850 |
+
>>> p = Permutation([2, 0, 3, 1]); p.rank_nonlex()
|
1851 |
+
5
|
1852 |
+
>>> p = p.next_nonlex(); p
|
1853 |
+
Permutation([3, 0, 1, 2])
|
1854 |
+
>>> p.rank_nonlex()
|
1855 |
+
6
|
1856 |
+
|
1857 |
+
See Also
|
1858 |
+
========
|
1859 |
+
|
1860 |
+
rank_nonlex, unrank_nonlex
|
1861 |
+
"""
|
1862 |
+
r = self.rank_nonlex()
|
1863 |
+
if r == ifac(self.size) - 1:
|
1864 |
+
return None
|
1865 |
+
return self.unrank_nonlex(self.size, r + 1)
|
1866 |
+
|
1867 |
+
def rank(self):
|
1868 |
+
"""
|
1869 |
+
Returns the lexicographic rank of the permutation.
|
1870 |
+
|
1871 |
+
Examples
|
1872 |
+
========
|
1873 |
+
|
1874 |
+
>>> from sympy.combinatorics import Permutation
|
1875 |
+
>>> p = Permutation([0, 1, 2, 3])
|
1876 |
+
>>> p.rank()
|
1877 |
+
0
|
1878 |
+
>>> p = Permutation([3, 2, 1, 0])
|
1879 |
+
>>> p.rank()
|
1880 |
+
23
|
1881 |
+
|
1882 |
+
See Also
|
1883 |
+
========
|
1884 |
+
|
1885 |
+
next_lex, unrank_lex, cardinality, length, order, size
|
1886 |
+
"""
|
1887 |
+
if self._rank is not None:
|
1888 |
+
return self._rank
|
1889 |
+
rank = 0
|
1890 |
+
rho = self.array_form[:]
|
1891 |
+
n = self.size - 1
|
1892 |
+
size = n + 1
|
1893 |
+
psize = int(ifac(n))
|
1894 |
+
for j in range(size - 1):
|
1895 |
+
rank += rho[j]*psize
|
1896 |
+
for i in range(j + 1, size):
|
1897 |
+
if rho[i] > rho[j]:
|
1898 |
+
rho[i] -= 1
|
1899 |
+
psize //= n
|
1900 |
+
n -= 1
|
1901 |
+
self._rank = rank
|
1902 |
+
return rank
|
1903 |
+
|
1904 |
+
@property
|
1905 |
+
def cardinality(self):
|
1906 |
+
"""
|
1907 |
+
Returns the number of all possible permutations.
|
1908 |
+
|
1909 |
+
Examples
|
1910 |
+
========
|
1911 |
+
|
1912 |
+
>>> from sympy.combinatorics import Permutation
|
1913 |
+
>>> p = Permutation([0, 1, 2, 3])
|
1914 |
+
>>> p.cardinality
|
1915 |
+
24
|
1916 |
+
|
1917 |
+
See Also
|
1918 |
+
========
|
1919 |
+
|
1920 |
+
length, order, rank, size
|
1921 |
+
"""
|
1922 |
+
return int(ifac(self.size))
|
1923 |
+
|
1924 |
+
def parity(self):
|
1925 |
+
"""
|
1926 |
+
Computes the parity of a permutation.
|
1927 |
+
|
1928 |
+
Explanation
|
1929 |
+
===========
|
1930 |
+
|
1931 |
+
The parity of a permutation reflects the parity of the
|
1932 |
+
number of inversions in the permutation, i.e., the
|
1933 |
+
number of pairs of x and y such that ``x > y`` but ``p[x] < p[y]``.
|
1934 |
+
|
1935 |
+
Examples
|
1936 |
+
========
|
1937 |
+
|
1938 |
+
>>> from sympy.combinatorics import Permutation
|
1939 |
+
>>> p = Permutation([0, 1, 2, 3])
|
1940 |
+
>>> p.parity()
|
1941 |
+
0
|
1942 |
+
>>> p = Permutation([3, 2, 0, 1])
|
1943 |
+
>>> p.parity()
|
1944 |
+
1
|
1945 |
+
|
1946 |
+
See Also
|
1947 |
+
========
|
1948 |
+
|
1949 |
+
_af_parity
|
1950 |
+
"""
|
1951 |
+
if self._cyclic_form is not None:
|
1952 |
+
return (self.size - self.cycles) % 2
|
1953 |
+
|
1954 |
+
return _af_parity(self.array_form)
|
1955 |
+
|
1956 |
+
@property
|
1957 |
+
def is_even(self):
|
1958 |
+
"""
|
1959 |
+
Checks if a permutation is even.
|
1960 |
+
|
1961 |
+
Examples
|
1962 |
+
========
|
1963 |
+
|
1964 |
+
>>> from sympy.combinatorics import Permutation
|
1965 |
+
>>> p = Permutation([0, 1, 2, 3])
|
1966 |
+
>>> p.is_even
|
1967 |
+
True
|
1968 |
+
>>> p = Permutation([3, 2, 1, 0])
|
1969 |
+
>>> p.is_even
|
1970 |
+
True
|
1971 |
+
|
1972 |
+
See Also
|
1973 |
+
========
|
1974 |
+
|
1975 |
+
is_odd
|
1976 |
+
"""
|
1977 |
+
return not self.is_odd
|
1978 |
+
|
1979 |
+
@property
|
1980 |
+
def is_odd(self):
|
1981 |
+
"""
|
1982 |
+
Checks if a permutation is odd.
|
1983 |
+
|
1984 |
+
Examples
|
1985 |
+
========
|
1986 |
+
|
1987 |
+
>>> from sympy.combinatorics import Permutation
|
1988 |
+
>>> p = Permutation([0, 1, 2, 3])
|
1989 |
+
>>> p.is_odd
|
1990 |
+
False
|
1991 |
+
>>> p = Permutation([3, 2, 0, 1])
|
1992 |
+
>>> p.is_odd
|
1993 |
+
True
|
1994 |
+
|
1995 |
+
See Also
|
1996 |
+
========
|
1997 |
+
|
1998 |
+
is_even
|
1999 |
+
"""
|
2000 |
+
return bool(self.parity() % 2)
|
2001 |
+
|
2002 |
+
@property
|
2003 |
+
def is_Singleton(self):
|
2004 |
+
"""
|
2005 |
+
Checks to see if the permutation contains only one number and is
|
2006 |
+
thus the only possible permutation of this set of numbers
|
2007 |
+
|
2008 |
+
Examples
|
2009 |
+
========
|
2010 |
+
|
2011 |
+
>>> from sympy.combinatorics import Permutation
|
2012 |
+
>>> Permutation([0]).is_Singleton
|
2013 |
+
True
|
2014 |
+
>>> Permutation([0, 1]).is_Singleton
|
2015 |
+
False
|
2016 |
+
|
2017 |
+
See Also
|
2018 |
+
========
|
2019 |
+
|
2020 |
+
is_Empty
|
2021 |
+
"""
|
2022 |
+
return self.size == 1
|
2023 |
+
|
2024 |
+
@property
|
2025 |
+
def is_Empty(self):
|
2026 |
+
"""
|
2027 |
+
Checks to see if the permutation is a set with zero elements
|
2028 |
+
|
2029 |
+
Examples
|
2030 |
+
========
|
2031 |
+
|
2032 |
+
>>> from sympy.combinatorics import Permutation
|
2033 |
+
>>> Permutation([]).is_Empty
|
2034 |
+
True
|
2035 |
+
>>> Permutation([0]).is_Empty
|
2036 |
+
False
|
2037 |
+
|
2038 |
+
See Also
|
2039 |
+
========
|
2040 |
+
|
2041 |
+
is_Singleton
|
2042 |
+
"""
|
2043 |
+
return self.size == 0
|
2044 |
+
|
2045 |
+
@property
|
2046 |
+
def is_identity(self):
|
2047 |
+
return self.is_Identity
|
2048 |
+
|
2049 |
+
@property
|
2050 |
+
def is_Identity(self):
|
2051 |
+
"""
|
2052 |
+
Returns True if the Permutation is an identity permutation.
|
2053 |
+
|
2054 |
+
Examples
|
2055 |
+
========
|
2056 |
+
|
2057 |
+
>>> from sympy.combinatorics import Permutation
|
2058 |
+
>>> p = Permutation([])
|
2059 |
+
>>> p.is_Identity
|
2060 |
+
True
|
2061 |
+
>>> p = Permutation([[0], [1], [2]])
|
2062 |
+
>>> p.is_Identity
|
2063 |
+
True
|
2064 |
+
>>> p = Permutation([0, 1, 2])
|
2065 |
+
>>> p.is_Identity
|
2066 |
+
True
|
2067 |
+
>>> p = Permutation([0, 2, 1])
|
2068 |
+
>>> p.is_Identity
|
2069 |
+
False
|
2070 |
+
|
2071 |
+
See Also
|
2072 |
+
========
|
2073 |
+
|
2074 |
+
order
|
2075 |
+
"""
|
2076 |
+
af = self.array_form
|
2077 |
+
return not af or all(i == af[i] for i in range(self.size))
|
2078 |
+
|
2079 |
+
def ascents(self):
|
2080 |
+
"""
|
2081 |
+
Returns the positions of ascents in a permutation, ie, the location
|
2082 |
+
where p[i] < p[i+1]
|
2083 |
+
|
2084 |
+
Examples
|
2085 |
+
========
|
2086 |
+
|
2087 |
+
>>> from sympy.combinatorics import Permutation
|
2088 |
+
>>> p = Permutation([4, 0, 1, 3, 2])
|
2089 |
+
>>> p.ascents()
|
2090 |
+
[1, 2]
|
2091 |
+
|
2092 |
+
See Also
|
2093 |
+
========
|
2094 |
+
|
2095 |
+
descents, inversions, min, max
|
2096 |
+
"""
|
2097 |
+
a = self.array_form
|
2098 |
+
pos = [i for i in range(len(a) - 1) if a[i] < a[i + 1]]
|
2099 |
+
return pos
|
2100 |
+
|
2101 |
+
def descents(self):
|
2102 |
+
"""
|
2103 |
+
Returns the positions of descents in a permutation, ie, the location
|
2104 |
+
where p[i] > p[i+1]
|
2105 |
+
|
2106 |
+
Examples
|
2107 |
+
========
|
2108 |
+
|
2109 |
+
>>> from sympy.combinatorics import Permutation
|
2110 |
+
>>> p = Permutation([4, 0, 1, 3, 2])
|
2111 |
+
>>> p.descents()
|
2112 |
+
[0, 3]
|
2113 |
+
|
2114 |
+
See Also
|
2115 |
+
========
|
2116 |
+
|
2117 |
+
ascents, inversions, min, max
|
2118 |
+
"""
|
2119 |
+
a = self.array_form
|
2120 |
+
pos = [i for i in range(len(a) - 1) if a[i] > a[i + 1]]
|
2121 |
+
return pos
|
2122 |
+
|
2123 |
+
def max(self):
|
2124 |
+
"""
|
2125 |
+
The maximum element moved by the permutation.
|
2126 |
+
|
2127 |
+
Examples
|
2128 |
+
========
|
2129 |
+
|
2130 |
+
>>> from sympy.combinatorics import Permutation
|
2131 |
+
>>> p = Permutation([1, 0, 2, 3, 4])
|
2132 |
+
>>> p.max()
|
2133 |
+
1
|
2134 |
+
|
2135 |
+
See Also
|
2136 |
+
========
|
2137 |
+
|
2138 |
+
min, descents, ascents, inversions
|
2139 |
+
"""
|
2140 |
+
max = 0
|
2141 |
+
a = self.array_form
|
2142 |
+
for i in range(len(a)):
|
2143 |
+
if a[i] != i and a[i] > max:
|
2144 |
+
max = a[i]
|
2145 |
+
return max
|
2146 |
+
|
2147 |
+
def min(self):
|
2148 |
+
"""
|
2149 |
+
The minimum element moved by the permutation.
|
2150 |
+
|
2151 |
+
Examples
|
2152 |
+
========
|
2153 |
+
|
2154 |
+
>>> from sympy.combinatorics import Permutation
|
2155 |
+
>>> p = Permutation([0, 1, 4, 3, 2])
|
2156 |
+
>>> p.min()
|
2157 |
+
2
|
2158 |
+
|
2159 |
+
See Also
|
2160 |
+
========
|
2161 |
+
|
2162 |
+
max, descents, ascents, inversions
|
2163 |
+
"""
|
2164 |
+
a = self.array_form
|
2165 |
+
min = len(a)
|
2166 |
+
for i in range(len(a)):
|
2167 |
+
if a[i] != i and a[i] < min:
|
2168 |
+
min = a[i]
|
2169 |
+
return min
|
2170 |
+
|
2171 |
+
def inversions(self):
|
2172 |
+
"""
|
2173 |
+
Computes the number of inversions of a permutation.
|
2174 |
+
|
2175 |
+
Explanation
|
2176 |
+
===========
|
2177 |
+
|
2178 |
+
An inversion is where i > j but p[i] < p[j].
|
2179 |
+
|
2180 |
+
For small length of p, it iterates over all i and j
|
2181 |
+
values and calculates the number of inversions.
|
2182 |
+
For large length of p, it uses a variation of merge
|
2183 |
+
sort to calculate the number of inversions.
|
2184 |
+
|
2185 |
+
Examples
|
2186 |
+
========
|
2187 |
+
|
2188 |
+
>>> from sympy.combinatorics import Permutation
|
2189 |
+
>>> p = Permutation([0, 1, 2, 3, 4, 5])
|
2190 |
+
>>> p.inversions()
|
2191 |
+
0
|
2192 |
+
>>> Permutation([3, 2, 1, 0]).inversions()
|
2193 |
+
6
|
2194 |
+
|
2195 |
+
See Also
|
2196 |
+
========
|
2197 |
+
|
2198 |
+
descents, ascents, min, max
|
2199 |
+
|
2200 |
+
References
|
2201 |
+
==========
|
2202 |
+
|
2203 |
+
.. [1] https://www.cp.eng.chula.ac.th/~prabhas//teaching/algo/algo2008/count-inv.htm
|
2204 |
+
|
2205 |
+
"""
|
2206 |
+
inversions = 0
|
2207 |
+
a = self.array_form
|
2208 |
+
n = len(a)
|
2209 |
+
if n < 130:
|
2210 |
+
for i in range(n - 1):
|
2211 |
+
b = a[i]
|
2212 |
+
for c in a[i + 1:]:
|
2213 |
+
if b > c:
|
2214 |
+
inversions += 1
|
2215 |
+
else:
|
2216 |
+
k = 1
|
2217 |
+
right = 0
|
2218 |
+
arr = a[:]
|
2219 |
+
temp = a[:]
|
2220 |
+
while k < n:
|
2221 |
+
i = 0
|
2222 |
+
while i + k < n:
|
2223 |
+
right = i + k * 2 - 1
|
2224 |
+
if right >= n:
|
2225 |
+
right = n - 1
|
2226 |
+
inversions += _merge(arr, temp, i, i + k, right)
|
2227 |
+
i = i + k * 2
|
2228 |
+
k = k * 2
|
2229 |
+
return inversions
|
2230 |
+
|
2231 |
+
def commutator(self, x):
|
2232 |
+
"""Return the commutator of ``self`` and ``x``: ``~x*~self*x*self``
|
2233 |
+
|
2234 |
+
If f and g are part of a group, G, then the commutator of f and g
|
2235 |
+
is the group identity iff f and g commute, i.e. fg == gf.
|
2236 |
+
|
2237 |
+
Examples
|
2238 |
+
========
|
2239 |
+
|
2240 |
+
>>> from sympy.combinatorics import Permutation
|
2241 |
+
>>> from sympy import init_printing
|
2242 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
2243 |
+
>>> p = Permutation([0, 2, 3, 1])
|
2244 |
+
>>> x = Permutation([2, 0, 3, 1])
|
2245 |
+
>>> c = p.commutator(x); c
|
2246 |
+
Permutation([2, 1, 3, 0])
|
2247 |
+
>>> c == ~x*~p*x*p
|
2248 |
+
True
|
2249 |
+
|
2250 |
+
>>> I = Permutation(3)
|
2251 |
+
>>> p = [I + i for i in range(6)]
|
2252 |
+
>>> for i in range(len(p)):
|
2253 |
+
... for j in range(len(p)):
|
2254 |
+
... c = p[i].commutator(p[j])
|
2255 |
+
... if p[i]*p[j] == p[j]*p[i]:
|
2256 |
+
... assert c == I
|
2257 |
+
... else:
|
2258 |
+
... assert c != I
|
2259 |
+
...
|
2260 |
+
|
2261 |
+
References
|
2262 |
+
==========
|
2263 |
+
|
2264 |
+
.. [1] https://en.wikipedia.org/wiki/Commutator
|
2265 |
+
"""
|
2266 |
+
|
2267 |
+
a = self.array_form
|
2268 |
+
b = x.array_form
|
2269 |
+
n = len(a)
|
2270 |
+
if len(b) != n:
|
2271 |
+
raise ValueError("The permutations must be of equal size.")
|
2272 |
+
inva = [None]*n
|
2273 |
+
for i in range(n):
|
2274 |
+
inva[a[i]] = i
|
2275 |
+
invb = [None]*n
|
2276 |
+
for i in range(n):
|
2277 |
+
invb[b[i]] = i
|
2278 |
+
return self._af_new([a[b[inva[i]]] for i in invb])
|
2279 |
+
|
2280 |
+
def signature(self):
|
2281 |
+
"""
|
2282 |
+
Gives the signature of the permutation needed to place the
|
2283 |
+
elements of the permutation in canonical order.
|
2284 |
+
|
2285 |
+
The signature is calculated as (-1)^<number of inversions>
|
2286 |
+
|
2287 |
+
Examples
|
2288 |
+
========
|
2289 |
+
|
2290 |
+
>>> from sympy.combinatorics import Permutation
|
2291 |
+
>>> p = Permutation([0, 1, 2])
|
2292 |
+
>>> p.inversions()
|
2293 |
+
0
|
2294 |
+
>>> p.signature()
|
2295 |
+
1
|
2296 |
+
>>> q = Permutation([0,2,1])
|
2297 |
+
>>> q.inversions()
|
2298 |
+
1
|
2299 |
+
>>> q.signature()
|
2300 |
+
-1
|
2301 |
+
|
2302 |
+
See Also
|
2303 |
+
========
|
2304 |
+
|
2305 |
+
inversions
|
2306 |
+
"""
|
2307 |
+
if self.is_even:
|
2308 |
+
return 1
|
2309 |
+
return -1
|
2310 |
+
|
2311 |
+
def order(self):
|
2312 |
+
"""
|
2313 |
+
Computes the order of a permutation.
|
2314 |
+
|
2315 |
+
When the permutation is raised to the power of its
|
2316 |
+
order it equals the identity permutation.
|
2317 |
+
|
2318 |
+
Examples
|
2319 |
+
========
|
2320 |
+
|
2321 |
+
>>> from sympy.combinatorics import Permutation
|
2322 |
+
>>> from sympy import init_printing
|
2323 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
2324 |
+
>>> p = Permutation([3, 1, 5, 2, 4, 0])
|
2325 |
+
>>> p.order()
|
2326 |
+
4
|
2327 |
+
>>> (p**(p.order()))
|
2328 |
+
Permutation([], size=6)
|
2329 |
+
|
2330 |
+
See Also
|
2331 |
+
========
|
2332 |
+
|
2333 |
+
identity, cardinality, length, rank, size
|
2334 |
+
"""
|
2335 |
+
|
2336 |
+
return reduce(lcm, [len(cycle) for cycle in self.cyclic_form], 1)
|
2337 |
+
|
2338 |
+
def length(self):
|
2339 |
+
"""
|
2340 |
+
Returns the number of integers moved by a permutation.
|
2341 |
+
|
2342 |
+
Examples
|
2343 |
+
========
|
2344 |
+
|
2345 |
+
>>> from sympy.combinatorics import Permutation
|
2346 |
+
>>> Permutation([0, 3, 2, 1]).length()
|
2347 |
+
2
|
2348 |
+
>>> Permutation([[0, 1], [2, 3]]).length()
|
2349 |
+
4
|
2350 |
+
|
2351 |
+
See Also
|
2352 |
+
========
|
2353 |
+
|
2354 |
+
min, max, support, cardinality, order, rank, size
|
2355 |
+
"""
|
2356 |
+
|
2357 |
+
return len(self.support())
|
2358 |
+
|
2359 |
+
@property
|
2360 |
+
def cycle_structure(self):
|
2361 |
+
"""Return the cycle structure of the permutation as a dictionary
|
2362 |
+
indicating the multiplicity of each cycle length.
|
2363 |
+
|
2364 |
+
Examples
|
2365 |
+
========
|
2366 |
+
|
2367 |
+
>>> from sympy.combinatorics import Permutation
|
2368 |
+
>>> Permutation(3).cycle_structure
|
2369 |
+
{1: 4}
|
2370 |
+
>>> Permutation(0, 4, 3)(1, 2)(5, 6).cycle_structure
|
2371 |
+
{2: 2, 3: 1}
|
2372 |
+
"""
|
2373 |
+
if self._cycle_structure:
|
2374 |
+
rv = self._cycle_structure
|
2375 |
+
else:
|
2376 |
+
rv = defaultdict(int)
|
2377 |
+
singletons = self.size
|
2378 |
+
for c in self.cyclic_form:
|
2379 |
+
rv[len(c)] += 1
|
2380 |
+
singletons -= len(c)
|
2381 |
+
if singletons:
|
2382 |
+
rv[1] = singletons
|
2383 |
+
self._cycle_structure = rv
|
2384 |
+
return dict(rv) # make a copy
|
2385 |
+
|
2386 |
+
@property
|
2387 |
+
def cycles(self):
|
2388 |
+
"""
|
2389 |
+
Returns the number of cycles contained in the permutation
|
2390 |
+
(including singletons).
|
2391 |
+
|
2392 |
+
Examples
|
2393 |
+
========
|
2394 |
+
|
2395 |
+
>>> from sympy.combinatorics import Permutation
|
2396 |
+
>>> Permutation([0, 1, 2]).cycles
|
2397 |
+
3
|
2398 |
+
>>> Permutation([0, 1, 2]).full_cyclic_form
|
2399 |
+
[[0], [1], [2]]
|
2400 |
+
>>> Permutation(0, 1)(2, 3).cycles
|
2401 |
+
2
|
2402 |
+
|
2403 |
+
See Also
|
2404 |
+
========
|
2405 |
+
sympy.functions.combinatorial.numbers.stirling
|
2406 |
+
"""
|
2407 |
+
return len(self.full_cyclic_form)
|
2408 |
+
|
2409 |
+
def index(self):
|
2410 |
+
"""
|
2411 |
+
Returns the index of a permutation.
|
2412 |
+
|
2413 |
+
The index of a permutation is the sum of all subscripts j such
|
2414 |
+
that p[j] is greater than p[j+1].
|
2415 |
+
|
2416 |
+
Examples
|
2417 |
+
========
|
2418 |
+
|
2419 |
+
>>> from sympy.combinatorics import Permutation
|
2420 |
+
>>> p = Permutation([3, 0, 2, 1, 4])
|
2421 |
+
>>> p.index()
|
2422 |
+
2
|
2423 |
+
"""
|
2424 |
+
a = self.array_form
|
2425 |
+
|
2426 |
+
return sum([j for j in range(len(a) - 1) if a[j] > a[j + 1]])
|
2427 |
+
|
2428 |
+
def runs(self):
|
2429 |
+
"""
|
2430 |
+
Returns the runs of a permutation.
|
2431 |
+
|
2432 |
+
An ascending sequence in a permutation is called a run [5].
|
2433 |
+
|
2434 |
+
|
2435 |
+
Examples
|
2436 |
+
========
|
2437 |
+
|
2438 |
+
>>> from sympy.combinatorics import Permutation
|
2439 |
+
>>> p = Permutation([2, 5, 7, 3, 6, 0, 1, 4, 8])
|
2440 |
+
>>> p.runs()
|
2441 |
+
[[2, 5, 7], [3, 6], [0, 1, 4, 8]]
|
2442 |
+
>>> q = Permutation([1,3,2,0])
|
2443 |
+
>>> q.runs()
|
2444 |
+
[[1, 3], [2], [0]]
|
2445 |
+
"""
|
2446 |
+
return runs(self.array_form)
|
2447 |
+
|
2448 |
+
def inversion_vector(self):
|
2449 |
+
"""Return the inversion vector of the permutation.
|
2450 |
+
|
2451 |
+
The inversion vector consists of elements whose value
|
2452 |
+
indicates the number of elements in the permutation
|
2453 |
+
that are lesser than it and lie on its right hand side.
|
2454 |
+
|
2455 |
+
The inversion vector is the same as the Lehmer encoding of a
|
2456 |
+
permutation.
|
2457 |
+
|
2458 |
+
Examples
|
2459 |
+
========
|
2460 |
+
|
2461 |
+
>>> from sympy.combinatorics import Permutation
|
2462 |
+
>>> p = Permutation([4, 8, 0, 7, 1, 5, 3, 6, 2])
|
2463 |
+
>>> p.inversion_vector()
|
2464 |
+
[4, 7, 0, 5, 0, 2, 1, 1]
|
2465 |
+
>>> p = Permutation([3, 2, 1, 0])
|
2466 |
+
>>> p.inversion_vector()
|
2467 |
+
[3, 2, 1]
|
2468 |
+
|
2469 |
+
The inversion vector increases lexicographically with the rank
|
2470 |
+
of the permutation, the -ith element cycling through 0..i.
|
2471 |
+
|
2472 |
+
>>> p = Permutation(2)
|
2473 |
+
>>> while p:
|
2474 |
+
... print('%s %s %s' % (p, p.inversion_vector(), p.rank()))
|
2475 |
+
... p = p.next_lex()
|
2476 |
+
(2) [0, 0] 0
|
2477 |
+
(1 2) [0, 1] 1
|
2478 |
+
(2)(0 1) [1, 0] 2
|
2479 |
+
(0 1 2) [1, 1] 3
|
2480 |
+
(0 2 1) [2, 0] 4
|
2481 |
+
(0 2) [2, 1] 5
|
2482 |
+
|
2483 |
+
See Also
|
2484 |
+
========
|
2485 |
+
|
2486 |
+
from_inversion_vector
|
2487 |
+
"""
|
2488 |
+
self_array_form = self.array_form
|
2489 |
+
n = len(self_array_form)
|
2490 |
+
inversion_vector = [0] * (n - 1)
|
2491 |
+
|
2492 |
+
for i in range(n - 1):
|
2493 |
+
val = 0
|
2494 |
+
for j in range(i + 1, n):
|
2495 |
+
if self_array_form[j] < self_array_form[i]:
|
2496 |
+
val += 1
|
2497 |
+
inversion_vector[i] = val
|
2498 |
+
return inversion_vector
|
2499 |
+
|
2500 |
+
def rank_trotterjohnson(self):
|
2501 |
+
"""
|
2502 |
+
Returns the Trotter Johnson rank, which we get from the minimal
|
2503 |
+
change algorithm. See [4] section 2.4.
|
2504 |
+
|
2505 |
+
Examples
|
2506 |
+
========
|
2507 |
+
|
2508 |
+
>>> from sympy.combinatorics import Permutation
|
2509 |
+
>>> p = Permutation([0, 1, 2, 3])
|
2510 |
+
>>> p.rank_trotterjohnson()
|
2511 |
+
0
|
2512 |
+
>>> p = Permutation([0, 2, 1, 3])
|
2513 |
+
>>> p.rank_trotterjohnson()
|
2514 |
+
7
|
2515 |
+
|
2516 |
+
See Also
|
2517 |
+
========
|
2518 |
+
|
2519 |
+
unrank_trotterjohnson, next_trotterjohnson
|
2520 |
+
"""
|
2521 |
+
if self.array_form == [] or self.is_Identity:
|
2522 |
+
return 0
|
2523 |
+
if self.array_form == [1, 0]:
|
2524 |
+
return 1
|
2525 |
+
perm = self.array_form
|
2526 |
+
n = self.size
|
2527 |
+
rank = 0
|
2528 |
+
for j in range(1, n):
|
2529 |
+
k = 1
|
2530 |
+
i = 0
|
2531 |
+
while perm[i] != j:
|
2532 |
+
if perm[i] < j:
|
2533 |
+
k += 1
|
2534 |
+
i += 1
|
2535 |
+
j1 = j + 1
|
2536 |
+
if rank % 2 == 0:
|
2537 |
+
rank = j1*rank + j1 - k
|
2538 |
+
else:
|
2539 |
+
rank = j1*rank + k - 1
|
2540 |
+
return rank
|
2541 |
+
|
2542 |
+
@classmethod
|
2543 |
+
def unrank_trotterjohnson(cls, size, rank):
|
2544 |
+
"""
|
2545 |
+
Trotter Johnson permutation unranking. See [4] section 2.4.
|
2546 |
+
|
2547 |
+
Examples
|
2548 |
+
========
|
2549 |
+
|
2550 |
+
>>> from sympy.combinatorics import Permutation
|
2551 |
+
>>> from sympy import init_printing
|
2552 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
2553 |
+
>>> Permutation.unrank_trotterjohnson(5, 10)
|
2554 |
+
Permutation([0, 3, 1, 2, 4])
|
2555 |
+
|
2556 |
+
See Also
|
2557 |
+
========
|
2558 |
+
|
2559 |
+
rank_trotterjohnson, next_trotterjohnson
|
2560 |
+
"""
|
2561 |
+
perm = [0]*size
|
2562 |
+
r2 = 0
|
2563 |
+
n = ifac(size)
|
2564 |
+
pj = 1
|
2565 |
+
for j in range(2, size + 1):
|
2566 |
+
pj *= j
|
2567 |
+
r1 = (rank * pj) // n
|
2568 |
+
k = r1 - j*r2
|
2569 |
+
if r2 % 2 == 0:
|
2570 |
+
for i in range(j - 1, j - k - 1, -1):
|
2571 |
+
perm[i] = perm[i - 1]
|
2572 |
+
perm[j - k - 1] = j - 1
|
2573 |
+
else:
|
2574 |
+
for i in range(j - 1, k, -1):
|
2575 |
+
perm[i] = perm[i - 1]
|
2576 |
+
perm[k] = j - 1
|
2577 |
+
r2 = r1
|
2578 |
+
return cls._af_new(perm)
|
2579 |
+
|
2580 |
+
def next_trotterjohnson(self):
|
2581 |
+
"""
|
2582 |
+
Returns the next permutation in Trotter-Johnson order.
|
2583 |
+
If self is the last permutation it returns None.
|
2584 |
+
See [4] section 2.4. If it is desired to generate all such
|
2585 |
+
permutations, they can be generated in order more quickly
|
2586 |
+
with the ``generate_bell`` function.
|
2587 |
+
|
2588 |
+
Examples
|
2589 |
+
========
|
2590 |
+
|
2591 |
+
>>> from sympy.combinatorics import Permutation
|
2592 |
+
>>> from sympy import init_printing
|
2593 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
2594 |
+
>>> p = Permutation([3, 0, 2, 1])
|
2595 |
+
>>> p.rank_trotterjohnson()
|
2596 |
+
4
|
2597 |
+
>>> p = p.next_trotterjohnson(); p
|
2598 |
+
Permutation([0, 3, 2, 1])
|
2599 |
+
>>> p.rank_trotterjohnson()
|
2600 |
+
5
|
2601 |
+
|
2602 |
+
See Also
|
2603 |
+
========
|
2604 |
+
|
2605 |
+
rank_trotterjohnson, unrank_trotterjohnson, sympy.utilities.iterables.generate_bell
|
2606 |
+
"""
|
2607 |
+
pi = self.array_form[:]
|
2608 |
+
n = len(pi)
|
2609 |
+
st = 0
|
2610 |
+
rho = pi[:]
|
2611 |
+
done = False
|
2612 |
+
m = n-1
|
2613 |
+
while m > 0 and not done:
|
2614 |
+
d = rho.index(m)
|
2615 |
+
for i in range(d, m):
|
2616 |
+
rho[i] = rho[i + 1]
|
2617 |
+
par = _af_parity(rho[:m])
|
2618 |
+
if par == 1:
|
2619 |
+
if d == m:
|
2620 |
+
m -= 1
|
2621 |
+
else:
|
2622 |
+
pi[st + d], pi[st + d + 1] = pi[st + d + 1], pi[st + d]
|
2623 |
+
done = True
|
2624 |
+
else:
|
2625 |
+
if d == 0:
|
2626 |
+
m -= 1
|
2627 |
+
st += 1
|
2628 |
+
else:
|
2629 |
+
pi[st + d], pi[st + d - 1] = pi[st + d - 1], pi[st + d]
|
2630 |
+
done = True
|
2631 |
+
if m == 0:
|
2632 |
+
return None
|
2633 |
+
return self._af_new(pi)
|
2634 |
+
|
2635 |
+
def get_precedence_matrix(self):
|
2636 |
+
"""
|
2637 |
+
Gets the precedence matrix. This is used for computing the
|
2638 |
+
distance between two permutations.
|
2639 |
+
|
2640 |
+
Examples
|
2641 |
+
========
|
2642 |
+
|
2643 |
+
>>> from sympy.combinatorics import Permutation
|
2644 |
+
>>> from sympy import init_printing
|
2645 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
2646 |
+
>>> p = Permutation.josephus(3, 6, 1)
|
2647 |
+
>>> p
|
2648 |
+
Permutation([2, 5, 3, 1, 4, 0])
|
2649 |
+
>>> p.get_precedence_matrix()
|
2650 |
+
Matrix([
|
2651 |
+
[0, 0, 0, 0, 0, 0],
|
2652 |
+
[1, 0, 0, 0, 1, 0],
|
2653 |
+
[1, 1, 0, 1, 1, 1],
|
2654 |
+
[1, 1, 0, 0, 1, 0],
|
2655 |
+
[1, 0, 0, 0, 0, 0],
|
2656 |
+
[1, 1, 0, 1, 1, 0]])
|
2657 |
+
|
2658 |
+
See Also
|
2659 |
+
========
|
2660 |
+
|
2661 |
+
get_precedence_distance, get_adjacency_matrix, get_adjacency_distance
|
2662 |
+
"""
|
2663 |
+
m = zeros(self.size)
|
2664 |
+
perm = self.array_form
|
2665 |
+
for i in range(m.rows):
|
2666 |
+
for j in range(i + 1, m.cols):
|
2667 |
+
m[perm[i], perm[j]] = 1
|
2668 |
+
return m
|
2669 |
+
|
2670 |
+
def get_precedence_distance(self, other):
|
2671 |
+
"""
|
2672 |
+
Computes the precedence distance between two permutations.
|
2673 |
+
|
2674 |
+
Explanation
|
2675 |
+
===========
|
2676 |
+
|
2677 |
+
Suppose p and p' represent n jobs. The precedence metric
|
2678 |
+
counts the number of times a job j is preceded by job i
|
2679 |
+
in both p and p'. This metric is commutative.
|
2680 |
+
|
2681 |
+
Examples
|
2682 |
+
========
|
2683 |
+
|
2684 |
+
>>> from sympy.combinatorics import Permutation
|
2685 |
+
>>> p = Permutation([2, 0, 4, 3, 1])
|
2686 |
+
>>> q = Permutation([3, 1, 2, 4, 0])
|
2687 |
+
>>> p.get_precedence_distance(q)
|
2688 |
+
7
|
2689 |
+
>>> q.get_precedence_distance(p)
|
2690 |
+
7
|
2691 |
+
|
2692 |
+
See Also
|
2693 |
+
========
|
2694 |
+
|
2695 |
+
get_precedence_matrix, get_adjacency_matrix, get_adjacency_distance
|
2696 |
+
"""
|
2697 |
+
if self.size != other.size:
|
2698 |
+
raise ValueError("The permutations must be of equal size.")
|
2699 |
+
self_prec_mat = self.get_precedence_matrix()
|
2700 |
+
other_prec_mat = other.get_precedence_matrix()
|
2701 |
+
n_prec = 0
|
2702 |
+
for i in range(self.size):
|
2703 |
+
for j in range(self.size):
|
2704 |
+
if i == j:
|
2705 |
+
continue
|
2706 |
+
if self_prec_mat[i, j] * other_prec_mat[i, j] == 1:
|
2707 |
+
n_prec += 1
|
2708 |
+
d = self.size * (self.size - 1)//2 - n_prec
|
2709 |
+
return d
|
2710 |
+
|
2711 |
+
def get_adjacency_matrix(self):
|
2712 |
+
"""
|
2713 |
+
Computes the adjacency matrix of a permutation.
|
2714 |
+
|
2715 |
+
Explanation
|
2716 |
+
===========
|
2717 |
+
|
2718 |
+
If job i is adjacent to job j in a permutation p
|
2719 |
+
then we set m[i, j] = 1 where m is the adjacency
|
2720 |
+
matrix of p.
|
2721 |
+
|
2722 |
+
Examples
|
2723 |
+
========
|
2724 |
+
|
2725 |
+
>>> from sympy.combinatorics import Permutation
|
2726 |
+
>>> p = Permutation.josephus(3, 6, 1)
|
2727 |
+
>>> p.get_adjacency_matrix()
|
2728 |
+
Matrix([
|
2729 |
+
[0, 0, 0, 0, 0, 0],
|
2730 |
+
[0, 0, 0, 0, 1, 0],
|
2731 |
+
[0, 0, 0, 0, 0, 1],
|
2732 |
+
[0, 1, 0, 0, 0, 0],
|
2733 |
+
[1, 0, 0, 0, 0, 0],
|
2734 |
+
[0, 0, 0, 1, 0, 0]])
|
2735 |
+
>>> q = Permutation([0, 1, 2, 3])
|
2736 |
+
>>> q.get_adjacency_matrix()
|
2737 |
+
Matrix([
|
2738 |
+
[0, 1, 0, 0],
|
2739 |
+
[0, 0, 1, 0],
|
2740 |
+
[0, 0, 0, 1],
|
2741 |
+
[0, 0, 0, 0]])
|
2742 |
+
|
2743 |
+
See Also
|
2744 |
+
========
|
2745 |
+
|
2746 |
+
get_precedence_matrix, get_precedence_distance, get_adjacency_distance
|
2747 |
+
"""
|
2748 |
+
m = zeros(self.size)
|
2749 |
+
perm = self.array_form
|
2750 |
+
for i in range(self.size - 1):
|
2751 |
+
m[perm[i], perm[i + 1]] = 1
|
2752 |
+
return m
|
2753 |
+
|
2754 |
+
def get_adjacency_distance(self, other):
|
2755 |
+
"""
|
2756 |
+
Computes the adjacency distance between two permutations.
|
2757 |
+
|
2758 |
+
Explanation
|
2759 |
+
===========
|
2760 |
+
|
2761 |
+
This metric counts the number of times a pair i,j of jobs is
|
2762 |
+
adjacent in both p and p'. If n_adj is this quantity then
|
2763 |
+
the adjacency distance is n - n_adj - 1 [1]
|
2764 |
+
|
2765 |
+
[1] Reeves, Colin R. Landscapes, Operators and Heuristic search, Annals
|
2766 |
+
of Operational Research, 86, pp 473-490. (1999)
|
2767 |
+
|
2768 |
+
|
2769 |
+
Examples
|
2770 |
+
========
|
2771 |
+
|
2772 |
+
>>> from sympy.combinatorics import Permutation
|
2773 |
+
>>> p = Permutation([0, 3, 1, 2, 4])
|
2774 |
+
>>> q = Permutation.josephus(4, 5, 2)
|
2775 |
+
>>> p.get_adjacency_distance(q)
|
2776 |
+
3
|
2777 |
+
>>> r = Permutation([0, 2, 1, 4, 3])
|
2778 |
+
>>> p.get_adjacency_distance(r)
|
2779 |
+
4
|
2780 |
+
|
2781 |
+
See Also
|
2782 |
+
========
|
2783 |
+
|
2784 |
+
get_precedence_matrix, get_precedence_distance, get_adjacency_matrix
|
2785 |
+
"""
|
2786 |
+
if self.size != other.size:
|
2787 |
+
raise ValueError("The permutations must be of the same size.")
|
2788 |
+
self_adj_mat = self.get_adjacency_matrix()
|
2789 |
+
other_adj_mat = other.get_adjacency_matrix()
|
2790 |
+
n_adj = 0
|
2791 |
+
for i in range(self.size):
|
2792 |
+
for j in range(self.size):
|
2793 |
+
if i == j:
|
2794 |
+
continue
|
2795 |
+
if self_adj_mat[i, j] * other_adj_mat[i, j] == 1:
|
2796 |
+
n_adj += 1
|
2797 |
+
d = self.size - n_adj - 1
|
2798 |
+
return d
|
2799 |
+
|
2800 |
+
def get_positional_distance(self, other):
|
2801 |
+
"""
|
2802 |
+
Computes the positional distance between two permutations.
|
2803 |
+
|
2804 |
+
Examples
|
2805 |
+
========
|
2806 |
+
|
2807 |
+
>>> from sympy.combinatorics import Permutation
|
2808 |
+
>>> p = Permutation([0, 3, 1, 2, 4])
|
2809 |
+
>>> q = Permutation.josephus(4, 5, 2)
|
2810 |
+
>>> r = Permutation([3, 1, 4, 0, 2])
|
2811 |
+
>>> p.get_positional_distance(q)
|
2812 |
+
12
|
2813 |
+
>>> p.get_positional_distance(r)
|
2814 |
+
12
|
2815 |
+
|
2816 |
+
See Also
|
2817 |
+
========
|
2818 |
+
|
2819 |
+
get_precedence_distance, get_adjacency_distance
|
2820 |
+
"""
|
2821 |
+
a = self.array_form
|
2822 |
+
b = other.array_form
|
2823 |
+
if len(a) != len(b):
|
2824 |
+
raise ValueError("The permutations must be of the same size.")
|
2825 |
+
return sum([abs(a[i] - b[i]) for i in range(len(a))])
|
2826 |
+
|
2827 |
+
@classmethod
|
2828 |
+
def josephus(cls, m, n, s=1):
|
2829 |
+
"""Return as a permutation the shuffling of range(n) using the Josephus
|
2830 |
+
scheme in which every m-th item is selected until all have been chosen.
|
2831 |
+
The returned permutation has elements listed by the order in which they
|
2832 |
+
were selected.
|
2833 |
+
|
2834 |
+
The parameter ``s`` stops the selection process when there are ``s``
|
2835 |
+
items remaining and these are selected by continuing the selection,
|
2836 |
+
counting by 1 rather than by ``m``.
|
2837 |
+
|
2838 |
+
Consider selecting every 3rd item from 6 until only 2 remain::
|
2839 |
+
|
2840 |
+
choices chosen
|
2841 |
+
======== ======
|
2842 |
+
012345
|
2843 |
+
01 345 2
|
2844 |
+
01 34 25
|
2845 |
+
01 4 253
|
2846 |
+
0 4 2531
|
2847 |
+
0 25314
|
2848 |
+
253140
|
2849 |
+
|
2850 |
+
Examples
|
2851 |
+
========
|
2852 |
+
|
2853 |
+
>>> from sympy.combinatorics import Permutation
|
2854 |
+
>>> Permutation.josephus(3, 6, 2).array_form
|
2855 |
+
[2, 5, 3, 1, 4, 0]
|
2856 |
+
|
2857 |
+
References
|
2858 |
+
==========
|
2859 |
+
|
2860 |
+
.. [1] https://en.wikipedia.org/wiki/Flavius_Josephus
|
2861 |
+
.. [2] https://en.wikipedia.org/wiki/Josephus_problem
|
2862 |
+
.. [3] https://web.archive.org/web/20171008094331/http://www.wou.edu/~burtonl/josephus.html
|
2863 |
+
|
2864 |
+
"""
|
2865 |
+
from collections import deque
|
2866 |
+
m -= 1
|
2867 |
+
Q = deque(list(range(n)))
|
2868 |
+
perm = []
|
2869 |
+
while len(Q) > max(s, 1):
|
2870 |
+
for dp in range(m):
|
2871 |
+
Q.append(Q.popleft())
|
2872 |
+
perm.append(Q.popleft())
|
2873 |
+
perm.extend(list(Q))
|
2874 |
+
return cls(perm)
|
2875 |
+
|
2876 |
+
@classmethod
|
2877 |
+
def from_inversion_vector(cls, inversion):
|
2878 |
+
"""
|
2879 |
+
Calculates the permutation from the inversion vector.
|
2880 |
+
|
2881 |
+
Examples
|
2882 |
+
========
|
2883 |
+
|
2884 |
+
>>> from sympy.combinatorics import Permutation
|
2885 |
+
>>> from sympy import init_printing
|
2886 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
2887 |
+
>>> Permutation.from_inversion_vector([3, 2, 1, 0, 0])
|
2888 |
+
Permutation([3, 2, 1, 0, 4, 5])
|
2889 |
+
|
2890 |
+
"""
|
2891 |
+
size = len(inversion)
|
2892 |
+
N = list(range(size + 1))
|
2893 |
+
perm = []
|
2894 |
+
try:
|
2895 |
+
for k in range(size):
|
2896 |
+
val = N[inversion[k]]
|
2897 |
+
perm.append(val)
|
2898 |
+
N.remove(val)
|
2899 |
+
except IndexError:
|
2900 |
+
raise ValueError("The inversion vector is not valid.")
|
2901 |
+
perm.extend(N)
|
2902 |
+
return cls._af_new(perm)
|
2903 |
+
|
2904 |
+
@classmethod
|
2905 |
+
def random(cls, n):
|
2906 |
+
"""
|
2907 |
+
Generates a random permutation of length ``n``.
|
2908 |
+
|
2909 |
+
Uses the underlying Python pseudo-random number generator.
|
2910 |
+
|
2911 |
+
Examples
|
2912 |
+
========
|
2913 |
+
|
2914 |
+
>>> from sympy.combinatorics import Permutation
|
2915 |
+
>>> Permutation.random(2) in (Permutation([1, 0]), Permutation([0, 1]))
|
2916 |
+
True
|
2917 |
+
|
2918 |
+
"""
|
2919 |
+
perm_array = list(range(n))
|
2920 |
+
random.shuffle(perm_array)
|
2921 |
+
return cls._af_new(perm_array)
|
2922 |
+
|
2923 |
+
@classmethod
|
2924 |
+
def unrank_lex(cls, size, rank):
|
2925 |
+
"""
|
2926 |
+
Lexicographic permutation unranking.
|
2927 |
+
|
2928 |
+
Examples
|
2929 |
+
========
|
2930 |
+
|
2931 |
+
>>> from sympy.combinatorics import Permutation
|
2932 |
+
>>> from sympy import init_printing
|
2933 |
+
>>> init_printing(perm_cyclic=False, pretty_print=False)
|
2934 |
+
>>> a = Permutation.unrank_lex(5, 10)
|
2935 |
+
>>> a.rank()
|
2936 |
+
10
|
2937 |
+
>>> a
|
2938 |
+
Permutation([0, 2, 4, 1, 3])
|
2939 |
+
|
2940 |
+
See Also
|
2941 |
+
========
|
2942 |
+
|
2943 |
+
rank, next_lex
|
2944 |
+
"""
|
2945 |
+
perm_array = [0] * size
|
2946 |
+
psize = 1
|
2947 |
+
for i in range(size):
|
2948 |
+
new_psize = psize*(i + 1)
|
2949 |
+
d = (rank % new_psize) // psize
|
2950 |
+
rank -= d*psize
|
2951 |
+
perm_array[size - i - 1] = d
|
2952 |
+
for j in range(size - i, size):
|
2953 |
+
if perm_array[j] > d - 1:
|
2954 |
+
perm_array[j] += 1
|
2955 |
+
psize = new_psize
|
2956 |
+
return cls._af_new(perm_array)
|
2957 |
+
|
2958 |
+
def resize(self, n):
|
2959 |
+
"""Resize the permutation to the new size ``n``.
|
2960 |
+
|
2961 |
+
Parameters
|
2962 |
+
==========
|
2963 |
+
|
2964 |
+
n : int
|
2965 |
+
The new size of the permutation.
|
2966 |
+
|
2967 |
+
Raises
|
2968 |
+
======
|
2969 |
+
|
2970 |
+
ValueError
|
2971 |
+
If the permutation cannot be resized to the given size.
|
2972 |
+
This may only happen when resized to a smaller size than
|
2973 |
+
the original.
|
2974 |
+
|
2975 |
+
Examples
|
2976 |
+
========
|
2977 |
+
|
2978 |
+
>>> from sympy.combinatorics import Permutation
|
2979 |
+
|
2980 |
+
Increasing the size of a permutation:
|
2981 |
+
|
2982 |
+
>>> p = Permutation(0, 1, 2)
|
2983 |
+
>>> p = p.resize(5)
|
2984 |
+
>>> p
|
2985 |
+
(4)(0 1 2)
|
2986 |
+
|
2987 |
+
Decreasing the size of the permutation:
|
2988 |
+
|
2989 |
+
>>> p = p.resize(4)
|
2990 |
+
>>> p
|
2991 |
+
(3)(0 1 2)
|
2992 |
+
|
2993 |
+
If resizing to the specific size breaks the cycles:
|
2994 |
+
|
2995 |
+
>>> p.resize(2)
|
2996 |
+
Traceback (most recent call last):
|
2997 |
+
...
|
2998 |
+
ValueError: The permutation cannot be resized to 2 because the
|
2999 |
+
cycle (0, 1, 2) may break.
|
3000 |
+
"""
|
3001 |
+
aform = self.array_form
|
3002 |
+
l = len(aform)
|
3003 |
+
if n > l:
|
3004 |
+
aform += list(range(l, n))
|
3005 |
+
return Permutation._af_new(aform)
|
3006 |
+
|
3007 |
+
elif n < l:
|
3008 |
+
cyclic_form = self.full_cyclic_form
|
3009 |
+
new_cyclic_form = []
|
3010 |
+
for cycle in cyclic_form:
|
3011 |
+
cycle_min = min(cycle)
|
3012 |
+
cycle_max = max(cycle)
|
3013 |
+
if cycle_min <= n-1:
|
3014 |
+
if cycle_max > n-1:
|
3015 |
+
raise ValueError(
|
3016 |
+
"The permutation cannot be resized to {} "
|
3017 |
+
"because the cycle {} may break."
|
3018 |
+
.format(n, tuple(cycle)))
|
3019 |
+
|
3020 |
+
new_cyclic_form.append(cycle)
|
3021 |
+
return Permutation(new_cyclic_form)
|
3022 |
+
|
3023 |
+
return self
|
3024 |
+
|
3025 |
+
# XXX Deprecated flag
|
3026 |
+
print_cyclic = None
|
3027 |
+
|
3028 |
+
|
3029 |
+
def _merge(arr, temp, left, mid, right):
|
3030 |
+
"""
|
3031 |
+
Merges two sorted arrays and calculates the inversion count.
|
3032 |
+
|
3033 |
+
Helper function for calculating inversions. This method is
|
3034 |
+
for internal use only.
|
3035 |
+
"""
|
3036 |
+
i = k = left
|
3037 |
+
j = mid
|
3038 |
+
inv_count = 0
|
3039 |
+
while i < mid and j <= right:
|
3040 |
+
if arr[i] < arr[j]:
|
3041 |
+
temp[k] = arr[i]
|
3042 |
+
k += 1
|
3043 |
+
i += 1
|
3044 |
+
else:
|
3045 |
+
temp[k] = arr[j]
|
3046 |
+
k += 1
|
3047 |
+
j += 1
|
3048 |
+
inv_count += (mid -i)
|
3049 |
+
while i < mid:
|
3050 |
+
temp[k] = arr[i]
|
3051 |
+
k += 1
|
3052 |
+
i += 1
|
3053 |
+
if j <= right:
|
3054 |
+
k += right - j + 1
|
3055 |
+
j += right - j + 1
|
3056 |
+
arr[left:k + 1] = temp[left:k + 1]
|
3057 |
+
else:
|
3058 |
+
arr[left:right + 1] = temp[left:right + 1]
|
3059 |
+
return inv_count
|
3060 |
+
|
3061 |
+
Perm = Permutation
|
3062 |
+
_af_new = Perm._af_new
|
3063 |
+
|
3064 |
+
|
3065 |
+
class AppliedPermutation(Expr):
|
3066 |
+
"""A permutation applied to a symbolic variable.
|
3067 |
+
|
3068 |
+
Parameters
|
3069 |
+
==========
|
3070 |
+
|
3071 |
+
perm : Permutation
|
3072 |
+
x : Expr
|
3073 |
+
|
3074 |
+
Examples
|
3075 |
+
========
|
3076 |
+
|
3077 |
+
>>> from sympy import Symbol
|
3078 |
+
>>> from sympy.combinatorics import Permutation
|
3079 |
+
|
3080 |
+
Creating a symbolic permutation function application:
|
3081 |
+
|
3082 |
+
>>> x = Symbol('x')
|
3083 |
+
>>> p = Permutation(0, 1, 2)
|
3084 |
+
>>> p.apply(x)
|
3085 |
+
AppliedPermutation((0 1 2), x)
|
3086 |
+
>>> _.subs(x, 1)
|
3087 |
+
2
|
3088 |
+
"""
|
3089 |
+
def __new__(cls, perm, x, evaluate=None):
|
3090 |
+
if evaluate is None:
|
3091 |
+
evaluate = global_parameters.evaluate
|
3092 |
+
|
3093 |
+
perm = _sympify(perm)
|
3094 |
+
x = _sympify(x)
|
3095 |
+
|
3096 |
+
if not isinstance(perm, Permutation):
|
3097 |
+
raise ValueError("{} must be a Permutation instance."
|
3098 |
+
.format(perm))
|
3099 |
+
|
3100 |
+
if evaluate:
|
3101 |
+
if x.is_Integer:
|
3102 |
+
return perm.apply(x)
|
3103 |
+
|
3104 |
+
obj = super().__new__(cls, perm, x)
|
3105 |
+
return obj
|
3106 |
+
|
3107 |
+
|
3108 |
+
@dispatch(Permutation, Permutation)
|
3109 |
+
def _eval_is_eq(lhs, rhs):
|
3110 |
+
if lhs._size != rhs._size:
|
3111 |
+
return None
|
3112 |
+
return lhs._array_form == rhs._array_form
|
venv/lib/python3.10/site-packages/sympy/combinatorics/polyhedron.py
ADDED
@@ -0,0 +1,1019 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics import Permutation as Perm
|
2 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
3 |
+
from sympy.core import Basic, Tuple, default_sort_key
|
4 |
+
from sympy.sets import FiniteSet
|
5 |
+
from sympy.utilities.iterables import (minlex, unflatten, flatten)
|
6 |
+
from sympy.utilities.misc import as_int
|
7 |
+
|
8 |
+
rmul = Perm.rmul
|
9 |
+
|
10 |
+
|
11 |
+
class Polyhedron(Basic):
|
12 |
+
"""
|
13 |
+
Represents the polyhedral symmetry group (PSG).
|
14 |
+
|
15 |
+
Explanation
|
16 |
+
===========
|
17 |
+
|
18 |
+
The PSG is one of the symmetry groups of the Platonic solids.
|
19 |
+
There are three polyhedral groups: the tetrahedral group
|
20 |
+
of order 12, the octahedral group of order 24, and the
|
21 |
+
icosahedral group of order 60.
|
22 |
+
|
23 |
+
All doctests have been given in the docstring of the
|
24 |
+
constructor of the object.
|
25 |
+
|
26 |
+
References
|
27 |
+
==========
|
28 |
+
|
29 |
+
.. [1] https://mathworld.wolfram.com/PolyhedralGroup.html
|
30 |
+
|
31 |
+
"""
|
32 |
+
_edges = None
|
33 |
+
|
34 |
+
def __new__(cls, corners, faces=(), pgroup=()):
|
35 |
+
"""
|
36 |
+
The constructor of the Polyhedron group object.
|
37 |
+
|
38 |
+
Explanation
|
39 |
+
===========
|
40 |
+
|
41 |
+
It takes up to three parameters: the corners, faces, and
|
42 |
+
allowed transformations.
|
43 |
+
|
44 |
+
The corners/vertices are entered as a list of arbitrary
|
45 |
+
expressions that are used to identify each vertex.
|
46 |
+
|
47 |
+
The faces are entered as a list of tuples of indices; a tuple
|
48 |
+
of indices identifies the vertices which define the face. They
|
49 |
+
should be entered in a cw or ccw order; they will be standardized
|
50 |
+
by reversal and rotation to be give the lowest lexical ordering.
|
51 |
+
If no faces are given then no edges will be computed.
|
52 |
+
|
53 |
+
>>> from sympy.combinatorics.polyhedron import Polyhedron
|
54 |
+
>>> Polyhedron(list('abc'), [(1, 2, 0)]).faces
|
55 |
+
{(0, 1, 2)}
|
56 |
+
>>> Polyhedron(list('abc'), [(1, 0, 2)]).faces
|
57 |
+
{(0, 1, 2)}
|
58 |
+
|
59 |
+
The allowed transformations are entered as allowable permutations
|
60 |
+
of the vertices for the polyhedron. Instance of Permutations
|
61 |
+
(as with faces) should refer to the supplied vertices by index.
|
62 |
+
These permutation are stored as a PermutationGroup.
|
63 |
+
|
64 |
+
Examples
|
65 |
+
========
|
66 |
+
|
67 |
+
>>> from sympy.combinatorics.permutations import Permutation
|
68 |
+
>>> from sympy import init_printing
|
69 |
+
>>> from sympy.abc import w, x, y, z
|
70 |
+
>>> init_printing(pretty_print=False, perm_cyclic=False)
|
71 |
+
|
72 |
+
Here we construct the Polyhedron object for a tetrahedron.
|
73 |
+
|
74 |
+
>>> corners = [w, x, y, z]
|
75 |
+
>>> faces = [(0, 1, 2), (0, 2, 3), (0, 3, 1), (1, 2, 3)]
|
76 |
+
|
77 |
+
Next, allowed transformations of the polyhedron must be given. This
|
78 |
+
is given as permutations of vertices.
|
79 |
+
|
80 |
+
Although the vertices of a tetrahedron can be numbered in 24 (4!)
|
81 |
+
different ways, there are only 12 different orientations for a
|
82 |
+
physical tetrahedron. The following permutations, applied once or
|
83 |
+
twice, will generate all 12 of the orientations. (The identity
|
84 |
+
permutation, Permutation(range(4)), is not included since it does
|
85 |
+
not change the orientation of the vertices.)
|
86 |
+
|
87 |
+
>>> pgroup = [Permutation([[0, 1, 2], [3]]), \
|
88 |
+
Permutation([[0, 1, 3], [2]]), \
|
89 |
+
Permutation([[0, 2, 3], [1]]), \
|
90 |
+
Permutation([[1, 2, 3], [0]]), \
|
91 |
+
Permutation([[0, 1], [2, 3]]), \
|
92 |
+
Permutation([[0, 2], [1, 3]]), \
|
93 |
+
Permutation([[0, 3], [1, 2]])]
|
94 |
+
|
95 |
+
The Polyhedron is now constructed and demonstrated:
|
96 |
+
|
97 |
+
>>> tetra = Polyhedron(corners, faces, pgroup)
|
98 |
+
>>> tetra.size
|
99 |
+
4
|
100 |
+
>>> tetra.edges
|
101 |
+
{(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)}
|
102 |
+
>>> tetra.corners
|
103 |
+
(w, x, y, z)
|
104 |
+
|
105 |
+
It can be rotated with an arbitrary permutation of vertices, e.g.
|
106 |
+
the following permutation is not in the pgroup:
|
107 |
+
|
108 |
+
>>> tetra.rotate(Permutation([0, 1, 3, 2]))
|
109 |
+
>>> tetra.corners
|
110 |
+
(w, x, z, y)
|
111 |
+
|
112 |
+
An allowed permutation of the vertices can be constructed by
|
113 |
+
repeatedly applying permutations from the pgroup to the vertices.
|
114 |
+
Here is a demonstration that applying p and p**2 for every p in
|
115 |
+
pgroup generates all the orientations of a tetrahedron and no others:
|
116 |
+
|
117 |
+
>>> all = ( (w, x, y, z), \
|
118 |
+
(x, y, w, z), \
|
119 |
+
(y, w, x, z), \
|
120 |
+
(w, z, x, y), \
|
121 |
+
(z, w, y, x), \
|
122 |
+
(w, y, z, x), \
|
123 |
+
(y, z, w, x), \
|
124 |
+
(x, z, y, w), \
|
125 |
+
(z, y, x, w), \
|
126 |
+
(y, x, z, w), \
|
127 |
+
(x, w, z, y), \
|
128 |
+
(z, x, w, y) )
|
129 |
+
|
130 |
+
>>> got = []
|
131 |
+
>>> for p in (pgroup + [p**2 for p in pgroup]):
|
132 |
+
... h = Polyhedron(corners)
|
133 |
+
... h.rotate(p)
|
134 |
+
... got.append(h.corners)
|
135 |
+
...
|
136 |
+
>>> set(got) == set(all)
|
137 |
+
True
|
138 |
+
|
139 |
+
The make_perm method of a PermutationGroup will randomly pick
|
140 |
+
permutations, multiply them together, and return the permutation that
|
141 |
+
can be applied to the polyhedron to give the orientation produced
|
142 |
+
by those individual permutations.
|
143 |
+
|
144 |
+
Here, 3 permutations are used:
|
145 |
+
|
146 |
+
>>> tetra.pgroup.make_perm(3) # doctest: +SKIP
|
147 |
+
Permutation([0, 3, 1, 2])
|
148 |
+
|
149 |
+
To select the permutations that should be used, supply a list
|
150 |
+
of indices to the permutations in pgroup in the order they should
|
151 |
+
be applied:
|
152 |
+
|
153 |
+
>>> use = [0, 0, 2]
|
154 |
+
>>> p002 = tetra.pgroup.make_perm(3, use)
|
155 |
+
>>> p002
|
156 |
+
Permutation([1, 0, 3, 2])
|
157 |
+
|
158 |
+
|
159 |
+
Apply them one at a time:
|
160 |
+
|
161 |
+
>>> tetra.reset()
|
162 |
+
>>> for i in use:
|
163 |
+
... tetra.rotate(pgroup[i])
|
164 |
+
...
|
165 |
+
>>> tetra.vertices
|
166 |
+
(x, w, z, y)
|
167 |
+
>>> sequentially = tetra.vertices
|
168 |
+
|
169 |
+
Apply the composite permutation:
|
170 |
+
|
171 |
+
>>> tetra.reset()
|
172 |
+
>>> tetra.rotate(p002)
|
173 |
+
>>> tetra.corners
|
174 |
+
(x, w, z, y)
|
175 |
+
>>> tetra.corners in all and tetra.corners == sequentially
|
176 |
+
True
|
177 |
+
|
178 |
+
Notes
|
179 |
+
=====
|
180 |
+
|
181 |
+
Defining permutation groups
|
182 |
+
---------------------------
|
183 |
+
|
184 |
+
It is not necessary to enter any permutations, nor is necessary to
|
185 |
+
enter a complete set of transformations. In fact, for a polyhedron,
|
186 |
+
all configurations can be constructed from just two permutations.
|
187 |
+
For example, the orientations of a tetrahedron can be generated from
|
188 |
+
an axis passing through a vertex and face and another axis passing
|
189 |
+
through a different vertex or from an axis passing through the
|
190 |
+
midpoints of two edges opposite of each other.
|
191 |
+
|
192 |
+
For simplicity of presentation, consider a square --
|
193 |
+
not a cube -- with vertices 1, 2, 3, and 4:
|
194 |
+
|
195 |
+
1-----2 We could think of axes of rotation being:
|
196 |
+
| | 1) through the face
|
197 |
+
| | 2) from midpoint 1-2 to 3-4 or 1-3 to 2-4
|
198 |
+
3-----4 3) lines 1-4 or 2-3
|
199 |
+
|
200 |
+
|
201 |
+
To determine how to write the permutations, imagine 4 cameras,
|
202 |
+
one at each corner, labeled A-D:
|
203 |
+
|
204 |
+
A B A B
|
205 |
+
1-----2 1-----3 vertex index:
|
206 |
+
| | | | 1 0
|
207 |
+
| | | | 2 1
|
208 |
+
3-----4 2-----4 3 2
|
209 |
+
C D C D 4 3
|
210 |
+
|
211 |
+
original after rotation
|
212 |
+
along 1-4
|
213 |
+
|
214 |
+
A diagonal and a face axis will be chosen for the "permutation group"
|
215 |
+
from which any orientation can be constructed.
|
216 |
+
|
217 |
+
>>> pgroup = []
|
218 |
+
|
219 |
+
Imagine a clockwise rotation when viewing 1-4 from camera A. The new
|
220 |
+
orientation is (in camera-order): 1, 3, 2, 4 so the permutation is
|
221 |
+
given using the *indices* of the vertices as:
|
222 |
+
|
223 |
+
>>> pgroup.append(Permutation((0, 2, 1, 3)))
|
224 |
+
|
225 |
+
Now imagine rotating clockwise when looking down an axis entering the
|
226 |
+
center of the square as viewed. The new camera-order would be
|
227 |
+
3, 1, 4, 2 so the permutation is (using indices):
|
228 |
+
|
229 |
+
>>> pgroup.append(Permutation((2, 0, 3, 1)))
|
230 |
+
|
231 |
+
The square can now be constructed:
|
232 |
+
** use real-world labels for the vertices, entering them in
|
233 |
+
camera order
|
234 |
+
** for the faces we use zero-based indices of the vertices
|
235 |
+
in *edge-order* as the face is traversed; neither the
|
236 |
+
direction nor the starting point matter -- the faces are
|
237 |
+
only used to define edges (if so desired).
|
238 |
+
|
239 |
+
>>> square = Polyhedron((1, 2, 3, 4), [(0, 1, 3, 2)], pgroup)
|
240 |
+
|
241 |
+
To rotate the square with a single permutation we can do:
|
242 |
+
|
243 |
+
>>> square.rotate(square.pgroup[0])
|
244 |
+
>>> square.corners
|
245 |
+
(1, 3, 2, 4)
|
246 |
+
|
247 |
+
To use more than one permutation (or to use one permutation more
|
248 |
+
than once) it is more convenient to use the make_perm method:
|
249 |
+
|
250 |
+
>>> p011 = square.pgroup.make_perm([0, 1, 1]) # diag flip + 2 rotations
|
251 |
+
>>> square.reset() # return to initial orientation
|
252 |
+
>>> square.rotate(p011)
|
253 |
+
>>> square.corners
|
254 |
+
(4, 2, 3, 1)
|
255 |
+
|
256 |
+
Thinking outside the box
|
257 |
+
------------------------
|
258 |
+
|
259 |
+
Although the Polyhedron object has a direct physical meaning, it
|
260 |
+
actually has broader application. In the most general sense it is
|
261 |
+
just a decorated PermutationGroup, allowing one to connect the
|
262 |
+
permutations to something physical. For example, a Rubik's cube is
|
263 |
+
not a proper polyhedron, but the Polyhedron class can be used to
|
264 |
+
represent it in a way that helps to visualize the Rubik's cube.
|
265 |
+
|
266 |
+
>>> from sympy import flatten, unflatten, symbols
|
267 |
+
>>> from sympy.combinatorics import RubikGroup
|
268 |
+
>>> facelets = flatten([symbols(s+'1:5') for s in 'UFRBLD'])
|
269 |
+
>>> def show():
|
270 |
+
... pairs = unflatten(r2.corners, 2)
|
271 |
+
... print(pairs[::2])
|
272 |
+
... print(pairs[1::2])
|
273 |
+
...
|
274 |
+
>>> r2 = Polyhedron(facelets, pgroup=RubikGroup(2))
|
275 |
+
>>> show()
|
276 |
+
[(U1, U2), (F1, F2), (R1, R2), (B1, B2), (L1, L2), (D1, D2)]
|
277 |
+
[(U3, U4), (F3, F4), (R3, R4), (B3, B4), (L3, L4), (D3, D4)]
|
278 |
+
>>> r2.rotate(0) # cw rotation of F
|
279 |
+
>>> show()
|
280 |
+
[(U1, U2), (F3, F1), (U3, R2), (B1, B2), (L1, D1), (R3, R1)]
|
281 |
+
[(L4, L2), (F4, F2), (U4, R4), (B3, B4), (L3, D2), (D3, D4)]
|
282 |
+
|
283 |
+
Predefined Polyhedra
|
284 |
+
====================
|
285 |
+
|
286 |
+
For convenience, the vertices and faces are defined for the following
|
287 |
+
standard solids along with a permutation group for transformations.
|
288 |
+
When the polyhedron is oriented as indicated below, the vertices in
|
289 |
+
a given horizontal plane are numbered in ccw direction, starting from
|
290 |
+
the vertex that will give the lowest indices in a given face. (In the
|
291 |
+
net of the vertices, indices preceded by "-" indicate replication of
|
292 |
+
the lhs index in the net.)
|
293 |
+
|
294 |
+
tetrahedron, tetrahedron_faces
|
295 |
+
------------------------------
|
296 |
+
|
297 |
+
4 vertices (vertex up) net:
|
298 |
+
|
299 |
+
0 0-0
|
300 |
+
1 2 3-1
|
301 |
+
|
302 |
+
4 faces:
|
303 |
+
|
304 |
+
(0, 1, 2) (0, 2, 3) (0, 3, 1) (1, 2, 3)
|
305 |
+
|
306 |
+
cube, cube_faces
|
307 |
+
----------------
|
308 |
+
|
309 |
+
8 vertices (face up) net:
|
310 |
+
|
311 |
+
0 1 2 3-0
|
312 |
+
4 5 6 7-4
|
313 |
+
|
314 |
+
6 faces:
|
315 |
+
|
316 |
+
(0, 1, 2, 3)
|
317 |
+
(0, 1, 5, 4) (1, 2, 6, 5) (2, 3, 7, 6) (0, 3, 7, 4)
|
318 |
+
(4, 5, 6, 7)
|
319 |
+
|
320 |
+
octahedron, octahedron_faces
|
321 |
+
----------------------------
|
322 |
+
|
323 |
+
6 vertices (vertex up) net:
|
324 |
+
|
325 |
+
0 0 0-0
|
326 |
+
1 2 3 4-1
|
327 |
+
5 5 5-5
|
328 |
+
|
329 |
+
8 faces:
|
330 |
+
|
331 |
+
(0, 1, 2) (0, 2, 3) (0, 3, 4) (0, 1, 4)
|
332 |
+
(1, 2, 5) (2, 3, 5) (3, 4, 5) (1, 4, 5)
|
333 |
+
|
334 |
+
dodecahedron, dodecahedron_faces
|
335 |
+
--------------------------------
|
336 |
+
|
337 |
+
20 vertices (vertex up) net:
|
338 |
+
|
339 |
+
0 1 2 3 4 -0
|
340 |
+
5 6 7 8 9 -5
|
341 |
+
14 10 11 12 13-14
|
342 |
+
15 16 17 18 19-15
|
343 |
+
|
344 |
+
12 faces:
|
345 |
+
|
346 |
+
(0, 1, 2, 3, 4) (0, 1, 6, 10, 5) (1, 2, 7, 11, 6)
|
347 |
+
(2, 3, 8, 12, 7) (3, 4, 9, 13, 8) (0, 4, 9, 14, 5)
|
348 |
+
(5, 10, 16, 15, 14) (6, 10, 16, 17, 11) (7, 11, 17, 18, 12)
|
349 |
+
(8, 12, 18, 19, 13) (9, 13, 19, 15, 14)(15, 16, 17, 18, 19)
|
350 |
+
|
351 |
+
icosahedron, icosahedron_faces
|
352 |
+
------------------------------
|
353 |
+
|
354 |
+
12 vertices (face up) net:
|
355 |
+
|
356 |
+
0 0 0 0 -0
|
357 |
+
1 2 3 4 5 -1
|
358 |
+
6 7 8 9 10 -6
|
359 |
+
11 11 11 11 -11
|
360 |
+
|
361 |
+
20 faces:
|
362 |
+
|
363 |
+
(0, 1, 2) (0, 2, 3) (0, 3, 4)
|
364 |
+
(0, 4, 5) (0, 1, 5) (1, 2, 6)
|
365 |
+
(2, 3, 7) (3, 4, 8) (4, 5, 9)
|
366 |
+
(1, 5, 10) (2, 6, 7) (3, 7, 8)
|
367 |
+
(4, 8, 9) (5, 9, 10) (1, 6, 10)
|
368 |
+
(6, 7, 11) (7, 8, 11) (8, 9, 11)
|
369 |
+
(9, 10, 11) (6, 10, 11)
|
370 |
+
|
371 |
+
>>> from sympy.combinatorics.polyhedron import cube
|
372 |
+
>>> cube.edges
|
373 |
+
{(0, 1), (0, 3), (0, 4), (1, 2), (1, 5), (2, 3), (2, 6), (3, 7), (4, 5), (4, 7), (5, 6), (6, 7)}
|
374 |
+
|
375 |
+
If you want to use letters or other names for the corners you
|
376 |
+
can still use the pre-calculated faces:
|
377 |
+
|
378 |
+
>>> corners = list('abcdefgh')
|
379 |
+
>>> Polyhedron(corners, cube.faces).corners
|
380 |
+
(a, b, c, d, e, f, g, h)
|
381 |
+
|
382 |
+
References
|
383 |
+
==========
|
384 |
+
|
385 |
+
.. [1] www.ocf.berkeley.edu/~wwu/articles/platonicsolids.pdf
|
386 |
+
|
387 |
+
"""
|
388 |
+
faces = [minlex(f, directed=False, key=default_sort_key) for f in faces]
|
389 |
+
corners, faces, pgroup = args = \
|
390 |
+
[Tuple(*a) for a in (corners, faces, pgroup)]
|
391 |
+
obj = Basic.__new__(cls, *args)
|
392 |
+
obj._corners = tuple(corners) # in order given
|
393 |
+
obj._faces = FiniteSet(*faces)
|
394 |
+
if pgroup and pgroup[0].size != len(corners):
|
395 |
+
raise ValueError("Permutation size unequal to number of corners.")
|
396 |
+
# use the identity permutation if none are given
|
397 |
+
obj._pgroup = PermutationGroup(
|
398 |
+
pgroup or [Perm(range(len(corners)))] )
|
399 |
+
return obj
|
400 |
+
|
401 |
+
@property
|
402 |
+
def corners(self):
|
403 |
+
"""
|
404 |
+
Get the corners of the Polyhedron.
|
405 |
+
|
406 |
+
The method ``vertices`` is an alias for ``corners``.
|
407 |
+
|
408 |
+
Examples
|
409 |
+
========
|
410 |
+
|
411 |
+
>>> from sympy.combinatorics import Polyhedron
|
412 |
+
>>> from sympy.abc import a, b, c, d
|
413 |
+
>>> p = Polyhedron(list('abcd'))
|
414 |
+
>>> p.corners == p.vertices == (a, b, c, d)
|
415 |
+
True
|
416 |
+
|
417 |
+
See Also
|
418 |
+
========
|
419 |
+
|
420 |
+
array_form, cyclic_form
|
421 |
+
"""
|
422 |
+
return self._corners
|
423 |
+
vertices = corners
|
424 |
+
|
425 |
+
@property
|
426 |
+
def array_form(self):
|
427 |
+
"""Return the indices of the corners.
|
428 |
+
|
429 |
+
The indices are given relative to the original position of corners.
|
430 |
+
|
431 |
+
Examples
|
432 |
+
========
|
433 |
+
|
434 |
+
>>> from sympy.combinatorics.polyhedron import tetrahedron
|
435 |
+
>>> tetrahedron = tetrahedron.copy()
|
436 |
+
>>> tetrahedron.array_form
|
437 |
+
[0, 1, 2, 3]
|
438 |
+
|
439 |
+
>>> tetrahedron.rotate(0)
|
440 |
+
>>> tetrahedron.array_form
|
441 |
+
[0, 2, 3, 1]
|
442 |
+
>>> tetrahedron.pgroup[0].array_form
|
443 |
+
[0, 2, 3, 1]
|
444 |
+
|
445 |
+
See Also
|
446 |
+
========
|
447 |
+
|
448 |
+
corners, cyclic_form
|
449 |
+
"""
|
450 |
+
corners = list(self.args[0])
|
451 |
+
return [corners.index(c) for c in self.corners]
|
452 |
+
|
453 |
+
@property
|
454 |
+
def cyclic_form(self):
|
455 |
+
"""Return the indices of the corners in cyclic notation.
|
456 |
+
|
457 |
+
The indices are given relative to the original position of corners.
|
458 |
+
|
459 |
+
See Also
|
460 |
+
========
|
461 |
+
|
462 |
+
corners, array_form
|
463 |
+
"""
|
464 |
+
return Perm._af_new(self.array_form).cyclic_form
|
465 |
+
|
466 |
+
@property
|
467 |
+
def size(self):
|
468 |
+
"""
|
469 |
+
Get the number of corners of the Polyhedron.
|
470 |
+
"""
|
471 |
+
return len(self._corners)
|
472 |
+
|
473 |
+
@property
|
474 |
+
def faces(self):
|
475 |
+
"""
|
476 |
+
Get the faces of the Polyhedron.
|
477 |
+
"""
|
478 |
+
return self._faces
|
479 |
+
|
480 |
+
@property
|
481 |
+
def pgroup(self):
|
482 |
+
"""
|
483 |
+
Get the permutations of the Polyhedron.
|
484 |
+
"""
|
485 |
+
return self._pgroup
|
486 |
+
|
487 |
+
@property
|
488 |
+
def edges(self):
|
489 |
+
"""
|
490 |
+
Given the faces of the polyhedra we can get the edges.
|
491 |
+
|
492 |
+
Examples
|
493 |
+
========
|
494 |
+
|
495 |
+
>>> from sympy.combinatorics import Polyhedron
|
496 |
+
>>> from sympy.abc import a, b, c
|
497 |
+
>>> corners = (a, b, c)
|
498 |
+
>>> faces = [(0, 1, 2)]
|
499 |
+
>>> Polyhedron(corners, faces).edges
|
500 |
+
{(0, 1), (0, 2), (1, 2)}
|
501 |
+
|
502 |
+
"""
|
503 |
+
if self._edges is None:
|
504 |
+
output = set()
|
505 |
+
for face in self.faces:
|
506 |
+
for i in range(len(face)):
|
507 |
+
edge = tuple(sorted([face[i], face[i - 1]]))
|
508 |
+
output.add(edge)
|
509 |
+
self._edges = FiniteSet(*output)
|
510 |
+
return self._edges
|
511 |
+
|
512 |
+
def rotate(self, perm):
|
513 |
+
"""
|
514 |
+
Apply a permutation to the polyhedron *in place*. The permutation
|
515 |
+
may be given as a Permutation instance or an integer indicating
|
516 |
+
which permutation from pgroup of the Polyhedron should be
|
517 |
+
applied.
|
518 |
+
|
519 |
+
This is an operation that is analogous to rotation about
|
520 |
+
an axis by a fixed increment.
|
521 |
+
|
522 |
+
Notes
|
523 |
+
=====
|
524 |
+
|
525 |
+
When a Permutation is applied, no check is done to see if that
|
526 |
+
is a valid permutation for the Polyhedron. For example, a cube
|
527 |
+
could be given a permutation which effectively swaps only 2
|
528 |
+
vertices. A valid permutation (that rotates the object in a
|
529 |
+
physical way) will be obtained if one only uses
|
530 |
+
permutations from the ``pgroup`` of the Polyhedron. On the other
|
531 |
+
hand, allowing arbitrary rotations (applications of permutations)
|
532 |
+
gives a way to follow named elements rather than indices since
|
533 |
+
Polyhedron allows vertices to be named while Permutation works
|
534 |
+
only with indices.
|
535 |
+
|
536 |
+
Examples
|
537 |
+
========
|
538 |
+
|
539 |
+
>>> from sympy.combinatorics import Polyhedron, Permutation
|
540 |
+
>>> from sympy.combinatorics.polyhedron import cube
|
541 |
+
>>> cube = cube.copy()
|
542 |
+
>>> cube.corners
|
543 |
+
(0, 1, 2, 3, 4, 5, 6, 7)
|
544 |
+
>>> cube.rotate(0)
|
545 |
+
>>> cube.corners
|
546 |
+
(1, 2, 3, 0, 5, 6, 7, 4)
|
547 |
+
|
548 |
+
A non-physical "rotation" that is not prohibited by this method:
|
549 |
+
|
550 |
+
>>> cube.reset()
|
551 |
+
>>> cube.rotate(Permutation([[1, 2]], size=8))
|
552 |
+
>>> cube.corners
|
553 |
+
(0, 2, 1, 3, 4, 5, 6, 7)
|
554 |
+
|
555 |
+
Polyhedron can be used to follow elements of set that are
|
556 |
+
identified by letters instead of integers:
|
557 |
+
|
558 |
+
>>> shadow = h5 = Polyhedron(list('abcde'))
|
559 |
+
>>> p = Permutation([3, 0, 1, 2, 4])
|
560 |
+
>>> h5.rotate(p)
|
561 |
+
>>> h5.corners
|
562 |
+
(d, a, b, c, e)
|
563 |
+
>>> _ == shadow.corners
|
564 |
+
True
|
565 |
+
>>> copy = h5.copy()
|
566 |
+
>>> h5.rotate(p)
|
567 |
+
>>> h5.corners == copy.corners
|
568 |
+
False
|
569 |
+
"""
|
570 |
+
if not isinstance(perm, Perm):
|
571 |
+
perm = self.pgroup[perm]
|
572 |
+
# and we know it's valid
|
573 |
+
else:
|
574 |
+
if perm.size != self.size:
|
575 |
+
raise ValueError('Polyhedron and Permutation sizes differ.')
|
576 |
+
a = perm.array_form
|
577 |
+
corners = [self.corners[a[i]] for i in range(len(self.corners))]
|
578 |
+
self._corners = tuple(corners)
|
579 |
+
|
580 |
+
def reset(self):
|
581 |
+
"""Return corners to their original positions.
|
582 |
+
|
583 |
+
Examples
|
584 |
+
========
|
585 |
+
|
586 |
+
>>> from sympy.combinatorics.polyhedron import tetrahedron as T
|
587 |
+
>>> T = T.copy()
|
588 |
+
>>> T.corners
|
589 |
+
(0, 1, 2, 3)
|
590 |
+
>>> T.rotate(0)
|
591 |
+
>>> T.corners
|
592 |
+
(0, 2, 3, 1)
|
593 |
+
>>> T.reset()
|
594 |
+
>>> T.corners
|
595 |
+
(0, 1, 2, 3)
|
596 |
+
"""
|
597 |
+
self._corners = self.args[0]
|
598 |
+
|
599 |
+
|
600 |
+
def _pgroup_calcs():
|
601 |
+
"""Return the permutation groups for each of the polyhedra and the face
|
602 |
+
definitions: tetrahedron, cube, octahedron, dodecahedron, icosahedron,
|
603 |
+
tetrahedron_faces, cube_faces, octahedron_faces, dodecahedron_faces,
|
604 |
+
icosahedron_faces
|
605 |
+
|
606 |
+
Explanation
|
607 |
+
===========
|
608 |
+
|
609 |
+
(This author did not find and did not know of a better way to do it though
|
610 |
+
there likely is such a way.)
|
611 |
+
|
612 |
+
Although only 2 permutations are needed for a polyhedron in order to
|
613 |
+
generate all the possible orientations, a group of permutations is
|
614 |
+
provided instead. A set of permutations is called a "group" if::
|
615 |
+
|
616 |
+
a*b = c (for any pair of permutations in the group, a and b, their
|
617 |
+
product, c, is in the group)
|
618 |
+
|
619 |
+
a*(b*c) = (a*b)*c (for any 3 permutations in the group associativity holds)
|
620 |
+
|
621 |
+
there is an identity permutation, I, such that I*a = a*I for all elements
|
622 |
+
in the group
|
623 |
+
|
624 |
+
a*b = I (the inverse of each permutation is also in the group)
|
625 |
+
|
626 |
+
None of the polyhedron groups defined follow these definitions of a group.
|
627 |
+
Instead, they are selected to contain those permutations whose powers
|
628 |
+
alone will construct all orientations of the polyhedron, i.e. for
|
629 |
+
permutations ``a``, ``b``, etc... in the group, ``a, a**2, ..., a**o_a``,
|
630 |
+
``b, b**2, ..., b**o_b``, etc... (where ``o_i`` is the order of
|
631 |
+
permutation ``i``) generate all permutations of the polyhedron instead of
|
632 |
+
mixed products like ``a*b``, ``a*b**2``, etc....
|
633 |
+
|
634 |
+
Note that for a polyhedron with n vertices, the valid permutations of the
|
635 |
+
vertices exclude those that do not maintain its faces. e.g. the
|
636 |
+
permutation BCDE of a square's four corners, ABCD, is a valid
|
637 |
+
permutation while CBDE is not (because this would twist the square).
|
638 |
+
|
639 |
+
Examples
|
640 |
+
========
|
641 |
+
|
642 |
+
The is_group checks for: closure, the presence of the Identity permutation,
|
643 |
+
and the presence of the inverse for each of the elements in the group. This
|
644 |
+
confirms that none of the polyhedra are true groups:
|
645 |
+
|
646 |
+
>>> from sympy.combinatorics.polyhedron import (
|
647 |
+
... tetrahedron, cube, octahedron, dodecahedron, icosahedron)
|
648 |
+
...
|
649 |
+
>>> polyhedra = (tetrahedron, cube, octahedron, dodecahedron, icosahedron)
|
650 |
+
>>> [h.pgroup.is_group for h in polyhedra]
|
651 |
+
...
|
652 |
+
[True, True, True, True, True]
|
653 |
+
|
654 |
+
Although tests in polyhedron's test suite check that powers of the
|
655 |
+
permutations in the groups generate all permutations of the vertices
|
656 |
+
of the polyhedron, here we also demonstrate the powers of the given
|
657 |
+
permutations create a complete group for the tetrahedron:
|
658 |
+
|
659 |
+
>>> from sympy.combinatorics import Permutation, PermutationGroup
|
660 |
+
>>> for h in polyhedra[:1]:
|
661 |
+
... G = h.pgroup
|
662 |
+
... perms = set()
|
663 |
+
... for g in G:
|
664 |
+
... for e in range(g.order()):
|
665 |
+
... p = tuple((g**e).array_form)
|
666 |
+
... perms.add(p)
|
667 |
+
...
|
668 |
+
... perms = [Permutation(p) for p in perms]
|
669 |
+
... assert PermutationGroup(perms).is_group
|
670 |
+
|
671 |
+
In addition to doing the above, the tests in the suite confirm that the
|
672 |
+
faces are all present after the application of each permutation.
|
673 |
+
|
674 |
+
References
|
675 |
+
==========
|
676 |
+
|
677 |
+
.. [1] https://dogschool.tripod.com/trianglegroup.html
|
678 |
+
|
679 |
+
"""
|
680 |
+
def _pgroup_of_double(polyh, ordered_faces, pgroup):
|
681 |
+
n = len(ordered_faces[0])
|
682 |
+
# the vertices of the double which sits inside a give polyhedron
|
683 |
+
# can be found by tracking the faces of the outer polyhedron.
|
684 |
+
# A map between face and the vertex of the double is made so that
|
685 |
+
# after rotation the position of the vertices can be located
|
686 |
+
fmap = dict(zip(ordered_faces,
|
687 |
+
range(len(ordered_faces))))
|
688 |
+
flat_faces = flatten(ordered_faces)
|
689 |
+
new_pgroup = []
|
690 |
+
for i, p in enumerate(pgroup):
|
691 |
+
h = polyh.copy()
|
692 |
+
h.rotate(p)
|
693 |
+
c = h.corners
|
694 |
+
# reorder corners in the order they should appear when
|
695 |
+
# enumerating the faces
|
696 |
+
reorder = unflatten([c[j] for j in flat_faces], n)
|
697 |
+
# make them canonical
|
698 |
+
reorder = [tuple(map(as_int,
|
699 |
+
minlex(f, directed=False)))
|
700 |
+
for f in reorder]
|
701 |
+
# map face to vertex: the resulting list of vertices are the
|
702 |
+
# permutation that we seek for the double
|
703 |
+
new_pgroup.append(Perm([fmap[f] for f in reorder]))
|
704 |
+
return new_pgroup
|
705 |
+
|
706 |
+
tetrahedron_faces = [
|
707 |
+
(0, 1, 2), (0, 2, 3), (0, 3, 1), # upper 3
|
708 |
+
(1, 2, 3), # bottom
|
709 |
+
]
|
710 |
+
|
711 |
+
# cw from top
|
712 |
+
#
|
713 |
+
_t_pgroup = [
|
714 |
+
Perm([[1, 2, 3], [0]]), # cw from top
|
715 |
+
Perm([[0, 1, 2], [3]]), # cw from front face
|
716 |
+
Perm([[0, 3, 2], [1]]), # cw from back right face
|
717 |
+
Perm([[0, 3, 1], [2]]), # cw from back left face
|
718 |
+
Perm([[0, 1], [2, 3]]), # through front left edge
|
719 |
+
Perm([[0, 2], [1, 3]]), # through front right edge
|
720 |
+
Perm([[0, 3], [1, 2]]), # through back edge
|
721 |
+
]
|
722 |
+
|
723 |
+
tetrahedron = Polyhedron(
|
724 |
+
range(4),
|
725 |
+
tetrahedron_faces,
|
726 |
+
_t_pgroup)
|
727 |
+
|
728 |
+
cube_faces = [
|
729 |
+
(0, 1, 2, 3), # upper
|
730 |
+
(0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (0, 3, 7, 4), # middle 4
|
731 |
+
(4, 5, 6, 7), # lower
|
732 |
+
]
|
733 |
+
|
734 |
+
# U, D, F, B, L, R = up, down, front, back, left, right
|
735 |
+
_c_pgroup = [Perm(p) for p in
|
736 |
+
[
|
737 |
+
[1, 2, 3, 0, 5, 6, 7, 4], # cw from top, U
|
738 |
+
[4, 0, 3, 7, 5, 1, 2, 6], # cw from F face
|
739 |
+
[4, 5, 1, 0, 7, 6, 2, 3], # cw from R face
|
740 |
+
|
741 |
+
[1, 0, 4, 5, 2, 3, 7, 6], # cw through UF edge
|
742 |
+
[6, 2, 1, 5, 7, 3, 0, 4], # cw through UR edge
|
743 |
+
[6, 7, 3, 2, 5, 4, 0, 1], # cw through UB edge
|
744 |
+
[3, 7, 4, 0, 2, 6, 5, 1], # cw through UL edge
|
745 |
+
[4, 7, 6, 5, 0, 3, 2, 1], # cw through FL edge
|
746 |
+
[6, 5, 4, 7, 2, 1, 0, 3], # cw through FR edge
|
747 |
+
|
748 |
+
[0, 3, 7, 4, 1, 2, 6, 5], # cw through UFL vertex
|
749 |
+
[5, 1, 0, 4, 6, 2, 3, 7], # cw through UFR vertex
|
750 |
+
[5, 6, 2, 1, 4, 7, 3, 0], # cw through UBR vertex
|
751 |
+
[7, 4, 0, 3, 6, 5, 1, 2], # cw through UBL
|
752 |
+
]]
|
753 |
+
|
754 |
+
cube = Polyhedron(
|
755 |
+
range(8),
|
756 |
+
cube_faces,
|
757 |
+
_c_pgroup)
|
758 |
+
|
759 |
+
octahedron_faces = [
|
760 |
+
(0, 1, 2), (0, 2, 3), (0, 3, 4), (0, 1, 4), # top 4
|
761 |
+
(1, 2, 5), (2, 3, 5), (3, 4, 5), (1, 4, 5), # bottom 4
|
762 |
+
]
|
763 |
+
|
764 |
+
octahedron = Polyhedron(
|
765 |
+
range(6),
|
766 |
+
octahedron_faces,
|
767 |
+
_pgroup_of_double(cube, cube_faces, _c_pgroup))
|
768 |
+
|
769 |
+
dodecahedron_faces = [
|
770 |
+
(0, 1, 2, 3, 4), # top
|
771 |
+
(0, 1, 6, 10, 5), (1, 2, 7, 11, 6), (2, 3, 8, 12, 7), # upper 5
|
772 |
+
(3, 4, 9, 13, 8), (0, 4, 9, 14, 5),
|
773 |
+
(5, 10, 16, 15, 14), (6, 10, 16, 17, 11), (7, 11, 17, 18,
|
774 |
+
12), # lower 5
|
775 |
+
(8, 12, 18, 19, 13), (9, 13, 19, 15, 14),
|
776 |
+
(15, 16, 17, 18, 19) # bottom
|
777 |
+
]
|
778 |
+
|
779 |
+
def _string_to_perm(s):
|
780 |
+
rv = [Perm(range(20))]
|
781 |
+
p = None
|
782 |
+
for si in s:
|
783 |
+
if si not in '01':
|
784 |
+
count = int(si) - 1
|
785 |
+
else:
|
786 |
+
count = 1
|
787 |
+
if si == '0':
|
788 |
+
p = _f0
|
789 |
+
elif si == '1':
|
790 |
+
p = _f1
|
791 |
+
rv.extend([p]*count)
|
792 |
+
return Perm.rmul(*rv)
|
793 |
+
|
794 |
+
# top face cw
|
795 |
+
_f0 = Perm([
|
796 |
+
1, 2, 3, 4, 0, 6, 7, 8, 9, 5, 11,
|
797 |
+
12, 13, 14, 10, 16, 17, 18, 19, 15])
|
798 |
+
# front face cw
|
799 |
+
_f1 = Perm([
|
800 |
+
5, 0, 4, 9, 14, 10, 1, 3, 13, 15,
|
801 |
+
6, 2, 8, 19, 16, 17, 11, 7, 12, 18])
|
802 |
+
# the strings below, like 0104 are shorthand for F0*F1*F0**4 and are
|
803 |
+
# the remaining 4 face rotations, 15 edge permutations, and the
|
804 |
+
# 10 vertex rotations.
|
805 |
+
_dodeca_pgroup = [_f0, _f1] + [_string_to_perm(s) for s in '''
|
806 |
+
0104 140 014 0410
|
807 |
+
010 1403 03104 04103 102
|
808 |
+
120 1304 01303 021302 03130
|
809 |
+
0412041 041204103 04120410 041204104 041204102
|
810 |
+
10 01 1402 0140 04102 0412 1204 1302 0130 03120'''.strip().split()]
|
811 |
+
|
812 |
+
dodecahedron = Polyhedron(
|
813 |
+
range(20),
|
814 |
+
dodecahedron_faces,
|
815 |
+
_dodeca_pgroup)
|
816 |
+
|
817 |
+
icosahedron_faces = [
|
818 |
+
(0, 1, 2), (0, 2, 3), (0, 3, 4), (0, 4, 5), (0, 1, 5),
|
819 |
+
(1, 6, 7), (1, 2, 7), (2, 7, 8), (2, 3, 8), (3, 8, 9),
|
820 |
+
(3, 4, 9), (4, 9, 10), (4, 5, 10), (5, 6, 10), (1, 5, 6),
|
821 |
+
(6, 7, 11), (7, 8, 11), (8, 9, 11), (9, 10, 11), (6, 10, 11)]
|
822 |
+
|
823 |
+
icosahedron = Polyhedron(
|
824 |
+
range(12),
|
825 |
+
icosahedron_faces,
|
826 |
+
_pgroup_of_double(
|
827 |
+
dodecahedron, dodecahedron_faces, _dodeca_pgroup))
|
828 |
+
|
829 |
+
return (tetrahedron, cube, octahedron, dodecahedron, icosahedron,
|
830 |
+
tetrahedron_faces, cube_faces, octahedron_faces,
|
831 |
+
dodecahedron_faces, icosahedron_faces)
|
832 |
+
|
833 |
+
# -----------------------------------------------------------------------
|
834 |
+
# Standard Polyhedron groups
|
835 |
+
#
|
836 |
+
# These are generated using _pgroup_calcs() above. However to save
|
837 |
+
# import time we encode them explicitly here.
|
838 |
+
# -----------------------------------------------------------------------
|
839 |
+
|
840 |
+
tetrahedron = Polyhedron(
|
841 |
+
Tuple(0, 1, 2, 3),
|
842 |
+
Tuple(
|
843 |
+
Tuple(0, 1, 2),
|
844 |
+
Tuple(0, 2, 3),
|
845 |
+
Tuple(0, 1, 3),
|
846 |
+
Tuple(1, 2, 3)),
|
847 |
+
Tuple(
|
848 |
+
Perm(1, 2, 3),
|
849 |
+
Perm(3)(0, 1, 2),
|
850 |
+
Perm(0, 3, 2),
|
851 |
+
Perm(0, 3, 1),
|
852 |
+
Perm(0, 1)(2, 3),
|
853 |
+
Perm(0, 2)(1, 3),
|
854 |
+
Perm(0, 3)(1, 2)
|
855 |
+
))
|
856 |
+
|
857 |
+
cube = Polyhedron(
|
858 |
+
Tuple(0, 1, 2, 3, 4, 5, 6, 7),
|
859 |
+
Tuple(
|
860 |
+
Tuple(0, 1, 2, 3),
|
861 |
+
Tuple(0, 1, 5, 4),
|
862 |
+
Tuple(1, 2, 6, 5),
|
863 |
+
Tuple(2, 3, 7, 6),
|
864 |
+
Tuple(0, 3, 7, 4),
|
865 |
+
Tuple(4, 5, 6, 7)),
|
866 |
+
Tuple(
|
867 |
+
Perm(0, 1, 2, 3)(4, 5, 6, 7),
|
868 |
+
Perm(0, 4, 5, 1)(2, 3, 7, 6),
|
869 |
+
Perm(0, 4, 7, 3)(1, 5, 6, 2),
|
870 |
+
Perm(0, 1)(2, 4)(3, 5)(6, 7),
|
871 |
+
Perm(0, 6)(1, 2)(3, 5)(4, 7),
|
872 |
+
Perm(0, 6)(1, 7)(2, 3)(4, 5),
|
873 |
+
Perm(0, 3)(1, 7)(2, 4)(5, 6),
|
874 |
+
Perm(0, 4)(1, 7)(2, 6)(3, 5),
|
875 |
+
Perm(0, 6)(1, 5)(2, 4)(3, 7),
|
876 |
+
Perm(1, 3, 4)(2, 7, 5),
|
877 |
+
Perm(7)(0, 5, 2)(3, 4, 6),
|
878 |
+
Perm(0, 5, 7)(1, 6, 3),
|
879 |
+
Perm(0, 7, 2)(1, 4, 6)))
|
880 |
+
|
881 |
+
octahedron = Polyhedron(
|
882 |
+
Tuple(0, 1, 2, 3, 4, 5),
|
883 |
+
Tuple(
|
884 |
+
Tuple(0, 1, 2),
|
885 |
+
Tuple(0, 2, 3),
|
886 |
+
Tuple(0, 3, 4),
|
887 |
+
Tuple(0, 1, 4),
|
888 |
+
Tuple(1, 2, 5),
|
889 |
+
Tuple(2, 3, 5),
|
890 |
+
Tuple(3, 4, 5),
|
891 |
+
Tuple(1, 4, 5)),
|
892 |
+
Tuple(
|
893 |
+
Perm(5)(1, 2, 3, 4),
|
894 |
+
Perm(0, 4, 5, 2),
|
895 |
+
Perm(0, 1, 5, 3),
|
896 |
+
Perm(0, 1)(2, 4)(3, 5),
|
897 |
+
Perm(0, 2)(1, 3)(4, 5),
|
898 |
+
Perm(0, 3)(1, 5)(2, 4),
|
899 |
+
Perm(0, 4)(1, 3)(2, 5),
|
900 |
+
Perm(0, 5)(1, 4)(2, 3),
|
901 |
+
Perm(0, 5)(1, 2)(3, 4),
|
902 |
+
Perm(0, 4, 1)(2, 3, 5),
|
903 |
+
Perm(0, 1, 2)(3, 4, 5),
|
904 |
+
Perm(0, 2, 3)(1, 5, 4),
|
905 |
+
Perm(0, 4, 3)(1, 5, 2)))
|
906 |
+
|
907 |
+
dodecahedron = Polyhedron(
|
908 |
+
Tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19),
|
909 |
+
Tuple(
|
910 |
+
Tuple(0, 1, 2, 3, 4),
|
911 |
+
Tuple(0, 1, 6, 10, 5),
|
912 |
+
Tuple(1, 2, 7, 11, 6),
|
913 |
+
Tuple(2, 3, 8, 12, 7),
|
914 |
+
Tuple(3, 4, 9, 13, 8),
|
915 |
+
Tuple(0, 4, 9, 14, 5),
|
916 |
+
Tuple(5, 10, 16, 15, 14),
|
917 |
+
Tuple(6, 10, 16, 17, 11),
|
918 |
+
Tuple(7, 11, 17, 18, 12),
|
919 |
+
Tuple(8, 12, 18, 19, 13),
|
920 |
+
Tuple(9, 13, 19, 15, 14),
|
921 |
+
Tuple(15, 16, 17, 18, 19)),
|
922 |
+
Tuple(
|
923 |
+
Perm(0, 1, 2, 3, 4)(5, 6, 7, 8, 9)(10, 11, 12, 13, 14)(15, 16, 17, 18, 19),
|
924 |
+
Perm(0, 5, 10, 6, 1)(2, 4, 14, 16, 11)(3, 9, 15, 17, 7)(8, 13, 19, 18, 12),
|
925 |
+
Perm(0, 10, 17, 12, 3)(1, 6, 11, 7, 2)(4, 5, 16, 18, 8)(9, 14, 15, 19, 13),
|
926 |
+
Perm(0, 6, 17, 19, 9)(1, 11, 18, 13, 4)(2, 7, 12, 8, 3)(5, 10, 16, 15, 14),
|
927 |
+
Perm(0, 2, 12, 19, 14)(1, 7, 18, 15, 5)(3, 8, 13, 9, 4)(6, 11, 17, 16, 10),
|
928 |
+
Perm(0, 4, 9, 14, 5)(1, 3, 13, 15, 10)(2, 8, 19, 16, 6)(7, 12, 18, 17, 11),
|
929 |
+
Perm(0, 1)(2, 5)(3, 10)(4, 6)(7, 14)(8, 16)(9, 11)(12, 15)(13, 17)(18, 19),
|
930 |
+
Perm(0, 7)(1, 2)(3, 6)(4, 11)(5, 12)(8, 10)(9, 17)(13, 16)(14, 18)(15, 19),
|
931 |
+
Perm(0, 12)(1, 8)(2, 3)(4, 7)(5, 18)(6, 13)(9, 11)(10, 19)(14, 17)(15, 16),
|
932 |
+
Perm(0, 8)(1, 13)(2, 9)(3, 4)(5, 12)(6, 19)(7, 14)(10, 18)(11, 15)(16, 17),
|
933 |
+
Perm(0, 4)(1, 9)(2, 14)(3, 5)(6, 13)(7, 15)(8, 10)(11, 19)(12, 16)(17, 18),
|
934 |
+
Perm(0, 5)(1, 14)(2, 15)(3, 16)(4, 10)(6, 9)(7, 19)(8, 17)(11, 13)(12, 18),
|
935 |
+
Perm(0, 11)(1, 6)(2, 10)(3, 16)(4, 17)(5, 7)(8, 15)(9, 18)(12, 14)(13, 19),
|
936 |
+
Perm(0, 18)(1, 12)(2, 7)(3, 11)(4, 17)(5, 19)(6, 8)(9, 16)(10, 13)(14, 15),
|
937 |
+
Perm(0, 18)(1, 19)(2, 13)(3, 8)(4, 12)(5, 17)(6, 15)(7, 9)(10, 16)(11, 14),
|
938 |
+
Perm(0, 13)(1, 19)(2, 15)(3, 14)(4, 9)(5, 8)(6, 18)(7, 16)(10, 12)(11, 17),
|
939 |
+
Perm(0, 16)(1, 15)(2, 19)(3, 18)(4, 17)(5, 10)(6, 14)(7, 13)(8, 12)(9, 11),
|
940 |
+
Perm(0, 18)(1, 17)(2, 16)(3, 15)(4, 19)(5, 12)(6, 11)(7, 10)(8, 14)(9, 13),
|
941 |
+
Perm(0, 15)(1, 19)(2, 18)(3, 17)(4, 16)(5, 14)(6, 13)(7, 12)(8, 11)(9, 10),
|
942 |
+
Perm(0, 17)(1, 16)(2, 15)(3, 19)(4, 18)(5, 11)(6, 10)(7, 14)(8, 13)(9, 12),
|
943 |
+
Perm(0, 19)(1, 18)(2, 17)(3, 16)(4, 15)(5, 13)(6, 12)(7, 11)(8, 10)(9, 14),
|
944 |
+
Perm(1, 4, 5)(2, 9, 10)(3, 14, 6)(7, 13, 16)(8, 15, 11)(12, 19, 17),
|
945 |
+
Perm(19)(0, 6, 2)(3, 5, 11)(4, 10, 7)(8, 14, 17)(9, 16, 12)(13, 15, 18),
|
946 |
+
Perm(0, 11, 8)(1, 7, 3)(4, 6, 12)(5, 17, 13)(9, 10, 18)(14, 16, 19),
|
947 |
+
Perm(0, 7, 13)(1, 12, 9)(2, 8, 4)(5, 11, 19)(6, 18, 14)(10, 17, 15),
|
948 |
+
Perm(0, 3, 9)(1, 8, 14)(2, 13, 5)(6, 12, 15)(7, 19, 10)(11, 18, 16),
|
949 |
+
Perm(0, 14, 10)(1, 9, 16)(2, 13, 17)(3, 19, 11)(4, 15, 6)(7, 8, 18),
|
950 |
+
Perm(0, 16, 7)(1, 10, 11)(2, 5, 17)(3, 14, 18)(4, 15, 12)(8, 9, 19),
|
951 |
+
Perm(0, 16, 13)(1, 17, 8)(2, 11, 12)(3, 6, 18)(4, 10, 19)(5, 15, 9),
|
952 |
+
Perm(0, 11, 15)(1, 17, 14)(2, 18, 9)(3, 12, 13)(4, 7, 19)(5, 6, 16),
|
953 |
+
Perm(0, 8, 15)(1, 12, 16)(2, 18, 10)(3, 19, 5)(4, 13, 14)(6, 7, 17)))
|
954 |
+
|
955 |
+
icosahedron = Polyhedron(
|
956 |
+
Tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
|
957 |
+
Tuple(
|
958 |
+
Tuple(0, 1, 2),
|
959 |
+
Tuple(0, 2, 3),
|
960 |
+
Tuple(0, 3, 4),
|
961 |
+
Tuple(0, 4, 5),
|
962 |
+
Tuple(0, 1, 5),
|
963 |
+
Tuple(1, 6, 7),
|
964 |
+
Tuple(1, 2, 7),
|
965 |
+
Tuple(2, 7, 8),
|
966 |
+
Tuple(2, 3, 8),
|
967 |
+
Tuple(3, 8, 9),
|
968 |
+
Tuple(3, 4, 9),
|
969 |
+
Tuple(4, 9, 10),
|
970 |
+
Tuple(4, 5, 10),
|
971 |
+
Tuple(5, 6, 10),
|
972 |
+
Tuple(1, 5, 6),
|
973 |
+
Tuple(6, 7, 11),
|
974 |
+
Tuple(7, 8, 11),
|
975 |
+
Tuple(8, 9, 11),
|
976 |
+
Tuple(9, 10, 11),
|
977 |
+
Tuple(6, 10, 11)),
|
978 |
+
Tuple(
|
979 |
+
Perm(11)(1, 2, 3, 4, 5)(6, 7, 8, 9, 10),
|
980 |
+
Perm(0, 5, 6, 7, 2)(3, 4, 10, 11, 8),
|
981 |
+
Perm(0, 1, 7, 8, 3)(4, 5, 6, 11, 9),
|
982 |
+
Perm(0, 2, 8, 9, 4)(1, 7, 11, 10, 5),
|
983 |
+
Perm(0, 3, 9, 10, 5)(1, 2, 8, 11, 6),
|
984 |
+
Perm(0, 4, 10, 6, 1)(2, 3, 9, 11, 7),
|
985 |
+
Perm(0, 1)(2, 5)(3, 6)(4, 7)(8, 10)(9, 11),
|
986 |
+
Perm(0, 2)(1, 3)(4, 7)(5, 8)(6, 9)(10, 11),
|
987 |
+
Perm(0, 3)(1, 9)(2, 4)(5, 8)(6, 11)(7, 10),
|
988 |
+
Perm(0, 4)(1, 9)(2, 10)(3, 5)(6, 8)(7, 11),
|
989 |
+
Perm(0, 5)(1, 4)(2, 10)(3, 6)(7, 9)(8, 11),
|
990 |
+
Perm(0, 6)(1, 5)(2, 10)(3, 11)(4, 7)(8, 9),
|
991 |
+
Perm(0, 7)(1, 2)(3, 6)(4, 11)(5, 8)(9, 10),
|
992 |
+
Perm(0, 8)(1, 9)(2, 3)(4, 7)(5, 11)(6, 10),
|
993 |
+
Perm(0, 9)(1, 11)(2, 10)(3, 4)(5, 8)(6, 7),
|
994 |
+
Perm(0, 10)(1, 9)(2, 11)(3, 6)(4, 5)(7, 8),
|
995 |
+
Perm(0, 11)(1, 6)(2, 10)(3, 9)(4, 8)(5, 7),
|
996 |
+
Perm(0, 11)(1, 8)(2, 7)(3, 6)(4, 10)(5, 9),
|
997 |
+
Perm(0, 11)(1, 10)(2, 9)(3, 8)(4, 7)(5, 6),
|
998 |
+
Perm(0, 11)(1, 7)(2, 6)(3, 10)(4, 9)(5, 8),
|
999 |
+
Perm(0, 11)(1, 9)(2, 8)(3, 7)(4, 6)(5, 10),
|
1000 |
+
Perm(0, 5, 1)(2, 4, 6)(3, 10, 7)(8, 9, 11),
|
1001 |
+
Perm(0, 1, 2)(3, 5, 7)(4, 6, 8)(9, 10, 11),
|
1002 |
+
Perm(0, 2, 3)(1, 8, 4)(5, 7, 9)(6, 11, 10),
|
1003 |
+
Perm(0, 3, 4)(1, 8, 10)(2, 9, 5)(6, 7, 11),
|
1004 |
+
Perm(0, 4, 5)(1, 3, 10)(2, 9, 6)(7, 8, 11),
|
1005 |
+
Perm(0, 10, 7)(1, 5, 6)(2, 4, 11)(3, 9, 8),
|
1006 |
+
Perm(0, 6, 8)(1, 7, 2)(3, 5, 11)(4, 10, 9),
|
1007 |
+
Perm(0, 7, 9)(1, 11, 4)(2, 8, 3)(5, 6, 10),
|
1008 |
+
Perm(0, 8, 10)(1, 7, 6)(2, 11, 5)(3, 9, 4),
|
1009 |
+
Perm(0, 9, 6)(1, 3, 11)(2, 8, 7)(4, 10, 5)))
|
1010 |
+
|
1011 |
+
tetrahedron_faces = [tuple(arg) for arg in tetrahedron.faces]
|
1012 |
+
|
1013 |
+
cube_faces = [tuple(arg) for arg in cube.faces]
|
1014 |
+
|
1015 |
+
octahedron_faces = [tuple(arg) for arg in octahedron.faces]
|
1016 |
+
|
1017 |
+
dodecahedron_faces = [tuple(arg) for arg in dodecahedron.faces]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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core import Basic
|
2 |
+
from sympy.core.containers import Tuple
|
3 |
+
from sympy.tensor.array import Array
|
4 |
+
from sympy.core.sympify import _sympify
|
5 |
+
from sympy.utilities.iterables import flatten, iterable
|
6 |
+
from sympy.utilities.misc import as_int
|
7 |
+
|
8 |
+
from collections import defaultdict
|
9 |
+
|
10 |
+
|
11 |
+
class Prufer(Basic):
|
12 |
+
"""
|
13 |
+
The Prufer correspondence is an algorithm that describes the
|
14 |
+
bijection between labeled trees and the Prufer code. A Prufer
|
15 |
+
code of a labeled tree is unique up to isomorphism and has
|
16 |
+
a length of n - 2.
|
17 |
+
|
18 |
+
Prufer sequences were first used by Heinz Prufer to give a
|
19 |
+
proof of Cayley's formula.
|
20 |
+
|
21 |
+
References
|
22 |
+
==========
|
23 |
+
|
24 |
+
.. [1] https://mathworld.wolfram.com/LabeledTree.html
|
25 |
+
|
26 |
+
"""
|
27 |
+
_prufer_repr = None
|
28 |
+
_tree_repr = None
|
29 |
+
_nodes = None
|
30 |
+
_rank = None
|
31 |
+
|
32 |
+
@property
|
33 |
+
def prufer_repr(self):
|
34 |
+
"""Returns Prufer sequence for the Prufer object.
|
35 |
+
|
36 |
+
This sequence is found by removing the highest numbered vertex,
|
37 |
+
recording the node it was attached to, and continuing until only
|
38 |
+
two vertices remain. The Prufer sequence is the list of recorded nodes.
|
39 |
+
|
40 |
+
Examples
|
41 |
+
========
|
42 |
+
|
43 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
44 |
+
>>> Prufer([[0, 3], [1, 3], [2, 3], [3, 4], [4, 5]]).prufer_repr
|
45 |
+
[3, 3, 3, 4]
|
46 |
+
>>> Prufer([1, 0, 0]).prufer_repr
|
47 |
+
[1, 0, 0]
|
48 |
+
|
49 |
+
See Also
|
50 |
+
========
|
51 |
+
|
52 |
+
to_prufer
|
53 |
+
|
54 |
+
"""
|
55 |
+
if self._prufer_repr is None:
|
56 |
+
self._prufer_repr = self.to_prufer(self._tree_repr[:], self.nodes)
|
57 |
+
return self._prufer_repr
|
58 |
+
|
59 |
+
@property
|
60 |
+
def tree_repr(self):
|
61 |
+
"""Returns the tree representation of the Prufer object.
|
62 |
+
|
63 |
+
Examples
|
64 |
+
========
|
65 |
+
|
66 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
67 |
+
>>> Prufer([[0, 3], [1, 3], [2, 3], [3, 4], [4, 5]]).tree_repr
|
68 |
+
[[0, 3], [1, 3], [2, 3], [3, 4], [4, 5]]
|
69 |
+
>>> Prufer([1, 0, 0]).tree_repr
|
70 |
+
[[1, 2], [0, 1], [0, 3], [0, 4]]
|
71 |
+
|
72 |
+
See Also
|
73 |
+
========
|
74 |
+
|
75 |
+
to_tree
|
76 |
+
|
77 |
+
"""
|
78 |
+
if self._tree_repr is None:
|
79 |
+
self._tree_repr = self.to_tree(self._prufer_repr[:])
|
80 |
+
return self._tree_repr
|
81 |
+
|
82 |
+
@property
|
83 |
+
def nodes(self):
|
84 |
+
"""Returns the number of nodes in the tree.
|
85 |
+
|
86 |
+
Examples
|
87 |
+
========
|
88 |
+
|
89 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
90 |
+
>>> Prufer([[0, 3], [1, 3], [2, 3], [3, 4], [4, 5]]).nodes
|
91 |
+
6
|
92 |
+
>>> Prufer([1, 0, 0]).nodes
|
93 |
+
5
|
94 |
+
|
95 |
+
"""
|
96 |
+
return self._nodes
|
97 |
+
|
98 |
+
@property
|
99 |
+
def rank(self):
|
100 |
+
"""Returns the rank of the Prufer sequence.
|
101 |
+
|
102 |
+
Examples
|
103 |
+
========
|
104 |
+
|
105 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
106 |
+
>>> p = Prufer([[0, 3], [1, 3], [2, 3], [3, 4], [4, 5]])
|
107 |
+
>>> p.rank
|
108 |
+
778
|
109 |
+
>>> p.next(1).rank
|
110 |
+
779
|
111 |
+
>>> p.prev().rank
|
112 |
+
777
|
113 |
+
|
114 |
+
See Also
|
115 |
+
========
|
116 |
+
|
117 |
+
prufer_rank, next, prev, size
|
118 |
+
|
119 |
+
"""
|
120 |
+
if self._rank is None:
|
121 |
+
self._rank = self.prufer_rank()
|
122 |
+
return self._rank
|
123 |
+
|
124 |
+
@property
|
125 |
+
def size(self):
|
126 |
+
"""Return the number of possible trees of this Prufer object.
|
127 |
+
|
128 |
+
Examples
|
129 |
+
========
|
130 |
+
|
131 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
132 |
+
>>> Prufer([0]*4).size == Prufer([6]*4).size == 1296
|
133 |
+
True
|
134 |
+
|
135 |
+
See Also
|
136 |
+
========
|
137 |
+
|
138 |
+
prufer_rank, rank, next, prev
|
139 |
+
|
140 |
+
"""
|
141 |
+
return self.prev(self.rank).prev().rank + 1
|
142 |
+
|
143 |
+
@staticmethod
|
144 |
+
def to_prufer(tree, n):
|
145 |
+
"""Return the Prufer sequence for a tree given as a list of edges where
|
146 |
+
``n`` is the number of nodes in the tree.
|
147 |
+
|
148 |
+
Examples
|
149 |
+
========
|
150 |
+
|
151 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
152 |
+
>>> a = Prufer([[0, 1], [0, 2], [0, 3]])
|
153 |
+
>>> a.prufer_repr
|
154 |
+
[0, 0]
|
155 |
+
>>> Prufer.to_prufer([[0, 1], [0, 2], [0, 3]], 4)
|
156 |
+
[0, 0]
|
157 |
+
|
158 |
+
See Also
|
159 |
+
========
|
160 |
+
prufer_repr: returns Prufer sequence of a Prufer object.
|
161 |
+
|
162 |
+
"""
|
163 |
+
d = defaultdict(int)
|
164 |
+
L = []
|
165 |
+
for edge in tree:
|
166 |
+
# Increment the value of the corresponding
|
167 |
+
# node in the degree list as we encounter an
|
168 |
+
# edge involving it.
|
169 |
+
d[edge[0]] += 1
|
170 |
+
d[edge[1]] += 1
|
171 |
+
for i in range(n - 2):
|
172 |
+
# find the smallest leaf
|
173 |
+
for x in range(n):
|
174 |
+
if d[x] == 1:
|
175 |
+
break
|
176 |
+
# find the node it was connected to
|
177 |
+
y = None
|
178 |
+
for edge in tree:
|
179 |
+
if x == edge[0]:
|
180 |
+
y = edge[1]
|
181 |
+
elif x == edge[1]:
|
182 |
+
y = edge[0]
|
183 |
+
if y is not None:
|
184 |
+
break
|
185 |
+
# record and update
|
186 |
+
L.append(y)
|
187 |
+
for j in (x, y):
|
188 |
+
d[j] -= 1
|
189 |
+
if not d[j]:
|
190 |
+
d.pop(j)
|
191 |
+
tree.remove(edge)
|
192 |
+
return L
|
193 |
+
|
194 |
+
@staticmethod
|
195 |
+
def to_tree(prufer):
|
196 |
+
"""Return the tree (as a list of edges) of the given Prufer sequence.
|
197 |
+
|
198 |
+
Examples
|
199 |
+
========
|
200 |
+
|
201 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
202 |
+
>>> a = Prufer([0, 2], 4)
|
203 |
+
>>> a.tree_repr
|
204 |
+
[[0, 1], [0, 2], [2, 3]]
|
205 |
+
>>> Prufer.to_tree([0, 2])
|
206 |
+
[[0, 1], [0, 2], [2, 3]]
|
207 |
+
|
208 |
+
References
|
209 |
+
==========
|
210 |
+
|
211 |
+
.. [1] https://hamberg.no/erlend/posts/2010-11-06-prufer-sequence-compact-tree-representation.html
|
212 |
+
|
213 |
+
See Also
|
214 |
+
========
|
215 |
+
tree_repr: returns tree representation of a Prufer object.
|
216 |
+
|
217 |
+
"""
|
218 |
+
tree = []
|
219 |
+
last = []
|
220 |
+
n = len(prufer) + 2
|
221 |
+
d = defaultdict(lambda: 1)
|
222 |
+
for p in prufer:
|
223 |
+
d[p] += 1
|
224 |
+
for i in prufer:
|
225 |
+
for j in range(n):
|
226 |
+
# find the smallest leaf (degree = 1)
|
227 |
+
if d[j] == 1:
|
228 |
+
break
|
229 |
+
# (i, j) is the new edge that we append to the tree
|
230 |
+
# and remove from the degree dictionary
|
231 |
+
d[i] -= 1
|
232 |
+
d[j] -= 1
|
233 |
+
tree.append(sorted([i, j]))
|
234 |
+
last = [i for i in range(n) if d[i] == 1] or [0, 1]
|
235 |
+
tree.append(last)
|
236 |
+
|
237 |
+
return tree
|
238 |
+
|
239 |
+
@staticmethod
|
240 |
+
def edges(*runs):
|
241 |
+
"""Return a list of edges and the number of nodes from the given runs
|
242 |
+
that connect nodes in an integer-labelled tree.
|
243 |
+
|
244 |
+
All node numbers will be shifted so that the minimum node is 0. It is
|
245 |
+
not a problem if edges are repeated in the runs; only unique edges are
|
246 |
+
returned. There is no assumption made about what the range of the node
|
247 |
+
labels should be, but all nodes from the smallest through the largest
|
248 |
+
must be present.
|
249 |
+
|
250 |
+
Examples
|
251 |
+
========
|
252 |
+
|
253 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
254 |
+
>>> Prufer.edges([1, 2, 3], [2, 4, 5]) # a T
|
255 |
+
([[0, 1], [1, 2], [1, 3], [3, 4]], 5)
|
256 |
+
|
257 |
+
Duplicate edges are removed:
|
258 |
+
|
259 |
+
>>> Prufer.edges([0, 1, 2, 3], [1, 4, 5], [1, 4, 6]) # a K
|
260 |
+
([[0, 1], [1, 2], [1, 4], [2, 3], [4, 5], [4, 6]], 7)
|
261 |
+
|
262 |
+
"""
|
263 |
+
e = set()
|
264 |
+
nmin = runs[0][0]
|
265 |
+
for r in runs:
|
266 |
+
for i in range(len(r) - 1):
|
267 |
+
a, b = r[i: i + 2]
|
268 |
+
if b < a:
|
269 |
+
a, b = b, a
|
270 |
+
e.add((a, b))
|
271 |
+
rv = []
|
272 |
+
got = set()
|
273 |
+
nmin = nmax = None
|
274 |
+
for ei in e:
|
275 |
+
for i in ei:
|
276 |
+
got.add(i)
|
277 |
+
nmin = min(ei[0], nmin) if nmin is not None else ei[0]
|
278 |
+
nmax = max(ei[1], nmax) if nmax is not None else ei[1]
|
279 |
+
rv.append(list(ei))
|
280 |
+
missing = set(range(nmin, nmax + 1)) - got
|
281 |
+
if missing:
|
282 |
+
missing = [i + nmin for i in missing]
|
283 |
+
if len(missing) == 1:
|
284 |
+
msg = 'Node %s is missing.' % missing.pop()
|
285 |
+
else:
|
286 |
+
msg = 'Nodes %s are missing.' % sorted(missing)
|
287 |
+
raise ValueError(msg)
|
288 |
+
if nmin != 0:
|
289 |
+
for i, ei in enumerate(rv):
|
290 |
+
rv[i] = [n - nmin for n in ei]
|
291 |
+
nmax -= nmin
|
292 |
+
return sorted(rv), nmax + 1
|
293 |
+
|
294 |
+
def prufer_rank(self):
|
295 |
+
"""Computes the rank of a Prufer sequence.
|
296 |
+
|
297 |
+
Examples
|
298 |
+
========
|
299 |
+
|
300 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
301 |
+
>>> a = Prufer([[0, 1], [0, 2], [0, 3]])
|
302 |
+
>>> a.prufer_rank()
|
303 |
+
0
|
304 |
+
|
305 |
+
See Also
|
306 |
+
========
|
307 |
+
|
308 |
+
rank, next, prev, size
|
309 |
+
|
310 |
+
"""
|
311 |
+
r = 0
|
312 |
+
p = 1
|
313 |
+
for i in range(self.nodes - 3, -1, -1):
|
314 |
+
r += p*self.prufer_repr[i]
|
315 |
+
p *= self.nodes
|
316 |
+
return r
|
317 |
+
|
318 |
+
@classmethod
|
319 |
+
def unrank(self, rank, n):
|
320 |
+
"""Finds the unranked Prufer sequence.
|
321 |
+
|
322 |
+
Examples
|
323 |
+
========
|
324 |
+
|
325 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
326 |
+
>>> Prufer.unrank(0, 4)
|
327 |
+
Prufer([0, 0])
|
328 |
+
|
329 |
+
"""
|
330 |
+
n, rank = as_int(n), as_int(rank)
|
331 |
+
L = defaultdict(int)
|
332 |
+
for i in range(n - 3, -1, -1):
|
333 |
+
L[i] = rank % n
|
334 |
+
rank = (rank - L[i])//n
|
335 |
+
return Prufer([L[i] for i in range(len(L))])
|
336 |
+
|
337 |
+
def __new__(cls, *args, **kw_args):
|
338 |
+
"""The constructor for the Prufer object.
|
339 |
+
|
340 |
+
Examples
|
341 |
+
========
|
342 |
+
|
343 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
344 |
+
|
345 |
+
A Prufer object can be constructed from a list of edges:
|
346 |
+
|
347 |
+
>>> a = Prufer([[0, 1], [0, 2], [0, 3]])
|
348 |
+
>>> a.prufer_repr
|
349 |
+
[0, 0]
|
350 |
+
|
351 |
+
If the number of nodes is given, no checking of the nodes will
|
352 |
+
be performed; it will be assumed that nodes 0 through n - 1 are
|
353 |
+
present:
|
354 |
+
|
355 |
+
>>> Prufer([[0, 1], [0, 2], [0, 3]], 4)
|
356 |
+
Prufer([[0, 1], [0, 2], [0, 3]], 4)
|
357 |
+
|
358 |
+
A Prufer object can be constructed from a Prufer sequence:
|
359 |
+
|
360 |
+
>>> b = Prufer([1, 3])
|
361 |
+
>>> b.tree_repr
|
362 |
+
[[0, 1], [1, 3], [2, 3]]
|
363 |
+
|
364 |
+
"""
|
365 |
+
arg0 = Array(args[0]) if args[0] else Tuple()
|
366 |
+
args = (arg0,) + tuple(_sympify(arg) for arg in args[1:])
|
367 |
+
ret_obj = Basic.__new__(cls, *args, **kw_args)
|
368 |
+
args = [list(args[0])]
|
369 |
+
if args[0] and iterable(args[0][0]):
|
370 |
+
if not args[0][0]:
|
371 |
+
raise ValueError(
|
372 |
+
'Prufer expects at least one edge in the tree.')
|
373 |
+
if len(args) > 1:
|
374 |
+
nnodes = args[1]
|
375 |
+
else:
|
376 |
+
nodes = set(flatten(args[0]))
|
377 |
+
nnodes = max(nodes) + 1
|
378 |
+
if nnodes != len(nodes):
|
379 |
+
missing = set(range(nnodes)) - nodes
|
380 |
+
if len(missing) == 1:
|
381 |
+
msg = 'Node %s is missing.' % missing.pop()
|
382 |
+
else:
|
383 |
+
msg = 'Nodes %s are missing.' % sorted(missing)
|
384 |
+
raise ValueError(msg)
|
385 |
+
ret_obj._tree_repr = [list(i) for i in args[0]]
|
386 |
+
ret_obj._nodes = nnodes
|
387 |
+
else:
|
388 |
+
ret_obj._prufer_repr = args[0]
|
389 |
+
ret_obj._nodes = len(ret_obj._prufer_repr) + 2
|
390 |
+
return ret_obj
|
391 |
+
|
392 |
+
def next(self, delta=1):
|
393 |
+
"""Generates the Prufer sequence that is delta beyond the current one.
|
394 |
+
|
395 |
+
Examples
|
396 |
+
========
|
397 |
+
|
398 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
399 |
+
>>> a = Prufer([[0, 1], [0, 2], [0, 3]])
|
400 |
+
>>> b = a.next(1) # == a.next()
|
401 |
+
>>> b.tree_repr
|
402 |
+
[[0, 2], [0, 1], [1, 3]]
|
403 |
+
>>> b.rank
|
404 |
+
1
|
405 |
+
|
406 |
+
See Also
|
407 |
+
========
|
408 |
+
|
409 |
+
prufer_rank, rank, prev, size
|
410 |
+
|
411 |
+
"""
|
412 |
+
return Prufer.unrank(self.rank + delta, self.nodes)
|
413 |
+
|
414 |
+
def prev(self, delta=1):
|
415 |
+
"""Generates the Prufer sequence that is -delta before the current one.
|
416 |
+
|
417 |
+
Examples
|
418 |
+
========
|
419 |
+
|
420 |
+
>>> from sympy.combinatorics.prufer import Prufer
|
421 |
+
>>> a = Prufer([[0, 1], [1, 2], [2, 3], [1, 4]])
|
422 |
+
>>> a.rank
|
423 |
+
36
|
424 |
+
>>> b = a.prev()
|
425 |
+
>>> b
|
426 |
+
Prufer([1, 2, 0])
|
427 |
+
>>> b.rank
|
428 |
+
35
|
429 |
+
|
430 |
+
See Also
|
431 |
+
========
|
432 |
+
|
433 |
+
prufer_rank, rank, next, size
|
434 |
+
|
435 |
+
"""
|
436 |
+
return Prufer.unrank(self.rank -delta, self.nodes)
|
venv/lib/python3.10/site-packages/sympy/combinatorics/rewritingsystem.py
ADDED
@@ -0,0 +1,453 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import deque
|
2 |
+
from sympy.combinatorics.rewritingsystem_fsm import StateMachine
|
3 |
+
|
4 |
+
class RewritingSystem:
|
5 |
+
'''
|
6 |
+
A class implementing rewriting systems for `FpGroup`s.
|
7 |
+
|
8 |
+
References
|
9 |
+
==========
|
10 |
+
.. [1] Epstein, D., Holt, D. and Rees, S. (1991).
|
11 |
+
The use of Knuth-Bendix methods to solve the word problem in automatic groups.
|
12 |
+
Journal of Symbolic Computation, 12(4-5), pp.397-414.
|
13 |
+
|
14 |
+
.. [2] GAP's Manual on its KBMAG package
|
15 |
+
https://www.gap-system.org/Manuals/pkg/kbmag-1.5.3/doc/manual.pdf
|
16 |
+
|
17 |
+
'''
|
18 |
+
def __init__(self, group):
|
19 |
+
self.group = group
|
20 |
+
self.alphabet = group.generators
|
21 |
+
self._is_confluent = None
|
22 |
+
|
23 |
+
# these values are taken from [2]
|
24 |
+
self.maxeqns = 32767 # max rules
|
25 |
+
self.tidyint = 100 # rules before tidying
|
26 |
+
|
27 |
+
# _max_exceeded is True if maxeqns is exceeded
|
28 |
+
# at any point
|
29 |
+
self._max_exceeded = False
|
30 |
+
|
31 |
+
# Reduction automaton
|
32 |
+
self.reduction_automaton = None
|
33 |
+
self._new_rules = {}
|
34 |
+
|
35 |
+
# dictionary of reductions
|
36 |
+
self.rules = {}
|
37 |
+
self.rules_cache = deque([], 50)
|
38 |
+
self._init_rules()
|
39 |
+
|
40 |
+
|
41 |
+
# All the transition symbols in the automaton
|
42 |
+
generators = list(self.alphabet)
|
43 |
+
generators += [gen**-1 for gen in generators]
|
44 |
+
# Create a finite state machine as an instance of the StateMachine object
|
45 |
+
self.reduction_automaton = StateMachine('Reduction automaton for '+ repr(self.group), generators)
|
46 |
+
self.construct_automaton()
|
47 |
+
|
48 |
+
def set_max(self, n):
|
49 |
+
'''
|
50 |
+
Set the maximum number of rules that can be defined
|
51 |
+
|
52 |
+
'''
|
53 |
+
if n > self.maxeqns:
|
54 |
+
self._max_exceeded = False
|
55 |
+
self.maxeqns = n
|
56 |
+
return
|
57 |
+
|
58 |
+
@property
|
59 |
+
def is_confluent(self):
|
60 |
+
'''
|
61 |
+
Return `True` if the system is confluent
|
62 |
+
|
63 |
+
'''
|
64 |
+
if self._is_confluent is None:
|
65 |
+
self._is_confluent = self._check_confluence()
|
66 |
+
return self._is_confluent
|
67 |
+
|
68 |
+
def _init_rules(self):
|
69 |
+
identity = self.group.free_group.identity
|
70 |
+
for r in self.group.relators:
|
71 |
+
self.add_rule(r, identity)
|
72 |
+
self._remove_redundancies()
|
73 |
+
return
|
74 |
+
|
75 |
+
def _add_rule(self, r1, r2):
|
76 |
+
'''
|
77 |
+
Add the rule r1 -> r2 with no checking or further
|
78 |
+
deductions
|
79 |
+
|
80 |
+
'''
|
81 |
+
if len(self.rules) + 1 > self.maxeqns:
|
82 |
+
self._is_confluent = self._check_confluence()
|
83 |
+
self._max_exceeded = True
|
84 |
+
raise RuntimeError("Too many rules were defined.")
|
85 |
+
self.rules[r1] = r2
|
86 |
+
# Add the newly added rule to the `new_rules` dictionary.
|
87 |
+
if self.reduction_automaton:
|
88 |
+
self._new_rules[r1] = r2
|
89 |
+
|
90 |
+
def add_rule(self, w1, w2, check=False):
|
91 |
+
new_keys = set()
|
92 |
+
|
93 |
+
if w1 == w2:
|
94 |
+
return new_keys
|
95 |
+
|
96 |
+
if w1 < w2:
|
97 |
+
w1, w2 = w2, w1
|
98 |
+
|
99 |
+
if (w1, w2) in self.rules_cache:
|
100 |
+
return new_keys
|
101 |
+
self.rules_cache.append((w1, w2))
|
102 |
+
|
103 |
+
s1, s2 = w1, w2
|
104 |
+
|
105 |
+
# The following is the equivalent of checking
|
106 |
+
# s1 for overlaps with the implicit reductions
|
107 |
+
# {g*g**-1 -> <identity>} and {g**-1*g -> <identity>}
|
108 |
+
# for any generator g without installing the
|
109 |
+
# redundant rules that would result from processing
|
110 |
+
# the overlaps. See [1], Section 3 for details.
|
111 |
+
|
112 |
+
if len(s1) - len(s2) < 3:
|
113 |
+
if s1 not in self.rules:
|
114 |
+
new_keys.add(s1)
|
115 |
+
if not check:
|
116 |
+
self._add_rule(s1, s2)
|
117 |
+
if s2**-1 > s1**-1 and s2**-1 not in self.rules:
|
118 |
+
new_keys.add(s2**-1)
|
119 |
+
if not check:
|
120 |
+
self._add_rule(s2**-1, s1**-1)
|
121 |
+
|
122 |
+
# overlaps on the right
|
123 |
+
while len(s1) - len(s2) > -1:
|
124 |
+
g = s1[len(s1)-1]
|
125 |
+
s1 = s1.subword(0, len(s1)-1)
|
126 |
+
s2 = s2*g**-1
|
127 |
+
if len(s1) - len(s2) < 0:
|
128 |
+
if s2 not in self.rules:
|
129 |
+
if not check:
|
130 |
+
self._add_rule(s2, s1)
|
131 |
+
new_keys.add(s2)
|
132 |
+
elif len(s1) - len(s2) < 3:
|
133 |
+
new = self.add_rule(s1, s2, check)
|
134 |
+
new_keys.update(new)
|
135 |
+
|
136 |
+
# overlaps on the left
|
137 |
+
while len(w1) - len(w2) > -1:
|
138 |
+
g = w1[0]
|
139 |
+
w1 = w1.subword(1, len(w1))
|
140 |
+
w2 = g**-1*w2
|
141 |
+
if len(w1) - len(w2) < 0:
|
142 |
+
if w2 not in self.rules:
|
143 |
+
if not check:
|
144 |
+
self._add_rule(w2, w1)
|
145 |
+
new_keys.add(w2)
|
146 |
+
elif len(w1) - len(w2) < 3:
|
147 |
+
new = self.add_rule(w1, w2, check)
|
148 |
+
new_keys.update(new)
|
149 |
+
|
150 |
+
return new_keys
|
151 |
+
|
152 |
+
def _remove_redundancies(self, changes=False):
|
153 |
+
'''
|
154 |
+
Reduce left- and right-hand sides of reduction rules
|
155 |
+
and remove redundant equations (i.e. those for which
|
156 |
+
lhs == rhs). If `changes` is `True`, return a set
|
157 |
+
containing the removed keys and a set containing the
|
158 |
+
added keys
|
159 |
+
|
160 |
+
'''
|
161 |
+
removed = set()
|
162 |
+
added = set()
|
163 |
+
rules = self.rules.copy()
|
164 |
+
for r in rules:
|
165 |
+
v = self.reduce(r, exclude=r)
|
166 |
+
w = self.reduce(rules[r])
|
167 |
+
if v != r:
|
168 |
+
del self.rules[r]
|
169 |
+
removed.add(r)
|
170 |
+
if v > w:
|
171 |
+
added.add(v)
|
172 |
+
self.rules[v] = w
|
173 |
+
elif v < w:
|
174 |
+
added.add(w)
|
175 |
+
self.rules[w] = v
|
176 |
+
else:
|
177 |
+
self.rules[v] = w
|
178 |
+
if changes:
|
179 |
+
return removed, added
|
180 |
+
return
|
181 |
+
|
182 |
+
def make_confluent(self, check=False):
|
183 |
+
'''
|
184 |
+
Try to make the system confluent using the Knuth-Bendix
|
185 |
+
completion algorithm
|
186 |
+
|
187 |
+
'''
|
188 |
+
if self._max_exceeded:
|
189 |
+
return self._is_confluent
|
190 |
+
lhs = list(self.rules.keys())
|
191 |
+
|
192 |
+
def _overlaps(r1, r2):
|
193 |
+
len1 = len(r1)
|
194 |
+
len2 = len(r2)
|
195 |
+
result = []
|
196 |
+
for j in range(1, len1 + len2):
|
197 |
+
if (r1.subword(len1 - j, len1 + len2 - j, strict=False)
|
198 |
+
== r2.subword(j - len1, j, strict=False)):
|
199 |
+
a = r1.subword(0, len1-j, strict=False)
|
200 |
+
a = a*r2.subword(0, j-len1, strict=False)
|
201 |
+
b = r2.subword(j-len1, j, strict=False)
|
202 |
+
c = r2.subword(j, len2, strict=False)
|
203 |
+
c = c*r1.subword(len1 + len2 - j, len1, strict=False)
|
204 |
+
result.append(a*b*c)
|
205 |
+
return result
|
206 |
+
|
207 |
+
def _process_overlap(w, r1, r2, check):
|
208 |
+
s = w.eliminate_word(r1, self.rules[r1])
|
209 |
+
s = self.reduce(s)
|
210 |
+
t = w.eliminate_word(r2, self.rules[r2])
|
211 |
+
t = self.reduce(t)
|
212 |
+
if s != t:
|
213 |
+
if check:
|
214 |
+
# system not confluent
|
215 |
+
return [0]
|
216 |
+
try:
|
217 |
+
new_keys = self.add_rule(t, s, check)
|
218 |
+
return new_keys
|
219 |
+
except RuntimeError:
|
220 |
+
return False
|
221 |
+
return
|
222 |
+
|
223 |
+
added = 0
|
224 |
+
i = 0
|
225 |
+
while i < len(lhs):
|
226 |
+
r1 = lhs[i]
|
227 |
+
i += 1
|
228 |
+
# j could be i+1 to not
|
229 |
+
# check each pair twice but lhs
|
230 |
+
# is extended in the loop and the new
|
231 |
+
# elements have to be checked with the
|
232 |
+
# preceding ones. there is probably a better way
|
233 |
+
# to handle this
|
234 |
+
j = 0
|
235 |
+
while j < len(lhs):
|
236 |
+
r2 = lhs[j]
|
237 |
+
j += 1
|
238 |
+
if r1 == r2:
|
239 |
+
continue
|
240 |
+
overlaps = _overlaps(r1, r2)
|
241 |
+
overlaps.extend(_overlaps(r1**-1, r2))
|
242 |
+
if not overlaps:
|
243 |
+
continue
|
244 |
+
for w in overlaps:
|
245 |
+
new_keys = _process_overlap(w, r1, r2, check)
|
246 |
+
if new_keys:
|
247 |
+
if check:
|
248 |
+
return False
|
249 |
+
lhs.extend(new_keys)
|
250 |
+
added += len(new_keys)
|
251 |
+
elif new_keys == False:
|
252 |
+
# too many rules were added so the process
|
253 |
+
# couldn't complete
|
254 |
+
return self._is_confluent
|
255 |
+
|
256 |
+
if added > self.tidyint and not check:
|
257 |
+
# tidy up
|
258 |
+
r, a = self._remove_redundancies(changes=True)
|
259 |
+
added = 0
|
260 |
+
if r:
|
261 |
+
# reset i since some elements were removed
|
262 |
+
i = min([lhs.index(s) for s in r])
|
263 |
+
lhs = [l for l in lhs if l not in r]
|
264 |
+
lhs.extend(a)
|
265 |
+
if r1 in r:
|
266 |
+
# r1 was removed as redundant
|
267 |
+
break
|
268 |
+
|
269 |
+
self._is_confluent = True
|
270 |
+
if not check:
|
271 |
+
self._remove_redundancies()
|
272 |
+
return True
|
273 |
+
|
274 |
+
def _check_confluence(self):
|
275 |
+
return self.make_confluent(check=True)
|
276 |
+
|
277 |
+
def reduce(self, word, exclude=None):
|
278 |
+
'''
|
279 |
+
Apply reduction rules to `word` excluding the reduction rule
|
280 |
+
for the lhs equal to `exclude`
|
281 |
+
|
282 |
+
'''
|
283 |
+
rules = {r: self.rules[r] for r in self.rules if r != exclude}
|
284 |
+
# the following is essentially `eliminate_words()` code from the
|
285 |
+
# `FreeGroupElement` class, the only difference being the first
|
286 |
+
# "if" statement
|
287 |
+
again = True
|
288 |
+
new = word
|
289 |
+
while again:
|
290 |
+
again = False
|
291 |
+
for r in rules:
|
292 |
+
prev = new
|
293 |
+
if rules[r]**-1 > r**-1:
|
294 |
+
new = new.eliminate_word(r, rules[r], _all=True, inverse=False)
|
295 |
+
else:
|
296 |
+
new = new.eliminate_word(r, rules[r], _all=True)
|
297 |
+
if new != prev:
|
298 |
+
again = True
|
299 |
+
return new
|
300 |
+
|
301 |
+
def _compute_inverse_rules(self, rules):
|
302 |
+
'''
|
303 |
+
Compute the inverse rules for a given set of rules.
|
304 |
+
The inverse rules are used in the automaton for word reduction.
|
305 |
+
|
306 |
+
Arguments:
|
307 |
+
rules (dictionary): Rules for which the inverse rules are to computed.
|
308 |
+
|
309 |
+
Returns:
|
310 |
+
Dictionary of inverse_rules.
|
311 |
+
|
312 |
+
'''
|
313 |
+
inverse_rules = {}
|
314 |
+
for r in rules:
|
315 |
+
rule_key_inverse = r**-1
|
316 |
+
rule_value_inverse = (rules[r])**-1
|
317 |
+
if (rule_value_inverse < rule_key_inverse):
|
318 |
+
inverse_rules[rule_key_inverse] = rule_value_inverse
|
319 |
+
else:
|
320 |
+
inverse_rules[rule_value_inverse] = rule_key_inverse
|
321 |
+
return inverse_rules
|
322 |
+
|
323 |
+
def construct_automaton(self):
|
324 |
+
'''
|
325 |
+
Construct the automaton based on the set of reduction rules of the system.
|
326 |
+
|
327 |
+
Automata Design:
|
328 |
+
The accept states of the automaton are the proper prefixes of the left hand side of the rules.
|
329 |
+
The complete left hand side of the rules are the dead states of the automaton.
|
330 |
+
|
331 |
+
'''
|
332 |
+
self._add_to_automaton(self.rules)
|
333 |
+
|
334 |
+
def _add_to_automaton(self, rules):
|
335 |
+
'''
|
336 |
+
Add new states and transitions to the automaton.
|
337 |
+
|
338 |
+
Summary:
|
339 |
+
States corresponding to the new rules added to the system are computed and added to the automaton.
|
340 |
+
Transitions in the previously added states are also modified if necessary.
|
341 |
+
|
342 |
+
Arguments:
|
343 |
+
rules (dictionary) -- Dictionary of the newly added rules.
|
344 |
+
|
345 |
+
'''
|
346 |
+
# Automaton variables
|
347 |
+
automaton_alphabet = []
|
348 |
+
proper_prefixes = {}
|
349 |
+
|
350 |
+
# compute the inverses of all the new rules added
|
351 |
+
all_rules = rules
|
352 |
+
inverse_rules = self._compute_inverse_rules(all_rules)
|
353 |
+
all_rules.update(inverse_rules)
|
354 |
+
|
355 |
+
# Keep track of the accept_states.
|
356 |
+
accept_states = []
|
357 |
+
|
358 |
+
for rule in all_rules:
|
359 |
+
# The symbols present in the new rules are the symbols to be verified at each state.
|
360 |
+
# computes the automaton_alphabet, as the transitions solely depend upon the new states.
|
361 |
+
automaton_alphabet += rule.letter_form_elm
|
362 |
+
# Compute the proper prefixes for every rule.
|
363 |
+
proper_prefixes[rule] = []
|
364 |
+
letter_word_array = list(rule.letter_form_elm)
|
365 |
+
len_letter_word_array = len(letter_word_array)
|
366 |
+
for i in range (1, len_letter_word_array):
|
367 |
+
letter_word_array[i] = letter_word_array[i-1]*letter_word_array[i]
|
368 |
+
# Add accept states.
|
369 |
+
elem = letter_word_array[i-1]
|
370 |
+
if elem not in self.reduction_automaton.states:
|
371 |
+
self.reduction_automaton.add_state(elem, state_type='a')
|
372 |
+
accept_states.append(elem)
|
373 |
+
proper_prefixes[rule] = letter_word_array
|
374 |
+
# Check for overlaps between dead and accept states.
|
375 |
+
if rule in accept_states:
|
376 |
+
self.reduction_automaton.states[rule].state_type = 'd'
|
377 |
+
self.reduction_automaton.states[rule].rh_rule = all_rules[rule]
|
378 |
+
accept_states.remove(rule)
|
379 |
+
# Add dead states
|
380 |
+
if rule not in self.reduction_automaton.states:
|
381 |
+
self.reduction_automaton.add_state(rule, state_type='d', rh_rule=all_rules[rule])
|
382 |
+
|
383 |
+
automaton_alphabet = set(automaton_alphabet)
|
384 |
+
|
385 |
+
# Add new transitions for every state.
|
386 |
+
for state in self.reduction_automaton.states:
|
387 |
+
current_state_name = state
|
388 |
+
current_state_type = self.reduction_automaton.states[state].state_type
|
389 |
+
# Transitions will be modified only when suffixes of the current_state
|
390 |
+
# belongs to the proper_prefixes of the new rules.
|
391 |
+
# The rest are ignored if they cannot lead to a dead state after a finite number of transisitons.
|
392 |
+
if current_state_type == 's':
|
393 |
+
for letter in automaton_alphabet:
|
394 |
+
if letter in self.reduction_automaton.states:
|
395 |
+
self.reduction_automaton.states[state].add_transition(letter, letter)
|
396 |
+
else:
|
397 |
+
self.reduction_automaton.states[state].add_transition(letter, current_state_name)
|
398 |
+
elif current_state_type == 'a':
|
399 |
+
# Check if the transition to any new state in possible.
|
400 |
+
for letter in automaton_alphabet:
|
401 |
+
_next = current_state_name*letter
|
402 |
+
while len(_next) and _next not in self.reduction_automaton.states:
|
403 |
+
_next = _next.subword(1, len(_next))
|
404 |
+
if not len(_next):
|
405 |
+
_next = 'start'
|
406 |
+
self.reduction_automaton.states[state].add_transition(letter, _next)
|
407 |
+
|
408 |
+
# Add transitions for new states. All symbols used in the automaton are considered here.
|
409 |
+
# Ignore this if `reduction_automaton.automaton_alphabet` = `automaton_alphabet`.
|
410 |
+
if len(self.reduction_automaton.automaton_alphabet) != len(automaton_alphabet):
|
411 |
+
for state in accept_states:
|
412 |
+
current_state_name = state
|
413 |
+
for letter in self.reduction_automaton.automaton_alphabet:
|
414 |
+
_next = current_state_name*letter
|
415 |
+
while len(_next) and _next not in self.reduction_automaton.states:
|
416 |
+
_next = _next.subword(1, len(_next))
|
417 |
+
if not len(_next):
|
418 |
+
_next = 'start'
|
419 |
+
self.reduction_automaton.states[state].add_transition(letter, _next)
|
420 |
+
|
421 |
+
def reduce_using_automaton(self, word):
|
422 |
+
'''
|
423 |
+
Reduce a word using an automaton.
|
424 |
+
|
425 |
+
Summary:
|
426 |
+
All the symbols of the word are stored in an array and are given as the input to the automaton.
|
427 |
+
If the automaton reaches a dead state that subword is replaced and the automaton is run from the beginning.
|
428 |
+
The complete word has to be replaced when the word is read and the automaton reaches a dead state.
|
429 |
+
So, this process is repeated until the word is read completely and the automaton reaches the accept state.
|
430 |
+
|
431 |
+
Arguments:
|
432 |
+
word (instance of FreeGroupElement) -- Word that needs to be reduced.
|
433 |
+
|
434 |
+
'''
|
435 |
+
# Modify the automaton if new rules are found.
|
436 |
+
if self._new_rules:
|
437 |
+
self._add_to_automaton(self._new_rules)
|
438 |
+
self._new_rules = {}
|
439 |
+
|
440 |
+
flag = 1
|
441 |
+
while flag:
|
442 |
+
flag = 0
|
443 |
+
current_state = self.reduction_automaton.states['start']
|
444 |
+
for i, s in enumerate(word.letter_form_elm):
|
445 |
+
next_state_name = current_state.transitions[s]
|
446 |
+
next_state = self.reduction_automaton.states[next_state_name]
|
447 |
+
if next_state.state_type == 'd':
|
448 |
+
subst = next_state.rh_rule
|
449 |
+
word = word.substituted_word(i - len(next_state_name) + 1, i+1, subst)
|
450 |
+
flag = 1
|
451 |
+
break
|
452 |
+
current_state = next_state
|
453 |
+
return word
|
venv/lib/python3.10/site-packages/sympy/combinatorics/rewritingsystem_fsm.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class State:
|
2 |
+
'''
|
3 |
+
A representation of a state managed by a ``StateMachine``.
|
4 |
+
|
5 |
+
Attributes:
|
6 |
+
name (instance of FreeGroupElement or string) -- State name which is also assigned to the Machine.
|
7 |
+
transisitons (OrderedDict) -- Represents all the transitions of the state object.
|
8 |
+
state_type (string) -- Denotes the type (accept/start/dead) of the state.
|
9 |
+
rh_rule (instance of FreeGroupElement) -- right hand rule for dead state.
|
10 |
+
state_machine (instance of StateMachine object) -- The finite state machine that the state belongs to.
|
11 |
+
'''
|
12 |
+
|
13 |
+
def __init__(self, name, state_machine, state_type=None, rh_rule=None):
|
14 |
+
self.name = name
|
15 |
+
self.transitions = {}
|
16 |
+
self.state_machine = state_machine
|
17 |
+
self.state_type = state_type[0]
|
18 |
+
self.rh_rule = rh_rule
|
19 |
+
|
20 |
+
def add_transition(self, letter, state):
|
21 |
+
'''
|
22 |
+
Add a transition from the current state to a new state.
|
23 |
+
|
24 |
+
Keyword Arguments:
|
25 |
+
letter -- The alphabet element the current state reads to make the state transition.
|
26 |
+
state -- This will be an instance of the State object which represents a new state after in the transition after the alphabet is read.
|
27 |
+
|
28 |
+
'''
|
29 |
+
self.transitions[letter] = state
|
30 |
+
|
31 |
+
class StateMachine:
|
32 |
+
'''
|
33 |
+
Representation of a finite state machine the manages the states and the transitions of the automaton.
|
34 |
+
|
35 |
+
Attributes:
|
36 |
+
states (dictionary) -- Collection of all registered `State` objects.
|
37 |
+
name (str) -- Name of the state machine.
|
38 |
+
'''
|
39 |
+
|
40 |
+
def __init__(self, name, automaton_alphabet):
|
41 |
+
self.name = name
|
42 |
+
self.automaton_alphabet = automaton_alphabet
|
43 |
+
self.states = {} # Contains all the states in the machine.
|
44 |
+
self.add_state('start', state_type='s')
|
45 |
+
|
46 |
+
def add_state(self, state_name, state_type=None, rh_rule=None):
|
47 |
+
'''
|
48 |
+
Instantiate a state object and stores it in the 'states' dictionary.
|
49 |
+
|
50 |
+
Arguments:
|
51 |
+
state_name (instance of FreeGroupElement or string) -- name of the new states.
|
52 |
+
state_type (string) -- Denotes the type (accept/start/dead) of the state added.
|
53 |
+
rh_rule (instance of FreeGroupElement) -- right hand rule for dead state.
|
54 |
+
|
55 |
+
'''
|
56 |
+
new_state = State(state_name, self, state_type, rh_rule)
|
57 |
+
self.states[state_name] = new_state
|
58 |
+
|
59 |
+
def __repr__(self):
|
60 |
+
return "%s" % (self.name)
|
venv/lib/python3.10/site-packages/sympy/combinatorics/schur_number.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
The Schur number S(k) is the largest integer n for which the interval [1,n]
|
3 |
+
can be partitioned into k sum-free sets.(https://mathworld.wolfram.com/SchurNumber.html)
|
4 |
+
"""
|
5 |
+
import math
|
6 |
+
from sympy.core import S
|
7 |
+
from sympy.core.basic import Basic
|
8 |
+
from sympy.core.function import Function
|
9 |
+
from sympy.core.numbers import Integer
|
10 |
+
|
11 |
+
|
12 |
+
class SchurNumber(Function):
|
13 |
+
r"""
|
14 |
+
This function creates a SchurNumber object
|
15 |
+
which is evaluated for `k \le 5` otherwise only
|
16 |
+
the lower bound information can be retrieved.
|
17 |
+
|
18 |
+
Examples
|
19 |
+
========
|
20 |
+
|
21 |
+
>>> from sympy.combinatorics.schur_number import SchurNumber
|
22 |
+
|
23 |
+
Since S(3) = 13, hence the output is a number
|
24 |
+
>>> SchurNumber(3)
|
25 |
+
13
|
26 |
+
|
27 |
+
We do not know the Schur number for values greater than 5, hence
|
28 |
+
only the object is returned
|
29 |
+
>>> SchurNumber(6)
|
30 |
+
SchurNumber(6)
|
31 |
+
|
32 |
+
Now, the lower bound information can be retrieved using lower_bound()
|
33 |
+
method
|
34 |
+
>>> SchurNumber(6).lower_bound()
|
35 |
+
536
|
36 |
+
|
37 |
+
"""
|
38 |
+
|
39 |
+
@classmethod
|
40 |
+
def eval(cls, k):
|
41 |
+
if k.is_Number:
|
42 |
+
if k is S.Infinity:
|
43 |
+
return S.Infinity
|
44 |
+
if k.is_zero:
|
45 |
+
return S.Zero
|
46 |
+
if not k.is_integer or k.is_negative:
|
47 |
+
raise ValueError("k should be a positive integer")
|
48 |
+
first_known_schur_numbers = {1: 1, 2: 4, 3: 13, 4: 44, 5: 160}
|
49 |
+
if k <= 5:
|
50 |
+
return Integer(first_known_schur_numbers[k])
|
51 |
+
|
52 |
+
def lower_bound(self):
|
53 |
+
f_ = self.args[0]
|
54 |
+
# Improved lower bounds known for S(6) and S(7)
|
55 |
+
if f_ == 6:
|
56 |
+
return Integer(536)
|
57 |
+
if f_ == 7:
|
58 |
+
return Integer(1680)
|
59 |
+
# For other cases, use general expression
|
60 |
+
if f_.is_Integer:
|
61 |
+
return 3*self.func(f_ - 1).lower_bound() - 1
|
62 |
+
return (3**f_ - 1)/2
|
63 |
+
|
64 |
+
|
65 |
+
def _schur_subsets_number(n):
|
66 |
+
|
67 |
+
if n is S.Infinity:
|
68 |
+
raise ValueError("Input must be finite")
|
69 |
+
if n <= 0:
|
70 |
+
raise ValueError("n must be a non-zero positive integer.")
|
71 |
+
elif n <= 3:
|
72 |
+
min_k = 1
|
73 |
+
else:
|
74 |
+
min_k = math.ceil(math.log(2*n + 1, 3))
|
75 |
+
|
76 |
+
return Integer(min_k)
|
77 |
+
|
78 |
+
|
79 |
+
def schur_partition(n):
|
80 |
+
"""
|
81 |
+
|
82 |
+
This function returns the partition in the minimum number of sum-free subsets
|
83 |
+
according to the lower bound given by the Schur Number.
|
84 |
+
|
85 |
+
Parameters
|
86 |
+
==========
|
87 |
+
|
88 |
+
n: a number
|
89 |
+
n is the upper limit of the range [1, n] for which we need to find and
|
90 |
+
return the minimum number of free subsets according to the lower bound
|
91 |
+
of schur number
|
92 |
+
|
93 |
+
Returns
|
94 |
+
=======
|
95 |
+
|
96 |
+
List of lists
|
97 |
+
List of the minimum number of sum-free subsets
|
98 |
+
|
99 |
+
Notes
|
100 |
+
=====
|
101 |
+
|
102 |
+
It is possible for some n to make the partition into less
|
103 |
+
subsets since the only known Schur numbers are:
|
104 |
+
S(1) = 1, S(2) = 4, S(3) = 13, S(4) = 44.
|
105 |
+
e.g for n = 44 the lower bound from the function above is 5 subsets but it has been proven
|
106 |
+
that can be done with 4 subsets.
|
107 |
+
|
108 |
+
Examples
|
109 |
+
========
|
110 |
+
|
111 |
+
For n = 1, 2, 3 the answer is the set itself
|
112 |
+
|
113 |
+
>>> from sympy.combinatorics.schur_number import schur_partition
|
114 |
+
>>> schur_partition(2)
|
115 |
+
[[1, 2]]
|
116 |
+
|
117 |
+
For n > 3, the answer is the minimum number of sum-free subsets:
|
118 |
+
|
119 |
+
>>> schur_partition(5)
|
120 |
+
[[3, 2], [5], [1, 4]]
|
121 |
+
|
122 |
+
>>> schur_partition(8)
|
123 |
+
[[3, 2], [6, 5, 8], [1, 4, 7]]
|
124 |
+
"""
|
125 |
+
|
126 |
+
if isinstance(n, Basic) and not n.is_Number:
|
127 |
+
raise ValueError("Input value must be a number")
|
128 |
+
|
129 |
+
number_of_subsets = _schur_subsets_number(n)
|
130 |
+
if n == 1:
|
131 |
+
sum_free_subsets = [[1]]
|
132 |
+
elif n == 2:
|
133 |
+
sum_free_subsets = [[1, 2]]
|
134 |
+
elif n == 3:
|
135 |
+
sum_free_subsets = [[1, 2, 3]]
|
136 |
+
else:
|
137 |
+
sum_free_subsets = [[1, 4], [2, 3]]
|
138 |
+
|
139 |
+
while len(sum_free_subsets) < number_of_subsets:
|
140 |
+
sum_free_subsets = _generate_next_list(sum_free_subsets, n)
|
141 |
+
missed_elements = [3*k + 1 for k in range(len(sum_free_subsets), (n-1)//3 + 1)]
|
142 |
+
sum_free_subsets[-1] += missed_elements
|
143 |
+
|
144 |
+
return sum_free_subsets
|
145 |
+
|
146 |
+
|
147 |
+
def _generate_next_list(current_list, n):
|
148 |
+
new_list = []
|
149 |
+
|
150 |
+
for item in current_list:
|
151 |
+
temp_1 = [number*3 for number in item if number*3 <= n]
|
152 |
+
temp_2 = [number*3 - 1 for number in item if number*3 - 1 <= n]
|
153 |
+
new_item = temp_1 + temp_2
|
154 |
+
new_list.append(new_item)
|
155 |
+
|
156 |
+
last_list = [3*k + 1 for k in range(len(current_list)+1) if 3*k + 1 <= n]
|
157 |
+
new_list.append(last_list)
|
158 |
+
current_list = new_list
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from itertools import combinations
|
2 |
+
|
3 |
+
from sympy.combinatorics.graycode import GrayCode
|
4 |
+
|
5 |
+
|
6 |
+
class Subset():
|
7 |
+
"""
|
8 |
+
Represents a basic subset object.
|
9 |
+
|
10 |
+
Explanation
|
11 |
+
===========
|
12 |
+
|
13 |
+
We generate subsets using essentially two techniques,
|
14 |
+
binary enumeration and lexicographic enumeration.
|
15 |
+
The Subset class takes two arguments, the first one
|
16 |
+
describes the initial subset to consider and the second
|
17 |
+
describes the superset.
|
18 |
+
|
19 |
+
Examples
|
20 |
+
========
|
21 |
+
|
22 |
+
>>> from sympy.combinatorics import Subset
|
23 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
24 |
+
>>> a.next_binary().subset
|
25 |
+
['b']
|
26 |
+
>>> a.prev_binary().subset
|
27 |
+
['c']
|
28 |
+
"""
|
29 |
+
|
30 |
+
_rank_binary = None
|
31 |
+
_rank_lex = None
|
32 |
+
_rank_graycode = None
|
33 |
+
_subset = None
|
34 |
+
_superset = None
|
35 |
+
|
36 |
+
def __new__(cls, subset, superset):
|
37 |
+
"""
|
38 |
+
Default constructor.
|
39 |
+
|
40 |
+
It takes the ``subset`` and its ``superset`` as its parameters.
|
41 |
+
|
42 |
+
Examples
|
43 |
+
========
|
44 |
+
|
45 |
+
>>> from sympy.combinatorics import Subset
|
46 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
47 |
+
>>> a.subset
|
48 |
+
['c', 'd']
|
49 |
+
>>> a.superset
|
50 |
+
['a', 'b', 'c', 'd']
|
51 |
+
>>> a.size
|
52 |
+
2
|
53 |
+
"""
|
54 |
+
if len(subset) > len(superset):
|
55 |
+
raise ValueError('Invalid arguments have been provided. The '
|
56 |
+
'superset must be larger than the subset.')
|
57 |
+
for elem in subset:
|
58 |
+
if elem not in superset:
|
59 |
+
raise ValueError('The superset provided is invalid as it does '
|
60 |
+
'not contain the element {}'.format(elem))
|
61 |
+
obj = object.__new__(cls)
|
62 |
+
obj._subset = subset
|
63 |
+
obj._superset = superset
|
64 |
+
return obj
|
65 |
+
|
66 |
+
def __eq__(self, other):
|
67 |
+
"""Return a boolean indicating whether a == b on the basis of
|
68 |
+
whether both objects are of the class Subset and if the values
|
69 |
+
of the subset and superset attributes are the same.
|
70 |
+
"""
|
71 |
+
if not isinstance(other, Subset):
|
72 |
+
return NotImplemented
|
73 |
+
return self.subset == other.subset and self.superset == other.superset
|
74 |
+
|
75 |
+
def iterate_binary(self, k):
|
76 |
+
"""
|
77 |
+
This is a helper function. It iterates over the
|
78 |
+
binary subsets by ``k`` steps. This variable can be
|
79 |
+
both positive or negative.
|
80 |
+
|
81 |
+
Examples
|
82 |
+
========
|
83 |
+
|
84 |
+
>>> from sympy.combinatorics import Subset
|
85 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
86 |
+
>>> a.iterate_binary(-2).subset
|
87 |
+
['d']
|
88 |
+
>>> a = Subset(['a', 'b', 'c'], ['a', 'b', 'c', 'd'])
|
89 |
+
>>> a.iterate_binary(2).subset
|
90 |
+
[]
|
91 |
+
|
92 |
+
See Also
|
93 |
+
========
|
94 |
+
|
95 |
+
next_binary, prev_binary
|
96 |
+
"""
|
97 |
+
bin_list = Subset.bitlist_from_subset(self.subset, self.superset)
|
98 |
+
n = (int(''.join(bin_list), 2) + k) % 2**self.superset_size
|
99 |
+
bits = bin(n)[2:].rjust(self.superset_size, '0')
|
100 |
+
return Subset.subset_from_bitlist(self.superset, bits)
|
101 |
+
|
102 |
+
def next_binary(self):
|
103 |
+
"""
|
104 |
+
Generates the next binary ordered subset.
|
105 |
+
|
106 |
+
Examples
|
107 |
+
========
|
108 |
+
|
109 |
+
>>> from sympy.combinatorics import Subset
|
110 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
111 |
+
>>> a.next_binary().subset
|
112 |
+
['b']
|
113 |
+
>>> a = Subset(['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'])
|
114 |
+
>>> a.next_binary().subset
|
115 |
+
[]
|
116 |
+
|
117 |
+
See Also
|
118 |
+
========
|
119 |
+
|
120 |
+
prev_binary, iterate_binary
|
121 |
+
"""
|
122 |
+
return self.iterate_binary(1)
|
123 |
+
|
124 |
+
def prev_binary(self):
|
125 |
+
"""
|
126 |
+
Generates the previous binary ordered subset.
|
127 |
+
|
128 |
+
Examples
|
129 |
+
========
|
130 |
+
|
131 |
+
>>> from sympy.combinatorics import Subset
|
132 |
+
>>> a = Subset([], ['a', 'b', 'c', 'd'])
|
133 |
+
>>> a.prev_binary().subset
|
134 |
+
['a', 'b', 'c', 'd']
|
135 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
136 |
+
>>> a.prev_binary().subset
|
137 |
+
['c']
|
138 |
+
|
139 |
+
See Also
|
140 |
+
========
|
141 |
+
|
142 |
+
next_binary, iterate_binary
|
143 |
+
"""
|
144 |
+
return self.iterate_binary(-1)
|
145 |
+
|
146 |
+
def next_lexicographic(self):
|
147 |
+
"""
|
148 |
+
Generates the next lexicographically ordered subset.
|
149 |
+
|
150 |
+
Examples
|
151 |
+
========
|
152 |
+
|
153 |
+
>>> from sympy.combinatorics import Subset
|
154 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
155 |
+
>>> a.next_lexicographic().subset
|
156 |
+
['d']
|
157 |
+
>>> a = Subset(['d'], ['a', 'b', 'c', 'd'])
|
158 |
+
>>> a.next_lexicographic().subset
|
159 |
+
[]
|
160 |
+
|
161 |
+
See Also
|
162 |
+
========
|
163 |
+
|
164 |
+
prev_lexicographic
|
165 |
+
"""
|
166 |
+
i = self.superset_size - 1
|
167 |
+
indices = Subset.subset_indices(self.subset, self.superset)
|
168 |
+
|
169 |
+
if i in indices:
|
170 |
+
if i - 1 in indices:
|
171 |
+
indices.remove(i - 1)
|
172 |
+
else:
|
173 |
+
indices.remove(i)
|
174 |
+
i = i - 1
|
175 |
+
while i >= 0 and i not in indices:
|
176 |
+
i = i - 1
|
177 |
+
if i >= 0:
|
178 |
+
indices.remove(i)
|
179 |
+
indices.append(i+1)
|
180 |
+
else:
|
181 |
+
while i not in indices and i >= 0:
|
182 |
+
i = i - 1
|
183 |
+
indices.append(i + 1)
|
184 |
+
|
185 |
+
ret_set = []
|
186 |
+
super_set = self.superset
|
187 |
+
for i in indices:
|
188 |
+
ret_set.append(super_set[i])
|
189 |
+
return Subset(ret_set, super_set)
|
190 |
+
|
191 |
+
def prev_lexicographic(self):
|
192 |
+
"""
|
193 |
+
Generates the previous lexicographically ordered subset.
|
194 |
+
|
195 |
+
Examples
|
196 |
+
========
|
197 |
+
|
198 |
+
>>> from sympy.combinatorics import Subset
|
199 |
+
>>> a = Subset([], ['a', 'b', 'c', 'd'])
|
200 |
+
>>> a.prev_lexicographic().subset
|
201 |
+
['d']
|
202 |
+
>>> a = Subset(['c','d'], ['a', 'b', 'c', 'd'])
|
203 |
+
>>> a.prev_lexicographic().subset
|
204 |
+
['c']
|
205 |
+
|
206 |
+
See Also
|
207 |
+
========
|
208 |
+
|
209 |
+
next_lexicographic
|
210 |
+
"""
|
211 |
+
i = self.superset_size - 1
|
212 |
+
indices = Subset.subset_indices(self.subset, self.superset)
|
213 |
+
|
214 |
+
while i >= 0 and i not in indices:
|
215 |
+
i = i - 1
|
216 |
+
|
217 |
+
if i == 0 or i - 1 in indices:
|
218 |
+
indices.remove(i)
|
219 |
+
else:
|
220 |
+
if i >= 0:
|
221 |
+
indices.remove(i)
|
222 |
+
indices.append(i - 1)
|
223 |
+
indices.append(self.superset_size - 1)
|
224 |
+
|
225 |
+
ret_set = []
|
226 |
+
super_set = self.superset
|
227 |
+
for i in indices:
|
228 |
+
ret_set.append(super_set[i])
|
229 |
+
return Subset(ret_set, super_set)
|
230 |
+
|
231 |
+
def iterate_graycode(self, k):
|
232 |
+
"""
|
233 |
+
Helper function used for prev_gray and next_gray.
|
234 |
+
It performs ``k`` step overs to get the respective Gray codes.
|
235 |
+
|
236 |
+
Examples
|
237 |
+
========
|
238 |
+
|
239 |
+
>>> from sympy.combinatorics import Subset
|
240 |
+
>>> a = Subset([1, 2, 3], [1, 2, 3, 4])
|
241 |
+
>>> a.iterate_graycode(3).subset
|
242 |
+
[1, 4]
|
243 |
+
>>> a.iterate_graycode(-2).subset
|
244 |
+
[1, 2, 4]
|
245 |
+
|
246 |
+
See Also
|
247 |
+
========
|
248 |
+
|
249 |
+
next_gray, prev_gray
|
250 |
+
"""
|
251 |
+
unranked_code = GrayCode.unrank(self.superset_size,
|
252 |
+
(self.rank_gray + k) % self.cardinality)
|
253 |
+
return Subset.subset_from_bitlist(self.superset,
|
254 |
+
unranked_code)
|
255 |
+
|
256 |
+
def next_gray(self):
|
257 |
+
"""
|
258 |
+
Generates the next Gray code ordered subset.
|
259 |
+
|
260 |
+
Examples
|
261 |
+
========
|
262 |
+
|
263 |
+
>>> from sympy.combinatorics import Subset
|
264 |
+
>>> a = Subset([1, 2, 3], [1, 2, 3, 4])
|
265 |
+
>>> a.next_gray().subset
|
266 |
+
[1, 3]
|
267 |
+
|
268 |
+
See Also
|
269 |
+
========
|
270 |
+
|
271 |
+
iterate_graycode, prev_gray
|
272 |
+
"""
|
273 |
+
return self.iterate_graycode(1)
|
274 |
+
|
275 |
+
def prev_gray(self):
|
276 |
+
"""
|
277 |
+
Generates the previous Gray code ordered subset.
|
278 |
+
|
279 |
+
Examples
|
280 |
+
========
|
281 |
+
|
282 |
+
>>> from sympy.combinatorics import Subset
|
283 |
+
>>> a = Subset([2, 3, 4], [1, 2, 3, 4, 5])
|
284 |
+
>>> a.prev_gray().subset
|
285 |
+
[2, 3, 4, 5]
|
286 |
+
|
287 |
+
See Also
|
288 |
+
========
|
289 |
+
|
290 |
+
iterate_graycode, next_gray
|
291 |
+
"""
|
292 |
+
return self.iterate_graycode(-1)
|
293 |
+
|
294 |
+
@property
|
295 |
+
def rank_binary(self):
|
296 |
+
"""
|
297 |
+
Computes the binary ordered rank.
|
298 |
+
|
299 |
+
Examples
|
300 |
+
========
|
301 |
+
|
302 |
+
>>> from sympy.combinatorics import Subset
|
303 |
+
>>> a = Subset([], ['a','b','c','d'])
|
304 |
+
>>> a.rank_binary
|
305 |
+
0
|
306 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
307 |
+
>>> a.rank_binary
|
308 |
+
3
|
309 |
+
|
310 |
+
See Also
|
311 |
+
========
|
312 |
+
|
313 |
+
iterate_binary, unrank_binary
|
314 |
+
"""
|
315 |
+
if self._rank_binary is None:
|
316 |
+
self._rank_binary = int("".join(
|
317 |
+
Subset.bitlist_from_subset(self.subset,
|
318 |
+
self.superset)), 2)
|
319 |
+
return self._rank_binary
|
320 |
+
|
321 |
+
@property
|
322 |
+
def rank_lexicographic(self):
|
323 |
+
"""
|
324 |
+
Computes the lexicographic ranking of the subset.
|
325 |
+
|
326 |
+
Examples
|
327 |
+
========
|
328 |
+
|
329 |
+
>>> from sympy.combinatorics import Subset
|
330 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
331 |
+
>>> a.rank_lexicographic
|
332 |
+
14
|
333 |
+
>>> a = Subset([2, 4, 5], [1, 2, 3, 4, 5, 6])
|
334 |
+
>>> a.rank_lexicographic
|
335 |
+
43
|
336 |
+
"""
|
337 |
+
if self._rank_lex is None:
|
338 |
+
def _ranklex(self, subset_index, i, n):
|
339 |
+
if subset_index == [] or i > n:
|
340 |
+
return 0
|
341 |
+
if i in subset_index:
|
342 |
+
subset_index.remove(i)
|
343 |
+
return 1 + _ranklex(self, subset_index, i + 1, n)
|
344 |
+
return 2**(n - i - 1) + _ranklex(self, subset_index, i + 1, n)
|
345 |
+
indices = Subset.subset_indices(self.subset, self.superset)
|
346 |
+
self._rank_lex = _ranklex(self, indices, 0, self.superset_size)
|
347 |
+
return self._rank_lex
|
348 |
+
|
349 |
+
@property
|
350 |
+
def rank_gray(self):
|
351 |
+
"""
|
352 |
+
Computes the Gray code ranking of the subset.
|
353 |
+
|
354 |
+
Examples
|
355 |
+
========
|
356 |
+
|
357 |
+
>>> from sympy.combinatorics import Subset
|
358 |
+
>>> a = Subset(['c','d'], ['a','b','c','d'])
|
359 |
+
>>> a.rank_gray
|
360 |
+
2
|
361 |
+
>>> a = Subset([2, 4, 5], [1, 2, 3, 4, 5, 6])
|
362 |
+
>>> a.rank_gray
|
363 |
+
27
|
364 |
+
|
365 |
+
See Also
|
366 |
+
========
|
367 |
+
|
368 |
+
iterate_graycode, unrank_gray
|
369 |
+
"""
|
370 |
+
if self._rank_graycode is None:
|
371 |
+
bits = Subset.bitlist_from_subset(self.subset, self.superset)
|
372 |
+
self._rank_graycode = GrayCode(len(bits), start=bits).rank
|
373 |
+
return self._rank_graycode
|
374 |
+
|
375 |
+
@property
|
376 |
+
def subset(self):
|
377 |
+
"""
|
378 |
+
Gets the subset represented by the current instance.
|
379 |
+
|
380 |
+
Examples
|
381 |
+
========
|
382 |
+
|
383 |
+
>>> from sympy.combinatorics import Subset
|
384 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
385 |
+
>>> a.subset
|
386 |
+
['c', 'd']
|
387 |
+
|
388 |
+
See Also
|
389 |
+
========
|
390 |
+
|
391 |
+
superset, size, superset_size, cardinality
|
392 |
+
"""
|
393 |
+
return self._subset
|
394 |
+
|
395 |
+
@property
|
396 |
+
def size(self):
|
397 |
+
"""
|
398 |
+
Gets the size of the subset.
|
399 |
+
|
400 |
+
Examples
|
401 |
+
========
|
402 |
+
|
403 |
+
>>> from sympy.combinatorics import Subset
|
404 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
405 |
+
>>> a.size
|
406 |
+
2
|
407 |
+
|
408 |
+
See Also
|
409 |
+
========
|
410 |
+
|
411 |
+
subset, superset, superset_size, cardinality
|
412 |
+
"""
|
413 |
+
return len(self.subset)
|
414 |
+
|
415 |
+
@property
|
416 |
+
def superset(self):
|
417 |
+
"""
|
418 |
+
Gets the superset of the subset.
|
419 |
+
|
420 |
+
Examples
|
421 |
+
========
|
422 |
+
|
423 |
+
>>> from sympy.combinatorics import Subset
|
424 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
425 |
+
>>> a.superset
|
426 |
+
['a', 'b', 'c', 'd']
|
427 |
+
|
428 |
+
See Also
|
429 |
+
========
|
430 |
+
|
431 |
+
subset, size, superset_size, cardinality
|
432 |
+
"""
|
433 |
+
return self._superset
|
434 |
+
|
435 |
+
@property
|
436 |
+
def superset_size(self):
|
437 |
+
"""
|
438 |
+
Returns the size of the superset.
|
439 |
+
|
440 |
+
Examples
|
441 |
+
========
|
442 |
+
|
443 |
+
>>> from sympy.combinatorics import Subset
|
444 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
445 |
+
>>> a.superset_size
|
446 |
+
4
|
447 |
+
|
448 |
+
See Also
|
449 |
+
========
|
450 |
+
|
451 |
+
subset, superset, size, cardinality
|
452 |
+
"""
|
453 |
+
return len(self.superset)
|
454 |
+
|
455 |
+
@property
|
456 |
+
def cardinality(self):
|
457 |
+
"""
|
458 |
+
Returns the number of all possible subsets.
|
459 |
+
|
460 |
+
Examples
|
461 |
+
========
|
462 |
+
|
463 |
+
>>> from sympy.combinatorics import Subset
|
464 |
+
>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
465 |
+
>>> a.cardinality
|
466 |
+
16
|
467 |
+
|
468 |
+
See Also
|
469 |
+
========
|
470 |
+
|
471 |
+
subset, superset, size, superset_size
|
472 |
+
"""
|
473 |
+
return 2**(self.superset_size)
|
474 |
+
|
475 |
+
@classmethod
|
476 |
+
def subset_from_bitlist(self, super_set, bitlist):
|
477 |
+
"""
|
478 |
+
Gets the subset defined by the bitlist.
|
479 |
+
|
480 |
+
Examples
|
481 |
+
========
|
482 |
+
|
483 |
+
>>> from sympy.combinatorics import Subset
|
484 |
+
>>> Subset.subset_from_bitlist(['a', 'b', 'c', 'd'], '0011').subset
|
485 |
+
['c', 'd']
|
486 |
+
|
487 |
+
See Also
|
488 |
+
========
|
489 |
+
|
490 |
+
bitlist_from_subset
|
491 |
+
"""
|
492 |
+
if len(super_set) != len(bitlist):
|
493 |
+
raise ValueError("The sizes of the lists are not equal")
|
494 |
+
ret_set = []
|
495 |
+
for i in range(len(bitlist)):
|
496 |
+
if bitlist[i] == '1':
|
497 |
+
ret_set.append(super_set[i])
|
498 |
+
return Subset(ret_set, super_set)
|
499 |
+
|
500 |
+
@classmethod
|
501 |
+
def bitlist_from_subset(self, subset, superset):
|
502 |
+
"""
|
503 |
+
Gets the bitlist corresponding to a subset.
|
504 |
+
|
505 |
+
Examples
|
506 |
+
========
|
507 |
+
|
508 |
+
>>> from sympy.combinatorics import Subset
|
509 |
+
>>> Subset.bitlist_from_subset(['c', 'd'], ['a', 'b', 'c', 'd'])
|
510 |
+
'0011'
|
511 |
+
|
512 |
+
See Also
|
513 |
+
========
|
514 |
+
|
515 |
+
subset_from_bitlist
|
516 |
+
"""
|
517 |
+
bitlist = ['0'] * len(superset)
|
518 |
+
if isinstance(subset, Subset):
|
519 |
+
subset = subset.subset
|
520 |
+
for i in Subset.subset_indices(subset, superset):
|
521 |
+
bitlist[i] = '1'
|
522 |
+
return ''.join(bitlist)
|
523 |
+
|
524 |
+
@classmethod
|
525 |
+
def unrank_binary(self, rank, superset):
|
526 |
+
"""
|
527 |
+
Gets the binary ordered subset of the specified rank.
|
528 |
+
|
529 |
+
Examples
|
530 |
+
========
|
531 |
+
|
532 |
+
>>> from sympy.combinatorics import Subset
|
533 |
+
>>> Subset.unrank_binary(4, ['a', 'b', 'c', 'd']).subset
|
534 |
+
['b']
|
535 |
+
|
536 |
+
See Also
|
537 |
+
========
|
538 |
+
|
539 |
+
iterate_binary, rank_binary
|
540 |
+
"""
|
541 |
+
bits = bin(rank)[2:].rjust(len(superset), '0')
|
542 |
+
return Subset.subset_from_bitlist(superset, bits)
|
543 |
+
|
544 |
+
@classmethod
|
545 |
+
def unrank_gray(self, rank, superset):
|
546 |
+
"""
|
547 |
+
Gets the Gray code ordered subset of the specified rank.
|
548 |
+
|
549 |
+
Examples
|
550 |
+
========
|
551 |
+
|
552 |
+
>>> from sympy.combinatorics import Subset
|
553 |
+
>>> Subset.unrank_gray(4, ['a', 'b', 'c']).subset
|
554 |
+
['a', 'b']
|
555 |
+
>>> Subset.unrank_gray(0, ['a', 'b', 'c']).subset
|
556 |
+
[]
|
557 |
+
|
558 |
+
See Also
|
559 |
+
========
|
560 |
+
|
561 |
+
iterate_graycode, rank_gray
|
562 |
+
"""
|
563 |
+
graycode_bitlist = GrayCode.unrank(len(superset), rank)
|
564 |
+
return Subset.subset_from_bitlist(superset, graycode_bitlist)
|
565 |
+
|
566 |
+
@classmethod
|
567 |
+
def subset_indices(self, subset, superset):
|
568 |
+
"""Return indices of subset in superset in a list; the list is empty
|
569 |
+
if all elements of ``subset`` are not in ``superset``.
|
570 |
+
|
571 |
+
Examples
|
572 |
+
========
|
573 |
+
|
574 |
+
>>> from sympy.combinatorics import Subset
|
575 |
+
>>> superset = [1, 3, 2, 5, 4]
|
576 |
+
>>> Subset.subset_indices([3, 2, 1], superset)
|
577 |
+
[1, 2, 0]
|
578 |
+
>>> Subset.subset_indices([1, 6], superset)
|
579 |
+
[]
|
580 |
+
>>> Subset.subset_indices([], superset)
|
581 |
+
[]
|
582 |
+
|
583 |
+
"""
|
584 |
+
a, b = superset, subset
|
585 |
+
sb = set(b)
|
586 |
+
d = {}
|
587 |
+
for i, ai in enumerate(a):
|
588 |
+
if ai in sb:
|
589 |
+
d[ai] = i
|
590 |
+
sb.remove(ai)
|
591 |
+
if not sb:
|
592 |
+
break
|
593 |
+
else:
|
594 |
+
return []
|
595 |
+
return [d[bi] for bi in b]
|
596 |
+
|
597 |
+
|
598 |
+
def ksubsets(superset, k):
|
599 |
+
"""
|
600 |
+
Finds the subsets of size ``k`` in lexicographic order.
|
601 |
+
|
602 |
+
This uses the itertools generator.
|
603 |
+
|
604 |
+
Examples
|
605 |
+
========
|
606 |
+
|
607 |
+
>>> from sympy.combinatorics.subsets import ksubsets
|
608 |
+
>>> list(ksubsets([1, 2, 3], 2))
|
609 |
+
[(1, 2), (1, 3), (2, 3)]
|
610 |
+
>>> list(ksubsets([1, 2, 3, 4, 5], 2))
|
611 |
+
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), \
|
612 |
+
(2, 5), (3, 4), (3, 5), (4, 5)]
|
613 |
+
|
614 |
+
See Also
|
615 |
+
========
|
616 |
+
|
617 |
+
Subset
|
618 |
+
"""
|
619 |
+
return combinations(superset, k)
|
venv/lib/python3.10/site-packages/sympy/combinatorics/tensor_can.py
ADDED
@@ -0,0 +1,1190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics.permutations import Permutation, _af_rmul, \
|
2 |
+
_af_invert, _af_new
|
3 |
+
from sympy.combinatorics.perm_groups import PermutationGroup, _orbit, \
|
4 |
+
_orbit_transversal
|
5 |
+
from sympy.combinatorics.util import _distribute_gens_by_base, \
|
6 |
+
_orbits_transversals_from_bsgs
|
7 |
+
|
8 |
+
"""
|
9 |
+
References for tensor canonicalization:
|
10 |
+
|
11 |
+
[1] R. Portugal "Algorithmic simplification of tensor expressions",
|
12 |
+
J. Phys. A 32 (1999) 7779-7789
|
13 |
+
|
14 |
+
[2] R. Portugal, B.F. Svaiter "Group-theoretic Approach for Symbolic
|
15 |
+
Tensor Manipulation: I. Free Indices"
|
16 |
+
arXiv:math-ph/0107031v1
|
17 |
+
|
18 |
+
[3] L.R.U. Manssur, R. Portugal "Group-theoretic Approach for Symbolic
|
19 |
+
Tensor Manipulation: II. Dummy Indices"
|
20 |
+
arXiv:math-ph/0107032v1
|
21 |
+
|
22 |
+
[4] xperm.c part of XPerm written by J. M. Martin-Garcia
|
23 |
+
http://www.xact.es/index.html
|
24 |
+
"""
|
25 |
+
|
26 |
+
|
27 |
+
def dummy_sgs(dummies, sym, n):
|
28 |
+
"""
|
29 |
+
Return the strong generators for dummy indices.
|
30 |
+
|
31 |
+
Parameters
|
32 |
+
==========
|
33 |
+
|
34 |
+
dummies : List of dummy indices.
|
35 |
+
`dummies[2k], dummies[2k+1]` are paired indices.
|
36 |
+
In base form, the dummy indices are always in
|
37 |
+
consecutive positions.
|
38 |
+
sym : symmetry under interchange of contracted dummies::
|
39 |
+
* None no symmetry
|
40 |
+
* 0 commuting
|
41 |
+
* 1 anticommuting
|
42 |
+
|
43 |
+
n : number of indices
|
44 |
+
|
45 |
+
Examples
|
46 |
+
========
|
47 |
+
|
48 |
+
>>> from sympy.combinatorics.tensor_can import dummy_sgs
|
49 |
+
>>> dummy_sgs(list(range(2, 8)), 0, 8)
|
50 |
+
[[0, 1, 3, 2, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 5, 4, 6, 7, 8, 9],
|
51 |
+
[0, 1, 2, 3, 4, 5, 7, 6, 8, 9], [0, 1, 4, 5, 2, 3, 6, 7, 8, 9],
|
52 |
+
[0, 1, 2, 3, 6, 7, 4, 5, 8, 9]]
|
53 |
+
"""
|
54 |
+
if len(dummies) > n:
|
55 |
+
raise ValueError("List too large")
|
56 |
+
res = []
|
57 |
+
# exchange of contravariant and covariant indices
|
58 |
+
if sym is not None:
|
59 |
+
for j in dummies[::2]:
|
60 |
+
a = list(range(n + 2))
|
61 |
+
if sym == 1:
|
62 |
+
a[n] = n + 1
|
63 |
+
a[n + 1] = n
|
64 |
+
a[j], a[j + 1] = a[j + 1], a[j]
|
65 |
+
res.append(a)
|
66 |
+
# rename dummy indices
|
67 |
+
for j in dummies[:-3:2]:
|
68 |
+
a = list(range(n + 2))
|
69 |
+
a[j:j + 4] = a[j + 2], a[j + 3], a[j], a[j + 1]
|
70 |
+
res.append(a)
|
71 |
+
return res
|
72 |
+
|
73 |
+
|
74 |
+
def _min_dummies(dummies, sym, indices):
|
75 |
+
"""
|
76 |
+
Return list of minima of the orbits of indices in group of dummies.
|
77 |
+
See ``double_coset_can_rep`` for the description of ``dummies`` and ``sym``.
|
78 |
+
``indices`` is the initial list of dummy indices.
|
79 |
+
|
80 |
+
Examples
|
81 |
+
========
|
82 |
+
|
83 |
+
>>> from sympy.combinatorics.tensor_can import _min_dummies
|
84 |
+
>>> _min_dummies([list(range(2, 8))], [0], list(range(10)))
|
85 |
+
[0, 1, 2, 2, 2, 2, 2, 2, 8, 9]
|
86 |
+
"""
|
87 |
+
num_types = len(sym)
|
88 |
+
m = [min(dx) if dx else None for dx in dummies]
|
89 |
+
res = indices[:]
|
90 |
+
for i in range(num_types):
|
91 |
+
for c, i in enumerate(indices):
|
92 |
+
for j in range(num_types):
|
93 |
+
if i in dummies[j]:
|
94 |
+
res[c] = m[j]
|
95 |
+
break
|
96 |
+
return res
|
97 |
+
|
98 |
+
|
99 |
+
def _trace_S(s, j, b, S_cosets):
|
100 |
+
"""
|
101 |
+
Return the representative h satisfying s[h[b]] == j
|
102 |
+
|
103 |
+
If there is not such a representative return None
|
104 |
+
"""
|
105 |
+
for h in S_cosets[b]:
|
106 |
+
if s[h[b]] == j:
|
107 |
+
return h
|
108 |
+
return None
|
109 |
+
|
110 |
+
|
111 |
+
def _trace_D(gj, p_i, Dxtrav):
|
112 |
+
"""
|
113 |
+
Return the representative h satisfying h[gj] == p_i
|
114 |
+
|
115 |
+
If there is not such a representative return None
|
116 |
+
"""
|
117 |
+
for h in Dxtrav:
|
118 |
+
if h[gj] == p_i:
|
119 |
+
return h
|
120 |
+
return None
|
121 |
+
|
122 |
+
|
123 |
+
def _dumx_remove(dumx, dumx_flat, p0):
|
124 |
+
"""
|
125 |
+
remove p0 from dumx
|
126 |
+
"""
|
127 |
+
res = []
|
128 |
+
for dx in dumx:
|
129 |
+
if p0 not in dx:
|
130 |
+
res.append(dx)
|
131 |
+
continue
|
132 |
+
k = dx.index(p0)
|
133 |
+
if k % 2 == 0:
|
134 |
+
p0_paired = dx[k + 1]
|
135 |
+
else:
|
136 |
+
p0_paired = dx[k - 1]
|
137 |
+
dx.remove(p0)
|
138 |
+
dx.remove(p0_paired)
|
139 |
+
dumx_flat.remove(p0)
|
140 |
+
dumx_flat.remove(p0_paired)
|
141 |
+
res.append(dx)
|
142 |
+
|
143 |
+
|
144 |
+
def transversal2coset(size, base, transversal):
|
145 |
+
a = []
|
146 |
+
j = 0
|
147 |
+
for i in range(size):
|
148 |
+
if i in base:
|
149 |
+
a.append(sorted(transversal[j].values()))
|
150 |
+
j += 1
|
151 |
+
else:
|
152 |
+
a.append([list(range(size))])
|
153 |
+
j = len(a) - 1
|
154 |
+
while a[j] == [list(range(size))]:
|
155 |
+
j -= 1
|
156 |
+
return a[:j + 1]
|
157 |
+
|
158 |
+
|
159 |
+
def double_coset_can_rep(dummies, sym, b_S, sgens, S_transversals, g):
|
160 |
+
r"""
|
161 |
+
Butler-Portugal algorithm for tensor canonicalization with dummy indices.
|
162 |
+
|
163 |
+
Parameters
|
164 |
+
==========
|
165 |
+
|
166 |
+
dummies
|
167 |
+
list of lists of dummy indices,
|
168 |
+
one list for each type of index;
|
169 |
+
the dummy indices are put in order contravariant, covariant
|
170 |
+
[d0, -d0, d1, -d1, ...].
|
171 |
+
|
172 |
+
sym
|
173 |
+
list of the symmetries of the index metric for each type.
|
174 |
+
|
175 |
+
possible symmetries of the metrics
|
176 |
+
* 0 symmetric
|
177 |
+
* 1 antisymmetric
|
178 |
+
* None no symmetry
|
179 |
+
|
180 |
+
b_S
|
181 |
+
base of a minimal slot symmetry BSGS.
|
182 |
+
|
183 |
+
sgens
|
184 |
+
generators of the slot symmetry BSGS.
|
185 |
+
|
186 |
+
S_transversals
|
187 |
+
transversals for the slot BSGS.
|
188 |
+
|
189 |
+
g
|
190 |
+
permutation representing the tensor.
|
191 |
+
|
192 |
+
Returns
|
193 |
+
=======
|
194 |
+
|
195 |
+
Return 0 if the tensor is zero, else return the array form of
|
196 |
+
the permutation representing the canonical form of the tensor.
|
197 |
+
|
198 |
+
Notes
|
199 |
+
=====
|
200 |
+
|
201 |
+
A tensor with dummy indices can be represented in a number
|
202 |
+
of equivalent ways which typically grows exponentially with
|
203 |
+
the number of indices. To be able to establish if two tensors
|
204 |
+
with many indices are equal becomes computationally very slow
|
205 |
+
in absence of an efficient algorithm.
|
206 |
+
|
207 |
+
The Butler-Portugal algorithm [3] is an efficient algorithm to
|
208 |
+
put tensors in canonical form, solving the above problem.
|
209 |
+
|
210 |
+
Portugal observed that a tensor can be represented by a permutation,
|
211 |
+
and that the class of tensors equivalent to it under slot and dummy
|
212 |
+
symmetries is equivalent to the double coset `D*g*S`
|
213 |
+
(Note: in this documentation we use the conventions for multiplication
|
214 |
+
of permutations p, q with (p*q)(i) = p[q[i]] which is opposite
|
215 |
+
to the one used in the Permutation class)
|
216 |
+
|
217 |
+
Using the algorithm by Butler to find a representative of the
|
218 |
+
double coset one can find a canonical form for the tensor.
|
219 |
+
|
220 |
+
To see this correspondence,
|
221 |
+
let `g` be a permutation in array form; a tensor with indices `ind`
|
222 |
+
(the indices including both the contravariant and the covariant ones)
|
223 |
+
can be written as
|
224 |
+
|
225 |
+
`t = T(ind[g[0]], \dots, ind[g[n-1]])`,
|
226 |
+
|
227 |
+
where `n = len(ind)`;
|
228 |
+
`g` has size `n + 2`, the last two indices for the sign of the tensor
|
229 |
+
(trick introduced in [4]).
|
230 |
+
|
231 |
+
A slot symmetry transformation `s` is a permutation acting on the slots
|
232 |
+
`t \rightarrow T(ind[(g*s)[0]], \dots, ind[(g*s)[n-1]])`
|
233 |
+
|
234 |
+
A dummy symmetry transformation acts on `ind`
|
235 |
+
`t \rightarrow T(ind[(d*g)[0]], \dots, ind[(d*g)[n-1]])`
|
236 |
+
|
237 |
+
Being interested only in the transformations of the tensor under
|
238 |
+
these symmetries, one can represent the tensor by `g`, which transforms
|
239 |
+
as
|
240 |
+
|
241 |
+
`g -> d*g*s`, so it belongs to the coset `D*g*S`, or in other words
|
242 |
+
to the set of all permutations allowed by the slot and dummy symmetries.
|
243 |
+
|
244 |
+
Let us explain the conventions by an example.
|
245 |
+
|
246 |
+
Given a tensor `T^{d3 d2 d1}{}_{d1 d2 d3}` with the slot symmetries
|
247 |
+
`T^{a0 a1 a2 a3 a4 a5} = -T^{a2 a1 a0 a3 a4 a5}`
|
248 |
+
|
249 |
+
`T^{a0 a1 a2 a3 a4 a5} = -T^{a4 a1 a2 a3 a0 a5}`
|
250 |
+
|
251 |
+
and symmetric metric, find the tensor equivalent to it which
|
252 |
+
is the lowest under the ordering of indices:
|
253 |
+
lexicographic ordering `d1, d2, d3` and then contravariant
|
254 |
+
before covariant index; that is the canonical form of the tensor.
|
255 |
+
|
256 |
+
The canonical form is `-T^{d1 d2 d3}{}_{d1 d2 d3}`
|
257 |
+
obtained using `T^{a0 a1 a2 a3 a4 a5} = -T^{a2 a1 a0 a3 a4 a5}`.
|
258 |
+
|
259 |
+
To convert this problem in the input for this function,
|
260 |
+
use the following ordering of the index names
|
261 |
+
(- for covariant for short) `d1, -d1, d2, -d2, d3, -d3`
|
262 |
+
|
263 |
+
`T^{d3 d2 d1}{}_{d1 d2 d3}` corresponds to `g = [4, 2, 0, 1, 3, 5, 6, 7]`
|
264 |
+
where the last two indices are for the sign
|
265 |
+
|
266 |
+
`sgens = [Permutation(0, 2)(6, 7), Permutation(0, 4)(6, 7)]`
|
267 |
+
|
268 |
+
sgens[0] is the slot symmetry `-(0, 2)`
|
269 |
+
`T^{a0 a1 a2 a3 a4 a5} = -T^{a2 a1 a0 a3 a4 a5}`
|
270 |
+
|
271 |
+
sgens[1] is the slot symmetry `-(0, 4)`
|
272 |
+
`T^{a0 a1 a2 a3 a4 a5} = -T^{a4 a1 a2 a3 a0 a5}`
|
273 |
+
|
274 |
+
The dummy symmetry group D is generated by the strong base generators
|
275 |
+
`[(0, 1), (2, 3), (4, 5), (0, 2)(1, 3), (0, 4)(1, 5)]`
|
276 |
+
where the first three interchange covariant and contravariant
|
277 |
+
positions of the same index (d1 <-> -d1) and the last two interchange
|
278 |
+
the dummy indices themselves (d1 <-> d2).
|
279 |
+
|
280 |
+
The dummy symmetry acts from the left
|
281 |
+
`d = [1, 0, 2, 3, 4, 5, 6, 7]` exchange `d1 \leftrightarrow -d1`
|
282 |
+
`T^{d3 d2 d1}{}_{d1 d2 d3} == T^{d3 d2}{}_{d1}{}^{d1}{}_{d2 d3}`
|
283 |
+
|
284 |
+
`g=[4, 2, 0, 1, 3, 5, 6, 7] -> [4, 2, 1, 0, 3, 5, 6, 7] = _af_rmul(d, g)`
|
285 |
+
which differs from `_af_rmul(g, d)`.
|
286 |
+
|
287 |
+
The slot symmetry acts from the right
|
288 |
+
`s = [2, 1, 0, 3, 4, 5, 7, 6]` exchanges slots 0 and 2 and changes sign
|
289 |
+
`T^{d3 d2 d1}{}_{d1 d2 d3} == -T^{d1 d2 d3}{}_{d1 d2 d3}`
|
290 |
+
|
291 |
+
`g=[4,2,0,1,3,5,6,7] -> [0, 2, 4, 1, 3, 5, 7, 6] = _af_rmul(g, s)`
|
292 |
+
|
293 |
+
Example in which the tensor is zero, same slot symmetries as above:
|
294 |
+
`T^{d2}{}_{d1 d3}{}^{d1 d3}{}_{d2}`
|
295 |
+
|
296 |
+
`= -T^{d3}{}_{d1 d3}{}^{d1 d2}{}_{d2}` under slot symmetry `-(0,4)`;
|
297 |
+
|
298 |
+
`= T_{d3 d1}{}^{d3}{}^{d1 d2}{}_{d2}` under slot symmetry `-(0,2)`;
|
299 |
+
|
300 |
+
`= T^{d3}{}_{d1 d3}{}^{d1 d2}{}_{d2}` symmetric metric;
|
301 |
+
|
302 |
+
`= 0` since two of these lines have tensors differ only for the sign.
|
303 |
+
|
304 |
+
The double coset D*g*S consists of permutations `h = d*g*s` corresponding
|
305 |
+
to equivalent tensors; if there are two `h` which are the same apart
|
306 |
+
from the sign, return zero; otherwise
|
307 |
+
choose as representative the tensor with indices
|
308 |
+
ordered lexicographically according to `[d1, -d1, d2, -d2, d3, -d3]`
|
309 |
+
that is ``rep = min(D*g*S) = min([d*g*s for d in D for s in S])``
|
310 |
+
|
311 |
+
The indices are fixed one by one; first choose the lowest index
|
312 |
+
for slot 0, then the lowest remaining index for slot 1, etc.
|
313 |
+
Doing this one obtains a chain of stabilizers
|
314 |
+
|
315 |
+
`S \rightarrow S_{b0} \rightarrow S_{b0,b1} \rightarrow \dots` and
|
316 |
+
`D \rightarrow D_{p0} \rightarrow D_{p0,p1} \rightarrow \dots`
|
317 |
+
|
318 |
+
where ``[b0, b1, ...] = range(b)`` is a base of the symmetric group;
|
319 |
+
the strong base `b_S` of S is an ordered sublist of it;
|
320 |
+
therefore it is sufficient to compute once the
|
321 |
+
strong base generators of S using the Schreier-Sims algorithm;
|
322 |
+
the stabilizers of the strong base generators are the
|
323 |
+
strong base generators of the stabilizer subgroup.
|
324 |
+
|
325 |
+
``dbase = [p0, p1, ...]`` is not in general in lexicographic order,
|
326 |
+
so that one must recompute the strong base generators each time;
|
327 |
+
however this is trivial, there is no need to use the Schreier-Sims
|
328 |
+
algorithm for D.
|
329 |
+
|
330 |
+
The algorithm keeps a TAB of elements `(s_i, d_i, h_i)`
|
331 |
+
where `h_i = d_i \times g \times s_i` satisfying `h_i[j] = p_j` for `0 \le j < i`
|
332 |
+
starting from `s_0 = id, d_0 = id, h_0 = g`.
|
333 |
+
|
334 |
+
The equations `h_0[0] = p_0, h_1[1] = p_1, \dots` are solved in this order,
|
335 |
+
choosing each time the lowest possible value of p_i
|
336 |
+
|
337 |
+
For `j < i`
|
338 |
+
`d_i*g*s_i*S_{b_0, \dots, b_{i-1}}*b_j = D_{p_0, \dots, p_{i-1}}*p_j`
|
339 |
+
so that for dx in `D_{p_0,\dots,p_{i-1}}` and sx in
|
340 |
+
`S_{base[0], \dots, base[i-1]}` one has `dx*d_i*g*s_i*sx*b_j = p_j`
|
341 |
+
|
342 |
+
Search for dx, sx such that this equation holds for `j = i`;
|
343 |
+
it can be written as `s_i*sx*b_j = J, dx*d_i*g*J = p_j`
|
344 |
+
`sx*b_j = s_i**-1*J; sx = trace(s_i**-1, S_{b_0,...,b_{i-1}})`
|
345 |
+
`dx**-1*p_j = d_i*g*J; dx = trace(d_i*g*J, D_{p_0,...,p_{i-1}})`
|
346 |
+
|
347 |
+
`s_{i+1} = s_i*trace(s_i**-1*J, S_{b_0,...,b_{i-1}})`
|
348 |
+
`d_{i+1} = trace(d_i*g*J, D_{p_0,...,p_{i-1}})**-1*d_i`
|
349 |
+
`h_{i+1}*b_i = d_{i+1}*g*s_{i+1}*b_i = p_i`
|
350 |
+
|
351 |
+
`h_n*b_j = p_j` for all j, so that `h_n` is the solution.
|
352 |
+
|
353 |
+
Add the found `(s, d, h)` to TAB1.
|
354 |
+
|
355 |
+
At the end of the iteration sort TAB1 with respect to the `h`;
|
356 |
+
if there are two consecutive `h` in TAB1 which differ only for the
|
357 |
+
sign, the tensor is zero, so return 0;
|
358 |
+
if there are two consecutive `h` which are equal, keep only one.
|
359 |
+
|
360 |
+
Then stabilize the slot generators under `i` and the dummy generators
|
361 |
+
under `p_i`.
|
362 |
+
|
363 |
+
Assign `TAB = TAB1` at the end of the iteration step.
|
364 |
+
|
365 |
+
At the end `TAB` contains a unique `(s, d, h)`, since all the slots
|
366 |
+
of the tensor `h` have been fixed to have the minimum value according
|
367 |
+
to the symmetries. The algorithm returns `h`.
|
368 |
+
|
369 |
+
It is important that the slot BSGS has lexicographic minimal base,
|
370 |
+
otherwise there is an `i` which does not belong to the slot base
|
371 |
+
for which `p_i` is fixed by the dummy symmetry only, while `i`
|
372 |
+
is not invariant from the slot stabilizer, so `p_i` is not in
|
373 |
+
general the minimal value.
|
374 |
+
|
375 |
+
This algorithm differs slightly from the original algorithm [3]:
|
376 |
+
the canonical form is minimal lexicographically, and
|
377 |
+
the BSGS has minimal base under lexicographic order.
|
378 |
+
Equal tensors `h` are eliminated from TAB.
|
379 |
+
|
380 |
+
|
381 |
+
Examples
|
382 |
+
========
|
383 |
+
|
384 |
+
>>> from sympy.combinatorics.permutations import Permutation
|
385 |
+
>>> from sympy.combinatorics.tensor_can import double_coset_can_rep, get_transversals
|
386 |
+
>>> gens = [Permutation(x) for x in [[2, 1, 0, 3, 4, 5, 7, 6], [4, 1, 2, 3, 0, 5, 7, 6]]]
|
387 |
+
>>> base = [0, 2]
|
388 |
+
>>> g = Permutation([4, 2, 0, 1, 3, 5, 6, 7])
|
389 |
+
>>> transversals = get_transversals(base, gens)
|
390 |
+
>>> double_coset_can_rep([list(range(6))], [0], base, gens, transversals, g)
|
391 |
+
[0, 1, 2, 3, 4, 5, 7, 6]
|
392 |
+
|
393 |
+
>>> g = Permutation([4, 1, 3, 0, 5, 2, 6, 7])
|
394 |
+
>>> double_coset_can_rep([list(range(6))], [0], base, gens, transversals, g)
|
395 |
+
0
|
396 |
+
"""
|
397 |
+
size = g.size
|
398 |
+
g = g.array_form
|
399 |
+
num_dummies = size - 2
|
400 |
+
indices = list(range(num_dummies))
|
401 |
+
all_metrics_with_sym = not any(_ is None for _ in sym)
|
402 |
+
num_types = len(sym)
|
403 |
+
dumx = dummies[:]
|
404 |
+
dumx_flat = []
|
405 |
+
for dx in dumx:
|
406 |
+
dumx_flat.extend(dx)
|
407 |
+
b_S = b_S[:]
|
408 |
+
sgensx = [h._array_form for h in sgens]
|
409 |
+
if b_S:
|
410 |
+
S_transversals = transversal2coset(size, b_S, S_transversals)
|
411 |
+
# strong generating set for D
|
412 |
+
dsgsx = []
|
413 |
+
for i in range(num_types):
|
414 |
+
dsgsx.extend(dummy_sgs(dumx[i], sym[i], num_dummies))
|
415 |
+
idn = list(range(size))
|
416 |
+
# TAB = list of entries (s, d, h) where h = _af_rmuln(d,g,s)
|
417 |
+
# for short, in the following d*g*s means _af_rmuln(d,g,s)
|
418 |
+
TAB = [(idn, idn, g)]
|
419 |
+
for i in range(size - 2):
|
420 |
+
b = i
|
421 |
+
testb = b in b_S and sgensx
|
422 |
+
if testb:
|
423 |
+
sgensx1 = [_af_new(_) for _ in sgensx]
|
424 |
+
deltab = _orbit(size, sgensx1, b)
|
425 |
+
else:
|
426 |
+
deltab = {b}
|
427 |
+
# p1 = min(IMAGES) = min(Union D_p*h*deltab for h in TAB)
|
428 |
+
if all_metrics_with_sym:
|
429 |
+
md = _min_dummies(dumx, sym, indices)
|
430 |
+
else:
|
431 |
+
md = [min(_orbit(size, [_af_new(
|
432 |
+
ddx) for ddx in dsgsx], ii)) for ii in range(size - 2)]
|
433 |
+
|
434 |
+
p_i = min([min([md[h[x]] for x in deltab]) for s, d, h in TAB])
|
435 |
+
dsgsx1 = [_af_new(_) for _ in dsgsx]
|
436 |
+
Dxtrav = _orbit_transversal(size, dsgsx1, p_i, False, af=True) \
|
437 |
+
if dsgsx else None
|
438 |
+
if Dxtrav:
|
439 |
+
Dxtrav = [_af_invert(x) for x in Dxtrav]
|
440 |
+
# compute the orbit of p_i
|
441 |
+
for ii in range(num_types):
|
442 |
+
if p_i in dumx[ii]:
|
443 |
+
# the orbit is made by all the indices in dum[ii]
|
444 |
+
if sym[ii] is not None:
|
445 |
+
deltap = dumx[ii]
|
446 |
+
else:
|
447 |
+
# the orbit is made by all the even indices if p_i
|
448 |
+
# is even, by all the odd indices if p_i is odd
|
449 |
+
p_i_index = dumx[ii].index(p_i) % 2
|
450 |
+
deltap = dumx[ii][p_i_index::2]
|
451 |
+
break
|
452 |
+
else:
|
453 |
+
deltap = [p_i]
|
454 |
+
TAB1 = []
|
455 |
+
while TAB:
|
456 |
+
s, d, h = TAB.pop()
|
457 |
+
if min([md[h[x]] for x in deltab]) != p_i:
|
458 |
+
continue
|
459 |
+
deltab1 = [x for x in deltab if md[h[x]] == p_i]
|
460 |
+
# NEXT = s*deltab1 intersection (d*g)**-1*deltap
|
461 |
+
dg = _af_rmul(d, g)
|
462 |
+
dginv = _af_invert(dg)
|
463 |
+
sdeltab = [s[x] for x in deltab1]
|
464 |
+
gdeltap = [dginv[x] for x in deltap]
|
465 |
+
NEXT = [x for x in sdeltab if x in gdeltap]
|
466 |
+
# d, s satisfy
|
467 |
+
# d*g*s*base[i-1] = p_{i-1}; using the stabilizers
|
468 |
+
# d*g*s*S_{base[0],...,base[i-1]}*base[i-1] =
|
469 |
+
# D_{p_0,...,p_{i-1}}*p_{i-1}
|
470 |
+
# so that to find d1, s1 satisfying d1*g*s1*b = p_i
|
471 |
+
# one can look for dx in D_{p_0,...,p_{i-1}} and
|
472 |
+
# sx in S_{base[0],...,base[i-1]}
|
473 |
+
# d1 = dx*d; s1 = s*sx
|
474 |
+
# d1*g*s1*b = dx*d*g*s*sx*b = p_i
|
475 |
+
for j in NEXT:
|
476 |
+
if testb:
|
477 |
+
# solve s1*b = j with s1 = s*sx for some element sx
|
478 |
+
# of the stabilizer of ..., base[i-1]
|
479 |
+
# sx*b = s**-1*j; sx = _trace_S(s, j,...)
|
480 |
+
# s1 = s*trace_S(s**-1*j,...)
|
481 |
+
s1 = _trace_S(s, j, b, S_transversals)
|
482 |
+
if not s1:
|
483 |
+
continue
|
484 |
+
else:
|
485 |
+
s1 = [s[ix] for ix in s1]
|
486 |
+
else:
|
487 |
+
s1 = s
|
488 |
+
# assert s1[b] == j # invariant
|
489 |
+
# solve d1*g*j = p_i with d1 = dx*d for some element dg
|
490 |
+
# of the stabilizer of ..., p_{i-1}
|
491 |
+
# dx**-1*p_i = d*g*j; dx**-1 = trace_D(d*g*j,...)
|
492 |
+
# d1 = trace_D(d*g*j,...)**-1*d
|
493 |
+
# to save an inversion in the inner loop; notice we did
|
494 |
+
# Dxtrav = [perm_af_invert(x) for x in Dxtrav] out of the loop
|
495 |
+
if Dxtrav:
|
496 |
+
d1 = _trace_D(dg[j], p_i, Dxtrav)
|
497 |
+
if not d1:
|
498 |
+
continue
|
499 |
+
else:
|
500 |
+
if p_i != dg[j]:
|
501 |
+
continue
|
502 |
+
d1 = idn
|
503 |
+
assert d1[dg[j]] == p_i # invariant
|
504 |
+
d1 = [d1[ix] for ix in d]
|
505 |
+
h1 = [d1[g[ix]] for ix in s1]
|
506 |
+
# assert h1[b] == p_i # invariant
|
507 |
+
TAB1.append((s1, d1, h1))
|
508 |
+
|
509 |
+
# if TAB contains equal permutations, keep only one of them;
|
510 |
+
# if TAB contains equal permutations up to the sign, return 0
|
511 |
+
TAB1.sort(key=lambda x: x[-1])
|
512 |
+
prev = [0] * size
|
513 |
+
while TAB1:
|
514 |
+
s, d, h = TAB1.pop()
|
515 |
+
if h[:-2] == prev[:-2]:
|
516 |
+
if h[-1] != prev[-1]:
|
517 |
+
return 0
|
518 |
+
else:
|
519 |
+
TAB.append((s, d, h))
|
520 |
+
prev = h
|
521 |
+
|
522 |
+
# stabilize the SGS
|
523 |
+
sgensx = [h for h in sgensx if h[b] == b]
|
524 |
+
if b in b_S:
|
525 |
+
b_S.remove(b)
|
526 |
+
_dumx_remove(dumx, dumx_flat, p_i)
|
527 |
+
dsgsx = []
|
528 |
+
for i in range(num_types):
|
529 |
+
dsgsx.extend(dummy_sgs(dumx[i], sym[i], num_dummies))
|
530 |
+
return TAB[0][-1]
|
531 |
+
|
532 |
+
|
533 |
+
def canonical_free(base, gens, g, num_free):
|
534 |
+
"""
|
535 |
+
Canonicalization of a tensor with respect to free indices
|
536 |
+
choosing the minimum with respect to lexicographical ordering
|
537 |
+
in the free indices.
|
538 |
+
|
539 |
+
Explanation
|
540 |
+
===========
|
541 |
+
|
542 |
+
``base``, ``gens`` BSGS for slot permutation group
|
543 |
+
``g`` permutation representing the tensor
|
544 |
+
``num_free`` number of free indices
|
545 |
+
The indices must be ordered with first the free indices
|
546 |
+
|
547 |
+
See explanation in double_coset_can_rep
|
548 |
+
The algorithm is a variation of the one given in [2].
|
549 |
+
|
550 |
+
Examples
|
551 |
+
========
|
552 |
+
|
553 |
+
>>> from sympy.combinatorics import Permutation
|
554 |
+
>>> from sympy.combinatorics.tensor_can import canonical_free
|
555 |
+
>>> gens = [[1, 0, 2, 3, 5, 4], [2, 3, 0, 1, 4, 5],[0, 1, 3, 2, 5, 4]]
|
556 |
+
>>> gens = [Permutation(h) for h in gens]
|
557 |
+
>>> base = [0, 2]
|
558 |
+
>>> g = Permutation([2, 1, 0, 3, 4, 5])
|
559 |
+
>>> canonical_free(base, gens, g, 4)
|
560 |
+
[0, 3, 1, 2, 5, 4]
|
561 |
+
|
562 |
+
Consider the product of Riemann tensors
|
563 |
+
``T = R^{a}_{d0}^{d1,d2}*R_{d2,d1}^{d0,b}``
|
564 |
+
The order of the indices is ``[a, b, d0, -d0, d1, -d1, d2, -d2]``
|
565 |
+
The permutation corresponding to the tensor is
|
566 |
+
``g = [0, 3, 4, 6, 7, 5, 2, 1, 8, 9]``
|
567 |
+
|
568 |
+
In particular ``a`` is position ``0``, ``b`` is in position ``9``.
|
569 |
+
Use the slot symmetries to get `T` is a form which is the minimal
|
570 |
+
in lexicographic order in the free indices ``a`` and ``b``, e.g.
|
571 |
+
``-R^{a}_{d0}^{d1,d2}*R^{b,d0}_{d2,d1}`` corresponding to
|
572 |
+
``[0, 3, 4, 6, 1, 2, 7, 5, 9, 8]``
|
573 |
+
|
574 |
+
>>> from sympy.combinatorics.tensor_can import riemann_bsgs, tensor_gens
|
575 |
+
>>> base, gens = riemann_bsgs
|
576 |
+
>>> size, sbase, sgens = tensor_gens(base, gens, [[], []], 0)
|
577 |
+
>>> g = Permutation([0, 3, 4, 6, 7, 5, 2, 1, 8, 9])
|
578 |
+
>>> canonical_free(sbase, [Permutation(h) for h in sgens], g, 2)
|
579 |
+
[0, 3, 4, 6, 1, 2, 7, 5, 9, 8]
|
580 |
+
"""
|
581 |
+
g = g.array_form
|
582 |
+
size = len(g)
|
583 |
+
if not base:
|
584 |
+
return g[:]
|
585 |
+
|
586 |
+
transversals = get_transversals(base, gens)
|
587 |
+
for x in sorted(g[:-2]):
|
588 |
+
if x not in base:
|
589 |
+
base.append(x)
|
590 |
+
h = g
|
591 |
+
for i, transv in enumerate(transversals):
|
592 |
+
h_i = [size]*num_free
|
593 |
+
# find the element s in transversals[i] such that
|
594 |
+
# _af_rmul(h, s) has its free elements with the lowest position in h
|
595 |
+
s = None
|
596 |
+
for sk in transv.values():
|
597 |
+
h1 = _af_rmul(h, sk)
|
598 |
+
hi = [h1.index(ix) for ix in range(num_free)]
|
599 |
+
if hi < h_i:
|
600 |
+
h_i = hi
|
601 |
+
s = sk
|
602 |
+
if s:
|
603 |
+
h = _af_rmul(h, s)
|
604 |
+
return h
|
605 |
+
|
606 |
+
|
607 |
+
def _get_map_slots(size, fixed_slots):
|
608 |
+
res = list(range(size))
|
609 |
+
pos = 0
|
610 |
+
for i in range(size):
|
611 |
+
if i in fixed_slots:
|
612 |
+
continue
|
613 |
+
res[i] = pos
|
614 |
+
pos += 1
|
615 |
+
return res
|
616 |
+
|
617 |
+
|
618 |
+
def _lift_sgens(size, fixed_slots, free, s):
|
619 |
+
a = []
|
620 |
+
j = k = 0
|
621 |
+
fd = list(zip(fixed_slots, free))
|
622 |
+
fd = [y for x, y in sorted(fd)]
|
623 |
+
num_free = len(free)
|
624 |
+
for i in range(size):
|
625 |
+
if i in fixed_slots:
|
626 |
+
a.append(fd[k])
|
627 |
+
k += 1
|
628 |
+
else:
|
629 |
+
a.append(s[j] + num_free)
|
630 |
+
j += 1
|
631 |
+
return a
|
632 |
+
|
633 |
+
|
634 |
+
def canonicalize(g, dummies, msym, *v):
|
635 |
+
"""
|
636 |
+
canonicalize tensor formed by tensors
|
637 |
+
|
638 |
+
Parameters
|
639 |
+
==========
|
640 |
+
|
641 |
+
g : permutation representing the tensor
|
642 |
+
|
643 |
+
dummies : list representing the dummy indices
|
644 |
+
it can be a list of dummy indices of the same type
|
645 |
+
or a list of lists of dummy indices, one list for each
|
646 |
+
type of index;
|
647 |
+
the dummy indices must come after the free indices,
|
648 |
+
and put in order contravariant, covariant
|
649 |
+
[d0, -d0, d1,-d1,...]
|
650 |
+
|
651 |
+
msym : symmetry of the metric(s)
|
652 |
+
it can be an integer or a list;
|
653 |
+
in the first case it is the symmetry of the dummy index metric;
|
654 |
+
in the second case it is the list of the symmetries of the
|
655 |
+
index metric for each type
|
656 |
+
|
657 |
+
v : list, (base_i, gens_i, n_i, sym_i) for tensors of type `i`
|
658 |
+
|
659 |
+
base_i, gens_i : BSGS for tensors of this type.
|
660 |
+
The BSGS should have minimal base under lexicographic ordering;
|
661 |
+
if not, an attempt is made do get the minimal BSGS;
|
662 |
+
in case of failure,
|
663 |
+
canonicalize_naive is used, which is much slower.
|
664 |
+
|
665 |
+
n_i : number of tensors of type `i`.
|
666 |
+
|
667 |
+
sym_i : symmetry under exchange of component tensors of type `i`.
|
668 |
+
|
669 |
+
Both for msym and sym_i the cases are
|
670 |
+
* None no symmetry
|
671 |
+
* 0 commuting
|
672 |
+
* 1 anticommuting
|
673 |
+
|
674 |
+
Returns
|
675 |
+
=======
|
676 |
+
|
677 |
+
0 if the tensor is zero, else return the array form of
|
678 |
+
the permutation representing the canonical form of the tensor.
|
679 |
+
|
680 |
+
Algorithm
|
681 |
+
=========
|
682 |
+
|
683 |
+
First one uses canonical_free to get the minimum tensor under
|
684 |
+
lexicographic order, using only the slot symmetries.
|
685 |
+
If the component tensors have not minimal BSGS, it is attempted
|
686 |
+
to find it; if the attempt fails canonicalize_naive
|
687 |
+
is used instead.
|
688 |
+
|
689 |
+
Compute the residual slot symmetry keeping fixed the free indices
|
690 |
+
using tensor_gens(base, gens, list_free_indices, sym).
|
691 |
+
|
692 |
+
Reduce the problem eliminating the free indices.
|
693 |
+
|
694 |
+
Then use double_coset_can_rep and lift back the result reintroducing
|
695 |
+
the free indices.
|
696 |
+
|
697 |
+
Examples
|
698 |
+
========
|
699 |
+
|
700 |
+
one type of index with commuting metric;
|
701 |
+
|
702 |
+
`A_{a b}` and `B_{a b}` antisymmetric and commuting
|
703 |
+
|
704 |
+
`T = A_{d0 d1} * B^{d0}{}_{d2} * B^{d2 d1}`
|
705 |
+
|
706 |
+
`ord = [d0,-d0,d1,-d1,d2,-d2]` order of the indices
|
707 |
+
|
708 |
+
g = [1, 3, 0, 5, 4, 2, 6, 7]
|
709 |
+
|
710 |
+
`T_c = 0`
|
711 |
+
|
712 |
+
>>> from sympy.combinatorics.tensor_can import get_symmetric_group_sgs, canonicalize, bsgs_direct_product
|
713 |
+
>>> from sympy.combinatorics import Permutation
|
714 |
+
>>> base2a, gens2a = get_symmetric_group_sgs(2, 1)
|
715 |
+
>>> t0 = (base2a, gens2a, 1, 0)
|
716 |
+
>>> t1 = (base2a, gens2a, 2, 0)
|
717 |
+
>>> g = Permutation([1, 3, 0, 5, 4, 2, 6, 7])
|
718 |
+
>>> canonicalize(g, range(6), 0, t0, t1)
|
719 |
+
0
|
720 |
+
|
721 |
+
same as above, but with `B_{a b}` anticommuting
|
722 |
+
|
723 |
+
`T_c = -A^{d0 d1} * B_{d0}{}^{d2} * B_{d1 d2}`
|
724 |
+
|
725 |
+
can = [0,2,1,4,3,5,7,6]
|
726 |
+
|
727 |
+
>>> t1 = (base2a, gens2a, 2, 1)
|
728 |
+
>>> canonicalize(g, range(6), 0, t0, t1)
|
729 |
+
[0, 2, 1, 4, 3, 5, 7, 6]
|
730 |
+
|
731 |
+
two types of indices `[a,b,c,d,e,f]` and `[m,n]`, in this order,
|
732 |
+
both with commuting metric
|
733 |
+
|
734 |
+
`f^{a b c}` antisymmetric, commuting
|
735 |
+
|
736 |
+
`A_{m a}` no symmetry, commuting
|
737 |
+
|
738 |
+
`T = f^c{}_{d a} * f^f{}_{e b} * A_m{}^d * A^{m b} * A_n{}^a * A^{n e}`
|
739 |
+
|
740 |
+
ord = [c,f,a,-a,b,-b,d,-d,e,-e,m,-m,n,-n]
|
741 |
+
|
742 |
+
g = [0,7,3, 1,9,5, 11,6, 10,4, 13,2, 12,8, 14,15]
|
743 |
+
|
744 |
+
The canonical tensor is
|
745 |
+
`T_c = -f^{c a b} * f^{f d e} * A^m{}_a * A_{m d} * A^n{}_b * A_{n e}`
|
746 |
+
|
747 |
+
can = [0,2,4, 1,6,8, 10,3, 11,7, 12,5, 13,9, 15,14]
|
748 |
+
|
749 |
+
>>> base_f, gens_f = get_symmetric_group_sgs(3, 1)
|
750 |
+
>>> base1, gens1 = get_symmetric_group_sgs(1)
|
751 |
+
>>> base_A, gens_A = bsgs_direct_product(base1, gens1, base1, gens1)
|
752 |
+
>>> t0 = (base_f, gens_f, 2, 0)
|
753 |
+
>>> t1 = (base_A, gens_A, 4, 0)
|
754 |
+
>>> dummies = [range(2, 10), range(10, 14)]
|
755 |
+
>>> g = Permutation([0, 7, 3, 1, 9, 5, 11, 6, 10, 4, 13, 2, 12, 8, 14, 15])
|
756 |
+
>>> canonicalize(g, dummies, [0, 0], t0, t1)
|
757 |
+
[0, 2, 4, 1, 6, 8, 10, 3, 11, 7, 12, 5, 13, 9, 15, 14]
|
758 |
+
"""
|
759 |
+
from sympy.combinatorics.testutil import canonicalize_naive
|
760 |
+
if not isinstance(msym, list):
|
761 |
+
if msym not in (0, 1, None):
|
762 |
+
raise ValueError('msym must be 0, 1 or None')
|
763 |
+
num_types = 1
|
764 |
+
else:
|
765 |
+
num_types = len(msym)
|
766 |
+
if not all(msymx in (0, 1, None) for msymx in msym):
|
767 |
+
raise ValueError('msym entries must be 0, 1 or None')
|
768 |
+
if len(dummies) != num_types:
|
769 |
+
raise ValueError(
|
770 |
+
'dummies and msym must have the same number of elements')
|
771 |
+
size = g.size
|
772 |
+
num_tensors = 0
|
773 |
+
v1 = []
|
774 |
+
for base_i, gens_i, n_i, sym_i in v:
|
775 |
+
# check that the BSGS is minimal;
|
776 |
+
# this property is used in double_coset_can_rep;
|
777 |
+
# if it is not minimal use canonicalize_naive
|
778 |
+
if not _is_minimal_bsgs(base_i, gens_i):
|
779 |
+
mbsgs = get_minimal_bsgs(base_i, gens_i)
|
780 |
+
if not mbsgs:
|
781 |
+
can = canonicalize_naive(g, dummies, msym, *v)
|
782 |
+
return can
|
783 |
+
base_i, gens_i = mbsgs
|
784 |
+
v1.append((base_i, gens_i, [[]] * n_i, sym_i))
|
785 |
+
num_tensors += n_i
|
786 |
+
|
787 |
+
if num_types == 1 and not isinstance(msym, list):
|
788 |
+
dummies = [dummies]
|
789 |
+
msym = [msym]
|
790 |
+
flat_dummies = []
|
791 |
+
for dumx in dummies:
|
792 |
+
flat_dummies.extend(dumx)
|
793 |
+
|
794 |
+
if flat_dummies and flat_dummies != list(range(flat_dummies[0], flat_dummies[-1] + 1)):
|
795 |
+
raise ValueError('dummies is not valid')
|
796 |
+
|
797 |
+
# slot symmetry of the tensor
|
798 |
+
size1, sbase, sgens = gens_products(*v1)
|
799 |
+
if size != size1:
|
800 |
+
raise ValueError(
|
801 |
+
'g has size %d, generators have size %d' % (size, size1))
|
802 |
+
free = [i for i in range(size - 2) if i not in flat_dummies]
|
803 |
+
num_free = len(free)
|
804 |
+
|
805 |
+
# g1 minimal tensor under slot symmetry
|
806 |
+
g1 = canonical_free(sbase, sgens, g, num_free)
|
807 |
+
if not flat_dummies:
|
808 |
+
return g1
|
809 |
+
# save the sign of g1
|
810 |
+
sign = 0 if g1[-1] == size - 1 else 1
|
811 |
+
|
812 |
+
# the free indices are kept fixed.
|
813 |
+
# Determine free_i, the list of slots of tensors which are fixed
|
814 |
+
# since they are occupied by free indices, which are fixed.
|
815 |
+
start = 0
|
816 |
+
for i, (base_i, gens_i, n_i, sym_i) in enumerate(v):
|
817 |
+
free_i = []
|
818 |
+
len_tens = gens_i[0].size - 2
|
819 |
+
# for each component tensor get a list od fixed islots
|
820 |
+
for j in range(n_i):
|
821 |
+
# get the elements corresponding to the component tensor
|
822 |
+
h = g1[start:(start + len_tens)]
|
823 |
+
fr = []
|
824 |
+
# get the positions of the fixed elements in h
|
825 |
+
for k in free:
|
826 |
+
if k in h:
|
827 |
+
fr.append(h.index(k))
|
828 |
+
free_i.append(fr)
|
829 |
+
start += len_tens
|
830 |
+
v1[i] = (base_i, gens_i, free_i, sym_i)
|
831 |
+
# BSGS of the tensor with fixed free indices
|
832 |
+
# if tensor_gens fails in gens_product, use canonicalize_naive
|
833 |
+
size, sbase, sgens = gens_products(*v1)
|
834 |
+
|
835 |
+
# reduce the permutations getting rid of the free indices
|
836 |
+
pos_free = [g1.index(x) for x in range(num_free)]
|
837 |
+
size_red = size - num_free
|
838 |
+
g1_red = [x - num_free for x in g1 if x in flat_dummies]
|
839 |
+
if sign:
|
840 |
+
g1_red.extend([size_red - 1, size_red - 2])
|
841 |
+
else:
|
842 |
+
g1_red.extend([size_red - 2, size_red - 1])
|
843 |
+
map_slots = _get_map_slots(size, pos_free)
|
844 |
+
sbase_red = [map_slots[i] for i in sbase if i not in pos_free]
|
845 |
+
sgens_red = [_af_new([map_slots[i] for i in y._array_form if i not in pos_free]) for y in sgens]
|
846 |
+
dummies_red = [[x - num_free for x in y] for y in dummies]
|
847 |
+
transv_red = get_transversals(sbase_red, sgens_red)
|
848 |
+
g1_red = _af_new(g1_red)
|
849 |
+
g2 = double_coset_can_rep(
|
850 |
+
dummies_red, msym, sbase_red, sgens_red, transv_red, g1_red)
|
851 |
+
if g2 == 0:
|
852 |
+
return 0
|
853 |
+
# lift to the case with the free indices
|
854 |
+
g3 = _lift_sgens(size, pos_free, free, g2)
|
855 |
+
return g3
|
856 |
+
|
857 |
+
|
858 |
+
def perm_af_direct_product(gens1, gens2, signed=True):
|
859 |
+
"""
|
860 |
+
Direct products of the generators gens1 and gens2.
|
861 |
+
|
862 |
+
Examples
|
863 |
+
========
|
864 |
+
|
865 |
+
>>> from sympy.combinatorics.tensor_can import perm_af_direct_product
|
866 |
+
>>> gens1 = [[1, 0, 2, 3], [0, 1, 3, 2]]
|
867 |
+
>>> gens2 = [[1, 0]]
|
868 |
+
>>> perm_af_direct_product(gens1, gens2, False)
|
869 |
+
[[1, 0, 2, 3, 4, 5], [0, 1, 3, 2, 4, 5], [0, 1, 2, 3, 5, 4]]
|
870 |
+
>>> gens1 = [[1, 0, 2, 3, 5, 4], [0, 1, 3, 2, 4, 5]]
|
871 |
+
>>> gens2 = [[1, 0, 2, 3]]
|
872 |
+
>>> perm_af_direct_product(gens1, gens2, True)
|
873 |
+
[[1, 0, 2, 3, 4, 5, 7, 6], [0, 1, 3, 2, 4, 5, 6, 7], [0, 1, 2, 3, 5, 4, 6, 7]]
|
874 |
+
"""
|
875 |
+
gens1 = [list(x) for x in gens1]
|
876 |
+
gens2 = [list(x) for x in gens2]
|
877 |
+
s = 2 if signed else 0
|
878 |
+
n1 = len(gens1[0]) - s
|
879 |
+
n2 = len(gens2[0]) - s
|
880 |
+
start = list(range(n1))
|
881 |
+
end = list(range(n1, n1 + n2))
|
882 |
+
if signed:
|
883 |
+
gens1 = [gen[:-2] + end + [gen[-2] + n2, gen[-1] + n2]
|
884 |
+
for gen in gens1]
|
885 |
+
gens2 = [start + [x + n1 for x in gen] for gen in gens2]
|
886 |
+
else:
|
887 |
+
gens1 = [gen + end for gen in gens1]
|
888 |
+
gens2 = [start + [x + n1 for x in gen] for gen in gens2]
|
889 |
+
|
890 |
+
res = gens1 + gens2
|
891 |
+
|
892 |
+
return res
|
893 |
+
|
894 |
+
|
895 |
+
def bsgs_direct_product(base1, gens1, base2, gens2, signed=True):
|
896 |
+
"""
|
897 |
+
Direct product of two BSGS.
|
898 |
+
|
899 |
+
Parameters
|
900 |
+
==========
|
901 |
+
|
902 |
+
base1 : base of the first BSGS.
|
903 |
+
|
904 |
+
gens1 : strong generating sequence of the first BSGS.
|
905 |
+
|
906 |
+
base2, gens2 : similarly for the second BSGS.
|
907 |
+
|
908 |
+
signed : flag for signed permutations.
|
909 |
+
|
910 |
+
Examples
|
911 |
+
========
|
912 |
+
|
913 |
+
>>> from sympy.combinatorics.tensor_can import (get_symmetric_group_sgs, bsgs_direct_product)
|
914 |
+
>>> base1, gens1 = get_symmetric_group_sgs(1)
|
915 |
+
>>> base2, gens2 = get_symmetric_group_sgs(2)
|
916 |
+
>>> bsgs_direct_product(base1, gens1, base2, gens2)
|
917 |
+
([1], [(4)(1 2)])
|
918 |
+
"""
|
919 |
+
s = 2 if signed else 0
|
920 |
+
n1 = gens1[0].size - s
|
921 |
+
base = list(base1)
|
922 |
+
base += [x + n1 for x in base2]
|
923 |
+
gens1 = [h._array_form for h in gens1]
|
924 |
+
gens2 = [h._array_form for h in gens2]
|
925 |
+
gens = perm_af_direct_product(gens1, gens2, signed)
|
926 |
+
size = len(gens[0])
|
927 |
+
id_af = list(range(size))
|
928 |
+
gens = [h for h in gens if h != id_af]
|
929 |
+
if not gens:
|
930 |
+
gens = [id_af]
|
931 |
+
return base, [_af_new(h) for h in gens]
|
932 |
+
|
933 |
+
|
934 |
+
def get_symmetric_group_sgs(n, antisym=False):
|
935 |
+
"""
|
936 |
+
Return base, gens of the minimal BSGS for (anti)symmetric tensor
|
937 |
+
|
938 |
+
Parameters
|
939 |
+
==========
|
940 |
+
|
941 |
+
n : rank of the tensor
|
942 |
+
antisym : bool
|
943 |
+
``antisym = False`` symmetric tensor
|
944 |
+
``antisym = True`` antisymmetric tensor
|
945 |
+
|
946 |
+
Examples
|
947 |
+
========
|
948 |
+
|
949 |
+
>>> from sympy.combinatorics.tensor_can import get_symmetric_group_sgs
|
950 |
+
>>> get_symmetric_group_sgs(3)
|
951 |
+
([0, 1], [(4)(0 1), (4)(1 2)])
|
952 |
+
"""
|
953 |
+
if n == 1:
|
954 |
+
return [], [_af_new(list(range(3)))]
|
955 |
+
gens = [Permutation(n - 1)(i, i + 1)._array_form for i in range(n - 1)]
|
956 |
+
if antisym == 0:
|
957 |
+
gens = [x + [n, n + 1] for x in gens]
|
958 |
+
else:
|
959 |
+
gens = [x + [n + 1, n] for x in gens]
|
960 |
+
base = list(range(n - 1))
|
961 |
+
return base, [_af_new(h) for h in gens]
|
962 |
+
|
963 |
+
riemann_bsgs = [0, 2], [Permutation(0, 1)(4, 5), Permutation(2, 3)(4, 5),
|
964 |
+
Permutation(5)(0, 2)(1, 3)]
|
965 |
+
|
966 |
+
|
967 |
+
def get_transversals(base, gens):
|
968 |
+
"""
|
969 |
+
Return transversals for the group with BSGS base, gens
|
970 |
+
"""
|
971 |
+
if not base:
|
972 |
+
return []
|
973 |
+
stabs = _distribute_gens_by_base(base, gens)
|
974 |
+
orbits, transversals = _orbits_transversals_from_bsgs(base, stabs)
|
975 |
+
transversals = [{x: h._array_form for x, h in y.items()} for y in
|
976 |
+
transversals]
|
977 |
+
return transversals
|
978 |
+
|
979 |
+
|
980 |
+
def _is_minimal_bsgs(base, gens):
|
981 |
+
"""
|
982 |
+
Check if the BSGS has minimal base under lexigographic order.
|
983 |
+
|
984 |
+
base, gens BSGS
|
985 |
+
|
986 |
+
Examples
|
987 |
+
========
|
988 |
+
|
989 |
+
>>> from sympy.combinatorics import Permutation
|
990 |
+
>>> from sympy.combinatorics.tensor_can import riemann_bsgs, _is_minimal_bsgs
|
991 |
+
>>> _is_minimal_bsgs(*riemann_bsgs)
|
992 |
+
True
|
993 |
+
>>> riemann_bsgs1 = ([2, 0], ([Permutation(5)(0, 1)(4, 5), Permutation(5)(0, 2)(1, 3)]))
|
994 |
+
>>> _is_minimal_bsgs(*riemann_bsgs1)
|
995 |
+
False
|
996 |
+
"""
|
997 |
+
base1 = []
|
998 |
+
sgs1 = gens[:]
|
999 |
+
size = gens[0].size
|
1000 |
+
for i in range(size):
|
1001 |
+
if not all(h._array_form[i] == i for h in sgs1):
|
1002 |
+
base1.append(i)
|
1003 |
+
sgs1 = [h for h in sgs1 if h._array_form[i] == i]
|
1004 |
+
return base1 == base
|
1005 |
+
|
1006 |
+
|
1007 |
+
def get_minimal_bsgs(base, gens):
|
1008 |
+
"""
|
1009 |
+
Compute a minimal GSGS
|
1010 |
+
|
1011 |
+
base, gens BSGS
|
1012 |
+
|
1013 |
+
If base, gens is a minimal BSGS return it; else return a minimal BSGS
|
1014 |
+
if it fails in finding one, it returns None
|
1015 |
+
|
1016 |
+
TODO: use baseswap in the case in which if it fails in finding a
|
1017 |
+
minimal BSGS
|
1018 |
+
|
1019 |
+
Examples
|
1020 |
+
========
|
1021 |
+
|
1022 |
+
>>> from sympy.combinatorics import Permutation
|
1023 |
+
>>> from sympy.combinatorics.tensor_can import get_minimal_bsgs
|
1024 |
+
>>> riemann_bsgs1 = ([2, 0], ([Permutation(5)(0, 1)(4, 5), Permutation(5)(0, 2)(1, 3)]))
|
1025 |
+
>>> get_minimal_bsgs(*riemann_bsgs1)
|
1026 |
+
([0, 2], [(0 1)(4 5), (5)(0 2)(1 3), (2 3)(4 5)])
|
1027 |
+
"""
|
1028 |
+
G = PermutationGroup(gens)
|
1029 |
+
base, gens = G.schreier_sims_incremental()
|
1030 |
+
if not _is_minimal_bsgs(base, gens):
|
1031 |
+
return None
|
1032 |
+
return base, gens
|
1033 |
+
|
1034 |
+
|
1035 |
+
def tensor_gens(base, gens, list_free_indices, sym=0):
|
1036 |
+
"""
|
1037 |
+
Returns size, res_base, res_gens BSGS for n tensors of the
|
1038 |
+
same type.
|
1039 |
+
|
1040 |
+
Explanation
|
1041 |
+
===========
|
1042 |
+
|
1043 |
+
base, gens BSGS for tensors of this type
|
1044 |
+
list_free_indices list of the slots occupied by fixed indices
|
1045 |
+
for each of the tensors
|
1046 |
+
|
1047 |
+
sym symmetry under commutation of two tensors
|
1048 |
+
sym None no symmetry
|
1049 |
+
sym 0 commuting
|
1050 |
+
sym 1 anticommuting
|
1051 |
+
|
1052 |
+
Examples
|
1053 |
+
========
|
1054 |
+
|
1055 |
+
>>> from sympy.combinatorics.tensor_can import tensor_gens, get_symmetric_group_sgs
|
1056 |
+
|
1057 |
+
two symmetric tensors with 3 indices without free indices
|
1058 |
+
|
1059 |
+
>>> base, gens = get_symmetric_group_sgs(3)
|
1060 |
+
>>> tensor_gens(base, gens, [[], []])
|
1061 |
+
(8, [0, 1, 3, 4], [(7)(0 1), (7)(1 2), (7)(3 4), (7)(4 5), (7)(0 3)(1 4)(2 5)])
|
1062 |
+
|
1063 |
+
two symmetric tensors with 3 indices with free indices in slot 1 and 0
|
1064 |
+
|
1065 |
+
>>> tensor_gens(base, gens, [[1], [0]])
|
1066 |
+
(8, [0, 4], [(7)(0 2), (7)(4 5)])
|
1067 |
+
|
1068 |
+
four symmetric tensors with 3 indices, two of which with free indices
|
1069 |
+
|
1070 |
+
"""
|
1071 |
+
def _get_bsgs(G, base, gens, free_indices):
|
1072 |
+
"""
|
1073 |
+
return the BSGS for G.pointwise_stabilizer(free_indices)
|
1074 |
+
"""
|
1075 |
+
if not free_indices:
|
1076 |
+
return base[:], gens[:]
|
1077 |
+
else:
|
1078 |
+
H = G.pointwise_stabilizer(free_indices)
|
1079 |
+
base, sgs = H.schreier_sims_incremental()
|
1080 |
+
return base, sgs
|
1081 |
+
|
1082 |
+
# if not base there is no slot symmetry for the component tensors
|
1083 |
+
# if list_free_indices.count([]) < 2 there is no commutation symmetry
|
1084 |
+
# so there is no resulting slot symmetry
|
1085 |
+
if not base and list_free_indices.count([]) < 2:
|
1086 |
+
n = len(list_free_indices)
|
1087 |
+
size = gens[0].size
|
1088 |
+
size = n * (size - 2) + 2
|
1089 |
+
return size, [], [_af_new(list(range(size)))]
|
1090 |
+
|
1091 |
+
# if any(list_free_indices) one needs to compute the pointwise
|
1092 |
+
# stabilizer, so G is needed
|
1093 |
+
if any(list_free_indices):
|
1094 |
+
G = PermutationGroup(gens)
|
1095 |
+
else:
|
1096 |
+
G = None
|
1097 |
+
|
1098 |
+
# no_free list of lists of indices for component tensors without fixed
|
1099 |
+
# indices
|
1100 |
+
no_free = []
|
1101 |
+
size = gens[0].size
|
1102 |
+
id_af = list(range(size))
|
1103 |
+
num_indices = size - 2
|
1104 |
+
if not list_free_indices[0]:
|
1105 |
+
no_free.append(list(range(num_indices)))
|
1106 |
+
res_base, res_gens = _get_bsgs(G, base, gens, list_free_indices[0])
|
1107 |
+
for i in range(1, len(list_free_indices)):
|
1108 |
+
base1, gens1 = _get_bsgs(G, base, gens, list_free_indices[i])
|
1109 |
+
res_base, res_gens = bsgs_direct_product(res_base, res_gens,
|
1110 |
+
base1, gens1, 1)
|
1111 |
+
if not list_free_indices[i]:
|
1112 |
+
no_free.append(list(range(size - 2, size - 2 + num_indices)))
|
1113 |
+
size += num_indices
|
1114 |
+
nr = size - 2
|
1115 |
+
res_gens = [h for h in res_gens if h._array_form != id_af]
|
1116 |
+
# if sym there are no commuting tensors stop here
|
1117 |
+
if sym is None or not no_free:
|
1118 |
+
if not res_gens:
|
1119 |
+
res_gens = [_af_new(id_af)]
|
1120 |
+
return size, res_base, res_gens
|
1121 |
+
|
1122 |
+
# if the component tensors have moinimal BSGS, so is their direct
|
1123 |
+
# product P; the slot symmetry group is S = P*C, where C is the group
|
1124 |
+
# to (anti)commute the component tensors with no free indices
|
1125 |
+
# a stabilizer has the property S_i = P_i*C_i;
|
1126 |
+
# the BSGS of P*C has SGS_P + SGS_C and the base is
|
1127 |
+
# the ordered union of the bases of P and C.
|
1128 |
+
# If P has minimal BSGS, so has S with this base.
|
1129 |
+
base_comm = []
|
1130 |
+
for i in range(len(no_free) - 1):
|
1131 |
+
ind1 = no_free[i]
|
1132 |
+
ind2 = no_free[i + 1]
|
1133 |
+
a = list(range(ind1[0]))
|
1134 |
+
a.extend(ind2)
|
1135 |
+
a.extend(ind1)
|
1136 |
+
base_comm.append(ind1[0])
|
1137 |
+
a.extend(list(range(ind2[-1] + 1, nr)))
|
1138 |
+
if sym == 0:
|
1139 |
+
a.extend([nr, nr + 1])
|
1140 |
+
else:
|
1141 |
+
a.extend([nr + 1, nr])
|
1142 |
+
res_gens.append(_af_new(a))
|
1143 |
+
res_base = list(res_base)
|
1144 |
+
# each base is ordered; order the union of the two bases
|
1145 |
+
for i in base_comm:
|
1146 |
+
if i not in res_base:
|
1147 |
+
res_base.append(i)
|
1148 |
+
res_base.sort()
|
1149 |
+
if not res_gens:
|
1150 |
+
res_gens = [_af_new(id_af)]
|
1151 |
+
|
1152 |
+
return size, res_base, res_gens
|
1153 |
+
|
1154 |
+
|
1155 |
+
def gens_products(*v):
|
1156 |
+
"""
|
1157 |
+
Returns size, res_base, res_gens BSGS for n tensors of different types.
|
1158 |
+
|
1159 |
+
Explanation
|
1160 |
+
===========
|
1161 |
+
|
1162 |
+
v is a sequence of (base_i, gens_i, free_i, sym_i)
|
1163 |
+
where
|
1164 |
+
base_i, gens_i BSGS of tensor of type `i`
|
1165 |
+
free_i list of the fixed slots for each of the tensors
|
1166 |
+
of type `i`; if there are `n_i` tensors of type `i`
|
1167 |
+
and none of them have fixed slots, `free = [[]]*n_i`
|
1168 |
+
sym 0 (1) if the tensors of type `i` (anti)commute among themselves
|
1169 |
+
|
1170 |
+
Examples
|
1171 |
+
========
|
1172 |
+
|
1173 |
+
>>> from sympy.combinatorics.tensor_can import get_symmetric_group_sgs, gens_products
|
1174 |
+
>>> base, gens = get_symmetric_group_sgs(2)
|
1175 |
+
>>> gens_products((base, gens, [[], []], 0))
|
1176 |
+
(6, [0, 2], [(5)(0 1), (5)(2 3), (5)(0 2)(1 3)])
|
1177 |
+
>>> gens_products((base, gens, [[1], []], 0))
|
1178 |
+
(6, [2], [(5)(2 3)])
|
1179 |
+
"""
|
1180 |
+
res_size, res_base, res_gens = tensor_gens(*v[0])
|
1181 |
+
for i in range(1, len(v)):
|
1182 |
+
size, base, gens = tensor_gens(*v[i])
|
1183 |
+
res_base, res_gens = bsgs_direct_product(res_base, res_gens, base,
|
1184 |
+
gens, 1)
|
1185 |
+
res_size = res_gens[0].size
|
1186 |
+
id_af = list(range(res_size))
|
1187 |
+
res_gens = [h for h in res_gens if h != id_af]
|
1188 |
+
if not res_gens:
|
1189 |
+
res_gens = [id_af]
|
1190 |
+
return res_size, res_base, res_gens
|
venv/lib/python3.10/site-packages/sympy/combinatorics/tests/test_testutil.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics.named_groups import SymmetricGroup, AlternatingGroup,\
|
2 |
+
CyclicGroup
|
3 |
+
from sympy.combinatorics.testutil import _verify_bsgs, _cmp_perm_lists,\
|
4 |
+
_naive_list_centralizer, _verify_centralizer,\
|
5 |
+
_verify_normal_closure
|
6 |
+
from sympy.combinatorics.permutations import Permutation
|
7 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
8 |
+
from sympy.core.random import shuffle
|
9 |
+
|
10 |
+
|
11 |
+
def test_cmp_perm_lists():
|
12 |
+
S = SymmetricGroup(4)
|
13 |
+
els = list(S.generate_dimino())
|
14 |
+
other = els[:]
|
15 |
+
shuffle(other)
|
16 |
+
assert _cmp_perm_lists(els, other) is True
|
17 |
+
|
18 |
+
|
19 |
+
def test_naive_list_centralizer():
|
20 |
+
# verified by GAP
|
21 |
+
S = SymmetricGroup(3)
|
22 |
+
A = AlternatingGroup(3)
|
23 |
+
assert _naive_list_centralizer(S, S) == [Permutation([0, 1, 2])]
|
24 |
+
assert PermutationGroup(_naive_list_centralizer(S, A)).is_subgroup(A)
|
25 |
+
|
26 |
+
|
27 |
+
def test_verify_bsgs():
|
28 |
+
S = SymmetricGroup(5)
|
29 |
+
S.schreier_sims()
|
30 |
+
base = S.base
|
31 |
+
strong_gens = S.strong_gens
|
32 |
+
assert _verify_bsgs(S, base, strong_gens) is True
|
33 |
+
assert _verify_bsgs(S, base[:-1], strong_gens) is False
|
34 |
+
assert _verify_bsgs(S, base, S.generators) is False
|
35 |
+
|
36 |
+
|
37 |
+
def test_verify_centralizer():
|
38 |
+
# verified by GAP
|
39 |
+
S = SymmetricGroup(3)
|
40 |
+
A = AlternatingGroup(3)
|
41 |
+
triv = PermutationGroup([Permutation([0, 1, 2])])
|
42 |
+
assert _verify_centralizer(S, S, centr=triv)
|
43 |
+
assert _verify_centralizer(S, A, centr=A)
|
44 |
+
|
45 |
+
|
46 |
+
def test_verify_normal_closure():
|
47 |
+
# verified by GAP
|
48 |
+
S = SymmetricGroup(3)
|
49 |
+
A = AlternatingGroup(3)
|
50 |
+
assert _verify_normal_closure(S, A, closure=A)
|
51 |
+
S = SymmetricGroup(5)
|
52 |
+
A = AlternatingGroup(5)
|
53 |
+
C = CyclicGroup(5)
|
54 |
+
assert _verify_normal_closure(S, A, closure=A)
|
55 |
+
assert _verify_normal_closure(S, C, closure=A)
|
venv/lib/python3.10/site-packages/sympy/combinatorics/testutil.py
ADDED
@@ -0,0 +1,358 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.combinatorics import Permutation
|
2 |
+
from sympy.combinatorics.util import _distribute_gens_by_base
|
3 |
+
|
4 |
+
rmul = Permutation.rmul
|
5 |
+
|
6 |
+
|
7 |
+
def _cmp_perm_lists(first, second):
|
8 |
+
"""
|
9 |
+
Compare two lists of permutations as sets.
|
10 |
+
|
11 |
+
Explanation
|
12 |
+
===========
|
13 |
+
|
14 |
+
This is used for testing purposes. Since the array form of a
|
15 |
+
permutation is currently a list, Permutation is not hashable
|
16 |
+
and cannot be put into a set.
|
17 |
+
|
18 |
+
Examples
|
19 |
+
========
|
20 |
+
|
21 |
+
>>> from sympy.combinatorics.permutations import Permutation
|
22 |
+
>>> from sympy.combinatorics.testutil import _cmp_perm_lists
|
23 |
+
>>> a = Permutation([0, 2, 3, 4, 1])
|
24 |
+
>>> b = Permutation([1, 2, 0, 4, 3])
|
25 |
+
>>> c = Permutation([3, 4, 0, 1, 2])
|
26 |
+
>>> ls1 = [a, b, c]
|
27 |
+
>>> ls2 = [b, c, a]
|
28 |
+
>>> _cmp_perm_lists(ls1, ls2)
|
29 |
+
True
|
30 |
+
|
31 |
+
"""
|
32 |
+
return {tuple(a) for a in first} == \
|
33 |
+
{tuple(a) for a in second}
|
34 |
+
|
35 |
+
|
36 |
+
def _naive_list_centralizer(self, other, af=False):
|
37 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
38 |
+
"""
|
39 |
+
Return a list of elements for the centralizer of a subgroup/set/element.
|
40 |
+
|
41 |
+
Explanation
|
42 |
+
===========
|
43 |
+
|
44 |
+
This is a brute force implementation that goes over all elements of the
|
45 |
+
group and checks for membership in the centralizer. It is used to
|
46 |
+
test ``.centralizer()`` from ``sympy.combinatorics.perm_groups``.
|
47 |
+
|
48 |
+
Examples
|
49 |
+
========
|
50 |
+
|
51 |
+
>>> from sympy.combinatorics.testutil import _naive_list_centralizer
|
52 |
+
>>> from sympy.combinatorics.named_groups import DihedralGroup
|
53 |
+
>>> D = DihedralGroup(4)
|
54 |
+
>>> _naive_list_centralizer(D, D)
|
55 |
+
[Permutation([0, 1, 2, 3]), Permutation([2, 3, 0, 1])]
|
56 |
+
|
57 |
+
See Also
|
58 |
+
========
|
59 |
+
|
60 |
+
sympy.combinatorics.perm_groups.centralizer
|
61 |
+
|
62 |
+
"""
|
63 |
+
from sympy.combinatorics.permutations import _af_commutes_with
|
64 |
+
if hasattr(other, 'generators'):
|
65 |
+
elements = list(self.generate_dimino(af=True))
|
66 |
+
gens = [x._array_form for x in other.generators]
|
67 |
+
commutes_with_gens = lambda x: all(_af_commutes_with(x, gen) for gen in gens)
|
68 |
+
centralizer_list = []
|
69 |
+
if not af:
|
70 |
+
for element in elements:
|
71 |
+
if commutes_with_gens(element):
|
72 |
+
centralizer_list.append(Permutation._af_new(element))
|
73 |
+
else:
|
74 |
+
for element in elements:
|
75 |
+
if commutes_with_gens(element):
|
76 |
+
centralizer_list.append(element)
|
77 |
+
return centralizer_list
|
78 |
+
elif hasattr(other, 'getitem'):
|
79 |
+
return _naive_list_centralizer(self, PermutationGroup(other), af)
|
80 |
+
elif hasattr(other, 'array_form'):
|
81 |
+
return _naive_list_centralizer(self, PermutationGroup([other]), af)
|
82 |
+
|
83 |
+
|
84 |
+
def _verify_bsgs(group, base, gens):
|
85 |
+
"""
|
86 |
+
Verify the correctness of a base and strong generating set.
|
87 |
+
|
88 |
+
Explanation
|
89 |
+
===========
|
90 |
+
|
91 |
+
This is a naive implementation using the definition of a base and a strong
|
92 |
+
generating set relative to it. There are other procedures for
|
93 |
+
verifying a base and strong generating set, but this one will
|
94 |
+
serve for more robust testing.
|
95 |
+
|
96 |
+
Examples
|
97 |
+
========
|
98 |
+
|
99 |
+
>>> from sympy.combinatorics.named_groups import AlternatingGroup
|
100 |
+
>>> from sympy.combinatorics.testutil import _verify_bsgs
|
101 |
+
>>> A = AlternatingGroup(4)
|
102 |
+
>>> A.schreier_sims()
|
103 |
+
>>> _verify_bsgs(A, A.base, A.strong_gens)
|
104 |
+
True
|
105 |
+
|
106 |
+
See Also
|
107 |
+
========
|
108 |
+
|
109 |
+
sympy.combinatorics.perm_groups.PermutationGroup.schreier_sims
|
110 |
+
|
111 |
+
"""
|
112 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
113 |
+
strong_gens_distr = _distribute_gens_by_base(base, gens)
|
114 |
+
current_stabilizer = group
|
115 |
+
for i in range(len(base)):
|
116 |
+
candidate = PermutationGroup(strong_gens_distr[i])
|
117 |
+
if current_stabilizer.order() != candidate.order():
|
118 |
+
return False
|
119 |
+
current_stabilizer = current_stabilizer.stabilizer(base[i])
|
120 |
+
if current_stabilizer.order() != 1:
|
121 |
+
return False
|
122 |
+
return True
|
123 |
+
|
124 |
+
|
125 |
+
def _verify_centralizer(group, arg, centr=None):
|
126 |
+
"""
|
127 |
+
Verify the centralizer of a group/set/element inside another group.
|
128 |
+
|
129 |
+
This is used for testing ``.centralizer()`` from
|
130 |
+
``sympy.combinatorics.perm_groups``
|
131 |
+
|
132 |
+
Examples
|
133 |
+
========
|
134 |
+
|
135 |
+
>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
|
136 |
+
... AlternatingGroup)
|
137 |
+
>>> from sympy.combinatorics.perm_groups import PermutationGroup
|
138 |
+
>>> from sympy.combinatorics.permutations import Permutation
|
139 |
+
>>> from sympy.combinatorics.testutil import _verify_centralizer
|
140 |
+
>>> S = SymmetricGroup(5)
|
141 |
+
>>> A = AlternatingGroup(5)
|
142 |
+
>>> centr = PermutationGroup([Permutation([0, 1, 2, 3, 4])])
|
143 |
+
>>> _verify_centralizer(S, A, centr)
|
144 |
+
True
|
145 |
+
|
146 |
+
See Also
|
147 |
+
========
|
148 |
+
|
149 |
+
_naive_list_centralizer,
|
150 |
+
sympy.combinatorics.perm_groups.PermutationGroup.centralizer,
|
151 |
+
_cmp_perm_lists
|
152 |
+
|
153 |
+
"""
|
154 |
+
if centr is None:
|
155 |
+
centr = group.centralizer(arg)
|
156 |
+
centr_list = list(centr.generate_dimino(af=True))
|
157 |
+
centr_list_naive = _naive_list_centralizer(group, arg, af=True)
|
158 |
+
return _cmp_perm_lists(centr_list, centr_list_naive)
|
159 |
+
|
160 |
+
|
161 |
+
def _verify_normal_closure(group, arg, closure=None):
|
162 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
163 |
+
"""
|
164 |
+
Verify the normal closure of a subgroup/subset/element in a group.
|
165 |
+
|
166 |
+
This is used to test
|
167 |
+
sympy.combinatorics.perm_groups.PermutationGroup.normal_closure
|
168 |
+
|
169 |
+
Examples
|
170 |
+
========
|
171 |
+
|
172 |
+
>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
|
173 |
+
... AlternatingGroup)
|
174 |
+
>>> from sympy.combinatorics.testutil import _verify_normal_closure
|
175 |
+
>>> S = SymmetricGroup(3)
|
176 |
+
>>> A = AlternatingGroup(3)
|
177 |
+
>>> _verify_normal_closure(S, A, closure=A)
|
178 |
+
True
|
179 |
+
|
180 |
+
See Also
|
181 |
+
========
|
182 |
+
|
183 |
+
sympy.combinatorics.perm_groups.PermutationGroup.normal_closure
|
184 |
+
|
185 |
+
"""
|
186 |
+
if closure is None:
|
187 |
+
closure = group.normal_closure(arg)
|
188 |
+
conjugates = set()
|
189 |
+
if hasattr(arg, 'generators'):
|
190 |
+
subgr_gens = arg.generators
|
191 |
+
elif hasattr(arg, '__getitem__'):
|
192 |
+
subgr_gens = arg
|
193 |
+
elif hasattr(arg, 'array_form'):
|
194 |
+
subgr_gens = [arg]
|
195 |
+
for el in group.generate_dimino():
|
196 |
+
for gen in subgr_gens:
|
197 |
+
conjugates.add(gen ^ el)
|
198 |
+
naive_closure = PermutationGroup(list(conjugates))
|
199 |
+
return closure.is_subgroup(naive_closure)
|
200 |
+
|
201 |
+
|
202 |
+
def canonicalize_naive(g, dummies, sym, *v):
|
203 |
+
"""
|
204 |
+
Canonicalize tensor formed by tensors of the different types.
|
205 |
+
|
206 |
+
Explanation
|
207 |
+
===========
|
208 |
+
|
209 |
+
sym_i symmetry under exchange of two component tensors of type `i`
|
210 |
+
None no symmetry
|
211 |
+
0 commuting
|
212 |
+
1 anticommuting
|
213 |
+
|
214 |
+
Parameters
|
215 |
+
==========
|
216 |
+
|
217 |
+
g : Permutation representing the tensor.
|
218 |
+
dummies : List of dummy indices.
|
219 |
+
msym : Symmetry of the metric.
|
220 |
+
v : A list of (base_i, gens_i, n_i, sym_i) for tensors of type `i`.
|
221 |
+
base_i, gens_i BSGS for tensors of this type
|
222 |
+
n_i number of tensors of type `i`
|
223 |
+
|
224 |
+
Returns
|
225 |
+
=======
|
226 |
+
|
227 |
+
Returns 0 if the tensor is zero, else returns the array form of
|
228 |
+
the permutation representing the canonical form of the tensor.
|
229 |
+
|
230 |
+
Examples
|
231 |
+
========
|
232 |
+
|
233 |
+
>>> from sympy.combinatorics.testutil import canonicalize_naive
|
234 |
+
>>> from sympy.combinatorics.tensor_can import get_symmetric_group_sgs
|
235 |
+
>>> from sympy.combinatorics import Permutation
|
236 |
+
>>> g = Permutation([1, 3, 2, 0, 4, 5])
|
237 |
+
>>> base2, gens2 = get_symmetric_group_sgs(2)
|
238 |
+
>>> canonicalize_naive(g, [2, 3], 0, (base2, gens2, 2, 0))
|
239 |
+
[0, 2, 1, 3, 4, 5]
|
240 |
+
"""
|
241 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
242 |
+
from sympy.combinatorics.tensor_can import gens_products, dummy_sgs
|
243 |
+
from sympy.combinatorics.permutations import _af_rmul
|
244 |
+
v1 = []
|
245 |
+
for i in range(len(v)):
|
246 |
+
base_i, gens_i, n_i, sym_i = v[i]
|
247 |
+
v1.append((base_i, gens_i, [[]]*n_i, sym_i))
|
248 |
+
size, sbase, sgens = gens_products(*v1)
|
249 |
+
dgens = dummy_sgs(dummies, sym, size-2)
|
250 |
+
if isinstance(sym, int):
|
251 |
+
num_types = 1
|
252 |
+
dummies = [dummies]
|
253 |
+
sym = [sym]
|
254 |
+
else:
|
255 |
+
num_types = len(sym)
|
256 |
+
dgens = []
|
257 |
+
for i in range(num_types):
|
258 |
+
dgens.extend(dummy_sgs(dummies[i], sym[i], size - 2))
|
259 |
+
S = PermutationGroup(sgens)
|
260 |
+
D = PermutationGroup([Permutation(x) for x in dgens])
|
261 |
+
dlist = list(D.generate(af=True))
|
262 |
+
g = g.array_form
|
263 |
+
st = set()
|
264 |
+
for s in S.generate(af=True):
|
265 |
+
h = _af_rmul(g, s)
|
266 |
+
for d in dlist:
|
267 |
+
q = tuple(_af_rmul(d, h))
|
268 |
+
st.add(q)
|
269 |
+
a = list(st)
|
270 |
+
a.sort()
|
271 |
+
prev = (0,)*size
|
272 |
+
for h in a:
|
273 |
+
if h[:-2] == prev[:-2]:
|
274 |
+
if h[-1] != prev[-1]:
|
275 |
+
return 0
|
276 |
+
prev = h
|
277 |
+
return list(a[0])
|
278 |
+
|
279 |
+
|
280 |
+
def graph_certificate(gr):
|
281 |
+
"""
|
282 |
+
Return a certificate for the graph
|
283 |
+
|
284 |
+
Parameters
|
285 |
+
==========
|
286 |
+
|
287 |
+
gr : adjacency list
|
288 |
+
|
289 |
+
Explanation
|
290 |
+
===========
|
291 |
+
|
292 |
+
The graph is assumed to be unoriented and without
|
293 |
+
external lines.
|
294 |
+
|
295 |
+
Associate to each vertex of the graph a symmetric tensor with
|
296 |
+
number of indices equal to the degree of the vertex; indices
|
297 |
+
are contracted when they correspond to the same line of the graph.
|
298 |
+
The canonical form of the tensor gives a certificate for the graph.
|
299 |
+
|
300 |
+
This is not an efficient algorithm to get the certificate of a graph.
|
301 |
+
|
302 |
+
Examples
|
303 |
+
========
|
304 |
+
|
305 |
+
>>> from sympy.combinatorics.testutil import graph_certificate
|
306 |
+
>>> gr1 = {0:[1, 2, 3, 5], 1:[0, 2, 4], 2:[0, 1, 3, 4], 3:[0, 2, 4], 4:[1, 2, 3, 5], 5:[0, 4]}
|
307 |
+
>>> gr2 = {0:[1, 5], 1:[0, 2, 3, 4], 2:[1, 3, 5], 3:[1, 2, 4, 5], 4:[1, 3, 5], 5:[0, 2, 3, 4]}
|
308 |
+
>>> c1 = graph_certificate(gr1)
|
309 |
+
>>> c2 = graph_certificate(gr2)
|
310 |
+
>>> c1
|
311 |
+
[0, 2, 4, 6, 1, 8, 10, 12, 3, 14, 16, 18, 5, 9, 15, 7, 11, 17, 13, 19, 20, 21]
|
312 |
+
>>> c1 == c2
|
313 |
+
True
|
314 |
+
"""
|
315 |
+
from sympy.combinatorics.permutations import _af_invert
|
316 |
+
from sympy.combinatorics.tensor_can import get_symmetric_group_sgs, canonicalize
|
317 |
+
items = list(gr.items())
|
318 |
+
items.sort(key=lambda x: len(x[1]), reverse=True)
|
319 |
+
pvert = [x[0] for x in items]
|
320 |
+
pvert = _af_invert(pvert)
|
321 |
+
|
322 |
+
# the indices of the tensor are twice the number of lines of the graph
|
323 |
+
num_indices = 0
|
324 |
+
for v, neigh in items:
|
325 |
+
num_indices += len(neigh)
|
326 |
+
# associate to each vertex its indices; for each line
|
327 |
+
# between two vertices assign the
|
328 |
+
# even index to the vertex which comes first in items,
|
329 |
+
# the odd index to the other vertex
|
330 |
+
vertices = [[] for i in items]
|
331 |
+
i = 0
|
332 |
+
for v, neigh in items:
|
333 |
+
for v2 in neigh:
|
334 |
+
if pvert[v] < pvert[v2]:
|
335 |
+
vertices[pvert[v]].append(i)
|
336 |
+
vertices[pvert[v2]].append(i+1)
|
337 |
+
i += 2
|
338 |
+
g = []
|
339 |
+
for v in vertices:
|
340 |
+
g.extend(v)
|
341 |
+
assert len(g) == num_indices
|
342 |
+
g += [num_indices, num_indices + 1]
|
343 |
+
size = num_indices + 2
|
344 |
+
assert sorted(g) == list(range(size))
|
345 |
+
g = Permutation(g)
|
346 |
+
vlen = [0]*(len(vertices[0])+1)
|
347 |
+
for neigh in vertices:
|
348 |
+
vlen[len(neigh)] += 1
|
349 |
+
v = []
|
350 |
+
for i in range(len(vlen)):
|
351 |
+
n = vlen[i]
|
352 |
+
if n:
|
353 |
+
base, gens = get_symmetric_group_sgs(i)
|
354 |
+
v.append((base, gens, n, 0))
|
355 |
+
v.reverse()
|
356 |
+
dummies = list(range(num_indices))
|
357 |
+
can = canonicalize(g, dummies, 0, *v)
|
358 |
+
return can
|
venv/lib/python3.10/site-packages/sympy/combinatorics/util.py
ADDED
@@ -0,0 +1,536 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
_af_new = Permutation._af_new
|
6 |
+
|
7 |
+
############################################
|
8 |
+
#
|
9 |
+
# Utilities for computational group theory
|
10 |
+
#
|
11 |
+
############################################
|
12 |
+
|
13 |
+
|
14 |
+
def _base_ordering(base, degree):
|
15 |
+
r"""
|
16 |
+
Order `\{0, 1, \dots, n-1\}` so that base points come first and in order.
|
17 |
+
|
18 |
+
Parameters
|
19 |
+
==========
|
20 |
+
|
21 |
+
base : the base
|
22 |
+
degree : the degree of the associated permutation group
|
23 |
+
|
24 |
+
Returns
|
25 |
+
=======
|
26 |
+
|
27 |
+
A list ``base_ordering`` such that ``base_ordering[point]`` is the
|
28 |
+
number of ``point`` in the ordering.
|
29 |
+
|
30 |
+
Examples
|
31 |
+
========
|
32 |
+
|
33 |
+
>>> from sympy.combinatorics import SymmetricGroup
|
34 |
+
>>> from sympy.combinatorics.util import _base_ordering
|
35 |
+
>>> S = SymmetricGroup(4)
|
36 |
+
>>> S.schreier_sims()
|
37 |
+
>>> _base_ordering(S.base, S.degree)
|
38 |
+
[0, 1, 2, 3]
|
39 |
+
|
40 |
+
Notes
|
41 |
+
=====
|
42 |
+
|
43 |
+
This is used in backtrack searches, when we define a relation `\ll` on
|
44 |
+
the underlying set for a permutation group of degree `n`,
|
45 |
+
`\{0, 1, \dots, n-1\}`, so that if `(b_1, b_2, \dots, b_k)` is a base we
|
46 |
+
have `b_i \ll b_j` whenever `i<j` and `b_i \ll a` for all
|
47 |
+
`i\in\{1,2, \dots, k\}` and `a` is not in the base. The idea is developed
|
48 |
+
and applied to backtracking algorithms in [1], pp.108-132. The points
|
49 |
+
that are not in the base are taken in increasing order.
|
50 |
+
|
51 |
+
References
|
52 |
+
==========
|
53 |
+
|
54 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.
|
55 |
+
"Handbook of computational group theory"
|
56 |
+
|
57 |
+
"""
|
58 |
+
base_len = len(base)
|
59 |
+
ordering = [0]*degree
|
60 |
+
for i in range(base_len):
|
61 |
+
ordering[base[i]] = i
|
62 |
+
current = base_len
|
63 |
+
for i in range(degree):
|
64 |
+
if i not in base:
|
65 |
+
ordering[i] = current
|
66 |
+
current += 1
|
67 |
+
return ordering
|
68 |
+
|
69 |
+
|
70 |
+
def _check_cycles_alt_sym(perm):
|
71 |
+
"""
|
72 |
+
Checks for cycles of prime length p with n/2 < p < n-2.
|
73 |
+
|
74 |
+
Explanation
|
75 |
+
===========
|
76 |
+
|
77 |
+
Here `n` is the degree of the permutation. This is a helper function for
|
78 |
+
the function is_alt_sym from sympy.combinatorics.perm_groups.
|
79 |
+
|
80 |
+
Examples
|
81 |
+
========
|
82 |
+
|
83 |
+
>>> from sympy.combinatorics.util import _check_cycles_alt_sym
|
84 |
+
>>> from sympy.combinatorics import Permutation
|
85 |
+
>>> a = Permutation([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12]])
|
86 |
+
>>> _check_cycles_alt_sym(a)
|
87 |
+
False
|
88 |
+
>>> b = Permutation([[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10]])
|
89 |
+
>>> _check_cycles_alt_sym(b)
|
90 |
+
True
|
91 |
+
|
92 |
+
See Also
|
93 |
+
========
|
94 |
+
|
95 |
+
sympy.combinatorics.perm_groups.PermutationGroup.is_alt_sym
|
96 |
+
|
97 |
+
"""
|
98 |
+
n = perm.size
|
99 |
+
af = perm.array_form
|
100 |
+
current_len = 0
|
101 |
+
total_len = 0
|
102 |
+
used = set()
|
103 |
+
for i in range(n//2):
|
104 |
+
if i not in used and i < n//2 - total_len:
|
105 |
+
current_len = 1
|
106 |
+
used.add(i)
|
107 |
+
j = i
|
108 |
+
while af[j] != i:
|
109 |
+
current_len += 1
|
110 |
+
j = af[j]
|
111 |
+
used.add(j)
|
112 |
+
total_len += current_len
|
113 |
+
if current_len > n//2 and current_len < n - 2 and isprime(current_len):
|
114 |
+
return True
|
115 |
+
return False
|
116 |
+
|
117 |
+
|
118 |
+
def _distribute_gens_by_base(base, gens):
|
119 |
+
r"""
|
120 |
+
Distribute the group elements ``gens`` by membership in basic stabilizers.
|
121 |
+
|
122 |
+
Explanation
|
123 |
+
===========
|
124 |
+
|
125 |
+
Notice that for a base `(b_1, b_2, \dots, b_k)`, the basic stabilizers
|
126 |
+
are defined as `G^{(i)} = G_{b_1, \dots, b_{i-1}}` for
|
127 |
+
`i \in\{1, 2, \dots, k\}`.
|
128 |
+
|
129 |
+
Parameters
|
130 |
+
==========
|
131 |
+
|
132 |
+
base : a sequence of points in `\{0, 1, \dots, n-1\}`
|
133 |
+
gens : a list of elements of a permutation group of degree `n`.
|
134 |
+
|
135 |
+
Returns
|
136 |
+
=======
|
137 |
+
|
138 |
+
List of length `k`, where `k` is
|
139 |
+
the length of ``base``. The `i`-th entry contains those elements in
|
140 |
+
``gens`` which fix the first `i` elements of ``base`` (so that the
|
141 |
+
`0`-th entry is equal to ``gens`` itself). If no element fixes the first
|
142 |
+
`i` elements of ``base``, the `i`-th element is set to a list containing
|
143 |
+
the identity element.
|
144 |
+
|
145 |
+
Examples
|
146 |
+
========
|
147 |
+
|
148 |
+
>>> from sympy.combinatorics.named_groups import DihedralGroup
|
149 |
+
>>> from sympy.combinatorics.util import _distribute_gens_by_base
|
150 |
+
>>> D = DihedralGroup(3)
|
151 |
+
>>> D.schreier_sims()
|
152 |
+
>>> D.strong_gens
|
153 |
+
[(0 1 2), (0 2), (1 2)]
|
154 |
+
>>> D.base
|
155 |
+
[0, 1]
|
156 |
+
>>> _distribute_gens_by_base(D.base, D.strong_gens)
|
157 |
+
[[(0 1 2), (0 2), (1 2)],
|
158 |
+
[(1 2)]]
|
159 |
+
|
160 |
+
See Also
|
161 |
+
========
|
162 |
+
|
163 |
+
_strong_gens_from_distr, _orbits_transversals_from_bsgs,
|
164 |
+
_handle_precomputed_bsgs
|
165 |
+
|
166 |
+
"""
|
167 |
+
base_len = len(base)
|
168 |
+
degree = gens[0].size
|
169 |
+
stabs = [[] for _ in range(base_len)]
|
170 |
+
max_stab_index = 0
|
171 |
+
for gen in gens:
|
172 |
+
j = 0
|
173 |
+
while j < base_len - 1 and gen._array_form[base[j]] == base[j]:
|
174 |
+
j += 1
|
175 |
+
if j > max_stab_index:
|
176 |
+
max_stab_index = j
|
177 |
+
for k in range(j + 1):
|
178 |
+
stabs[k].append(gen)
|
179 |
+
for i in range(max_stab_index + 1, base_len):
|
180 |
+
stabs[i].append(_af_new(list(range(degree))))
|
181 |
+
return stabs
|
182 |
+
|
183 |
+
|
184 |
+
def _handle_precomputed_bsgs(base, strong_gens, transversals=None,
|
185 |
+
basic_orbits=None, strong_gens_distr=None):
|
186 |
+
"""
|
187 |
+
Calculate BSGS-related structures from those present.
|
188 |
+
|
189 |
+
Explanation
|
190 |
+
===========
|
191 |
+
|
192 |
+
The base and strong generating set must be provided; if any of the
|
193 |
+
transversals, basic orbits or distributed strong generators are not
|
194 |
+
provided, they will be calculated from the base and strong generating set.
|
195 |
+
|
196 |
+
Parameters
|
197 |
+
==========
|
198 |
+
|
199 |
+
``base`` - the base
|
200 |
+
``strong_gens`` - the strong generators
|
201 |
+
``transversals`` - basic transversals
|
202 |
+
``basic_orbits`` - basic orbits
|
203 |
+
``strong_gens_distr`` - strong generators distributed by membership in basic
|
204 |
+
stabilizers
|
205 |
+
|
206 |
+
Returns
|
207 |
+
=======
|
208 |
+
|
209 |
+
``(transversals, basic_orbits, strong_gens_distr)`` where ``transversals``
|
210 |
+
are the basic transversals, ``basic_orbits`` are the basic orbits, and
|
211 |
+
``strong_gens_distr`` are the strong generators distributed by membership
|
212 |
+
in basic stabilizers.
|
213 |
+
|
214 |
+
Examples
|
215 |
+
========
|
216 |
+
|
217 |
+
>>> from sympy.combinatorics.named_groups import DihedralGroup
|
218 |
+
>>> from sympy.combinatorics.util import _handle_precomputed_bsgs
|
219 |
+
>>> D = DihedralGroup(3)
|
220 |
+
>>> D.schreier_sims()
|
221 |
+
>>> _handle_precomputed_bsgs(D.base, D.strong_gens,
|
222 |
+
... basic_orbits=D.basic_orbits)
|
223 |
+
([{0: (2), 1: (0 1 2), 2: (0 2)}, {1: (2), 2: (1 2)}], [[0, 1, 2], [1, 2]], [[(0 1 2), (0 2), (1 2)], [(1 2)]])
|
224 |
+
|
225 |
+
See Also
|
226 |
+
========
|
227 |
+
|
228 |
+
_orbits_transversals_from_bsgs, _distribute_gens_by_base
|
229 |
+
|
230 |
+
"""
|
231 |
+
if strong_gens_distr is None:
|
232 |
+
strong_gens_distr = _distribute_gens_by_base(base, strong_gens)
|
233 |
+
if transversals is None:
|
234 |
+
if basic_orbits is None:
|
235 |
+
basic_orbits, transversals = \
|
236 |
+
_orbits_transversals_from_bsgs(base, strong_gens_distr)
|
237 |
+
else:
|
238 |
+
transversals = \
|
239 |
+
_orbits_transversals_from_bsgs(base, strong_gens_distr,
|
240 |
+
transversals_only=True)
|
241 |
+
else:
|
242 |
+
if basic_orbits is None:
|
243 |
+
base_len = len(base)
|
244 |
+
basic_orbits = [None]*base_len
|
245 |
+
for i in range(base_len):
|
246 |
+
basic_orbits[i] = list(transversals[i].keys())
|
247 |
+
return transversals, basic_orbits, strong_gens_distr
|
248 |
+
|
249 |
+
|
250 |
+
def _orbits_transversals_from_bsgs(base, strong_gens_distr,
|
251 |
+
transversals_only=False, slp=False):
|
252 |
+
"""
|
253 |
+
Compute basic orbits and transversals from a base and strong generating set.
|
254 |
+
|
255 |
+
Explanation
|
256 |
+
===========
|
257 |
+
|
258 |
+
The generators are provided as distributed across the basic stabilizers.
|
259 |
+
If the optional argument ``transversals_only`` is set to True, only the
|
260 |
+
transversals are returned.
|
261 |
+
|
262 |
+
Parameters
|
263 |
+
==========
|
264 |
+
|
265 |
+
``base`` - The base.
|
266 |
+
``strong_gens_distr`` - Strong generators distributed by membership in basic
|
267 |
+
stabilizers.
|
268 |
+
``transversals_only`` - bool
|
269 |
+
A flag switching between returning only the
|
270 |
+
transversals and both orbits and transversals.
|
271 |
+
``slp`` -
|
272 |
+
If ``True``, return a list of dictionaries containing the
|
273 |
+
generator presentations of the elements of the transversals,
|
274 |
+
i.e. the list of indices of generators from ``strong_gens_distr[i]``
|
275 |
+
such that their product is the relevant transversal element.
|
276 |
+
|
277 |
+
Examples
|
278 |
+
========
|
279 |
+
|
280 |
+
>>> from sympy.combinatorics import SymmetricGroup
|
281 |
+
>>> from sympy.combinatorics.util import _distribute_gens_by_base
|
282 |
+
>>> S = SymmetricGroup(3)
|
283 |
+
>>> S.schreier_sims()
|
284 |
+
>>> strong_gens_distr = _distribute_gens_by_base(S.base, S.strong_gens)
|
285 |
+
>>> (S.base, strong_gens_distr)
|
286 |
+
([0, 1], [[(0 1 2), (2)(0 1), (1 2)], [(1 2)]])
|
287 |
+
|
288 |
+
See Also
|
289 |
+
========
|
290 |
+
|
291 |
+
_distribute_gens_by_base, _handle_precomputed_bsgs
|
292 |
+
|
293 |
+
"""
|
294 |
+
from sympy.combinatorics.perm_groups import _orbit_transversal
|
295 |
+
base_len = len(base)
|
296 |
+
degree = strong_gens_distr[0][0].size
|
297 |
+
transversals = [None]*base_len
|
298 |
+
slps = [None]*base_len
|
299 |
+
if transversals_only is False:
|
300 |
+
basic_orbits = [None]*base_len
|
301 |
+
for i in range(base_len):
|
302 |
+
transversals[i], slps[i] = _orbit_transversal(degree, strong_gens_distr[i],
|
303 |
+
base[i], pairs=True, slp=True)
|
304 |
+
transversals[i] = dict(transversals[i])
|
305 |
+
if transversals_only is False:
|
306 |
+
basic_orbits[i] = list(transversals[i].keys())
|
307 |
+
if transversals_only:
|
308 |
+
return transversals
|
309 |
+
else:
|
310 |
+
if not slp:
|
311 |
+
return basic_orbits, transversals
|
312 |
+
return basic_orbits, transversals, slps
|
313 |
+
|
314 |
+
|
315 |
+
def _remove_gens(base, strong_gens, basic_orbits=None, strong_gens_distr=None):
|
316 |
+
"""
|
317 |
+
Remove redundant generators from a strong generating set.
|
318 |
+
|
319 |
+
Parameters
|
320 |
+
==========
|
321 |
+
|
322 |
+
``base`` - a base
|
323 |
+
``strong_gens`` - a strong generating set relative to ``base``
|
324 |
+
``basic_orbits`` - basic orbits
|
325 |
+
``strong_gens_distr`` - strong generators distributed by membership in basic
|
326 |
+
stabilizers
|
327 |
+
|
328 |
+
Returns
|
329 |
+
=======
|
330 |
+
|
331 |
+
A strong generating set with respect to ``base`` which is a subset of
|
332 |
+
``strong_gens``.
|
333 |
+
|
334 |
+
Examples
|
335 |
+
========
|
336 |
+
|
337 |
+
>>> from sympy.combinatorics import SymmetricGroup
|
338 |
+
>>> from sympy.combinatorics.util import _remove_gens
|
339 |
+
>>> from sympy.combinatorics.testutil import _verify_bsgs
|
340 |
+
>>> S = SymmetricGroup(15)
|
341 |
+
>>> base, strong_gens = S.schreier_sims_incremental()
|
342 |
+
>>> new_gens = _remove_gens(base, strong_gens)
|
343 |
+
>>> len(new_gens)
|
344 |
+
14
|
345 |
+
>>> _verify_bsgs(S, base, new_gens)
|
346 |
+
True
|
347 |
+
|
348 |
+
Notes
|
349 |
+
=====
|
350 |
+
|
351 |
+
This procedure is outlined in [1],p.95.
|
352 |
+
|
353 |
+
References
|
354 |
+
==========
|
355 |
+
|
356 |
+
.. [1] Holt, D., Eick, B., O'Brien, E.
|
357 |
+
"Handbook of computational group theory"
|
358 |
+
|
359 |
+
"""
|
360 |
+
from sympy.combinatorics.perm_groups import _orbit
|
361 |
+
base_len = len(base)
|
362 |
+
degree = strong_gens[0].size
|
363 |
+
if strong_gens_distr is None:
|
364 |
+
strong_gens_distr = _distribute_gens_by_base(base, strong_gens)
|
365 |
+
if basic_orbits is None:
|
366 |
+
basic_orbits = []
|
367 |
+
for i in range(base_len):
|
368 |
+
basic_orbit = _orbit(degree, strong_gens_distr[i], base[i])
|
369 |
+
basic_orbits.append(basic_orbit)
|
370 |
+
strong_gens_distr.append([])
|
371 |
+
res = strong_gens[:]
|
372 |
+
for i in range(base_len - 1, -1, -1):
|
373 |
+
gens_copy = strong_gens_distr[i][:]
|
374 |
+
for gen in strong_gens_distr[i]:
|
375 |
+
if gen not in strong_gens_distr[i + 1]:
|
376 |
+
temp_gens = gens_copy[:]
|
377 |
+
temp_gens.remove(gen)
|
378 |
+
if temp_gens == []:
|
379 |
+
continue
|
380 |
+
temp_orbit = _orbit(degree, temp_gens, base[i])
|
381 |
+
if temp_orbit == basic_orbits[i]:
|
382 |
+
gens_copy.remove(gen)
|
383 |
+
res.remove(gen)
|
384 |
+
return res
|
385 |
+
|
386 |
+
|
387 |
+
def _strip(g, base, orbits, transversals):
|
388 |
+
"""
|
389 |
+
Attempt to decompose a permutation using a (possibly partial) BSGS
|
390 |
+
structure.
|
391 |
+
|
392 |
+
Explanation
|
393 |
+
===========
|
394 |
+
|
395 |
+
This is done by treating the sequence ``base`` as an actual base, and
|
396 |
+
the orbits ``orbits`` and transversals ``transversals`` as basic orbits and
|
397 |
+
transversals relative to it.
|
398 |
+
|
399 |
+
This process is called "sifting". A sift is unsuccessful when a certain
|
400 |
+
orbit element is not found or when after the sift the decomposition
|
401 |
+
does not end with the identity element.
|
402 |
+
|
403 |
+
The argument ``transversals`` is a list of dictionaries that provides
|
404 |
+
transversal elements for the orbits ``orbits``.
|
405 |
+
|
406 |
+
Parameters
|
407 |
+
==========
|
408 |
+
|
409 |
+
``g`` - permutation to be decomposed
|
410 |
+
``base`` - sequence of points
|
411 |
+
``orbits`` - a list in which the ``i``-th entry is an orbit of ``base[i]``
|
412 |
+
under some subgroup of the pointwise stabilizer of `
|
413 |
+
`base[0], base[1], ..., base[i - 1]``. The groups themselves are implicit
|
414 |
+
in this function since the only information we need is encoded in the orbits
|
415 |
+
and transversals
|
416 |
+
``transversals`` - a list of orbit transversals associated with the orbits
|
417 |
+
``orbits``.
|
418 |
+
|
419 |
+
Examples
|
420 |
+
========
|
421 |
+
|
422 |
+
>>> from sympy.combinatorics import Permutation, SymmetricGroup
|
423 |
+
>>> from sympy.combinatorics.util import _strip
|
424 |
+
>>> S = SymmetricGroup(5)
|
425 |
+
>>> S.schreier_sims()
|
426 |
+
>>> g = Permutation([0, 2, 3, 1, 4])
|
427 |
+
>>> _strip(g, S.base, S.basic_orbits, S.basic_transversals)
|
428 |
+
((4), 5)
|
429 |
+
|
430 |
+
Notes
|
431 |
+
=====
|
432 |
+
|
433 |
+
The algorithm is described in [1],pp.89-90. The reason for returning
|
434 |
+
both the current state of the element being decomposed and the level
|
435 |
+
at which the sifting ends is that they provide important information for
|
436 |
+
the randomized version of the Schreier-Sims algorithm.
|
437 |
+
|
438 |
+
References
|
439 |
+
==========
|
440 |
+
|
441 |
+
.. [1] Holt, D., Eick, B., O'Brien, E."Handbook of computational group theory"
|
442 |
+
|
443 |
+
See Also
|
444 |
+
========
|
445 |
+
|
446 |
+
sympy.combinatorics.perm_groups.PermutationGroup.schreier_sims
|
447 |
+
sympy.combinatorics.perm_groups.PermutationGroup.schreier_sims_random
|
448 |
+
|
449 |
+
"""
|
450 |
+
h = g._array_form
|
451 |
+
base_len = len(base)
|
452 |
+
for i in range(base_len):
|
453 |
+
beta = h[base[i]]
|
454 |
+
if beta == base[i]:
|
455 |
+
continue
|
456 |
+
if beta not in orbits[i]:
|
457 |
+
return _af_new(h), i + 1
|
458 |
+
u = transversals[i][beta]._array_form
|
459 |
+
h = _af_rmul(_af_invert(u), h)
|
460 |
+
return _af_new(h), base_len + 1
|
461 |
+
|
462 |
+
|
463 |
+
def _strip_af(h, base, orbits, transversals, j, slp=[], slps={}):
|
464 |
+
"""
|
465 |
+
optimized _strip, with h, transversals and result in array form
|
466 |
+
if the stripped elements is the identity, it returns False, base_len + 1
|
467 |
+
|
468 |
+
j h[base[i]] == base[i] for i <= j
|
469 |
+
|
470 |
+
"""
|
471 |
+
base_len = len(base)
|
472 |
+
for i in range(j+1, base_len):
|
473 |
+
beta = h[base[i]]
|
474 |
+
if beta == base[i]:
|
475 |
+
continue
|
476 |
+
if beta not in orbits[i]:
|
477 |
+
if not slp:
|
478 |
+
return h, i + 1
|
479 |
+
return h, i + 1, slp
|
480 |
+
u = transversals[i][beta]
|
481 |
+
if h == u:
|
482 |
+
if not slp:
|
483 |
+
return False, base_len + 1
|
484 |
+
return False, base_len + 1, slp
|
485 |
+
h = _af_rmul(_af_invert(u), h)
|
486 |
+
if slp:
|
487 |
+
u_slp = slps[i][beta][:]
|
488 |
+
u_slp.reverse()
|
489 |
+
u_slp = [(i, (g,)) for g in u_slp]
|
490 |
+
slp = u_slp + slp
|
491 |
+
if not slp:
|
492 |
+
return h, base_len + 1
|
493 |
+
return h, base_len + 1, slp
|
494 |
+
|
495 |
+
|
496 |
+
def _strong_gens_from_distr(strong_gens_distr):
|
497 |
+
"""
|
498 |
+
Retrieve strong generating set from generators of basic stabilizers.
|
499 |
+
|
500 |
+
This is just the union of the generators of the first and second basic
|
501 |
+
stabilizers.
|
502 |
+
|
503 |
+
Parameters
|
504 |
+
==========
|
505 |
+
|
506 |
+
``strong_gens_distr`` - strong generators distributed by membership in basic
|
507 |
+
stabilizers
|
508 |
+
|
509 |
+
Examples
|
510 |
+
========
|
511 |
+
|
512 |
+
>>> from sympy.combinatorics import SymmetricGroup
|
513 |
+
>>> from sympy.combinatorics.util import (_strong_gens_from_distr,
|
514 |
+
... _distribute_gens_by_base)
|
515 |
+
>>> S = SymmetricGroup(3)
|
516 |
+
>>> S.schreier_sims()
|
517 |
+
>>> S.strong_gens
|
518 |
+
[(0 1 2), (2)(0 1), (1 2)]
|
519 |
+
>>> strong_gens_distr = _distribute_gens_by_base(S.base, S.strong_gens)
|
520 |
+
>>> _strong_gens_from_distr(strong_gens_distr)
|
521 |
+
[(0 1 2), (2)(0 1), (1 2)]
|
522 |
+
|
523 |
+
See Also
|
524 |
+
========
|
525 |
+
|
526 |
+
_distribute_gens_by_base
|
527 |
+
|
528 |
+
"""
|
529 |
+
if len(strong_gens_distr) == 1:
|
530 |
+
return strong_gens_distr[0][:]
|
531 |
+
else:
|
532 |
+
result = strong_gens_distr[0]
|
533 |
+
for gen in strong_gens_distr[1]:
|
534 |
+
if gen not in result:
|
535 |
+
result.append(gen)
|
536 |
+
return result
|
venv/lib/python3.10/site-packages/sympy/geometry/__init__.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
A geometry module for the SymPy library. This module contains all of the
|
3 |
+
entities and functions needed to construct basic geometrical data and to
|
4 |
+
perform simple informational queries.
|
5 |
+
|
6 |
+
Usage:
|
7 |
+
======
|
8 |
+
|
9 |
+
Examples
|
10 |
+
========
|
11 |
+
|
12 |
+
"""
|
13 |
+
from sympy.geometry.point import Point, Point2D, Point3D
|
14 |
+
from sympy.geometry.line import Line, Ray, Segment, Line2D, Segment2D, Ray2D, \
|
15 |
+
Line3D, Segment3D, Ray3D
|
16 |
+
from sympy.geometry.plane import Plane
|
17 |
+
from sympy.geometry.ellipse import Ellipse, Circle
|
18 |
+
from sympy.geometry.polygon import Polygon, RegularPolygon, Triangle, rad, deg
|
19 |
+
from sympy.geometry.util import are_similar, centroid, convex_hull, idiff, \
|
20 |
+
intersection, closest_points, farthest_points
|
21 |
+
from sympy.geometry.exceptions import GeometryError
|
22 |
+
from sympy.geometry.curve import Curve
|
23 |
+
from sympy.geometry.parabola import Parabola
|
24 |
+
|
25 |
+
__all__ = [
|
26 |
+
'Point', 'Point2D', 'Point3D',
|
27 |
+
|
28 |
+
'Line', 'Ray', 'Segment', 'Line2D', 'Segment2D', 'Ray2D', 'Line3D',
|
29 |
+
'Segment3D', 'Ray3D',
|
30 |
+
|
31 |
+
'Plane',
|
32 |
+
|
33 |
+
'Ellipse', 'Circle',
|
34 |
+
|
35 |
+
'Polygon', 'RegularPolygon', 'Triangle', 'rad', 'deg',
|
36 |
+
|
37 |
+
'are_similar', 'centroid', 'convex_hull', 'idiff', 'intersection',
|
38 |
+
'closest_points', 'farthest_points',
|
39 |
+
|
40 |
+
'GeometryError',
|
41 |
+
|
42 |
+
'Curve',
|
43 |
+
|
44 |
+
'Parabola',
|
45 |
+
]
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.46 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/curve.cpython-310.pyc
ADDED
Binary file (11.8 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/ellipse.cpython-310.pyc
ADDED
Binary file (48.4 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/entity.cpython-310.pyc
ADDED
Binary file (21.2 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/exceptions.cpython-310.pyc
ADDED
Binary file (455 Bytes). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/line.cpython-310.pyc
ADDED
Binary file (70.2 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/parabola.cpython-310.pyc
ADDED
Binary file (11.1 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/plane.cpython-310.pyc
ADDED
Binary file (25.8 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/point.cpython-310.pyc
ADDED
Binary file (39.3 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/polygon.cpython-310.pyc
ADDED
Binary file (78 kB). View file
|
|
venv/lib/python3.10/site-packages/sympy/geometry/__pycache__/util.cpython-310.pyc
ADDED
Binary file (20.2 kB). View file
|
|