1from sympy import symbols, FiniteSet
2from sympy.combinatorics.polyhedron import (Polyhedron,
3    tetrahedron, cube as square, octahedron, dodecahedron, icosahedron,
4    cube_faces)
5from sympy.combinatorics.permutations import Permutation
6from sympy.combinatorics.perm_groups import PermutationGroup
7from sympy.testing.pytest import raises
8
9rmul = Permutation.rmul
10
11
12def test_polyhedron():
13    raises(ValueError, lambda: Polyhedron(list('ab'),
14        pgroup=[Permutation([0])]))
15    pgroup = [Permutation([[0, 7, 2, 5], [6, 1, 4, 3]]),
16              Permutation([[0, 7, 1, 6], [5, 2, 4, 3]]),
17              Permutation([[3, 6, 0, 5], [4, 1, 7, 2]]),
18              Permutation([[7, 4, 5], [1, 3, 0], [2], [6]]),
19              Permutation([[1, 3, 2], [7, 6, 5], [4], [0]]),
20              Permutation([[4, 7, 6], [2, 0, 3], [1], [5]]),
21              Permutation([[1, 2, 0], [4, 5, 6], [3], [7]]),
22              Permutation([[4, 2], [0, 6], [3, 7], [1, 5]]),
23              Permutation([[3, 5], [7, 1], [2, 6], [0, 4]]),
24              Permutation([[2, 5], [1, 6], [0, 4], [3, 7]]),
25              Permutation([[4, 3], [7, 0], [5, 1], [6, 2]]),
26              Permutation([[4, 1], [0, 5], [6, 2], [7, 3]]),
27              Permutation([[7, 2], [3, 6], [0, 4], [1, 5]]),
28              Permutation([0, 1, 2, 3, 4, 5, 6, 7])]
29    corners = tuple(symbols('A:H'))
30    faces = cube_faces
31    cube = Polyhedron(corners, faces, pgroup)
32
33    assert cube.edges == FiniteSet(*(
34        (0, 1), (6, 7), (1, 2), (5, 6), (0, 3), (2, 3),
35        (4, 7), (4, 5), (3, 7), (1, 5), (0, 4), (2, 6)))
36
37    for i in range(3):  # add 180 degree face rotations
38        cube.rotate(cube.pgroup[i]**2)
39
40    assert cube.corners == corners
41
42    for i in range(3, 7):  # add 240 degree axial corner rotations
43        cube.rotate(cube.pgroup[i]**2)
44
45    assert cube.corners == corners
46    cube.rotate(1)
47    raises(ValueError, lambda: cube.rotate(Permutation([0, 1])))
48    assert cube.corners != corners
49    assert cube.array_form == [7, 6, 4, 5, 3, 2, 0, 1]
50    assert cube.cyclic_form == [[0, 7, 1, 6], [2, 4, 3, 5]]
51    cube.reset()
52    assert cube.corners == corners
53
54    def check(h, size, rpt, target):
55
56        assert len(h.faces) + len(h.vertices) - len(h.edges) == 2
57        assert h.size == size
58
59        got = set()
60        for p in h.pgroup:
61            # make sure it restores original
62            P = h.copy()
63            hit = P.corners
64            for i in range(rpt):
65                P.rotate(p)
66                if P.corners == hit:
67                    break
68            else:
69                print('error in permutation', p.array_form)
70            for i in range(rpt):
71                P.rotate(p)
72                got.add(tuple(P.corners))
73                c = P.corners
74                f = [[c[i] for i in f] for f in P.faces]
75                assert h.faces == Polyhedron(c, f).faces
76        assert len(got) == target
77        assert PermutationGroup([Permutation(g) for g in got]).is_group
78
79    for h, size, rpt, target in zip(
80        (tetrahedron, square, octahedron, dodecahedron, icosahedron),
81        (4, 8, 6, 20, 12),
82        (3, 4, 4, 5, 5),
83            (12, 24, 24, 60, 60)):
84        check(h, size, rpt, target)
85
86
87def test_pgroups():
88    from sympy.combinatorics.polyhedron import (tetrahedron, cube, octahedron,
89            dodecahedron, icosahedron, tetrahedron_faces, cube_faces,
90            octahedron_faces, dodecahedron_faces, icosahedron_faces)
91    from sympy.combinatorics.polyhedron import _pgroup_calcs
92    (tetrahedron2, cube2, octahedron2, dodecahedron2, icosahedron2,
93     tetrahedron_faces2, cube_faces2, octahedron_faces2,
94     dodecahedron_faces2, icosahedron_faces2) = _pgroup_calcs()
95
96    assert tetrahedron == tetrahedron2
97    assert cube == cube2
98    assert octahedron == octahedron2
99    assert dodecahedron == dodecahedron2
100    assert icosahedron == icosahedron2
101    assert sorted(map(sorted, tetrahedron_faces)) == sorted(map(sorted, tetrahedron_faces2))
102    assert sorted(cube_faces) == sorted(cube_faces2)
103    assert sorted(octahedron_faces) == sorted(octahedron_faces2)
104    assert sorted(dodecahedron_faces) == sorted(dodecahedron_faces2)
105    assert sorted(icosahedron_faces) == sorted(icosahedron_faces2)
106