K i)rdZddlZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z dd lmZmZdd lmZdd lmZdd lmZed k(rdgZed k(r8ddlZej2j5d^ZZZeeeefdkrdZndZdZdZ dZ!eeddgGdde e Z"e"xZ#Z$y)z.Implementation of :class:`FiniteField` class. N) GROUND_TYPES)doctest_depends_on) int_valued)Field)ModularIntegerFactory) SimpleDomain) gf_zassenhausgf_irred_p_rabin)CoercionFailed)public) SymPyIntegerflint FiniteField.)rctjtjtj dfd}fd}||fS#t $rYywxYw)Nr)NNcT |S#t$r|cYSwxYwN TypeError)xindexmodnmods e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/domains/finitefield.pyctxz&_modular_int_factory_nmod..ctx/s4 '3<  'a#& & 's  ''c|Sr)csr nmod_polys rpoly_ctxz+_modular_int_factory_nmod..poly_ctx5sS!!)operatorrrrr OverflowError)rrr!rrr s` @@@r_modular_int_factory_nmodr%"s_ NNE *C ::DI Q ' " = s A A$#A$ctjtj|tj|tj fd}fd}||fS)NcP |S#t$r|cYSwxYwrr)rfctxrs rrz*_modular_int_factory_fmpz_mod..ctxAs. "7N "a> ! "s  %%c|Srr)r fctx_poly fmpz_mod_polys rr!z/_modular_int_factory_fmpz_mod..poly_ctxHsR++r")r#rr fmpz_mod_ctxfmpz_mod_poly_ctxr+)rrr!r(r*r+rs @@@@r_modular_int_factory_fmpz_modr.;sM NNE   c "D'',I''M", =r"c |j|}d\}}}t0|j r d}t |\}}|t |\}}|t||||}d}|||fS#t$rtd|zwxYw)Nz"modulus must be an integer, got %s)NNFT)convertr ValueErrorris_primer%r.r)rdom symmetricselfrr!is_flints r_modular_int_factoryr7NsEkk#0C8 S\\^2#6 X ;9#>MC {$Ci> ( ""/ E=CDDEs A&&A>pythongmpy)modulesceZdZdZdZdZdxZZdZdZ dZ dZ dZ d!dZ edZedZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZd"dZd"dZd"dZd"dZ d"dZ!d"dZ"d"dZ#d"dZ$d"dZ%dZ&dZ'd Z(y)#ra 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>ddlm}|}|dkrtd|zt||||\}}}||_||_||_|j d|_|j d|_||_ ||_ ||_ t|j|_ y)Nr)ZZz*modulus must be a positive integer, got %s)sympy.polys.domainsr>r1r7dtype _poly_ctx _is_flintzerooner3rsymtype_tp)r5rr4r>r3rr!r6s r__init__zFiniteField.__init__s* !8ICOP P"6sCD"QXx !!JJqM ::a= ?r"c|jSr)rHr5s rtpzFiniteField.tp xxr"cbt|dd}|ddlm}||jx|_}|S)N _is_fieldr)isprime)getattrsympy.ntheory.primetestrPrrO)r5is_fieldrPs ris_FieldzFiniteField.is_Fields34d3   7(/(9 9DNXr"c d|jzS)NzGF(%s)rrKs r__str__zFiniteField.__str__s$((""r"ct|jj|j|j|j fSr)hash __class____name__rArr3rKs r__hash__zFiniteField.__hash__s,T^^,,djj$((DHHMNNr"ct|txr4|j|jk(xr|j|jk(S)z0Returns ``True`` if two domains are equivalent. ) isinstancerrr3)r5others r__eq__zFiniteField.__eq__s;%-< HH !<&*hh%))&; !BC Cr"cvt|}|jr!||jdzkDr||jz}|S)z,Convert ``val`` to a Python ``int`` object. )rlrFr)r5rhavals rrfzFiniteField.to_ints41v 88txx1}, DHH D r"ct|S)z#Returns True if ``a`` is positive. )boolrgs r is_positivezFiniteField.is_positives Awr"cy)z'Returns True if ``a`` is non-negative. Trrgs ris_nonnegativezFiniteField.is_nonnegativesr"cy)z#Returns True if ``a`` is negative. Frrgs r is_negativezFiniteField.is_negative"sr"c| S)z'Returns True if ``a`` is non-positive. rrgs ris_nonpositivezFiniteField.is_nonpositive&s u r"c~|j|jjt||jSz.Convert ``ModularInteger(int)`` to ``dtype``. )rAr3from_ZZrlK1rhK0s rfrom_FFzFiniteField.from_FF*s(xxs1vrvv677r"c~|j|jjt||jSr{)rAr3from_ZZ_pythonrlr}s rfrom_FF_pythonzFiniteField.from_FF_python.s*xx--c!fbff=>>r"cX|j|jj||Sz'Convert Python's ``int`` to ``dtype``. rAr3rr}s rr|zFiniteField.from_ZZ2"xx--a455r"cX|j|jj||Srrr}s rrzFiniteField.from_ZZ_python6rr"cX|jdk(r|j|jSyz,Convert Python's ``Fraction`` to ``dtype``. r?N denominatorr numeratorr}s rfrom_QQzFiniteField.from_QQ:( ==A $$Q[[1 1 r"cX|jdk(r|j|jSyrrr}s rfrom_QQ_pythonzFiniteField.from_QQ_python?rr"c|j|jj|j|jS)z.Convert ``ModularInteger(mpz)`` to ``dtype``. )rAr3 from_ZZ_gmpyvalr}s r from_FF_gmpyzFiniteField.from_FF_gmpyDs*xx++AEE266:;;r"cX|j|jj||S)z%Convert GMPY's ``mpz`` to ``dtype``. )rAr3rr}s rrzFiniteField.from_ZZ_gmpyHs"xx++Ar233r"cX|jdk(r|j|jSy)z%Convert GMPY's ``mpq`` to ``dtype``. r?N)rrrr}s r from_QQ_gmpyzFiniteField.from_QQ_gmpyLs& ==A ??1;;/ / r"c|j|\}}|dk(r*|j|jj|Sy)z'Convert mpmath's ``mpf`` to ``dtype``. r?N) to_rationalrAr3)r~rhrpqs rfrom_RealFieldzFiniteField.from_RealFieldQs;~~a 1 688BFFLLO, , r"c|j|j| fDcgc] }t|}}t||j|j  Scc}w)z7Returns True if ``a`` is a quadratic residue modulo p. )rErDrlr rr3)r5rhrpolys r is_squarezFiniteField.is_squareXsK"&499qb 9:1A::#D$((DHH===;sAcZ|jdk(s|dk(r|S|j|j| fDcgc] }t|}}t ||j|j D]<}t |dk(s|d|jdzks(|j|dcSycc}w)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``. rorr?N)rrErDrlr r3lenrA)r5rhrrfactors rexsqrtzFiniteField.exsqrt^s 88q=AFH!%499qb 9:1A::#D$((DHH= -F6{aF1IQ$>zz&),, - ;sB()Tr))r[ __module__ __qualname____doc__repaliasis_FiniteFieldis_FF is_Numericalhas_assoc_Ringhas_assoc_Fieldr3rrIpropertyrLrTrWr\r`rbrdrirmrfrsrurwryrrr|rrrrrrrrrrr"rrrlsVp C E!!NULNO C C#(#O< ,D8?662 2 <40 -> r")%rr#sympy.external.gmpyrsympy.utilities.decoratorrsympy.core.numbersrsympy.polys.domains.fieldr"sympy.polys.domains.modularintegerr sympy.polys.domains.simpledomainrsympy.polys.galoistoolsr r sympy.polys.polyerrorsr sympy.utilitiesr sympy.polys.domains.groundtypesr __doctest_skip__r __version__split_major_minor_rlr%r.r7rr<GFrr"rrs4,8)+D9C1"87%7**005FFQ F S[!F* E2&#<Xv./%0D Rr"