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