def test_rotate(): A = [0, 1, 2, 3, 4] assert rotate_left(A, 2) == [2, 3, 4, 0, 1] assert rotate_right(A, 1) == [4, 0, 1, 2, 3] A = [] B = rotate_right(A, 1) assert B == [] B.append(1) assert A == [] B = rotate_left(A, 1) assert B == [] B.append(1) assert A == []
def cyclic(n): """ Generates the cyclic group of order n, Cn. Examples ======== >>> from diofant.combinatorics.permutations import Permutation >>> Permutation.print_cyclic = True >>> from diofant.combinatorics.generators import cyclic >>> list(cyclic(5)) [Permutation(4), Permutation(0, 1, 2, 3, 4), Permutation(0, 2, 4, 1, 3), Permutation(0, 3, 1, 4, 2), Permutation(0, 4, 3, 2, 1)] See Also ======== dihedral """ gen = list(range(n)) for i in range(n): yield Permutation(gen) gen = rotate_left(gen, 1)
def dihedral(n): """ Generates the dihedral group of order 2n, Dn. The result is given as a subgroup of Sn, except for the special cases n=1 (the group S2) and n=2 (the Klein 4-group) where that's not possible and embeddings in S2 and S4 respectively are given. Examples ======== >>> from diofant.combinatorics.permutations import Permutation >>> Permutation.print_cyclic = True >>> from diofant.combinatorics.generators import dihedral >>> list(dihedral(3)) [Permutation(2), Permutation(0, 2), Permutation(0, 1, 2), Permutation(1, 2), Permutation(0, 2, 1), Permutation(2)(0, 1)] See Also ======== cyclic """ if n == 1: yield Permutation([0, 1]) yield Permutation([1, 0]) elif n == 2: yield Permutation([0, 1, 2, 3]) yield Permutation([1, 0, 3, 2]) yield Permutation([2, 3, 0, 1]) yield Permutation([3, 2, 1, 0]) else: gen = list(range(n)) for i in range(n): yield Permutation(gen) yield Permutation(gen[::-1]) gen = rotate_left(gen, 1)