+
    i)                        R t ^ RIt^ RIHt ^ RIHt ^ RIHt ^ RIH	t	 ^ RI
Ht ^ RIHt ^ RIHtHt ^ R	IHt ^ R
IHt ^ RIHt ]R8X  d   R.t]R8X  d;   ^ RIt]P2                  P5                  R4      vttt]! ]4      ]! ]4      3R8  d   RtMRtR tR t R t!]]! RR.R7       ! R R]	]4      4       4       t"]";t#t$R# )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.c                    a aaa \         P                  oS! S 4      o \        P                  o\        P                  o S! ^ S 4       TT T3R lpT T3R lpY3#   \
         d    Ru # i ; i)    c                 X   <  S! V S4      #   \          d    S! S! T 4      S4      u # i ; iN	TypeError)xindexmodnmods   &/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/polys/domains/finitefield.pyctx&_modular_int_factory_nmod.<locals>.ctx/   s4    	'3< 	'a#&&	's    ))c                    < S! V S4      # r    )csr   	nmod_polys   &r   poly_ctx+_modular_int_factory_nmod.<locals>.poly_ctx5   s    S!!    )NN)operatorr   r   r   r    OverflowError)r   r   r!   r   r   r    s   f  @@@r   _modular_int_factory_nmodr&   "   s`    NNE
*C::DIQ'" =  s   	A A)(A)c                    aaaa \         P                  o\        P                  ! V 4      o\        P                  ! V 4      o\        P
                  oVV3R  lpVV3R lpW3# )c                 T   <  S! V 4      #   \          d    S! S! T 4      4      u # i ; ir   r   )r   fctxr   s   &r   r   *_modular_int_factory_fmpz_mod.<locals>.ctxA   s.    	"7N 	"a>!	"s    ''c                    < S! V S4      # r   r   )r   	fctx_polyfmpz_mod_polys   &r   r!   /_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_modr1   ;   sK    NNEc"D'',I''M", =r#   c                     VP                  V 4      p RRRrep\        e7   T P	                  4       '       d!   Rp\        T 4      w  rETf   \        T 4      w  rETf   \        YY#4      pRpYET3#   \         d    \        R T ,          4      hi ; i)z"modulus must be an integer, got %sNFT)convertr
   
ValueErrorr   is_primer&   r1   r   )r   dom	symmetricselfr   r!   is_flints   &&&&   r   _modular_int_factoryr:   N   s    Ekk# #D%8C S\\^^ 2#6;9#>MC
{ $Ci>(""/  E=CDDEs   A, ,B
pythongmpy)modulesc                   &  a  ] tR t^lt o RtRtRtR;ttRt	Rt
RtRtRtR"R lt]R 4       t]R 4       tR	 tR
 tR tR tR tR tR tR tR tR tR tR tR#R ltR#R ltR#R lt R#R lt!R#R lt"R#R lt#R#R lt$R#R lt%R#R lt&R t'R t(R  t)R!t*V t+R# )$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                :   ^ RI Hp TpV^ 8:  d   \        RV,          4      h\        WW 4      w  rVpWPn        W`n        Wpn        V P	                  ^ 4      V n        V P	                  ^4      V n        W@n	        Wn
        W n        \        V P                  4      V n        R# )r   )ZZz*modulus must be a positive integer, got %sN)sympy.polys.domainsrA   r4   r:   dtype	_poly_ctx	_is_flintzerooner6   r   symtype_tp)r8   r   r7   rA   r6   r   r!   r9   s   &&&     r   __init__FiniteField.__init__   s~    *!8ICOPP"6s"Qx
!!JJqM	::a=		?r#   c                    V P                   # r   )rJ   r8   s   &r   tpFiniteField.tp       xxr#   c                f    \        V R R4      pVf    ^ RIHp V! V P                  4      ;V n        pV# )	_is_fieldN)isprime)getattrsympy.ntheory.primetestrT   r   rS   )r8   is_fieldrT   s   &  r   is_FieldFiniteField.is_Field   s3    4d37(/(99DNXr#   c                (    R V P                   ,          # )zGF(%s)r   rN   s   &r   __str__FiniteField.__str__   s    $((""r#   c                    \        V P                  P                  V P                  V P                  V P
                  34      # r   )hash	__class____name__rC   r   r6   rN   s   &r   __hash__FiniteField.__hash__   s,    T^^,,djj$((DHHMNNr#   c                    \        V\        4      ;'       d;    V P                  VP                  8H  ;'       d    V P                  VP                  8H  # )z0Returns ``True`` if two domains are equivalent. )
isinstancer   r   r6   )r8   others   &&r   __eq__FiniteField.__eq__   sE    %- < <HH		!< <&*hh%))&;	<r#   c                    V P                   # )z*Return the characteristic of this domain. r[   rN   s   &r   characteristicFiniteField.characteristic   rQ   r#   c                    V # )z*Returns a field associated with ``self``. r   rN   s   &r   	get_fieldFiniteField.get_field  s    r#   c                6    \        V P                  V4      4      # )z!Convert ``a`` to a SymPy object. )r   to_intr8   as   &&r   to_sympyFiniteField.to_sympy  s    DKKN++r#   c                8   VP                   '       d4   V P                  V P                  P                  \        V4      4      4      # \	        V4      '       d4   V P                  V P                  P                  \        V4      4      4      # \        RV,          4      h)z0Convert SymPy's Integer to SymPy's ``Integer``. zexpected an integer, got %s)
is_IntegerrC   r6   intr   r
   rq   s   &&r   
from_sympyFiniteField.from_sympy
  se    <<<::dhhnnSV455]]::dhhnnSV455 !>!BCCr#   c                    \        V4      pV P                  '       d*   W P                  ^,          8  d   W P                  ,          pV# )z,Convert ``val`` to a Python ``int`` object. )rw   rH   r   )r8   rr   avals   && r   rp   FiniteField.to_int  s2    1v888xx1},HHDr#   c                    \        V4      # )z#Returns True if ``a`` is positive. )boolrq   s   &&r   is_positiveFiniteField.is_positive  s    Awr#   c                    R# )z'Returns True if ``a`` is non-negative. Tr   rq   s   &&r   is_nonnegativeFiniteField.is_nonnegative  s    r#   c                    R# )z#Returns True if ``a`` is negative. Fr   rq   s   &&r   is_negativeFiniteField.is_negative"  s    r#   c                    V'       * # )z'Returns True if ``a`` is non-positive. r   rq   s   &&r   is_nonpositiveFiniteField.is_nonpositive&  s	    ur#   c                ~    V P                  V P                  P                  \        V4      VP                  4      4      # z.Convert ``ModularInteger(int)`` to ``dtype``. )rC   r6   from_ZZrw   K1rr   K0s   &&&r   from_FFFiniteField.from_FF*  s(    xxs1vrvv677r#   c                ~    V P                  V P                  P                  \        V4      VP                  4      4      # r   )rC   r6   from_ZZ_pythonrw   r   s   &&&r   from_FF_pythonFiniteField.from_FF_python.  s*    xx--c!fbff=>>r#   c                V    V P                  V P                  P                  W4      4      # z'Convert Python's ``int`` to ``dtype``. rC   r6   r   r   s   &&&r   r   FiniteField.from_ZZ2       xx--a455r#   c                V    V P                  V P                  P                  W4      4      # r   r   r   s   &&&r   r   FiniteField.from_ZZ_python6  r   r#   c                ^    VP                   ^8X  d   V P                  VP                  4      # R# z,Convert Python's ``Fraction`` to ``dtype``. Ndenominatorr   	numeratorr   s   &&&r   from_QQFiniteField.from_QQ:  (    ==A$$Q[[11 r#   c                ^    VP                   ^8X  d   V P                  VP                  4      # R# r   r   r   s   &&&r   from_QQ_pythonFiniteField.from_QQ_python?  r   r#   c                    V P                  V P                  P                  VP                  VP                  4      4      # )z.Convert ``ModularInteger(mpz)`` to ``dtype``. )rC   r6   from_ZZ_gmpyvalr   s   &&&r   from_FF_gmpyFiniteField.from_FF_gmpyD  s*    xx++AEE266:;;r#   c                V    V P                  V P                  P                  W4      4      # )z%Convert GMPY's ``mpz`` to ``dtype``. )rC   r6   r   r   s   &&&r   r   FiniteField.from_ZZ_gmpyH  s     xx++A233r#   c                ^    VP                   ^8X  d   V P                  VP                  4      # R# )z%Convert GMPY's ``mpq`` to ``dtype``. N)r   r   r   r   s   &&&r   from_QQ_gmpyFiniteField.from_QQ_gmpyL  s&    ==A??1;;// r#   c                    VP                  V4      w  r4V^8X  d+   V P                  V P                  P                  V4      4      # R# )z'Convert mpmath's ``mpf`` to ``dtype``. N)to_rationalrC   r6   )r   rr   r   pqs   &&&  r   from_RealFieldFiniteField.from_RealFieldQ  s9    ~~a 688BFFLLO,, r#   c                    V P                   V P                  V) 3 Uu. uF  p\        V4      NK  	  pp\        W0P                  V P
                  4      '       * # u upi )z7Returns True if ``a`` is a quadratic residue modulo p. )rG   rF   rw   r	   r   r6   )r8   rr   r   polys   &&  r   	is_squareFiniteField.is_squareX  sL     "&499qb 9: 91A 9:#D((DHH=== ;s   Ac                   V P                   ^8X  g   V^ 8X  d   V# V P                  V P                  V) 3 Uu. uF  p\        V4      NK  	  pp\	        W0P                   V P
                  4       FN  p\        V4      ^8X  g   K  V^,          V P                   ^,          8:  g   K6  V P                  V^,          4      u # 	  R# u upi )zSquare root modulo p of ``a`` if it is a quadratic residue.

Explanation
===========
Always returns the square root that is no larger than ``p // 2``.
N)r   rG   rF   rw   r   r6   lenrC   )r8   rr   r   r   factors   &&   r   exsqrtFiniteField.exsqrt^  s     88q=AFH!%499qb 9: 91A 9:#D((DHH=F6{aF1IQ$>zz&),, > 	 ;s   C)
rS   rE   rD   rJ   r6   rC   r   rG   rH   rF   )Tr   ),ra   
__module____qualname____firstlineno____doc__repaliasis_FiniteFieldis_FFis_Numericalhas_assoc_Ringhas_assoc_Fieldr6   r   rK   propertyrO   rX   r\   rb   rg   rj   rm   rs   rx   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes____classdictcell__)__classdict__s   @r   r   r   l   s     Vp CE!!NULNO
C
C#(    #O<
,D8?662
2
<40
-> r#   )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_rw   r&   r1   r:   r   r?   GFr   r#   r   <module>r      s    4  , 8 ) + D 9 C 1 " 8 7% 7 **005FFQFS[!F*E2&#< Xv./%  0 D  Rr#   