
    \h                       S SK JrJrJr  S SKJrJr  S SKJ	r	  S SK
JrJrJrJrJrJr  S SKJrJrJrJrJrJrJrJr  S SKJr  S SKJrJrJr  S SK J!r!  S S	K"J#r#  S S
K$Jr  S SK%J&r&J'r'  S SK(J)r)J*r*  S SK+J,r,  S SK-J.r.J/r/J0r0  \	Rb                  r2\	Rf                  r3 " S S\5      r4SS jr5S r6SS jr7S r8\4r9 " S S\5      r: " S S\5      r;g)    )	factoriallogprod)chainproduct)Permutation)_af_commutes_with
_af_invert_af_rmul	_af_rmuln_af_powCycle)_check_cycles_alt_sym_distribute_gens_by_base_orbits_transversals_from_bsgs_handle_precomputed_bsgs_base_ordering_strong_gens_from_distr_strip	_strip_af)Basic)
_randrange	randrangechoice)Symbol)_sympify)r   )primefactorssieve)	factorintmultiplicity)isprime)has_varietyis_sequenceuniqc                      \ rS rSrSrSrSS.S jrS rS rS r	S	 r
S
 rS rScS jrS rS r\S 5       r  SdS jr\S 5       r\S 5       r\S 5       rS rS rS rS rS rS rS rSeS jrSeS jrS rSeS jr \S  5       r!\S! 5       r"\S" 5       r#S# r$S$ r%SfS% jr&SeS& jr'SeS' jr(\S( 5       r)SgS) jr*\S* 5       r+\S+ 5       r,S, r-S- r.ShS. jr/SiS/ jr0SiS0 jr1\S1 5       r2SgS2 jr3SgS3 jr4SgS4 jr5\S5 5       r6SgS6 jr7\S7 5       r8SgS8 jr9\S9 5       r:S: r;\S; 5       r<S< r=S= r>S> r?SjS? jr@SkS@ jrAScSA jrBSeSB jrCSeSC jrDSD rESE rF\SF 5       rG\SG 5       rH\ISH 5       rJ\SI 5       rK\SJ 5       rLSgSK jrMScSL jrNSeSM jrOSlSN jrPSmSO jrQSP rRScSQ jrSSnSR jrT  SoSS jrUST rVSU rW\SV 5       rXSW rY  SpSX jrZ\SY 5       r[SZ r\S[ r]S\ r^S] r_S^ r`S_ raSgS` jrbSa rcSbrdg)qPermutationGroup   aJ
  The class defining a Permutation group.

Explanation
===========

``PermutationGroup([p1, p2, ..., pn])`` returns the permutation group
generated by the list of permutations. This group can be supplied
to Polyhedron if one desires to decorate the elements to which the
indices of the permutation refer.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> from sympy.combinatorics import Polyhedron

The permutations corresponding to motion of the front, right and
bottom face of a $2 \times 2$ Rubik's cube are defined:

>>> F = Permutation(2, 19, 21, 8)(3, 17, 20, 10)(4, 6, 7, 5)
>>> R = Permutation(1, 5, 21, 14)(3, 7, 23, 12)(8, 10, 11, 9)
>>> D = Permutation(6, 18, 14, 10)(7, 19, 15, 11)(20, 22, 23, 21)

These are passed as permutations to PermutationGroup:

>>> G = PermutationGroup(F, R, D)
>>> G.order()
3674160

The group can be supplied to a Polyhedron in order to track the
objects being moved. An example involving the $2 \times 2$ Rubik's cube is
given there, but here is a simple demonstration:

>>> a = Permutation(2, 1)
>>> b = Permutation(1, 0)
>>> G = PermutationGroup(a, b)
>>> P = Polyhedron(list('ABC'), pgroup=G)
>>> P.corners
(A, B, C)
>>> P.rotate(0) # apply permutation 0
>>> P.corners
(A, C, B)
>>> P.reset()
>>> P.corners
(A, B, C)

Or one can make a permutation as a product of selected permutations
and apply them to an iterable directly:

>>> P10 = G.make_perm([0, 1])
>>> P10('ABC')
['C', 'A', 'B']

See Also
========

sympy.combinatorics.polyhedron.Polyhedron,
sympy.combinatorics.permutations.Permutation

References
==========

.. [1] Holt, D., Eick, B., O'Brien, E.
       "Handbook of Computational Group Theory"

.. [2] Seress, A.
       "Permutation Group Algorithms"

.. [3] https://en.wikipedia.org/wiki/Schreier_vector

.. [4] https://en.wikipedia.org/wiki/Nielsen_transformation#Product_replacement_algorithm

.. [5] Frank Celler, Charles R.Leedham-Green, Scott H.Murray,
       Alice C.Niemeyer, and E.A.O'Brien. "Generating Random
       Elements of a Finite Group"

.. [6] https://en.wikipedia.org/wiki/Block_%28permutation_group_theory%29

.. [7] https://algorithmist.com/wiki/Union_find

.. [8] https://en.wikipedia.org/wiki/Multiply_transitive_group#Multiply_transitive_groups

.. [9] https://en.wikipedia.org/wiki/Center_%28group_theory%29

.. [10] https://en.wikipedia.org/wiki/Centralizer_and_normalizer

.. [11] https://groupprops.subwiki.org/wiki/Derived_subgroup

.. [12] https://en.wikipedia.org/wiki/Nilpotent_group

.. [13] https://www.math.colostate.edu/~hulpke/CGT/cgtnotes.pdf

.. [14] https://docs.gap-system.org/doc/ref/manual.pdf

T)dupsc                    U(       d  [        5       /nO5[        [        US   5      (       a  US   OU5      nU(       d  [        5       /n[        S U 5       5      (       a  U Vs/ s H  n[        U5      PM     nn[	        S U 5       5      (       ad  UR                  SS5      nUc  [        S U 5       5      n[        [        U5      5       H%  nX&   R                  U:w  d  M  [        X&   US9X&'   M'     U(       a7  [        [        U Vs/ s H  n[        [        U5      5      PM     sn5      5      n[        U5      S:  a$  U Vs/ s H  owR                  (       a  M  UPM     nn[        R                  " U /UQ70 UD6$ s  snf s  snf s  snf )	zwThe default constructor. Accepts Cycle and Permutation forms.
Removes duplicates unless ``dups`` keyword is ``False``.
r   c              3   B   #    U  H  n[        U[        5      v   M     g 7fN)
isinstancer   .0as     W/var/www/auris/envauris/lib/python3.13/site-packages/sympy/combinatorics/perm_groups.py	<genexpr>+PermutationGroup.__new__.<locals>.<genexpr>   s     2Tz!U##Ts   c              3   8   #    U  H  oR                   v   M     g 7fr+   sizer-   s     r0   r1   r2      s     ,t!vvt   degreeNc              3   8   #    U  H  oR                   v   M     g 7fr+   r4   r-   s     r0   r1   r2      s     2TVVTr6   r4      )r   listr#   anyr"   popmaxrangelenr5   r$   _af_newis_identityr   __new__)clsr(   argskwargsr/   r7   igs           r0   rB   PermutationGroup.__new__|   s?    M?D;tAw#7#7QTBD#2T222,01DqKNDD1,t,,,ZZ$/F~2T223t9%7<<6))$'?DG & =1gd1g.=>?Dt9q=#9t!==AtD9}}S242622 2 >9s   $FFFFc                    [        U R                  5      U l        S U l        / U l        S U l        S U l        S U l        S U l        S U l	        S U l
        S U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l        ['        U R                  5      U l        U R                  S   R*                  U l        / U l        / U l        / U l        / U l        / U l        / U l        / U l        S U l        g Nr   )r:   rD   _generators_order	_elements_center_is_abelian_is_transitive_is_sym_is_alt_is_primitive_is_nilpotent_is_solvable_is_trivial_transitivity_degree_max_div_is_perfect
_is_cyclic_is_dihedralr?   _rr5   _degree_base_strong_gens_strong_gens_slp_basic_orbits_transversals_transversal_slp_random_gens_fp_presentationselfrD   rE   s      r0   __init__PermutationGroup.__init__   s    		?"!! $(! d&&'''*// 
 " "  !%    c                      U R                   U   $ r+   rK   rg   rF   s     r0   __getitem__PermutationGroup.__getitem__   s    ""rj   c                 |    [        U[        5      (       d  [        S[        U5      -  5      eU R	                  U5      $ )zReturn ``True`` if *i* is contained in PermutationGroup.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> p = Permutation(1, 2, 3)
>>> Permutation(3) in PermutationGroup(p)
True

zRA PermutationGroup contains only Permutations as elements, not elements of type %s)r,   r   	TypeErrortypecontainsrm   s     r0   __contains__PermutationGroup.__contains__   sB     ![)) @BFq'J K K}}Qrj   c                 ,    [        U R                  5      $ r+   )r?   rK   rg   s    r0   __len__PermutationGroup.__len__   s    4##$$rj   c                    [        U[        5      (       d  g[        U R                  5      n[        UR                  5      nX#:X  a  gU H  nUR	                  U5      (       a  M    g   U H  nU R	                  U5      (       a  M    g   g)a  Return ``True`` if PermutationGroup generated by elements in the
group are same i.e they represent the same PermutationGroup.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> p = Permutation(0, 1, 2, 3, 4, 5)
>>> G = PermutationGroup([p, p**2])
>>> H = PermutationGroup([p**2, p])
>>> G.generators == H.generators
False
>>> G.equals(H)
True

FT)r,   r&   set
generatorsrs   )rg   otherset_self_gensset_other_gensgen1gen2s         r0   equalsPermutationGroup.equals   s    " %!122DOO,U--.
 * "D>>$'' " #D==&& # rj   c                    [        U[        5      (       a
  [        XSS9$ U R                   Vs/ s H  o"R                  PM     nnUR                   Vs/ s H  o"R                  PM     nnU R
                  nUR
                  n[        [        U5      5      n[        [        XUU-   5      5      n[        [        U5      5       H  n	XI    V
s/ s H  oU-   PM	     sn
XI'   M     U Vs/ s H  oU-   PM	     nnU Vs/ s H  oU-   PM	     nnX4-   nU V
s/ s H  n
[        U
5      PM     nn
[        U5      $ s  snf s  snf s  sn
f s  snf s  snf s  sn
f )a  
Return the direct product of two permutation groups as a permutation
group.

Explanation
===========

This implementation realizes the direct product by shifting the index
set for the generators of the second group: so if we have ``G`` acting
on ``n1`` points and ``H`` acting on ``n2`` points, ``G*H`` acts on
``n1 + n2`` points.

Examples
========

>>> from sympy.combinatorics.named_groups import CyclicGroup
>>> G = CyclicGroup(5)
>>> H = G*G
>>> H
PermutationGroup([
    (9)(0 1 2 3 4),
    (5 6 7 8 9)])
>>> H.order()
25

+)dir)r,   r   Cosetr|   _array_formr]   r:   r>   r?   r@   r&   )rg   r}   permgens1gens2n1n2startendrF   xgentogethergenss                 r0   __mul__PermutationGroup.__mul__   s%   6 e[))#...2oo>od!!o>.3.>.>?.>d!!.>?\\]]U2Y5"W%&s5z"A(-11B1EH #(-..&+,esse,=$,-Hq
H-%% ?? 2.,-s#   D3D8
D=%E9EENc                    U R                   nU R                   Vs/ s H  oUR                  PM     nn[        U5      nXq:  a(  [	        Xq5       H  nUR                  XhU-
     5        M     [        [	        U5      5      n	UR                  U	5        X`l        Uc#  [	        U5       H  nU R                  5         M     g[	        U5       H  nU R                  X8   S9  M     gs  snf )a&  Initialize random generators for the product replacement algorithm.

Explanation
===========

The implementation uses a modification of the original product
replacement algorithm due to Leedham-Green, as described in [1],
pp. 69-71; also, see [2], pp. 27-29 for a detailed theoretical
analysis of the original product replacement algorithm, and [4].

The product replacement algorithm is used for producing random,
uniformly distributed elements of a group `G` with a set of generators
`S`. For the initialization ``_random_pr_init``, a list ``R`` of
`\max\{r, |S|\}` group generators is created as the attribute
``G._random_gens``, repeating elements of `S` if necessary, and the
identity element of `G` is appended to ``R`` - we shall refer to this
last element as the accumulator. Then the function ``random_pr()``
is called ``n`` times, randomizing the list ``R`` while preserving
the generation of `G` by ``R``. The function ``random_pr()`` itself
takes two random elements ``g, h`` among all elements of ``R`` but
the accumulator and replaces ``g`` with a randomly chosen element
from `\{gh, g(~h), hg, (~h)g\}`. Then the accumulator is multiplied
by whatever ``g`` was replaced by. The new value of the accumulator is
then returned by ``random_pr()``.

The elements returned will eventually (for ``n`` large enough) become
uniformly distributed across `G` ([5]). For practical purposes however,
the values ``n = 50, r = 11`` are suggested in [1].

Notes
=====

THIS FUNCTION HAS SIDE EFFECTS: it changes the attribute
self._random_gens

See Also
========

random_pr

N)_random_prec)	r7   r|   r   r?   r>   appendr:   rd   	random_pr)
rg   rn_random_prec_ndegr   random_genskrF   accs
             r0   _random_pr_init PermutationGroup._random_pr_init!  s    T kk.2oo>o}}o>51["";1u#56 !5:3' !1X   1XN,=>  ?s   Cc                     U R                  X5      nU R                  X$5      nXg:w  aB  X6   X7   :  a  XgpOXvpX8   X9   -   n
XR                  :  a  gXU	'   XU'   UR                  U	5        gg)a  Merges two classes in a union-find data structure.

Explanation
===========

Used in the implementation of Atkinson's algorithm as suggested in [1],
pp. 83-87. The class merging process uses union by rank as an
optimization. ([7])

Notes
=====

THIS FUNCTION HAS SIDE EFFECTS: the list of class representatives,
``parents``, the list of class sizes, ``ranks``, and the list of
elements that are not representatives, ``not_rep``, are changed due to
class merging.

See Also
========

minimal_block, _union_find_rep

References
==========

.. [1] Holt, D., Eick, B., O'Brien, E.
       "Handbook of computational group theory"

.. [7] https://algorithmist.com/wiki/Union_find

r9   r   )_union_find_repmax_divr   )rg   firstsecondranksparentsnot_rep	rep_first
rep_secondnew_1new_2
total_ranks              r0   _union_find_merge"PermutationGroup._union_find_merge]  s~    @ ((8	))&:
"5#44(u)u4JLL("EN%%LNN5!rj   c                 j    XU   pCXC:w  a  UnX#   nXC:w  a  M  XU   pEXC:w  a  X2U'   UnX%   nXC:w  a  M  U$ )a  Find representative of a class in a union-find data structure.

Explanation
===========

Used in the implementation of Atkinson's algorithm as suggested in [1],
pp. 83-87. After the representative of the class to which ``num``
belongs is found, path compression is performed as an optimization
([7]).

Notes
=====

THIS FUNCTION HAS SIDE EFFECTS: the list of class representatives,
``parents``, is altered due to path compression.

See Also
========

minimal_block, _union_find_merge

References
==========

.. [1] Holt, D., Eick, B., O'Brien, E.
       "Handbook of computational group theory"

.. [7] https://algorithmist.com/wiki/Union_find

 )rg   numr   repparenttemps         r0   r    PermutationGroup._union_find_rep  sV    > 3<VmC\F m CLfmDMD]F m 
rj   c                 Z    U R                   / :X  a  U R                  5         U R                   $ )a  Return a base from the Schreier-Sims algorithm.

Explanation
===========

For a permutation group `G`, a base is a sequence of points
`B = (b_1, b_2, \dots, b_k)` such that no element of `G` apart
from the identity fixes all the points in `B`. The concepts of
a base and strong generating set and their applications are
discussed in depth in [1], pp. 87-89 and [2], pp. 55-57.

An alternative way to think of `B` is that it gives the
indices of the stabilizer cosets that contain more than the
identity permutation.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> G = PermutationGroup([Permutation(0, 1, 3)(2, 4)])
>>> G.base
[0, 2]

See Also
========

strong_gens, basic_transversals, basic_orbits, basic_stabilizers

)r^   schreier_simsrw   s    r0   basePermutationGroup.base  s&    > :: zzrj   c           
          [        XUXg5      u  pVn[        U5      nU R                  n	[        Xc   5      [        XcS-      5      -  [        [        XU   XS-      5      5      -  n
US-   US-
  :  a  / nO
XsS-      SS nUSL a  [	        Xs   5      nUR                  XS-      5      n[        [        XX   5      5      U
:w  aC  UR                  XS-      US9nUR                  U5        [        [        XX   5      5      U
:w  a  MC  GO![        Xc   5      nUR                  X   5        XS-      U;   a  UR                  XS-      5        [        [        XX   5      5      U
:w  a  [        [        U5      5      nXS   U   nUR                  R                  XS-      5      nUXcS-      ;  a  U[        XU5      -
  nORXSS-      U   n[        UU5      nU" X   5      [        XX   5      ;  a"  UR                  U5        U[        XX   5      -
  n[        [        XX   5      5      U
:w  a  M  USS nUUUS-   '   USS nUUS-      UU   sUU'   UUS-   '   [        U5      nU H  nUU;  d  M  UR                  U5        M     UU4$ )a  Swap two consecutive base points in base and strong generating set.

Explanation
===========

If a base for a group `G` is given by `(b_1, b_2, \dots, b_k)`, this
function returns a base `(b_1, b_2, \dots, b_{i+1}, b_i, \dots, b_k)`,
where `i` is given by ``pos``, and a strong generating set relative
to that base. The original base and strong generating set are not
modified.

The randomized version (default) is of Las Vegas type.

Parameters
==========

base, strong_gens
    The base and strong generating set.
pos
    The position at which swapping is performed.
randomized
    A switch between randomized and deterministic version.
transversals
    The transversals for the basic orbits, if known.
basic_orbits
    The basic orbits, if known.
strong_gens_distr
    The strong generators distributed by basic stabilizers, if known.

Returns
=======

(base, strong_gens)
    ``base`` is the new base, and ``strong_gens`` is a generating set
    relative to it.

Examples
========

>>> from sympy.combinatorics.named_groups import SymmetricGroup
>>> from sympy.combinatorics.testutil import _verify_bsgs
>>> from sympy.combinatorics.perm_groups import PermutationGroup
>>> S = SymmetricGroup(4)
>>> S.schreier_sims()
>>> S.base
[0, 1, 2]
>>> base, gens = S.baseswap(S.base, S.strong_gens, 1, randomized=False)
>>> base, gens
([0, 2, 1],
[(0 1 2 3), (3)(0 1), (1 3 2),
 (2 3), (1 3)])

check that base, gens is a BSGS

>>> S1 = PermutationGroup(gens)
>>> _verify_bsgs(S1, base, gens)
True

See Also
========

schreier_sims

Notes
=====

The deterministic version of the algorithm is discussed in
[1], pp. 102-103; the randomized version is discussed in [1], p.103, and
[2], p.98. It is of Las Vegas type.
Notice that [1] contains a mistake in the pseudocode and
discussion of BASESWAP: on line 3 of the pseudocode,
`|\beta_{i+1}^{\left\langle T\right\rangle}|` should be replaced by
`|\beta_{i}^{\left\langle T\right\rangle}|`, and the same for the
discussion of the algorithm.

r9      NT)schreier_vector)r   r?   r7   _orbitr&   r   random_stabr   r{   removenextiterr   indexrmulr   )rg   r   strong_genspos
randomizedtransversalsbasic_orbitsstrong_gens_distrbase_lenr7   r5   Tstab_posr   newGammagammar   r   yelstrong_gens_new_distrbase_newstrong_gens_newr   s                            r0   baseswapPermutationGroup.baseswap  s   b %T!-B 	6$5 t9 <$%c,Qw*?&@@&3!7AgGHI 7X\!A!'*1-A'(9(>?H&66t!G}EOfV	23t;**4a=;J + L fV	23t; )*ELL#!G}%T']+ fV	23t;T%[) %e,}}**4a=9|!G44!F6e$<<E$1W-d3AaB$)}F6di,HH %v$)(D D fV	23t; !2! 4)*cAg&7+3C!G+<hsm(xa(12GHC/)&&s+  ((rj   c                 Z    U R                   / :X  a  U R                  5         U R                   $ )ao  
Return the basic orbits relative to a base and strong generating set.

Explanation
===========

If `(b_1, b_2, \dots, b_k)` is a base for a group `G`, and
`G^{(i)} = G_{b_1, b_2, \dots, b_{i-1}}` is the ``i``-th basic stabilizer
(so that `G^{(1)} = G`), the ``i``-th basic orbit relative to this base
is the orbit of `b_i` under `G^{(i)}`. See [1], pp. 87-89 for more
information.

Examples
========

>>> from sympy.combinatorics.named_groups import SymmetricGroup
>>> S = SymmetricGroup(4)
>>> S.basic_orbits
[[0, 1, 2, 3], [1, 2, 3], [2, 3]]

See Also
========

base, strong_gens, basic_transversals, basic_stabilizers

)ra   r   rw   s    r0   r   PermutationGroup.basic_orbitsb  s*    8 # !!!rj   c                     U R                   / :X  a  U R                  5         U R                  nU R                  nU(       d  / $ [	        X!5      n/ nU H  nUR                  [        U5      5        M     U$ )at  
Return a chain of stabilizers relative to a base and strong generating
set.

Explanation
===========

The ``i``-th basic stabilizer `G^{(i)}` relative to a base
`(b_1, b_2, \dots, b_k)` is `G_{b_1, b_2, \dots, b_{i-1}}`. For more
information, see [1], pp. 87-89.

Examples
========

>>> from sympy.combinatorics.named_groups import AlternatingGroup
>>> A = AlternatingGroup(4)
>>> A.schreier_sims()
>>> A.base
[0, 1]
>>> for g in A.basic_stabilizers:
...     print(g)
...
PermutationGroup([
    (3)(0 1 2),
    (1 2 3)])
PermutationGroup([
    (1 2 3)])

See Also
========

base, strong_gens, basic_orbits, basic_transversals

)rb   r   r_   r^   r   r   r&   )rg   r   r   r   basic_stabilizersr   s         r0   r   "PermutationGroup.basic_stabilizers  sp    J # ''zzI4TG%D$$%5d%;< &  rj   c                 Z    U R                   / :X  a  U R                  5         U R                   $ )a  
Return basic transversals relative to a base and strong generating set.

Explanation
===========

The basic transversals are transversals of the basic orbits. They
are provided as a list of dictionaries, each dictionary having
keys - the elements of one of the basic orbits, and values - the
corresponding transversal elements. See [1], pp. 87-89 for more
information.

Examples
========

>>> from sympy.combinatorics.named_groups import AlternatingGroup
>>> A = AlternatingGroup(4)
>>> A.basic_transversals
[{0: (3), 1: (3)(0 1 2), 2: (3)(0 2 1), 3: (0 3 1)}, {1: (3), 2: (1 2 3), 3: (1 3 2)}]

See Also
========

strong_gens, base, basic_orbits, basic_stabilizers

)rb   r   rw   s    r0   basic_transversals#PermutationGroup.basic_transversals  s*    : # !!!rj   c           
         U R                  5       n[        S US   R                   5       5      (       d  [        S5      e/ n[	        [        U5      S-
  5       H  nXS-      n/ nX   R                   H  n[        U/UR                  -   5      nUR                  5       UR                  5       -  n/ n	[        U5      R                  5        HC  u  p[	        U5       H/  nU	R                  [        U/UR                  -   5      5        Xj-  nM1     ME     X-   nUnM     X   US'   UR                  U5        M     UR                  US   5        U$ )a~  
Return the composition series for a group as a list
of permutation groups.

Explanation
===========

The composition series for a group `G` is defined as a
subnormal series `G = H_0 > H_1 > H_2 \ldots` A composition
series is a subnormal series such that each factor group
`H(i+1) / H(i)` is simple.
A subnormal series is a composition series only if it is of
maximum length.

The algorithm works as follows:
Starting with the derived series the idea is to fill
the gap between `G = der[i]` and `H = der[i+1]` for each
`i` independently. Since, all subgroups of the abelian group
`G/H` are normal so, first step is to take the generators
`g` of `G` and add them to generators of `H` one by one.

The factor groups formed are not simple in general. Each
group is obtained from the previous one by adding one
generator `g`, if the previous group is denoted by `H`
then the next group `K` is generated by `g` and `H`.
The factor group `K/H` is cyclic and it's order is
`K.order()//G.order()`. The series is then extended between
`K` and `H` by groups generated by powers of `g` and `H`.
The series formed is then prepended to the already existing
series.

Examples
========
>>> from sympy.combinatorics.named_groups import SymmetricGroup
>>> from sympy.combinatorics.named_groups import CyclicGroup
>>> S = SymmetricGroup(12)
>>> G = S.sylow_subgroup(2)
>>> C = G.composition_series()
>>> [H.order() for H in C]
[1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
>>> G = S.sylow_subgroup(3)
>>> C = G.composition_series()
>>> [H.order() for H in C]
[243, 81, 27, 9, 3, 1]
>>> G = CyclicGroup(12)
>>> C = G.composition_series()
>>> [H.order() for H in C]
[12, 6, 3, 1]

c              3   8   #    U  H  oR                   v   M     g 7fr+   )rA   r.   rG   s     r0   r1   6PermutationGroup.composition_series.<locals>.<genexpr>  s     =*<Q==*<r6   r   zGroup should be solvabler9   r   )derived_seriesallr|   NotImplementedErrorr>   r?   r&   orderr   itemsr   extend)rg   derseriesrF   Hup_segrG   Kr   down_segpe_s                r0   composition_series#PermutationGroup.composition_series  s3   f !!#=#b'*<*<===%&@AAs3xz"AaCAFV&&$aS1<<%78	QWWY.%e,224DA"1X (8!q||9K(LMD & 5 "* ' F1IMM&! # 	c"grj   c                   ^^^^^ UR                  U 5      (       d  [        S5      eUR                  5       S:X  a  U R                  $ U R	                  UR
                  S9  U R
                  m[        TU R                  5      m[        U R                  S-
  5      nU R                  SS n[        U5       H'  u  mn[        UR                  5       UUU4S jS9UT'   M)     UR                  nUR                  nU R                  n[        Xv5       VV	s/ s H&  u  pUR                  5       U	R                  5       -  PM(     n
nn	[!        U5      [!        U5      :  a  U[!        U5         R                  nOU/n[!        U5      S-
  m[!        U5      nTS:  a  / nUT    H  nX:X  a  M
  TTT   U-     mU HN  nXN-  m[#        UUU4S jUT    5       5      (       a  UR%                  T5        U[!        U5      -   U
T   :X  d  MN    O   U[!        U5      -   U
T   :X  d  M    O   X-  nU[!        U5      -  nTS-  mTS:  a  M  UR'                  U5        U/U-   nU$ s  sn	nf )	zReturn a transversal of the right cosets of self by its subgroup H
using the second method described in [1], Subsection 4.6.7

The argument must be a subgroupr9   r   Nc                    > TTT   U -     $ r+   r   )r   r   base_orderingls    r0   <lambda>4PermutationGroup.coset_transversal.<locals>.<lambda>6  s    d1gai0Hrj   keyr   c              3   :   >#    U  H  nTUT-     T:  v   M     g 7fr+   r   )r.   hbr   r   s     r0   r1   5PermutationGroup.coset_transversal.<locals>.<genexpr>P  s     FIq=1-2I   )is_subgroup
ValueErrorr   elements_schreier_simsr   r   r7   r   r   	enumeratesortedvaluesr   r   zipr?   r   r   r   )rg   r   identityr   torbitsh_stabsg_stabsr   r   indicesr   t_lenT_nextur  r   r   r   r   s                  @@@@@r0   coset_transversal"PermutationGroup.coset_transversal  s-    }}T"">??779>== (yy&tT[[9t{{Q/..q1 l+DAq$QXXZ&HJLO , %%((474IJ4IDA1779aggi'4IJ w<#g,&G%..A
ALNA"fF!!_=!$q'!),AAFF1IFFFa(s6{*gaj8  3v;&'!*4 % KAS[ EFA! "f" 	
JN? Ks   -Ic                 :  ^^^^^^	 UR                  5       S:X  a  U$ U R                  S[        UR                  5       UR                  :X  d  U R                  UR                  S9  UR                  SS mUR
                   Vs/ s H  n[        UR                  5       5      PM     snmU R
                   Vs/ s H  n[        UR                  5       5      PM     snm	U R                  m[        TU R                  5      mUUUUUU	4S jmT" SU5      $ s  snf s  snf )zoReturn the representative of Hg from the transversal that
would be computed by ``self.coset_transversal(H)``.

r9   Nr   c                 2  >^ [        T	U    UU4S jS9nTU     Vs/ s H  nTU    U-  PM     snR                  U5      nTU    U   T-  mU [        T	5      S-
  :  a5  TU     H  nTU    U-  TU    T-  :X  d  M    O   T
" U S-   TWS-  -  5      U-  mT$ s  snf )Nc                    > TU T-     $ r+   r   )r   r   r   s    r0   r   FPermutationGroup._coset_representative.<locals>.step.<locals>.<lambda>m  s    =13Erj   r   r9   r   )minr   r?   )r   r   r   r  rF   r  r   r   h_transversalsr  stepr   s    `    r0   r   4PermutationGroup._coset_representative.<locals>.stepl  s    q	)EFE$21$56$5qa$56<<UCAq!!$Q&A3v;q= %aAAwqyDGAI- ) 1a2g&q(H 7s   Br   )
r   r   r?   r
  r   r   r:   r  r   r7   )
rg   rG   r   r   r   r   r  r  r   r   s
       @@@@@@r0   _coset_representative&PermutationGroup._coset_representative]  s    
 779>Hyy#aff+&!&&0QVV,"454H4HI4Hq$qxxz*4HI262I2IJ2IQQXXZ(2IJyy&tT[[9		 		 Aqz JJs   #D7#Dc                 .   UR                  U 5      (       d  [        S5      eU R                  U5      n[        U5      n[	        [
        R                  " S U R                   5       5      5      n/ n[        U5       HZ  nU Vs/ s H  opR                  X&   U-  U5      PM     nnU V	s/ s H  oR                  U	5      PM     nn	UR                  U5        M\     [        [        U5      5      nSn
[        [        U5      U5       H  u  pX[   U   nX:  ag  X:  a]  U HW  nXZ   U   nX]   U   XZ   U'   XU   U'   [        U5       H+  nXV   U   U:X  a	  XU   U'   M  XV   U   U
:X  d  M$  XU   U'   M-     MY     U
S-  n
XS-
  :  d  M  Us  $    gs  snf s  sn	f )zMReturn the standardised (right) coset table of self in H as
a list of lists.
r   c              3   .   #    U  H  nXS -  4v   M     g7f)r   Nr   )r.   r   s     r0   r1   /PermutationGroup.coset_table.<locals>.<genexpr>  s      %0. '*7^.s   r9   N)r  r  r  r?   r:   r   from_iterabler|   r>   r"  r   r   r   )rg   r   r   r   AtablerF   r   rowr   r   alphar/   betazs                  r0   coset_tablePermutationGroup.coset_tablex  s    }}T"">??""1%F$$ %0#%0 0 1 qABCD!Q--ad1fa8!CD'*+s!771:sC+LL  #a&Ma!,HE<?D}<!LO*/+a.Q)*dA!&qA$x{d2.3a!&!!5.2a	 "*	  
!| - E+s    F(Fc                 h    U R                   (       d  U R                  U 5      U l         U R                   $ )aU  
Return the center of a permutation group.

Explanation
===========

The center for a group `G` is defined as
`Z(G) = \{z\in G | \forall g\in G, zg = gz \}`,
the set of elements of `G` that commute with all elements of `G`.
It is equal to the centralizer of `G` inside `G`, and is naturally a
subgroup of `G` ([9]).

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> D = DihedralGroup(4)
>>> G = D.center()
>>> G.order()
2

See Also
========

centralizer

Notes
=====

This is a naive implementation that is a straightforward application
of ``.centralizer()``

)rN   centralizerrw   s    r0   centerPermutationGroup.center  s(    D ||++D1DL||rj   c                   ^^^^^ [        TS5      (       Ga  TR                  (       d  U R                  (       a  U $ U R                  n[        [	        [        U5      5      5      nTR                  5       n[        U5      nUR                  S S9  / nS/U-  mS/U-  nS/U-  m[        U5       H7  n[	        XH   5      n	U	S   TU'   [        U5      Xx'   U	 H  n
UTU
'   M
     Xi-   nM9     U R                  US9u  mn[        TU5      nSn[        [        T5      5       H  nX   U/:X  d  M    O   TSU mUn[        U5       H  nTUS-
     XN   ;   d  M    O   USWS-    n[        U5      nS/U-  m[        U5       H$  nTU   n[        TR                  USS	95      TU'   M&     S
 nS/U-  n[        U5       H$  nTU   T;   a  UUU'   M  U4UUUU4S jjnUUU'   M&     U4S jnU R                  UTUUS9$ [        TS5      (       a%  [	        T5      nU R                  [        U5      5      $ [        TS5      (       a  U R                  [        T/5      5      $ g)ao  
Return the centralizer of a group/set/element.

Explanation
===========

The centralizer of a set of permutations ``S`` inside
a group ``G`` is the set of elements of ``G`` that commute with all
elements of ``S``::

    `C_G(S) = \{ g \in G | gs = sg \forall s \in S\}` ([10])

Usually, ``S`` is a subset of ``G``, but if ``G`` is a proper subgroup of
the full symmetric group, we allow for ``S`` to have elements outside
``G``.

It is naturally a subgroup of ``G``; the centralizer of a permutation
group is equal to the centralizer of any set of generators for that
group, since any element commuting with the generators commutes with
any product of the  generators.

Parameters
==========

other
    a permutation group/list of permutations/single permutation

Examples
========

>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
... CyclicGroup)
>>> S = SymmetricGroup(6)
>>> C = CyclicGroup(6)
>>> H = S.centralizer(C)
>>> H.is_subgroup(C)
True

See Also
========

subgroup_search

Notes
=====

The implementation is an application of ``.subgroup_search()`` with
tests using a specific base for the group ``G``.

r|   c                     [        U 5      * $ r+   r?   r   s    r0   r   .PermutationGroup.centralizer.<locals>.<lambda>  s
    s1vgrj   r   Nr   r   r9   Tpairsc                     gNTr   r7  s    r0   r   r8  "      Trj   c                    > X   nT	TU      nT
U   nUR                   TU      nUR                   U   nTU   TU      nXWR                   U   :H  $ r+   r   )computed_wordsr   rG   rep_orb_indexr   imim_reptr_elr   orbit_descr
orbit_repsr   s           r0   test*PermutationGroup.centralizer.<locals>.test(  sl    *-(3DG(<(7]]473!"s!3 ,] ;DG D
  "%6%6v%>>>rj   c                    > TR                    Vs/ s H  n[        X5      PM     snTR                    Vs/ s H  n[        X5      PM     sn:H  $ s  snf s  snf r+   )r|   r   )rG   r   r}   s     r0   prop*PermutationGroup.centralizer.<locals>.prop6  sQ    050@0@A0@Q0@A050@0@A0@S0@AB BAAs
   AA)r   r   testsrn   
array_form)hasattr
is_trivialr7   r@   r:   r>   r  r?   sortschreier_sims_incrementalr   dictorbit_transversalsubgroup_searchr1  r&   )rg   r}   r7   r  r  
num_orbits	long_baseorbit_reps_indicesrF   orbitpointr   r   r   j
rel_orbitsnum_rel_orbitsr   trivial_testrL  r   rG  rJ  r   r   rE  rF  r   s    `                      @@@@r0   r1  PermutationGroup.centralizer  s   f 5,''4??[[FtE&M23H\\^FVJKK-K.I
*J"&
!2&-K:&VY %a
1(+I"%"E)*K& #%-	 ' !% > >I > ND+ 8{ KA3t9%$'H:5 & 8DH:&1%2 '  !a%J _N 6.0L>* m"&++Ct+<#>Q + *LF8OE8_7j(+E!H/0 ? ?  $E!H! %$B ''44?u ( N NUM**;D##$4T$:;;UL))##$4eW$=>> *rj   c                     UR                   nUR                   n/ nU H3  nU H*  n[        XvU) U) 5      nX;  d  M  UR                  U5        M,     M5     U R                  U5      n	U	$ )a  
Return the commutator of two subgroups.

Explanation
===========

For a permutation group ``K`` and subgroups ``G``, ``H``, the
commutator of ``G`` and ``H`` is defined as the group generated
by all the commutators `[g, h] = hgh^{-1}g^{-1}` for ``g`` in ``G`` and
``h`` in ``H``. It is naturally a subgroup of ``K`` ([1], p.27).

Examples
========

>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
... AlternatingGroup)
>>> S = SymmetricGroup(5)
>>> A = AlternatingGroup(5)
>>> G = S.commutator(S, A)
>>> G.is_subgroup(A)
True

See Also
========

derived_subgroup

Notes
=====

The commutator of two subgroups `H, G` is equal to the normal closure
of the commutators of all the generators, i.e. `hgh^{-1}g^{-1}` for `h`
a generator of `H` and `g` a generator of `G` ([1], p.28)

)r|   r   r   normal_closure)
rg   Gr   ggenshgenscommutatorsggenhgen
commutatorress
             r0   rg  PermutationGroup.commutatorA  sm    H D!$tedU;
0&&z2  
 !!+.
rj   c                    [        U[        [        45      (       a  UR                  5       n[	        U5      U R
                  :w  a  [        S5      e[        [        U R
                  5      5      nU R                  nU R                  n/ nU R                  nUn[        [	        U5      5       Hh  n	XU	      n
XU	   :X  a  UR                  U
5        M%  XU	   ;  a  / s  $ XY   U
   R                  n[        [        U5      U5      nUR                  U
5        Mj     X:w  a  / $ U(       a  U$ U R                  n[        [	        U5      5       V	s/ s H  oU	   Xi      PM     nn	U$ s  sn	f )a8  Return ``G``'s (self's) coset factorization of ``g``

Explanation
===========

If ``g`` is an element of ``G`` then it can be written as the product
of permutations drawn from the Schreier-Sims coset decomposition,

The permutations returned in ``f`` are those for which
the product gives ``g``: ``g = f[n]*...f[1]*f[0]`` where ``n = len(B)``
and ``B = G.base``. f[i] is one of the permutations in
``self._basic_orbits[i]``.

If factor_index==True,
returns a tuple ``[b[0],..,b[n]]``, where ``b[i]``
belongs to ``self._basic_orbits[i]``

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation(0, 1, 3, 7, 6, 4)(2, 5)
>>> b = Permutation(0, 1, 3, 2)(4, 5, 7, 6)
>>> G = PermutationGroup([a, b])

Define g:

>>> g = Permutation(7)(1, 2, 4)(3, 6, 5)

Confirm that it is an element of G:

>>> G.contains(g)
True

Thus, it can be written as a product of factors (up to
3) drawn from u. See below that a factor from u1 and u2
and the Identity permutation have been used:

>>> f = G.coset_factor(g)
>>> f[2]*f[1]*f[0] == g
True
>>> f1 = G.coset_factor(g, True); f1
[0, 4, 4]
>>> tr = G.basic_transversals
>>> f[0] == tr[0][f1[0]]
True

If g is not an element of G then [] is returned:

>>> c = Permutation(5, 6, 7)
>>> G.coset_factor(c)
[]

See Also
========

sympy.combinatorics.util._strip

z.g should be the same size as permutations of G)r,   r   r   r:   r?   r]   r  r>   r   rb   r   r   r   r   r
   r   )rg   rG   factor_indexIr   r   factorsr   r  rF   r,  r  trs                r0   coset_factorPermutationGroup.coset_factorp  sA   x a%-..Aq6T\\! MNNt||$%(())yys4y!A!W:DAwt$?*	%11AA*ANN4  " 6IN$$.3CI.>?.>a5$.>? @s   Ec           	      j   / nUR                   (       a  / $ XR                  ;   aS  U(       a  XR                  ;   a  U/$ U R                  U   nU H#  nUR                  " U R                  USS95        M%     U$ US-  U R                  ;   a  US-  nU(       a  XR                  ;   a  US-  /$ U R                  U   nU H#  nUR                  " U R                  USS95        M%     [        U5      n[        U5       Vs/ s H  osXg-
  S-
     S-  PM     nnU$ U R                  US5      n[        U5       Hw  u  pyU R                  U   U	   nU HZ  nU(       d!  UR                  " U R                  U   5        M+  U R                  U   nUR                  " U R                  USS95        M\     My     U$ s  snf )z
Return a list of strong generators `[s1, \dots, sn]`
s.t `g = sn \times \dots \times s1`. If ``original=True``, make the
list contain only the original group generators

Toriginalr   r9   )rA   r   r|   r`   r   generator_productr?   r>   ro  r  rc   r   )
rg   rG   rs  r   slpsr   rF   frZ  s
             r0   rt  "PermutationGroup.generator_product  s    ==I   qOO3s
++A.ANN4#9#9!d#9#KL Ud&&&2AqOO32w++A.ANN4#9#9!d#9#KL L7<Qx@x!13q5>2-x@a&aLDA''*1-CNN4#3#3A#67((+ANN4#9#9!d#9#KL  !  As   <F0c                    U R                  US5      nU(       d  gSnSnU R                  nU R                  nU R                  n[	        [        U5      5       H1  nX(   n	Xx   R                  U	5      n
X4U
-  -  nU[        XX   5      -  nM3     U$ )a  rank using Schreier-Sims representation.

Explanation
===========

The coset rank of ``g`` is the ordering number in which
it appears in the lexicographic listing according to the
coset decomposition

The ordering is the same as in G.generate(method='coset').
If ``g`` does not belong to the group it returns None.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation(0, 1, 3, 7, 6, 4)(2, 5)
>>> b = Permutation(0, 1, 3, 2)(4, 5, 7, 6)
>>> G = PermutationGroup([a, b])
>>> c = Permutation(7)(2, 4)(3, 5)
>>> G.coset_rank(c)
16
>>> G.coset_unrank(16)
(7)(2 4)(3 5)

See Also
========

coset_factor

TNr   r9   )ro  rb   r^   ra   r>   r?   r   )rg   rG   rm  rankr  r   r   r   rF   r   rZ  s              r0   
coset_rankPermutationGroup.coset_rank  s    @ ##At,))zz))s4y!A
A%%a(AaCKD#lo&&A	 "
 rj   c                    US:  d  XR                  5       :  a  gU R                  nU R                  nU R                  n[	        U5      nS/U-  n[        U5       H%  n[        U[	        XH   5      5      u  pXX   U	   Xx'   M'     [        U5       Vs/ s H  oU   Xx      R                  PM     n
n[        U
6 nU(       a  U$ [        U5      $ s  snf )zunrank using Schreier-Sims representation

coset_unrank is the inverse operation of coset_rank
if 0 <= rank < order; otherwise it returns None.

r   N)
r   r   r   r   r?   r>   divmodr   r   r@   )rg   rz  afr   r   r   mvrF   cr/   r  s               r0   coset_unrankPermutationGroup.coset_unrank#  s     !8tzz|+yy..((ICEqAT3|#78GD?1%AD  9>aA1!_QT"..AqMH1: Bs   Cc                     U R                   $ )a  Returns the size of the permutations in the group.

Explanation
===========

The number of permutations comprising the group is given by
``len(group)``; the number of permutations that can be generated
by the group is given by ``group.order()``.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([1, 0, 2])
>>> G = PermutationGroup([a])
>>> G.degree
3
>>> len(G)
1
>>> G.order()
2
>>> list(G.generate())
[(2), (2)(0 1)]

See Also
========

order
)r]   rw   s    r0   r7   PermutationGroup.degree;  s    > ||rj   c                 P    [        [        [        U R                  5      5      5      $ )z8
Return the identity element of the permutation group.

)r@   r:   r>   r7   rw   s    r0   r  PermutationGroup.identity\  s     tE$++./00rj   c                 x    U R                   (       d  [        U R                  5       5      U l         U R                   $ )a  Returns all the elements of the permutation group as a list

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> p = PermutationGroup(Permutation(1, 3), Permutation(1, 2))
>>> p.elements
[(3), (3)(1 2), (1 3), (2 3), (1 2 3), (1 3 2)]

)rM   r:   generaterw   s    r0   r	  PermutationGroup.elementsd  s'     ~~!$--/2DN~~rj   c                     U /nU nU R                  5       nUR                  U5      (       d;  UR                  U5        UnUR                  5       nUR                  U5      (       d  M;  U$ )aa  Return the derived series for the group.

Explanation
===========

The derived series for a group `G` is defined as
`G = G_0 > G_1 > G_2 > \ldots` where `G_i = [G_{i-1}, G_{i-1}]`,
i.e. `G_i` is the derived subgroup of `G_{i-1}`, for
`i\in\mathbb{N}`. When we have `G_k = G_{k-1}` for some
`k\in\mathbb{N}`, the series terminates.

Returns
=======

A list of permutation groups containing the members of the derived
series in the order `G = G_0, G_1, G_2, \ldots`.

Examples
========

>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
... AlternatingGroup, DihedralGroup)
>>> A = AlternatingGroup(5)
>>> len(A.derived_series())
1
>>> S = SymmetricGroup(4)
>>> len(S.derived_series())
4
>>> S.derived_series()[1].is_subgroup(AlternatingGroup(4))
True
>>> S.derived_series()[2].is_subgroup(DihedralGroup(2))
True

See Also
========

derived_subgroup

)derived_subgroupr  r   rg   rh  currentnxts       r0   r   PermutationGroup.derived_seriesv  sh    P f##%%%c**JJsOG&&(C %%c** 
rj   c                    U R                   nU R                   Vs/ s H  o"R                  PM     nn[        5       nU R                  n[        [        U5      5      n[        U5       Hk  n[        U5       HY  nX7   n	X8   n
[        [        U5      5      nU H  nXU      XX      '   M     [        U5      nX;  d  MH  UR                  U5        M[     Mm     U Vs/ s H  n[        U5      PM     nnU R                  U5      nU$ s  snf s  snf )as  Compute the derived subgroup.

Explanation
===========

The derived subgroup, or commutator subgroup is the subgroup generated
by all commutators `[g, h] = hgh^{-1}g^{-1}` for `g, h\in G` ; it is
equal to the normal closure of the set of commutators of the generators
([1], p.28, [11]).

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([1, 0, 2, 4, 3])
>>> b = Permutation([0, 1, 3, 2, 4])
>>> G = PermutationGroup([a, b])
>>> C = G.derived_subgroup()
>>> list(C.generate(af=True))
[[0, 1, 2, 3, 4], [0, 1, 3, 4, 2], [0, 1, 4, 2, 3]]

See Also
========

derived_series

)r\   r|   r   r{   r]   r:   r>   tupleaddr@   r`  )rg   r   r   r   set_commutatorsr7   rngrF   rZ  p1p2r  r   ctcmsG2s                   r0   r  !PermutationGroup.derived_subgroup  s    8 GG'+7!7%5=!qA1XWWv'A#%e9AiL 1X,#''+   $33?awqz?3  %	! 8 4s   DD	c                 z    US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ [        SU-  5      e)a  Return iterator to generate the elements of the group.

Explanation
===========

Iteration is done with one of these methods::

  method='coset'  using the Schreier-Sims coset representation
  method='dimino' using the Dimino method

If ``af = True`` it yields the array form of the permutations

Examples
========

>>> from sympy.combinatorics import PermutationGroup
>>> from sympy.combinatorics.polyhedron import tetrahedron

The permutation group given in the tetrahedron object is also
true groups:

>>> G = tetrahedron.pgroup
>>> G.is_group
True

Also the group generated by the permutations in the tetrahedron
pgroup -- even the first two -- is a proper group:

>>> H = PermutationGroup(G[0], G[1])
>>> J = PermutationGroup(list(H.generate())); J
PermutationGroup([
    (0 1)(2 3),
    (1 2 3),
    (1 3 2),
    (0 3 1),
    (0 2 3),
    (0 3)(1 2),
    (0 1 3),
    (3)(0 2 1),
    (0 3 2),
    (3)(0 1 2),
    (0 2)(1 3)])
>>> _.is_group
True
cosetdiminozNo generation defined for %s)generate_schreier_simsgenerate_diminor   )rg   methodr  s      r0   r  PermutationGroup.generate  sH    \ W..r22x''++%&Dv&MNNrj   c           	   #     #    [        [        U R                  5      5      nSnU/n[        U5      1nU(       a  Uv   O[	        U5      v   U R
                   Vs/ s H  ofR                  PM     nn[        [        U5      5       H  nUR                  5       n	U/n
U
(       d  M  U
n/ n
U H  nUSUS-     H  n[        X5      n[        U5      U;  d  M  U	 Hj  nUS-  n[        X5      nU(       a  Uv   O[	        U5      nUv   UR                  U5        UR                  [        U5      5        U
R                  U5        Ml     M     M     U
(       a  M  M     [        U5      U l        gs  snf 7f)aL  Yield group elements using Dimino's algorithm.

If ``af == True`` it yields the array form of the permutations.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1, 3])
>>> b = Permutation([0, 2, 3, 1])
>>> g = PermutationGroup([a, b])
>>> list(g.generate_dimino(af=True))
[[0, 1, 2, 3], [0, 2, 1, 3], [0, 2, 3, 1],
 [0, 1, 3, 2], [0, 3, 2, 1], [0, 3, 1, 2]]

References
==========

.. [1] The Implementation of Various Algorithms for Permutation Groups in
       the Computer Algebra System: AXIOM, N.J. Doye, M.Sc. Thesis

r   Nr9   )r:   r>   r7   r  r@   r|   r   r?   copyr   r   r  rL   )rg   r  idnr   element_listset_element_listr   r   rF   DNr(  r/   rG   agdaps                    r0   r   PermutationGroup.generate_dimino  s@    . 5%&u!#J<I#,'+7!7s4y!A!!#AA!A!&1q5\%a^ 9,<<%& %
%-a_#%*,H(/A*+G , 3 3B 7 0 4 4U2Y ? ! &'	 *  !	 ". ,'3 8s%   AE.E).1E.#,E.A?E.E.c              #   &  #    U R                   nU R                  nU R                  n[        U5      S:X  a/  U R                   H  nU(       a  UR
                  v   M  Uv   M      g[        U5      S:X  a4  US    H*  nU(       a  US   U   R
                  v   M   US   U   v   M,     g[        [        U5      5      nUSSS2   n[        [        U5      5      /nU Vs/ s H  n[        U5      PM     nn[        U5      S-
  n	S/U	-  n
Sn X   X   :  a"  US:X  a  gSX'   US-  nUR                  5         M-  [        X;   XK   X         R
                  US   5      nX==   S-  ss'   UR                  U5        US-  nX:X  a  U(       a0  US    H&  n[        US   U   R
                  US   5      nUv   M(     O:US    H1  n[        US   U   R
                  US   5      n[        U5      nUv   M3     UR                  5         US-  nGM  s  snf 7f)a  Yield group elements using the Schreier-Sims representation
in coset_rank order

If ``af = True`` it yields the array form of the permutations

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1, 3])
>>> b = Permutation([0, 2, 3, 1])
>>> g = PermutationGroup([a, b])
>>> list(g.generate_schreier_sims(af=True))
[[0, 1, 2, 3], [0, 2, 1, 3], [0, 3, 2, 1],
 [0, 1, 3, 2], [0, 2, 3, 1], [0, 3, 1, 2]]
r   Nr9   r   )r]   r   ra   r?   r|   r   r:   reversedr>   r<   r   r   r@   )rg   r  r   r  r   r   rF   stgposmaxr   r   r  r   r  s                 r0   r  'PermutationGroup.generate_schreier_simsE  s    $ LL##))q6Q;__--'G	 %
 q6Q;!!_A$q'---A$q'M	 %
 !#DbD)E!H~"#$!Q#a&!$[1_c"fv"6Q	locf56BBCGLAFaKFJJqMFAw)"-$QrU1X%9%93r7C . *"-$QrU1X%9%93r7C$QZ  . 	Q1 	 %s   CHH2DHc                     U R                   $ )zReturns the generators of the group.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1])
>>> b = Permutation([1, 0, 2])
>>> G = PermutationGroup([a, b])
>>> G.generators
[(1 2), (2)(0 1)]

rl   rw   s    r0   r|   PermutationGroup.generators  s     rj   c                    [        U[        5      (       d  gUR                  U R                  :w  a  U(       a  g[        XR                  S9nXR                  ;   a  g[        U R                  UR                  S5      5      $ )a_  Test if permutation ``g`` belong to self, ``G``.

Explanation
===========

If ``g`` is an element of ``G`` it can be written as a product
of factors drawn from the cosets of ``G``'s stabilizers. To see
if ``g`` is one of the actual generators defining the group use
``G.has(g)``.

If ``strict`` is not ``True``, ``g`` will be resized, if necessary,
to match the size of permutations in ``self``.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup

>>> a = Permutation(1, 2)
>>> b = Permutation(2, 3, 1)
>>> G = PermutationGroup(a, b, degree=5)
>>> G.contains(G[0]) # trivial check
True
>>> elem = Permutation([[2, 3]], size=5)
>>> G.contains(elem)
True
>>> G.contains(Permutation(4)(0, 1, 2, 3))
False

If strict is False, a permutation will be resized, if
necessary:

>>> H = PermutationGroup(Permutation(5))
>>> H.contains(Permutation(3))
False
>>> H.contains(Permutation(3), strict=False)
True

To test if a given permutation is present in the group:

>>> elem in G.generators
False
>>> G.has(elem)
False

See Also
========

coset_factor, sympy.core.basic.Basic.has, __contains__

Fr4   T)r,   r   r5   r7   r|   boolro  rM  )rg   rG   stricts      r0   rs   PermutationGroup.contains  sb    h ![))66T[[ AKK0AD%%allD9::rj   c                 |    U R                   c$  U R                  U R                  5       5      U l         U R                   $ )a0  Return ``True`` if the group is perfect.
A group is perfect if it equals to its derived subgroup.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation(1,2,3)(4,5)
>>> b = Permutation(1,2,3,4,5)
>>> G = PermutationGroup([a, b])
>>> G.is_perfect
False

)rY   r   r  rw   s    r0   
is_perfectPermutationGroup.is_perfect  s6      ##{{4+@+@+BCDrj   c                     U R                   b  U R                   $ SU l         U R                   Vs/ s H  oR                  PM     nnU H-  nU H$  nXC::  a  M
  [        X45      (       a  M  SU l             g   M/     gs  snf )a>  Test if the group is Abelian.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1])
>>> b = Permutation([1, 0, 2])
>>> G = PermutationGroup([a, b])
>>> G.is_abelian
False
>>> a = Permutation([0, 2, 1])
>>> G = PermutationGroup([a])
>>> G.is_abelian
True

TF)rO   r|   r   r	   )rg   r   r   r   r   s        r0   
is_abelianPermutationGroup.is_abelian  sz    & '###'+7!7A6(..',D$     8s   A:c                    U R                   (       a  / $ U R                  n/ nU nUR                  5       nUR                  n[        UR	                  5       5       H  n/ n / nU H0  n	X-  n
UR                  U
5      (       a  M  UR                  U
5        M2     U(       a  [        XX-   5      OUnUR	                  5       UR	                  5       -  nUnUnUS:X  a  OUR                  [        Xl5      5        M  U(       d  M  S/US   -  nU H  n[        U5       H  nX   U-  X'   M     M      UR                  U5        M     UR                  5         U$ )aF  
Returns the abelian invariants for the given group.
Let ``G`` be a nontrivial finite abelian group. Then G is isomorphic to
the direct product of finitely many nontrivial cyclic groups of
prime-power order.

Explanation
===========

The prime-powers that occur as the orders of the factors are uniquely
determined by G. More precisely, the primes that occur in the orders of the
factors in any such decomposition of ``G`` are exactly the primes that divide
``|G|`` and for any such prime ``p``, if the orders of the factors that are
p-groups in one such decomposition of ``G`` are ``p^{t_1} >= p^{t_2} >= ... p^{t_r}``,
then the orders of the factors that are p-groups in any such decomposition of ``G``
are ``p^{t_1} >= p^{t_2} >= ... p^{t_r}``.

The uniquely determined integers ``p^{t_1} >= p^{t_2} >= ... p^{t_r}``, taken
for all primes that divide ``|G|`` are called the invariants of the nontrivial
group ``G`` as suggested in ([14], p. 542).

Notes
=====

We adopt the convention that the invariants of a trivial group are [].

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1])
>>> b = Permutation([1, 0, 2])
>>> G = PermutationGroup([a, b])
>>> G.abelian_invariants()
[2]
>>> from sympy.combinatorics import CyclicGroup
>>> G = CyclicGroup(7)
>>> G.abelian_invariants()
[7]

r9   r   )rO  r|   r  r   r   rs   r   r&   r    r>   r   rP  )rg   gnsinvra  r   Hgensr   r   powsrG   elmr   r   rF   rZ  s                  r0   abelian_invariants#PermutationGroup.abelian_invariants  s3   T ??Ioo aggi(AEA$C::c??C(  7;$U\2GGIqwwy(6\!/0  us58|A"1X"&'!) &  

4 + ), 	

rj   c                 h   ^ U R                   =(       a    [        U4S jU R                   5       5      $ )a  Return ``True`` if the group is elementary abelian. An elementary
abelian group is a finite abelian group, where every nontrivial
element has order `p`, where `p` is a prime.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1])
>>> G = PermutationGroup([a])
>>> G.is_elementary(2)
True
>>> a = Permutation([0, 2, 1, 3])
>>> b = Permutation([3, 1, 2, 0])
>>> G = PermutationGroup([a, b])
>>> G.is_elementary(2)
True
>>> G.is_elementary(3)
False

c              3   H   >#    U  H  oR                  5       T:H  v   M     g 7fr+   r   )r.   rG   r   s     r0   r1   1PermutationGroup.is_elementary.<locals>.<genexpr>o  s     &O!wwyA~s   ")r  r   r|   )rg   r   s    `r0   is_elementaryPermutationGroup.is_elementaryY  s#    , O3&Ot&O#OOrj   c                    U(       a!  U(       a  [        SR                  X5      5      eU R                  n[        U5      nU R	                  5       nXT:X  a  SU l        SU l        U(       + $ SU-  U:X  a  SU l        SU l        U(       + $ g)z#A naive test using the group order.z$Both {} and {} cannot be set to TrueTFr   )r  formatr7   
_factorialr   rQ   rR   )rg   only_symonly_altr   	sym_orderr   s         r0   _eval_is_alt_sym_naive'PermutationGroup._eval_is_alt_sym_naiveq  s    6+- - KKqM	

DL DL<U7i DLDL<rj   c                   ^  Ucm  T R                   nUS:  a  SnOSnU[        S5      -  [        U5      -  n[        [        U5      * U-  5      nU 4S j[        U5       5       nT R	                  US9$ U H  n[        U5      (       d  M    g   g)	a  A test using monte-carlo algorithm.

Parameters
==========

eps : float, optional
    The criterion for the incorrect ``False`` return.

perms : list[Permutation], optional
    If explicitly given, it tests over the given candidates
    for testing.

    If ``None``, it randomly computes ``N_eps`` and chooses
    ``N_eps`` sample of the permutation from the group.

See Also
========

_check_cycles_alt_sym
   g(\?g=
ףp=?r   c              3   D   >#    U  H  nTR                  5       v   M     g 7fr+   )r   )r.   rF   rg   s     r0   r1   @PermutationGroup._eval_is_alt_sym_monte_carlo.<locals>.<genexpr>  s     <|!T^^%%|    permsTF)r7   r   intr>   _eval_is_alt_sym_monte_carlor   )rg   epsr  r   c_nd_nN_epsr   s   `       r0   r  -PermutationGroup._eval_is_alt_sym_monte_carlo  s    * =A2vs1v:s1v%CS	#&E<uU|<E4454AAD$T**  rj   c                   ^ Tb)  TS   nU4S j[        U5       5       nU R                  US9$ U R                  (       d  U R                  (       a  gU R                  SL a  U R                  SL a  gU R                  nUS:  a  U R                  5       $ U R                  5       (       a  U R                  US9$ Su  U l        U l        g)	ae  Monte Carlo test for the symmetric/alternating group for degrees
>= 8.

Explanation
===========

More specifically, it is one-sided Monte Carlo with the
answer True (i.e., G is symmetric/alternating) guaranteed to be
correct, and the answer False being incorrect with probability eps.

For degree < 8, the order of the group is checked so the test
is deterministic.

Notes
=====

The algorithm itself uses some nontrivial results from group theory and
number theory:
1) If a transitive group ``G`` of degree ``n`` contains an element
with a cycle of length ``n/2 < p < n-2`` for ``p`` a prime, ``G`` is the
symmetric or alternating group ([1], pp. 81-82)
2) The proportion of elements in the symmetric/alternating group having
the property described in 1) is approximately `\log(2)/\log(n)`
([1], p.82; [2], pp. 226-227).
The helper function ``_check_cycles_alt_sym`` is used to
go over the cycles in a permutation and look for ones satisfying 1).

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> D = DihedralGroup(10)
>>> D.is_alt_sym()
False

See Also
========

_check_cycles_alt_sym

r  c              3   .   >#    U  H
  nTU   v   M     g 7fr+   r   )r.   rF   r   s     r0   r1   .PermutationGroup.is_alt_sym.<locals>.<genexpr>  s     :\LO\s   r  TF   )r  FF)r>   r  rQ   rR   r7   r  is_transitive)rg   r  r   r  r  r   s     `   r0   
is_alt_symPermutationGroup.is_alt_sym  s    T # )E:U5\:E4454AA<<4<<<<5 T\\U%:KKq5..00!!444==%1"dlrj   c                 D  ^ U R                   c  U R                  5       nU[        U5      S-
     nUR                  nU R                  n[        [        [        U5      5      5      m[        U4S jU 5       5      (       a  SU l	        SU l         gSU l         gU R                   $ )a  Test if the group is nilpotent.

Explanation
===========

A group `G` is nilpotent if it has a central series of finite length.
Alternatively, `G` is nilpotent if its lower central series terminates
with the trivial group. Every nilpotent group is also solvable
([1], p.29, [12]).

Examples
========

>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
... CyclicGroup)
>>> C = CyclicGroup(6)
>>> C.is_nilpotent
True
>>> S = SymmetricGroup(5)
>>> S.is_nilpotent
False

See Also
========

lower_central_series, is_solvable

r9   c              3   ,   >#    U  H	  oT:H  v   M     g 7fr+   r   r.   rG   r  s     r0   r1   0PermutationGroup.is_nilpotent.<locals>.<genexpr>       /$Q=$   TF)
rT   lower_central_seriesr?   r|   r7   r@   r:   r>   r   rU   )rg   lcs
terminatorr   r7   r  s        @r0   is_nilpotentPermutationGroup.is_nilpotent  s    < %++-CSX\*J((D[[FtE&M23H/$///$(!%)"%*"%%%rj   c           	         U R                  XS9(       d  gU R                  nUR                  nU R                  (       a  X4:X  d  U(       d  gU R                  (       a  gU R	                  5       nU(       dU  X4:w  aP  X4:  a&  [        UR                  [        US-
  5      /-   5      nO%[        UR                  [        US-
  5      /-   5      nUR                   Vs/ s H  ofR                  PM     nnUR                   Vs/ s H  ofR                  PM     nnU H;  n	U H2  n
[        X[        U	5      5      nUR                  US5      (       a  M1      g   M=     gs  snf s  snf )a  Test if ``G=self`` is a normal subgroup of ``gr``.

Explanation
===========

G is normal in gr if
for each g2 in G, g1 in gr, ``g = g1*g2*g1**-1`` belongs to G
It is sufficient to check this for each g1 in gr.generators and
g2 in G.generators.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([1, 2, 0])
>>> b = Permutation([1, 0, 2])
>>> G = PermutationGroup([a, b])
>>> G1 = PermutationGroup([a, Permutation([2, 0, 1])])
>>> G1.is_normal(G)
True

r  FTr9   )r  r7   rO  rO   r  	PermGroupr|   r   r   r   r
   ro  )rg   grr  d_selfd_grnew_selfr   r   r   g1g2s              r0   	is_normalPermutationGroup.is_normal  s   . 2yy??f99;&.}$X%8%8Kq<Q;R%RSr}}FQJ0G/HHI(0(;(;<(;1(;<(*616Bbjn5,,Q55   
  =6s   E=Ec                    U R                   b  U R                   $ U R                  5       SL a  gU(       a[  / nU R                  S5      n[        [	        U 5      5       H$  nUR                  U R                  SU5      5        M&     [        U5      nOU R                  S5      nUR                  5       nU HL  nUR                  5       nUS:w  d  M  [        S U R                  SU/5       5       5      (       d  ME  SU l           g   SU l         g)as  Test if a group is primitive.

Explanation
===========

A permutation group ``G`` acting on a set ``S`` is called primitive if
``S`` contains no nontrivial block under the action of ``G``
(a block is nontrivial if its cardinality is more than ``1``).

Notes
=====

The algorithm is described in [1], p.83, and uses the function
minimal_block to search for blocks of the form `\{0, k\}` for ``k``
ranging over representatives for the orbits of `G_0`, the stabilizer of
``0``. This algorithm has complexity `O(n^2)` where ``n`` is the degree
of the group, and will perform badly if `G_0` is small.

There are two implementations offered: one finds `G_0`
deterministically using the function ``stabilizer``, and the other
(default) produces random elements of `G_0` using ``random_stab``,
hoping that they generate a subgroup of `G_0` with not too many more
orbits than `G_0` (this is suggested in [1], p.83). Behavior is changed
by the ``randomized`` flag.

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> D = DihedralGroup(10)
>>> D.is_primitive()
False

See Also
========

minimal_block, random_stab

Fr   c              3   *   #    U  H	  oS :g  v   M     g7fr   Nr   r.   r   s     r0   r1   0PermutationGroup.is_primitive.<locals>.<genexpr>  s     I.H1f.H   T)rS   r  r   r>   r?   r   r   r&   
stabilizerr  r<   r;   minimal_block)	rg   r   random_stab_gensr  r   stabr  orbr   s	            r0   is_primitivePermutationGroup.is_primitiveG  s    P )%%%5(!$$Q'A3t9% ''(8(8A(>? &#$45D??1%DC	AAv#Id.@.@!Q.HIII%*"	 
 "rj   c                    S nU R                  5       (       d  g/ n/ n/ nU(       a[  / nU R                  S5      n[        [        U 5      5       H$  nUR	                  U R                  SU5      5        M&     [        U5      n	OU R                  S5      n	U	R                  5       n
U
 GH  nUR                  5       nUS:w  d  M  U R                  SU/5      nU" U5      u  p[        U R                  5       Vs1 s H  nUU   S:X  d  M  UiM     nnSnS/[        U5      -  n[        U5       Ho  u  nn[        U5      [        U5      :  a  UR                  U5      (       a  SUU'   M;  [        U5      [        U5      :  d  MU  UR                  U5      (       d  Mm  Sn  O   [        U5       VVs/ s H  u  nnUU   (       a  M  UPM     nnn[        U5       VVs/ s H  u  nnUU   (       a  M  UPM     nnn[        U5       VVs/ s H  u  nnUU   (       a  M  UPM     nnnU(       d  GM  X;  d  GM  UR	                  U5        UR	                  U5        UR	                  U5        GM     U$ s  snf s  snnf s  snnf s  snnf )a  
For a transitive group, return the list of all minimal
block systems. If a group is intransitive, return `False`.

Examples
========
>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> DihedralGroup(6).minimal_blocks()
[[0, 1, 0, 1, 0, 1], [0, 1, 2, 0, 1, 2]]
>>> G = PermutationGroup(Permutation(1,2,5))
>>> G.minimal_blocks()
False

See Also
========

minimal_block, is_transitive, is_primitive

c                     [        U 5      n0 nSnS /U-  n[        U5       H%  nX   U;  a  X2X   '   X4U'   US-  nM  X U      XE'   M'     [        U5      U4$ )Nr   r9   )r?   r>   r  )blocksr   appearedr  r  rF   s         r0   _number_blocks7PermutationGroup.minimal_blocks.<locals>._number_blocks  sr    
 FAHAqA1X9H,*+VY'aDFA#1I.AD  8Q;rj   Fr   T)r  r   r>   r?   r   r   r&   r  r  r<   r	  r7   r  issubset)rg   r   r  r  
num_blocks
rep_blocksr
  r  rF   r  r  r  r   block	num_blockr   rZ  r   minimalblocks_remove_maskr   r  r   s                          r0   minimal_blocksPermutationGroup.minimal_blocks  s6   *	$ !!##

!$$Q'A3t9% ''(8(8A(>? &#$45D??1%DC	AAv**Aq62-e4	"'"4J"4Q	!8Iq"4J &+Ws6{%:"%j1DAq1vC(S\\!__04*1-Q#c(*qzz#"' 2 )2&(9W(91ASTUAV!(9W,5j,A_,ADAqI[\]I^a,A
_,5j,A_,ADAqI[\]I^a,A
_7y:MM%(%%i0%%c*7 8 - K X__s0   5I-I-+I2 I2I8,I8I>I>c                 f  ^ U R                   c  U R                  5       S-  S:w  a  gU R                  5       nU[        U5      S-
     nUR                  nU R
                  n[        [        [        U5      5      5      m[        U4S jU 5       5      (       a  SU l         gSU l         gU R                   $ )a.  Test if the group is solvable.

``G`` is solvable if its derived series terminates with the trivial
group ([1], p.29).

Examples
========

>>> from sympy.combinatorics.named_groups import SymmetricGroup
>>> S = SymmetricGroup(3)
>>> S.is_solvable
True

See Also
========

is_nilpotent, derived_series

r   r   Tr9   c              3   ,   >#    U  H	  oT:H  v   M     g 7fr+   r   r  s     r0   r1   /PermutationGroup.is_solvable.<locals>.<genexpr>  r  r  F)
rU   r   r   r?   r|   r7   r@   r:   r>   r   )rg   dsr  r   r7   r  s        @r0   is_solvablePermutationGroup.is_solvable  s    * $zz|a1$$$&BCGaKJ((D[[FtE&M23H/$///$(!$)!$$$rj   c                   ^^ [        T[        5      (       a  U R                  TR                  :w  a  gg[        T[        5      (       d  gU T:X  d  U R                  S   [        5       :X  a  gTR                  5       U R                  5       -  S:w  a  gU R                  TR                  :X  d!  U R                  TR                  :  a  T(       d  U R                  nOg[        UU4S jU 5       5      $ )aF  Return ``True`` if all elements of ``self`` belong to ``G``.

If ``strict`` is ``False`` then if ``self``'s degree is smaller
than ``G``'s, the elements will be resized to have the same degree.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> from sympy.combinatorics import SymmetricGroup, CyclicGroup

Testing is strict by default: the degree of each group must be the
same:

>>> p = Permutation(0, 1, 2, 3, 4, 5)
>>> G1 = PermutationGroup([Permutation(0, 1, 2), Permutation(0, 1)])
>>> G2 = PermutationGroup([Permutation(0, 2), Permutation(0, 1, 2)])
>>> G3 = PermutationGroup([p, p**2])
>>> assert G1.order() == G2.order() == G3.order() == 6
>>> G1.is_subgroup(G2)
True
>>> G1.is_subgroup(G3)
False
>>> G3.is_subgroup(PermutationGroup(G3[1]))
False
>>> G3.is_subgroup(PermutationGroup(G3[0]))
True

To ignore the size, set ``strict`` to ``False``:

>>> S3 = SymmetricGroup(3)
>>> S5 = SymmetricGroup(5)
>>> S3.is_subgroup(S5, strict=False)
True
>>> C7 = CyclicGroup(7)
>>> G = S5*C7
>>> S5.is_subgroup(G, False)
True
>>> C7.is_subgroup(G, 0)
False

FTr   c              3   D   >#    U  H  nTR                  UTS 9v   M     g7f)r  N)rs   )r.   rG   ra  r  s     r0   r1   /PermutationGroup.is_subgroup.<locals>.<genexpr>9	  s     >A1::a:/r  )r,   SymmetricPermutationGroupr7   r&   r|   r   r   r   )rg   ra  r  r   s    `` r0   r  PermutationGroup.is_subgroup  s    V a233{{ahh&!-..19*KM9779tzz|#q(;;!(("qxx'??D>>>>rj   c                     U R                   $ )a  Return ``True`` if a group is polycyclic. A group is polycyclic if
it has a subnormal series with cyclic factors. For finite groups,
this is the same as if the group is solvable.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1, 3])
>>> b = Permutation([2, 0, 1, 3])
>>> G = PermutationGroup([a, b])
>>> G.is_polycyclic
True

)r"  rw   s    r0   is_polycyclicPermutationGroup.is_polycyclic;	  s    " rj   c                 H   U R                   (       a  U R                   $ U(       aH  U R                   b  U R                   $ [        U R                  S5      5      U R                  :H  nX l         U$ SnU R	                  5        H  n[        U5      S:  d  M  U(       a    gSnM!     U$ )a  Test if the group is transitive.

Explanation
===========

A group is transitive if it has a single orbit.

If ``strict`` is ``False`` the group is transitive if it has
a single orbit of length different from 1.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1, 3])
>>> b = Permutation([2, 0, 1, 3])
>>> G1 = PermutationGroup([a, b])
>>> G1.is_transitive()
False
>>> G1.is_transitive(strict=False)
True
>>> c = Permutation([2, 3, 0, 1])
>>> G2 = PermutationGroup([a, c])
>>> G2.is_transitive()
True
>>> d = Permutation([1, 0, 2, 3])
>>> e = Permutation([0, 1, 3, 2])
>>> G3 = PermutationGroup([d, e])
>>> G3.is_transitive() or G3.is_transitive(strict=False)
False

r   Fr9   T)rP   r?   rX  r7   r  )rg   r  ansgot_orbr   s        r0   r  PermutationGroup.is_transitiveN	  s    B &&&"".***djjm$3C"%JA1vz 	 
 rj   c                     U R                   c)  [        U 5      S:H  =(       a    U S   R                  U l         U R                   $ )a  Test if the group is the trivial group.

This is true if the group contains only the identity permutation.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> G = PermutationGroup([Permutation([0, 1, 2])])
>>> G.is_trivial
True

r9   r   )rV   r?   is_Identityrw   s    r0   rO  PermutationGroup.is_trivial	  s;     #"4yA~E$q'2E2EDrj   c                     U /nU nU R                  X5      nUR                  U5      (       d<  UR                  U5        UnU R                  X5      nUR                  U5      (       d  M<  U$ )a  Return the lower central series for the group.

The lower central series for a group `G` is the series
`G = G_0 > G_1 > G_2 > \ldots` where
`G_k = [G, G_{k-1}]`, i.e. every term after the first is equal to the
commutator of `G` and the previous term in `G1` ([1], p.29).

Returns
=======

A list of permutation groups in the order `G = G_0, G_1, G_2, \ldots`

Examples
========

>>> from sympy.combinatorics.named_groups import (AlternatingGroup,
... DihedralGroup)
>>> A = AlternatingGroup(4)
>>> len(A.lower_central_series())
2
>>> A.lower_central_series()[1].is_subgroup(DihedralGroup(2))
True

See Also
========

commutator, derived_series

)rg  r  r   r  s       r0   r  %PermutationGroup.lower_central_series	  sg    < food,%%c**JJsOG//$0C %%c** 
rj   c                     U R                   b  U R                   $ U R                  nUS:X  a  g[         H  nX-  S:X  d  M  X-  nX0l         Us  $    g)aR  Maximum proper divisor of the degree of a permutation group.

Explanation
===========

Obviously, this is the degree divided by its minimal proper divisor
(larger than ``1``, if one exists). As it is guaranteed to be prime,
the ``sieve`` from ``sympy.ntheory`` is used.
This function is also used as an optimization tool for the functions
``minimal_block`` and ``_union_find_merge``.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> G = PermutationGroup([Permutation([0, 2, 1, 3])])
>>> G.max_div
2

See Also
========

minimal_block, _union_find_merge

Nr9   r   )rX   r7   r   )rg   r   r   r  s       r0   r   PermutationGroup.max_div	  sO    6 ==$== KK6AuzD !	 rj   c                    U R                  5       (       d  gU R                  nU R                  n[        [	        U5      5      nS/U-  n/ n[        U5      nXpR                  :  a  S/U-  $ [	        US-
  5       H&  nUS   XAUS-      '   UR                  XS-      5        M(     XuUS   '   SnUS-
  n	X:  a\  Xh   n
US-  nU HF  nU R                  X5      nU R                  U" U
5      U" U5      UXF5      nUS:X  a  S/U-  s  $ X-  n	MH     X:  a  M\  [	        U5       H  nU R                  X5        M     0 n[        U5       VVs/ s H  u  pUR                  X5      PM     snn$ s  snnf )a  For a transitive group, finds the block system generated by
``points``.

Explanation
===========

If a group ``G`` acts on a set ``S``, a nonempty subset ``B`` of ``S``
is called a block under the action of ``G`` if for all ``g`` in ``G``
we have ``gB = B`` (``g`` fixes ``B``) or ``gB`` and ``B`` have no
common points (``g`` moves ``B`` entirely). ([1], p.23; [6]).

The distinct translates ``gB`` of a block ``B`` for ``g`` in ``G``
partition the set ``S`` and this set of translates is known as a block
system. Moreover, we obviously have that all blocks in the partition
have the same size, hence the block size divides ``|S|`` ([1], p.23).
A ``G``-congruence is an equivalence relation ``~`` on the set ``S``
such that ``a ~ b`` implies ``g(a) ~ g(b)`` for all ``g`` in ``G``.
For a transitive group, the equivalence classes of a ``G``-congruence
and the blocks of a block system are the same thing ([1], p.23).

The algorithm below checks the group for transitivity, and then finds
the ``G``-congruence generated by the pairs ``(p_0, p_1), (p_0, p_2),
..., (p_0,p_{k-1})`` which is the same as finding the maximal block
system (i.e., the one with minimum block size) such that
``p_0, ..., p_{k-1}`` are in the same block ([1], p.83).

It is an implementation of Atkinson's algorithm, as suggested in [1],
and manipulates an equivalence relation on the set ``S`` using a
union-find data structure. The running time is just above
`O(|points||S|)`. ([1], pp. 83-87; [7]).

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> D = DihedralGroup(10)
>>> D.minimal_block([0, 5])
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
>>> D.minimal_block([0, 1])
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

See Also
========

_union_find_rep, _union_find_merge, is_transitive, is_primitive

Fr9   r   r   )r  r7   r|   r:   r>   r?   r   r   r   r   r  
setdefault)rg   pointsr   r   r   r   r   r   rF   len_not_repr   r   deltar   new_repsr   s                   r0   r	  PermutationGroup.minimal_block	  s   ` !!##KKuQx.AK||3q5Lq1uA%+AYG1q5M"NN6a%=)  fQi!eoJEFA ,,U< --c%j#e*e.5@2:3q5L#  o qA   ,  6?6HI6Hda##A)6HIIIs   E&c                    U1nUn[        U5      S:  aj  [        5       nU H6  nU R                   H#  nXe-  U) -  nXr;  d  M  UR                  U5        M%     M8     UR	                  U5        Un[        U5      S:  a  Mj  U$ )a  Return the conjugacy class of an element in the group.

Explanation
===========

The conjugacy class of an element ``g`` in a group ``G`` is the set of
elements ``x`` in ``G`` that are conjugate with ``g``, i.e. for which

    ``g = xax^{-1}``

for some ``a`` in ``G``.

Note that conjugacy is an equivalence relation, and therefore that
conjugacy classes are partitions of ``G``. For a list of all the
conjugacy classes of the group, use the conjugacy_classes() method.

In a permutation group, each conjugacy class corresponds to a particular
`cycle structure': for example, in ``S_3``, the conjugacy classes are:

    * the identity class, ``{()}``
    * all transpositions, ``{(1 2), (1 3), (2 3)}``
    * all 3-cycles, ``{(1 2 3), (1 3 2)}``

Examples
========

>>> from sympy.combinatorics import Permutation, SymmetricGroup
>>> S3 = SymmetricGroup(3)
>>> S3.conjugacy_class(Permutation(0, 1, 2))
{(0 1 2), (0 2 1)}

Notes
=====

This procedure computes the conjugacy class directly by finding the
orbit of the element under conjugation in G. This algorithm is only
feasible for permutation groups of relatively small order, but is like
the orbit() function itself in that respect.
r   )r?   r{   r|   r  update)rg   r   	new_classlast_iterationthis_iterationr   rv  
conjugateds           r0   conjugacy_class PermutationGroup.conjugacy_class:
  s    T C	".!A% UN#A!"1"J!2&**:6 ) $ ^,+N .!A% rj   c                    [        [        [        U R                  5      5      5      nU1nUR	                  5       /nU R                  5        H=  nXB;  d  M
  U R                  U5      nUR                  U5        UR                  U5        M?     U$ )a  Return the conjugacy classes of the group.

Explanation
===========

As described in the documentation for the .conjugacy_class() function,
conjugacy is an equivalence relation on a group G which partitions the
set of elements. This method returns a list of all these conjugacy
classes of G.

Examples
========

>>> from sympy.combinatorics import SymmetricGroup
>>> SymmetricGroup(3).conjugacy_classes()
[{(2)}, {(0 1 2), (0 2 1)}, {(0 2), (1 2), (2)(0 1)}]

)	r@   r:   r>   r7   r  r  rD  r   r?  )rg   r  known_elementsclassesr   r@  s         r0   conjugacy_classes"PermutationGroup.conjugacy_classesv
  s{    & 4dkk 234"!&&()A& 003	y)%%i0	 ! rj   c                   ^ [        US5      (       Ga  U R                  n[        [        [	        U5      5      5      m[        U4S jUR                   5       5      (       a  U$ [        UR                  SS 5      nUR                  5       u  pV[        XV5      n[        XW5      u  pU R                  SSS9  Sn
U
(       GaI  UR                  SSS9  [	        U5       H  nU R                  5       nUR                  5       nX-  n[        XX5      nUS   T:w  d  US	   [        U5      S	-   :w  d  MS  UR                  nUR                  U5        [        U5      nUR                  U5        UR                  XV5      u  nnUUpe[        XV5      n[        UU5      u  pM     S
n
U R                   HR  nUR                   H6  nX-  n[        XUU	5      nUS   T:w  d  US	   [        U5      S	-   :w  d  M4  Sn
  O   U
(       d  MR    O   U
(       a  GMI  U$ [        US5      (       a  U R!                  [        U5      5      $ [        US5      (       a  U R!                  [        U/5      5      $ g)a  Return the normal closure of a subgroup/set of permutations.

Explanation
===========

If ``S`` is a subset of a group ``G``, the normal closure of ``A`` in ``G``
is defined as the intersection of all normal subgroups of ``G`` that
contain ``A`` ([1], p.14). Alternatively, it is the group generated by
the conjugates ``x^{-1}yx`` for ``x`` a generator of ``G`` and ``y`` a
generator of the subgroup ``\left\langle S\right\rangle`` generated by
``S`` (for some chosen generating set for ``\left\langle S\right\rangle``)
([1], p.73).

Parameters
==========

other
    a subgroup/list of permutations/single permutation
k
    an implementation-specific parameter that determines the number
    of conjugates that are adjoined to ``other`` at once

Examples
========

>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
... CyclicGroup, AlternatingGroup)
>>> S = SymmetricGroup(5)
>>> C = CyclicGroup(5)
>>> G = S.normal_closure(C)
>>> G.order()
60
>>> G.is_subgroup(AlternatingGroup(5))
True

See Also
========

commutator, derived_subgroup, random_pr

Notes
=====

The algorithm is described in [1], pp. 73-74; it makes use of the
generation of random elements for permutation groups by the product
replacement algorithm.

r|   c              3   ,   >#    U  H	  oT:H  v   M     g 7fr+   r   r  s     r0   r1   2PermutationGroup.normal_closure.<locals>.<genexpr>
  s     ;*:Q=*:r  N
      )r   r   Tr   r9   Frn   rM  )rN  r7   r@   r:   r>   r   r|   r&   rQ  r   r   r   r   r   r?   r   r`  )rg   r}   r   r7   Zr   r   r   r   r   _loopr   rG   r  conjrh  r   	temp_basetemp_strong_gensr  s                      @r0   r`  PermutationGroup.normal_closure
  sN   b 5,''[[FtE&M23H;%*:*:;;; !1!1!!45A ! ; ; =D 8 K.tG -L   2 ,E!!B"!-qA(AA3D \NC1v)SVs4y1}-D ||D),T2#**4077J 4	#3,57Gk4TG * ;4 13 9&8 "$ A\\ s$T%79q6X-Q3t9q=1H$(E! * u )+ %> HUM**&&'7'>??UL))&&'7'@AA *rj   c                 D    [        U R                  U R                  X5      $ )a  Compute the orbit of alpha `\{g(\alpha) | g \in G\}` as a set.

Explanation
===========

The time complexity of the algorithm used here is `O(|Orb|*r)` where
`|Orb|` is the size of the orbit and ``r`` is the number of generators of
the group. For a more detailed analysis, see [1], p.78, [2], pp. 19-21.
Here alpha can be a single point, or a list of points.

If alpha is a single point, the ordinary orbit is computed.
if alpha is a list of points, there are three available options:

'union' - computes the union of the orbits of the points in the list
'tuples' - computes the orbit of the list interpreted as an ordered
tuple under the group action ( i.e., g((1,2,3)) = (g(1), g(2), g(3)) )
'sets' - computes the orbit of the list interpreted as a sets

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([1, 2, 0, 4, 5, 6, 3])
>>> G = PermutationGroup([a])
>>> G.orbit(0)
{0, 1, 2}
>>> G.orbit([0, 4], 'union')
{0, 1, 2, 3, 4, 5, 6}

See Also
========

orbit_transversal

)r   r7   r|   )rg   r+  actions      r0   rX  PermutationGroup.orbit
  s    H dkk4??EBBrj   c                    Uc  U R                  U5      nX2   c  gX2   nU R                   Vs/ s H  oUR                  PM     nn/ nUS:w  a2  UR                  Xd   5        Xd   R	                  U5      nX2   nUS:w  a  M2  U(       a  [        [        U6 5      $ [        [        [        U R                  5      5      5      $ s  snf )a  Return a group element which sends ``alpha`` to ``beta``.

Explanation
===========

If ``beta`` is not in the orbit of ``alpha``, the function returns
``False``. This implementation makes use of the schreier vector.
For a proof of correctness, see [1], p.80

Examples
========

>>> from sympy.combinatorics.named_groups import AlternatingGroup
>>> G = AlternatingGroup(5)
>>> G.orbit_rep(0, 4)
(0 4 1 2 3)

See Also
========

schreier_vector

Fr   )
r   r|   r   r   r   r@   r   r:   r>   r]   )rg   r+  r,  r   r   r   r   r/   s           r0   	orbit_repPermutationGroup.orbit_rep   s    0 ""2259O (!'+7!72gHHTW7==&D%A 2g 9a=))4dll 3455 8s   B=c                 D    [        U R                  U R                  X5      $ )a  Computes a transversal for the orbit of ``alpha`` as a set.

Explanation
===========

For a permutation group `G`, a transversal for the orbit
`Orb = \{g(\alpha) | g \in G\}` is a set
`\{g_\beta | g_\beta(\alpha) = \beta\}` for `\beta \in Orb`.
Note that there may be more than one possible transversal.
If ``pairs`` is set to ``True``, it returns the list of pairs
`(\beta, g_\beta)`. For a proof of correctness, see [1], p.79

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> G = DihedralGroup(6)
>>> G.orbit_transversal(0)
[(5), (0 1 2 3 4 5), (0 5)(1 4)(2 3), (0 2 4)(1 3 5), (5)(0 4)(1 3), (0 3)(1 4)(2 5)]

See Also
========

orbit

)_orbit_transversalr]   r|   )rg   r+  r:  s      r0   rS  "PermutationGroup.orbit_transversalH  s    6 "$,,NNrj   c                 B    [        U R                  U R                  5      $ )aD  Return the orbits of ``self``, ordered according to lowest element
in each orbit.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation(1, 5)(2, 3)(4, 0, 6)
>>> b = Permutation(1, 5)(3, 4)(2, 6, 0)
>>> G = PermutationGroup([a, b])
>>> G.orbits()
[{0, 2, 3, 4, 6}, {1, 5}]
)_orbitsr]   rK   )rg   r   s     r0   r  PermutationGroup.orbitse  s     t||T%5%566rj   c                    U R                   b  U R                   $ U R                  (       a(  U R                  n[        U5      U l         U R                   $ U R                  (       a+  U R                  n[        U5      S-  U l         U R                   $ [        U R                   Vs/ s H  n[        U5      PM     sn5      nX0l         U$ s  snf )at  Return the order of the group: the number of permutations that
can be generated from elements of the group.

The number of permutations comprising the group is given by
``len(group)``; the length of each permutation in the group is
given by ``group.size``.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup

>>> a = Permutation([1, 0, 2])
>>> G = PermutationGroup([a])
>>> G.degree
3
>>> len(G)
1
>>> G.order()
2
>>> list(G.generate())
[(2), (2)(0 1)]

>>> a = Permutation([0, 2, 1])
>>> b = Permutation([1, 0, 2])
>>> G = PermutationGroup([a, b])
>>> G.order()
6

See Also
========

degree

r   )rL   rQ   r]   r   rR   r   r   r?   )rg   r   r   r  s       r0   r   PermutationGroup.orderu  s    H ;;";;<<A#A,DK;;<<A#A,q.DK;;$"9"9:"9Q#a&"9:; ;s   "Cc                 r    UR                  U 5      (       a!  U R                  5       UR                  5       -  $ g)a   
Returns the index of a permutation group.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation(1,2,3)
>>> b =Permutation(3)
>>> G = PermutationGroup([a])
>>> H = PermutationGroup([b])
>>> G.index(H)
3

N)r  r   )rg   r   s     r0   r   PermutationGroup.index  s/      ==::<** rj   c                 ~   U R                   nUb  U$ U R                  nUS:  a  U R                  5       (       ag  U R                  5       nU(       aA  [	        S U R
                   5       5      (       a  Su  U l         U l        gSu  U l         U l        gU R                  SS9$ Su  U l         U l        gU R                  SS9$ )	a  Return ``True`` if the group is symmetric.

Examples
========

>>> from sympy.combinatorics import SymmetricGroup
>>> g = SymmetricGroup(5)
>>> g.is_symmetric
True

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> g = PermutationGroup(
...     Permutation(0, 1, 2, 3, 4),
...     Permutation(2, 3))
>>> g.is_symmetric
True

Notes
=====

This uses a naive test involving the computation of the full
group order.
If you need more quicker taxonomy for large groups, you can use
:meth:`PermutationGroup.is_alt_sym`.
However, :meth:`PermutationGroup.is_alt_sym` may not be accurate
and is not able to distinguish between an alternating group and
a symmetric group.

See Also
========

is_alt_sym
r  c              3   8   #    U  H  oR                   v   M     g 7fr+   )is_oddr   s     r0   r1   0PermutationGroup.is_symmetric.<locals>.<genexpr>  s     =_88_r6   TFTFTF)r  r  )rQ   r7   r  r  r;   r|   rR   r  )rg   rQ   r   _is_alt_syms       r0   is_symmetricPermutationGroup.is_symmetric  s    F ,,NKK6!!##"??A=T__===5@2dl#1<.DL$, 22D2AA)5&DL$,**D*99rj   c                 ~   U R                   nUb  U$ U R                  nUS:  a  U R                  5       (       ag  U R                  5       nU(       aA  [	        S U R
                   5       5      (       a  Su  U l        U l         gSu  U l        U l         gU R                  SS9$ Su  U l        U l         gU R                  SS9$ )	a  Return ``True`` if the group is alternating.

Examples
========

>>> from sympy.combinatorics import AlternatingGroup
>>> g = AlternatingGroup(5)
>>> g.is_alternating
True

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> g = PermutationGroup(
...     Permutation(0, 1, 2, 3, 4),
...     Permutation(2, 3, 4))
>>> g.is_alternating
True

Notes
=====

This uses a naive test involving the computation of the full
group order.
If you need more quicker taxonomy for large groups, you can use
:meth:`PermutationGroup.is_alt_sym`.
However, :meth:`PermutationGroup.is_alt_sym` may not be accurate
and is not able to distinguish between an alternating group and
a symmetric group.

See Also
========

is_alt_sym
r  c              3   8   #    U  H  oR                   v   M     g 7fr+   is_evenr   s     r0   r1   2PermutationGroup.is_alternating.<locals>.<genexpr>"  s     >o99or6   rk  Trj  F)r  r  )rR   r7   r  r  r   r|   rQ   r  )rg   rR   r   rl  s       r0   is_alternatingPermutationGroup.is_alternating  s    F ,,NKK6!!##"??A>doo>>>5@2dl#1<.DL$, 22D2AA)5&DL$,**D*99rj   c                     [        U5      n[        U5      n[        U5       H)  n[        US-   U5       H  nX   X   -  S:X  d  M      g   M+     g)zCSubroutine to test if there is only one cyclic group for the
order.r9   NT)r  r?   r>   )rC   primesr   rF   rZ  s        r0   _distinct_primes_lemma'PermutationGroup._distinct_primes_lemma0  sQ     KqA1Q3]9vy(A- #  rj   c                   ^ ^ T R                   b  T R                   $ [        T R                  5      S:X  a  ST l         ST l        gT R                  SL a  ST l         gT R	                  5       mTS:  a  ST l        TS:w  a  ST l         g[        T5      n[        S UR                  5        5       5      (       aY  T R                  (       a  ST l         g[        UR                  5       5      n[        R                  U5      SL a  ST l         ST l        gT R                  (       d  ST l         g[        UU 4S jUR                  5        5       5      T l         T R                   $ )a1  
Return ``True`` if the group is Cyclic.

Examples
========

>>> from sympy.combinatorics.named_groups import AbelianGroup
>>> G = AbelianGroup(3, 4)
>>> G.is_cyclic
True
>>> G = AbelianGroup(4, 4)
>>> G.is_cyclic
False

Notes
=====

If the order of a group $n$ can be factored into the distinct
primes $p_1, p_2, \dots , p_s$ and if

.. math::
    \forall i, j \in \{1, 2, \dots, s \}:
    p_i \not \equiv 1 \pmod {p_j}

holds true, there is only one group of the order $n$ which
is a cyclic group [1]_. This is a generalization of the lemma
that the group of order $15, 35, \dots$ are cyclic.

And also, these additional lemmas can be used to test if a
group is cyclic if the order of the group is already found.

- If the group is abelian and the order of the group is
  square-free, the group is cyclic.
- If the order of the group is less than $6$ and is not $4$, the
  group is cyclic.
- If the order of the group is prime, the group is cyclic.

References
==========

.. [1] 1978: John S. Rose: A Course on Group Theory,
    Introduction to Finite Group Theory: 1.4
r9   TF      c              3   *   #    U  H	  oS :H  v   M     g7f)r9   Nr   )r.   r  s     r0   r1   -PermutationGroup.is_cyclic.<locals>.<genexpr>~  s     0/!Av/r  c              3   |   >^#    U  H0  u  moS :  d  M  [        UUU4S jTR                   5       5      v   M2     g7f)r9   c              3   L   >#    U  H  oTT-  -  TR                   :g  v   M     g 7fr+   )r  )r.   rG   r   r   rg   s     r0   r1   7PermutationGroup.is_cyclic.<locals>.<genexpr>.<genexpr>  s      H1E1H.s   !$N)r;   r|   )r.   r   r   r   rg   s     @r0   r1   r~    s5      
'1q5 ICHHHH's   <(<)rZ   r?   r|   rO   r   r   r   r  r:   keysr&   rx  r  r   )rg   rm  rw  r   s   `  @r0   	is_cyclicPermutationGroup.is_cyclic<  s&   Z ??&??"t1$"DO#Du$#DO

19#Dz"&E"0w~~/000"&',,.)F66v>$F"&#' #DO 

 
 rj   c                    U R                   b  U R                   $ U R                  5       nUS-  S:X  a  SU l         gUS:X  a  SU l         gUS:X  a"  U R                  (       + U l         U R                   $ U R                  (       a  SU l         gUS-  nU R                  n[        U5      S:X  af  Uu  pEUR                  5       UR                  5       pvXg:  a  XTXv4u  pEpgUSs=:X  a  U:X  a  O  OSU l         gXb:X  a  US:X  a  XT-  U-  U) :X  a  SU l         g/ / pU R                   HD  n
U
R                  5       nUS:X  a  UR                  U
5        M,  X:X  d  M3  U	R                  U
5        MF     [        U5      US-   US-  -
  :w  a  SU l         gU	(       d  SU l         gU	S   nUS   nUS-  S:X  a  XTUS-  -  :X  a  US   nXT-  U-  U) :H  U l         U R                   $ )aQ  
Return ``True`` if the group is dihedral.

Examples
========

>>> from sympy.combinatorics.perm_groups import PermutationGroup
>>> from sympy.combinatorics.permutations import Permutation
>>> from sympy.combinatorics.named_groups import SymmetricGroup, CyclicGroup
>>> G = PermutationGroup(Permutation(1, 6)(2, 5)(3, 4), Permutation(0, 1, 2, 3, 4, 5, 6))
>>> G.is_dihedral
True
>>> G = SymmetricGroup(3)
>>> G.is_dihedral
True
>>> G = CyclicGroup(6)
>>> G.is_dihedral
False

References
==========

.. [Di1] https://math.stackexchange.com/questions/827230/given-a-cayley-table-is-there-an-algorithm-to-determine-if-it-is-a-dihedral-gro/827273#827273
.. [Di2] https://kconrad.math.uconn.edu/blurbs/grouptheory/dihedral.pdf
.. [Di3] https://kconrad.math.uconn.edu/blurbs/grouptheory/dihedral2.pdf
.. [Di4] https://en.wikipedia.org/wiki/Dihedral_group
r   r9   FTr|  r   )r[   r   r  r  r|   r?   r	  r   )rg   r   r   r   r   r   r/   r  order_2order_nr   r   s               r0   is_dihedralPermutationGroup.is_dihedral  s   : ($$$

19> %DA: $DA:$(NN 2D$$$?? %D QJ t9>DA779aggiqu1Z
aA{{$(!v!q&QSUqb[$(! rA	AAvq!q!  w<1q5AE?* %D %DAJ AJq5A:!1a4y.
ASUqb[   rj   c                    U(       a  U R                  US9u  p4/ nU R                  nU H2  nU Vs/ s H
  o" U5      PM     snU:X  d  M!  UR                  U5        M4     U(       d  [        [	        [        U5      5      5      n[        U5      $ U R                  n	U R                  nU H  n
[        XiU
5      n	M     [        U	5      $ s  snf )aW  Return the pointwise stabilizer for a set of points.

Explanation
===========

For a permutation group `G` and a set of points
`\{p_1, p_2,\ldots, p_k\}`, the pointwise stabilizer of
`p_1, p_2, \ldots, p_k` is defined as
`G_{p_1,\ldots, p_k} =
\{g\in G | g(p_i) = p_i \forall i\in\{1, 2,\ldots,k\}\}` ([1],p20).
It is a subgroup of `G`.

Examples
========

>>> from sympy.combinatorics.named_groups import SymmetricGroup
>>> S = SymmetricGroup(7)
>>> Stab = S.pointwise_stabilizer([2, 3, 5])
>>> Stab.is_subgroup(S.stabilizer(2).stabilizer(3).stabilizer(5))
True

See Also
========

stabilizer, schreier_sims_incremental

Notes
=====

When incremental == True,
rather than the obvious implementation using successive calls to
``.stabilizer()``, this uses the incremental Schreier-Sims algorithm
to obtain a base with starting segment - the given points.

r   )	rQ  r7   r   r@   r:   r>   r&   rK   _stabilizer)rg   r9  incrementalr   r   	stab_gensr7   r   rY  r   r   s              r0   pointwise_stabilizer%PermutationGroup.pointwise_stabilizer  s    H  $ > >F > KDI[[F",23F5CJF3v=$$S) # #Dv$78	#I..##D[[F"63 %% 4s   Cc                 n   [        U5      (       a  Ub  [        S5      e[        U5      Up!O [        U5      n[        U5      n[        [        [        U R                  5      5      5      n[        U 5      n[        U5       H  nX" U5         n[        XG5      nM     U$ ! [         a    [        S5      ef = f)am  
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, PermutationGroup
>>> a, b = [Permutation([1, 0, 3, 2]), Permutation([1, 3, 0, 2])]
>>> G = PermutationGroup([a, b])
>>> G.make_perm(1, [0])
(0 1)(2 3)
>>> G.make_perm(3, [0, 1, 0])
(0 2 3 1)
>>> G.make_perm([0, 1, 0])
(0 2 3 1)

See Also
========

random
z'If n is a sequence, seed should be Nonez#n must be an integer or a sequence.)r#   r  r?   r  rq   r   r   r:   r>   r7   r   )rg   r   seedrandomrangeresultr  r   r   s           r0   	make_permPermutationGroup.make_perm)  s    @ q>> !JKK!fatHF !& T%"456IqA[^$A&_F    H !FGGHs   B B4c                 V    [        U R                  5       5      nU R                  X!5      $ )z&Return a random group element
        )r   r   r  )rg   r  rz  s      r0   randomPermutationGroup.random\  s%     &  **rj   c                    U R                   / :X  a  U R                  X5        U R                   n[        U5      S-
  nUc>  [        U5      n[        US-
  5      nXv:X  a  US-
  n[	        SS/5      n[	        SS/5      n	OUS   nUS   nXv:X  a  US-
  nUS   nUS   n	US:X  a/  [        XF   [        XG   U	5      5      XF'   [        XE   XF   5      XE'   O.[        [        XG   U	5      XF   5      XF'   [        XF   XE   5      XE'   [        XE   5      $ )aq  Return a random group element using product replacement.

Explanation
===========

For the details of the product replacement algorithm, see
``_random_pr_init`` In ``random_pr`` the actual 'product replacement'
is performed. Notice that if the attribute ``_random_gens``
is empty, it needs to be initialized by ``_random_pr_init``.

See Also
========

_random_pr_init

r9   r   r   rv  r  r   r   )rd   r   r?   r   r   r   r   r@   )
rg   	gen_count
iterationsr   r   r   rv  r  r   r   s
             r0   r   PermutationGroup.random_prb  s!   " "  7''q  !A!a% AvE1vAAwAS!AS!AvES!AS!A6%kngkna6PQKN%knknEKN%gkna&@+.QKN%knknEKN{~&&rj   c                     Uc  U R                  U5      nUc  U R                  5       nOUS   nU" U5      nU R                  XU5      n[        U) U5      $ )zRandom element from the stabilizer of ``alpha``.

The schreier vector for ``alpha`` is an optional argument used
for speeding up repeated calls. The algorithm is described in [1], p.81

See Also
========

random_pr, orbit_rep

rand)r   r   rZ  r   )rg   r+  r   r   r  r,  r  s          r0   r   PermutationGroup.random_stab  s]     ""2259O>>#D'DE{NN58QB~rj   c                 H    U R                   (       a  gU R                  5         g)ax  Schreier-Sims algorithm.

Explanation
===========

It computes the generators of the chain of stabilizers
`G > G_{b_1} > .. > G_{b1,..,b_r} > 1`
in which `G_{b_1,..,b_i}` stabilizes `b_1,..,b_i`,
and the corresponding ``s`` cosets.
An element of the group can be written as the product
`h_1*..*h_s`.

We use the incremental Schreier-Sims algorithm.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([0, 2, 1])
>>> b = Permutation([1, 0, 2])
>>> G = PermutationGroup([a, b])
>>> G.schreier_sims()
>>> G.basic_transversals
[{0: (2)(0 1), 1: (2), 2: (1 2)},
 {0: (2), 2: (0 2)}]
N)rb   r
  rw   s    r0   r   PermutationGroup.schreier_sims  s    6 rj   c           	         U R                  USS9nUS S u  pXl        X0l        US   U l        U(       d  / U l        / U l        g [        X5      n[        UUSS9u  pVn[        U5       H9  u  pXH   n
U	 H*  nX    Vs/ s H  oR                  X   5      PM     snX'   M,     M;     X`l        U Vs/ s H  n[        U5      PM     snU l        Xpl        g s  snf s  snf )NT)r   slp_dictr   )ru  )rQ  r^   r_   r`   rb   ra   r   r   r  r   r  rc   )rg   r   schreierr   r   r   r   slpsrF   ru  r   r   rv  r   s                 r0   r
  PermutationGroup._schreier_sims  s    11td1K$RaL
' (!#D!#D4TG+I$!t,-(D  oFA$'D>AfEf++DG4fE  &
 *1=>AfQi> $	 F ?s   <C/Cc                 0	  ^& Uc  / nUc  U R                   SS nU R                  n[        [        U5      5      n[	        U5      S:X  a+  US   R
                  (       a  U(       a  XUS   US   /04$ X4$ USS USS pvU Vs/ s H  oR
                  (       a  M  UPM     nnU HP  m&[        U&4S jU 5       5      (       d  M  U H  n	T&R                  U	   U	:w  d  M    O    eUR                  U	5        MR     [        Xg5      n
/ n0 n0 n0 n[	        U5      n[        U5       HO  n[        XJU   UU   SSSS9u  UU'   UU'   [        UU   5      UU'   [        UU   R                  5       5      UU'   MQ     US-
  nUS:  Ga  Sn0 n[        UU   R                  5       5       GH  u  nn[        U
U   5       GH  u  nm&T&R                  U   nUU   U   n[        T&R                  U5      nUU   U    Vs/ s H  nUU4PM	     nnUU4/U-   nUU:w  GaO  Sn UU   n[        UU5      nUU   U   SS nUR%                  5         U Vs/ s H  nUU44PM
     nnUU-   n['        UXlUUUUS9u  nnnUU::  a  SnOKU(       aD  SnSn UU    U :X  a  U S-  n UU    U :X  a  M  UR                  U 5        US-  nU
R                  / 5        USL a  [)        U5      nUR                  UU45        [        US-   U5       Hc  n!U
U!   R                  U5        [        XJU!   UU!   SSSS9u  UU!'   UU!'   [        UU!   5      UU!'   [        UU!   R                  5       5      UU!'   Me     US-
  nSnUSL d  GM    O   USL d  GM    O   USL a  GM  US-  nUS:  a  GM  USS n"U(       a  U Hy  u  n#nU"R                  U#5        [        [	        U5      5       HJ  nUU   n$[+        U$S   [,        5      (       a  U
U$S      U$S   S      S	-  UU'   M9  U
U$S      U$S      UU'   ML     M{     [        U5      nU H	  nU/UU'   M     UU"U4$ U"R/                  U V#V%s/ s H  u  n#n%U#PM
     sn%n#5        UU"4$ s  snf s  snf ! [          a    [#        U5      =nUU'    GNqf = fs  snf s  sn%n#f )
aw  Extend a sequence of points and generating set to a base and strong
generating set.

Parameters
==========

base
    The sequence of points to be extended to a base. Optional
    parameter with default value ``[]``.
gens
    The generating set to be extended to a strong generating set
    relative to the base obtained. Optional parameter with default
    value ``self.generators``.

slp_dict
    If `True`, return a dictionary `{g: gens}` for each strong
    generator `g` where `gens` is a list of strong generators
    coming before `g` in `strong_gens`, such that the product
    of the elements of `gens` is equal to `g`.

Returns
=======

(base, strong_gens)
    ``base`` is the base obtained, and ``strong_gens`` is the strong
    generating set relative to it. The original parameters ``base``,
    ``gens`` remain unchanged.

Examples
========

>>> from sympy.combinatorics.named_groups import AlternatingGroup
>>> from sympy.combinatorics.testutil import _verify_bsgs
>>> A = AlternatingGroup(7)
>>> base = [2, 3]
>>> seq = [2, 3]
>>> base, strong_gens = A.schreier_sims_incremental(base=seq)
>>> _verify_bsgs(A, base, strong_gens)
True
>>> base[:2]
[2, 3]

Notes
=====

This version of the Schreier-Sims algorithm runs in polynomial time.
There are certain assumptions in the implementation - if the trivial
group is provided, ``base`` and ``gens`` are returned immediately,
as any sequence of points is a base for the trivial group. If the
identity is present in the generators ``gens``, it is removed as
it is a redundant generator.
The implementation is described in [1], pp. 90-93.

See Also
========

schreier_sims, schreier_sims_random

Nr9   r   c              3   F   >#    U  H  oTR                   U   :H  v   M     g 7fr+   r?  r.   r   r   s     r0   r1   =PermutationGroup.schreier_sims_incremental.<locals>.<genexpr>,  s     :Eq**Es   !T)r:  r  ru  F)ru  r  r   )r|   r7   r:   r>   r?   r1  r   r   r   r   r]  rR  r  r   r  r   KeyErrorr
   reverser   r@   r,   r  r   )'rg   r   r   r  r7   id_afr^   _gensr   r   r   strong_gens_slporbsr   r  r   rF   
continue_idbr,  u_betarZ  gbu1r  rG   ru  r   u1_invschreier_gen
u1_inv_slpr  movedr   r   r   rv  r   r   s'                                         @r0   rQ  *PermutationGroup.schreier_sims_incremental  s4   x <D<??1%DU6]#t9>d1g11DGd1gY#777:AwQu!7EqE7C:E::: Cs+s2 !  4S!  5UBu:xA'9&TUBVa4(9$LOT!W"<?3LO<?//12DG	 ! qL1f JB $\!_%:%:%< =f'(9!(<=FAs.B%a,B!#//6:B+/74=9=aAq6=C9q6(S.CRx !=%'VF (0';%)!WR[^
"**,9C%DAq1$i
%D(3.$-lEWX^ahl$m	1c= %A %A$%E"#E(e"3 %
 #$E(e"3!LL/$MH-44R8: !(
A+22As8<%*1q5!_ 1! 4 ; ;A > 26Q;O$)!HDTt!M !9Qa 37|A2GQ*.|A/C/C/E*FQ &5 !"AA)-J!T)a >b %g !>h T!FAy 1f| Ah *3""1%s3xAAA!!A$..!21Q4!81a!A2!EA!21Q4!81!>A ) * #?3O&'S" ;88/:/$!QA/:;k!![ 8J :  ( =.8n<FRV=
 &Et ;s0    Q"Q"1Q'Q,RR
,R
	R
c           
      x  ^ Uc  / nUc  U R                   n[        U5      nU R                  nU Hd  m[        U4S jU 5       5      (       d  M  SnTR                  U   U:X  a  US-  nTR                  U   U:X  a  M  UR                  U5        US-  nMf     [        X5      n0 n	0 n
[        U5       H;  n[        [        XhU   X   SS95      X'   [        X   R                  5       5      X'   M=     SnX:  Ga  Uc  U R                  5       nOUS   R                  5       n[        XX5      u  pSnX::  a  SnO[UR                  (       dJ  SnSnU" U5      U:X  a  US-  nU" U5      U:X  a  M  UR                  U5        US-  nUR                  / 5        USL ag  [        SU5       HT  nUU   R                  U5        [        [        UUU   UU   SS95      U	U'   [        U	U   R                  5       5      U
U'   MV     SnOUS-  nX:  a  GM  US   SS nUS    H  mTU;  d  M  UR                  T5        M     UU4$ )	aA  Randomized Schreier-Sims algorithm.

Explanation
===========

The randomized Schreier-Sims algorithm takes the sequence ``base``
and the generating set ``gens``, and extends ``base`` to a base, and
``gens`` to a strong generating set relative to that base with
probability of a wrong answer at most `2^{-consec\_succ}`,
provided the random generators are sufficiently random.

Parameters
==========

base
    The sequence to be extended to a base.
gens
    The generating set to be extended to a strong generating set.
consec_succ
    The parameter defining the probability of a wrong answer.
_random_prec
    An internal parameter used for testing purposes.

Returns
=======

(base, strong_gens)
    ``base`` is the base and ``strong_gens`` is the strong generating
    set relative to it.

Examples
========

>>> from sympy.combinatorics.testutil import _verify_bsgs
>>> from sympy.combinatorics.named_groups import SymmetricGroup
>>> S = SymmetricGroup(5)
>>> base, strong_gens = S.schreier_sims_random(consec_succ=5)
>>> _verify_bsgs(S, base, strong_gens) #doctest: +SKIP
True

Notes
=====

The algorithm is described in detail in [1], pp. 97-98. It extends
the orbits ``orbs`` and the permutation groups ``stabs`` to
basic orbits and basic stabilizers for the base and strong generating
set produced in the end.
The idea of the extension process
is to "sift" random group elements through the stabilizer chain
and amend the stabilizers/orbits along the way when a sift
is not successful.
The helper function ``_strip`` is used to attempt
to decompose a random group element according to the current
state of the stabilizer chain and report whether the element was
fully decomposed (successful sift) or not (unsuccessful sift). In
the latter case, the level at which the sift failed is reported and
used to amend ``stabs``, ``base``, ``gens`` and ``orbs`` accordingly.
The halting condition is for ``consec_succ`` consecutive successful
sifts to pass. This makes sure that the current ``base`` and ``gens``
form a BSGS with probability at least `1 - 1/\text{consec\_succ}`.

See Also
========

schreier_sims

Nc              3   :   >#    U  H  nT" U5      U:H  v   M     g 7fr+   r   r  s     r0   r1   8PermutationGroup.schreier_sims_random.<locals>.<genexpr>  s     -13q6Q;r  r   r9   Tr9  rG   F)r|   r?   r7   r   r   r   r   r>   rR  r]  r:   r  r   r<   r   r1  )rg   r   r   consec_succr   r   r   r   r   r   r  rF   r  rG   r  rZ  r   r  r   r   r   s                       @r0   schreier_sims_random%PermutationGroup.schreier_sims_random  sb   J <D<??Dt9KKC----ooc*c11HC ooc*c1C A  5T@xA"#5a19Mt$% &LO<?//12DG !
  o#NN$ %))+!46DAA}]]h%'QJE h%'E"A!((, Ezq!A%a(//2&*+=a)!,d1gT,C 'DLO"<?#7#7#9:DG	 %
 Q9 o< (*1-$Q'C+%""3' ( [  rj   c                    U R                   nS/U-  nSX1'   U/nS/U-  nSXQ'   U R                  n[        U5      nU HH  n[        U5       H6  n	Xi   R                  U   n
XZ   SL d  M  UR                  U
5        SXZ'   XU
'   M8     MJ     U$ )ap  Computes the schreier vector for ``alpha``.

Explanation
===========

The Schreier vector efficiently stores information
about the orbit of ``alpha``. It can later be used to quickly obtain
elements of the group that send ``alpha`` to a particular element
in the orbit. Notice that the Schreier vector depends on the order
in which the group generators are listed. For a definition, see [3].
Since list indices start from zero, we adopt the convention to use
"None" instead of 0 to signify that an element does not belong
to the orbit.
For the algorithm and its correctness, see [2], pp.78-80.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([2, 4, 6, 3, 1, 5, 0])
>>> b = Permutation([0, 1, 3, 5, 4, 6, 2])
>>> G = PermutationGroup([a, b])
>>> G.schreier_vector(0)
[-1, None, 0, 1, None, 1, 0]

See Also
========

orbit

Nr   FT)r7   r|   r?   r>   r   r   )rg   r+  r   r  r  usedr   r   r  rF   r   s              r0   r    PermutationGroup.schreier_vector  s    @ KKF1HgwqyIA1Xw**1-:&JJt$!%DJdG   rj   c                 V    [        [        U R                  U R                  U5      5      $ )a  Return the stabilizer subgroup of ``alpha``.

Explanation
===========

The stabilizer of `\alpha` is the group `G_\alpha =
\{g \in G | g(\alpha) = \alpha\}`.
For a proof of correctness, see [1], p.79.

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> G = DihedralGroup(6)
>>> G.stabilizer(5)
PermutationGroup([
    (5)(0 4)(1 3)])

See Also
========

orbit

)r  r  r]   rK   )rg   r+  s     r0   r  PermutationGroup.stabilizerM  s"    2 T\\43C3CUKLLrj   c                 Z    U R                   / :X  a  U R                  5         U R                   $ )aP  Return a strong generating set from the Schreier-Sims algorithm.

Explanation
===========

A generating set `S = \{g_1, g_2, \dots, g_t\}` for a permutation group
`G` is a strong generating set relative to the sequence of points
(referred to as a "base") `(b_1, b_2, \dots, b_k)` if, for
`1 \leq i \leq k` we have that the intersection of the pointwise
stabilizer `G^{(i+1)} := G_{b_1, b_2, \dots, b_i}` with `S` generates
the pointwise stabilizer `G^{(i+1)}`. The concepts of a base and
strong generating set and their applications are discussed in depth
in [1], pp. 87-89 and [2], pp. 55-57.

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> D = DihedralGroup(4)
>>> D.strong_gens
[(0 1 2 3), (0 3)(1 2), (1 3)]
>>> D.base
[0, 1]

See Also
========

base, basic_transversals, basic_orbits, basic_stabilizers

)r_   r   rw   s    r0   r   PermutationGroup.strong_gensh  s+    @ "    rj   c                 h   ^  [        U 4S jU 5       5      (       d  [        S5      e[        U5      nU$ )zR
Return the subgroup generated by `gens` which is a list of
elements of the group
c              3   ,   >#    U  H	  oT;   v   M     g 7fr+   r   )r.   rG   rg   s     r0   r1   ,PermutationGroup.subgroup.<locals>.<genexpr>  s     +d9dr  z2The group does not contain the supplied generators)r   r  r&   )rg   r   ra  s   `  r0   subgroupPermutationGroup.subgroup  s1     +d+++QRRT"rj   c           	        ^&^'^(^)^*^+ U&4S jnU&U'U(U)U*U+4S jnUc  U R                  5       u  p#[        U5      nU R                  m([        [	        [        T(5      5      5      n	[        UT(5      m&T&R                  T(5        T&R                  S5        [        X#5      n
[        UU
5      u  m'nUc  [        U	/5      nUc(  S n/ n[        U5       H  nUR                  U5        M     UnUS-
  nUS-
  nUSS nUR                  US9u  nn[        UU5      nUR                  n[        U5       Vs/ s H  n[        T(UU   UU   5      PM     snm*S/U-  n[        T(UU   5      nU" U5      UU'   UU   R                  X/   5        S/U-  nU	/U-  nS/U-  m+[        U5       H$  nT'U   SS T+U'   T+U   R                  U&4S	 jS
9  M&     S/U-  nS/U-  m)T(S-   UU'   U" U5        U	/U-  n UUS-
  :  Ga  UU   " UU   5      UU   ;   Ga  T&UU      T&UU   " UU   5         s=:  a  T&T)U      :  Ga  O  GOUU   " U5      (       Ga  UU   " UU   5      nUUU'   [!        T(UU   U5      nUUUS-   '   [        T(U5      nU" U5      UUS-   '   US-  nT'U    Vs/ s H  nUUS-
     " U5      PM     nnUR                  U&4S jS
9  UT+U'   T(S-   n[        U5       H.  nUU   T*U   ;   d  M  UU   " X-   5      n T&U    T&U   :  d  M,  U nM0     UUU'   U" U5        SUU'   T+U   UU      n!UUS-
     R"                  R%                  U!5      n"UU   U"   UU'   ['        UUS-
     UU   5      UU'   UUS-
  :  aS  UU   " UU   5      UU   ;   a>  T&UU      T&UU   " UU   5         s=:  a  T&T)U      :  a  O  OUU   " U5      (       a  GM  UU   n#U#" UU   5      n!UUS-
  :X  a  T&UU      T&U!   s=:  a  T&T)U      :  a  O  OU!UU   ;   a  UU   " U5      (       az  U" U#5      (       am  UR                  U#5        USS nUR                  U#5        [        UU5      n[        U5       Vs/ s H  n[        T(UU   UU   5      PM     snm*U" U5      UU'   UnUS:  a=  UU   [        T'U   5      S-
  :X  a%  US-
  nUS:  a  UU   [        T'U   5      S-
  :X  a  M%  US:X  a  [        U5      $ UU:  ao  UnSUU'   [        T(UU   5      n$U" U$5      UU'   T(S-   UU'   [        T'U   5      S-   [        T*U   5      -
  n%U%[        T+U   5      :  a	  T&T(   T)U'   OT+U   U%   T)U'   UU==   S-  ss'   US:X  a  T+U   UU      n"O*UUS-
     R"                  R%                  T+U   UU      5      n"UU   U"   UU'   US:X  a	  UU   UU'   O['        UUS-
     UU   5      UU'   GM  s  snf s  snf s  snf )a
  Find the subgroup of all elements satisfying the property ``prop``.

Explanation
===========

This is done by a depth-first search with respect to base images that
uses several tests to prune the search tree.

Parameters
==========

prop
    The property to be used. Has to be callable on group elements
    and always return ``True`` or ``False``. It is assumed that
    all group elements satisfying ``prop`` indeed form a subgroup.
base
    A base for the supergroup.
strong_gens
    A strong generating set for the supergroup.
tests
    A list of callables of length equal to the length of ``base``.
    These are used to rule out group elements by partial base images,
    so that ``tests[l](g)`` returns False if the element ``g`` is known
    not to satisfy prop base on where g sends the first ``l + 1`` base
    points.
init_subgroup
    if a subgroup of the sought group is
    known in advance, it can be passed to the function as this
    parameter.

Returns
=======

res
    The subgroup of all elements satisfying ``prop``. The generating
    set for this group is guaranteed to be a strong generating set
    relative to the base ``base``.

Examples
========

>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
... AlternatingGroup)
>>> from sympy.combinatorics.testutil import _verify_bsgs
>>> S = SymmetricGroup(7)
>>> prop_even = lambda x: x.is_even
>>> base, strong_gens = S.schreier_sims_incremental()
>>> G = S.subgroup_search(prop_even, base=base, strong_gens=strong_gens)
>>> G.is_subgroup(AlternatingGroup(7))
True
>>> _verify_bsgs(G, base, G.generators)
True

Notes
=====

This function is extremely lengthy and complicated and will require
some careful attention. The implementation is described in
[1], pp. 114-117, and the comments for the code here follow the lines
of the pseudocode in the book for clarity.

The complexity is exponential in general, since the search process by
itself visits all members of the supergroup. However, there are a lot
of tests which are used to prune the search tree, and users can define
their own tests via the ``tests`` parameter, so in practice, and for
some computations, it's not terrible.

A crucial part in the procedure is the frequent base change performed
(this is line 11 in the pseudocode) in order to obtain a new basic
stabilizer. The book mentiones that this can be done by using
``.baseswap(...)``, however the current implementation uses a more
straightforward way to find the next basic stabilizer - calling the
function ``.stabilizer(...)`` on the previous basic stabilizer.

c                 L   > U  Vs/ s H  n[        UU4S jS9PM     sn$ s  snf )Nc                    > TU    $ r+   r   )r   r   s    r0   r   DPermutationGroup.subgroup_search.<locals>.get_reps.<locals>.<lambda>  s
    }Q/?rj   r   )r  )r  rX  r   s     r0   get_reps2PermutationGroup.subgroup_search.<locals>.get_reps  s2     "#!% %?@!# # #s   !c                    > [        TU    5      S-   [        TU    5      -
  nU[        TU    5      :  a	  TT   TU '   g TU    U   TU '   g )Nr9   r6  )r   
temp_indexr   r   r7   nures_basic_orbits_init_basesorted_orbitss     r0   	update_nu3PermutationGroup.subgroup_search.<locals>.update_nu  s^    \!_-17:;<J Sq!122%f-1%a(41rj   Nr   c                     gr<  r   r7  s    r0   r   2PermutationGroup.subgroup_search.<locals>.<lambda>  r=  rj   r9   r   r   c                    > TU    $ r+   r   rY  r   s    r0   r   r  '  s
    M%4Hrj   r   c                    > TU    $ r+   r   r  s    r0   r   r  G  s
    -2Frj   )rQ  r?   r7   r@   r:   r>   r   r   r   r   r&   r|   r   r`  r   rP  r  r   r   r   ),rg   rJ  r   r   rL  init_subgroupr  r  r   r  r   r   r]  rF   rh  rw  r   res_baseres_strong_gensres_strong_gens_distrres_generatorsrF  r  r  r  mur@  	new_pointnew_stab_gensrY  
temp_orbitnew_mu	candidate
temp_pointr   rG   temp_orbitsr  r   r   r7   r  r  r  s,                                         @@@@@@r0   rT   PermutationGroup.subgroup_search  sV   \	#
	5 	5 < $ > > @Dt94f./&tV4V$R 4TG%CD%6&8"l  ,hZ8M=)LE8_\* % qLqL7$'$A$A %B %!/ 8 /!1 /	#!Q 
-a0(1+	>!	# 	# VH_
!6q!9: (
1 	1TW%CLJxxxA+Aq1M!!!!&H!I ! VH_VH_
1!"H,hl"q!$q'*jm;be$nQ/Q89%be$% % !H^,,*1-d1g6	' +F4I!4L!!#/<%a!e, !7$,V$4
1q5!Q ,Q1 / @EnQU3E: /  1$FG#-a !qAAw"<Q"??$21$5dg$>	(3mF6KK%.F	 "
 1!!*1-ad3
&q1u-99??
K#Au-!$(A)>!$Eq!K hl"q!$q'*jm;be$nQ/Q89%be$% !H^,,D q!A47JHqL be$j)A,9"Q%,@Ajm+a((GG%%a(7&&q)(@:I)K%  /+)Q  5a 8(1+F)+ +
 !) 0
1 q&QqTSa%9A%==E q&QqTSa%9A%== Bw'771u!%f.CA.FG ( 5
1
1 a1A521567
]1%5!66)&1BqE)!,Z8BqE aDAIDAv&q)!A$/&q1u-99??a@PQRSTQU@VW?5)AaDAv$%aDq!$(A)>!$Eq!E 5	#\1J+s   +WW 'W%c                     U R                   ce  U R                  nU n[        U5       H@  nUR                  U5      n[	        U5      X-
  :w  a
  X0l         Us  $ UR                  U5      nMB     Xl         U$ U R                   $ )a  Compute the degree of transitivity of the group.

Explanation
===========

A permutation group `G` acting on `\Omega = \{0, 1, \dots, n-1\}` is
``k``-fold transitive, if, for any `k` points
`(a_1, a_2, \dots, a_k) \in \Omega` and any `k` points
`(b_1, b_2, \dots, b_k) \in \Omega` there exists `g \in  G` such that
`g(a_1) = b_1, g(a_2) = b_2, \dots, g(a_k) = b_k`
The degree of transitivity of `G` is the maximum ``k`` such that
`G` is ``k``-fold transitive. ([8])

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> a = Permutation([1, 2, 0])
>>> b = Permutation([1, 0, 2])
>>> G = PermutationGroup([a, b])
>>> G.transitivity_degree
3

See Also
========

is_transitive, orbit

)rW   r7   r>   rX  r?   r  )rg   r   ra  rF   r  s        r0   transitivity_degree$PermutationGroup.transitivity_degree  sz    > $$,AA 1Xggajs8qu$01-HLLO  )*%H,,,rj   c                    U R                   SS n[        US SS9nU Vs/ s H  o3UR                  5       U-  -  PM     nn/ n[        [	        U5      5       GH  nX&   nUR                  5       nXxU-  -  n	US:  a  [        USU 5      n
O[        U R                  5      n
XxU-  -  U
;  a  UR                  XxU-  -  5        Ml  U
R                  U	SS9nU H
  nX{S-  -  nM     X-  nX&	 XF	 US-
  nU[	        U5      :  aB  X,   R                  5       U:  a,  US-  nU[	        U5      :  a  X,   R                  5       U:  a  M,  USU U/-   X,S -   nUSU U/-   XLS -   nGM	     [        U5      $ s  snf )	zg
For an abelian p-group, return the subgroup consisting of
all elements of order p (and the identity)

Nc                 "    U R                  5       $ r+   r  r7  s    r0   r   4PermutationGroup._p_elements_group.<locals>.<lambda>  s
    !'')rj   T)r  r  r   rr  r   r9   )	r|   r  r   r>   r?   r&   r  r   rt  )rg   r   r   rG   gens_pgens_rrF   r   x_orderx_pPrv  rZ  s                r0   _p_elements_group"PermutationGroup._p_elements_group  s    q!d 3TB,01Dqaggik"D1s4y!AAggiGai.C1u$VBQZ0$T]]319~Q&a!)n- ''d';AR%A !)GIE#d)m7(BFA #d)m7(BBQx1#~R0qc)F2J63 "4  ''9 2s   E-c           
         U R                   n/ n[        US-
  5      nUS:H  =(       a    [        S U R                   5       5      n/ nUnUS:  a  UR	                  Xq-  5        Xq-  nUS:  a  M  [        U5      S-
  n[        SUS-   5       H  n	U	S:X  a	  U(       a  M  [        [        X-  5       V
s/ s H  oXS-
  -  -   X-  -  PM     sn
5      nUR	                  XK-  5        U(       d  Ma  [        SS5      U-  [        SS5      -  U-  nUR	                  U5        M     SnUS:  a  Xh   n[        U5       H  n[        5       nUS:  a  [        X-  5       H  n	U" XU	-   5      nM     U(       a9  [        SS5      U-  [        SS5      -  U-  nUR	                  U5        SUS-
  -  n
OUn
[        USU
 5       H/  u  pU(       a  U	S-  S:X  a  M  X-  U-  nUR	                  U5        M1     XU-  -  nM     US-
  nUS:  a  M  U$ s  sn
f )a  
Return a p-Sylow subgroup of a symmetric or an
alternating group.

Explanation
===========

The algorithm for this is hinted at in [1], Chapter 4,
Exercise 4.

For Sym(n) with n = p^i, the idea is as follows. Partition
the interval [0..n-1] into p equal parts, each of length p^(i-1):
[0..p^(i-1)-1], [p^(i-1)..2*p^(i-1)-1]...[(p-1)*p^(i-1)..p^i-1].
Find a p-Sylow subgroup of Sym(p^(i-1)) (treated as a subgroup
of ``self``) acting on each of the parts. Call the subgroups
P_1, P_2...P_p. The generators for the subgroups P_2...P_p
can be obtained from those of P_1 by applying a "shifting"
permutation to them, that is, a permutation mapping [0..p^(i-1)-1]
to the second part (the other parts are obtained by using the shift
multiple times). The union of this permutation and the generators
of P_1 is a p-Sylow subgroup of ``self``.

For n not equal to a power of p, partition
[0..n-1] in accordance with how n would be written in base p.
E.g. for p=2 and n=11, 11 = 2^3 + 2^2 + 1 so the partition
is [[0..7], [8..9], {10}]. To generate a p-Sylow subgroup,
take the union of the generators for each of the parts.
For the above example, {(0 1), (0 2)(1 3), (0 4), (1 5)(2 7)}
from the first part, {(8 9)} from the second part and
nothing from the third. This gives 4 generators in total, and
the subgroup they generate is p-Sylow.

Alternating groups are treated the same except when p=2. In this
case, (0 1)(s s+1) should be added for an appropriate s (the start
of a part) for each part in the partitions.

See Also
========

sylow_subgroup, is_alt_sym

r9   r   c              3   8   #    U  H  oR                   v   M     g 7fr+   rq  r   s     r0   r1   2PermutationGroup._sylow_alt_sym.<locals>.<genexpr>  s     @1YYr6   r   N)r7   r   r   r|   r   r?   r>   r  )rg   r   r   r   r  altcoeffsr  powerrF   rZ  r   r   r/   r   shifts                   r0   _sylow_alt_symPermutationGroup._sylow_alt_sym  s   V KKqs# 1f@@@@ !eMM!% A !e FA q%'"AAv#eADkJkA!H4kJKCKK%s!!Q'+K1,==cAC  # aiA 1X#19"18_ %a 3 - )!Q/5k!Q6GGMC(uqyM!"+D!H"51q5A:$ $ioC( #6 E!+ , !GE7 ai: O Ks   1H
c                   ^ SSK JnJn  [        T5      (       d  [	        S5      eU4S jnU4S jnU R                  5       nUT-  S:w  a  [        U R                  /5      $ U" U 5      u  pxU(       a  U $ U R                  5       (       a  [        U R                  T5      5      $ U R                  5       n	U	 V
s/ s H*  n
[        U
5      T-  S:w  d  M  [        U
5      S:w  d  M(  U
PM,     nn
U(       a<  U R                  [        US   5      R                  5       5      nUR                  T5      $ U R!                  5       (       dI  [#        U	[        S9n	U	R                  5       nU	S   R$                  " U	6 nU" X5      nU" X5      nU" UU5      $ U R'                  5       n[        U5      S:  a!  U" U US   5      nU" U US   5      nU" UU5      $ [        U5      S:X  a  [        U5      S   n[)        S U 5       5      (       af  U" U U5      nU" UR+                  5       5      S   (       d?  UR+                  5       R                  T5      nUR-                  U5      R                  T5      $ U R/                  5       nUR                  5       nUT-  S:w  d  US:X  a3  U R/                  5       nUR                  5       nUT-  S:w  a  M+  US:X  a  M3  UUT-  -  nUTS	-  -  S:w  a  [        U5      $ U R1                  U5      nUR                  5       TU-  -  S:w  a  UR                  T5      nUR                  5       nUR3                  5       nUR5                  T5      nUR/                  5       nU R1                  U5      nUR                  5       T-  U-  S:w  a=  UR/                  5       nU R1                  U5      nUR                  5       T-  U-  S:w  a  M=  UnUR                  5       TU-  -  S:w  a  M  UR                  T5      $ s  sn
f )
a!  
Return a p-Sylow subgroup of the group.

The algorithm is described in [1], Chapter 4, Section 7

Examples
========
>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> from sympy.combinatorics.named_groups import SymmetricGroup
>>> from sympy.combinatorics.named_groups import AlternatingGroup

>>> D = DihedralGroup(6)
>>> S = D.sylow_subgroup(2)
>>> S.order()
4
>>> G = SymmetricGroup(6)
>>> S = G.sylow_subgroup(5)
>>> S.order()
5

>>> G1 = AlternatingGroup(3)
>>> G2 = AlternatingGroup(5)
>>> G3 = AlternatingGroup(9)

>>> S1 = G1.sylow_subgroup(3)
>>> S2 = G2.sylow_subgroup(3)
>>> S3 = G3.sylow_subgroup(3)

>>> len1 = len(S1.lower_central_series())
>>> len2 = len(S2.lower_central_series())
>>> len3 = len(S3.lower_central_series())

>>> len1 == len2
True
>>> len1 < len3
True

r   )orbit_homomorphismblock_homomorphismzp must be a primec                    > U R                  5       nSnUT-  S:X  a  UT-  nUS-  nUS:X  a  SU4$ UT-  S:X  a  M  SU4$ )Nr   r9   TFr  )ra  r  r   r   s      r0   
is_p_group3PermutationGroup.sylow_subgroup.<locals>.is_p_group  sZ     	AAa%1*aCQ67N	 a%1*
 !8Orj   c                    > U R                  5       R                  T5      nU R                  U5      nUR                  U5      nUR                  5       R                  T5      nUR                  U5      $ r+   )imagesylow_subgroupinvert_subgrouprestrict_to)r  r  QRr   s       r0   _sylow_reduce6PermutationGroup.sylow_subgroup.<locals>._sylow_reduce  s`     
))!,A""1%A"B
))!,A%%a((rj   r9   r   c              3   *   #    U  H	  oS :g  v   M     g7fr  r   r  s     r0   r1   2PermutationGroup.sylow_subgroup.<locals>.<genexpr>  s     )5a65r  r   )!sympy.combinatorics.homomorphismsr  r  r!   r  r   r&   r  r  r  r  r?   r  r:   r<   r  r  r  unionr  r;   r
  r  r  r1  r2  r  )rg   r   r  r  r  r  r   p_groupr   r  onon_p_orbitsra  omega1omega2r  r  r  r  SrG   g_orderCs_orderrP  r  r  C_hs    `                          r0   r  PermutationGroup.sylow_subgroupX  s   N	8 qzz011
		) 

19>#T]]O44%
K??#D$7$7$:;;
 #)M6aSVaZ1_Q16M\!_ 5 9 9 ;<A##A&&!!##F,FZZ\FAY__f-F#D1B#D1B R(($$&v;?#D&)4B#D&)4B R(([ALOE)5)))'e4!"((*-a0
11!4A--a0??BB KKM'')kQ'Q,AggiG kQ'Q, 11a4<1#A&&Qggi!Q$!#  #AggiG
A##A&A
A""1%C))+/')Q.HHJ&&q) ))+/')Q. A ggi!Q$!# ""i Ns   6O9O9!O9c                    [        U R                  U5      5      nS/[        U5      -  nS/[        U5      -  n/ /nS/[        U5      -  nUR                  USS9nU H6  u  pUS   R	                  U	5        UR                  U	5      nSXK'   X%U'   XU'   M8     SnSnX::  GaI  Xl   S   n
U R                   GH$  nX-  nUR                  U5      nUU   nUS:  a  US-  nUnXsR                  U
5         U-  UU'   UUU'   UnUUU'   U/nXl   SS   Hr  nUR                  U5      nUU-  nUR                  U5      nUU   S:  a(  UU   U-  UU'   UUU'   UUU'   UR	                  U5        M[  UU   U-  UU   S-  -  nSU4s  s  $    UR	                  U5        M  Xl   SS   H6  nUU-  UU   ;  d  M  XsR                  U
5         U-  UU   S-  -  nSU4s  s  $    GM'     US-  nX::  a  GMI  SU4$ )Nr   r   Tr9  r9   F)r  rX  r?   rS  r   r   r|   )rg   Lr+  r;  r   r  Br  r  r/   r,  i_arhor  rG   r  i_dsigmar   newbr   i_gammarv  r  s                           r0   _block_verifyPermutationGroup._block_verify  sA   tzz%() DUOc%jDCE
NT2GAaDKKN++a.CAF3KcF  h6!9D__Fkk!n#19FAE{{401!3AcF"AcFC"%F3K5D!""'++e"4!!G#kk!nS6A:%&wZ\AcF%*AcF*-F3K KKN !"'
1QsVb\ 9A#(!8O ", HHTNVABZQ3ah. !++d"3 4Q 6qv| CA#(!8O	 (; %D 1HCI hL V|rj   c           
         U R                  U5      nXCS-  -  nUR                  U5      nXF/n[        U V	s1 s H  n	[        UR                  U	5      5      iM     sn	5      n
U
 Vs/ s H  oS   PM	     nnU H  nX;  d  M
  UR	                  U5        M     XF/nXBR                  U R                  5      XbR                  US-  5      0nUR                  USS9 H'  u  nnUU;  d  M  X   UR                  U5      -  UU'   M)     UR                  U5      R                  UR                  U5      5      n[        U5      [        U5      :  a  U H  nUU;   d  M  UU-  nUU;  d  M  UR	                  U5        UU   UR                  U5      -  UU'   UR                  USS9 H(  u  nnUU;  d  M  UU   UR                  U5      -  UU'   M*     UR                  UR                  U5      5      n  O   [        U5      [        U5      :  a  M  / nU H  nUR                  U5      R                  nU H  nUU   nUR                  USS9nUSSS2    H  nUUR                  U5      -  nM     UUU   S-  -  nU" U5      n UR                  USS9nU H  nUUR                  U5      S-  -  nM     UU;  d  M  UR	                  U5        M     M     U Hx  nUU   UR                  U5      -  UUU-     S-  -  nU" U5      n UR                  USS9nU H  nUUR                  U5      S-  -  nM     UU;  d  Mg  UR	                  U5        Mz     SU4$ s  sn	f s  snf ! [         a    SU4s s  s  $ f = f! [         a	    SU4s s  $ f = f)a  
Return a list of relators ``rels`` in generators ``gens`_h` that
are mapped to ``H.generators`` by ``phi`` so that given a finite
presentation <gens_k | rels_k> of ``K`` on a subset of ``gens_h``
<gens_h | rels_k + rels> is a finite presentation of ``H``.

Explanation
===========

``H`` should be generated by the union of ``K.generators`` and ``z``
(a single generator), and ``H.stabilizer(alpha) == K``; ``phi`` is a
canonical injection from a free group into a permutation group
containing ``H``.

The algorithm is described in [1], Chapter 6.

Examples
========

>>> from sympy.combinatorics import free_group, Permutation, PermutationGroup
>>> from sympy.combinatorics.homomorphisms import homomorphism
>>> from sympy.combinatorics.fp_groups import FpGroup

>>> H = PermutationGroup(Permutation(0, 2), Permutation (1, 5))
>>> K = PermutationGroup(Permutation(5)(0, 2))
>>> F = free_group("x_0 x_1")[0]
>>> gens = F.generators
>>> phi = homomorphism(F, H, F.generators, H.generators)
>>> rels_k = [gens[0]**2] # relators for presentation of K
>>> z= Permutation(1, 5)
>>> check, rels_h = H._verify(K, phi, z, 1)
>>> check
True
>>> rels = rels_k + rels_h
>>> G = FpGroup(F, rels) # presentation of H
>>> G.order() == H.order()
True

See also
========

strong_presentation, presentation, stabilizer

r   r   Tr9  rr  NF)rX  r  r:   r  r   invertr  rS  r  r?   r|   rt  r  )r   r   phir-  r+  rX  r,  K_betagammasr  r  r  rF  r   betastransversalrv  rG   r  r   r   relsr  k_gensr   new_relr   r   s                               r0   _verifyPermutationGroup._verify  s   \ e}d# u=u!uV\\!_-u=>(./!f
/C c" 
 jj4dJJq"u<MN''D'9DAq#!,!23::a=!@A :
 $$QWWT]35zCJ&E>aA~Q)4U);CJJqM)IA$%$7$7$7$FDAq 31<Q

11MA %G !&AGGAJ 7   5zCJ& A\\!_//F%a.**1t*<ddA%cjjm3G $!+a.""447|'..td.CD A%cjjmR&77G $&KK(  & E!%(A6{577KR7OOGw<D#**4$*? !#**Q-"33 d"G$  Tz >/V " ' $;&'  #d{"#s/   $M%M<M>M1M.-M.1NNc                    SSK JnJn  SSKJn  SSKJnJnJn  U R                  SS nU R                  SS nU R                  SS n	[        [        U5      5       V
s/ s H  n
SU
-  PM
     nn
U" SR                  U5      5      S   nU" XUR                  U5      n[!        U R"                  5      nU(       Ga,  U	R%                  5       nUnUR%                  5       nUR                   Vs/ s H  nUU;  d  M  UPM     nnUR'                  5       S:X  a?  UR%                  5       nUR                  S	   UR'                  5       -  /nU/n[!        U5      nU(       Ga  UR%                  5       nU/W-   n[!        U5      nUR)                  U5      nUR)                  U5      nUU:X  a  UU;   a  UR+                  U5      nUnOBUR-                  XU-  5      nUR+                  U5      UR+                  U5      S	-  -  nUUS	-  -  nUR/                  US
S9 H  nUUR+                  U5      S	-  -  nM     U/nGO{[        U5      S:X  a  UR1                  UUUU5      u  nnGOSUR3                  UU5      u  nnU(       Ga6  U" UU5      nUR4                  R6                  n UR6                  n![9        5       n"[        U 5       H  n
U"[9        XU!-   5      -  n"M     UR:                  n#U# Vs0 s H  nUUU"-  U#U   -  U"-  _M     n$nU R                  S[        UR                  5      *   H  nUU$U'   M
     [!        [=        U$R?                  5       5      5      n%U" U U%U$5      n&[!        UR                   Vs/ s H  nU&" U5      PM     sn5      n'U%R1                  U'U&RA                  U5      U&" U5      U!5      u  nnW H  n(U(W;  d  M  URC                  U(5        M     UnU(       a  GM  U(       a  GM,  U" UW5      n)U" U)5      $ s  sn
f s  snf s  snf s  snf )a  
Return a strong finite presentation of group. The generators
of the returned group are in the same order as the strong
generators of group.

The algorithm is based on Sims' Verify algorithm described
in [1], Chapter 6.

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> P = DihedralGroup(4)
>>> G = P.strong_presentation()
>>> P.order() == G.order()
True

See Also
========

presentation, _verify

r   FpGroupsimplify_presentation
free_group)r  homomorphismGroupHomomorphismNx_%d, r9   r   Trr  )"sympy.combinatorics.fp_groupsr:  r;  sympy.combinatorics.free_groupsr=  r  r  r>  r?  r   r   r   r>   r?   joinr|   r&   r  r<   r   rX  r-  rZ  rt  r6  r*  codomainr7   r   imagesr:   r  composer   )*rg   r:  r;  r=  r  r>  r?  r   stabsr   rF   gen_symsFr.  r   r+  r   rG   new_gensr-  r3  intermediate_gensK_srX  orbit_krelr   r  new_relssuccesscheckr  r  r  r   t_imgrF  K_s_actrw  K_actr   groups*                                             r0   strong_presentation$PermutationGroup.strong_presentation  s   0	C>	L 	L &&q)&&q)yy| ).c+.>(?@(?1VAX(?@tyy*+A.1ALL+>T]]+HHJEA		A#$<<><a1A:<H>wwyA~LLNR(!'')34%&C!$%67 LLN%&C*;$;!&'89		%(''%. e#Av!jjm KKQw7!jjmCJJqM2,== Bw000E!#**Q-"33 F #uH\Q& ),AsAu(E%GX $'#4#4Q#>LE5
 /sE:JJ--JJ (M!&qAQ!!44A "* !" >C!CU!QqSq\!^"3U!C!%!1!12GC4G3G!HA()F1I "I"243H"I-dGVD 01M1!A$1M N,3OOE199S>STUVSWYZ,[)!A}A "  ( e\ 4 $U++i A ?x "D 2Ns   O5(
O:6O:O?'Pc                 d  ^# SSK Jm#Jn  SSKJn  SSKJn  SSKJn  U R                  (       a  U R                  $ U#4S jnU R                  n[        U5      nUS:X  a:  US   R                  5       n	U	S:X  a  U" / 5      S   $ U" S5      u  pT#" XU	-  /5      $ U R                  5       S	:  a  U R                  SUS-   S
-   nO/ n[        U5      nUR                  5       n[        UR                  5      nU R                  U5      n[        U5      n[!        [        U5      5       Vs/ s H  nSU-  PM
     nnU" SR#                  U5      5      S   n
[!        U5       Vs/ s H  nU
R                  U   PM     nnU" XUR                  USS9nU" UR$                  5      nT#" U
U5      nU" UU UR                  U5      nU" U/ 5      n[!        U5       Vs/ s H  nS/S
U-  -  PM     snUl        S/U-  nUR(                  US'   [!        S
U-  5       H  nSUR&                  S   U'   M     Sn[+        [!        U5      [!        S
U-  5      5       Hv  u  nnUU   U   nUU:X  d  M  UR                  US
-     SUS
-  -  -  nUU   U-  UU'   UUR&                  U   U'   UUR&                  U   USUS
-  -  -   '   US-  nUU:X  d  Mv    O   [-        [!        U5      5      Ul        S=nnUR1                  5       (       Gd  UR&                  U   U   UU   U   :X  a7  US-   S
U-  -  nUS:X  a  US-   U-  nUR&                  U   U   UU   U   :X  a  M7  UR                  US
-     SUS
-  -  -  nUU   U-  UUU   U      S-  -  nU" U5      n UR(                  n!UR3                  U SS9 H  n"U!UR5                  U"5      S-  -  n!M     UU!-  nU" UU/5      nUR7                  SU5        UR9                  / SUUSS9nUR1                  5       (       d  GM  U" U5      U l	        U R                  $ s  snf s  snf s  snf )zb
Return an `FpGroup` presentation of the group.

The algorithm is described in [1], Chapter 6.1.

r   r9  )
CosetTabler<  )r>  c                    > [        U T5      (       a@  UR                  U R                  5        T" U R                  [	        [        U5      5      5      $ T" X5      $ r+   )r,   r   relatorsr=  r:   r{   )ra  r3  r:  s     r0   _factor_group_by_rels<PermutationGroup.presentation.<locals>._factor_group_by_rels  sD    !W%%AJJ'q||T#d)_==1##rj   r9   r   rO  r   r@  rA  F)rR  Nr   Trr  r.  )strategydraft
max_cosets
incomplete)rB  r:  r;  sympy.combinatorics.coset_tablerZ  rC  r=  r  r>  re   r|   r?   r   r&   presentationr.  r>   rD  r\  r)  r  r   r:   r   is_completert  r-  scan_and_fillcoset_enumeration)$rg   eliminate_gensr;  rZ  r=  r>  r]  r   len_gr   rJ  r   	half_gensr   H_plen_hr  r   rF   rI  rF  r  r3  G_pr   C_pr2  r   r+  r,  r   r5  r   r  rv  r:  s$                                      @r0   rd  PermutationGroup.presentation  sL   	C>>B  (((	$ D	A:GMMOEz!"~a((c?DA1%xj))::<"57Q,7IIY'nnCNN#QF(-c$i(89(81VAX(89tyy*+A. ,1<8<a!,,q/<8uE a dCNND9b!/4Qx8x!dVQuW%x8	 fQhA qwACIIaLO   a%%.9HE1U8A;Du}nnQT*bAE];$/$6s$:D!&*		% #5:		$R1q5M 12
A: : U1Xq//##))D/!$$
2Uqw'6 1H>D ))D/!$$
2 ..A&"A7C!$'+K$
,CR,GGGW:D,,C(((=!((1+r/) >ckG (gY7Ca)'']&)aD ( JC' //##, !6c :$$$A : 9 9s   $P#P(P-c                 :   SSK Jn  U R                  (       d  [        S5      eU R	                  5       n/ n/ n/ nUR                  US   5        UR                  5         [        [        U5      S-
  5       H  nX&   nX&S-      R                   H  nX;  d  M
  [        U/UR                  -   5      nUR                  SU5        UR                  SU5        US   R                  5       n	US   R                  5       n
UR                  SX-  5        M     M     U" XCUSS9$ )a  
Return the PolycyclicGroup instance with below parameters:

Explanation
===========

* pc_sequence : Polycyclic sequence is formed by collecting all
  the missing generators between the adjacent groups in the
  derived series of given permutation group.

* pc_series : Polycyclic series is formed by adding all the missing
  generators of ``der[i+1]`` in ``der[i]``, where ``der`` represents
  the derived series.

* relative_order : A list, computed by the ratio of adjacent groups in
  pc_series.

r   )PolycyclicGroupzThe group must be solvabler   r9   N)	collector)sympy.combinatorics.pc_groupsrq  r*  r  r   r   r  r>   r?   r|   r&   insertr   )rg   rq  r   	pc_seriespc_sequencerelative_orderrF   r   rG   G1r  s              r0   polycyclic_group!PermutationGroup.polycyclic_group{  s   & 	B!!9::!!#	R!s3xz"AA1X((:(!q||);<A$$Q*&&q!,"1++-B"1++-B"))!RX6 ) # {~QUVVrj   )r^   ra   rN   r]   rM   re   rK   rO   rR   rZ   r[   rT   rY   rS   rU   rQ   rP   rV   rX   rL   r\   rd   r_   r`   rW   rc   rb   r+   )FNNN)F)r  F)Tr  )g?N)rN  tuples)   2   N)NN)NNF)NNrN  N)NNNN)e__name__
__module____qualname____firstlineno____doc__is_grouprB   rh   rn   rt   rx   r   r   r   r   r   propertyr   r   r   r   r   r   r  r"  r.  r2  r1  rg  ro  rt  r{  r  r7   r  r	  r   r  r  r  r  r|   rs   r  r  r  r  r  r  r  r  r   r  r  r"  r  r*  r  rO  r  r   r	  rD  rI  r`  rX  rZ  rS  r  r   r   rm  rt  classmethodrx  r  r  r  r  r  r   r   r   r
  rQ  r  r   r  r   r  rT  r  r  r  r  r*  r6  rW  rd  ry  __static_attributes__r   rj   r0   r&   r&      s   ^~ H!% 32!%F# "%%N)&V:?x/b)V    D ;@IMD)L " "> .! .!` " "@HT=~6*X$Lu?n-^Yv'R-^0  @ 1 1  "/b-^3Oj8(tDL     <;|    &  @HTP0.$L;z +& +&Z,\=~Qf #% #%J:?x    $1f    $%N # #JWJr9x>cBJ$CL&6PO:7 1f+& 7: 7:t 7: 7:r 	 	 T Tl ^! ^!@3&j1f+,'\,@%2w"r FH*.B!H/bM6 !! !!F
 HL&*{Fz 0- 0-d$(LhTE#N;ztlx,tm%^*Wrj   r&   c           	      l   [        US5      (       d  U/nU Vs/ s H  oDR                  PM     nn[        U5      S:X  d  US:X  aU  UnS/U -  nU H  nSXx'   M	     U H/  n	U H&  n
X   nX{   S:X  d  M  UR                  U5        SX{'   M(     M1     [	        U5      $ US:X  au  [        U5      nU/nU1nU HS  n	U HJ  n
[        U	 Vs/ s H  oJU   PM	     sn5      nX;  d  M(  UR                  U5        UR                  U5        ML     MU     [	        U5      $ US:X  a  [        U5      nU/nU1nU HS  n	U HJ  n
[        U	 Vs/ s H  oJU   PM	     sn5      nX;  d  M(  UR                  U5        UR                  U5        ML     MU     U Vs1 s H  n[        U5      iM     sn$ gs  snf s  snf s  snf s  snf )	at  Compute the orbit of alpha `\{g(\alpha) | g \in G\}` as a set.

Explanation
===========

The time complexity of the algorithm used here is `O(|Orb|*r)` where
`|Orb|` is the size of the orbit and ``r`` is the number of generators of
the group. For a more detailed analysis, see [1], p.78, [2], pp. 19-21.
Here alpha can be a single point, or a list of points.

If alpha is a single point, the ordinary orbit is computed.
if alpha is a list of points, there are three available options:

'union' - computes the union of the orbits of the points in the list
'tuples' - computes the orbit of the list interpreted as an ordered
tuple under the group action ( i.e., g((1, 2, 3)) = (g(1), g(2), g(3)) )
'sets' - computes the orbit of the list interpreted as a sets

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup
>>> from sympy.combinatorics.perm_groups import _orbit
>>> a = Permutation([1, 2, 0, 4, 5, 6, 3])
>>> G = PermutationGroup([a])
>>> _orbit(G.degree, G.generators, 0)
{0, 1, 2}
>>> _orbit(G.degree, G.generators, [0, 4], 'union')
{0, 1, 2, 3, 4, 5, 6}

See Also
========

orbit, orbit_transversal

rn   r9   r  FTr|  setsN)rN  r   r?   r   r{   r  r  	frozenset)r7   r|   r+  rW  r   r   r  r  r   r  r   r   s               r0   r   r     s   J 5-((#-.:aMM:D.
5zQ&G+wv~BDH Av:&JJt$!%DJ	   3x	8	egwAa0a!fa01#JJt$HHTN	   3x	6	% gwA !!4!Qa&!!45#JJt$HHTN	   #&&#Qa#&& 
1 /& 1 "5 's   F"F'?F,	F1c                     / n[        [        U 5      5      n[        U5      nU(       aH  US   n[        XU5      nUR	                  U5        XF-  nU Vs/ s H  oUU;  d  M
  UPM     nnU(       a  MH  U$ s  snf )a_  Compute the orbits of G.

If ``rep=False`` it returns a list of sets else it returns a list of
representatives of the orbits

Examples
========

>>> from sympy.combinatorics import Permutation
>>> from sympy.combinatorics.perm_groups import _orbits
>>> a = Permutation([0, 2, 1])
>>> b = Permutation([1, 0, 2])
>>> _orbits(a.size, [a, b])
[{0, 1, 2}]
r   )r:   r>   r{   r   r   )r7   r|   r  sorted_Irl  rF   r  s          r0   r`  r`    sv    " DE&M"HHA
QKV+C	'8x!C<Ax8 ! K 9s   	A3 A3c           	         U[        [        U 5      5      4/nU/ 0nS/U -  nSX'   U V	s/ s H  oR                  PM     n
n	U HW  u  pXy   nU
 HH  nX   nX   S:X  d  M  U
R                  U5      /U-   X~'   UR	                  U[        X5      45        SX'   MJ     MY     U(       a4  U(       d!  U V	Vs/ s H  u  pU	[        U5      4PM     nn	nU(       d  U$ Xg4$ U(       a"  U VVs/ s H  u  noPM	     nnnU(       d  U$ Xg4$ U VVs/ s H  u  nn[        U5      PM     nnnU(       d  U$ Xg4$ s  sn	f s  snn	f s  snnf s  snnf )ae  Computes a transversal for the orbit of ``alpha`` as a set.

Explanation
===========

generators   generators of the group ``G``

For a permutation group ``G``, a transversal for the orbit
`Orb = \{g(\alpha) | g \in G\}` is a set
`\{g_\beta | g_\beta(\alpha) = \beta\}` for `\beta \in Orb`.
Note that there may be more than one possible transversal.
If ``pairs`` is set to ``True``, it returns the list of pairs
`(\beta, g_\beta)`. For a proof of correctness, see [1], p.79

if ``af`` is ``True``, the transversal elements are given in
array form.

If `slp` is `True`, a dictionary `{beta: slp_beta}` is returned
for `\beta \in Orb` where `slp_beta` is a list of indices of the
generators in `generators` s.t. if `slp_beta = [i_1 \dots i_n]`
`g_\beta = generators[i_n] \times \dots \times generators[i_1]`.

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> from sympy.combinatorics.perm_groups import _orbit_transversal
>>> G = DihedralGroup(6)
>>> _orbit_transversal(G.degree, G.generators, 0, False)
[(5), (0 1 2 3 4 5), (0 5)(1 4)(2 3), (0 2 4)(1 3 5), (5)(0 4)(1 3), (0 3)(1 4)(2 5)]
FT)r:   r>   r   r   r   r   r@   )r7   r|   r+  r:  r  ru  rn  r  r  r   r   pxpx_slpr   r   r   r   s                    r0   r]  r]    sO   B $uV}%
&	'Br{H76>DDK#-.:aMM:D.C6DzU""&**S/!2V!;		4#!234!
   .01bda1gaj/bB1I|	BDAqaBI|!#	$A'!*B	$	<3 / 2  
 
%s   D.1D3%D9D?c                    U/nU[        [        U 5      5      0nU[        [        U 5      5      0nS/U -  nSXb'   U Vs/ s H  owR                  PM     nn/ n	U Hv  n
U Hm  nX   nXl   SL a6  [        XU
   5      nUR	                  U5        XU'   [        U5      X\'   SXl'   MD  [        X\   XU
   5      nX;  d  M\  U	R	                  U5        Mo     Mx     U	 Vs/ s H  n[        U5      PM     sn$ s  snf s  snf )a  Return the stabilizer subgroup of ``alpha``.

Explanation
===========

The stabilizer of `\alpha` is the group `G_\alpha =
\{g \in G | g(\alpha) = \alpha\}`.
For a proof of correctness, see [1], p.79.

degree :       degree of G
generators :   generators of G

Examples
========

>>> from sympy.combinatorics.perm_groups import _stabilizer
>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> G = DihedralGroup(6)
>>> _stabilizer(G.degree, G.generators, 5)
[(5)(0 4)(1 3), (5)]

See Also
========

orbit

FT)r:   r>   r   r   r   r
   r   r@   )r7   r|   r+  r  r)  	table_invr  r   r   r  r  r   r   gen_tempr  s                  r0   r  r  T  s   8 'CDv'(EU6]+,I76>DDK#-.:aMM:D.IC6DzU"#Cq2

4 &d",X"6	!
(#QxH0$$\2   !**	1GAJ	** / +s   C/C4c                   P    \ rS rSrSrS rS rS rS r\	S 5       r
\	S 5       rS	rg
)r'  i  zA
The class defining the lazy form of SymmetricGroup.

deg : int

c                 H    [        U5      n[        R                  " X5      nU$ r+   )r   r   rB   )rC   r   objs      r0   rB   !SymmetricPermutationGroup.__new__  s    smmmC%
rj   c                 :    U R                   S   U l        S U l        g rJ   )rD   _degrL   rf   s      r0   rh   "SymmetricPermutationGroup.__init__  s    IIaL	rj   c                     [        U[        5      (       d  [        S[        U5      -  5      eUR                  U R
                  :H  $ )zReturn ``True`` if *i* is contained in SymmetricPermutationGroup.

Examples
========

>>> from sympy.combinatorics import Permutation, SymmetricPermutationGroup
>>> G = SymmetricPermutationGroup(4)
>>> Permutation(1, 2, 3) in G
True

z[A SymmetricPermutationGroup contains only Permutations as elements, not elements of type %s)r,   r   rq   rr   r5   r7   rm   s     r0   rt   &SymmetricPermutationGroup.__contains__  sF     ![)) @BFq'J K Kvv$$rj   c                     U R                   b  U R                   $ U R                  n[        U5      U l         U R                   $ )z
Return the order of the SymmetricPermutationGroup.

Examples
========

>>> from sympy.combinatorics import SymmetricPermutationGroup
>>> G = SymmetricPermutationGroup(4)
>>> G.order()
24
)rL   r  r   )rg   r   s     r0   r   SymmetricPermutationGroup.order  s6     ;;";;IIl{{rj   c                     U R                   $ )z
Return the degree of the SymmetricPermutationGroup.

Examples
========

>>> from sympy.combinatorics import SymmetricPermutationGroup
>>> G = SymmetricPermutationGroup(4)
>>> G.degree
4

)r  rw   s    r0   r7    SymmetricPermutationGroup.degree  s     yyrj   c                 P    [        [        [        U R                  5      5      5      $ )z
Return the identity element of the SymmetricPermutationGroup.

Examples
========

>>> from sympy.combinatorics import SymmetricPermutationGroup
>>> G = SymmetricPermutationGroup(4)
>>> G.identity()
(3)

)r@   r:   r>   r  rw   s    r0   r  "SymmetricPermutationGroup.identity  s     tE$)),-..rj   )r  rL   N)r  r  r  r  r  rB   rh   rt   r   r  r7   r  r  r   rj   r0   r'  r'    sC    
%"$   / /rj   r'  c                   N    \ rS rSrSrS
S jrS r\S 5       r\S 5       r	S r
S	rg)r   i  a  A left coset of a permutation group with respect to an element.

Parameters
==========

g : Permutation

H : PermutationGroup

dir : "+" or "-", If not specified by default it will be "+"
    here ``dir`` specified the type of coset "+" represent the
    right coset and "-" represent the left coset.

G : PermutationGroup, optional
    The group which contains *H* as its subgroup and *g* as its
    element.

    If not specified, it would automatically become a symmetric
    group ``SymmetricPermutationGroup(g.size)`` and
    ``SymmetricPermutationGroup(H.degree)`` if ``g.size`` and ``H.degree``
    are matching.``SymmetricPermutationGroup`` is a lazy form of SymmetricGroup
    used for representation purpose.

Nc                 8   [        U5      n[        U[        5      (       d  [        e[        U5      n[        U[        5      (       d  [        eUb|  [        U5      n[        U[        [
        45      (       d  [        eUR                  U5      (       d  [        SR                  X#5      5      eX;  a  [        SR                  X5      5      eOLUR                  nUR                  nXV:w  a  [        SR                  X5      5      e[        UR                  5      n[        U[        5      (       a  [        U5      nO,[        U[        5      (       d  [        S[        U5      -  5      e[        U5      S;  a  [        SU-  5      e[        R                   " XX#U5      nU$ )Nz{} must be a subgroup of {}.z{} must be an element of {}.z]The size of the permutation {} and the degree of the permutation group {} should be matching z0dir must be of type basestring or Symbol, not %s)r   -z$dir must be one of '+' or '-' not %s)r   r,   r   r   r&   r'  r  r  r  r5   r7   strr   rq   rr   r   rB   )rC   rG   r   ra  r   g_sizeh_degreer  s           r0   rB   Coset.__new__  se   QK![))%%QK!-..%%=Aa"24M!NOO))==## !?!F!Fq!LMMz !?!F!Fq!LMM  VVFxxH! CVA\# # *!&&1Ac3+CC(( %'+Cy1 2 2s8:%CcIJJmmCA#.
rj   c                 ,    U R                   S   U l        g )N   )rD   _dirrf   s      r0   rh   Coset.__init__  s    IIaL	rj   c                 2    [        U R                  5      S:H  $ )a  
Check if the coset is left coset that is ``gH``.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup, Coset
>>> a = Permutation(1, 2)
>>> b = Permutation(0, 1)
>>> G = PermutationGroup([a, b])
>>> cst = Coset(a, G, dir="-")
>>> cst.is_left_coset
True

r  r  r  rw   s    r0   is_left_cosetCoset.is_left_coset       " 499~$$rj   c                 2    [        U R                  5      S:H  $ )a!  
Check if the coset is right coset that is ``Hg``.

Examples
========

>>> from sympy.combinatorics import Permutation, PermutationGroup, Coset
>>> a = Permutation(1, 2)
>>> b = Permutation(0, 1)
>>> G = PermutationGroup([a, b])
>>> cst = Coset(a, G, dir="+")
>>> cst.is_right_coset
True

r   r  rw   s    r0   is_right_cosetCoset.is_right_coset3  r  rj   c                    U R                   S   nU R                   S   n/ n[        U R                  5      S:X  a(  UR                   H  nUR	                  XA-  5        M     U$ UR                   H  nUR	                  X-  5        M     U$ )z7
Return all the elements of coset in the form of list.
r   r9   r   )rD   r  r  r	  r   )rg   rG   r   cstr  s        r0   as_listCoset.as_listF  sw     IIaLIIaLtyy>S ZZ

13  
 
 ZZ

13  
rj   )r  )Nr   )r  r  r  r  r  rB   rh   r  r  r  r  r  r   rj   r0   r   r     s?    2#J! % %$ % %$rj   r   Nr{  r  )<mathr   r  r   r   	itertoolsr   r   sympy.combinatoricsr    sympy.combinatorics.permutationsr	   r
   r   r   r   r   sympy.combinatorics.utilr   r   r   r   r   r   r   r   
sympy.corer   sympy.core.randomr   r   r   sympy.core.symbolr   sympy.core.sympifyr   (sympy.functions.combinatorial.factorialssympy.ntheoryr   r   sympy.ntheory.factor_r   r    sympy.ntheory.primetestr!   sympy.utilities.iterablesr"   r#   r$   rmul_with_afr   r@   r&   r   r`  r]  r  r  r'  r   r   rj   r0   <module>r     s    3 3 $ ,) )    ; ; $ ' > - ; + D D


KNWu KNW\\J'Z<>B0+f 	Q/ Q/huE urj   