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 == []
Exemple #2
0
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 == []
Exemple #3
0
    def array_form(self):
        """
        This is used to convert from cyclic notation to the
        canonical notation.
        Currently singleton cycles need to be written
        explicitly.

        Examples:
        >>> from sympy.combinatorics.permutations import Permutation
        >>> p = Permutation([[2,0],[3,1]])
        >>> p.array_form
        [1, 3, 0, 2]
        """
        if self._array_form is not None:
            return self._array_form
        if not isinstance(self.args[0][0], list):
            self._array_form = self.args[0]
            return self._array_form
        cycles = self.args[0]
        linear_form = []
        for cycle in cycles:
            min_element = min(cycle)
            while cycle[0] != min_element:
                cycle = rotate_left(cycle, 1)
            linear_form.append(cycle)
        linear_form.sort(key=lambda t: -t[0])
        self._array_form = list(itertools.chain(*linear_form))
        return self._array_form
Exemple #4
0
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 sympy.combinatorics.permutations import Permutation
    >>> from sympy.combinatorics.generators import dihedral
    >>> list(dihedral(3))
    [(2), (0 2), (0 1 2), (1 2), (0 2 1), (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)
Exemple #5
0
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 sympy.combinatorics.permutations import Permutation
    >>> Permutation.print_cyclic = True
    >>> from sympy.combinatorics.generators import dihedral
    >>> list(dihedral(3))
    [(2), (0 2), (0 1 2), (1 2), (0 2 1), (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)
Exemple #6
0
    def array_form(self):
        """
        This is used to convert from cyclic notation to the
        canonical notation.
        Currently singleton cycles need to be written
        explicitly.

        Examples:
        >>> from sympy.combinatorics.permutations import Permutation
        >>> p = Permutation([[2,0],[3,1]])
        >>> p.array_form
        [1, 3, 0, 2]
        """
        if self._array_form is not None:
            return self._array_form
        if not isinstance(self.args[0][0], list):
            self._array_form = self.args[0]
            return self._array_form
        cycles = self.args[0]
        linear_form = []
        for cycle in cycles:
            min_element = min(cycle)
            while cycle[0] != min_element:
                cycle = rotate_left(cycle, 1)
            linear_form.append(cycle)
        linear_form.sort(key=lambda t: -t[0])
        self._array_form = list(itertools.chain(*linear_form))
        return self._array_form
Exemple #7
0
def cyclic(n):
    """
    Generates the cyclic group of order n, Cn.

    Examples:
    >>> from sympy.combinatorics.generators import cyclic
    >>> list(cyclic(5))
    [Permutation([0, 1, 2, 3, 4]), Permutation([1, 2, 3, 4, 0]), \
    Permutation([2, 3, 4, 0, 1]), Permutation([3, 4, 0, 1, 2]), \
    Permutation([4, 0, 1, 2, 3])]
    """
    gen = range(n)
    for i in xrange(n):
        yield Permutation(gen)
        gen = rotate_left(gen, 1)
Exemple #8
0
def cyclic(n):
    """
    Generates the cyclic group of order n, Cn.

    Examples
    ========
    >>> from sympy.combinatorics.generators import cyclic
    >>> list(cyclic(5))
    [Permutation([0, 1, 2, 3, 4]), Permutation([1, 2, 3, 4, 0]), \
    Permutation([2, 3, 4, 0, 1]), Permutation([3, 4, 0, 1, 2]), \
    Permutation([4, 0, 1, 2, 3])]
    """
    gen = range(n)
    for i in xrange(n):
        yield Permutation(gen)
        gen = rotate_left(gen, 1)
Exemple #9
0
def dihedral(n):
    """
    Generates the dihedral group of order 2n, D2n.

    Examples:
    >>> from sympy.combinatorics.generators import dihedral
    >>> list(dihedral(4))
    [Permutation([0, 1, 2, 3]), Permutation([3, 2, 1, 0]), \
    Permutation([1, 2, 3, 0]), Permutation([0, 3, 2, 1]), \
    Permutation([2, 3, 0, 1]), Permutation([1, 0, 3, 2]), \
    Permutation([3, 0, 1, 2]), Permutation([2, 1, 0, 3])]
    """
    gen = range(n)
    for i in xrange(n):
        yield Permutation(gen)
        yield Permutation(gen[::-1])
        gen = rotate_left(gen, 1)
Exemple #10
0
def dihedral(n):
    """
    Generates the dihedral group of order 2n, D2n.

    Examples
    ========
    >>> from sympy.combinatorics.generators import dihedral
    >>> list(dihedral(4))
    [Permutation([0, 1, 2, 3]), Permutation([3, 2, 1, 0]), \
    Permutation([1, 2, 3, 0]), Permutation([0, 3, 2, 1]), \
    Permutation([2, 3, 0, 1]), Permutation([1, 0, 3, 2]), \
    Permutation([3, 0, 1, 2]), Permutation([2, 1, 0, 3])]
    """
    gen = range(n)
    for i in xrange(n):
        yield Permutation(gen)
        yield Permutation(gen[::-1])
        gen = rotate_left(gen, 1)
def cyclic(n):
    """
    Generates the cyclic group of order n, Cn.

    Examples
    ========

    >>> from sympy.combinatorics.generators import cyclic
    >>> list(cyclic(5))
    [(4), (0 1 2 3 4), (0 2 4 1 3),
     (0 3 1 4 2), (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)
Exemple #12
0
def cyclic(n):
    """
    Generates the cyclic group of order n, Cn.

    Examples
    ========

    >>> from sympy.combinatorics.permutations import Permutation
    >>> Permutation.print_cyclic = True
    >>> from sympy.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 = range(n)
    for i in xrange(n):
        yield Permutation(gen)
        gen = rotate_left(gen, 1)
Exemple #13
0
def cyclic(n):
    """
    Generates the cyclic group of order n, Cn.

    Examples
    ========

    >>> from sympy.combinatorics.permutations import Permutation
    >>> Permutation.print_cyclic = True
    >>> from sympy.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)
Exemple #14
0
def cyclic(n):
    """
    Generates the cyclic group of order n, Cn.

    Examples
    ========

    >>> from sympy.combinatorics.permutations import Permutation
    >>> Permutation.print_cyclic = True
    >>> from sympy.combinatorics.generators import cyclic
    >>> list(cyclic(5))
    [(4), (0 1 2 3 4), (0 2 4 1 3),
     (0 3 1 4 2), (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)
Exemple #15
0
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]
Exemple #16
0
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]