test_homomorphisms.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from sympy.combinatorics import Permutation
  2. from sympy.combinatorics.perm_groups import PermutationGroup
  3. from sympy.combinatorics.homomorphisms import homomorphism, group_isomorphism, is_isomorphic
  4. from sympy.combinatorics.free_groups import free_group
  5. from sympy.combinatorics.fp_groups import FpGroup
  6. from sympy.combinatorics.named_groups import AlternatingGroup, DihedralGroup, CyclicGroup
  7. from sympy.testing.pytest import raises
  8. def test_homomorphism():
  9. # FpGroup -> PermutationGroup
  10. F, a, b = free_group("a, b")
  11. G = FpGroup(F, [a**3, b**3, (a*b)**2])
  12. c = Permutation(3)(0, 1, 2)
  13. d = Permutation(3)(1, 2, 3)
  14. A = AlternatingGroup(4)
  15. T = homomorphism(G, A, [a, b], [c, d])
  16. assert T(a*b**2*a**-1) == c*d**2*c**-1
  17. assert T.is_isomorphism()
  18. assert T(T.invert(Permutation(3)(0, 2, 3))) == Permutation(3)(0, 2, 3)
  19. T = homomorphism(G, AlternatingGroup(4), G.generators)
  20. assert T.is_trivial()
  21. assert T.kernel().order() == G.order()
  22. E, e = free_group("e")
  23. G = FpGroup(E, [e**8])
  24. P = PermutationGroup([Permutation(0, 1, 2, 3), Permutation(0, 2)])
  25. T = homomorphism(G, P, [e], [Permutation(0, 1, 2, 3)])
  26. assert T.image().order() == 4
  27. assert T(T.invert(Permutation(0, 2)(1, 3))) == Permutation(0, 2)(1, 3)
  28. T = homomorphism(E, AlternatingGroup(4), E.generators, [c])
  29. assert T.invert(c**2) == e**-1 #order(c) == 3 so c**2 == c**-1
  30. # FreeGroup -> FreeGroup
  31. T = homomorphism(F, E, [a], [e])
  32. assert T(a**-2*b**4*a**2).is_identity
  33. # FreeGroup -> FpGroup
  34. G = FpGroup(F, [a*b*a**-1*b**-1])
  35. T = homomorphism(F, G, F.generators, G.generators)
  36. assert T.invert(a**-1*b**-1*a**2) == a*b**-1
  37. # PermutationGroup -> PermutationGroup
  38. D = DihedralGroup(8)
  39. p = Permutation(0, 1, 2, 3, 4, 5, 6, 7)
  40. P = PermutationGroup(p)
  41. T = homomorphism(P, D, [p], [p])
  42. assert T.is_injective()
  43. assert not T.is_isomorphism()
  44. assert T.invert(p**3) == p**3
  45. T2 = homomorphism(F, P, [F.generators[0]], P.generators)
  46. T = T.compose(T2)
  47. assert T.domain == F
  48. assert T.codomain == D
  49. assert T(a*b) == p
  50. D3 = DihedralGroup(3)
  51. T = homomorphism(D3, D3, D3.generators, D3.generators)
  52. assert T.is_isomorphism()
  53. def test_isomorphisms():
  54. F, a, b = free_group("a, b")
  55. E, c, d = free_group("c, d")
  56. # Infinite groups with differently ordered relators.
  57. G = FpGroup(F, [a**2, b**3])
  58. H = FpGroup(F, [b**3, a**2])
  59. assert is_isomorphic(G, H)
  60. # Trivial Case
  61. # FpGroup -> FpGroup
  62. H = FpGroup(F, [a**3, b**3, (a*b)**2])
  63. F, c, d = free_group("c, d")
  64. G = FpGroup(F, [c**3, d**3, (c*d)**2])
  65. check, T = group_isomorphism(G, H)
  66. assert check
  67. assert T(c**3*d**2) == a**3*b**2
  68. # FpGroup -> PermutationGroup
  69. # FpGroup is converted to the equivalent isomorphic group.
  70. F, a, b = free_group("a, b")
  71. G = FpGroup(F, [a**3, b**3, (a*b)**2])
  72. H = AlternatingGroup(4)
  73. check, T = group_isomorphism(G, H)
  74. assert check
  75. assert T(b*a*b**-1*a**-1*b**-1) == Permutation(0, 2, 3)
  76. assert T(b*a*b*a**-1*b**-1) == Permutation(0, 3, 2)
  77. # PermutationGroup -> PermutationGroup
  78. D = DihedralGroup(8)
  79. p = Permutation(0, 1, 2, 3, 4, 5, 6, 7)
  80. P = PermutationGroup(p)
  81. assert not is_isomorphic(D, P)
  82. A = CyclicGroup(5)
  83. B = CyclicGroup(7)
  84. assert not is_isomorphic(A, B)
  85. # Two groups of the same prime order are isomorphic to each other.
  86. G = FpGroup(F, [a, b**5])
  87. H = CyclicGroup(5)
  88. assert G.order() == H.order()
  89. assert is_isomorphic(G, H)
  90. def test_check_homomorphism():
  91. a = Permutation(1,2,3,4)
  92. b = Permutation(1,3)
  93. G = PermutationGroup([a, b])
  94. raises(ValueError, lambda: homomorphism(G, G, [a], [a]))