
    \h                      x    S SK Jr  S SKJr  S SKJrJr  S SKJr   " S S\5      r	  SS jr
  SS	 jr  SS
 jrg)    )
free_group)DefaultPrinting)chainproduct)bisect_leftc                      \ rS rSrSrSrSrSrS&S jr\	S 5       r
S rS	 r\r\	S
 5       rS rS'S jrS rS rS rS(S jrS rS)S jrS'S jrS)S jrS rS rS rS rS rS rS rS r S r!S r"S r#S'S  jr$S! r%S" r&S# r'S$ r(S%r)g)*
CosetTable   aK  

Properties
==========

[1] `0 \in \Omega` and `\tau(1) = \epsilon`
[2] `\alpha^x = \beta \Leftrightarrow \beta^{x^{-1}} = \alpha`
[3] If `\alpha^x = \beta`, then `H \tau(\alpha)x = H \tau(\beta)`
[4] `\forall \alpha \in \Omega, 1^{\tau(\alpha)} = \alpha`

References
==========

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

.. [2] John J. Cannon; Lucien A. Dimino; George Havas; Jane M. Watson
       Mathematics of Computation, Vol. 27, No. 123. (Jul., 1973), pp. 463-490.
       "Implementation and Analysis of the Todd-Coxeter Algorithm"

i > Nd   c           	         U(       d  [         R                  nXl        X l        X0l        S/U l        [        [        R                  " S U R                  R                   5       5      5      U l
        S /[        U R                  5      -  /U l        S /[        U R                  5      -  /U l        U R                   Vs0 s H  oDU R                  R                  U5      _M      snU l        0 U l        U R                  R#                  5        HN  u  pEUS-  S:X  a!  U R                  U   S-   U R                   U'   M/  U R                  U   S-
  U R                   U'   MP     / U l        U R                  n['        SR)                  [+        [        U5      5       Vs/ s H  nSU-  PM
     sn5      5      S   U l        S /[        U R                  5      -  /U l        0 U l        g s  snf s  snf )Nr   c              3   .   #    U  H  nXS -  4v   M     g7fN ).0gens     W/var/www/auris/envauris/lib/python3.13/site-packages/sympy/combinatorics/coset_table.py	<genexpr>&CosetTable.__init__.<locals>.<genexpr>@   s      *53C ,/R.3s         z, za_%d)r	   coset_table_max_limitfp_groupsubgroupcoset_table_limitplistr   from_iterable
generatorsAlenPtableindexA_dict
A_dict_invitemsdeduction_stackr   joinrange_grpp_p)selffp_grpr   
max_cosetsxr$   His           r   __init__CosetTable.__init__7   s   #99J !+e)) *5==33*5 5 6 &TVV$%fS[()
3766:6a$&&,,q/)6:))+HAqyA~%)[[^a%7"%)[[^a%7"	 ,  "MMtzzuSV}*M}!6A:}*MNOPQR	&TVV$% ; +Ns   =%GG$c                     [        [        U R                  5      5       Vs/ s H  oR                  U   U:X  d  M  UPM     sn$ s  snf )zSet of live cosets. )r*   r!   r   )r-   cosets     r   omegaCosetTable.omegaW   s8     $)TVV#5P#5%%9O#5PPPs
   AAc                    U R                  U R                  U R                  5      nU R                   Vs/ s H  n[	        U5      PM     snUl        [	        U R
                  5      Ul        [	        U R                  5      Ul        U$ s  snf )z:
Return a shallow copy of Coset Table instance ``self``.

)	__class__r   r   r#   r   r   r(   )r-   	self_copyperm_reps      r   copyCosetTable.copy\   sh    
 NN4==$--@	:>**E*h4>*E	466l	$()=)=$>	! Fs   Bc                 @    SU R                   < SU R                  < S3$ )NzCoset Table on z with z as subgroup generators)r   r   r-   s    r   __str__CosetTable.__str__g   s    ==$--1 	1    c                 V    U R                   (       d  g[        U R                  5      S-   $ )zQThe number `n` represents the length of the sublist containing the
live cosets.

r   r   )r#   maxr7   r@   s    r   nCosetTable.nm   s!     zz4::""rC   c                 L   ^  [        U 4S jT R                   5       5      (       + $ )z
The coset table is called complete if it has no undefined entries
on the live cosets; that is, `\alpha^x` is defined for all
`\alpha \in \Omega` and `x \in A`.

c              3   H   >#    U  H  nS TR                   U   ;   v   M     g 7fN)r#   )r   r6   r-   s     r   r   )CosetTable.is_complete.<locals>.<genexpr>   s     IjUttzz%00j   ")anyr7   r@   s   `r   is_completeCosetTable.is_completex   s     IdjjIIIIrC   c                    U R                   nU R                  n[        U5      nX`R                  :  a  [	        SU R                  -  5      eUR                  S/[        U5      -  5        U R                  R                  S/[        U R                   5      -  5        UnU R                  R                  U5        XuU   U R                  U   '   XU   U R                  U   '   U(       a  U R                  R                  U R                  U   U R                  U   '   U R                  R                  U R                  U   U R                  U   '   U R                  R                  U R                  U'   gg)ak  
This routine is used in the relator-based strategy of Todd-Coxeter
algorithm if some `\alpha^x` is undefined. We check whether there is
space available for defining a new coset. If there is enough space
then we remedy this by adjoining a new coset `\beta` to `\Omega`
(i.e to set of live cosets) and put that equal to `\alpha^x`, then
make an assignment satisfying Property[1]. If there is not enough space
then we halt the Coset Table creation. The maximum amount of space that
can be used by Coset Table can be manipulated using the class variable
``CosetTable.coset_table_max_limit``.

See Also
========

define_c

ethe coset enumeration has defined more than %s cosets. Try with a greater value max number of cosets N)r    r#   r!   r   
ValueErrorappendr"   r   r%   r&   r+   identityr,   )r-   alphar0   modifiedr    r#   	len_tablebetas           r   defineCosetTable.define   s)   $ FF

J	... P,,- . . 	dVCF]#tfS[()d'+eT[[^$*/dDOOA&',0II,>,>DFF5M$++a.)/3yy/A/ADFF4L+,!YY//DHHTN rC   c                    U R                   nU R                  n[        U5      nXPR                  :  a  [	        SU R                  -  5      eUR                  S/[        U5      -  5        UnU R                  R                  U5        XdU   U R                  U   '   XU   U R                  U   '   U R                  R                  X45        g)a  
A variation of ``define`` routine, described on Pg. 165 [1], used in
the coset table-based strategy of Todd-Coxeter algorithm. It differs
from ``define`` routine in that for each definition it also adds the
tuple `(\alpha, x)` to the deduction stack.

See Also
========

define

rQ   N)
r    r#   r!   r   rR   rS   r   r%   r&   r(   )r-   rU   r0   r    r#   rW   rX   s          r   define_cCosetTable.define_c   s     FF

J	... P,,- . . 	dVCF]#d'+eT[[^$*/dDOOA&'##UJ/rC   c                 4   U R                   nU R                  nU R                  nUnSn[        U5      nUn	US-
  n
Xz::  a2  XV   X2U         b%  XV   X2U         nUS-  nXz::  a  XV   X2U         b  M%  Xz:  a  Xi:w  a  U R	                  Xi5        gX:  a2  XY   XBU
         b%  XY   XBU
         n	U
S-  n
X:  a  XY   XBU
         b  M%  X:  a  U R	                  Xi5        gX:X  a8  XU   X2U      '   XeU	   XBU      '   U R
                  R                  XbU   45        gg)z
A variation of ``scan`` routine, described on pg. 165 of [1], which
puts at tuple, whenever a deduction occurs, to deduction stack.

See Also
========

scan, scan_check, scan_and_fill, scan_and_fill_c

r   r   N)r%   r&   r#   r!   coincidence_cr(   rS   r-   rU   wordr%   r&   r#   fr2   rbjs              r   scan_cCosetTable.scan_c   sV    __


IEf&a/2>Q)AFA f&a/2> 5v""1(f*!W"56BG,-AFA f*!W"56B 5 q$V()!HVG_%,-!HZQ()  ''G5	 rC   c                 V   U R                   nU R                  nU R                  n/ nU R                  XU5        [	        U5      S:  a  UR                  S5      nU H  nXW   X8      n	U	c  M  SXY   XH   '   U R                  R                  XS-  45        U R                  U5      n
U R                  U	5      nXZ   X8      b  U R                  XU
   X8      U5        M  X[   XH      b  U R                  XU   XH      U5        M  XU
   X8   '   XU   XH   '   M     [	        U5      S:  a  M  gg)a  
A variation of ``coincidence`` routine used in the coset-table based
method of coset enumeration. The only difference being on addition of
a new coset in coset table(i.e new coset introduction), then it is
appended to ``deduction_stack``.

See Also
========

coincidence

r   Nr   )	r%   r&   r#   merger!   popr(   rS   rep)r-   rU   rX   r%   r&   r#   qgammar0   deltamunus               r   r_   CosetTable.coincidence_c   s#    __




5"!fqjEE!HEVY/$26EL/((//2?%B%By+7

2Ry';Q?:=1=

2Ry'?C/1b	&),35b	*-0  !fqjrC   c                 t   U R                   nU R                  nU R                  nUn	Sn
[        U5      nUnUS-
  nUnU(       a  U R                  R
                  nSnU(       d  US:X  Ga  SnX::  aS  X   XbU
         bF  U(       a  WU R                  U	   XbU
         -  nX   XbU
         n	U
S-  n
X::  a  X   XbU
         b  MF  X:  a7  X:w  a1  U(       a  U R                  XWS-  U-  5        gU R                  X5        gX:  a\  X   XrU         bO  U(       a#  XR                  U   U R                  X-         -  nX   XrU         nUS-  nX:  a  X   XrU         b  MO  X:  a2  U(       a  U R                  XWS-  U-  5        OU R                  X5        OX:X  an  XU	   XbU
      '   XU   XrU
      '   U(       aN  WS-  U-  U R                  U	   U R                   X*      '   US-  U-  U R                  U   U R                  X*      '   gU(       a  U R                  XU
   US9  U(       a  GM  US:X  a  GM  gg)a  
``scan`` performs a scanning process on the input ``word``.
It first locates the largest prefix ``s`` of ``word`` for which
`\alpha^s` is defined (i.e is not ``None``), ``s`` may be empty. Let
``word=sv``, let ``t`` be the longest suffix of ``v`` for which
`\alpha^{t^{-1}}` is defined, and let ``v=ut``. Then three
possibilities are there:

1. If ``t=v``, then we say that the scan completes, and if, in addition
`\alpha^s = \alpha^{t^{-1}}`, then we say that the scan completes
correctly.

2. It can also happen that scan does not complete, but `|u|=1`; that
is, the word ``u`` consists of a single generator `x \in A`. In that
case, if `\alpha^s = \beta` and `\alpha^{t^{-1}} = \gamma`, then we can
set `\beta^x = \gamma` and `\gamma^{x^{-1}} = \beta`. These assignments
are known as deductions and enable the scan to complete correctly.

3. See ``coicidence`` routine for explanation of third condition.

Notes
=====

The code for the procedure of scanning `\alpha \in \Omega`
under `w \in A*` is defined on pg. 155 [1]

See Also
========

scan_c, scan_check, scan_and_fill, scan_and_fill_c

Scan and Fill
=============

Performed when the default argument fill=True.

Modified Scan
=============

Performed when the default argument modified=True

r   r   Nr   rV   )
r%   r&   r#   r!   r+   rT   r"   modified_coincidencecoincidencerY   )r-   rU   ra   yfillrV   r%   r&   r#   rb   r2   rc   rd   re   b_pf_pflags                    r   scanCosetTable.scan  sY   ^ __


IE))$$CdaiD&UXf!Wo6BdffQiAw88CHVG_-Q	 &UXf!Wo6B
 u611!R	B  ((.&UXja&9:FffQi(@AACHZQ01Q	 &UXja&9:F
 u --aCGCK@$$Q*,-aQ)01aG,-692gckDFF1Idkk$'23:=r'#+DFF1Idoodg67AAw:G ddaiirC   c                    U R                   nU R                  nU R                  nUnSn[        U5      nUn	US-
  n
Xz::  a2  XV   X2U         b%  XV   X2U         nUS-  nXz::  a  XV   X2U         b  M%  Xz:  a  Xi:H  $ X:  a2  XY   XBU
         b%  XY   XBU
         n	U
S-  n
X:  a  XY   XBU
         b  M%  X:  a  gX:X  a  XU   X2U      '   XeU	   XBU      '   g)aj  
Another version of ``scan`` routine, described on, it checks whether
`\alpha` scans correctly under `word`, it is a straightforward
modification of ``scan``. ``scan_check`` returns ``False`` (rather than
calling ``coincidence``) if the scan completes incorrectly; otherwise
it returns ``True``.

See Also
========

scan, scan_c, scan_and_fill, scan_and_fill_c

r   r   FT)r%   r&   r#   r!   r`   s              r   
scan_checkCosetTable.scan_check}  s    $ __


IEf&a/2>Q)AFA f&a/2> 56Mf*!W"56BG,-AFA f*!W"56B 5 V()!HVG_%,-!HZQ()rC   c                    U R                   nU R                  nU" XS9nU" X%S9n	X:w  a  [        X5      n
[        X5      nXU'   U(       am  X:X  a3  U R                  U   S-  U-  U R                  U   -  U R                  U'   O5U R                  U   S-  US-  -  U R                  U   -  U R                  U	'   UR                  U5        gg)aF  
Merge two classes with representatives ``k`` and ``lamda``, described
on Pg. 157 [1] (for pseudocode), start by putting ``p[k] = lamda``.
It is more efficient to choose the new representative from the larger
of the two classes being merged, i.e larger among ``k`` and ``lamda``.
procedure ``merge`` performs the merging operation, adds the deleted
class representative to the queue ``q``.

Parameters
==========

'k', 'lamda' being the two class representatives to be merged.

Notes
=====

Pg. 86-87 [1] contains a description of this method.

See Also
========

coincidence, rep

rs   r   N)r   rk   minrE   r,   rS   )r-   klamdarl   wrV   r   rk   phipsiro   vs               r   ri   CosetTable.merge  s    2 FFhh!'%+:SBCAaD8$(HHQKOA$5dhhuo$EDHHSM$(HHUOR$72$=dhhqk$IDHHSMHHQK rC   c                 V   U R                   nUnX4   nU(       a  USS nXT:w  a  U(       a  UWU'   UnX4   nXT:w  a  M  U(       aI  WU   nXQ:w  a=  UnXg   nXCU'   U R                  U   U R                  U   -  U R                  U'   XQ:w  a  M=  U$ UnX7   nXT:w  a  XCU'   UnX7   nXT:w  a  M  U$ )a  
Parameters
==========

`k \in [0 \ldots n-1]`, as for ``self`` only array ``p`` is used

Returns
=======

Representative of the class containing ``k``.

Returns the representative of `\sim` class containing ``k``, it also
makes some modification to array ``p`` of ``self`` to ease further
computations, described on Pg. 157 [1].

The information on classes under `\sim` is stored in array `p` of
``self`` argument, which will always satisfy the property:

`p[\alpha] \sim \alpha` and `p[\alpha]=\alpha \iff \alpha=rep(\alpha)`
`\forall \in [0 \ldots n-1]`.

So, for `\alpha \in [0 \ldots n-1]`, we find `rep(self, \alpha)` by
continually replacing `\alpha` by `p[\alpha]` until it becomes
constant (i.e satisfies `p[\alpha] = \alpha`):w

To increase the efficiency of later ``rep`` calculations, whenever we
find `rep(self, \alpha)=\beta`, we set
`p[\gamma] = \beta \forall \gamma \in p-chain` from `\alpha` to `\beta`

Notes
=====

``rep`` routine is also described on Pg. 85-87 [1] in Atkinson's
algorithm, this results from the fact that ``coincidence`` routine
introduces functionality similar to that introduced by the
``minimal_block`` routine on Pg. 85-87 [1].

See Also
========

coincidence, merge

N)r   r,   )r-   r   rV   r   r   rhosro   s           r   rk   CosetTable.rep  s    X FFh!Al#E(C	 l
 E(C(e# $dhhrl :	 (  B%C,"e , rC   c                    U R                   nU R                  nU R                  n/ nU(       a  U R                  XX85        OU R	                  XU5        [        U5      S:  Gau  UR                  S5      n	U GHJ  n
Xy   XZ      nUc  M  SX{   Xj   '   U R                  XS9nU R                  XS9nX|   XZ      b  U(       a  U R                  U   S-  U R                  U	   U R                   U
      S-  -  nXR                  U	   -  U R                  U   U R                   U
      -  nU R                  XU   U R                   U
      X5        M  U R	                  XU   XZ      U5        M  X}   Xj      b  U(       a  U R                  U	   S-  U R                  U	   U R                   U
      -  nXR                  U   -  U R                  U   U R                  U
      -  nU R                  XU   U R                  U
      X5        GM  U R	                  XU   Xj      U5        GM  XU   XZ   '   XU   Xj   '   U(       d  GM  U R                  U	   S-  U R                  U	   U R                   U
      -  U R                  U   -  nXR                  U   U R                   U
   '   US-  U R                  U   U R                  U
   '   GMM     [        U5      S:  a  GMt  gg)a)  
The third situation described in ``scan`` routine is handled by this
routine, described on Pg. 156-161 [1].

The unfortunate situation when the scan completes but not correctly,
then ``coincidence`` routine is run. i.e when for some `i` with
`1 \le i \le r+1`, we have `w=st` with `s = x_1 x_2 \dots x_{i-1}`,
`t = x_i x_{i+1} \dots x_r`, and `\beta = \alpha^s` and
`\gamma = \alpha^{t-1}` are defined but unequal. This means that
`\beta` and `\gamma` represent the same coset of `H` in `G`. Described
on Pg. 156 [1]. ``rep``

See Also
========

scan

r   Nrs   r   )
r%   r&   r#   modified_mergeri   r!   rj   rk   r,   r"   )r-   rU   rX   r   rV   r%   r&   r#   rl   rm   r0   rn   ro   rp   r   s                  r   ru   CosetTable.coincidence  s   & __


Q2JJuA&!fqjEE!HEVY/$26EL/%;B%;By+7# $ 3DFF5M$++a.4QSU4U UA !((5/ 1$&&*T[[^2L LA //"Idkk!n4MqT JJr9VY+?C:=1=# $ 3DFF5M$++a.4Q QA !((5/ 1$&&*T__Q=O2P PA //"Idooa>P4QSTX JJr9Z]+CQG/1b	&),35b	*-0#8 $ 3DFF5M$++a.4Q QRVRZRZ[`Ra aA9:FF2Jt{{1~6=>UDFF2Jtq'9:5  !fqjjrC   c                 $    U R                  XSS9  g)a_  
A modified version of ``scan`` routine used in the relator-based
method of coset enumeration, described on pg. 162-163 [1], which
follows the idea that whenever the procedure is called and the scan
is incomplete then it makes new definitions to enable the scan to
complete; i.e it fills in the gaps in the scan of the relator or
subgroup generator.

Trw   Nr{   )r-   rU   ra   s      r   scan_and_fillCosetTable.scan_and_fillT  s     			%D	)rC   c                 `   U R                   nU R                  nU R                  n[        U5      nUnSnUn	US-
  n
 X::  a2  XW   X2U         b%  XW   X2U         nUS-  nX::  a  XW   X2U         b  M%  X:  a  Xy:w  a  U R	                  Xy5        gX:  a2  XY   XBU
         b%  XY   XBU
         n	U
S-  n
X:  a  XY   XBU
         b  M%  X:  a  U R	                  Xy5        OQX:X  a8  XU   X2U      '   XuU	   XBU      '   U R
                  R                  XrU   45        OU R                  XrU   5        M  )a>  
A modified version of ``scan`` routine, described on Pg. 165 second
para. [1], with modification similar to that of ``scan_anf_fill`` the
only difference being it calls the coincidence procedure used in the
coset-table based method i.e. the routine ``coincidence_c`` is used.

See Also
========

scan, scan_and_fill

r   r   N)r%   r&   r#   r!   r_   r(   rS   r\   )r-   rU   ra   r%   r&   r#   rc   rb   r2   rd   re   s              r   scan_and_fill_cCosetTable.scan_and_fill_c`  sg    __


IE&UXf!Wo6BHVG_-Q &UXf!Wo6B u6&&q,&UXja&9:FHZQ01Q &UXja&9:F u""1(,-aQ)01aG,-$$++QQL9aa)) rC   c                     U R                   R                  nU R                  nU R                   H(  nU H  nU R	                  X45        X#   U:  d  M    M&     M*     g)a  
When combined with the HLT method this is known as HLT+Lookahead
method of coset enumeration, described on pg. 164 [1]. Whenever
``define`` aborts due to lack of space available this procedure is
executed. This routine helps in recovering space resulting from
"coincidence" of cosets.

N)r   relatorsr   r7   r{   )r-   Rr   rX   r   s        r   
look_aheadCosetTable.look_ahead  sN     MM""FF JJD		$"7T>  rC   c                 D   U R                   nU R                  n[        U R                  5      S:  a  [        U R                  5      [        R
                  :  a!  U R                  5         U R                  SS2	 Ma  U R                  R                  5       u  pVX5   U:X  a$  U H  nU R                  XW5        X5   U:  d  M    O   XE   U R                  U      nUb,  X8   U:X  a$  U H  nU R                  X5        X8   U:  d  M    O   [        U R                  5      S:  a  M  gg)z
Processes the deductions that have been pushed onto ``deduction_stack``,
described on Pg. 166 [1] and is used in coset-table based enumeration.

See Also
========

deduction_stack

r   N)
r   r#   r!   r(   r	   max_stack_sizer   rj   rf   r%   )	r-   R_c_x	R_c_x_invr   r#   rU   r0   r   rX   s	            r   process_deductionsCosetTable.process_deductions  s     FF

$&&'!+4''(J,E,EE!((+//3358u$"E-8e+! # <A/DAGtO"AKK(w~ # $&&'!+rC   c                 d  ^ ^^ T R                   n[        T R                  5      S:  a  T R                  R                  5       u  mn[	        UU 4S jU 5       5      (       d  gUT   T R
                  U      mTb  [	        UU 4S jU 5       5      (       d  g[        T R                  5      S:  a  M  g)z
A variation of ``process_deductions``, this calls ``scan_check``
wherever ``process_deductions`` calls ``scan``, described on Pg. [1].

See Also
========

process_deductions

r   c              3   H   >#    U  H  nTR                  TU5      v   M     g 7frJ   r~   )r   r   rU   r-   s     r   r   6CosetTable.process_deductions_check.<locals>.<genexpr>  s     @%Qtua00%rL   Fc              3   H   >#    U  H  nTR                  TU5      v   M     g 7frJ   r   )r   r   rX   r-   s     r   r   r     s     GY4??433YrL   T)r#   r!   r(   rj   allr%   )r-   r   r   r#   r0   rU   rX   s   `    @@r   process_deductions_check#CosetTable.process_deductions_check  s     

$&&'!+++//1HE1@%@@@<A/DGYGGG  $&&'!+ rC   c                 v   U R                   nU R                  nU R                  nU H  nXR   XF      nXQ   XF      XR   XF   '   XuU   XF   '   [        [	        U R
                  5      5       HH  nU R
                  U   U:X  d  M  XX   XF      U:X  a  X%U   XF   '   M0  XX   XF      U:X  d  M?  XU   XF   '   MJ     M     g)zSwitch the elements `\beta, \gamma \in \Omega` of ``self``, used
by the ``standardize`` procedure, described on Pg. 167 [1].

See Also
========

standardize

N)r    r%   r#   r*   r!   r   )	r-   rX   rm   r    r%   r#   r0   zrU   s	            r   switchCosetTable.switch  s     FF

AVY'A&+k&)&<EL#%&$K	"s466{+66%=E)|FI.$627eVY/fi0E926eVY/ ,	 rC   c                    U R                   nU R                  nSn[        [        U R                  5      U5       HL  u  pEU R
                  U   X%      nXc:  d  M   Xc:  a  U R                  X65        US-  nX0R                  :X  d  ML    g   g)aY  
A coset table is standardized if when running through the cosets and
within each coset through the generator images (ignoring generator
inverses), the cosets appear in order of the integers
`0, 1, \dots, n`. "Standardize" reorders the elements of `\Omega`
such that, if we scan the coset table first by elements of `\Omega`
and then by elements of A, then the cosets occur in ascending order.
``standardize()`` is used at the end of an enumeration to permute the
cosets so that they occur in some sort of standard order.

Notes
=====

procedure is described on pg. 167-168 [1], it also makes use of the
``switch`` routine to replace by smaller integer value.

Examples
========

>>> from sympy.combinatorics import free_group
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r
>>> F, x, y = free_group("x, y")

# Example 5.3 from [1]
>>> f = FpGroup(F, [x**2*y**2, x**3*y**5])
>>> C = coset_enumeration_r(f, [])
>>> C.compress()
>>> C.table
[[1, 3, 1, 3], [2, 0, 2, 0], [3, 1, 3, 1], [0, 2, 0, 2]]
>>> C.standardize()
>>> C.table
[[1, 2, 1, 2], [3, 0, 3, 0], [0, 3, 0, 3], [2, 1, 2, 1]]

r   N)r    r%   r   r*   rF   r#   r   )r-   r    r%   rm   rU   r0   rX   s          r   standardizeCosetTable.standardize  sw    F FFdffq1HE::e$VY/D}<KK,
FF? 2rC   c           
         SnU R                   nU R                  nU R                  nU R                  n[	        [        [        U R                  5      5       Vs/ s H  o`R                  U   U:w  d  M  UPM     sn5      nU R                   H6  nUS-  nX:w  d  M  U H!  n	XX   X9      n
XU   X9   '   XZ   XI      U:H    M#     M8     [        [        US-   5      5      U l        U[        U R                  5      S2	 U H@  n[        [        U R                   5      5       H  nX==   [        X{U   5      -  ss'   M     MB     gs  snf )zMRemoves the non-live cosets from the coset table, described on
pg. 167 [1].

r   r   N)r    r%   r&   r#   tupler*   r!   r   r7   r   r   )r-   rm   r    r%   r&   r#   r2   chirU   r0   rX   rowre   s                r   compressCosetTable.compress  s   
 FF__


c$&&k 2E 21ffQi1nQ 2EFZZEQJE~A <	2D.2%L+ K
.%7 	   eEAI&'#dff+,C3tvv;'+cq622 ( ) Fs   E 2E c                 J   [        [        R                  " S U 5       5      5      n[        5       nU H  nUR	                  U5      nM     / nU R
                   HA  nU Vs1 s H  owS   U:X  d  M  UiM     nnUR                  U5        UR                  U5        MC     U$ s  snf )Nc              3   h   #    U  H(  nUR                  5       US -  R                  5       4v   M*     g7fr   cyclic_conjugatesr   rels     r   r   (CosetTable.conjugates.<locals>.<genexpr>B  s5      '=:;3 ),(=(=(?b++-(/:;   02r   )r   r   r   setunionr    rS   difference_update)	r-   r   R_cR_set	conjugateR_c_listr0   ra   rc   s	            r   
conjugatesCosetTable.conjugatesA  s    5&& '=:;'= = >IKK	*E A"'8%$7a<%A8OOA##A&   9s    B 0B c                     U R                    H^  nU R                  U   U R                  U      nUS:X  a  U R                  R                  s  $ X1:  d  MG  U R                  U5      US-  -  s  $    g)a  
Compute the coset representative of a given coset.

Examples
========

>>> from sympy.combinatorics import free_group
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r
>>> F, x, y = free_group("x, y")
>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
>>> C = coset_enumeration_r(f, [x])
>>> C.compress()
>>> C.table
[[0, 0, 1, 2], [1, 1, 2, 0], [2, 2, 0, 1]]
>>> C.coset_representative(0)
<identity>
>>> C.coset_representative(1)
y
>>> C.coset_representative(2)
y**-1

r   r   N)r    r#   r%   r   rT   coset_representative)r-   r6   r0   rm   s       r   r   CosetTable.coset_representativeN  sc    . AJJu%dkk!n5Ez}}---}0072== rC   c                 $    U R                  XSS9  g)z
Define a function p_p from from [1..n] to A* as
an additional component of the modified coset table.

Parameters
==========

\alpha \in \Omega
x \in A*

See Also
========

define

Trs   N)rY   )r-   rU   r0   s      r   modified_defineCosetTable.modified_definep  s    " 	Et,rC   c                 &    U R                  XX4SS9  g)z
Parameters
==========
\alpha \in \Omega
w \in A*
y \in (YUY^-1)
fill -- `modified_scan_and_fill` when set to True.

See Also
========

scan
T)rv   rw   rV   Nr   )r-   rU   r   rv   rw   s        r   modified_scanCosetTable.modified_scan  s     			%aT	:rC   c                 &    U R                  XUSS9  g )NTr   )r   )r-   rU   r   rv   s       r   modified_scan_and_fill!CosetTable.modified_scan_and_fill  s    5QT2rC   c                 &    U R                  XXCSS9  g)z
Parameters
==========

'k', 'lamda' -- the two class representatives to be merged.
q -- queue of length l of elements to be deleted from `\Omega` *.
w -- Word in (YUY^-1)

See Also
========

merge
Tr   rV   N)ri   )r-   r   r   r   rl   s        r   r   CosetTable.modified_merge  s     	

1Qd
3rC   c                 $    U R                  USS9  g)zG
Parameters
==========

`k \in [0 \ldots n-1]`

See Also
========

rep
Trs   N)rk   )r-   r   s     r   modified_repCosetTable.modified_rep  s     	T"rC   c                 &    U R                  XUSS9  g)z{
Parameters
==========

A coincident pair `\alpha, \beta \in \Omega, w \in Y \cup Y^{-1}`

See Also
========

coincidence

Tr   N)ru   )r-   rU   rX   r   s       r   rt   CosetTable.modified_coincidence  s     	D9rC   )r    r%   r&   r"   r+   r   r(   r   r   r,   r   r#   rJ   )F)NFF)NF)*__name__
__module____qualname____firstlineno____doc__r   r   r   r3   propertyr7   r=   rA   __repr__rF   rN   rY   r\   rf   r_   r{   r~   ri   rk   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   __static_attributes__r   rC   r   r	   r	      s    . $ N@ Q Q	1 H# #J%0N0<*6^#6J^;D*X&PDP8Cv
***Z(@,70-` 3D>D-&; 34 #:rC   r	   Nc                 X   [        XUS9nU(       a  UR                  nUR                  nOUR                  nUR                  nU(       a(  UR
                  SS Ul        UR                  SS Ul        U R                  n	UR                  n
UR                  n[        [        U5      5       H:  nU(       a%  U" SX   UR                  R                  U   5        M/  U" SX   5        M<     SnXR                  :  a  X   U:X  aq   U	 H:  nU(       a  U" XUR                  R                  5        OU" X5        X   U:  d  M:    O   X   U:X  a(  U
 H"  nUR
                  U   X      b  M  U" X5        M$     US-  nXR                  :  a  M  U$ ! [         a  nU(       a  Us SnA$ UeSnAff = f)a  
This is easier of the two implemented methods of coset enumeration.
and is often called the HLT method, after Hazelgrove, Leech, Trotter
The idea is that we make use of ``scan_and_fill`` makes new definitions
whenever the scan is incomplete to enable the scan to complete; this way
we fill in the gaps in the scan of the relator or subgroup generator,
that's why the name relator-based method.

An instance of `CosetTable` for `fp_grp` can be passed as the keyword
argument `draft` in which case the coset enumeration will start with
that instance and attempt to complete it.

When `incomplete` is `True` and the function is unable to complete for
some reason, the partially complete table will be returned.

# TODO: complete the docstring

See Also
========

scan_and_fill,

Examples
========

>>> from sympy.combinatorics.free_groups import free_group
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r
>>> F, x, y = free_group("x, y")

# Example 5.1 from [1]
>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
>>> C = coset_enumeration_r(f, [x])
>>> for i in range(len(C.p)):
...     if C.p[i] == i:
...         print(C.table[i])
[0, 0, 1, 2]
[1, 1, 2, 0]
[2, 2, 0, 1]
>>> C.p
[0, 1, 2, 1, 1]

# Example from exercises Q2 [1]
>>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3])
>>> C = coset_enumeration_r(f, [])
>>> C.compress(); C.standardize()
>>> C.table
[[1, 2, 3, 4],
[5, 0, 6, 7],
[0, 5, 7, 6],
[7, 6, 5, 0],
[6, 7, 0, 5],
[2, 1, 4, 3],
[3, 4, 2, 1],
[4, 3, 1, 2]]

# Example 5.2
>>> f = FpGroup(F, [x**2, y**3, (x*y)**3])
>>> Y = [x*y]
>>> C = coset_enumeration_r(f, Y)
>>> for i in range(len(C.p)):
...     if C.p[i] == i:
...         print(C.table[i])
[1, 1, 2, 1]
[0, 0, 0, 2]
[3, 3, 1, 0]
[2, 2, 3, 3]

# Example 5.3
>>> f = FpGroup(F, [x**2*y**2, x**3*y**5])
>>> Y = []
>>> C = coset_enumeration_r(f, Y)
>>> for i in range(len(C.p)):
...     if C.p[i] == i:
...         print(C.table[i])
[1, 3, 1, 3]
[2, 0, 2, 0]
[3, 1, 3, 1]
[0, 2, 0, 2]

# Example 5.4
>>> F, a, b, c, d, e = free_group("a, b, c, d, e")
>>> f = FpGroup(F, [a*b*c**-1, b*c*d**-1, c*d*e**-1, d*e*a**-1, e*a*b**-1])
>>> Y = [a]
>>> C = coset_enumeration_r(f, Y)
>>> for i in range(len(C.p)):
...     if C.p[i] == i:
...         print(C.table[i])
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

# example of "compress" method
>>> C.compress()
>>> C.table
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

# Exercises Pg. 161, Q2.
>>> F, x, y = free_group("x, y")
>>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3])
>>> Y = []
>>> C = coset_enumeration_r(f, Y)
>>> C.compress()
>>> C.standardize()
>>> C.table
[[1, 2, 3, 4],
[5, 0, 6, 7],
[0, 5, 7, 6],
[7, 6, 5, 0],
[6, 7, 0, 5],
[2, 1, 4, 3],
[3, 4, 2, 1],
[4, 3, 1, 2]]

# John J. Cannon; Lucien A. Dimino; George Havas; Jane M. Watson
# Mathematics of Computation, Vol. 27, No. 123. (Jul., 1973), pp. 463-490
# from 1973chwd.pdf
# Table 1. Ex. 1
>>> F, r, s, t = free_group("r, s, t")
>>> E1 = FpGroup(F, [t**-1*r*t*r**-2, r**-1*s*r*s**-2, s**-1*t*s*t**-2])
>>> C = coset_enumeration_r(E1, [r])
>>> for i in range(len(C.p)):
...     if C.p[i] == i:
...         print(C.table[i])
[0, 0, 0, 0, 0, 0]

Ex. 2
>>> F, a, b = free_group("a, b")
>>> Cox = FpGroup(F, [a**6, b**6, (a*b)**2, (a**2*b**2)**2, (a**3*b**3)**5])
>>> C = coset_enumeration_r(Cox, [a])
>>> index = 0
>>> for i in range(len(C.p)):
...     if C.p[i] == i:
...         index += 1
>>> index
500

# Ex. 3
>>> F, a, b = free_group("a, b")
>>> B_2_4 = FpGroup(F, [a**4, b**4, (a*b)**4, (a**-1*b)**4, (a**2*b)**4,             (a*b**2)**4, (a**2*b**2)**4, (a**-1*b*a*b)**4, (a*b**-1*a*b)**4])
>>> C = coset_enumeration_r(B_2_4, [a])
>>> index = 0
>>> for i in range(len(C.p)):
...     if C.p[i] == i:
...         index += 1
>>> index
1024

References
==========

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

r/   Nr   r   )r	   r   r   r   rY   r#   r   r   r%   r*   r!   r+   r   rF   rT   rR   )r.   Yr/   draft
incompleterV   C_scan_and_fill_definer   r%   r   r2   rU   r   r0   es                    r   coset_enumeration_rr     sx   x 	64A11##((++a.ggajAXXF	A3q6]1adAFF$5$5a$891ad#	 
 E
##+8uA&uA&u0x%'  8u$#775>&)4<#E- $ 	
' ##+( H  Hs0   8F
 >$F
 &F
 

F)F$F)"F$$F)c           	          [        XX#USS9$ )aC  
Introduce a new set of symbols y \in Y that correspond to the
generators of the subgroup. Store the elements of Y as a
word P[\alpha, x] and compute the coset table similar to that of
the regular coset enumeration methods.

Examples
========

>>> from sympy.combinatorics.free_groups import free_group
>>> from sympy.combinatorics.fp_groups import FpGroup
>>> from sympy.combinatorics.coset_table import modified_coset_enumeration_r
>>> F, x, y = free_group("x, y")
>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
>>> C = modified_coset_enumeration_r(f, [x])
>>> C.table
[[0, 0, 1, 2], [1, 1, 2, 0], [2, 2, 0, 1], [None, 1, None, None], [1, 3, None, None]]

See Also
========

coset_enumertation_r

References
==========

.. [1] Holt, D., Eick, B., O'Brien, E.,
       "Handbook of Computational Group Theory",
       Section 5.3.2
T)r/   r   r   rV   )r   )r.   r   r/   r   r   s        r   modified_coset_enumeration_rr     s    @ vZ(2TC CrC   c                    U R                   nU R                  n[        XUS9nU(       a  UR                  SS Ul        UR                  SS Ul        UR
                  Ul        [        [        [        UR                  5      5      U5       HC  u  pUR                  U   UR                  U	      c  M'  UR
                  R                  X45        ME     UR                  n
U Vs/ s H  oR                  5       PM     nn[        [        R                  " S U 5       5      5      n[!        5       nU H  nUR#                  U5      nM     / nUR                   HB  n	U Vs1 s H  nUS   U	:X  d  M  UiM     nnUR                  U5        UR%                  U5        MD     U H  nUR'                  SU5        M     U
 H5  n	UR)                  UUR                  U	      UUR*                  U	      5        M7     SnU[        UR                  5      :  a  UR                  U   U:X  a   UR                   H}  n	UR                  U   U:w  a    OjUR                  U   UR                  U	      b  M:  UR-                  X5        UR)                  UUR                  U	      UUR*                  U	      5        M      US-  nU[        UR                  5      :  a  M  U$ s  snf s  snf ! [.         a  nU(       a  Us SnA$ UeSnAff = f)a6  
>>> from sympy.combinatorics.free_groups import free_group
>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_c
>>> F, x, y = free_group("x, y")
>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
>>> C = coset_enumeration_c(f, [x])
>>> C.table
[[0, 0, 1, 2], [1, 1, 2, 0], [2, 2, 0, 1]]

r   Nc              3   h   #    U  H(  nUR                  5       US -  R                  5       4v   M*     g7fr   r   r   s     r   r   &coset_enumeration_c.<locals>.<genexpr>  s2      #"  %($9$9$;c2g=X=X=Z#[ r   r   r   )r   r   r	   r#   r   r(   r   r*   r!   r%   rS   r    identity_cyclic_reductionr   r   r   r   r   r   r   r   r&   r\   rR   )r.   r   r/   r   r   Xr   r   rU   r0   r    r   	R_cyc_redr   r   r   r   ra   rc   r   r   s                        r   coset_enumeration_cr     s    	AA64A++a.ggaj!11c!''l 3Q7HEwwu~ahhqk*6!!((%4 8 	
A<=>AS..0AI>
u"" #" #" " #CEE	I&  HSS#4edtAw!|Te4"  	!Q 	Xahhqk2HQ\\!_4MN E
#agg,
33u:
Ass5zU*wwu~ahhqk2:

5,,,Xahhqk-BHQ\\Z[_D]^  	
 #agg,
 H? ? 5"  HsC   "KK-K #K K &AK 
K9$K4,K92K44K9)NNFF)NNF)sympy.combinatorics.free_groupsr   sympy.printing.defaultsr   	itertoolsr   r   bisectr   r	   r   r   r   r   rC   r   <module>r      sM    6 3 $ u: u:x ;??DDL DH/4!CJ ;?;@8rC   