
    \h?                         S SK JrJrJr  S SKJr  \R                  r\R                  rS rS r	S r
  SS jr SS jrSS	 jrS
 r/ 0 4S jrS rg)    )Permutation
_af_invert_af_rmul)isprimec                     [        U 5      nS/U-  n[        U5       H	  nXCX   '   M     Un[        U5       H  nX@;  d  M
  XSU'   US-  nM     U$ )aP  
Order `\{0, 1, \dots, n-1\}` so that base points come first and in order.

Parameters
==========

base : the base
degree : the degree of the associated permutation group

Returns
=======

A list ``base_ordering`` such that ``base_ordering[point]`` is the
number of ``point`` in the ordering.

Examples
========

>>> from sympy.combinatorics import SymmetricGroup
>>> from sympy.combinatorics.util import _base_ordering
>>> S = SymmetricGroup(4)
>>> S.schreier_sims()
>>> _base_ordering(S.base, S.degree)
[0, 1, 2, 3]

Notes
=====

This is used in backtrack searches, when we define a relation `\ll` on
the underlying set for a permutation group of degree `n`,
`\{0, 1, \dots, n-1\}`, so that if `(b_1, b_2, \dots, b_k)` is a base we
have `b_i \ll b_j` whenever `i<j` and `b_i \ll a` for all
`i\in\{1,2, \dots, k\}` and `a` is not in the base. The idea is developed
and applied to backtracking algorithms in [1], pp.108-132. The points
that are not in the base are taken in increasing order.

References
==========

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

r      )lenrange)basedegreebase_lenorderingicurrents         P/var/www/auris/envauris/lib/python3.13/site-packages/sympy/combinatorics/util.py_base_orderingr      s`    X 4yHs6zH8_ G6]=!QKqLG  O    c                 x   U R                   nU R                  nSnSn[        5       n[        US-  5       H  nXe;  d  M
  XaS-  U-
  :  d  M  SnUR	                  U5        UnX'   U:w  a$  US-  nX'   nUR	                  U5        X'   U:w  a  M$  XC-  nX1S-  :  d  Mf  X1S-
  :  d  Mp  [        U5      (       d  M    g   g)as  
Checks for cycles of prime length p with n/2 < p < n-2.

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

Here `n` is the degree of the permutation. This is a helper function for
the function is_alt_sym from sympy.combinatorics.perm_groups.

Examples
========

>>> from sympy.combinatorics.util import _check_cycles_alt_sym
>>> from sympy.combinatorics import Permutation
>>> a = Permutation([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12]])
>>> _check_cycles_alt_sym(a)
False
>>> b = Permutation([[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10]])
>>> _check_cycles_alt_sym(b)
True

See Also
========

sympy.combinatorics.perm_groups.PermutationGroup.is_alt_sym

r      r   TF)size
array_formsetr
   addr   )permnafcurrent_len	total_lenusedr   js           r   _check_cycles_alt_symr!   F   s    8 			A	BKI5D1a4[=QA	!11KHHQKA%1*q E %1* $IT!kE&9gk>R>R  r   c           
         [        U 5      nUS   R                  n[        U5       Vs/ s H  n/ PM     nnSnU Hy  nSnXS-
  :  a=  UR                  X      X   :X  a&  US-  nXS-
  :  a  UR                  X      X   :X  a  M&  X:  a  Un[        US-   5       H  n	XY   R	                  U5        M     M{     [        US-   U5       H1  n
XZ   R	                  [        [        [        U5      5      5      5        M3     U$ s  snf )a  
Distribute the group elements ``gens`` by membership in basic stabilizers.

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

Notice that for a base `(b_1, b_2, \dots, b_k)`, the basic stabilizers
are defined as `G^{(i)} = G_{b_1, \dots, b_{i-1}}` for
`i \in\{1, 2, \dots, k\}`.

Parameters
==========

base : a sequence of points in `\{0, 1, \dots, n-1\}`
gens : a list of elements of a permutation group of degree `n`.

Returns
=======
list
    List of length `k`, where `k` is the length of *base*. The `i`-th entry
    contains those elements in *gens* which fix the first `i` elements of
    *base* (so that the `0`-th entry is equal to *gens* itself). If no
    element fixes the first `i` elements of *base*, the `i`-th element is
    set to a list containing the identity element.

Examples
========

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

See Also
========

_strong_gens_from_distr, _orbits_transversals_from_bsgs,
_handle_precomputed_bsgs

r   r   )r	   r   r
   _array_formappend_af_newlist)r   gensr   r   _stabsmax_stab_indexgenr    kr   s              r   _distribute_gens_by_baser-   v   s    ` 4yH!W\\Fx)ARE)NQ,3??47#;tw#FFA Q,3??47#;tw#FNq1uAHOOC    >A%x0U6] 345 1L *s   C=Nc                     Uc  [        X5      nUc  Uc  [        X5      u  p2ON[        XSS9nOCUc@  [        U 5      nS/U-  n[        U5       H   n[	        X&   R                  5       5      X6'   M"     X#U4$ )a  
Calculate BSGS-related structures from those present.

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

The base and strong generating set must be provided; if any of the
transversals, basic orbits or distributed strong generators are not
provided, they will be calculated from the base and strong generating set.

Parameters
==========

base : the base
strong_gens : the strong generators
transversals : basic transversals
basic_orbits : basic orbits
strong_gens_distr : strong generators distributed by membership in basic stabilizers

Returns
=======

(transversals, basic_orbits, strong_gens_distr)
    where *transversals* are the basic transversals, *basic_orbits* are the
    basic orbits, and *strong_gens_distr* are the strong generators distributed
    by membership in basic stabilizers.

Examples
========

>>> from sympy.combinatorics.named_groups import DihedralGroup
>>> from sympy.combinatorics.util import _handle_precomputed_bsgs
>>> D = DihedralGroup(3)
>>> D.schreier_sims()
>>> _handle_precomputed_bsgs(D.base, D.strong_gens,
... basic_orbits=D.basic_orbits)
([{0: (2), 1: (0 1 2), 2: (0 2)}, {1: (2), 2: (1 2)}], [[0, 1, 2], [1, 2]], [[(0 1 2), (0 2), (1 2)], [(1 2)]])

See Also
========

_orbits_transversals_from_bsgs, _distribute_gens_by_base

NT)transversals_only)r-   _orbits_transversals_from_bsgsr	   r
   r&   keys)r   strong_genstransversalsbasic_orbitsstrong_gens_distrr   r   s          r   _handle_precomputed_bsgsr6      s    \  4TG.tG 'L, /t=AC  4yH 6(?L8_"&|';';'="> %'888r   c           	      \   SSK Jn  [        U 5      nUS   S   R                  nS/U-  nS/U-  nUSL a  S/U-  n	[	        U5       HL  n
U" XaU
   X
   SSS9u  Xz'   X'   [        Xz   5      Xz'   USL d  M.  [        Xz   R                  5       5      W	U
'   MN     U(       a  U$ U(       d  W	U4$ W	Xx4$ )a  
Compute basic orbits and transversals from a base and strong generating set.

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

The generators are provided as distributed across the basic stabilizers.
If the optional argument ``transversals_only`` is set to True, only the
transversals are returned.

Parameters
==========

base : The base.
strong_gens_distr : Strong generators distributed by membership in basic stabilizers.
transversals_only : bool, default: False
    A flag switching between returning only the
    transversals and both orbits and transversals.
slp : bool, default: False
    If ``True``, return a list of dictionaries containing the
    generator presentations of the elements of the transversals,
    i.e. the list of indices of generators from ``strong_gens_distr[i]``
    such that their product is the relevant transversal element.

Examples
========

>>> from sympy.combinatorics import SymmetricGroup
>>> from sympy.combinatorics.util import _distribute_gens_by_base
>>> S = SymmetricGroup(3)
>>> S.schreier_sims()
>>> strong_gens_distr = _distribute_gens_by_base(S.base, S.strong_gens)
>>> (S.base, strong_gens_distr)
([0, 1], [[(0 1 2), (2)(0 1), (1 2)], [(1 2)]])

See Also
========

_distribute_gens_by_base, _handle_precomputed_bsgs

r   )_orbit_transversalNFT)pairsslp)sympy.combinatorics.perm_groupsr8   r	   r   r
   dictr&   r1   )r   r5   r/   r:   r8   r   r   r3   slpsr4   r   s              r   r0   r0      s    V C4yHq!!$))F6(?L6(?DE!vh8_#5fPQ>R!%$$@ |/%"<?#7#7#9:LO  --\//r   c                    SSK Jn  [        U 5      nUS   R                  nUc  [	        X5      nUc3  / n[        U5       H"  nU" XcU   X   5      nUR                  U5        M$     UR                  / 5        USS n	[        US-
  SS5       Hw  nX7   SS n
X7    He  nXUS-      ;  d  M  U
SS nUR                  U5        U/ :X  a  M.  U" XlX   5      nXU   :X  d  MC  U
R                  U5        U	R                  U5        Mg     My     U	$ )a  
Remove redundant generators from a strong generating set.

Parameters
==========

base : a base
strong_gens : a strong generating set relative to *base*
basic_orbits : basic orbits
strong_gens_distr : strong generators distributed by membership in basic stabilizers

Returns
=======

A strong generating set with respect to ``base`` which is a subset of
``strong_gens``.

Examples
========

>>> from sympy.combinatorics import SymmetricGroup
>>> from sympy.combinatorics.util import _remove_gens
>>> from sympy.combinatorics.testutil import _verify_bsgs
>>> S = SymmetricGroup(15)
>>> base, strong_gens = S.schreier_sims_incremental()
>>> new_gens = _remove_gens(base, strong_gens)
>>> len(new_gens)
14
>>> _verify_bsgs(S, base, new_gens)
True

Notes
=====

This procedure is outlined in [1],p.95.

References
==========

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

r   )_orbitNr   )r;   r?   r	   r   r-   r
   r$   remove)r   r2   r4   r5   r?   r   r   r   basic_orbitres	gens_copyr+   	temp_gens
temp_orbits                 r   _remove_gensrG   8  s   X 74yH^  F 4TGxA 1)=twGK, ! R 
a.C8a<R(%(+	$'CAE22%aL	  %?#Ftw?
a0$$S)JJsO ( ) Jr   c                    U R                   n[        U5      n[        U5       HT  nXAU      nXqU   :X  a  M  XrU   ;  a  [        U5      US-   4s  $ X6   U   R                   n[	        [        U5      U5      nMV     [        U5      US-   4$ )ae  
Attempt to decompose a permutation using a (possibly partial) BSGS
structure.

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

This is done by treating the sequence ``base`` as an actual base, and
the orbits ``orbits`` and transversals ``transversals`` as basic orbits and
transversals relative to it.

This process is called "sifting". A sift is unsuccessful when a certain
orbit element is not found or when after the sift the decomposition
does not end with the identity element.

The argument ``transversals`` is a list of dictionaries that provides
transversal elements for the orbits ``orbits``.

Parameters
==========

g : permutation to be decomposed
base : sequence of points
orbits : list
    A list in which the ``i``-th entry is an orbit of ``base[i]``
    under some subgroup of the pointwise stabilizer of `
    `base[0], base[1], ..., base[i - 1]``. The groups themselves are implicit
    in this function since the only information we need is encoded in the orbits
    and transversals
transversals : list
    A list of orbit transversals associated with the orbits *orbits*.

Examples
========

>>> from sympy.combinatorics import Permutation, SymmetricGroup
>>> from sympy.combinatorics.util import _strip
>>> S = SymmetricGroup(5)
>>> S.schreier_sims()
>>> g = Permutation([0, 2, 3, 1, 4])
>>> _strip(g, S.base, S.basic_orbits, S.basic_transversals)
((4), 5)

Notes
=====

The algorithm is described in [1],pp.89-90. The reason for returning
both the current state of the element being decomposed and the level
at which the sifting ends is that they provide important information for
the randomized version of the Schreier-Sims algorithm.

References
==========

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

See Also
========

sympy.combinatorics.perm_groups.PermutationGroup.schreier_sims
sympy.combinatorics.perm_groups.PermutationGroup.schreier_sims_random

r   )r#   r	   r
   r%   r   r   )	gr   orbitsr3   hr   r   betaus	            r   _striprN     s    @ 	
A4yH8_az7?ay 1:q1u$$OD!--Z]A&  1:x!|##r   c                    [        U5      n[        US-   U5       H  nXU      n	XU   :X  a  M  XU   ;  a  U(       d  XS-   4s  $ XS-   U4s  $ X8   U	   n
X
:X  a  U(       d	  SUS-   4s  $ SUS-   U4s  $ [        [        U
5      U 5      n U(       d  Mx  Xh   U	   SS nUR	                  5         U Vs/ s H  oU44PM	     nnX-   nM     U(       d  XS-   4$ XS-   U4$ s  snf )z
optimized _strip, with h, transversals and result in array form
if the stripped elements is the identity, it returns False, base_len + 1

j    h[base[i]] == base[i] for i <= j

r   FN)r	   r
   r   r   reverse)rK   r   rJ   r3   r    r:   r=   r   r   rL   rM   u_slprI   s                r   	_strip_afrR     s    4yH1Q3!az7?ay a%x!eS= OD!6hl**(Q,++Z]A&3GDM!$EMMO(-.1!YE.+C% "& Q,lC	 /s   1Cc                     [        U 5      S:X  a  U S   SS $ U S   nU S    H  nX!;  d  M
  UR                  U5        M     U$ )a  
Retrieve strong generating set from generators of basic stabilizers.

This is just the union of the generators of the first and second basic
stabilizers.

Parameters
==========

strong_gens_distr : strong generators distributed by membership in basic stabilizers

Examples
========

>>> from sympy.combinatorics import SymmetricGroup
>>> from sympy.combinatorics.util import (_strong_gens_from_distr,
... _distribute_gens_by_base)
>>> S = SymmetricGroup(3)
>>> S.schreier_sims()
>>> S.strong_gens
[(0 1 2), (2)(0 1), (1 2)]
>>> strong_gens_distr = _distribute_gens_by_base(S.base, S.strong_gens)
>>> _strong_gens_from_distr(strong_gens_distr)
[(0 1 2), (2)(0 1), (1 2)]

See Also
========

_distribute_gens_by_base

r   r   N)r	   r$   )r5   resultr+   s      r   _strong_gens_from_distrrU     sU    @ " #A&&"1%$Q'C c" ( r   )NNN)FF)NN) sympy.combinatorics.permutationsr   r   r   sympy.ntheoryr   rmulr%   r   r!   r-   r6   r0   rG   rN   rR   rU    r   r   <module>rZ      sr    N N !


5p-`>B >BBF>9D AF=0@DNJ$Z 57R  B'r   