
    wg                     r    d dl mZ d dlmZ d dlmZmZ d dlmZ  G d de      Z		 	 ddZ
	 	 dd	Z	 	 dd
Zy)    )
free_group)DefaultPrinting)chainproduct)bisect_leftc                       e Zd ZdZdZdZdZd$dZed        Z	d Z
d ZeZed	        Zd
 Zd%dZd Zd Zd Zd&dZd Zd'dZd%dZd'dZd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d%dZ#d  Z$d! Z%d" Z&d# Z'y)(
CosetTablea  

    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           	         |st         j                  }|| _        || _        || _        dg| _        t        t        j                  d | j                  j                  D                    | _
        d gt        | j                        z  g| _        d gt        | j                        z  g| _        | j                  D ci c]  }|| j                  j                  |        c}| _        i | _        | j                  j#                         D ]L  \  }}|dz  dk(  r | j                  |   dz   | j                   |<   .| j                  |   dz
  | j                   |<   N g | _        | j                  }t'        dj)                  t+        t        |            D cg c]  }d|z  	 c}            d   | _        d gt        | j                        z  g| _        i | _        y c c}w c c}w )Nr   c              3   *   K   | ]  }||d z  f  ywN ).0gens     d/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/combinatorics/coset_table.py	<genexpr>z&CosetTable.__init__.<locals>.<genexpr>@   s       *5 ,/R. *5s         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__zCosetTable.__init__7   s   #99J !+e)) *5==33*5 5 6 &TVV$%fS[()
3766:aq$&&,,q/):))+ 	8HAuqyA~%)[[^a%7"%)[[^a%7"		8  "MMtzzuSV}*M!6A:*MNOPQR	&TVV$% ; +Ns   :#GGc                     t        t        | j                              D cg c]  }| j                  |   |k(  s| c}S c c}w )zSet of live cosets. )r(   r   r   )r+   cosets     r   omegazCosetTable.omegaW   s4     $)TVV#5P%%9OPPPs
   A A c                    | j                  | j                  | j                        }| j                  D cg c]  }t	        |       c}|_        t	        | j
                        |_        t	        | j                        |_        |S c c}w )zJ
        Return a shallow copy of Coset Table instance ``self``.

        )	__class__r   r   r!   r   r   r&   )r+   	self_copyperm_reps      r   copyzCosetTable.copy\   sd    
 NN4==$--@	:>**Eh4>E	466l	$()=)=$>	! Fs   Bc                 <    d| j                   d| j                  dS )NzCoset Table on z with z as subgroup generators)r   r   r+   s    r   __str__zCosetTable.__str__g   s    ==$--1 	1    c                 L    | j                   syt        | j                        dz   S )zaThe number `n` represents the length of the sublist containing the
        live cosets.

        r   r   )r!   maxr4   r;   s    r   nzCosetTable.nm   s!     zz4::""r=   c                 B     t         fd j                  D               S )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   @   K   | ]  }d j                   |   v   y wN)r!   )r   r3   r+   s     r   r   z)CosetTable.is_complete.<locals>.<genexpr>   s     IUttzz%00Is   )anyr4   r;   s   `r   is_completezCosetTable.is_completex   s     IdjjIIIIr=   c                    | j                   }| j                  }t        |      }|| j                  k\  rt	        d| j                  z        |j                  dgt        |      z         | j                  j                  dgt        | j                         z         |}| j                  j                  |       |||   | j                  |   <   |||   | j                  |   <   |r| j                  j                  | j                  |   | j                  |   <   | j                  j                  | j                  |   | j                  |   <   | j                  j                  | j                  |<   yy)a  
        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+   alphar.   modifiedr   r!   	len_tablebetas           r   definezCosetTable.define   s/   $ FF

J	... P,,- . . 	dVCF]#tfS[()d'+eT[[^$*/dDOOA&',0II,>,>DFF5M$++a.)/3yy/A/ADFF4L+,!YY//DHHTN r=   c                    | j                   }| j                  }t        |      }|| j                  k\  rt	        d| j                  z        |j                  dgt        |      z         |}| j                  j                  |       |||   | j                  |   <   |||   | j                  |   <   | j                  j                  ||f       y)aX  
        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

        rG   N)
r   r!   r   r   rH   rI   r   r#   r$   r&   )r+   rK   r.   r   r!   rM   rN   s          r   define_czCosetTable.define_c   s     FF

J	... P,,- . . 	dVCF]#d'+eT[[^$*/dDOOA&'##UAJ/r=   c                 N   | j                   }| j                  }| j                  }|}d}t        |      }|}	|dz
  }
||
k  r5||   |||         '||   |||         }|dz  }||
k  r||   |||         '||
kD  r||	k7  r| j	                  ||	       y|
|k\  r5||	   |||
         '||	   |||
         }	|
dz  }
|
|k\  r||	   |||
         '|
|k  r| j	                  ||	       y|
|k(  r=|	||   |||      <   |||	   |||      <   | j
                  j                  |||   f       yy)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&   rI   r+   rK   wordr#   r$   r!   fr0   rbjs              r   scan_czCosetTable.scan_c   s    __


IE1fq&a/2>aQ)AFA 1fq&a/2> q5Av""1a(1fq*T!W"56BaDG,-AFA 1fq*T!W"56B q5 q!$!V()E!HVDG_%,-E!HZQ()  ''DG5	 r=   c                 d   | j                   }| j                  }| j                  }g }| j                  |||       t	        |      dkD  r|j                  d      }|D ]  }||   ||      }	|	d||	   ||   <   | j                  j                  |	|dz  f       | j                  |      }
| j                  |	      }||
   ||      | j                  |||
   ||      |       ||   ||      | j                  |
||   ||      |       |||
   ||   <   |
||   ||   <    t	        |      dkD  ryy)aQ  
        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&   rI   rep)r+   rK   rN   r#   r$   r!   qgammar.   deltamunus               r   rS   zCosetTable.coincidence_c   sL    __




5$"!fqjEE!HE 6eVAY/$26E%LA/((//2?%B%BRy+7

2uRy';Q?r:a=1=

2uRyA'?C/1b	&),35b	*Q-06 !fqjr=   c                 @   | j                   }| j                  }| j                  }|}	d}
t        |      }|}|dz
  }|}|r| j                  j
                  }d}|s|dk(  rd}|
|k  rR||	   |||
         D|r| j                  |	   |||
         z  }||	   |||
         }	|
dz  }
|
|k  r||	   |||
         D|
|kD  r4|	|k7  r.|r| j                  |	|dz  |z         y| j                  |	|       y||
k\  r\||   |||         N|r%|| j                  |   | j                  ||         z  }||   |||         }|dz  }||
k\  r||   |||         N||
k  r/|r| j                  |	|dz  |z         n| j                  |	|       n||
k(  ro|||	   |||
      <   |	||   |||
      <   |rPdz  |z  | j                  |	   | j                   ||
      <   |dz  |z  | j                  |   | j                  ||
      <   y|r| j                  |	||
   |       |r|dk(  ryy)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   rL   )
r#   r$   r!   r   r)   rJ   r    modified_coincidencecoincidencerO   )r+   rK   rU   yfillrL   r#   r$   r!   rV   r0   rW   rX   rY   b_pf_pflags                    r   scanzCosetTable.scan  s   ^ __


IE))$$CdaiDq&U1XfT!Wo6BdffQitAw88C!HVDG_-Q	 q&U1XfT!Wo6B
 1u611!QR	B  ((A.q&U1Xja&9:FdffQiQ(@AAC!HZQ01Q	 q&U1Xja&9:F
 1u --aCGCK@$$Q*a,-aQ)01aDG,-692gckDFF1Idkk$q'23:=r'#+DFF1Idood1g67AtAw:G dair=   c                    | j                   }| j                  }| j                  }|}d}t        |      }|}	|dz
  }
||
k  r5||   |||         '||   |||         }|dz  }||
k  r||   |||         '||
kD  r||	k(  S |
|k\  r5||	   |||
         '||	   |||
         }	|
dz  }
|
|k\  r||	   |||
         '|
|k  ry|
|k(  r|	||   |||      <   |||	   |||      <   y)a  
        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   rT   s              r   
scan_checkzCosetTable.scan_check}  sP   $ __


IE1fq&a/2>aQ)AFA 1fq&a/2> q56M1fq*T!W"56BaDG,-AFA 1fq*T!W"56B q5 !V()E!HVDG_%,-E!HZQ()r=   c                    | j                   }| j                  } |||      } |||      }	||	k7  rt        ||	      }
t        ||	      }|
||<   |rm||k(  r3| j                  |   dz  |z  | j                  |   z  | j                  |<   n5| j                  |   dz  |dz  z  | j                  |   z  | j                  |	<   |j                  |       yy)a  
        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

        re   r   N)r   r^   minr?   r*   rI   )r+   klamdar_   wrL   r   r^   phipsirb   vs               r   r\   zCosetTable.merge  s    2 FFhh!h'%(+#:S#BCAAaD8$(HHQKOA$5dhhuo$EDHHSM$(HHUOR$72$=dhhqk$IDHHSMHHQK r=   c                 @   | j                   }|}||   }|r|dd }||k7  r|r||<   |}||   }||k7  r|rJ|   }||k7  r>|}||   }|||<   | j                  |   | j                  |   z  | j                  |<   ||k7  r>|S |}||   }||k7  r|||<   |}||   }||k7  r|S )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+   rr   rL   r   rs   rhosrb   s           r   r^   zCosetTable.rep  s    X FFh!AUl#EE(C	 Ul
 E(C(e# $dhhrl :	 (  BB%C,"e , r=   c                    | j                   }| j                  }| j                  }g }|r| j                  ||||       n| j	                  |||       t        |      dkD  rx|j                  d      }	|D ]P  }
||	   ||
      }|d||   ||
   <   | j                  |	|      }| j                  ||      }||   ||
      |r| j                  |   dz  | j                  |	   | j                   |
      dz  z  }|| j                  |	   z  | j                  |   | j                   |
      z  }| j                  |||   | j                   |
      ||       | j	                  |||   ||
      |       ||   ||
      |r| j                  |	   dz  | j                  |	   | j                   |
      z  }|| j                  |   z  | j                  |   | j                  |
      z  }| j                  |||   | j                  |
      ||       | j	                  |||   ||
      |       |||   ||
   <   |||   ||
   <   |s| j                  |	   dz  | j                  |	   | j                   |
      z  | j                  |   z  }|| j                  |   | j                   |
   <   |dz  | j                  |   | j                  |
   <   S t        |      dkD  rwyy)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   Nre   r   )
r#   r$   r!   modified_merger\   r   r]   r^   r*   r    )r+   rK   rN   rt   rL   r#   r$   r!   r_   r`   r.   ra   rb   rc   rw   s                  r   rg   zCosetTable.coincidence  s   & __


tQ2JJudA&!fqjEE!HE CeVAY/$26E%LA/%(;B%(;BRy+7# $ 3DFF5M$++a.4QSU4U UA !$((5/ 1$&&*T[[^2L LA //E"Idkk!n4MqRST JJr59VAY+?Cr:a=1=# $ 3DFF5M$++a.4Q QA !$((5/ 1$&&*T__Q=O2P PA //E"Idooa>P4QSTVWX JJr59Z]+CQG/1b	&),35b	*Q-0# $ 3DFF5M$++a.4Q QRVRZRZ[`Ra aA9:DFF2Jt{{1~6=>UDFF2Jtq'9:5C !fqjr=   c                 ,    | j                  ||d       y)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ri   Nrm   )r+   rK   rU   s      r   scan_and_fillzCosetTable.scan_and_fillT  s     			%D	)r=   c                 |   | j                   }| j                  }| j                  }t        |      }|}d}|}	|dz
  }
	 ||
k  r5||   |||         '||   |||         }|dz  }||
k  r||   |||         '||
kD  r||	k7  r| j	                  ||	       y|
|k\  r5||	   |||
         '||	   |||
         }	|
dz  }
|
|k\  r||	   |||
         '|
|k  r| j	                  ||	       nW|
|k(  r=|	||   |||      <   |||	   |||      <   | j
                  j                  |||   f       n| j                  |||          )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   rS   r&   rI   rQ   )r+   rK   rU   r#   r$   r!   rW   rV   r0   rX   rY   s              r   scan_and_fill_czCosetTable.scan_and_fill_c`  s    __


IEq&U1XfT!Wo6B!HVDG_-Q q&U1XfT!Wo6B 1u6&&q!,q&U1Xja&9:F!HZQ01Q q&U1Xja&9:F 1u""1a(a,-aQ)01aDG,-$$++QQL9aa)) r=   c                     | j                   j                  }| j                  }| j                  D ]%  }|D ]  }| j	                  ||       ||   |k  s % ' y)aO  
        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   r4   rm   )r+   Rr   rN   rt   s        r   
look_aheadzCosetTable.look_ahead  s\     MM""FF JJ 	D 		$"T7T>	r=   c                 8   | j                   }| j                  }t        | j                        dkD  rt        | j                        t        j
                  k\  r | j                          | j                  dd= ^| j                  j                         \  }}||   |k(  r#|D ]  }| j                  ||       ||   |k  s n ||   | j                  |      }|+||   |k(  r#|D ]  }| j                  ||       ||   |k  s n t        | j                        dkD  ryy)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   r]   rZ   r#   )	r+   R_c_x	R_c_x_invr   r!   rK   r.   rt   rN   s	            r   process_deductionszCosetTable.process_deductions  s    FF

$&&'!+4''(J,E,EE!((+//335qU8u$" "E1-U8e+!" <A/DAdGtO" AKKa(w~ $&&'!+r=   c                 R   | j                   }t        | j                        dkD  r| j                  j                         \  }}|D ]  }| j	                  ||      r y ||   | j
                  |      }||D ]  }| j	                  ||      r y t        | j                        dkD  ry)z
        A variation of ``process_deductions``, this calls ``scan_check``
        wherever ``process_deductions`` calls ``scan``, described on Pg. [1].

        See Also
        ========

        process_deductions

        r   FT)r!   r   r&   r]   ro   r#   )r+   r   r   r!   rK   r.   rt   rN   s           r   process_deductions_checkz#CosetTable.process_deductions_check  s     

$&&'!+++//1HE1 !ua0 ! <A/D" %A??43$% $&&'!+ r=   c                    | j                   }| j                  }| j                  }|D ]  }||   ||      }||   ||      ||   ||   <   |||   ||   <   t        t	        | j
                              D ]I  }| j
                  |   |k(  s||   ||      |k(  r|||   ||   <   0||   ||      |k(  s?|||   ||   <   K  y)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+   rN   r`   r   r#   r!   r.   zrK   s	            r   switchzCosetTable.switch  s     FF

 		7AeVAY'A&+Dk&)&<E%L#%&E$Kq	"s466{+ 766%=E)U|F1I.$627eVAY/ufQi0E926eVAY/7			7r=   c                    | j                   }| j                  }d}t        t        | j                        |      D ]M  \  }}| j
                  |   ||      }||k\  s!||kD  r| j                  ||       |dz  }|| j                  k(  sM y y)a1  
        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(   r@   r!   r   )r+   r   r#   r`   rK   r.   rN   s          r   standardizezCosetTable.standardize  s    F FFdffq1 	HE1::e$VAY/Du}%<KKt,
DFF?	r=   c           
         d}| j                   }| j                  }| j                  }| j                  }t	        t        t        | j                              D cg c]  }| j                  |   |k7  s| c}      }| j                  D ]8  }|dz  }||k7  s|D ]&  }	||   ||	      }
|
||   ||	   <   ||
   ||	      |k(   ( : t        t        |dz               | _        |t        | j                        d= |D ]?  }t        t        | j                               D ]  }||xx   t        |||         z  cc<    A yc c}w )z]Removes the non-live cosets from the coset table, described on
        pg. 167 [1].

        r   r   N)r   r#   r$   r!   tupler(   r   r   r4   r   r   )r+   r`   r   r#   r$   r!   r0   chirK   r.   rN   rowrY   s                r   compresszCosetTable.compress!  s;   
 FF__


c$&&k 2E1dffQi1nQEFZZ 	8EQJE~ 8A <q	2D.2E%L+$K
1.%78		8 eEAI&'#dff+, 	3C3tvv;' 3A+c3q6223	3 Fs   D;0D;c                 8   t        t        j                  d |D                    }t               }|D ]  }|j	                  |      } g }| j
                  D ]<  }|D ch c]  }|d   |k(  s| }}|j                  |       |j                  |       > |S c c}w )Nc              3   b   K   | ]'  }|j                         |d z  j                         f ) ywr   cyclic_conjugatesr   rels     r   r   z(CosetTable.conjugates.<locals>.<genexpr>>  s6      '=36 ),(=(=(?b++-(/ '=   -/r   )r   r   r   setunionr   rI   difference_update)	r+   r   R_cR_set	conjugateR_c_listr.   rU   rW   s	            r   
conjugateszCosetTable.conjugates=  s    5&& '=:;'= = > 	+IKK	*E	+ 	'A"'8$47a<8A8OOA##A&	'  9s   B+Bc                     | j                   D ]\  }| j                  |   | j                  |      }|dk(  r| j                  j                  c S ||k  sE| j                  |      |dz  z  c S  y)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   rJ   coset_representative)r+   r3   r.   r`   s       r   r   zCosetTable.coset_representativeJ  sj    .  	>AJJu%dkk!n5Ez}}---u}0072==	>r=   c                 ,    | j                  ||d       y)a  
        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

        Tre   N)rO   )r+   rK   r.   s      r   modified_definezCosetTable.modified_definel  s    " 	E1t,r=   c                 0    | j                  ||||d       y)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)rh   ri   rL   Nr   )r+   rK   rt   rh   ri   s        r   modified_scanzCosetTable.modified_scan  s     			%adT	:r=   c                 .    | j                  |||d       y )NTr~   )r   )r+   rK   rt   rh   s       r   modified_scan_and_fillz!CosetTable.modified_scan_and_fill  s    5!QT2r=   c                 0    | j                  ||||d       y)a  
        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rt   rL   N)r\   )r+   rr   rs   rt   r_   s        r   r|   zCosetTable.modified_merge  s     	

1eQ!d
3r=   c                 *    | j                  |d       y)z
        Parameters
        ==========

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

        See Also
        ========

        rep
        Tre   N)r^   )r+   rr   s     r   modified_repzCosetTable.modified_rep  s     	T"r=   c                 .    | j                  |||d       y)z
        Parameters
        ==========

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

        See Also
        ========

        coincidence

        Tr   N)rg   )r+   rK   rN   rt   s       r   rf   zCosetTable.modified_coincidence  s     	D9r=   rC   )F)NFF)NF)(__name__
__module____qualname____doc__r   r   r   r1   propertyr4   r9   r<   __repr__r@   rE   rO   rQ   rZ   rS   rm   ro   r\   r^   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r|   r   rf   r   r=   r   r	   r	      s    . $ N@ Q Q	1 H# #J%0N0<*6^#6J^;D*X&PDP8Cv
***Z(@070-`38>D-&; 34 #:r=   r	   Nc                 $   t        | ||      }|r|j                  }|j                  }n|j                  }|j                  }|r(|j
                  dd |_        |j                  dd |_        | j                  }	|j                  }
|j                  }t        t        |            D ]5  }|r% |d||   |j                  j                  |          * |d||          7 d}||j                  k  r||   |k(  rj	 |	D ]6  }|r ||||j                  j                         n	 |||       ||   |k  s6 n ||   |k(  r&|
D ]!  }|j
                  |   |
|       |||       # |dz  }||j                  k  r|S # t        $ r}|r|cY d}~S |d}~ww xY w)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   rO   r!   r   r   r#   r(   r   r)   r   r@   rJ   rH   )r,   Yr-   draft
incompleterL   C_scan_and_fill_definer   r#   r   r0   rK   rt   r.   es                    r   coset_enumeration_rr     s   x 	614A11##((++a.ggajAXXF	A3q6] $1adAFF$5$5a$891ad#	$
 E
!##+U8u A&uaA&ua0x%' U8u$# .775>&)4<#E1-. 	
' !##+( H  Hs0   67E6 .%E6 E6 6	F?F
FF

Fc                 $    t        | ||||d      S )a  
    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   rL   )r   )r,   r   r-   r   r   s        r   modified_coset_enumeration_rr     s!    @ vqZu(2TC Cr=   c                    | j                   }| j                  }t        | ||      }|r|j                  dd |_        |j                  dd |_        |j
                  |_        t        t        t        |j                              |      D ]B  \  }}	|j                  |   |j                  |	      &|j
                  j                  ||	f       D |j                  }
|D cg c]  }|j                          }}t        t        j                  d |D                    }t!               }|D ]  }|j#                  |      } g }|j                  D ]<  }	|D ch c]  }|d   |	k(  s| }}|j                  |       |j%                  |       > |D ]  }|j'                  d|        |
D ]4  }	|j)                  ||j                  |	      ||j*                  |	             6 d}|t        |j                        k  r|j                  |   |k(  r	 |j                  D ]z  }	|j                  |   |k7  r nf|j                  |   |j                  |	      7|j-                  ||	       |j)                  ||j                  |	      ||j*                  |	             | |dz  }|t        |j                        k  r|S c c}w c c}w # t.        $ r}|r|cY d}~S |d}~ww xY w)aV  
    >>> 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   b   K   | ]'  }|j                         |d z  j                         f ) ywr   r   r   s     r   r   z&coset_enumeration_c.<locals>.<genexpr>  s3      #" %($9$9$;c2g=X=X=Z#[ #"r   r   r   )r   r   r	   r!   r   r&   r   r(   r   r#   rI   r   identity_cyclic_reductionr   r   r   r   r   r   r   r   r$   rQ   rH   )r,   r   r-   r   r   Xr   r   rK   r.   r   r   	R_cyc_redr   r   r   r   rU   rW   rt   r   s                        r   coset_enumeration_cr     s    	AA614A++a.ggaj!11c!''l 3Q7 	5HE1wwu~ahhqk*6!!((%4	5 	
A<=>S..0>I>
u"" #" #" " #CEE '	I&' HSS ##4dtAw!|T44"#   	!Q  O	Xahhqk2HQ\\!_4MNOE
#agg,
33u:
 _Ass5zU*wwu~ahhqk2:

5!,,,Xahhqk-BHQ\\Z[_D]^_ 	
 #agg,
 H? ? 5"  Hs>   J4J9"J9AJ> AJ> >	KK
KKK)NNFF)NNF)sympy.combinatorics.free_groupsr   sympy.printing.defaultsr   	itertoolsr   r   bisectr   r	   r   r   r   r   r=   r   <module>r      sM    6 3 $ q: q:p ;??DDL DH/4!CJ ;?;@8r=   