m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
7.7 KiB

6 months ago
  1. from sympy.combinatorics.group_constructs import DirectProduct
  2. from sympy.combinatorics.perm_groups import PermutationGroup
  3. from sympy.combinatorics.permutations import Permutation
  4. _af_new = Permutation._af_new
  5. def AbelianGroup(*cyclic_orders):
  6. """
  7. Returns the direct product of cyclic groups with the given orders.
  8. Explanation
  9. ===========
  10. According to the structure theorem for finite abelian groups ([1]),
  11. every finite abelian group can be written as the direct product of
  12. finitely many cyclic groups.
  13. Examples
  14. ========
  15. >>> from sympy.combinatorics.named_groups import AbelianGroup
  16. >>> AbelianGroup(3, 4)
  17. PermutationGroup([
  18. (6)(0 1 2),
  19. (3 4 5 6)])
  20. >>> _.is_group
  21. True
  22. See Also
  23. ========
  24. DirectProduct
  25. References
  26. ==========
  27. .. [1] http://groupprops.subwiki.org/wiki/Structure_theorem_for_finitely_generated_abelian_groups
  28. """
  29. groups = []
  30. degree = 0
  31. order = 1
  32. for size in cyclic_orders:
  33. degree += size
  34. order *= size
  35. groups.append(CyclicGroup(size))
  36. G = DirectProduct(*groups)
  37. G._is_abelian = True
  38. G._degree = degree
  39. G._order = order
  40. return G
  41. def AlternatingGroup(n):
  42. """
  43. Generates the alternating group on ``n`` elements as a permutation group.
  44. Explanation
  45. ===========
  46. For ``n > 2``, the generators taken are ``(0 1 2), (0 1 2 ... n-1)`` for
  47. ``n`` odd
  48. and ``(0 1 2), (1 2 ... n-1)`` for ``n`` even (See [1], p.31, ex.6.9.).
  49. After the group is generated, some of its basic properties are set.
  50. The cases ``n = 1, 2`` are handled separately.
  51. Examples
  52. ========
  53. >>> from sympy.combinatorics.named_groups import AlternatingGroup
  54. >>> G = AlternatingGroup(4)
  55. >>> G.is_group
  56. True
  57. >>> a = list(G.generate_dimino())
  58. >>> len(a)
  59. 12
  60. >>> all(perm.is_even for perm in a)
  61. True
  62. See Also
  63. ========
  64. SymmetricGroup, CyclicGroup, DihedralGroup
  65. References
  66. ==========
  67. .. [1] Armstrong, M. "Groups and Symmetry"
  68. """
  69. # small cases are special
  70. if n in (1, 2):
  71. return PermutationGroup([Permutation([0])])
  72. a = list(range(n))
  73. a[0], a[1], a[2] = a[1], a[2], a[0]
  74. gen1 = a
  75. if n % 2:
  76. a = list(range(1, n))
  77. a.append(0)
  78. gen2 = a
  79. else:
  80. a = list(range(2, n))
  81. a.append(1)
  82. a.insert(0, 0)
  83. gen2 = a
  84. gens = [gen1, gen2]
  85. if gen1 == gen2:
  86. gens = gens[:1]
  87. G = PermutationGroup([_af_new(a) for a in gens], dups=False)
  88. if n < 4:
  89. G._is_abelian = True
  90. G._is_nilpotent = True
  91. else:
  92. G._is_abelian = False
  93. G._is_nilpotent = False
  94. if n < 5:
  95. G._is_solvable = True
  96. else:
  97. G._is_solvable = False
  98. G._degree = n
  99. G._is_transitive = True
  100. G._is_alt = True
  101. return G
  102. def CyclicGroup(n):
  103. """
  104. Generates the cyclic group of order ``n`` as a permutation group.
  105. Explanation
  106. ===========
  107. The generator taken is the ``n``-cycle ``(0 1 2 ... n-1)``
  108. (in cycle notation). After the group is generated, some of its basic
  109. properties are set.
  110. Examples
  111. ========
  112. >>> from sympy.combinatorics.named_groups import CyclicGroup
  113. >>> G = CyclicGroup(6)
  114. >>> G.is_group
  115. True
  116. >>> G.order()
  117. 6
  118. >>> list(G.generate_schreier_sims(af=True))
  119. [[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 0], [2, 3, 4, 5, 0, 1],
  120. [3, 4, 5, 0, 1, 2], [4, 5, 0, 1, 2, 3], [5, 0, 1, 2, 3, 4]]
  121. See Also
  122. ========
  123. SymmetricGroup, DihedralGroup, AlternatingGroup
  124. """
  125. a = list(range(1, n))
  126. a.append(0)
  127. gen = _af_new(a)
  128. G = PermutationGroup([gen])
  129. G._is_abelian = True
  130. G._is_nilpotent = True
  131. G._is_solvable = True
  132. G._degree = n
  133. G._is_transitive = True
  134. G._order = n
  135. return G
  136. def DihedralGroup(n):
  137. r"""
  138. Generates the dihedral group `D_n` as a permutation group.
  139. Explanation
  140. ===========
  141. The dihedral group `D_n` is the group of symmetries of the regular
  142. ``n``-gon. The generators taken are the ``n``-cycle ``a = (0 1 2 ... n-1)``
  143. (a rotation of the ``n``-gon) and ``b = (0 n-1)(1 n-2)...``
  144. (a reflection of the ``n``-gon) in cycle rotation. It is easy to see that
  145. these satisfy ``a**n = b**2 = 1`` and ``bab = ~a`` so they indeed generate
  146. `D_n` (See [1]). After the group is generated, some of its basic properties
  147. are set.
  148. Examples
  149. ========
  150. >>> from sympy.combinatorics.named_groups import DihedralGroup
  151. >>> G = DihedralGroup(5)
  152. >>> G.is_group
  153. True
  154. >>> a = list(G.generate_dimino())
  155. >>> [perm.cyclic_form for perm in a]
  156. [[], [[0, 1, 2, 3, 4]], [[0, 2, 4, 1, 3]],
  157. [[0, 3, 1, 4, 2]], [[0, 4, 3, 2, 1]], [[0, 4], [1, 3]],
  158. [[1, 4], [2, 3]], [[0, 1], [2, 4]], [[0, 2], [3, 4]],
  159. [[0, 3], [1, 2]]]
  160. See Also
  161. ========
  162. SymmetricGroup, CyclicGroup, AlternatingGroup
  163. References
  164. ==========
  165. .. [1] https://en.wikipedia.org/wiki/Dihedral_group
  166. """
  167. # small cases are special
  168. if n == 1:
  169. return PermutationGroup([Permutation([1, 0])])
  170. if n == 2:
  171. return PermutationGroup([Permutation([1, 0, 3, 2]),
  172. Permutation([2, 3, 0, 1]), Permutation([3, 2, 1, 0])])
  173. a = list(range(1, n))
  174. a.append(0)
  175. gen1 = _af_new(a)
  176. a = list(range(n))
  177. a.reverse()
  178. gen2 = _af_new(a)
  179. G = PermutationGroup([gen1, gen2])
  180. # if n is a power of 2, group is nilpotent
  181. if n & (n-1) == 0:
  182. G._is_nilpotent = True
  183. else:
  184. G._is_nilpotent = False
  185. G._is_abelian = False
  186. G._is_solvable = True
  187. G._degree = n
  188. G._is_transitive = True
  189. G._order = 2*n
  190. return G
  191. def SymmetricGroup(n):
  192. """
  193. Generates the symmetric group on ``n`` elements as a permutation group.
  194. Explanation
  195. ===========
  196. The generators taken are the ``n``-cycle
  197. ``(0 1 2 ... n-1)`` and the transposition ``(0 1)`` (in cycle notation).
  198. (See [1]). After the group is generated, some of its basic properties
  199. are set.
  200. Examples
  201. ========
  202. >>> from sympy.combinatorics.named_groups import SymmetricGroup
  203. >>> G = SymmetricGroup(4)
  204. >>> G.is_group
  205. True
  206. >>> G.order()
  207. 24
  208. >>> list(G.generate_schreier_sims(af=True))
  209. [[0, 1, 2, 3], [1, 2, 3, 0], [2, 3, 0, 1], [3, 1, 2, 0], [0, 2, 3, 1],
  210. [1, 3, 0, 2], [2, 0, 1, 3], [3, 2, 0, 1], [0, 3, 1, 2], [1, 0, 2, 3],
  211. [2, 1, 3, 0], [3, 0, 1, 2], [0, 1, 3, 2], [1, 2, 0, 3], [2, 3, 1, 0],
  212. [3, 1, 0, 2], [0, 2, 1, 3], [1, 3, 2, 0], [2, 0, 3, 1], [3, 2, 1, 0],
  213. [0, 3, 2, 1], [1, 0, 3, 2], [2, 1, 0, 3], [3, 0, 2, 1]]
  214. See Also
  215. ========
  216. CyclicGroup, DihedralGroup, AlternatingGroup
  217. References
  218. ==========
  219. .. [1] https://en.wikipedia.org/wiki/Symmetric_group#Generators_and_relations
  220. """
  221. if n == 1:
  222. G = PermutationGroup([Permutation([0])])
  223. elif n == 2:
  224. G = PermutationGroup([Permutation([1, 0])])
  225. else:
  226. a = list(range(1, n))
  227. a.append(0)
  228. gen1 = _af_new(a)
  229. a = list(range(n))
  230. a[0], a[1] = a[1], a[0]
  231. gen2 = _af_new(a)
  232. G = PermutationGroup([gen1, gen2])
  233. if n < 3:
  234. G._is_abelian = True
  235. G._is_nilpotent = True
  236. else:
  237. G._is_abelian = False
  238. G._is_nilpotent = False
  239. if n < 5:
  240. G._is_solvable = True
  241. else:
  242. G._is_solvable = False
  243. G._degree = n
  244. G._is_transitive = True
  245. G._is_sym = True
  246. return G
  247. def RubikGroup(n):
  248. """Return a group of Rubik's cube generators
  249. >>> from sympy.combinatorics.named_groups import RubikGroup
  250. >>> RubikGroup(2).is_group
  251. True
  252. """
  253. from sympy.combinatorics.generators import rubik
  254. if n <= 1:
  255. raise ValueError("Invalid cube. n has to be greater than 1")
  256. return PermutationGroup(rubik(n))