test_free_groups.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from sympy.combinatorics.free_groups import free_group, FreeGroup
  2. from sympy.core import Symbol
  3. from sympy.testing.pytest import raises
  4. from sympy.core.numbers import oo
  5. F, x, y, z = free_group("x, y, z")
  6. def test_FreeGroup__init__():
  7. x, y, z = map(Symbol, "xyz")
  8. assert len(FreeGroup("x, y, z").generators) == 3
  9. assert len(FreeGroup(x).generators) == 1
  10. assert len(FreeGroup(("x", "y", "z"))) == 3
  11. assert len(FreeGroup((x, y, z)).generators) == 3
  12. def test_free_group():
  13. G, a, b, c = free_group("a, b, c")
  14. assert F.generators == (x, y, z)
  15. assert x*z**2 in F
  16. assert x in F
  17. assert y*z**-1 in F
  18. assert (y*z)**0 in F
  19. assert a not in F
  20. assert a**0 not in F
  21. assert len(F) == 3
  22. assert str(F) == '<free group on the generators (x, y, z)>'
  23. assert not F == G
  24. assert F.order() is oo
  25. assert F.is_abelian == False
  26. assert F.center() == {F.identity}
  27. (e,) = free_group("")
  28. assert e.order() == 1
  29. assert e.generators == ()
  30. assert e.elements == {e.identity}
  31. assert e.is_abelian == True
  32. def test_FreeGroup__hash__():
  33. assert hash(F)
  34. def test_FreeGroup__eq__():
  35. assert free_group("x, y, z")[0] == free_group("x, y, z")[0]
  36. assert free_group("x, y, z")[0] is free_group("x, y, z")[0]
  37. assert free_group("x, y, z")[0] != free_group("a, x, y")[0]
  38. assert free_group("x, y, z")[0] is not free_group("a, x, y")[0]
  39. assert free_group("x, y")[0] != free_group("x, y, z")[0]
  40. assert free_group("x, y")[0] is not free_group("x, y, z")[0]
  41. assert free_group("x, y, z")[0] != free_group("x, y")[0]
  42. assert free_group("x, y, z")[0] is not free_group("x, y")[0]
  43. def test_FreeGroup__getitem__():
  44. assert F[0:] == FreeGroup("x, y, z")
  45. assert F[1:] == FreeGroup("y, z")
  46. assert F[2:] == FreeGroup("z")
  47. def test_FreeGroupElm__hash__():
  48. assert hash(x*y*z)
  49. def test_FreeGroupElm_copy():
  50. f = x*y*z**3
  51. g = f.copy()
  52. h = x*y*z**7
  53. assert f == g
  54. assert f != h
  55. def test_FreeGroupElm_inverse():
  56. assert x.inverse() == x**-1
  57. assert (x*y).inverse() == y**-1*x**-1
  58. assert (y*x*y**-1).inverse() == y*x**-1*y**-1
  59. assert (y**2*x**-1).inverse() == x*y**-2
  60. def test_FreeGroupElm_type_error():
  61. raises(TypeError, lambda: 2/x)
  62. raises(TypeError, lambda: x**2 + y**2)
  63. raises(TypeError, lambda: x/2)
  64. def test_FreeGroupElm_methods():
  65. assert (x**0).order() == 1
  66. assert (y**2).order() is oo
  67. assert (x**-1*y).commutator(x) == y**-1*x**-1*y*x
  68. assert len(x**2*y**-1) == 3
  69. assert len(x**-1*y**3*z) == 5
  70. def test_FreeGroupElm_eliminate_word():
  71. w = x**5*y*x**2*y**-4*x
  72. assert w.eliminate_word( x, x**2 ) == x**10*y*x**4*y**-4*x**2
  73. w3 = x**2*y**3*x**-1*y
  74. assert w3.eliminate_word(x, x**2) == x**4*y**3*x**-2*y
  75. assert w3.eliminate_word(x, y) == y**5
  76. assert w3.eliminate_word(x, y**4) == y**8
  77. assert w3.eliminate_word(y, x**-1) == x**-3
  78. assert w3.eliminate_word(x, y*z) == y*z*y*z*y**3*z**-1
  79. assert (y**-3).eliminate_word(y, x**-1*z**-1) == z*x*z*x*z*x
  80. #assert w3.eliminate_word(x, y*x) == y*x*y*x**2*y*x*y*x*y*x*z**3
  81. #assert w3.eliminate_word(x, x*y) == x*y*x**2*y*x*y*x*y*x*y*z**3
  82. def test_FreeGroupElm_array_form():
  83. assert (x*z).array_form == ((Symbol('x'), 1), (Symbol('z'), 1))
  84. assert (x**2*z*y*x**-2).array_form == \
  85. ((Symbol('x'), 2), (Symbol('z'), 1), (Symbol('y'), 1), (Symbol('x'), -2))
  86. assert (x**-2*y**-1).array_form == ((Symbol('x'), -2), (Symbol('y'), -1))
  87. def test_FreeGroupElm_letter_form():
  88. assert (x**3).letter_form == (Symbol('x'), Symbol('x'), Symbol('x'))
  89. assert (x**2*z**-2*x).letter_form == \
  90. (Symbol('x'), Symbol('x'), -Symbol('z'), -Symbol('z'), Symbol('x'))
  91. def test_FreeGroupElm_ext_rep():
  92. assert (x**2*z**-2*x).ext_rep == \
  93. (Symbol('x'), 2, Symbol('z'), -2, Symbol('x'), 1)
  94. assert (x**-2*y**-1).ext_rep == (Symbol('x'), -2, Symbol('y'), -1)
  95. assert (x*z).ext_rep == (Symbol('x'), 1, Symbol('z'), 1)
  96. def test_FreeGroupElm__mul__pow__():
  97. x1 = x.group.dtype(((Symbol('x'), 1),))
  98. assert x**2 == x1*x
  99. assert (x**2*y*x**-2)**4 == x**2*y**4*x**-2
  100. assert (x**2)**2 == x**4
  101. assert (x**-1)**-1 == x
  102. assert (x**-1)**0 == F.identity
  103. assert (y**2)**-2 == y**-4
  104. assert x**2*x**-1 == x
  105. assert x**2*y**2*y**-1 == x**2*y
  106. assert x*x**-1 == F.identity
  107. assert x/x == F.identity
  108. assert x/x**2 == x**-1
  109. assert (x**2*y)/(x**2*y**-1) == x**2*y**2*x**-2
  110. assert (x**2*y)/(y**-1*x**2) == x**2*y*x**-2*y
  111. assert x*(x**-1*y*z*y**-1) == y*z*y**-1
  112. assert x**2*(x**-2*y**-1*z**2*y) == y**-1*z**2*y
  113. def test_FreeGroupElm__len__():
  114. assert len(x**5*y*x**2*y**-4*x) == 13
  115. assert len(x**17) == 17
  116. assert len(y**0) == 0
  117. def test_FreeGroupElm_comparison():
  118. assert not (x*y == y*x)
  119. assert x**0 == y**0
  120. assert x**2 < y**3
  121. assert not x**3 < y**2
  122. assert x*y < x**2*y
  123. assert x**2*y**2 < y**4
  124. assert not y**4 < y**-4
  125. assert not y**4 < x**-4
  126. assert y**-2 < y**2
  127. assert x**2 <= y**2
  128. assert x**2 <= x**2
  129. assert not y*z > z*y
  130. assert x > x**-1
  131. assert not x**2 >= y**2
  132. def test_FreeGroupElm_syllables():
  133. w = x**5*y*x**2*y**-4*x
  134. assert w.number_syllables() == 5
  135. assert w.exponent_syllable(2) == 2
  136. assert w.generator_syllable(3) == Symbol('y')
  137. assert w.sub_syllables(1, 2) == y
  138. assert w.sub_syllables(3, 3) == F.identity
  139. def test_FreeGroup_exponents():
  140. w1 = x**2*y**3
  141. assert w1.exponent_sum(x) == 2
  142. assert w1.exponent_sum(x**-1) == -2
  143. assert w1.generator_count(x) == 2
  144. w2 = x**2*y**4*x**-3
  145. assert w2.exponent_sum(x) == -1
  146. assert w2.generator_count(x) == 5
  147. def test_FreeGroup_generators():
  148. assert (x**2*y**4*z**-1).contains_generators() == {x, y, z}
  149. assert (x**-1*y**3).contains_generators() == {x, y}
  150. def test_FreeGroupElm_words():
  151. w = x**5*y*x**2*y**-4*x
  152. assert w.subword(2, 6) == x**3*y
  153. assert w.subword(3, 2) == F.identity
  154. assert w.subword(6, 10) == x**2*y**-2
  155. assert w.substituted_word(0, 7, y**-1) == y**-1*x*y**-4*x
  156. assert w.substituted_word(0, 7, y**2*x) == y**2*x**2*y**-4*x