K in>dZddlmZddlmZddlmZddlmZddl m Z m Z ddl m Z mZddlmZmZmZmZmZdd lmZmZmZdd lmZdd lmZeGd d eeZdZeGddeZGddeZ edZ!y)z1Implementation of :class:`PolynomialRing` class. FreeModulePolyRing)CompositeDomain) FractionField)Ring) monomial_keybuild_product_order)DMPDMF)GeneratorsNeededPolynomialErrorCoercionFailedExactQuotientFailed NotReversible)dict_from_basicbasic_from_dict _dict_reorder)public)iterableceZdZdZdZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"d Z#d!Z$y")#PolynomialRingBasez Base class for generalized polynomial rings. This base class should be used for uniform access to generalized polynomial rings. Subclasses only supply information about the element storage etc. Do not instantiate. Tgrevlexcj|s tdt|dz }t||_|jj |||_|jj |||_|x|_|_|x|_|_ |jdt|j|_ y)Nzgenerators not specifiedorder)r lenngensdtypezeroonedomaindomsymbolsgensgetr default_orderr)selfr"r$optslevs l/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/domains/old_polynomialring.py__init__zPolynomialRingBase.__init__!s"#=> >$i!mY JJOOC- ::>>#s+!$$ dh#'' tyXXg|D4F4F'GH cV|j|g|jd|jiS)z0Return a new polynomial ring with given domain. r) __class__r$r)r'r"s r* set_domainzPolynomialRingBase.set_domain0s%t~~c@DII@TZZ@@r,ch|j||jt|jdz SNr)rr"rr$r'elements r*newzPolynomialRingBase.new4s&zz'488S^a-?@@r,c8|jj|SN)r ground_newr2s r* _ground_newzPolynomialRingBase._ground_new7sxx""7++r,cptj|t|jdz |jSr1)r from_dictrr$r"r2s r* _from_dictzPolynomialRingBase._from_dict:s&}}Wc$))nq&8$((CCr,ct|j}||jk7rd|znd}t|jdzdj t t|j z|zdzS)Nz order=[,])strrr&r"joinmapr$)r's_orderorderstrs r*__str__zPolynomialRingBase.__str__=sbdjj/$+t/A/A$AI r 488}s"SXXc#tyy.A%BBXMPSSSr,ct|jj|j|j|j |j fSr6)hashr.__name__rr"r$rr's r*__hash__zPolynomialRingBase.__hash__Cs9T^^,,djj$((YY ,- -r,ct|txrj|j|jk(xrO|j|jk(xr4|j|jk(xr|j |j k(S)z0Returns ``True`` if two domains are equivalent. ) isinstancerrr"r$r)r'others r*__eq__zPolynomialRingBase.__eq__Gsk%!34B JJ%++ %B*.((eii*?B II #B(, ekk(A Br,cX|j|jj||Sz.Convert a Python ``int`` object to ``dtype``. r8r"convertK1aK0s r*from_ZZzPolynomialRingBase.from_ZZM ~~bffnnQ344r,cX|j|jj||SrQrRrTs r*from_ZZ_pythonz!PolynomialRingBase.from_ZZ_pythonQrYr,cX|j|jj||Sz3Convert a Python ``Fraction`` object to ``dtype``. rRrTs r*from_QQzPolynomialRingBase.from_QQUrYr,cX|j|jj||Sr]rRrTs r*from_QQ_pythonz!PolynomialRingBase.from_QQ_pythonYrYr,cX|j|jj||S)z,Convert a GMPY ``mpz`` object to ``dtype``. rRrTs r* from_ZZ_gmpyzPolynomialRingBase.from_ZZ_gmpy]rYr,cX|j|jj||S)z,Convert a GMPY ``mpq`` object to ``dtype``. rRrTs r* from_QQ_gmpyzPolynomialRingBase.from_QQ_gmpyarYr,cX|j|jj||S)z.Convert a mpmath ``mpf`` object to ``dtype``. rRrTs r*from_RealFieldz!PolynomialRingBase.from_RealFielderYr,cD|j|k(r|j|Sy)z'Convert a ``ANP`` object to ``dtype``. N)r"r8rTs r*from_AlgebraicFieldz&PolynomialRingBase.from_AlgebraicFieldis! 66R<>>!$ $ r,c djjk(rijjk(rt|Sfd}j |j Dcic]\}}|||c}}St |jjj\}}jjk7r3|Dcgc](}jj|j*}}j tt||Scc}}wcc}w)z/Convert a ``PolyElement`` object to ``dtype``. cPjj|jSr6)r" convert_from)crWrUs r*z8PolynomialRingBase.from_PolynomialRing..ts(;(;Arvv(Fr,) r$r#r"dictr;itemsrto_dictrSzip)rUrVrW convert_dommrlmonomscoeffss` ` r*from_PolynomialRingz&PolynomialRingBase.from_PolynomialRingns 77bjj vv$q'{"F }}AGGI%NDAqaQ&7%NOO*199; BGGLNFFvv>DF266>>!RVV4FF==c&&&9!:; ;&O Gs *D' -D-c|j|jk(rJ|j|jk7r|j|j}||jSt |j |j|j\}}|j|jk7r3|Dcgc](}|jj||j*}}|t t||Scc}w)z'Convert a ``DMP`` object to ``dtype``. )r$r"rSto_listrrprnrq)rUrVrWrtrurls r*from_GlobalPolynomialRingz,PolynomialRingBase.from_GlobalPolynomialRing~s 77bgg vvIIbff%aiik? "*199;INFFvv>DF266>>!RVV4FFd3vv./0 0Gs3-C=cBt|jg|jS)z*Returns a field associated with ``self``. )rr"r$rJs r* get_fieldzPolynomialRingBase.get_fieldsTXX2 22r,ctd)z*Returns a polynomial ring, i.e. ``K[X]``. nested domains not allowedNotImplementedErrorr'r$s r* poly_ringzPolynomialRingBase.poly_ring!">??r,ctd)z)Returns a fraction field, i.e. ``K(X)``. r}r~rs r* frac_fieldzPolynomialRingBase.frac_fieldrr,c~ |j|j|S#ttf$rt d|zwxYw)Nz%s is not a unit)exquor rZeroDivisionErrorrr'rVs r*revertzPolynomialRingBase.revertsB 8::dhh* *#%67 8 2Q 67 7 8s<c$|j|S)z!Extended GCD of ``a`` and ``b``. )gcdexr'rVbs r*rzPolynomialRingBase.gcdexswwqzr,c$|j|S)z Returns GCD of ``a`` and ``b``. )gcdrs r*rzPolynomialRingBase.gcduuQxr,c$|j|S)z Returns LCM of ``a`` and ``b``. )lcmrs r*rzPolynomialRingBase.lcmrr,cV|j|jj|S)zReturns factorial of ``a``. )rr" factorialrs r*rzPolynomialRingBase.factorials zz$((,,Q/00r,ct)z For internal use by the modules class. Convert an iterable of elements of this ring into a sparse distributed module element. r~r'vrs r*_vector_to_sdmz!PolynomialRingBase._vector_to_sdms "!r,cddlm}||}t|Dcgc]}i}}|jD]\}}|||d|dd<|Scc}w)zHelper for _sdm_to_vector.r) sdm_to_dictrN)sympy.polys.distributedmodulesrrangero) r'snrdic_reskrs r* _sdm_to_dicszPolynomialRingBase._sdm_to_dicss`>!n 8$ar$$IIK !DAq C!Iae  ! %s Ac\|j||}|Dcgc] }|| c}Scc}w)a For internal use by the modules class. Convert a sparse distributed module into a list of length ``n``. Examples ======== >>> from sympy import QQ, ilex >>> from sympy.abc import x, y >>> R = QQ.old_poly_ring(x, y, order=ilex) >>> L = [((1, 1, 1), QQ(1)), ((0, 1, 0), QQ(1)), ((0, 0, 1), QQ(2))] >>> R._sdm_to_vector(L, 2) [DMF([[1], [2, 0]], [[1]], QQ), DMF([[1, 0], []], [[1]], QQ)] )r)r'rrdicsxs r*_sdm_to_vectorz!PolynomialRingBase._sdm_to_vectors.   A&!%&AQ&&&s)ct||S)z Generate a free module of rank ``rank`` over ``self``. Examples ======== >>> from sympy.abc import x >>> from sympy import QQ >>> QQ.old_poly_ring(x).free_module(2) QQ[x]**2 r)r'ranks r* free_modulezPolynomialRingBase.free_modules"$--r,N)%rI __module__ __qualname____doc__has_assoc_Ringhas_assoc_Fieldr&r+r/r4r8r;rFrKrOrXr[r^r`rbrdrfrhrvryr{rrrrrrrrrrrr,r*rrsNOM IAA,DT -B 5555555% < 13@@8 1"'( .r,rcddlm}i}t|D]4\}}|jj D]\}}|||f|z<6|||S)z=Helper method for common code in Global and Local poly rings.r) sdm_from_dict)rr enumeraterpro)rrrdiekeyvalues r*_vector_to_sdm_helperrsa< A! "1))+++- "JC!AqdSjM "" E ""r,cReZdZdZdxZZeZdZdZ dZ dZ dZ dZ d Zd Zd Zy ) GlobalPolynomialRingz*A true polynomial ring, with objects DMP. Tcft|tr7tj|t |j dz |j S||j vr*|j|j j|S|j||j t |j dz Sr1) rMrnr r:rr$r"r8rSrr2s r*r4zGlobalPolynomialRing.news gt $==#dii.1* >::gtxxTYY!1CD Dr,cx|jjr |j|j|Sy)a Convert a ``DMF`` object to ``DMP``. Examples ======== >>> from sympy.polys.polyclasses import DMP, DMF >>> from sympy.polys.domains import ZZ >>> from sympy.abc import x >>> f = DMF(([ZZ(1), ZZ(1)], [ZZ(1)]), ZZ) >>> K = ZZ.old_frac_field(x) >>> F = ZZ.old_poly_ring(x).from_FractionField(f, K) >>> F == DMP([ZZ(1), ZZ(1)], ZZ) True >>> type(F) # doctest: +SKIP N)denomis_onerynumerrTs r*from_FractionFieldz'GlobalPolynomialRing.from_FractionFields1, 779  // 2> > r,cJt|jg|jSz!Convert ``a`` to a SymPy object. )r to_sympy_dictr$rs r*to_sympyzGlobalPolynomialRing.to_sympysq0=499==r,c< t||j\}}|j D]#\}}|j j |||<%tj||jdz |j S#t$rtd|d|wxYw))Convert SymPy's expression to ``dtype``. r$zCannot convert z to type r) rr$r rror" from_sympyr r:r)r'rVreprrrs r*rzGlobalPolynomialRing.from_sympys M$QTYY7FCIIK ,DAqXX((+CF ,}}S$**q.$((;;  M 1d!KL L Ms BBcT|jj|jS)z'Returns True if ``LC(a)`` is positive. )r" is_positiveLCrs r*rz GlobalPolynomialRing.is_positive%xx##ADDF++r,cT|jj|jS)z'Returns True if ``LC(a)`` is negative. )r" is_negativerrs r*rz GlobalPolynomialRing.is_negative)rr,cT|jj|jS)z+Returns True if ``LC(a)`` is non-positive. )r"is_nonpositiverrs r*rz#GlobalPolynomialRing.is_nonpositive-xx&&qttv..r,cT|jj|jS)z+Returns True if ``LC(a)`` is non-negative. )r"is_nonnegativerrs r*rz#GlobalPolynomialRing.is_nonnegative1rr,ct||S)aG Examples ======== >>> from sympy import lex, QQ >>> from sympy.abc import x, y >>> R = QQ.old_poly_ring(x, y) >>> f = R.convert(x + 2*y) >>> g = R.convert(x * y) >>> R._vector_to_sdm([f, g], lex) [((1, 1, 1), 1), ((0, 1, 0), 1), ((0, 0, 1), 2)] )rrs r*rz#GlobalPolynomialRing._vector_to_sdm5s%Q..r,N)rIrrris_PolynomialRingis_Polyr rr4rrrrrrrrrr,r*rrsC4"&& EE?2> <,,// /r,rc>eZdZdZeZdZdZdZdZ dZ dZ dZ y ) GeneralizedPolynomialRingz1A generalized polynomial ring, with objects DMF. c8|j||jt|jdz }|j j |j dddt|jzk7rddlm}td||d||S)z4Construct an element of ``self`` domain from ``a``. rrrr)sstrz denominator z not allowed in ) rr"rr$rtermsrsympy.printing.strrr)r'rVrrs r*r4zGeneralizedPolynomialRing.newJsjjDHHc$))nq&89 99;  4::  .q 1! 4S^8K K / $(It"56 6 r,c |j|}|jj|jdddt |j zk(S#t$rYywxYw)NFrrr)rSrrrrrr$rs r* __contains__z&GeneralizedPolynomialRing.__contains__Usa  QAwwyTZZ03A6$s499~:MMM  sA A('A(ct|jjg|jt|j jg|jz Sr)rrrr$rrs r*rz"GeneralizedPolynomialRing.to_sympy\sN 7 7 9FDIIF 7 7 9FDIIFG Hr,c|j\}}t||j\}}t||j\}}|jD]#\}}|jj |||<%|jD]#\}}|jj |||<%|||fj S)rr)as_numer_denomrr$ror"rcancel) r'rVpqnumrdenrrs r*rz$GeneralizedPolynomialRing.from_sympyas!1 3Q 3QIIK ,DAqXX((+CF ,IIK ,DAqXX((+CF ,S#J&&((r,c||z } |j|j|jf}|S#t$rt |||wxYw)z#Exact quotient of ``a`` and ``b``. )r4rrrr)r'rVrrs r*rzGeneralizedPolynomialRing.exquopsS E 2!%%(A 2%aD1 1 2s '0Ac~|jj||}||j|jfSr6)r{rrr)rUrVrWdmfs r*rz,GeneralizedPolynomialRing.from_FractionField}s2lln//26377CGG$%%r,c|jj}|D]}||jz}t|Dcgc]&}|j|z|jz (c}|Scc}w)a Turn an iterable into a sparse distributed module. Note that the vector is multiplied by a unit first to make all entries polynomials. Examples ======== >>> from sympy import ilex, QQ >>> from sympy.abc import x, y >>> R = QQ.old_poly_ring(x, y, order=ilex) >>> f = R.convert((x + 2*y) / (1 + x)) >>> g = R.convert(x * y) >>> R._vector_to_sdm([f, g], ilex) [((0, 0, 1), 2), ((0, 1, 0), 1), ((1, 1, 1), 1), ((1, 2, 1), 1)] )r rrr)r'rrurs r*rz(GeneralizedPolynomialRing._vector_to_sdmsb( HHNN  A NA $Q%Gaggik!'')&;%GOO%Gs+A1N) rIrrrr rr4rrrrrrrr,r*rrEs0; E NH ) &Pr,rc|jdtj}t|r t ||}t |}||d<|j rt|g|i|St|g|i|S)ay Create a generalized multivariate polynomial ring. A generalized polynomial ring is defined by a ground field `K`, a set of generators (typically `x_1, \ldots, x_n`) and a monomial order `<`. The monomial order can be global, local or mixed. In any case it induces a total ordering on the monomials, and there exists for every (non-zero) polynomial `f \in K[x_1, \ldots, x_n]` a well-defined "leading monomial" `LM(f) = LM(f, >)`. One can then define a multiplicative subset `S = S_> = \{f \in K[x_1, \ldots, x_n] | LM(f) = 1\}`. The generalized polynomial ring corresponding to the monomial order is `R = S^{-1}K[x_1, \ldots, x_n]`. If `>` is a so-called global order, that is `1` is the smallest monomial, then we just have `S = K` and `R = K[x_1, \ldots, x_n]`. Examples ======== A few examples may make this clearer. >>> from sympy.abc import x, y >>> from sympy import QQ Our first ring uses global lexicographic order. >>> R1 = QQ.old_poly_ring(x, y, order=(("lex", x, y),)) The second ring uses local lexicographic order. Note that when using a single (non-product) order, you can just specify the name and omit the variables: >>> R2 = QQ.old_poly_ring(x, y, order="ilex") The third and fourth rings use a mixed orders: >>> o1 = (("ilex", x), ("lex", y)) >>> o2 = (("lex", x), ("ilex", y)) >>> R3 = QQ.old_poly_ring(x, y, order=o1) >>> R4 = QQ.old_poly_ring(x, y, order=o2) We will investigate what elements of `K(x, y)` are contained in the various rings. >>> L = [x, 1/x, y/(1 + x), 1/(1 + y), 1/(1 + x*y)] >>> test = lambda R: [f in R for f in L] The first ring is just `K[x, y]`: >>> test(R1) [True, False, False, False, False] The second ring is R1 localised at the maximal ideal (x, y): >>> test(R2) [True, False, True, True, True] The third ring is R1 localised at the prime ideal (x): >>> test(R3) [True, False, True, False, True] Finally the fourth ring is R1 localised at `S = K[x, y] \setminus yK[y]`: >>> test(R4) [True, False, False, True, False] r)r%rr&rr r is_globalr)r"r$r(rs r*PolynomialRingrssL HHW7EE FE#E40  EDM #C7$7$77(rs78??)C,<<QQ".N.N.N.b#T/-T/T/nSP 2SPlN=N=r,