§
    OŠtjž)  ã                   óœ  — 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    r>ddlZej                             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$dS )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‰ ¦  «         n# t
          $ r Y dS w xY wˆˆ ˆfd„}ˆ ˆfd„}||fS )Nr   )NNc                 óh   •— 	  ‰| ‰¦  «        S # t           $ r  ‰ ‰| ¦  «        ‰¦  «        cY S w xY w©N©Ú	TypeError)ÚxÚindexÚmodÚnmods    €€€ú]/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/polys/domains/finitefield.pyÚctxz&_modular_int_factory_nmod.<locals>.ctx/   sS   ø€ ð	'Ø�4˜˜3‘<”<ÐøÝð 	'ð 	'ð 	'Ø�4˜˜˜a™œ #Ñ&Ô&Ð&Ð&Ð&ð	'øøøs   ƒ �1°1c                 ó   •—  ‰| ‰¦  «        S r   © )Úcsr   Ú	nmod_polys    €€r   Úpoly_ctxz+_modular_int_factory_nmod.<locals>.poly_ctx5   s   ø€ Øˆy˜˜SÑ!Ô!Ð!ó    )Úoperatorr   r   r   r    ÚOverflowError)r   r   r!   r   r   r    s   `  @@@r   Ú_modular_int_factory_nmodr%   "   s­   øøøø€ åŒN€EØ
ˆ%�‰*Œ*€CÝŒ:€DÝ”€IðØˆˆQ�‰ŒˆˆøÝð ð ð Øˆzˆzðøøøð'ð 'ð 'ð 'ð 'ð 'ð 'ð"ð "ð "ð "ð "ð "ð �ˆ=Ðs   µA Á
AÁAc                 óª   ‡‡‡‡— t           j        Št          j        | ¦  «        Št          j        | ¦  «        Št          j        Šˆˆfd„}ˆˆfd„}||fS )Nc                 ód   •— 	  ‰| ¦  «        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   sL   ø€ ð	"Ø�4˜‘7”7ˆNøÝð 	"ð 	"ð 	"à�4˜˜˜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.   ;   s}   øøøø€ ÝŒN€EÝÔ˜cÑ"Ô"€DÝÔ'¨Ñ,Ô,€IÝÔ'€Mð"ð "ð "ð "ð "ð "ð,ð ,ð ,ð ,ð ,ð ,ð �ˆ=Ðr"   c                 ó8  — 	 |                      | ¦  «        } n # t          $ r t          d| z  ¦  «        ‚w xY wd\  }}}t          �<|                      ¦   «         r(d}t          | ¦  «        \  }}|€t          | ¦  «        \  }}|€t          | |||¦  «        }d }|||fS )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˜#ÑÔˆˆøÝð Eð Eð EÝÐ=ÀÑCÑDÔDÐDðEøøøð 0Ñ€Cˆ�8õ Ð˜SŸ\š\™^œ^Ðàˆõ 2°#Ñ6Ô6‰ˆˆXàˆ;å9¸#Ñ>Ô>‰MˆC�à
€{õ $ C¨¨i¸Ñ>Ô>ˆØˆà�˜(Ð"Ð"s   ‚ ˜5Ú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(dS )#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          |||| ¦  «        \  }}}|| _        || _        || _        |                      d¦  «        | _        |                      d¦  «        | _        || _	        || _
        || _        t          | j        ¦  «        | _        d S )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ˆ8ÝÐIÈCÑOÑPÔPÐPå"6°s¸CÀÈDÑ"QÔ"QÑˆˆX�xàˆŒ
Ø!ˆŒØ!ˆŒà—J’J˜q‘M”MˆŒ	Ø—:’:˜a‘=”=ˆŒØˆŒØˆŒØˆŒÝ˜œ	‘?”?ˆŒˆˆr"   c                 ó   — | j         S r   )rH   ©r5   s    r   ÚtpzFiniteField.tpç   ó	   € àŒxˆr"   c                 óf   — 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ë   sH   € å˜4 ¨dÑ3Ô3ˆØÐØ7Ð7Ð7Ð7Ð7Ð7Ø(/¨°´Ñ(9Ô(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                 óZ   — 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¸$¼(ÀDÄHÐMÑNÔNÐNr"   c                 ól   — t          |t          ¦  «        o| j        |j        k    o| j        |j        k    S )z0Returns ``True`` if two domains are equivalent. )Ú
isinstancer   r   r3   )r5   Úothers     r   Ú__eq__zFiniteField.__eq__ù   s6   € å˜%¥Ñ-Ô-ð <ØŒ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                 óF   — t          |                      |¦  «        ¦  «        S )z!Convert ``a`` to a SymPy object. )r   Úto_int©r5   Úas     r   Úto_sympyzFiniteField.to_sympy  s   € å˜DŸKšK¨™NœNÑ+Ô+Ð+r"   c                 ó:  — |j         r:|                      | j                             t          |¦  «        ¦  «        ¦  «        S t	          |¦  «        r:|                      | 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
  s|   € àŒ<ð 	DØ—:’:˜dœhŸnšn­S°©V¬VÑ4Ô4Ñ5Ô5Ð5Ý˜‰]Œ]ð 	DØ—:’:˜dœhŸnšn­S°©V¬VÑ4Ô4Ñ5Ô5Ð5å Ð!>ÀÑ!BÑCÔCÐCr"   c                 ób   — t          |¦  «        }| j        r|| j        dz  k    r
|| j        z  }|S )z,Convert ``val`` to a Python ``int`` object. é   )rl   rF   r   )r5   rh   Úavals      r   rf   zFiniteField.to_int  s8   € å�1‰vŒvˆØŒ8ð 	˜˜tœx¨1™}Ò,Ð,Ø�D”HÑˆDØˆr"   c                 ó    — t          |¦  «        S )z#Returns True if ``a`` is positive. )Úboolrg   s     r   Úis_positivezFiniteField.is_positive  s   € å�A‰wŒwˆr"   c                 ó   — dS )z'Returns True if ``a`` is non-negative. Tr   rg   s     r   Úis_nonnegativezFiniteField.is_nonnegative  s   € àˆtr"   c                 ó   — dS )z#Returns True if ``a`` is negative. Fr   rg   s     r   Úis_negativezFiniteField.is_negative"  s   € àˆur"   c                 ó   — | S )z'Returns True if ``a`` is non-positive. r   rg   s     r   Úis_nonpositivezFiniteField.is_nonpositive&  s	   € àˆuˆr"   c                 ó‚   — |                       | 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¤v¨r¬vÑ6Ô6Ñ7Ô7Ð7r"   c                 ó‚   — |                       | 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¬f°b´fÑ=Ô=Ñ>Ô>Ð>r"   c                 ó^   — |                       | j                             ||¦  «        ¦  «        S ©z'Convert Python's ``int`` to ``dtype``. ©rA   r3   r‚   r}   s      r   r|   zFiniteField.from_ZZ2  ó&   € à�xŠx˜œ×-Ò-¨a°Ñ4Ô4Ñ5Ô5Ð5r"   c                 ó^   — |                       | j                             ||¦  «        ¦  «        S r…   r†   r}   s      r   r‚   zFiniteField.from_ZZ_python6  r‡   r"   c                 óP   — |j         dk    r|                      |j        ¦  «        S dS ©z,Convert Python's ``Fraction`` to ``dtype``. r?   N©Údenominatorr‚   Ú	numeratorr}   s      r   Úfrom_QQzFiniteField.from_QQ:  ó-   € àŒ=˜AÒÐØ×$Ò$ Q¤[Ñ1Ô1Ð1ð Ðr"   c                 óP   — |j         dk    r|                      |j        ¦  «        S dS rŠ   r‹   r}   s      r   Úfrom_QQ_pythonzFiniteField.from_QQ_python?  r�   r"   c                 ór   — |                       | 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°2´6Ñ:Ô:Ñ;Ô;Ð;r"   c                 ó^   — |                       | 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Ô2Ñ3Ô3Ð3r"   c                 óP   — |j         dk    r|                      |j        ¦  «        S dS )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                 óœ   — |                      |¦  «        \  }}|dk    r-|                      | j                             |¦  «        ¦  «        S dS )z'Convert mpmath's ``mpf`` to ``dtype``. r?   N)Úto_rationalrA   r3   )r~   rh   r   ÚpÚqs        r   Úfrom_RealFieldzFiniteField.from_RealFieldQ  sE   € à�~Š~˜aÑ Ô ‰ˆˆ1à�Š6ˆ6Ø—8’8˜BœFŸLšL¨™OœOÑ,Ô,Ð,ð ˆ6r"   c                 ón   — d„ | j         | j        | fD ¦   «         }t          || j        | j        ¦  «         S )z7Returns True if ``a`` is a quadratic residue modulo p. c                 ó,   — g | ]}t          |¦  «        ‘ŒS r   ©rl   ©Ú.0r   s     r   ú
<listcomp>z)FiniteField.is_square.<locals>.<listcomp>[  ó   € Ð:Ð:Ð:˜1•�A‘”Ð:Ð:Ð:r"   )rE   rD   r
   r   r3   )r5   rh   Úpolys      r   Ú	is_squarezFiniteField.is_squareX  s=   € ð ;Ð: ¤¨4¬9°q°bÐ 9Ð:Ñ:Ô:ˆÝ# D¨$¬(°D´HÑ=Ô=Ð=Ð=r"   c                 ó$  — | j         dk    s|dk    r|S d„ | j        | j        | fD ¦   «         }t          || j         | j        ¦  «        D ]F}t          |¦  «        dk    r1|d         | j         dz  k    r|                      |d         ¦  «        c S ŒGdS )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   c                 ó,   — g | ]}t          |¦  «        ‘ŒS r   r    r¡   s     r   r£   z&FiniteField.exsqrt.<locals>.<listcomp>i  r¤   r"   r?   N)r   rE   rD   r	   r3   ÚlenrA   )r5   rh   r¥   Úfactors       r   ÚexsqrtzFiniteField.exsqrt^  s¢   € ð Œ8�qŠ=ˆ=˜A šF˜FØˆHà:Ð: ¤¨4¬9°q°bÐ 9Ð:Ñ:Ô:ˆÝ# D¨$¬(°D´HÑ=Ô=ð 	-ð 	-ˆFÝ�6‰{Œ{˜aÒÐ F¨1¤I°´¸Q±Ò$>Ð$>Ø—z’z &¨¤)Ñ,Ô,Ð,Ð,Ð,øØˆtr"   )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   s5  € € € € € ðVð Vðp €CØ€Eà!Ð!€N�UØ€Là€NØ€Oà
€CØ
€Cð#ð #ð #ð #ð( ðð ñ „Xðð ðð ñ „Xðð#ð #ð #ðOð Oð Oð<ð <ð <ð
ð ð ðð ð ð,ð ,ð ,ðDð Dð Dðð ð ðð ð ðð ð ðð ð ðð ð ð8ð 8ð 8ð 8ð?ð ?ð ?ð ?ð6ð 6ð 6ð 6ð6ð 6ð 6ð 6ð2ð 2ð 2ð 2ð
2ð 2ð 2ð 2ð
<ð <ð <ð <ð4ð 4ð 4ð 4ð0ð 0ð 0ð 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Ð 4à €€€à ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8à )Ð )Ð )Ð )Ð )Ð )Ø +Ð +Ð +Ð +Ð +Ð +à DÐ DÐ DÐ DÐ DÐ DØ 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø "Ð "Ð "Ð "Ð "Ð "Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8ð �7ÒÐØ%�Ðð �7ÒÐØ€L€L€Lð Ô*×0Ò0°Ñ5Ô5Ð€FˆF�QØˆˆF‰Œ�S�S˜‘[”[Ð! FÒ*Ð*Øˆøà€Eðð ð ð2ð ð ð&#ð #ð #ð< ØÐ˜X vÐ.Ð/Ñ/Ô/ðð ð ð ð �%˜ñ ô ñ 0Ô/ñ „ððD Ð €€R€R€Rr"   