o
    §ƒðj¿P  ã                   @   sD  d Z ddlmZ ddlZddl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 dd
lmZmZ ddlmZmZmZmZ ddlmZ ddlmZmZmZmZ ddl m!Z! ddl"m#Z# G dd„ deƒZ$		d)dd„Z%dd„ Z&d*dd„Z'd*dd„Z(d*dd„Z)d*d d!„Z*d*d"d#„Z+d*d$d%„Z,e#dddd&œd'd(„ƒZ-dS )+zà
Compute Galois groups of polynomials.

We use algorithms from [1], with some modifications to use lookup tables for
resolvents.

References
==========

.. [1] Cohen, H. *A Course in Computational Algebraic Number Theory*.

é    )ÚdefaultdictN)ÚDummyÚsymbols)Ú	is_square©ÚZZ)Ú
dup_random)Údup_eval)Údup_discriminant)Údup_factor_listÚdup_irreducible_p)ÚGaloisGroupExceptionÚget_resolvent_by_lookupÚdefine_resolventsÚ	Resolvent)Úcoeff_search)ÚPolyÚpoly_from_exprÚPolificationFailedÚComputationFailed)Ú	dup_sqf_p)Úpublicc                   @   s   e Zd ZdS )ÚMaxTriesExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú§/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/polys/numberfields/galoisgroups.pyr   #   s    r   é
   é   Tc                    sN  t dƒ}|  ¡ }|du rtƒ }| | j¡ |ri ‰ d}d}‡ fdd„}	t|ƒD ]}}
|rh|	|ƒ}t|ƒ}tdd„ |D ƒƒ}|| |kr[|dkrO|d	7 }|d	 }n|d	8 }|	|ƒ}t|ƒ}td	ƒgd
d„ |D ƒ }nt	|
d d	 |ƒ}t
 d|d	 ¡}t|| |tƒ}t|| jƒ}t|  || ¡|ƒ}|j|vr¤t|j ¡ tƒr¤||f  S q't‚)a  
    Given a univariate, monic, irreducible polynomial over the integers, find
    another such polynomial defining the same number field.

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

    See Alg 6.3.4 of [1].

    Parameters
    ==========

    T : Poly
        The given polynomial
    max_coeff : int
        When choosing a transformation as part of the process,
        keep the coeffs between plus and minus this.
    max_tries : int
        Consider at most this many transformations.
    history : set, None, optional (default=None)
        Pass a set of ``Poly.rep``'s in order to prevent any of these
        polynomials from being returned as the polynomial ``U`` i.e. the
        transformation of the given polynomial *T*. The given poly *T* will
        automatically be added to this set, before we try to find a new one.
    fixed_order : bool, default True
        If ``True``, work through candidate transformations A(x) in a fixed
        order, from small coeffs to large, resulting in deterministic behavior.
        If ``False``, the A(x) are chosen randomly, while still working our way
        up from small coefficients to larger ones.

    Returns
    =======

    Pair ``(A, U)``

        ``A`` and ``U`` are ``Poly``, ``A`` is the
        transformation, and ``U`` is the transformed polynomial that defines
        the same number field as *T*. The polynomial ``A`` maps the roots of
        *T* to the roots of ``U``.

    Raises
    ======

    MaxTriesException
        if could not find a polynomial before exceeding *max_tries*.

    ÚXNé   é   c                    s   ˆ   | t| dƒ¡}|ˆ | < |S )Né   )Úgetr   )ÚdegreeÚgen©Úcoeff_generatorsr   r   Úget_coeff_generatorc   s   z9tschirnhausen_transformation.<locals>.get_coeff_generatorc                 s   s   � | ]}t |ƒV  qd S )N)Úabs©Ú.0Úcr   r   r   Ú	<genexpr>x   s   € z/tschirnhausen_transformation.<locals>.<genexpr>r#   c                 S   s   g | ]}t |ƒ‘qS r   r   r+   r   r   r   Ú
<listcomp>�   s    z0tschirnhausen_transformation.<locals>.<listcomp>é   )r   r%   ÚsetÚaddÚrepÚrangeÚnextÚmaxr   ÚminÚrandomÚrandintr   r   r&   Ú	resultantr   Úto_listr   )ÚTÚ	max_coeffÚ	max_triesÚhistoryÚfixed_orderr    ÚnÚdeg_coeff_sumÚcurrent_degreer)   Úir&   ÚcoeffsÚmÚaÚCÚdÚAÚUr   r'   r   Útschirnhausen_transformation'   s@   1
€rL   c                 C   s$   t | tƒr	|  ¡ nt| tƒ}t|ƒS )z?Convenience to check if a Poly or dup has square discriminant. )Ú
isinstancer   Údiscriminantr
   r   r   )r<   rI   r   r   r   Úhas_square_disc’   s   rO   Fc                 C   s(   ddl m} t| ƒr|jdfS |jdfS )z~
    Compute the Galois group of a polynomial of degree 3.

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

    Uses Prop 6.3.5 of [1].

    r   )ÚS3TransitiveSubgroupsTF)Úsympy.combinatorics.galoisrP   rO   ÚA3ÚS3)r<   r>   Ú	randomizerP   r   r   r   Ú_galois_group_degree_3˜   s   
ÿrU   c                    sð  ddl m} ddlm} tdƒ}|d |d  |d |d   }|dƒ|dƒddƒ|dƒddƒg}t|||ƒ}|d |d d  |d |d d   |d |d d   |d |d d   }	|dƒ|dƒddƒg}
tƒ }t|ƒD ]„}|dkr‚t| ||| d�\}} |j	| d	d
�\}}}t
|tƒs’qqt| ƒ}|du rª|r£|jd	f  S |jdf  S |r³|jd	f  S || ‰ |	jt|ˆ |ƒƒd	d�}‡ fdd„|
D ƒ}t|||ƒ}| 	| ¡\}}}t|tƒ}|dkräqqt|ƒrï|jdf  S |jdf  S t‚)zª
    Compute the Galois group of a polynomial of degree 4.

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

    Follows Alg 6.3.7 of [1], using a pure root approximation approach.

    r   ©ÚPermutation©ÚS4TransitiveSubgroupszX0 X1 X2 X3r"   r#   r!   ©r>   r?   r@   T)Úfind_integer_rootNF©Úsimultaneousc                    ó   g | ]}ˆ | ˆ  ‘qS r   r   ©r,   Útau©Úsigmar   r   r/   ï   ó    z6_galois_group_degree_4_root_approx.<locals>.<listcomp>)Ú sympy.combinatorics.permutationsrW   rQ   rY   r   r   r1   r4   rL   Úeval_for_polyr   r   rO   ÚA4ÚS4ÚVÚsubsÚzipr
   r   ÚC4ÚD4r   )r<   r>   rT   rW   rY   r    ÚF1Ús1ÚR1ÚF2_preÚs2_prer?   rD   Ú_ÚR_dupÚi0Úsq_discÚF2Ús2ÚR2rI   r   ra   r   Ú"_galois_group_degree_4_root_approx§   sT   
 ýPþ
þ
ÿ
ry   c           
      C   sä   ddl m} tƒ }t|ƒD ]}t| dƒ}t|tƒr nt| ||| d�\}} qt‚t	|tƒ}t
tdd„ |d D ƒg ƒƒ}	|	dgkrOt| ƒrJ|jdfS |jd	fS |	g d
¢krZ|jd	fS |	g d¢kre|jdfS |	ddgksmJ ‚|jd	fS )z¢
    Compute the Galois group of a polynomial of degree 4.

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

    Based on Alg 6.3.6 of [1], but uses resolvent coeff lookup.

    r   rX   rZ   c                 S   s"   g | ]\}}t |ƒd  g| ‘qS )r#   ©Úlen)r,   ÚrÚer   r   r   r/     s    ÿz1_galois_group_degree_4_lookup.<locals>.<listcomp>r#   é   TF©r#   r#   é   )r"   r"   r"   r"   r€   )rQ   rY   r1   r4   r   r   r   rL   r   r   ÚsortedÚsumrO   rf   rg   rk   rh   rl   )
r<   r>   rT   rY   r?   rD   rs   rr   ÚflÚLr   r   r   Ú_galois_group_degree_4_lookupþ   s6   


þ

ÿþ
ÿ


r…   c                    s   ddl m} ddlm} tdƒ}tƒ }|d \}}}	|j|Ž }t|||	ƒ}
tƒ }d}t	|ƒD ]ß}|dkr?t
| ||| d�\}} t| dƒ}t|tƒsJq.|snt| ƒ}t|tƒre|r^|jd	f  S |jdf  S |sn|jdf  S d	}|
 | ¡}| ¡ D ]\}}t||tƒs… nqy|}|d |d d
  |d |d
 d
   |d
 |d d
   |d |d d
   |d |d d
   }|dƒ|dƒddƒd
dƒg}|}|	| ‰ |jt|ˆ |ƒƒd	d�}‡ fdd„|D ƒ}t|||ƒ}| | ¡\}}}t|tƒ}|dkrûq.t|ƒ�r|jd	f  S |jd	f  S t‚)zÜ
    Compute the Galois group of a polynomial of degree 5.

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

    Based on Alg 6.3.9 of [1], but uses a hybrid approach, combining resolvent
    coeff lookup, with root approximation.

    r   ©ÚS5TransitiveSubgroupsrV   zX0,X1,X2,X3,X4)r0   r#   FrZ   r#   Tr"   r!   r€   r\   c                    r^   r   r   r_   ra   r   r   r/   k  rc   z1_galois_group_degree_5_hybrid.<locals>.<listcomp>)rQ   r‡   rd   rW   r   r   Úas_exprr   r1   r4   rL   r   r   r   rO   r   ÚA5ÚS5ÚM20Ú round_roots_to_integers_for_polyÚitemsr	   ri   rj   re   r
   r   ÚC5ÚD5r   )r<   r>   rT   r‡   rW   ÚX5ÚresÚF51rr   Ús51ÚR51r?   Úreached_second_stagerD   ÚR51_dupru   Úrounded_rootsÚpermutation_indexÚcandidate_rootr    rp   rq   rt   rv   rw   rx   rs   rI   r   ra   r   Ú_galois_group_degree_5_hybrid)  sd   

þ


ÿ
ÿdþ

rš   c                 C   sÈ   ddl m} | }tƒ }t|ƒD ]}t| dƒ}t|tƒr nt| ||| d�\}} qt‚t	| ƒ}	t
|tƒr@|	r;|jdfS |jdfS |	sG|jdfS t|t |¡d� ¡ d }
t|
ƒdkr_|jdfS |jdfS )	zÖ
    Compute the Galois group of a polynomial of degree 5.

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

    Based on Alg 6.3.9 of [1], but uses resolvent coeff lookup, plus
    factorization over an algebraic extension.

    r   r†   r#   rZ   TF)Údomainr0   )rQ   r‡   r1   r4   r   r   r   rL   r   rO   r   r‰   rŠ   r‹   r   Úalg_field_from_polyÚfactor_listr{   rŽ   r�   )r<   r>   rT   r‡   Ú_Tr?   rD   rs   rr   ru   rƒ   r   r   r   Ú(_galois_group_degree_5_lookup_ext_factorz  s.   

þ
ÿ


rŸ   c                 C   s`  ddl m} tƒ }t|ƒD ]}t| dƒ}t|tƒr nt| ||| d�\}} qt‚t	|tƒ}t
tƒ}	|d D ]\}
}|	t|
ƒd   |
¡ q6ttdd„ |	 ¡ D ƒg ƒƒ}t| ƒ}|g d¢krr|	d d }t|ƒrm|jd	fS |jd	fS |ddgkr’|	d \}}t|ƒp…t|ƒ}|r�|jd	fS |jd	fS |d
dgkr³|rŸ|jdfS |	d d }t|ƒr®|jd	fS |jd	fS |g d¢krÅ|rÀ|jdfS |jd	fS |ddgkr×|rÒ|jdfS |jd	fS |g d¢krâ|jd	fS |dgkséJ ‚tƒ }t|ƒD ]}t| d
ƒ}t|tƒrþ nt| ||| d�\}} qðt‚t| ƒ}t|tƒ�r#|�r|jdfS |j d	fS |�r+|j!dfS |j"d	fS )z£
    Compute the Galois group of a polynomial of degree 6.

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

    Based on Alg 6.3.10 of [1], but uses resolvent coeff lookup.

    r   )ÚS6TransitiveSubgroupsr#   rZ   c                 S   s   g | ]\}}|gt |ƒ ‘qS r   rz   )r,   rI   Úffr   r   r   r/   Ç  s    ÿz1_galois_group_degree_6_lookup.<locals>.<listcomp>)r#   r"   r!   r!   Fr"   r€   Tr   r0   )r#   r#   r#   r!   r~   )#rQ   r    r1   r4   r   r   r   rL   r   r   r   Úlistr{   Úappendr�   r‚   r�   rO   ÚC6ÚD6ÚG18ÚG36mÚS4pÚA4xC2ÚS4xC2rf   ÚS4mÚPSL2F5ÚPGL2F5rS   r   ÚA6ÚS6ÚG36pÚG72)r<   r>   rT   r    r?   rD   rs   rr   rƒ   Úfactors_by_degr|   r„   ÚT_has_sq_discÚf1Úf2Ú
any_squarer   r   r   Ú_galois_group_degree_6_lookup§  sˆ   


þ

ÿþÿÿ
ÿÿÿ


þÿÿr·   ©Úby_namer>   rT   c          	   
   O   sd   |pg }|pi }zt | g|¢R i |¤Ž\}}W n ty) } ztdd|ƒ‚d}~ww |j|||d�S )a˜  
    Compute the Galois group for polynomials *f* up to degree 6.

    Examples
    ========

    >>> from sympy import galois_group
    >>> from sympy.abc import x
    >>> f = x**4 + 1
    >>> G, alt = galois_group(f)
    >>> print(G)
    PermutationGroup([
    (0 1)(2 3),
    (0 2)(1 3)])

    The group is returned along with a boolean, indicating whether it is
    contained in the alternating group $A_n$, where $n$ is the degree of *T*.
    Along with other group properties, this can help determine which group it
    is:

    >>> alt
    True
    >>> G.order()
    4

    Alternatively, the group can be returned by name:

    >>> G_name, _ = galois_group(f, by_name=True)
    >>> print(G_name)
    S4TransitiveSubgroups.V

    The group itself can then be obtained by calling the name's
    ``get_perm_group()`` method:

    >>> G_name.get_perm_group()
    PermutationGroup([
    (0 1)(2 3),
    (0 2)(1 3)])

    Group names are values of the enum classes
    :py:class:`sympy.combinatorics.galois.S1TransitiveSubgroups`,
    :py:class:`sympy.combinatorics.galois.S2TransitiveSubgroups`,
    etc.

    Parameters
    ==========

    f : Expr
        Irreducible polynomial over :ref:`ZZ` or :ref:`QQ`, whose Galois group
        is to be determined.
    gens : optional list of symbols
        For converting *f* to Poly, and will be passed on to the
        :py:func:`~.poly_from_expr` function.
    by_name : bool, default False
        If ``True``, the Galois group will be returned by name.
        Otherwise it will be returned as a :py:class:`~.PermutationGroup`.
    max_tries : int, default 30
        Make at most this many attempts in those steps that involve
        generating Tschirnhausen transformations.
    randomize : bool, default False
        If ``True``, then use random coefficients when generating Tschirnhausen
        transformations. Otherwise try transformations in a fixed order. Both
        approaches start with small coefficients and degrees and work upward.
    args : optional
        For converting *f* to Poly, and will be passed on to the
        :py:func:`~.poly_from_expr` function.

    Returns
    =======

    Pair ``(G, alt)``
        The first element ``G`` indicates the Galois group. It is an instance
        of one of the :py:class:`sympy.combinatorics.galois.S1TransitiveSubgroups`
        :py:class:`sympy.combinatorics.galois.S2TransitiveSubgroups`, etc. enum
        classes if *by_name* was ``True``, and a :py:class:`~.PermutationGroup`
        if ``False``.

        The second element is a boolean, saying whether the group is contained
        in the alternating group $A_n$ ($n$ the degree of *T*).

    Raises
    ======

    ValueError
        if *f* is of an unsupported degree.

    MaxTriesException
        if could not complete before exceeding *max_tries* in those steps
        that involve generating Tschirnhausen transformations.

    See Also
    ========

    .Poly.galois_group

    Úgalois_groupr#   Nr¸   )r   r   r   rº   )	Úfr¹   r>   rT   ÚgensÚargsÚFÚoptÚexcr   r   r   rº     s   b€ÿÿrº   )r   r   NT)r   F).Ú__doc__Úcollectionsr   r8   Úsympy.core.symbolr   r   Úsympy.ntheory.primetestr   Úsympy.polys.domainsr   Úsympy.polys.densebasicr   Úsympy.polys.densetoolsr	   Úsympy.polys.euclidtoolsr
   Úsympy.polys.factortoolsr   r   Ú*sympy.polys.numberfields.galois_resolventsr   r   r   r   Ú"sympy.polys.numberfields.utilitiesr   Úsympy.polys.polytoolsr   r   r   r   Úsympy.polys.sqfreetoolsr   Úsympy.utilitiesr   r   rL   rO   rU   ry   r…   rš   rŸ   r·   rº   r   r   r   r   Ú<module>   s8    
ÿk


W
+
Q
-]