o
    ¦ƒðjœI  ã                   @   sž   d dl Z d dlmZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ G dd	„ d	ƒZddd„Zdd„ Zdd„ Zdd„ Zddd„Zdd„ ZdS )é    N)ÚFpGroupÚ
FpSubgroupÚsimplify_presentation)Ú	FreeGroup)ÚPermutationGroup)Úigcd)Útotient)ÚSc                   @   sˆ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd S )!ÚGroupHomomorphismzÖ
    A class representing group homomorphisms. Instantiate using `homomorphism()`.

    References
    ==========

    .. [1] Holt, D., Eick, B. and O'Brien, E. (2005). Handbook of computational group theory.

    c                 C   s(   || _ || _|| _d | _d | _d | _d S ©N)ÚdomainÚcodomainÚimagesÚ	_inversesÚ_kernelÚ_image)Úselfr   r   r   © r   ú£/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/combinatorics/homomorphisms.pyÚ__init__   s   
zGroupHomomorphism.__init__c           
      C   sâ   |   ¡ }i }t| j ¡ ƒD ]}| j| }||v s|js|||< qt| jtƒr*|j}n|j	}|D ]?}||v s8|jr9q/| j
j}t| jtƒrN|j| ddd… }n|}|D ]}	|	|v r_|||	  }qR|||	d  d  }qR|||< q/|S )zÚ
        Return a dictionary with `{gen: inverse}` where `gen` is a rewriting
        generator of `codomain` (e.g. strong generator for permutation groups)
        and `inverse` is an element of its preimage

        Néÿÿÿÿ)ÚimageÚlistr   ÚkeysÚis_identityÚ
isinstancer   r   Ústrong_gensÚ
generatorsr   ÚidentityÚ_strong_gens_slp)
r   r   ÚinversesÚkÚvÚgensÚgÚwÚpartsÚsr   r   r   Ú_invs   s2   
ÿ€
zGroupHomomorphism._invsc           	         sþ   ddl m} ddlm} t|||fƒrotˆ jtƒrˆ j |¡}ˆ jdu r)ˆ  	¡ ˆ _ˆ  
¡ }ˆ jj}tˆ jtƒrB| |¡ddd… }n|}tt|ƒƒD ]"}|| }|jrTqJ|ˆ jv ra|ˆ j|  }qJ|ˆ j|d  d  }qJ|S t|tƒr}‡ fdd„|D ƒS dS )aÏ  
        Return an element of the preimage of ``g`` or of each element
        of ``g`` if ``g`` is a list.

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

        If the codomain is an FpGroup, the inverse for equal
        elements might not always be the same unless the FpGroup's
        rewriting system is confluent. However, making a system
        confluent can be time-consuming. If it's important, try
        `self.codomain.make_confluent()` first.

        r   ©ÚPermutation)ÚFreeGroupElementNr   c                    ó   g | ]}ˆ   |¡‘qS r   )Úinvert©Ú.0Úe©r   r   r   Ú
<listcomp>k   ó    z,GroupHomomorphism.invert.<locals>.<listcomp>)Úsympy.combinatoricsr*   Úsympy.combinatorics.free_groupsr+   r   r   r   Úreducer   r(   r   r   r   r   Úgenerator_productÚrangeÚlenr   r   )	r   r$   r*   r+   r   r%   r#   Úir'   r   r1   r   r-   ?   s.   



ÿzGroupHomomorphism.invertc                 C   s   | j du r
|  ¡ | _ | j S )z0
        Compute the kernel of `self`.

        N)r   Ú_compute_kernelr1   r   r   r   Úkernelm   s   

zGroupHomomorphism.kernelc                 C   sÊ   | j }| ¡ }|tju rtdƒ‚g }t|tƒrt|jƒ}nt||dd�}|  	¡  ¡ }| ¡ | |krc| 
¡ }||  | |ƒ¡d  }||vr[| |¡ t|tƒrTt|ƒ}nt||dd�}| ¡ | |ks2|S )Nz9Kernel computation is not implemented for infinite groupsT)Únormalr   )r   Úorderr	   ÚInfinityÚNotImplementedErrorr   r   r   r   r   Úrandomr-   Úappend)r   ÚGÚG_orderr#   ÚKr:   Úrr!   r   r   r   r;   v   s*   
ÿ



ø	z!GroupHomomorphism._compute_kernelc                 C   sP   | j du r%tt| j ¡ ƒƒ}t| jtƒr| j |¡| _ | j S t	| j|ƒ| _ | j S )z/
        Compute the image of `self`.

        N)
r   r   Úsetr   Úvaluesr   r   r   Úsubgroupr   )r   rH   r   r   r   r   �   s   
ÿzGroupHomomorphism.imagec           	         sö   |ˆ j vrt|ttfƒr‡ fdd„|D ƒS tdƒ‚|jr ˆ jjS ˆ j}ˆ jj}tˆ j t	ƒrRˆ j j
|dd�}|D ]}|ˆ jv rE|| | }q7||d  d | }q7|S d}|jD ]!\}}|dk rf|| d }n|| }||| |  }|t|ƒ7 }qW|S )z*
        Apply `self` to `elem`.

        c                    r,   r   ©Ú_applyr.   r1   r   r   r2   ¡   r3   z,GroupHomomorphism._apply.<locals>.<listcomp>z2The supplied element does not belong to the domainT)Úoriginalr   r   )r   r   r   ÚtupleÚ
ValueErrorr   r   r   r   r   r7   Ú
array_formÚabs)	r   Úelemr   Úvaluer#   r$   r:   Ú_Úpr   r1   r   rK   š   s.   


øzGroupHomomorphism._applyc                 C   s
   |   |¡S r   rJ   )r   rQ   r   r   r   Ú__call__º   s   
zGroupHomomorphism.__call__c                 C   ó   |   ¡  ¡ dkS )z9
        Check if the homomorphism is injective

        é   )r<   r>   r1   r   r   r   Úis_injective½   ó   zGroupHomomorphism.is_injectivec                 C   s6   |   ¡  ¡ }| j ¡ }|tju r|tju rdS ||kS )z:
        Check if the homomorphism is surjective

        N)r   r>   r   r	   r?   )r   ÚimÚothr   r   r   Úis_surjectiveÄ   s
   
zGroupHomomorphism.is_surjectivec                 C   s   |   ¡ o|  ¡ S )z5
        Check if `self` is an isomorphism.

        )rX   r\   r1   r   r   r   Úis_isomorphismÐ   rY   z GroupHomomorphism.is_isomorphismc                 C   rV   )zs
        Check is `self` is a trivial homomorphism, i.e. all elements
        are mapped to the identity.

        rW   )r   r>   r1   r   r   r   Ú
is_trivial×   s   zGroupHomomorphism.is_trivialc                    s>   ˆ   ¡  ˆj¡stdƒ‚‡ ‡fdd„ˆ jD ƒ}tˆ jˆj|ƒS )z°
        Return the composition of `self` and `other`, i.e.
        the homomorphism phi such that for all g in the domain
        of `other`, phi(g) = self(other(g))

        z?The image of `other` must be a subgroup of the domain of `self`c                    s   i | ]	}|ˆˆ |ƒƒ“qS r   r   ©r/   r$   ©Úotherr   r   r   Ú
<dictcomp>é   ó    z-GroupHomomorphism.compose.<locals>.<dictcomp>)r   Úis_subgroupr   rN   r   r
   r   )r   ra   r   r   r`   r   Úcomposeß   s   zGroupHomomorphism.composec                    sD   t |tƒr| ˆ j¡stdƒ‚|}‡ fdd„|jD ƒ}t|ˆ j|ƒS )zh
        Return the restriction of the homomorphism to the subgroup `H`
        of the domain.

        z'Given H is not a subgroup of the domainc                    s   i | ]}|ˆ |ƒ“qS r   r   r_   r1   r   r   rb   õ   r3   z1GroupHomomorphism.restrict_to.<locals>.<dictcomp>)r   r   rd   r   rN   r   r
   r   )r   ÚHr   r   r   r1   r   Úrestrict_toì   s
   zGroupHomomorphism.restrict_toc                 C   sŽ   |  |  ¡ ¡stdƒ‚g }t|  ¡ jƒ}|jD ]-}|  |¡}||vr+| |¡ t|ƒ}|  ¡ jD ]}|| |vrC| || ¡ t|ƒ}q0q|S )z†
        Return the subgroup of the domain that is the inverse image
        of the subgroup ``H`` of the homomorphism image

        z&Given H is not a subgroup of the image)	rd   r   rN   r   r   r   r-   rB   r<   )r   rf   r#   ÚPÚhÚh_ir!   r   r   r   Úinvert_subgroupø   s    


€ýz!GroupHomomorphism.invert_subgroupN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   r-   r<   r;   r   rK   rU   rX   r\   r]   r^   re   rg   rk   r   r   r   r   r
   	   s"    
#.	 r
   r   Tc                    s  t | tttfƒstdƒ‚t ˆ tttfƒstdƒ‚| j‰t‡fdd„ˆD ƒƒs*tdƒ‚t‡ fdd„|D ƒƒs9tdƒ‚|rGt|ƒtˆƒkrGtdƒ‚t	ˆƒ‰t	|ƒ}| 
ˆ jgtˆƒt|ƒ  ¡ ˆ 
‡fd	d
„ˆD ƒ¡ ttˆ|ƒƒ}|r}t| ˆ |ƒs}tdƒ‚t| ˆ |ƒS )aŠ  
    Create (if possible) a group homomorphism from the group ``domain``
    to the group ``codomain`` defined by the images of the domain's
    generators ``gens``. ``gens`` and ``images`` can be either lists or tuples
    of equal sizes. If ``gens`` is a proper subset of the group's generators,
    the unspecified generators will be mapped to the identity. If the
    images are not specified, a trivial homomorphism will be created.

    If the given images of the generators do not define a homomorphism,
    an exception is raised.

    If ``check`` is ``False``, do not check whether the given images actually
    define a homomorphism.

    zThe domain must be a groupzThe codomain must be a groupc                 3   ó   � | ]}|ˆ v V  qd S r   r   r_   )r   r   r   Ú	<genexpr>#  ó   € zhomomorphism.<locals>.<genexpr>zCThe supplied generators must be a subset of the domain's generatorsc                 3   rp   r   r   r_   )r   r   r   rq   %  rr   z+The images must be elements of the codomainz>The number of images must be equal to the number of generatorsc                    s   g | ]}|ˆ vr|‘qS r   r   r_   )r#   r   r   r2   /  s    z homomorphism.<locals>.<listcomp>z-The given images do not define a homomorphism)r   r   r   r   Ú	TypeErrorr   ÚallrN   r9   r   Úextendr   ÚdictÚzipÚ_check_homomorphismr
   )r   r   r#   r   Úcheckr   )r   r   r#   r   Úhomomorphism  s&   rz   c                    sÈ   t | dƒr| n|  ¡ }|j}|j}dd„ |D ƒ}tt|| jƒƒ‰|j‰ ‡ ‡‡fdd„}|D ]4}t|tƒrW| 	||ƒˆ ¡}	|	du rV| 
¡ }
| 	||ƒˆ ¡}	|	du rV|
sVtdƒ‚n||ƒj}	|	sa dS q-d	S )
a]  
    Check that a given mapping of generators to images defines a homomorphism.

    Parameters
    ==========
    domain : PermutationGroup, FpGroup, FreeGroup
    codomain : PermutationGroup, FpGroup, FreeGroup
    images : dict
        The set of keys must be equal to domain.generators.
        The values must be elements of the codomain.

    Úrelatorsc                 S   s   g | ]}|j d  ‘qS )r   )Úext_repr_   r   r   r   r2   F  r3   z'_check_homomorphism.<locals>.<listcomp>c                    s0   ˆ }| j D ]\}}ˆ| }|ˆ| | 9 }q|S r   )rO   )rF   r%   ÚsymbolÚpowerr$   ©r   r   Úsymbols_to_domain_generatorsr   r   r   J  s
   z#_check_homomorphism.<locals>._imageNzÖCan't determine if the images define a homomorphism. Try increasing the maximum number of rewriting rules (group._rewriting_system.set_max(new_value); the current value is stored in group._rewriting_system.maxeqns)FT)ÚhasattrÚpresentationr{   r   rv   rw   r   r   r   ÚequalsÚmake_confluentÚRuntimeErrorr   )r   r   r   ÚpresÚrelsr#   Úsymbolsr   rF   r'   Úsuccessr   r   r   rx   6  s*   
€
ÿrx   c                    sš   ddl m‰  ddlm} |tˆƒƒ}|j‰tˆƒ‰‡ ‡‡fdd„| jD ƒ}| jˆd� t	| ||ƒ}t| j
ƒtˆƒkrD| j
tˆƒ |_|S t| jgƒ|_|S )z•
    Return the homomorphism induced by the action of the permutation
    group ``group`` on the set ``omega`` that is closed under the action.

    r   r)   ©ÚSymmetricGroupc                    s*   i | ]‰ ˆ ˆˆ‡ ‡fd d„ˆD ƒƒ “qS )c                    s   g | ]	}ˆ  |ˆ A ¡‘qS r   )Úindex)r/   Úo)r$   Úomegar   r   r2   r  rc   z1orbit_homomorphism.<locals>.<dictcomp>.<listcomp>r   ©r/   ©r*   r   rŽ   ©r$   r   rb   r  s   * z&orbit_homomorphism.<locals>.<dictcomp>)Úbase)r4   r*   Ú sympy.combinatorics.named_groupsr‹   r9   r   r   r   Ú_schreier_simsr
   Úbasic_stabilizersr   r   )ÚgrouprŽ   r‹   r   r   rf   r   r�   r   Úorbit_homomorphismg  s   ÿr—   c           	         s¾   ddl m‰  ddlm} t|ƒ}d}g ‰dg| ‰t|ƒD ]}|| |kr2ˆ |¡ |ˆ|< |d7 }qt|ƒD ]
}ˆ||  ˆ|< q7||ƒ}t|ƒ‰‡ ‡‡‡fdd„| jD ƒ}t| ||ƒ}|S )ab  
    Return the homomorphism induced by the action of the permutation
    group ``group`` on the block system ``blocks``. The latter should be
    of the same form as returned by the ``minimal_block`` method for
    permutation groups, namely a list of length ``group.degree`` where
    the i-th entry is a representative of the block i belongs to.

    r   r)   rŠ   NrW   c                    s(   i | ]‰ ˆ ˆ‡‡ ‡fd d„ˆD ƒƒ“qS )c                    s   g | ]
}ˆ ˆ| ˆA  ‘qS r   r   )r/   r:   )Úbr$   rT   r   r   r2   ›  s    z1block_homomorphism.<locals>.<dictcomp>.<listcomp>r   r�   ©r*   r˜   r   rT   r‘   r   rb   ›  s   ( z&block_homomorphism.<locals>.<dictcomp>)	r4   r*   r“   r‹   r9   r8   rB   r   r
   )	r–   Úblocksr‹   ÚnÚmr:   r   r   rf   r   r™   r   Úblock_homomorphism{  s&   	

€r�   c                 C   sÎ  t | ttfƒstdƒ‚t |ttfƒstdƒ‚t | tƒrGt |tƒrGt| ƒ} t|ƒ}| j|jkrG| j ¡ |j ¡ krG|s<dS dt| || j|jƒfS |}|  	¡ }| 	¡ }|t
ju rZtdƒ‚t |tƒrn|t
ju rhtdƒ‚| ¡ \}}||ksx| j|jkr~|s|dS dS |s�|}t|t|ƒƒdkr�dS t| jƒ}t |t|ƒ¡D ]F}	t|	ƒ}
|
 |jgt| jƒt|
ƒ  ¡ tt||
ƒƒ}t| ||ƒràt |tƒrÇ| |
¡}
t| || j|
dd�}| ¡ rà|sÚ dS d|f  S qš|sådS dS )aE  
    Compute an isomorphism between 2 given groups.

    Parameters
    ==========

    G : A finite ``FpGroup`` or a ``PermutationGroup``.
        First group.

    H : A finite ``FpGroup`` or a ``PermutationGroup``
        Second group.

    isomorphism : bool
        This is used to avoid the computation of homomorphism
        when the user only wants to check if there exists
        an isomorphism between the groups.

    Returns
    =======

    If isomorphism = False -- Returns a boolean.
    If isomorphism = True  -- Returns a boolean and an isomorphism between `G` and `H`.

    Examples
    ========

    >>> from sympy.combinatorics import free_group, Permutation
    >>> from sympy.combinatorics.perm_groups import PermutationGroup
    >>> from sympy.combinatorics.fp_groups import FpGroup
    >>> from sympy.combinatorics.homomorphisms import group_isomorphism
    >>> from sympy.combinatorics.named_groups import DihedralGroup, AlternatingGroup

    >>> D = DihedralGroup(8)
    >>> p = Permutation(0, 1, 2, 3, 4, 5, 6, 7)
    >>> P = PermutationGroup(p)
    >>> group_isomorphism(D, P)
    (False, None)

    >>> F, a, b = free_group("a, b")
    >>> G = FpGroup(F, [a**3, b**3, (a*b)**2])
    >>> H = AlternatingGroup(4)
    >>> (check, T) = group_isomorphism(G, H)
    >>> check
    True
    >>> T(b*a*b**-1*a**-1*b**-1)
    (0 2 3)

    Notes
    =====

    Uses the approach suggested by Robert Tarjan to compute the isomorphism between two groups.
    First, the generators of ``G`` are mapped to the elements of ``H`` and
    we check if the mapping induces an isomorphism.

    z2The group must be a PermutationGroup or an FpGroupTz<Isomorphism methods are not implemented for infinite groups.F)FNrW   )ry   )r   r   r   rs   r   r   r{   Úsortrz   r>   r	   r?   r@   Ú_to_perm_groupÚ
is_abelianr   r   r   Ú	itertoolsÚpermutationsr9   ru   r   rv   rw   rx   r-   r]   )rC   rf   ÚisomorphismÚ_HÚg_orderÚh_orderÚh_isomorphismr›   r#   Úsubsetr   Ú_imagesÚTr   r   r   Úgroup_isomorphismŸ  sZ   8 



 

€r«   c                 C   s   t | |dd�S )a  
    Check if the groups are isomorphic to each other

    Parameters
    ==========

    G : A finite ``FpGroup`` or a ``PermutationGroup``
        First group.

    H : A finite ``FpGroup`` or a ``PermutationGroup``
        Second group.

    Returns
    =======

    boolean
    F)r£   )r«   )rC   rf   r   r   r   Úis_isomorphic  s   r¬   )r   T)T)r¡   Úsympy.combinatorics.fp_groupsr   r   r   r5   r   Úsympy.combinatorics.perm_groupsr   Úsympy.core.intfuncr   Ú%sympy.functions.combinatorial.numbersr   Úsympy.core.singletonr	   r
   rz   rx   r—   r�   r«   r¬   r   r   r   r   Ú<module>   s       
)1
$t