test_polyhedron.py 4.1 KB

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