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