o
    §ƒðjž)  ã                   @   s  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rKdgZedkriddlZej d¡^ZZZeeƒeeƒfdk rhdZndZdd„ Zdd„ Z dd„ Z!eeddgd�G dd„ de	eƒƒƒZ"e" 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                    sd   t j‰ ˆ ˆƒ‰tj‰tj‰zˆdˆƒ W n
 ty   Y dS w ‡ ‡‡fdd„}‡‡fdd„}||fS )Nr   )NNc                    s.   zˆ| ˆƒW S  t y   ˆˆ | ƒˆƒ Y S w ©N©Ú	TypeError©Úx)ÚindexÚmodÚnmod© ú¡/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/polys/domains/finitefield.pyÚctx/   s
   ÿz&_modular_int_factory_nmod.<locals>.ctxc                    ó
   ˆ| ˆ ƒS r   r   ©Úcs)r   Ú	nmod_polyr   r   Úpoly_ctx5   ó   
z+_modular_int_factory_nmod.<locals>.poly_ctx)Úoperatorr   r   r   r   ÚOverflowError©r   r   r    r   )r   r   r   r   r   Ú_modular_int_factory_nmod"   s   ÿr%   c                    sD   t j‰t | ¡‰ t | ¡‰tj‰‡ ‡fdd„}‡‡fdd„}||fS )Nc                    s*   zˆ | ƒW S  t y   ˆ ˆ| ƒƒ Y S w r   r   r   )Úfctxr   r   r   r   A   s
   
þz*_modular_int_factory_fmpz_mod.<locals>.ctxc                    r   r   r   r   )Ú	fctx_polyÚfmpz_mod_polyr   r   r    H   r!   z/_modular_int_factory_fmpz_mod.<locals>.poly_ctx)r"   r   r   Úfmpz_mod_ctxÚfmpz_mod_poly_ctxr(   r$   r   )r&   r'   r(   r   r   Ú_modular_int_factory_fmpz_mod;   s   

r+   c                 C   sŒ   z|  | ¡} W n ty   td|  ƒ‚w d\}}}td ur4|  ¡ r4d}t| ƒ\}}|d u r4t| ƒ\}}|d u rAt| |||ƒ}d }|||fS )Nz"modulus must be an integer, got %s)NNFT)Úconvertr
   Ú
ValueErrorr   Úis_primer%   r+   r   )r   ÚdomÚ	symmetricÚselfr   r    Úis_flintr   r   r   Ú_modular_int_factoryN   s   ÿ

r3   ÚpythonÚgmpy)Úmodulesc                   @   s(  e Zd ZdZdZdZd ZZdZdZ	dZ
dZdZd<dd„Zedd	„ ƒZe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$d%„Zd=d&d'„Zd=d(d)„Zd=d*d+„Z d=d,d-„Z!d=d.d/„Z"d=d0d1„Z#d=d2d3„Z$d=d4d5„Z%d6d7„ Z&d8d9„ Z'd: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                 C   s„   ddl m} |}|dkrtd| ƒ‚t|||| ƒ\}}}|| _|| _|| _|  d¡| _|  d¡| _|| _	|| _
|| _t| jƒ| _d S )Nr   )ÚZZz*modulus must be a positive integer, got %sé   )Úsympy.polys.domainsr8   r-   r3   ÚdtypeÚ	_poly_ctxÚ	_is_flintÚzeroÚoner/   r   ÚsymÚtypeÚ_tp)r1   r   r0   r8   r/   r   r    r2   r   r   r   Ú__init__Ó   s   zFiniteField.__init__c                 C   ó   | j S r   )rB   ©r1   r   r   r   Útpç   ó   zFiniteField.tpc                 C   s4   t | dd ƒ}|d u rddlm} || jƒ | _}|S )NÚ	_is_fieldr   )Úisprime)ÚgetattrÚsympy.ntheory.primetestrI   r   rH   )r1   Úis_fieldrI   r   r   r   Úis_Fieldë   s
   zFiniteField.is_Fieldc                 C   s
   d| j  S )NzGF(%s)©r   rE   r   r   r   Ú__str__ó   r!   zFiniteField.__str__c                 C   s   t | jj| j| j| jfƒS r   )ÚhashÚ	__class__Ú__name__r;   r   r/   rE   r   r   r   Ú__hash__ö   s   zFiniteField.__hash__c                 C   s"   t |tƒo| j|jko| j|jkS )z0Returns ``True`` if two domains are equivalent. )Ú
isinstancer   r   r/   )r1   Úotherr   r   r   Ú__eq__ù   s
   

ÿ
ÿzFiniteField.__eq__c                 C   rD   )z*Return the characteristic of this domain. rN   rE   r   r   r   Úcharacteristicþ   rG   zFiniteField.characteristicc                 C   s   | S )z*Returns a field associated with ``self``. r   rE   r   r   r   Ú	get_field  ó   zFiniteField.get_fieldc                 C   s   t |  |¡ƒS )z!Convert ``a`` to a SymPy object. )r   Úto_int©r1   Úar   r   r   Úto_sympy  s   zFiniteField.to_sympyc                 C   sF   |j r|  | j t|ƒ¡¡S t|ƒr|  | j t|ƒ¡¡S td| ƒ‚)z0Convert SymPy's Integer to SymPy's ``Integer``. zexpected an integer, got %s)Ú
is_Integerr;   r/   Úintr   r
   r[   r   r   r   Ú
from_sympy
  s
   zFiniteField.from_sympyc                 C   s*   t |ƒ}| jr|| jd kr|| j8 }|S )z,Convert ``val`` to a Python ``int`` object. é   )r_   r@   r   )r1   r\   Úavalr   r   r   rZ     s   
zFiniteField.to_intc                 C   s   t |ƒS )z#Returns True if ``a`` is positive. )Úboolr[   r   r   r   Úis_positive  s   zFiniteField.is_positivec                 C   ó   dS )z'Returns True if ``a`` is non-negative. Tr   r[   r   r   r   Úis_nonnegative  rY   zFiniteField.is_nonnegativec                 C   re   )z#Returns True if ``a`` is negative. Fr   r[   r   r   r   Úis_negative"  rY   zFiniteField.is_negativec                 C   s   | S )z'Returns True if ``a`` is non-positive. r   r[   r   r   r   Úis_nonpositive&  rG   zFiniteField.is_nonpositivec                 C   ó   |   | j t|ƒ|j¡¡S ©z.Convert ``ModularInteger(int)`` to ``dtype``. )r;   r/   Úfrom_ZZr_   ©ÚK1r\   ÚK0r   r   r   Úfrom_FF*  ó   zFiniteField.from_FFc                 C   ri   rj   )r;   r/   Úfrom_ZZ_pythonr_   rl   r   r   r   Úfrom_FF_python.  rp   zFiniteField.from_FF_pythonc                 C   ó   |   | j ||¡¡S ©z'Convert Python's ``int`` to ``dtype``. ©r;   r/   rq   rl   r   r   r   rk   2  ó   zFiniteField.from_ZZc                 C   rs   rt   ru   rl   r   r   r   rq   6  rv   zFiniteField.from_ZZ_pythonc                 C   ó   |j dkr|  |j¡S dS ©z,Convert Python's ``Fraction`` to ``dtype``. r9   N©Údenominatorrq   Ú	numeratorrl   r   r   r   Úfrom_QQ:  ó   
ÿzFiniteField.from_QQc                 C   rw   rx   ry   rl   r   r   r   Úfrom_QQ_python?  r}   zFiniteField.from_QQ_pythonc                 C   s   |   | j |j|j¡¡S )z.Convert ``ModularInteger(mpz)`` to ``dtype``. )r;   r/   Úfrom_ZZ_gmpyÚvalrl   r   r   r   Úfrom_FF_gmpyD  s   zFiniteField.from_FF_gmpyc                 C   rs   )z%Convert GMPY's ``mpz`` to ``dtype``. )r;   r/   r   rl   r   r   r   r   H  rv   zFiniteField.from_ZZ_gmpyc                 C   rw   )z%Convert GMPY's ``mpq`` to ``dtype``. r9   N)rz   r   r{   rl   r   r   r   Úfrom_QQ_gmpyL  r}   zFiniteField.from_QQ_gmpyc                 C   s,   |  |¡\}}|dkr|  | j |¡¡S dS )z'Convert mpmath's ``mpf`` to ``dtype``. r9   N)Úto_rationalr;   r/   )rm   r\   rn   ÚpÚqr   r   r   Úfrom_RealFieldQ  s   ÿzFiniteField.from_RealFieldc                 C   s,   dd„ | j | j| fD ƒ}t|| j| jƒ S )z7Returns True if ``a`` is a quadratic residue modulo p. c                 S   ó   g | ]}t |ƒ‘qS r   ©r_   ©Ú.0r   r   r   r   Ú
<listcomp>[  ó    z)FiniteField.is_square.<locals>.<listcomp>)r?   r>   r	   r   r/   )r1   r\   Úpolyr   r   r   Ú	is_squareX  s   zFiniteField.is_squarec                 C   sz   | j dks	|dkr|S dd„ | j| j| fD ƒ}t|| j | jƒD ]}t|ƒdkr:|d | j d kr:|  |d ¡  S q d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``.
        ra   r   c                 S   r‡   r   rˆ   r‰   r   r   r   r‹   i  rŒ   z&FiniteField.exsqrt.<locals>.<listcomp>r9   N)r   r?   r>   r   r/   Úlenr;   )r1   r\   r�   Úfactorr   r   r   Úexsqrt^  s   €zFiniteField.exsqrt)Tr   ))rR   Ú
__module__Ú__qualname__Ú__doc__ÚrepÚaliasÚis_FiniteFieldÚis_FFÚis_NumericalÚhas_assoc_RingÚhas_assoc_Fieldr/   r   rC   ÚpropertyrF   rM   rO   rS   rV   rW   rX   r]   r`   rZ   rd   rf   rg   rh   ro   rr   rk   rq   r|   r~   r�   r   r‚   r†   rŽ   r‘   r   r   r   r   r   l   sN    X


	








)%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Ú_r_   r%   r+   r3   r   r7   ÚGFr   r   r   r   Ú<module>   s<    €  