Skip to content

Commit

Permalink
move main make_perm to perm_groups
Browse files Browse the repository at this point in the history
Standardize the input to the PermutationGroup so a list of permutations
always gets stored in the object (rather than the unacked args):

PG([a, b]) for PG(a, b) and PG([a, b]) rather than PG(a, b) for the
former.

Correct spelling of continuing.
  • Loading branch information
smichr committed Sep 11, 2012
1 parent 0de06e2 commit 8761a8e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
65 changes: 57 additions & 8 deletions sympy/combinatorics/perm_groups.py
Expand Up @@ -5,13 +5,14 @@
from sympy.combinatorics import Permutation
from sympy.combinatorics.permutations import (_af_commutes_with, _af_invert,
_af_mul)
from sympy.combinatorics.util import _check_cycles_alt_sym,\
_distribute_gens_by_base, _orbits_transversals_from_bsgs,\
_handle_precomputed_bsgs, _base_ordering, _strong_gens_from_distr, _strip

from sympy.combinatorics.util import (_check_cycles_alt_sym,
_distribute_gens_by_base, _orbits_transversals_from_bsgs,
_handle_precomputed_bsgs, _base_ordering, _strong_gens_from_distr,
_strip)
from sympy.functions.combinatorial.factorials import factorial
from sympy.ntheory import isprime, sieve
from sympy.utilities.iterables import has_variety, is_sequence
from sympy.utilities.randtest import _randrange

lmul = Permutation.lmul
_af_new = Permutation._af_new
Expand Down Expand Up @@ -338,9 +339,9 @@ class PermutationGroup(Basic):
>>> from sympy.combinatorics.perm_groups import PermutationGroup
>>> a = Permutation([0, 2, 1])
>>> b = Permutation([1, 0, 2])
>>> G = PermutationGroup([a, b])
>>> G = PermutationGroup(a, b)
>>> G
PermutationGroup([Permutation([0, 2, 1]), Permutation([1, 0, 2])])
PermutationGroup(Permutation([0, 2, 1]), Permutation([1, 0, 2]))
>>> G[0]
Permutation([0, 2, 1])
Expand Down Expand Up @@ -381,8 +382,9 @@ class PermutationGroup(Basic):
def __new__(cls, *args, **kw_args):
"""The default constructor.
"""
obj = Basic.__new__(cls, *args, **kw_args)
obj._generators = list(args[0] if is_sequence(args[0]) else args)
args = list(args[0] if is_sequence(args[0]) else args)
obj = Basic.__new__(cls, *[args], **kw_args)
obj._generators = args
obj._order = None
obj._center = []
obj._is_abelian = None
Expand Down Expand Up @@ -2399,6 +2401,53 @@ def pointwise_stabilizer(self, points):
stab_gens.append(gen)
return PermutationGroup(stab_gens)

def make_perm(self, n, seed=None):
"""
Multiply ``n`` randomly selected permutations from
pgroup together, starting with the identity
permutation. If ``n`` is a list of integers, those
integers will be used to select the permutations and they
will be applied in L to R order: make_perm((A, B, C)) will
give CBA(I) where I is the identity permutation.
``seed`` is used to set the seed for the random selection
of permutations from pgroup. If this is a list of integers,
the corresponding permutations from pgroup will be selected
in the order give. This is mainly used for testing purposes.
Examples
========
>>> from sympy.combinatorics import Permutation
>>> from sympy.combinatorics.perm_groups import PermutationGroup
>>> a, b = [Permutation([1, 0, 3, 2]), Permutation([1, 3, 0, 2])]
>>> G = PermutationGroup([a, b])
>>> G.make_perm(1, [0])
Permutation([1, 0, 3, 2])
>>> G.make_perm(3, [0, 1, 0])
Permutation([2, 0, 3, 1])
>>> G.make_perm([0, 1, 0])
Permutation([2, 0, 3, 1])
See Also
========
random
"""
if is_sequence(n):
if is_sequence(seed):
raise ValueError('If n is a sequence, seed should be None')
n, seed = len(n), n
randrange = _randrange(seed)

# start with the identity permutation
result = Permutation(range(self.degree))
m = len(self)
for i in range(n):
p = self[randrange(m)]
result = lmul(result, p)
return result

def random(self, af=False):
"""Return a random group element
"""
Expand Down
4 changes: 2 additions & 2 deletions sympy/combinatorics/permutations.py
Expand Up @@ -1930,8 +1930,8 @@ def josephus(self, m, n, s = 1):
were selected.
The parameter ``s`` stops the selection process when there are ``s``
items remaining and these are selected by countining the selection
counting by 1 rather than by m.
items remaining and these are selected by countinuing the selection,
counting by 1 rather than by ``m``.
Examples
=======
Expand Down
34 changes: 7 additions & 27 deletions sympy/combinatorics/polyhedron.py
Expand Up @@ -139,7 +139,8 @@ def __new__(cls, corners, faces=[], pgroup=[]):
Permutation([0, 3, 1, 2])
To select the permutations that should be used, supply a list
of indices to the permutations in pgroup:
of indices to the permutations in pgroup in the order they should
be applied:
>>> use = [0, 0, 2]
>>> saved = tetra.make_perm(3, use)
Expand Down Expand Up @@ -469,18 +470,9 @@ def edges(self):
return self._edges

def make_perm(self, n, seed=None):
"""
Multiply ``n`` randomly selected permutations from
pgroup together, starting with the identity
permutation. If ``n`` is a list of integers, those
integers will be used to select the permutations and they
will be applied in L to R order: make_perm((A, B, C)) will
give CBA(I) where I is the identity permutation.
``seed`` is used to set the seed for the random selection
of permutations from pgroup. If this is a list of integers,
the corresponding permutations from pgroup will be selected
in the order give. This is mainly used for testing purposes.
"""A wrapper to the ``pgroup`` method of the same name which
multiplies ``n`` randomly selected permutations from
the pgroup together.
Examples
========
Expand All @@ -498,21 +490,9 @@ def make_perm(self, n, seed=None):
See Also
========
rotate
rotate, sympy.combinatorics.perm_groups.PermutationGroup.make_perm
"""
if is_sequence(n):
if is_sequence(seed):
raise ValueError('If n is a sequence, seed should be None')
n, seed = len(n), n
randrange = _randrange(seed)

# start with the identity permutation
result = Perm(range(len(self.corners)))
m = len(self.pgroup)
for i in range(n):
p = self.pgroup[randrange(m)]
result = lmul(result, p)
return result
return self.pgroup.make_perm(n, seed)

def rotate(self, perm):
"""
Expand Down

0 comments on commit 8761a8e

Please sign in to comment.