Ë
    7^(hž)  ã                   ór  — d Z ddl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 dd	lmZmZ dd
lmZ ddlmZ ddlmZ edk(  rdgZedk(  r8ddlZej2                  j5                  d«      ^ZZZ ee«       ee«      fdk  rdZndZd„ Zd„ Z d„ Z!e eddg¬«       G d„ de	e«      «       «       Z"e"xZ#Z$y)z.Implementation of :class:`FiniteField` class. é    N)ÚGROUND_TYPES)Údoctest_depends_on)Ú
int_valued)ÚField)ÚModularIntegerFactory)ÚSimpleDomain)Úgf_zassenhausÚgf_irred_p_rabin)ÚCoercionFailed)Úpublic)ÚSymPyIntegerÚflintÚFiniteFieldú.)r   é   c                 óÎ   ‡ ‡‡‡— t         j                  Š ‰‰ «      Š t        j                  Št        j                  Š	  ‰d‰ «       ˆˆ ˆfd„}ˆ ˆfd„}||fS # t
        $ r Y yw xY w)Nr   )NNc                 óT   •— 	  ‰| ‰«      S # t         $ r  ‰ ‰| «      ‰«      cY S w xY w©N©Ú	TypeError)ÚxÚindexÚmodÚnmods    €€€ú]/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/polys/domains/finitefield.pyÚctxz&_modular_int_factory_nmod.<locals>.ctx/   s4   ø€ ð	'Ù˜˜3“<ÐøÜò 	'Ù™˜a› #Ó&Ò&ð	'ús   ƒ Œ'¦'c                 ó   •—  ‰| ‰«      S r   © )Úcsr   Ú	nmod_polys    €€r   Úpoly_ctxz+_modular_int_factory_nmod.<locals>.poly_ctx5   s   ø€ Ù˜˜SÓ!Ð!ó    )Úoperatorr   r   r   r    ÚOverflowError)r   r   r!   r   r   r    s   `  @@@r   Ú_modular_int_factory_nmodr%   "   s_   û€ ä�N‰N€EÙ
�‹*€CÜ�:‰:€DÜ—‘€IðÙˆQ�Œö'õ"ð �ˆ=Ðøô ò Ùðús   ¾	A Á	A$Á#A$c                 ó¾   ‡‡‡‡— t         j                  Št        j                  | «      Št        j                  | «      Št        j
                  Šˆˆfd„}ˆˆfd„}||fS )Nc                 óP   •— 	  ‰| «      S # t         $ r  ‰ ‰| «      «      cY S w xY wr   r   )r   Úfctxr   s    €€r   r   z*_modular_int_factory_fmpz_mod.<locals>.ctxA   s.   ø€ ð	"Ù˜“7ˆNøÜò 	"á™˜a›“>Ò!ð	"ús   ƒ ‹%¤%c                 ó   •—  ‰| ‰«      S r   r   )r   Ú	fctx_polyÚfmpz_mod_polys    €€r   r!   z/_modular_int_factory_fmpz_mod.<locals>.poly_ctxH   s   ø€ Ù˜R Ó+Ð+r"   )r#   r   r   Úfmpz_mod_ctxÚfmpz_mod_poly_ctxr+   )r   r   r!   r(   r*   r+   r   s      @@@@r   Ú_modular_int_factory_fmpz_modr.   ;   sM   û€ Ü�N‰N€EÜ×Ñ˜cÓ"€DÜ×'Ñ'¨Ó,€IÜ×'Ñ'€Mõ"õ,ð �ˆ=Ðr"   c                 ó  — 	 |j                  | «      } d\  }}}t        �0| j	                  «       r d}t        | «      \  }}|€t        | «      \  }}|€t        | |||«      }d }|||fS # t        $ r t        d| z  «      ‚w xY w)Nz"modulus must be an integer, got %s)NNFT)Úconvertr   Ú
ValueErrorr   Úis_primer%   r.   r   )r   ÚdomÚ	symmetricÚselfr   r!   Úis_flints          r   Ú_modular_int_factoryr7   N   s©   € ðEØ�k‰k˜#Óˆð 0Ñ€Cˆ�8ô Ð˜SŸ\™\œ^àˆô 2°#Ó6‰ˆˆXàˆ;ä9¸#Ó>‰MˆC�à
€{ô $ C¨¨i¸Ó>ˆØˆà�˜(Ð"Ð"øô/ ò EÜÐ=ÀÑCÓDÐDðEús   ‚A& Á&A>ÚpythonÚgmpy)Úmodulesc                   óþ   — e Zd ZdZdZdZdxZZdZdZ	dZ
dZdZd!d„Zed„ «       Ze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„ 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„ Z&d„ Z'd „ Z(y)#r   a	  Finite field of prime order :ref:`GF(p)`

    A :ref:`GF(p)` domain represents a `finite field`_ `\mathbb{F}_p` of prime
    order as :py:class:`~.Domain` in the domain system (see
    :ref:`polys-domainsintro`).

    A :py:class:`~.Poly` created from an expression with integer
    coefficients will have the domain :ref:`ZZ`. However, if the ``modulus=p``
    option is given then the domain will be a finite field instead.

    >>> from sympy import Poly, Symbol
    >>> x = Symbol('x')
    >>> p = Poly(x**2 + 1)
    >>> p
    Poly(x**2 + 1, x, domain='ZZ')
    >>> p.domain
    ZZ
    >>> p2 = Poly(x**2 + 1, modulus=2)
    >>> p2
    Poly(x**2 + 1, x, modulus=2)
    >>> p2.domain
    GF(2)

    It is possible to factorise a polynomial over :ref:`GF(p)` using the
    modulus argument to :py:func:`~.factor` or by specifying the domain
    explicitly. The domain can also be given as a string.

    >>> from sympy import factor, GF
    >>> factor(x**2 + 1)
    x**2 + 1
    >>> factor(x**2 + 1, modulus=2)
    (x + 1)**2
    >>> factor(x**2 + 1, domain=GF(2))
    (x + 1)**2
    >>> factor(x**2 + 1, domain='GF(2)')
    (x + 1)**2

    It is also possible to use :ref:`GF(p)` with the :py:func:`~.cancel`
    and :py:func:`~.gcd` functions.

    >>> from sympy import cancel, gcd
    >>> cancel((x**2 + 1)/(x + 1))
    (x**2 + 1)/(x + 1)
    >>> cancel((x**2 + 1)/(x + 1), domain=GF(2))
    x + 1
    >>> gcd(x**2 + 1, x + 1)
    1
    >>> gcd(x**2 + 1, x + 1, domain=GF(2))
    x + 1

    When using the domain directly :ref:`GF(p)` can be used as a constructor
    to create instances which then support the operations ``+,-,*,**,/``

    >>> from sympy import GF
    >>> K = GF(5)
    >>> K
    GF(5)
    >>> x = K(3)
    >>> y = K(2)
    >>> x
    3 mod 5
    >>> y
    2 mod 5
    >>> x * y
    1 mod 5
    >>> x / y
    4 mod 5

    Notes
    =====

    It is also possible to create a :ref:`GF(p)` domain of **non-prime**
    order but the resulting ring is **not** a field: it is just the ring of
    the integers modulo ``n``.

    >>> K = GF(9)
    >>> z = K(3)
    >>> z
    3 mod 9
    >>> z**2
    0 mod 9

    It would be good to have a proper implementation of prime power fields
    (``GF(p**n)``) but these are not yet implemented in SymPY.

    .. _finite field: https://en.wikipedia.org/wiki/Finite_field
    ÚFFTFNc                 ó>  — ddl m} |}|dk  rt        d|z  «      ‚t        |||| «      \  }}}|| _        || _        || _        | j	                  d«      | _        | j	                  d«      | _        || _	        || _
        || _        t        | j                  «      | _        y )Nr   )ÚZZz*modulus must be a positive integer, got %sé   )Úsympy.polys.domainsr>   r1   r7   ÚdtypeÚ	_poly_ctxÚ	_is_flintÚzeroÚoner3   r   ÚsymÚtypeÚ_tp)r5   r   r4   r>   r3   r   r!   r6   s           r   Ú__init__zFiniteField.__init__Ó   s�   € Ý*Øˆà�!Š8ÜÐIÈCÑOÓPÐPä"6°s¸CÀÈDÓ"QÑˆˆX�xàˆŒ
Ø!ˆŒØ!ˆŒà—J‘J˜q“MˆŒ	Ø—:‘:˜a“=ˆŒØˆŒØˆŒØˆŒÜ˜Ÿ	™	“?ˆ�r"   c                 ó   — | j                   S r   )rH   ©r5   s    r   ÚtpzFiniteField.tpç   ó   € à�x‰xˆr"   c                 ób   — t        | dd «      }|€ddlm}  || j                  «      x| _        }|S )NÚ	_is_fieldr   )Úisprime)ÚgetattrÚsympy.ntheory.primetestrP   r   rO   )r5   Úis_fieldrP   s      r   Úis_FieldzFiniteField.is_Fieldë   s3   € ä˜4 ¨dÓ3ˆØÐÝ7Ù(/°·±Ó(9Ð9ˆDŒN˜XØˆr"   c                 ó    — d| j                   z  S )NzGF(%s)©r   rK   s    r   Ú__str__zFiniteField.__str__ó   s   € Ø˜$Ÿ(™(Ñ"Ð"r"   c                 ó„   — t        | j                  j                  | j                  | j                  | j
                  f«      S r   )ÚhashÚ	__class__Ú__name__rA   r   r3   rK   s    r   Ú__hash__zFiniteField.__hash__ö   s,   € Ü�T—^‘^×,Ñ,¨d¯j©j¸$¿(¹(ÀDÇHÁHÐMÓNÐNr"   c                 óŽ   — t        |t        «      xr4 | j                  |j                  k(  xr | j                  |j                  k(  S )z0Returns ``True`` if two domains are equivalent. )Ú
isinstancer   r   r3   )r5   Úothers     r   Ú__eq__zFiniteField.__eq__ù   s;   € ä˜%¤Ó-ò <Ø�H‰H˜Ÿ	™	Ñ!ò<Ø&*§h¡h°%·)±)Ñ&;ð	<r"   c                 ó   — | j                   S )z*Return the characteristic of this domain. rV   rK   s    r   ÚcharacteristiczFiniteField.characteristicþ   rM   r"   c                 ó   — | S )z*Returns a field associated with ``self``. r   rK   s    r   Ú	get_fieldzFiniteField.get_field  s   € àˆr"   c                 ó6   — t        | j                  |«      «      S )z!Convert ``a`` to a SymPy object. )r   Úto_int©r5   Úas     r   Úto_sympyzFiniteField.to_sympy  s   € ä˜DŸK™K¨›NÓ+Ð+r"   c                 ó  — |j                   r3| j                  | j                  j                  t        |«      «      «      S t	        |«      r3| j                  | j                  j                  t        |«      «      «      S t        d|z  «      ‚)z0Convert SymPy's Integer to SymPy's ``Integer``. zexpected an integer, got %s)Ú
is_IntegerrA   r3   Úintr   r   rg   s     r   Ú
from_sympyzFiniteField.from_sympy
  sa   € à�<Š<Ø—:‘:˜dŸh™hŸn™n¬S°«VÓ4Ó5Ð5Ü˜Œ]Ø—:‘:˜dŸh™hŸn™n¬S°«VÓ4Ó5Ð5ä Ð!>ÀÑ!BÓCÐCr"   c                 óv   — t        |«      }| j                  r!|| j                  dz  kD  r|| j                  z  }|S )z,Convert ``val`` to a Python ``int`` object. é   )rl   rF   r   )r5   rh   Úavals      r   rf   zFiniteField.to_int  s4   € ä�1‹vˆØ�8Š8˜˜tŸx™x¨1™}Ò,Ø�D—H‘HÑˆDØˆr"   c                 ó   — t        |«      S )z#Returns True if ``a`` is positive. )Úboolrg   s     r   Úis_positivezFiniteField.is_positive  s   € ä�A‹wˆr"   c                  ó   — y)z'Returns True if ``a`` is non-negative. Tr   rg   s     r   Úis_nonnegativezFiniteField.is_nonnegative  s   € àr"   c                  ó   — y)z#Returns True if ``a`` is negative. Fr   rg   s     r   Úis_negativezFiniteField.is_negative"  s   € àr"   c                 ó   — | S )z'Returns True if ``a`` is non-positive. r   rg   s     r   Úis_nonpositivezFiniteField.is_nonpositive&  s	   € àˆuˆr"   c                 ó~   — | j                  | j                  j                  t        |«      |j                  «      «      S ©z.Convert ``ModularInteger(int)`` to ``dtype``. )rA   r3   Úfrom_ZZrl   ©ÚK1rh   ÚK0s      r   Úfrom_FFzFiniteField.from_FF*  s(   € à�x‰x˜Ÿ™Ÿ™¤s¨1£v¨r¯v©vÓ6Ó7Ð7r"   c                 ó~   — | j                  | j                  j                  t        |«      |j                  «      «      S r{   )rA   r3   Úfrom_ZZ_pythonrl   r}   s      r   Úfrom_FF_pythonzFiniteField.from_FF_python.  s*   € à�x‰x˜Ÿ™×-Ñ-¬c°!«f°b·f±fÓ=Ó>Ð>r"   c                 óX   — | j                  | j                  j                  ||«      «      S ©z'Convert Python's ``int`` to ``dtype``. ©rA   r3   r‚   r}   s      r   r|   zFiniteField.from_ZZ2  ó"   € à�x‰x˜Ÿ™×-Ñ-¨a°Ó4Ó5Ð5r"   c                 óX   — | j                  | j                  j                  ||«      «      S r…   r†   r}   s      r   r‚   zFiniteField.from_ZZ_python6  r‡   r"   c                 óX   — |j                   dk(  r| j                  |j                  «      S y©z,Convert Python's ``Fraction`` to ``dtype``. r?   N©Údenominatorr‚   Ú	numeratorr}   s      r   Úfrom_QQzFiniteField.from_QQ:  ó(   € à�=‰=˜AÒØ×$Ñ$ Q§[¡[Ó1Ð1ð r"   c                 óX   — |j                   dk(  r| j                  |j                  «      S yrŠ   r‹   r}   s      r   Úfrom_QQ_pythonzFiniteField.from_QQ_python?  r�   r"   c                 ó€   — | j                  | j                  j                  |j                  |j                  «      «      S )z.Convert ``ModularInteger(mpz)`` to ``dtype``. )rA   r3   Úfrom_ZZ_gmpyÚvalr}   s      r   Úfrom_FF_gmpyzFiniteField.from_FF_gmpyD  s*   € à�x‰x˜Ÿ™×+Ñ+¨A¯E©E°2·6±6Ó:Ó;Ð;r"   c                 óX   — | j                  | j                  j                  ||«      «      S )z%Convert GMPY's ``mpz`` to ``dtype``. )rA   r3   r“   r}   s      r   r“   zFiniteField.from_ZZ_gmpyH  s"   € à�x‰x˜Ÿ™×+Ñ+¨A¨rÓ2Ó3Ð3r"   c                 óX   — |j                   dk(  r| j                  |j                  «      S y)z%Convert GMPY's ``mpq`` to ``dtype``. r?   N)rŒ   r“   r�   r}   s      r   Úfrom_QQ_gmpyzFiniteField.from_QQ_gmpyL  s&   € à�=‰=˜AÒØ—?‘? 1§;¡;Ó/Ð/ð r"   c                 óŠ   — |j                  |«      \  }}|dk(  r*| j                  | j                  j                  |«      «      S y)z'Convert mpmath's ``mpf`` to ``dtype``. r?   N)Úto_rationalrA   r3   )r~   rh   r   ÚpÚqs        r   Úfrom_RealFieldzFiniteField.from_RealFieldQ  s;   € à�~‰~˜aÓ ‰ˆˆ1à�Š6Ø—8‘8˜BŸF™FŸL™L¨›OÓ,Ð,ð r"   c                 ó°   — | j                   | j                  | fD �cg c]  }t        |«      ‘Œ }}t        || j                  | j
                  «       S c c}w )z7Returns True if ``a`` is a quadratic residue modulo p. )rE   rD   rl   r
   r   r3   )r5   rh   r   Úpolys       r   Ú	is_squarezFiniteField.is_squareX  sK   € ð "&§¡¨4¯9©9°q°bÐ 9Ö:˜1”�A•Ð:ˆÐ:Ü# D¨$¯(©(°D·H±HÓ=Ð=Ð=ùò ;s   �Ac                 óZ  — | j                   dk(  s|dk(  r|S | j                  | j                  | fD �cg c]  }t        |«      ‘Œ }}t	        || j                   | j
                  «      D ]<  }t        |«      dk(  sŒ|d   | j                   dz  k  sŒ(| j                  |d   «      c S  yc c}w )z·Square root modulo p of ``a`` if it is a quadratic residue.

        Explanation
        ===========
        Always returns the square root that is no larger than ``p // 2``.
        ro   r   r?   N)r   rE   rD   rl   r	   r3   ÚlenrA   )r5   rh   r   rŸ   Úfactors        r   ÚexsqrtzFiniteField.exsqrt^  sž   € ð �8‰8�qŠ=˜A šFØˆHà!%§¡¨4¯9©9°q°bÐ 9Ö:˜1”�A•Ð:ˆÐ:Ü# D¨$¯(©(°D·H±HÓ=ò 	-ˆFÜ�6‹{˜aÓ F¨1¡I°·±¸Q±Ó$>Ø—z‘z &¨¡)Ó,Ò,ð	-ð ùò	 ;s   ³B()Tr   ))r[   Ú
__module__Ú__qualname__Ú__doc__ÚrepÚaliasÚis_FiniteFieldÚis_FFÚis_NumericalÚhas_assoc_RingÚhas_assoc_Fieldr3   r   rI   ÚpropertyrL   rT   rW   r\   r`   rb   rd   ri   rm   rf   rs   ru   rw   ry   r€   rƒ   r|   r‚   rŽ   r‘   r•   r“   r˜   r�   r    r¤   r   r"   r   r   r   l   sß   „ ñVðp €CØ€Eà!Ð!€N�UØ€Là€NØ€Oà
€CØ
€Có#ð( ñó ðð ñó ðò#òOò<ò
òò,òDòòòòòó8ó?ó6ó6ó2ó
2ó
<ó4ó0ò
-ò>ór"   )%r§   r#   Úsympy.external.gmpyr   Úsympy.utilities.decoratorr   Úsympy.core.numbersr   Úsympy.polys.domains.fieldr   Ú"sympy.polys.domains.modularintegerr   Ú sympy.polys.domains.simpledomainr   Úsympy.polys.galoistoolsr	   r
   Úsympy.polys.polyerrorsr   Úsympy.utilitiesr   Úsympy.polys.domains.groundtypesr   Ú__doctest_skip__r   Ú__version__ÚsplitÚ_majorÚ_minorÚ_rl   r%   r.   r7   r   r<   ÚGFr   r"   r   ú<module>rÁ      sÔ   ðÙ 4ã å ,Ý 8å )Ý +å DÝ 9ß CÝ 1Ý "Ý 8ð �7ÒØ%�Ðð �7ÒÛð ×*Ñ*×0Ñ0°Ó5Ð€FˆF�QÙˆF‹‘S˜“[Ð! FÒ*Ø‰à€Eòò2ò&#ð< Ù˜X vÐ.Ô/ô�%˜ó ó 0ó ððD Ð €�Rr"   