K i?dZddlmZddlmZmZddlmZddlm Z ddl m Z m Z ddl mZmZmZmZddlmZdd lmZdd lmZmZmZdd lmZmZmZmZmZdd l m!Z!dd l"m#Z#m$Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*ddl+m,Z,m-Z-ddl.m/Z/ddl0m1Z1m2Z2ddl3m4Z4m5Z5ddl6m7Z7m8Z8ddl9m:Z:ddl;mm?Z?ddl@mAZAmBZBmCZCddlDmEZEddlFmGZGddlHmIZJddlKmLZLddlMmNZNddlOmPZPmQZQmRZRddlSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^dd l_m`Z`maZambZbmcZcmdZdmeZedd!lfmgZgdd"lhmiZidd#ljmkZkmlZlmmZmdd$lnmoZodd%lpmqZqmrZrdd&l;Zsdd<Ztdd'lumvZvd(ZwelGd)d*eZxelGd+d,exZyeld-Zzd.Z{eld/Z|d0Z}d1Z~eldyd2Zeld3Zeld4Zeld5Zeld6Zeld7Zeld8Zeld9Zeld:Zeld;Zeld<Zeld=Zeld>Zeld?Zeld@ZeldAZeldBZeldCZeldDdEdFZeldGZeldHZeldIZeldzdJZeldKZeldzdLZeldMZeldNZeldOZeldPZeldQZeldRZeldSZeldTZeldUZeldVZeldWZeldXZdYZdZZd[Zd\Zd]Zd^Zd_Zd`ZdaZeldbZeldcZelddZeldDdedfZeld{dgZeld|dhZeld}diZeld~dkZeld~dlZelddmZeldnZeldoZeldjdpdqZeldrZeldsZIeldtZelGdudveZeldwZdxZy&)z8User-friendly public interface to polynomial functions. ) annotations)wrapsreducemul)Optional)Counter defaultdict)SExprAddTuple)Basic) _sympifyit)Factors factor_nc factor_terms) pure_complexevalffastlog_evalf_with_bounded_errorquad_to_mpmath) Derivative)Mul _keep_coeff)ilcm)IInteger equal_valued) RelationalEquality)ordered)DummySymbol)sympify_sympify)preorder_traversal bottom_up) BooleanAtom) polyoptions)construct_domain)FFQQZZ) DomainElement) matrix_fglm)groebner)Monomial) monomial_key)DMPDMFANP) OperationNotSupported DomainErrorCoercionFailedUnificationFailedGeneratorsNeededPolynomialErrorMultivariatePolynomialErrorExactQuotientFailedPolificationFailedComputationFailedGeneratorsError)basic_from_dict _sort_gens _unify_gens _dict_reorder_dict_from_expr_parallel_dict_from_expr)together)dup_isolate_real_roots_list)grouppublic filldedent)sympy_deprecation_warning)iterablesiftN) NoConvergencec.tfd}|S)Nct|}t|tr ||St|tr3|j|g|j d|j i}||St|tr' |j|g|j }||StS#t$r[|jrtcYSt|jj}||}|turtddd|cYSwxYw)Ndomaina@ Mixing Poly with non-polynomial expressions in binary operations is deprecated. Either explicitly convert the non-Poly operand to a Poly with as_poly() or convert the Poly to an Expr with as_expr(). z1.6z)deprecated-poly-nonpoly-binary-operations)deprecated_since_versionactive_deprecations_target)r& isinstancePolyr from_exprgensrSr r< is_MatrixNotImplementedgetattras_expr__name__rM)fg expr_methodresultfuncs [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/polytools.pywrapperz_polifyit..wrapperFs QK a 1:  7 # A88qxx8A1:  4  "AKK+AFF+&Aqz!! !)# ;;))%aiik4==A $Q/- 273^  ! s:B&&D AD  D )r)rcres` rd _polifyitrfEs  4[""> Nc$eZdZUdZdZdZdZdZded<ded<dd Z e d Z e d Z e d Zd Ze dZe dZe dZe dZe dZe dZe dZe dZe dZfdZe dZe dZe dZe dZe dZe dZdZ dZ!dd Z"d!Z#d"Z$d#Z%d$Z&d%Z'd&Z(dd'Z)d(Z*d)Z+d*Z,d+Z-d,Z.d-Z/d.Z0dd/Z1dd0Z2dd1Z3dd2Z4dd3Z5d4Z6d5Z7d6Z8d7Z9d8Z:dd9Z;dd:Zd=Z?d>Z@dd?ZAd@ZBdAZCdBZDdCZEdDZFdEZGdFZHdGZIdHZJdIZKdJZLdKZMdLZNdMZOdNZPdOZQdPZRdQZSddRZTddSZUddTZVddUZWdVZXddWZYdXZZdYZ[dZZ\d[Z]dd\Z^d]Z_dd^Z`d_Zad`ZbddaZcddbZdddcZedddZfddeZgdfZhdgZiddhZjdiZkdjZldkZmemZnddlZodmZpddnZqddoZrddpZsdqZtdrZuddsZvdtZwdduZxddvZydwZzdxZ{dyZ|dzZ}dd{Z~d|Zd}Zd~ZdZdZdZdZddZdZdZdZdZddZddZddZdZddZddZddZddZddZddZddZdZdZdZdZdZdZddZdZddZe dZe dZe dZe dZe dZe dZe dZe dZe dZe dZe dZe dZe dZe dZdZdZedZedZedZedZedZedZededZedZedZedZedZedZedZededZededZededZededZdZddZdd„ZdÄZƈxZS)rWaP Generic class for representing and operating on polynomial expressions. See :ref:`polys-docs` for general documentation. Poly is a subclass of Basic rather than Expr but instances can be converted to Expr with the :py:meth:`~.Poly.as_expr` method. .. deprecated:: 1.6 Combining Poly with non-Poly objects in binary operations is deprecated. Explicitly convert both objects to either Poly or Expr first. See :ref:`deprecated-poly-nonpoly-binary-operations`. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y Create a univariate polynomial: >>> Poly(x*(x**2 + x - 1)**2) Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ') Create a univariate polynomial with specific domain: >>> from sympy import sqrt >>> Poly(x**2 + 2*x + sqrt(3), domain='R') Poly(1.0*x**2 + 2.0*x + 1.73205080756888, x, domain='RR') Create a multivariate polynomial: >>> Poly(y*x**2 + x*y + 1) Poly(x**2*y + x*y + 1, x, y, domain='ZZ') Create a univariate polynomial, where y is a constant: >>> Poly(y*x**2 + x*y + 1,x) Poly(y*x**2 + y*x + 1, x, domain='ZZ[y]') You can evaluate the above polynomial as a function of y: >>> Poly(y*x**2 + x*y + 1,x).eval(2) 6*y + 1 See Also ======== sympy.core.expr.Expr reprYTgn$@r4rjztuple[Expr, ...]rYctj||}d|vr tdt|tt t tfr|j||St|tr=t|tr|j||S|jt||St|t!|tu}|j"r|j%||S|j'||S)z:Create a new polynomial instance out of something useful. orderz&'order' keyword is not implemented yet)exclude)evaluate)options build_optionsNotImplementedErrorrVr4r5r6r/_from_domain_elementrNstrdict _from_dict _from_listlistr%typeis_Poly _from_poly _from_exprclsrjrYargsopts rd__new__z Poly.__new__s##D$/ c>%&NO O cCc=9 :++C5 5 c3 '#t$~~c3//~~d3i55#S (<=C{{~~c3//~~c3//rgct|tstd|z|jt |dz k7rtd|d|t j |}||_||_|S)z:Construct :class:`Poly` instance from raw representation. z%invalid polynomial representation: %szinvalid arguments: z, ) rVr4r<levlenrrrjrY)r}rjrYobjs rdnewzPoly.newsh#s#!7#=? ? WWD A %!d"KL LmmC  rgc^t|jjg|jSN)rBrj to_sympy_dictrYselfs rdexprz Poly.exprs#txx557D$))DDrgc6|jf|jzSr)rrYrs rdr~z Poly.argss |dii''rgc6|jf|jzSrrirs rd_hashable_contentzPoly._hashable_contents{TYY&&rgcRtj||}|j||S)(Construct a polynomial from a ``dict``. )rorprur|s rd from_dictzPoly.from_dict'##D$/~~c3''rgcRtj||}|j||S)(Construct a polynomial from a ``list``. )rorprvr|s rd from_listzPoly.from_listrrgcRtj||}|j||S)*Construct a polynomial from a polynomial. )rorprzr|s rd from_polyzPoly.from_polyrrgcRtj||}|j||S+Construct a polynomial from an expression. )rorpr{r|s rdrXzPoly.from_exprrrgc6|j}|s tdt|dz }|j}|t ||\}}n,|j D]\}}|j |||<|jtj|||g|S)rz0Cannot initialize from 'dict' without generatorsrr) rYr;rrSr+itemsconvertrr4r)r}rjrrYlevelrSmonomcoeffs rdruzPoly._from_dictsxx"BD DD A  >*3C8KFC #  3 u#^^E2E  3swws}}S%8@4@@rgcN|j}|s tdt|dk7r tdt|dz }|j}|t ||\}}nt t|j|}|jtj|||g|S)rz0Cannot initialize from 'list' without generatorsrz#'list' representation not supportedr) rYr;rr=rSr+rwmaprrr4r)r}rjrrYrrSs rdrvzPoly._from_listsxx"BD D Y!^-57 7D A  >*3C8KFCs6>>3/0Cswws}}S%8@4@@rgc||jk7r'|j|jg|j}|j}|j}|j }|r_|j|k7rPt |jt |k7r |j|j|S|j|}d|vr|r|j|}|S|dur|j}|S)rrST) __class__rrjrYfieldrSsetr{r]reorder set_domainto_field)r}rjrrYrrSs rdrzzPoly._from_poly%s #-- #''#''-CHH-Cxx  CHH$388}D )~~ckkmS99!ckk4( s?v..(C d],,.C rgcDt||\}}|j||Sr)rFru)r}rjrs rdr{zPoly._from_expr<s%#3,S~~c3''rgc|j}|j}t|dz }|j|g}|jt j |||g|SNr)rYrSrrrr4r)r}rjrrYrSrs rdrrzPoly._from_domain_elementBsUxxD A ~~c"#swws}}S%8@4@@rgc t|Srsuper__hash__rrs rdrz Poly.__hash__Lw!!rgct}|j}tt|D]0}|j D]}||s |||j z}02||j zS)a Free symbols of a polynomial expression. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> Poly(x**2 + 1).free_symbols {x} >>> Poly(x**2 + y).free_symbols {x, y} >>> Poly(x**2 + y, x).free_symbols {x, y} >>> Poly(x**2 + y, x, z).free_symbols {x, y} )rrYrangermonoms free_symbolsfree_symbols_in_domain)rsymbolsrYirs rdrzPoly.free_symbolsOsr*%yys4y! A 8tAw333G   4444rgc|jjt}}|jr"|jD]}||j z}|S|j r$|jD]}||j z}|S)aj Free symbols of the domain of ``self``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 1).free_symbols_in_domain set() >>> Poly(x**2 + y).free_symbols_in_domain set() >>> Poly(x**2 + y, x).free_symbols_in_domain {y} )rjdomr is_Compositerris_EXcoeffs)rrSrgenrs rdrzPoly.free_symbols_in_domainns&((,,   ~~ ,3+++ ,  \\ .5--- .rgc |jdS)z Return the principal generator. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).gen x rrYrs rdrzPoly.gensyy|rgc"|jS)aGet the ground domain of a :py:class:`~.Poly` Returns ======= :py:class:`~.Domain`: Ground domain of the :py:class:`~.Poly`. Examples ======== >>> from sympy import Poly, Symbol >>> x = Symbol('x') >>> p = Poly(x**2 + x) >>> p Poly(x**2 + x, x, domain='ZZ') >>> p.domain ZZ ) get_domainrs rdrSz Poly.domains*  rgc|j|jj|jj|jjg|j S)z3Return zero polynomial with ``self``'s properties. )rrjzerorrrYrs rdrz Poly.zeros=txx dhhllDHHLLANDIINNrgc|j|jj|jj|jjg|j S)z2Return one polynomial with ``self``'s properties. )rrjonerrrYrs rdrzPoly.ones=txx TXX\\488<<@M499MMrgcN|j|\}}}}||||fS)a Make ``f`` and ``g`` belong to the same domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f, g = Poly(x/2 + 1), Poly(2*x + 1) >>> f Poly(1/2*x + 1, x, domain='QQ') >>> g Poly(2*x + 1, x, domain='ZZ') >>> F, G = f.unify(g) >>> F Poly(1/2*x + 1, x, domain='QQ') >>> G Poly(2*x + 1, x, domain='QQ') )_unify)r_r`_perFGs rdunifyz Poly.unifys+2xx{ 311vs1v~rgc bt|}|jsm |jjj |}|jj|j |j|jj |fSt|jtr.perL!GV}tFQJK'88<<,,3773&& &rg)r%ryrjr from_sympyr ground_newr9r:rVr4rDrYrrrEto_dictrrrtrwzipr)r_r`g_coeffrYrrf_monomsf_coeffscrg_monomsg_coeffsrrr}s @rdrz Poly._unifysX AJyy J%%))..q1uuyy!%%0@0@0III aeeS !j&<qvvqvv.Duuyyquuyy$7TQCvv~%2EEMMOQVVT&3"(5599#CKLa Aquuyy 9LHLMM$tC(,C'D"EsCPEEMM#&vv~%2EEMMOQVVT&3"(5599#CKLa Aquuyy 9LHLMM$tC(,C'D"EsCPEEMM#&#A$FG Gkk4 'CA~[" L'Q(JKK L M Ms%L -L'-L, L$c| |j}|5|d|||dzdz}|s%|jjj|S|jj |g|S)ab Create a Poly out of the given representation. Examples ======== >>> from sympy import Poly, ZZ >>> from sympy.abc import x, y >>> from sympy.polys.polyclasses import DMP >>> a = Poly(x**2 + 1) >>> a.per(DMP([ZZ(1), ZZ(1)], ZZ), gens=[y]) Poly(y + 1, y, domain='ZZ') Nr)rYrjrrrr)r_rjrYrs rdrzPoly.persi$ <66D  =4 #44Duuyy))#..q{{s*T**rgctj|jd|i}|j|jj |j S)z Set the ground domain of ``f``. rS)rorprYrrjrrS)r_rSrs rdrzPoly.set_domain,s=##AFFXv,>?uuQUU]]3::.//rgc.|jjS)z Get the ground domain of ``f``. )rjrr_s rdrzPoly.get_domain1suuyyrgcttjj|}|jt |S)z Set the modulus of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(5*x**2 + 2*x - 1, x).set_modulus(2) Poly(x**2 + 1, x, modulus=2) )roModulus preprocessrr,)r_moduluss rd set_moduluszPoly.set_modulus5s+//,,W5||BwK((rgc|j}|jrt|jSt d)z Get the modulus of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, modulus=2).get_modulus() 2 z$not a polynomial over a Galois field)ris_FiniteFieldrcharacteristicr<)r_rSs rd get_moduluszPoly.get_modulusFs8  60023 3!"HI Irgc||jvr1|jr|j||S |j||S|j j ||S#t$rY+wxYw)z)Internal implementation of :func:`subs`. )rY is_numberevalreplacer<r]subs)r_oldrs rd _eval_subszPoly._eval_subs[sj !&&=}}vvc3''99S#..yy{S))'sA A,+A,c|jj\}}t|jDcgc] \}}||vs |}}}|j ||Scc}}w)a  Remove unnecessary generators from ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import a, b, c, d, x >>> Poly(a + x, a, b, c, d, x).exclude() Poly(a + x, a, x, domain='ZZ') r)rjrm enumeraterYr)r_JrjrrYs rdrmz Poly.excludehsV3"+AFF"3B3qzBBuuStu$$Cs AAc |&|jr|j|}}n td||k(s||jvr|S||jvr~||jvrp|j }|j r||j vrFt|j}|||j|<|j|j|Std|d|d|)a Replace ``x`` with ``y`` in generators list. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 1, x).replace(x, y) Poly(y**2 + 1, y, domain='ZZ') z(syntax supported only in univariate caserzCannot replace r in ) is_univariaterr<rYrrrrwindexrrj)r_xy_ignorerrYs rdrz Poly.replace{s 9uua1%>@@ 6Qaff_H ;1AFF?,,.C##q ';AFF|&'TZZ]#uuQUUu..1aKLLrgcB|jj|i|S)z-Match expression from Poly. See Basic.match())r]match)r_r~kwargss rdrz Poly.matchs  qyy{  $1&11rgc tjd|}|st|j|}n,t |jt |k7r t dt ttt|jj|j|}|jtj|t|dz |jj |S)a Efficiently apply new order of generators. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + x*y**2, x, y).reorder(y, x) Poly(y**2*x + x**2, y, x, domain='ZZ') rz7generators list can differ only up to order of elementsrr)roOptionsrCrYrr<rtrwrrErjrrr4rrr)r_rYr~rrjs rdrz Poly.reordersoob$'aff#.D [CI %!IK K4]155==?AFFDIJKLuuS]]3D A quuyyAuMMrgcr|jd}|j|}i}|jD])\}}t|d|rt d|z||||d<+|j |d}|j tj|t|dz |jjg|S)a( Remove dummy generators from ``f`` that are to the left of specified ``gen`` in the generators as ordered. When ``gen`` is an integer, it refers to the generator located at that position within the tuple of generators of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> Poly(y**2 + y*z**2, x, y, z).ltrim(y) Poly(y**2 + y*z**2, y, z, domain='ZZ') >>> Poly(z, x, y, z).ltrim(-1) Poly(z, z, domain='ZZ') T)nativeNzCannot left trim %sr) as_dict _gen_to_levelranyr<rYrr4rrrjr)r_rrjrtermsrrrYs rdltrimz Poly.ltrims&iiti$ OOC IIK %LE55!9~%&;a&?@@$E%)   %vvabzquuS]]5#d)a-CKdKKrgc t}|D]/} |jj|}|j|1|j D]}t|D]\}}||vs |sy!y#t$rt |d|dwxYw)aJ Return ``True`` if ``Poly(f, *gens)`` retains ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> Poly(x*y + 1, x, y, z).has_only_gens(x, y) True >>> Poly(x*y + z, x, y, z).has_only_gens(x, y) False  doesn't have as generatorFT)rrYradd ValueErrorrArr)r_rYindicesrrrrelts rd has_only_genszPoly.has_only_genss % #C # S)  E" #XXZ !E#E* !3G#  ! !  B%9:C@BB Bs A22B ct|jdr|jj}n t|d|j |S)z Make the ground domain a ring. Examples ======== >>> from sympy import Poly, QQ >>> from sympy.abc import x >>> Poly(x**2 + 1, domain=QQ).to_ring() Poly(x**2 + 1, x, domain='ZZ') to_ring)hasattrrjrr7rr_rbs rdrz Poly.to_rings< 155) $UU]]_F'95 5uuV}rgct|jdr|jj}n t|d|j |S)z Make the ground domain a field. Examples ======== >>> from sympy import Poly, ZZ >>> from sympy.abc import x >>> Poly(x**2 + 1, x, domain=ZZ).to_field() Poly(x**2 + 1, x, domain='QQ') r)rrjrr7rrs rdrz Poly.to_field= 155* %UU^^%F':6 6uuV}rgct|jdr|jj}n t|d|j |S)z Make the ground domain exact. Examples ======== >>> from sympy import Poly, RR >>> from sympy.abc import x >>> Poly(x**2 + 1.0, x, domain=RR).to_exact() Poly(x**2 + 1, x, domain='QQ') to_exact)rrjr r7rrs rdr z Poly.to_exact,rrgct|jd||jjxsd\}}|j ||j |S)a Recalculate the ground domain of a polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = Poly(x**2 + 1, x, domain='QQ[y]') >>> f Poly(x**2 + 1, x, domain='QQ[y]') >>> f.retract() Poly(x**2 + 1, x, domain='ZZ') >>> f.retract(field=True) Poly(x**2 + 1, x, domain='QQ') TrN)r compositerS)r+r rSrrrY)r_rrrjs rdretractz Poly.retractAsM($AII4I$8188#8#8#@DBS{{3s{33rgc|d||}}}n|j|}t|t|}}t|jdr|jj |||}n t |d|j |S)z1Take a continuous subsequence of terms of ``f``. rslice)r intrrjr'r7r)r_rmnrrbs rdr'z Poly.sliceYsr 9A!qA"A1vs1v1 155' "UU[[Aq)F'73 3uuV}rgc|jj|Dcgc]'}|jjj|)c}Scc}w)aQ Returns all non-zero coefficients from ``f`` in lex order. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x + 3, x).coeffs() [1, 2, 3] See Also ======== all_coeffs coeff_monomial nth rl)rjrrr)r_rlrs rdrz Poly.coeffsis:(01uu||%|/HI! ""1%IIIs,Ac:|jj|S)aU Returns all non-zero monomials from ``f`` in lex order. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).monoms() [(2, 0), (1, 2), (1, 1), (0, 1)] See Also ======== all_monoms r,)rjrr_rls rdrz Poly.monomss$uu||%|((rgc|jj|Dcgc],\}}||jjj|f.c}}Scc}}w)ac Returns all non-zero terms from ``f`` in lex order. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).terms() [((2, 0), 1), ((1, 2), 2), ((1, 1), 1), ((0, 1), 3)] See Also ======== all_terms r,)rjrrr)r_rlr)rs rdrz Poly.termssC$89uu{{{7OPtq!AEEII&&q)*PPPs1Ac|jjDcgc]'}|jjj|)c}Scc}w)a Returns all coefficients from a univariate polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x - 1, x).all_coeffs() [1, 0, 2, -1] )rj all_coeffsrr)r_rs rdr1zPoly.all_coeffss801uu/?/?/AB! ""1%BBBs,A c6|jjS)a? Returns all monomials from a univariate polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x - 1, x).all_monoms() [(3,), (2,), (1,), (0,)] See Also ======== all_terms )rj all_monomsrs rdr3zPoly.all_monomss$uu!!rgc|jjDcgc],\}}||jjj|f.c}}Scc}}w)a Returns all terms from a univariate polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x - 1, x).all_terms() [((3,), 1), ((2,), 0), ((1,), 2), ((0,), -1)] )rj all_termsrr)r_r)rs rdr5zPoly.all_termss?89uu7HItq!AEEII&&q)*IIIs1Aci}|jD]@\}}|||}t|tr|\}}n|}|s*||vr|||<4td|z|j|g|xs |j i|S)ah Apply a function to all terms of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> def func(k, coeff): ... k = k[0] ... return coeff//10**(2-k) >>> Poly(x**2 + 20*x + 400).termwise(func) Poly(x**2 + 2*x + 4, x, domain='ZZ') z%s monomial was generated twice)rrVtupler<rrY)r_rcrYr~rrrrbs rdtermwisez Poly.termwises$GGI CLE5%'F&%(% u%#(E%L)9EACC Cq{{5>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 2*x - 1).length() 3 )rr rs rdlengthz Poly.lengths199;rgcv|r|jj|S|jj|S)a Switch to a ``dict`` representation. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x*y**2 - y, x, y).as_dict() {(0, 1): -1, (1, 2): 2, (2, 0): 1} r")rjrr)r_r rs rdr z Poly.as_dicts4 55==d=+ +55&&D&1 1rgcn|r|jjS|jjS)z%Switch to a ``list`` representation. )rjto_list to_sympy_list)r_r s rdas_listz Poly.as_list#s( 55==? "55&&( (rgcv|s |jSt|dk(r\t|dtrI|d}t |j }|j D]\}} |j|}|||<t|jjg|S#t$rt|d|dwxYw)ar Convert a Poly instance to an Expr instance. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2 + 2*x*y**2 - y, x, y) >>> f.as_expr() x**2 + 2*x*y**2 - y >>> f.as_expr({x: 5}) 10*y**2 - y + 25 >>> f.as_expr(5, 6) 379 rrrr) rrrVrtrwrYrrrrArBrjr)r_rYmappingrvaluers rdr]z Poly.as_expr*s(66M t9>ja$71gGDFFFs !BB8c^ t|g|i|}|jsy|S#t$rYywxYw)a{Converts ``self`` to a polynomial or returns ``None``. >>> from sympy import sin >>> from sympy.abc import x, y >>> print((x**2 + x*y).as_poly()) Poly(x**2 + x*y, x, y, domain='ZZ') >>> print((x**2 + x*y).as_poly(x, y)) Poly(x**2 + x*y, x, y, domain='ZZ') >>> print((x**2 + sin(y)).as_poly(x, y)) None N)rWryr<)rrYr~polys rdas_polyz Poly.as_polyPs<  ,t,t,D<<   s  ,,ct|jdr|jj}n t|d|j |S)a Convert algebraic coefficients to rationals. Examples ======== >>> from sympy import Poly, I >>> from sympy.abc import x >>> Poly(x**2 + I*x + 1, x, extension=I).lift() Poly(x**4 + 3*x**2 + 1, x, domain='QQ') lift)rrjrGr7rrs rdrGz Poly.liftjs< 155& !UUZZ\F'62 2uuV}rgct|jdr|jj\}}n t|d||j |fS)a+ Reduce degree of ``f`` by mapping ``x_i**m`` to ``y_i``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**6*y**2 + x**3 + 1, x, y).deflate() ((3, 2), Poly(x**2*y + x + 1, x, y, domain='ZZ')) deflate)rrjrIr7rr_rrbs rdrIz Poly.deflatesE 155) $ IAv'95 5!%%-rgc|jj}|jr|S|jst d|zt |jdr|jj |}n t|d|r|j|jz}n|j|jz}|j|g|S)a Inject ground domain generators into ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x) >>> f.inject() Poly(x**2*y + x*y**3 + x*y + 1, x, y, domain='ZZ') >>> f.inject(front=True) Poly(y**3*x + y*x**2 + y*x + 1, y, x, domain='ZZ') z Cannot inject generators over %sinjectfront) rjr is_Numericalryr8rrLr7rrYr)r_rNrrbrYs rdrLz Poly.injects$eeii   H@3FG G 155( #UU\\\.F'84 4 ;;'D66CKK'DquuV#d##rgc|jj}|jstd|zt |}|j d||k(r|j |dd}}n1|j | d|k(r|j d| d}}n t d|j|}t|jdr|jj||}n t|d|j|g|S)a Eject selected generators into the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x, y) >>> f.eject(x) Poly(x*y**3 + (x**2 + x)*y + 1, y, domain='ZZ[x]') >>> f.eject(y) Poly(y*x**2 + (y**3 + y)*x + 1, x, domain='ZZ[y]') zCannot eject generators over %sNTFz'can only eject front or back generatorsejectrM) rjrrOr8rrYrqrLrrQr7r)r_rYrk_gensrNrbs rdrQz Poly.ejects$eeii?#EF F I 66"1: 66!":t5E VVQBC[D 66#A2;5E%9; ;cjj$ 155' "UU[[E[2F'73 3quuV$e$$rgct|jdr|jj\}}n t|d||j |fS)a Remove GCD of terms from the polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**6*y**2 + x**3*y, x, y).terms_gcd() ((3, 1), Poly(x**3*y + 1, x, y, domain='ZZ')) terms_gcd)rrjrUr7rrJs rdrUzPoly.terms_gcdsF 155+ &)IAv';7 7!%%-rgct|jdr|jj|}n t|d|j |S)z Add an element of the ground domain to ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x + 1).add_ground(2) Poly(x + 3, x, domain='ZZ') add_ground)rrjrWr7rr_rrbs rdrWzPoly.add_groundA 155, 'UU%%e,F'<8 8uuV}rgct|jdr|jj|}n t|d|j |S)z Subtract an element of the ground domain from ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x + 1).sub_ground(2) Poly(x - 1, x, domain='ZZ') sub_ground)rrjr[r7rrXs rdr[zPoly.sub_ground rYrgct|jdr|jj|}n t|d|j |S)z Multiply ``f`` by a an element of the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x + 1).mul_ground(2) Poly(2*x + 2, x, domain='ZZ') mul_ground)rrjr]r7rrXs rdr]zPoly.mul_ground"rYrgct|jdr|jj|}n t|d|j |S)aO Quotient of ``f`` by a an element of the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x + 4).quo_ground(2) Poly(x + 2, x, domain='ZZ') >>> Poly(2*x + 3).quo_ground(2) Poly(x + 1, x, domain='ZZ') quo_ground)rrjr_r7rrXs rdr_zPoly.quo_ground7sA" 155, 'UU%%e,F'<8 8uuV}rgct|jdr|jj|}n t|d|j |S)a Exact quotient of ``f`` by a an element of the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x + 4).exquo_ground(2) Poly(x + 2, x, domain='ZZ') >>> Poly(2*x + 3).exquo_ground(2) Traceback (most recent call last): ... ExactQuotientFailed: 2 does not divide 3 in ZZ exquo_ground)rrjrar7rrXs rdrazPoly.exquo_groundOsA& 155. )UU''.F'>: :uuV}rgct|jdr|jj}n t|d|j |S)z Make all coefficients in ``f`` positive. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).abs() Poly(x**2 + 1, x, domain='ZZ') abs)rrjrcr7rrs rdrczPoly.absis< 155% UUYY[F'51 1uuV}rgct|jdr|jj}n t|d|j |S)a4 Negate all coefficients in ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).neg() Poly(-x**2 + 1, x, domain='ZZ') >>> -Poly(x**2 - 1, x) Poly(-x**2 + 1, x, domain='ZZ') neg)rrjrer7rrs rdrezPoly.neg~<" 155% UUYY[F'51 1uuV}rgct|}|js|j|S|j|\}}}}t |j dr|j |}n t|d||S)a[ Add two polynomials ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).add(Poly(x - 2, x)) Poly(x**2 + x - 1, x, domain='ZZ') >>> Poly(x**2 + 1, x) + Poly(x - 2, x) Poly(x**2 + x - 1, x, domain='ZZ') r)r%ryrWrrrjrr7r_r`rrrrrbs rdrzPoly.addi" AJyy<<? "xx{ 31 155% UU1XF'51 16{rgct|}|js|j|S|j|\}}}}t |j dr|j |}n t|d||S)a` Subtract two polynomials ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).sub(Poly(x - 2, x)) Poly(x**2 - x + 3, x, domain='ZZ') >>> Poly(x**2 + 1, x) - Poly(x - 2, x) Poly(x**2 - x + 3, x, domain='ZZ') sub)r%ryr[rrrjrkr7rhs rdrkzPoly.subrirgct|}|js|j|S|j|\}}}}t |j dr|j |}n t|d||S)ap Multiply two polynomials ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).mul(Poly(x - 2, x)) Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ') >>> Poly(x**2 + 1, x)*Poly(x - 2, x) Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ') r)r%ryr]rrrjrr7rhs rdrzPoly.mulrirgct|jdr|jj}n t|d|j |S)a3 Square a polynomial ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x - 2, x).sqr() Poly(x**2 - 4*x + 4, x, domain='ZZ') >>> Poly(x - 2, x)**2 Poly(x**2 - 4*x + 4, x, domain='ZZ') sqr)rrjrnr7rrs rdrnzPoly.sqrrfrgct|}t|jdr|jj|}n t |d|j |S)aX Raise ``f`` to a non-negative power ``n``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x - 2, x).pow(3) Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ') >>> Poly(x - 2, x)**3 Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ') pow)r(rrjrpr7rr_r*rbs rdrpzPoly.pow sG" F 155% UUYYq\F'51 1uuV}rgc|j|\}}}}t|jdr|j|\}}n t |d||||fS)a# Polynomial pseudo-division of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).pdiv(Poly(2*x - 4, x)) (Poly(2*x + 4, x, domain='ZZ'), Poly(20, x, domain='ZZ')) pdiv)rrrjrsr7)r_r`rrrrqrs rdrsz Poly.pdiv%sWxx{ 31 155& !66!9DAq'62 21vs1v~rgc|j|\}}}}t|jdr|j|}n t |d||S)aN Polynomial pseudo-remainder of ``f`` by ``g``. Caveat: The function prem(f, g, x) can be safely used to compute in Z[x] _only_ subresultant polynomial remainder sequences (prs's). To safely compute Euclidean and Sturmian prs's in Z[x] employ anyone of the corresponding functions found in the module sympy.polys.subresultants_qq_zz. The functions in the module with suffix _pg compute prs's in Z[x] employing rem(f, g, x), whereas the functions with suffix _amv compute prs's in Z[x] employing rem_z(f, g, x). The function rem_z(f, g, x) differs from prem(f, g, x) in that to compute the remainder polynomials in Z[x] it premultiplies the divident times the absolute value of the leading coefficient of the divisor raised to the power degree(f, x) - degree(g, x) + 1. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).prem(Poly(2*x - 4, x)) Poly(20, x, domain='ZZ') prem)rrrjrwr7rhs rdrwz Poly.prem<sK<xx{ 31 155& !VVAYF'62 26{rgc|j|\}}}}t|jdr|j|}n t |d||S)a Polynomial pseudo-quotient of ``f`` by ``g``. See the Caveat note in the function prem(f, g). Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).pquo(Poly(2*x - 4, x)) Poly(2*x + 4, x, domain='ZZ') >>> Poly(x**2 - 1, x).pquo(Poly(2*x - 2, x)) Poly(2*x + 2, x, domain='ZZ') pquo)rrrjryr7rhs rdryz Poly.pquocsK&xx{ 31 155& !VVAYF'62 26{rgc&|j|\}}}}t|jdr |j|}n t|d||S#t$r3}|j |j |j d}~wwxYw)a Polynomial exact pseudo-quotient of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).pexquo(Poly(2*x - 2, x)) Poly(2*x + 2, x, domain='ZZ') >>> Poly(x**2 + 1, x).pexquo(Poly(2*x - 4, x)) Traceback (most recent call last): ... ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1 pexquoN)rrrjr{r>rr]r7)r_r`rrrrrbexcs rdr{z Poly.pexquos&xx{ 31 155( # 8!(84 46{ ' 8ggaiik199;77 8sA B.B  Bc|j|\}}}}d}|r:|jr.|js"|j|j}}d}t |j dr|j |\}} n t|d|r% |j| j} } | | } }|||| fS#t$rYwxYw)a Polynomial division with remainder of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x)) (Poly(1/2*x + 1, x, domain='QQ'), Poly(5, x, domain='QQ')) >>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x), auto=False) (Poly(0, x, domain='ZZ'), Poly(x**2 + 1, x, domain='ZZ')) FTdiv) ris_Ringis_Fieldrrrjr~r7rr9) r_r`autorrrrr%rtruQRs rdr~zPoly.divs"!S!Q CKK ::<qAG 155% 558DAq'51 1  yy{AIIK1!11vs1v~ "  s C CCcf|j|\}}}}d}|r:|jr.|js"|j|j}}d}t |j dr|j |}n t|d|r |j}||S#t$rYwxYw)ao Computes the polynomial remainder of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x)) Poly(5, x, domain='ZZ') >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x), auto=False) Poly(x**2 + 1, x, domain='ZZ') FTrem) rrrrrrjrr7rr9) r_r`rrrrrr%rus rdrzPoly.rem"!S!Q CKK ::<qAG 155% aA'51 1  IIK1v "   B$$ B0/B0cf|j|\}}}}d}|r:|jr.|js"|j|j}}d}t |j dr|j |}n t|d|r |j}||S#t$rYwxYw)aa Computes polynomial quotient of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).quo(Poly(2*x - 4, x)) Poly(1/2*x + 1, x, domain='QQ') >>> Poly(x**2 - 1, x).quo(Poly(x - 1, x)) Poly(x + 1, x, domain='ZZ') FTquo) rrrrrrjrr7rr9) r_r`rrrrrr%rts rdrzPoly.quorrc|j|\}}}}d}|r:|jr.|js"|j|j}}d}t |j dr |j |}n t|d|r |j}||S#t$r3} | j|j|jd} ~ wwxYw#t$rYRwxYw)a Computes polynomial exact quotient of ``f`` by ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).exquo(Poly(x - 1, x)) Poly(x + 1, x, domain='ZZ') >>> Poly(x**2 + 1, x).exquo(Poly(2*x - 4, x)) Traceback (most recent call last): ... ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1 FTexquoN) rrrrrrjrr>rr]r7rr9) r_r`rrrrrr%rtr|s rdrz Poly.exquos&!S!Q CKK ::<qAG 155' " 8GGAJ(73 3  IIK1v ' 8ggaiik199;77 8"  s*,B% C$% C!..CC!$ C0/C0c*t|trDt|j}| |cxkr|krnn |dkr||zS|St d|d|d| |jj t |S#t$rt d|zwxYw)z3Returns level associated with the given generator. r-z <= gen < z expected, got z"a valid generator expected, got %s)rVr(rrYr<rr%r)r_rr:s rdr zPoly._gen_to_level9s c3 [Fw#&&7!C<'J%'-vs'<== @vv||GCL11 @%83>@@ @s #A::Bc|j|}t|jdr2|jj|}|dkrtj }|St |d)au Returns degree of ``f`` in ``x_j``. The degree of 0 is negative infinity. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + y*x + 1, x, y).degree() 2 >>> Poly(x**2 + y*x + y, x, y).degree(y) 1 >>> Poly(0, x).degree() -oo degreer)r rrjrr NegativeInfinityr7)r_rrds rdrz Poly.degreeMsU( OOC  155( # QA1u&&H'84 4rgczt|jdr|jjSt|d)z Returns a list of degrees of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + y*x + 1, x, y).degree_list() (2, 1) degree_list)rrjrr7rs rdrzPoly.degree_listks2 155- (55$$& &'=9 9rgczt|jdr|jjSt|d)a Returns the total degree of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + y*x + 1, x, y).total_degree() 2 >>> Poly(x + y**5, x, y).total_degree() 5 total_degree)rrjrr7rs rdrzPoly.total_degree~s2 155. )55%%' ''>: :rgct|tstdt|z||jvr(|jj |}|j}n%t |j}|j|fz}t|jdr,|j|jj||St|d)a Returns the homogeneous polynomial of ``f``. A homogeneous polynomial is a polynomial whose all monomials with non-zero coefficients have the same total degree. If you only want to check if a polynomial is homogeneous, then use :func:`Poly.is_homogeneous`. If you want not only to check if a polynomial is homogeneous but also compute its homogeneous order, then use :func:`Poly.homogeneous_order`. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> f = Poly(x**5 + 2*x**2*y**2 + 9*x*y**3) >>> f.homogenize(z) Poly(x**5 + 2*x**2*y**2*z + 9*x*y**3*z, x, y, z, domain='ZZ') z``Symbol`` expected, got %s homogenizerhomogeneous_order) rVr$ TypeErrorrxrYrrrrjrrr7)r_srrYs rdrzPoly.homogenizes,!V$9DGCD D ; QA66DAFF A66QD=D 155, '55))!,458 8#A':;;rgczt|jdr|jjSt|d)a- Returns the homogeneous order of ``f``. A homogeneous polynomial is a polynomial whose all monomials with non-zero coefficients have the same total degree. This degree is the homogeneous order of ``f``. If you only want to check if a polynomial is homogeneous, then use :func:`Poly.is_homogeneous`. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**5 + 2*x**3*y**2 + 9*x*y**4) >>> f.homogeneous_order() 5 r)rrjrr7rs rdrzPoly.homogeneous_orders4( 155- .55**, ,'+>? ?rgc||j|dSt|jdr|jj}n t |d|jj j |S)z Returns the leading coefficient of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(4*x**3 + 2*x**2 + 3*x, x).LC() 4 rLC)rrrjrr7rr)r_rlrbs rdrzPoly.LCs`  88E?1% % 155$ UUXXZF'40 0uuyy!!&))rgct|jdr|jj}n t|d|jjj |S)z Returns the trailing coefficient of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x**2 + 3*x, x).TC() 0 TC)rrjrr7rrrs rdrzPoly.TCsG 155$ UUXXZF'40 0uuyy!!&))rgcnt|jdr|j|dSt|d)z Returns the last non-zero coefficient of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 + 2*x**2 + 3*x, x).EC() 3 rEC)rrjrr7r.s rdrzPoly.ECs2 155( #88E?2& &'40 0rgc\|jt||jjS)aE Returns the coefficient of ``monom`` in ``f`` if there, else None. Examples ======== >>> from sympy import Poly, exp >>> from sympy.abc import x, y >>> p = Poly(24*x*y*exp(8) + 23*x, x, y) >>> p.coeff_monomial(x) 23 >>> p.coeff_monomial(y) 0 >>> p.coeff_monomial(x*y) 24*exp(8) Note that ``Expr.coeff()`` behaves differently, collecting terms if possible; the Poly must be converted to an Expr to use that method, however: >>> p.as_expr().coeff(x) 24*y*exp(8) + 23 >>> p.as_expr().coeff(y) 24*x*exp(8) >>> p.as_expr().coeff(x*y) 24*exp(8) See Also ======== nth: more efficient query using exponents of the monomial's generators )nthr2rY exponents)r_rs rdcoeff_monomialzPoly.coeff_monomials'Fquuhuaff-7788rgcJt|jdr]t|t|jk7r t d|jj t tt|}n t|d|jjj|S)a. Returns the ``n``-th coefficient of ``f`` where ``N`` are the exponents of the generators in the term of interest. Examples ======== >>> from sympy import Poly, sqrt >>> from sympy.abc import x, y >>> Poly(x**3 + 2*x**2 + 3*x, x).nth(2) 2 >>> Poly(x**3 + 2*x*y**2 + y**2, x, y).nth(1, 2) 2 >>> Poly(4*sqrt(x)*y) Poly(4*y*(sqrt(x)), y, sqrt(x), domain='ZZ') >>> _.nth(1, 1) 4 See Also ======== coeff_monomial rz,exponent of each generator must be specified) rrjrrYrrrwrr(r7rr)r_Nrbs rdrzPoly.nth3sw2 155% 1vQVV$ !OPPQUUYYSa[ 12F'51 1uuyy!!&))rgctd)NzyEither convert to Expr with `as_expr` method to use Expr's coeff method or else use the `coeff_monomial` method of Polys.rq)r_rr*rights rdrz Poly.coeffUs" 01 1rgcRt|j|d|jS)a Returns the leading monomial of ``f``. The Leading monomial signifies the monomial having the highest power of the principal generator in the expression f. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LM() x**2*y**0 rr2rrYr.s rdLMzPoly.LMas"$*AFF33rgcRt|j|d|jS)z Returns the last non-zero monomial of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).EM() x**0*y**1 rrr.s rdEMzPoly.EMus"+QVV44rgc`|j|d\}}t||j|fS)a Returns the leading term of ``f``. The Leading term signifies the term having the highest power of the principal generator in the expression f along with its coefficient. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LT() (x**2*y**0, 4) rrr2rYr_rlrrs rdLTzPoly.LTs0$wwu~a( uqvv&--rgc`|j|d\}}t||j|fS)z Returns the last non-zero term of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).ET() (x**0*y**1, 3) rrrs rdETzPoly.ETs0wwu~b) uqvv&--rgct|jdr|jj}n t|d|jjj |S)z Returns maximum norm of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(-x**2 + 2*x - 3, x).max_norm() 3 max_norm)rrjrr7rrrs rdrz Poly.max_normsH 155* %UU^^%F':6 6uuyy!!&))rgct|jdr|jj}n t|d|jjj |S)z Returns l1 norm of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(-x**2 + 2*x - 3, x).l1_norm() 6 l1_norm)rrjrr7rrrs rdrz Poly.l1_normG 155) $UU]]_F'95 5uuyy!!&))rgc|}|jjjstj|fS|j }|j r$|jjj}t|jdr|jj\}}n t|d|j||j|}}|r |j s||fS||jfS)a Clear denominators, but keep the ground domain. Examples ======== >>> from sympy import Poly, S, QQ >>> from sympy.abc import x >>> f = Poly(x/2 + S(1)/3, x, domain=QQ) >>> f.clear_denoms() (6, Poly(3*x + 2, x, domain='QQ')) >>> f.clear_denoms(convert=True) (6, Poly(3*x + 2, x, domain='ZZ')) clear_denoms)rjrrr Onerhas_assoc_Ringget_ringrrr7rrr)rrr_rrrbs rdrzPoly.clear_denomss$ uuyy!!55!8Olln   %%))$$&C 155. )EE..0ME6'>: :<<&f qc00!8O!))+% %rgc*|}|j|\}}}}||}||}|jr |js||fS|jd\}}|jd\}}|j |}|j |}||fS)a Clear denominators in a rational function ``f/g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = Poly(x**2/y + 1, x) >>> g = Poly(x**3 + y, x) >>> p, q = f.rat_clear_denoms(g) >>> p Poly(x**2 + y, x, domain='ZZ[y]') >>> q Poly(y*x**3 + y**2, x, domain='ZZ[y]') Tr)rrrrr])rr`r_rrabs rdrat_clear_denomszPoly.rat_clear_denomss* !S!Q F F !3!3a4K~~d~+1~~d~+1 LLO LLO!t rgc|}|jddr0|jjjr|j }t |jdr|s+|j |jjdS|j}|D]F}t|tr|\}}n|d}}|jt||j|}H|j |St|d)a Computes indefinite integral of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x + 1, x).integrate() Poly(1/3*x**3 + x**2 + x, x, domain='QQ') >>> Poly(x*y**2 + x, x, y).integrate((0, 1), (1, 0)) Poly(1/2*x**2*y**2 + 1/2*x**2, x, y, domain='QQ') rT integraterr)) getrjrrrrrrrVr7r(r r7)rspecsr~r_rjspecrr)s rdrzPoly.integrate# s"  88FD !aeeii&7&7 A 155+ &uuQUU__q_122%%C BdE*!FC!1CmmCFAOOC,@A  B55: ';7 7rgc|jddst|g|i|St|jdr|s+|j |jj dS|j}|D]F}t |tr|\}}n|d}}|j t||j|}H|j |St|d)aX Computes partial derivative of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + 2*x + 1, x).diff() Poly(2*x + 2, x, domain='ZZ') >>> Poly(x*y**2 + x, x, y).diff((0, 0), (1, 1)) Poly(2*x*y, x, y, domain='ZZ') rnTdiffrr) rrrrjrrrVr7r(r r7)r_rrrjrrr)s rdrz Poly.diffK s"zz*d+a2%262 2 155& !uuQUUZZ!Z_--%%C =dE*!FC!1Chhs1vqs';<  =55: '62 2rgc|}|t|tr.|}|jD]\}}|j||}|St|tt fr`|}t |t |jkDr tdt|j|D]\}}|j||}|Sd|}} n|j|} t|jds t|d |jj|| } |j-| | S#t$r|s%td|d|jj t#|g\} \}|j%j'| |j} |j)| }| j+|| }|jj|| } YwxYw)a Evaluate ``f`` at ``a`` in the given variable. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> Poly(x**2 + 2*x + 3, x).eval(2) 11 >>> Poly(2*x*y + 3*x + y + 2, x, y).eval(x, 2) Poly(5*y + 8, y, domain='ZZ') >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z) >>> f.eval({x: 2}) Poly(5*y + 2*z + 6, y, z, domain='ZZ') >>> f.eval({x: 2, y: 5}) Poly(2*z + 31, z, domain='ZZ') >>> f.eval({x: 2, y: 5, z: 7}) 45 >>> f.eval((2, 5)) Poly(2*z + 31, z, domain='ZZ') >>> f(2, 5) Poly(2*z + 31, z, domain='ZZ') ztoo many values providedrrzCannot evaluate at rr)rVrtrrr7rwrrYrrr rrjr7r9r8rr+runify_with_symbolsrrr) rrrrr_rArrBvaluesrrba_domain new_domains rdrz Poly.evals s>  9!T"")--/+JCsE*A+At}-v;QVV,$%?@@"%afff"5+JCsE*A+!1"Aquuf%'62 2 *UUZZ1%FuuVAu&& *!1aeeii"PQQ 0! 5 #1\\^>>xP LL,&&q(3Aq) *s2D!!B*G Gc$|j|S)az Evaluate ``f`` at the give values. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y, z >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z) >>> f(2) Poly(5*y + 2*z + 6, y, z, domain='ZZ') >>> f(2, 5) Poly(2*z + 31, z, domain='ZZ') >>> f(2, 5, 7) 45 )r)r_rs rd__call__z Poly.__call__ s(vvf~rgc|j|\}}}}|r,|jr |j|j}}t|jdr|j |\}}n t |d||||fS)a Half extended Euclidean algorithm of ``f`` and ``g``. Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15 >>> g = x**3 + x**2 - 4*x - 4 >>> Poly(f).half_gcdex(Poly(g)) (Poly(-1/5*x + 3/5, x, domain='QQ'), Poly(x + 1, x, domain='QQ')) half_gcdex)rrrrrjrr7) r_r`rrrrrrhs rdrzPoly.half_gcdex su&!S!Q CKK::<qA 155, '<<?DAq'<8 81vs1v~rgc(|j|\}}}}|r,|jr |j|j}}t|jdr|j |\}}} n t |d|||||| fS)a Extended Euclidean algorithm of ``f`` and ``g``. Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15 >>> g = x**3 + x**2 - 4*x - 4 >>> Poly(f).gcdex(Poly(g)) (Poly(-1/5*x + 3/5, x, domain='QQ'), Poly(1/5*x**2 - 6/5*x + 2, x, domain='QQ'), Poly(x + 1, x, domain='QQ')) gcdex)rrrrrjrr7) r_r`rrrrrrtrs rdrz Poly.gcdex s~*!S!Q CKK::<qA 155' "ggajGAq!'73 31vs1vs1v%%rgc|j|\}}}}|r,|jr |j|j}}t|jdr|j |}n t |d||S)a Invert ``f`` modulo ``g`` when possible. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).invert(Poly(2*x - 1, x)) Poly(-4/3, x, domain='QQ') >>> Poly(x**2 - 1, x).invert(Poly(x - 1, x)) Traceback (most recent call last): ... NotInvertible: zero divisor invert)rrrrrjrr7)r_r`rrrrrrbs rdrz Poly.invert si&!S!Q CKK::<qA 155( #XXa[F'84 46{rgct|jdr%|jjt|}n t |d|j |S)ad Compute ``f**(-1)`` mod ``x**n``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(1, x).revert(2) Poly(1, x, domain='ZZ') >>> Poly(1 + x, x).revert(1) Poly(1, x, domain='ZZ') >>> Poly(x**2 - 2, x).revert(2) Traceback (most recent call last): ... NotReversible: only units are reversible in a ring >>> Poly(1/x, x).revert(1) Traceback (most recent call last): ... PolynomialError: 1/x contains an element of the generators set revert)rrjrr(r7rrqs rdrz Poly.revert3 sC6 155( #UU\\#a&)F'84 4uuV}rgc|j|\}}}}t|jdr|j|}n t |dt t ||S)ad Computes the subresultant PRS of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 1, x).subresultants(Poly(x**2 - 1, x)) [Poly(x**2 + 1, x, domain='ZZ'), Poly(x**2 - 1, x, domain='ZZ'), Poly(-2, x, domain='ZZ')] subresultants)rrrjrr7rwrrhs rdrzPoly.subresultantsU sT xx{ 31 155/ *__Q'F'?; ;CV$%%rgc|j|\}}}}t|jdr+|r|j||\}}n|j|}n t |d|r||dt t |fS||dS)a Computes the resultant of ``f`` and ``g`` via PRS. If includePRS=True, it includes the subresultant PRS in the result. Because the PRS is used to calculate the resultant, this is more efficient than calling :func:`subresultants` separately. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = Poly(x**2 + 1, x) >>> f.resultant(Poly(x**2 - 1, x)) 4 >>> f.resultant(Poly(x**2 - 1, x), includePRS=True) (4, [Poly(x**2 + 1, x, domain='ZZ'), Poly(x**2 - 1, x, domain='ZZ'), Poly(-2, x, domain='ZZ')]) resultant includePRSrr)rrrjrr7rwr) r_r`rrrrrrbrs rdrzPoly.resultantn s.xx{ 31 155+ &KKjKA Q';7 7 q)4C +<= =6!$$rgct|jdr|jj}n t|d|j |dS)z Computes the discriminant of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + 2*x + 3, x).discriminant() -8 discriminantrr)rrjrr7rrs rdrzPoly.discriminant sD 155. )UU'')F'>: :uuVAu&&rgc ddlm}|||S)aCompute the *dispersion set* of two polynomials. For two polynomials `f(x)` and `g(x)` with `\deg f > 0` and `\deg g > 0` the dispersion set `\operatorname{J}(f, g)` is defined as: .. math:: \operatorname{J}(f, g) & := \{a \in \mathbb{N}_0 | \gcd(f(x), g(x+a)) \neq 1\} \\ & = \{a \in \mathbb{N}_0 | \deg \gcd(f(x), g(x+a)) \geq 1\} For a single polynomial one defines `\operatorname{J}(f) := \operatorname{J}(f, f)`. Examples ======== >>> from sympy import poly >>> from sympy.polys.dispersion import dispersion, dispersionset >>> from sympy.abc import x Dispersion set and dispersion of a simple polynomial: >>> fp = poly((x - 3)*(x + 3), x) >>> sorted(dispersionset(fp)) [0, 6] >>> dispersion(fp) 6 Note that the definition of the dispersion is not symmetric: >>> fp = poly(x**4 - 3*x**2 + 1, x) >>> gp = fp.shift(-3) >>> sorted(dispersionset(fp, gp)) [2, 3, 4] >>> dispersion(fp, gp) 4 >>> sorted(dispersionset(gp, fp)) [] >>> dispersion(gp, fp) -oo Computing the dispersion also works over field extensions: >>> from sympy import sqrt >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ') >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ') >>> sorted(dispersionset(fp, gp)) [2] >>> sorted(dispersionset(gp, fp)) [1, 4] We can even perform the computations for polynomials having symbolic coefficients: >>> from sympy.abc import a >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x) >>> sorted(dispersionset(fp)) [0, 1] See Also ======== dispersion References ========== 1. [ManWright94]_ 2. [Koepf98]_ 3. [Abramov71]_ 4. [Man93]_ r) dispersionset)sympy.polys.dispersionr)r_r`rs rdrzPoly.dispersionset sP 9Q""rgc ddlm}|||S)aCompute the *dispersion* of polynomials. For two polynomials `f(x)` and `g(x)` with `\deg f > 0` and `\deg g > 0` the dispersion `\operatorname{dis}(f, g)` is defined as: .. math:: \operatorname{dis}(f, g) & := \max\{ J(f,g) \cup \{0\} \} \\ & = \max\{ \{a \in \mathbb{N} | \gcd(f(x), g(x+a)) \neq 1\} \cup \{0\} \} and for a single polynomial `\operatorname{dis}(f) := \operatorname{dis}(f, f)`. Examples ======== >>> from sympy import poly >>> from sympy.polys.dispersion import dispersion, dispersionset >>> from sympy.abc import x Dispersion set and dispersion of a simple polynomial: >>> fp = poly((x - 3)*(x + 3), x) >>> sorted(dispersionset(fp)) [0, 6] >>> dispersion(fp) 6 Note that the definition of the dispersion is not symmetric: >>> fp = poly(x**4 - 3*x**2 + 1, x) >>> gp = fp.shift(-3) >>> sorted(dispersionset(fp, gp)) [2, 3, 4] >>> dispersion(fp, gp) 4 >>> sorted(dispersionset(gp, fp)) [] >>> dispersion(gp, fp) -oo Computing the dispersion also works over field extensions: >>> from sympy import sqrt >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ') >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ') >>> sorted(dispersionset(fp, gp)) [2] >>> sorted(dispersionset(gp, fp)) [1, 4] We can even perform the computations for polynomials having symbolic coefficients: >>> from sympy.abc import a >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x) >>> sorted(dispersionset(fp)) [0, 1] See Also ======== dispersionset References ========== 1. [ManWright94]_ 2. [Koepf98]_ 3. [Abramov71]_ 4. [Man93]_ r) dispersion)rr)r_r`rs rdrzPoly.dispersion sP 6!Qrgc|j|\}}}}t|jdr|j|\}}}n t |d||||||fS)a# Returns the GCD of ``f`` and ``g`` and their cofactors. Returns polynomials ``(h, cff, cfg)`` such that ``h = gcd(f, g)``, and ``cff = quo(f, h)`` and ``cfg = quo(g, h)`` are, so called, cofactors of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).cofactors(Poly(x**2 - 3*x + 2, x)) (Poly(x - 1, x, domain='ZZ'), Poly(x + 1, x, domain='ZZ'), Poly(x - 2, x, domain='ZZ')) cofactors)rrrjrr7) r_r`rrrrrcffcfgs rdrzPoly.cofactors> s`(xx{ 31 155+ &++a.KAsC';7 71vs3xS))rgc|j|\}}}}t|jdr|j|}n t |d||S)a  Returns the polynomial GCD of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).gcd(Poly(x**2 - 3*x + 2, x)) Poly(x - 1, x, domain='ZZ') gcd)rrrjrr7rhs rdrzPoly.gcd[ Kxx{ 31 155% UU1XF'51 16{rgc|j|\}}}}t|jdr|j|}n t |d||S)a Returns polynomial LCM of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 1, x).lcm(Poly(x**2 - 3*x + 2, x)) Poly(x**3 - 2*x**2 - x + 2, x, domain='ZZ') lcm)rrrjrr7rhs rdrzPoly.lcmr rrgc|jjj|}t|jdr|jj |}n t |d|j |S)a Reduce ``f`` modulo a constant ``p``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**3 + 3*x**2 + 5*x + 7, x).trunc(3) Poly(-x**3 - x + 1, x, domain='ZZ') trunc)rjrrrrr7r)r_prbs rdrz Poly.trunc sV EEII  a  155' "UU[[^F'73 3uuV}rgc|}|r0|jjjr|j}t |jdr|jj }n t |d|j|S)az Divides all coefficients by ``LC(f)``. Examples ======== >>> from sympy import Poly, ZZ >>> from sympy.abc import x >>> Poly(3*x**2 + 6*x + 9, x, domain=ZZ).monic() Poly(x**2 + 2*x + 3, x, domain='QQ') >>> Poly(3*x**2 + 4*x + 2, x, domain=ZZ).monic() Poly(x**2 + 4/3*x + 2/3, x, domain='QQ') monic)rjrrrrrr7rrrr_rbs rdrz Poly.monic s_"  AEEII%% A 155' "UU[[]F'73 3uuV}rgct|jdr|jj}n t|d|jjj |S)z Returns the GCD of polynomial coefficients. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(6*x**2 + 8*x + 12, x).content() 2 content)rrjrr7rrrs rdrz Poly.content rrgct|jdr|jj\}}n t|d|jjj ||j |fS)a Returns the content and a primitive form of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**2 + 8*x + 12, x).primitive() (2, Poly(x**2 + 4*x + 6, x, domain='ZZ')) primitive)rrjrr7rrr)r_contrbs rdrzPoly.primitive sY 155+ &55??,LD&';7 7uuyy!!$'v66rgc|j|\}}}}t|jdr|j|}n t |d||S)a Computes the functional composition of ``f`` and ``g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + x, x).compose(Poly(x - 1, x)) Poly(x**2 - x, x, domain='ZZ') compose)rrrjrr7rhs rdrz Poly.compose sKxx{ 31 155) $YYq\F'95 56{rgct|jdr|jj}n t|dt t |j |S)a= Computes a functional decomposition of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**4 + 2*x**3 - x - 1, x, domain='ZZ').decompose() [Poly(x**2 - x - 1, x, domain='ZZ'), Poly(x**2 + x, x, domain='ZZ')] decompose)rrjrr7rwrrrs rdrzPoly.decompose sE 155+ &UU__&F';7 7Cv&''rgcV|j|jj|S)am Efficiently compute Taylor shift ``f(x + a)``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 2*x + 1, x).shift(2) Poly(x**2 + 2*x + 1, x, domain='ZZ') See Also ======== shift_list: Analogous method for multivariate polynomials. )rrjshiftr_rs rdrz Poly.shift s$uuQUU[[^$$rgcV|j|jj|S)ah Efficiently compute Taylor shift ``f(X + A)``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x*y, [x,y]).shift_list([1, 2]) == Poly((x+1)*(y+2), [x,y]) True See Also ======== shift: Analogous method for univariate polynomials. )rrj shift_listrs rdr zPoly.shift_list' s"$uuQUU%%a())rgcB|j|\}}|j|\}}|j|\}}t|jdr1|jj|j|j}n t |d|j |S)a3 Efficiently evaluate the functional transformation ``q**n * f(p/q)``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 2*x + 1, x).transform(Poly(x + 1, x), Poly(x - 1, x)) Poly(4, x, domain='ZZ') transform)rrrjr r7r)r_rrtPrrrbs rdr zPoly.transform; s|wwqz1wwqz1wwqz1 155+ &UU__QUUAEE2F';7 7uuV}rgc"|}|r0|jjjr|j}t |jdr|jj }n t |dtt|j|S)a Computes the Sturm sequence of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 - 2*x**2 + x - 3, x).sturm() [Poly(x**3 - 2*x**2 + x - 3, x, domain='QQ'), Poly(3*x**2 - 4*x + 1, x, domain='QQ'), Poly(2/9*x + 25/9, x, domain='QQ'), Poly(-2079/4, x, domain='QQ')] sturm) rjrrrrrr7rwrrrs rdrz Poly.sturmU sg"  AEEII%% A 155' "UU[[]F'73 3Cv&''rgct|jdr|jj}n t|d|Dcgc]\}}|j ||fc}}Scc}}w)aI Computes greatest factorial factorization of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = x**5 + 2*x**4 - x**3 - 2*x**2 >>> Poly(f).gff_list() [(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)] gff_list)rrjrr7r)r_rbr`rRs rdrz Poly.gff_listr sS 155* %UU^^%F':6 6*01$!Qq1 111sA$ct|jdr|jj}n t|d|j |S)a Computes the product, ``Norm(f)``, of the conjugates of a polynomial ``f`` defined over a number field ``K``. Examples ======== >>> from sympy import Poly, sqrt >>> from sympy.abc import x >>> a, b = sqrt(2), sqrt(3) A polynomial over a quadratic extension. Two conjugates x - a and x + a. >>> f = Poly(x - a, x, extension=a) >>> f.norm() Poly(x**2 - 2, x, domain='QQ') A polynomial over a quartic extension. Four conjugates x - a, x - a, x + a and x + a. >>> f = Poly(x - a, x, extension=(a, b)) >>> f.norm() Poly(x**4 - 4*x**2 + 4, x, domain='QQ') norm)rrjrr7r)r_rus rdrz Poly.norm s;8 155& ! A'62 2uuQxrgct|jdr|jj\}}}n t|d||j ||j |fS)ah Computes square-free norm of ``f``. Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and ``r(x) = Norm(g(x))`` is a square-free polynomial over ``K``, where ``a`` is the algebraic extension of the ground domain. Examples ======== >>> from sympy import Poly, sqrt >>> from sympy.abc import x >>> s, f, r = Poly(x**2 + 1, x, extension=[sqrt(3)]).sqf_norm() >>> s [1] >>> f Poly(x**2 - 2*sqrt(3)*x + 4, x, domain='QQ') >>> r Poly(x**4 - 4*x**2 + 16, x, domain='QQ') sqf_norm)rrjrr7r)r_rr`rus rdrz Poly.sqf_norm sR0 155* %eenn&GAq!':6 6!%%(AEE!H$$rgct|jdr|jj}n t|d|j |S)z Computes square-free part of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**3 - 3*x - 2, x).sqf_part() Poly(x**2 - x - 2, x, domain='ZZ') sqf_part)rrjrr7rrs rdrz Poly.sqf_part rrgc&t|jdr|jj|\}}n t|d|jjj ||Dcgc]\}}|j ||fc}}fScc}}w)a  Returns a list of square-free factors of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16 >>> Poly(f).sqf_list() (2, [(Poly(x + 1, x, domain='ZZ'), 2), (Poly(x + 2, x, domain='ZZ'), 3)]) >>> Poly(f).sqf_list(all=True) (2, [(Poly(1, x, domain='ZZ'), 1), (Poly(x + 1, x, domain='ZZ'), 2), (Poly(x + 2, x, domain='ZZ'), 3)]) sqf_list)rrjrr7rrr)r_allrfactorsr`rRs rdrz Poly.sqf_list sq, 155* %UU^^C0NE7':6 6uuyy!!%(W*MTQAEE!Ha=*MMM*Ms+B ct|jdr|jj|}n t|d|Dcgc]\}}|j ||fc}}Scc}}w)a Returns a list of square-free factors of ``f``. Examples ======== >>> from sympy import Poly, expand >>> from sympy.abc import x >>> f = expand(2*(x + 1)**3*x**4) >>> f 2*x**7 + 6*x**6 + 6*x**5 + 2*x**4 >>> Poly(f).sqf_list_include() [(Poly(2, x, domain='ZZ'), 1), (Poly(x + 1, x, domain='ZZ'), 3), (Poly(x, x, domain='ZZ'), 4)] >>> Poly(f).sqf_list_include(all=True) [(Poly(2, x, domain='ZZ'), 1), (Poly(1, x, domain='ZZ'), 2), (Poly(x + 1, x, domain='ZZ'), 3), (Poly(x, x, domain='ZZ'), 4)] sqf_list_include)rrjrr7r)r_rrr`rRs rdrzPoly.sqf_list_include sY4 155, -ee,,S1G'+=> >*12$!Qq1 222sA%ct|jdr |jj\}}n t|d|jjj||Dcgc]\}}|j||fc}}fS#t$r?|j dk(r|j gfcYSt j|dfgfcYSwxYwcc}}w)a~ Returns a list of irreducible factors of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y >>> Poly(f).factor_list() (2, [(Poly(x + y, x, y, domain='ZZ'), 1), (Poly(x**2 + 1, x, y, domain='ZZ'), 2)]) factor_listrr) rrjrr8rr]r rr7rrr)r_rrr`rRs rdrzPoly.factor_list s" 155- ( +!"!2!2!4w(=9 9uuyy!!%(W*MTQAEE!Ha=*MMM +88:?99;?*55Aq6(?*  ++NsB +C .C=CCct|jdr |jj}n t |d|Dcgc]\}}|j ||fc}}S#t$r|dfgcYSwxYwcc}}w)a Returns a list of irreducible factors of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y >>> Poly(f).factor_list_include() [(Poly(2*x + 2*y, x, y, domain='ZZ'), 1), (Poly(x**2 + 1, x, y, domain='ZZ'), 2)] factor_list_includer)rrjr r8r7r)r_rr`rRs rdr zPoly.factor_list_include< s{" 155/ 0 %%335(+@A A*12$!Qq1 22  Ax 3sA%A9%A65A6ch|%tj|}|dkr td|tj|}|tj|}t|jdr"|jj ||||||}n t |d|rLd}|stt||Sd} |\} } tt|| tt| | fSd}|stt||Sd} |\} } tt|| tt| | fS) a Compute isolating intervals for roots of ``f``. For real roots the Vincent-Akritas-Strzebonski (VAS) continued fractions method is used. References ========== .. [#] Alkiviadis G. Akritas and Adam W. Strzebonski: A Comparative Study of Two Real Root Isolation Methods . Nonlinear Analysis: Modelling and Control, Vol. 10, No. 4, 297-304, 2005. .. [#] Alkiviadis G. Akritas, Adam W. Strzebonski and Panagiotis S. Vigklas: Improving the Performance of the Continued Fractions Method Using new Bounds of Positive Roots. Nonlinear Analysis: Modelling and Control, Vol. 13, No. 3, 265-279, 2008. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 3, x).intervals() [((-2, -1), 1), ((1, 2), 1)] >>> Poly(x**2 - 3, x).intervals(eps=1e-2) [((-26/15, -19/11), 1), ((19/11, 26/15), 1)] r!'eps' must be a positive rational intervalsrepsinfsupfastsqfc`|\}}tj|tj|fSrr-r)intervalrrs rd_realzPoly.intervals.._real s&1 A A77rgc|\\}}\}}tj|ttj|zztj|ttj|zzfSrr-rr) rectangleuvrrs rd_complexz Poly.intervals.._complex sW!*AA A2;;q>)99 A2;;q>)99;;rgcj|\\}}}tj|tj|f|fSrr+)r,rrrRs rdr-zPoly.intervals.._real s/$ AQQ8!<._complex sg&/# !Q!Q!Q!BKKN*::Q!BKKN*::<=>@@rg) r-rrrrjr#r7rwr) r_rr%r&r'r(r)rbr-r3 real_part complex_parts rdr#zPoly.intervalsW s34 ?**S/Cax !DEE ?**S/C ?**S/C 155+ &UU__Scs3%HF(;7 7  8Cv.// ; '- #I|E9-.S<5P0QQ Q =Cv.// @ '- #I|E9-.S<5P0QQ Qrgc|r|js tdtj|tj|}}|%tj|}|dkr t d| t |}n|d}t |jdr$|jj|||||\}}n t|dtj|tj|fS)a Refine an isolating interval of a root to the given precision. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 3, x).refine_root(1, 2, eps=1e-2) (19/11, 26/15) z&only square-free polynomials supportedrr"r refine_root)r%stepsr() is_sqfr<r-rrr(rrjr9r7r) r_rrr%r:r( check_sqfr Ts rdr9zPoly.refine_root s QXX!"JK Kzz!}bjjm1 ?**S/Cax !DEE  JE [E 155- (55$$Qs%d$KDAq'=9 9{{1~r{{1~--rgcdd\}}|rt|}|tjurd}nR|j\}}|st j |}n't ttj ||fd}}|rt|}|tjurd}nR|j\}}|st j |}n't ttj ||fd}}|rL|rJt|jdr(|jj||}t|St|d|r||tjf}|r||tjf}t|jdr(|jj||}t|St|d)a< Return the number of roots of ``f`` in ``[inf, sup]`` interval. Examples ======== >>> from sympy import Poly, I >>> from sympy.abc import x >>> Poly(x**4 - 4, x).count_roots(-3, 3) 2 >>> Poly(x**4 - 4, x).count_roots(0, 1 + 3*I) 1 TTNFcount_real_rootsr&r'count_complex_roots)r%r r as_real_imagr-rrwrInfinityrrjr@r7rrBr)r_r&r'inf_realsup_realreimcounts rd count_rootszPoly.count_roots s (( ?#,Ca((())+B**S/C$(RZZ"b)B$CUC ?#,Cajj ))+B**S/C$(RZZ"b)B$CUC quu01..3C.@u~,A/ABBCOBGGnCOBGGnquu3411cs1Cu~,A/DEErgcZtjjj|||S)a  Get an indexed root of a polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = Poly(2*x**3 - 7*x**2 + 4*x + 4) >>> f.root(0) -1/2 >>> f.root(1) 2 >>> f.root(2) 2 >>> f.root(3) Traceback (most recent call last): ... IndexError: root index out of [-3, 2] range, got 3 >>> Poly(x**5 + x + 1).root(0) CRootOf(x**3 - x**2 + 1, 0) radicals)sympypolys rootoftoolsrootof)r_rrMs rdrootz Poly.roots&6{{&&--a-JJrgctjjjj ||}|r|St |dS)a Return a list of real roots with multiplicities. See :func:`real_roots` for more explanation. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).real_roots() [-1/2, 2, 2] >>> Poly(x**3 + x + 1).real_roots() [CRootOf(x**3 + x + 1, 0)] rLFmultiple)rNrOrPCRootOf real_rootsrJ)r_rUrMrealss rdrWzPoly.real_roots$s>" ''//::1x:P L/ /rgctjjjj ||}|r|St |dS)a Return a list of real and complex roots with multiplicities. See :func:`all_roots` for more explanation. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).all_roots() [-1/2, 2, 2] >>> Poly(x**3 + x + 1).all_roots() [CRootOf(x**3 + x + 1, 0), CRootOf(x**3 + x + 1, 1), CRootOf(x**3 + x + 1, 2)] rLFrT)rNrOrPrV all_rootsrJ)r_rUrMrootss rdrZzPoly.all_roots<s>( ''//99!h9O L/ /rgc  |jrtd|z|jdkrgS|jjt ur'|j Dcgc] }t|}}n|jjturY|j Dcgc]}|j}}t|}|j Dcgc]}t||z}}nz|j Dcgc]"}|j|j$}}tj|5 |Dcgc]}tj|}} dddtj$j&}|tj$_ddlm  tj,|||d|jdz } t/t1t2t5| fd  } |tj$_| Scc}wcc}wcc}wcc}wcc}w#t $r#t#d|jjzwxYw#1swYxYw#t6$ru tj,|||d|jd z } t/t1t2t5| fd  } n#t6$rt7d|d|wxYwYwxYw#|tj$_wxYw)a Compute numerical approximations of roots of ``f``. Parameters ========== n ... the number of digits to calculate maxsteps ... the maximum number of iterations to do If the accuracy `n` cannot be reached in `maxsteps`, it will raise an exception. You need to rerun with higher maxsteps. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 3).nroots(n=15) [-1.73205080756888, 1.73205080756888] >>> Poly(x**2 - 3).nroots(n=30) [-1.73205080756887729352744634151, 1.73205080756887729352744634151] z$Cannot compute numerical roots of %sr)r*z!Numerical domain expected, got %sN)signF )maxstepscleanuperror extraprecc|jrdnd|jt|j|jfSNrrimagrealrcrur]s rdzPoly.nroots..s1!&&QaQVVVZ[\[a[aVb,crgkeyc|jrdnd|jt|j|jfSrdrerhs rdrizPoly.nroots..s4aff!QVVSQRQWQW[Z^_`_e_eZf0grgz$convergence to root failed; try n < z or maxsteps > )is_multivariater=rrjrr.r1r(r-rtrrrCmpmathworkdpsmpcrr8mpdps$sympy.functions.elementary.complexesr] polyrootsrwrr%sortedrP) r_r*r_r`rrdenomsfacrsr[r]s @rdnrootsz Poly.nrootsWs2  -6:< < 88:?I 5599?./lln=Uc%j=F= UUYY"_+,<<>:%egg:F:-C23,,.Ac%)nAFA"#1kkAk&3351F1" ''>DEUfjj%0EFE 'iimm = $$Vh#5AHHJrMKE Wu"cdfgE FIIM W>:A1F '%&IEEII'&''' ' '* " "(((#5AHHJrMKS5&ghjk  "#x!"" " " FIIMsH"HH ;'H9I ;H?HHAIH,II  I K AJ32K3KKKKKK.c|jrtd|zi}|jdD].\}}|js|j \}}||| |z <0|S)a Compute roots of ``f`` by factorization in the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**6 - 4*x**4 + 4*x**3 - x**2).ground_roots() {0: 2, 1: 2} z!Cannot compute ground roots of %sr)rnr=r is_linearr1)r_r[factorrRrrs rd ground_rootszPoly.ground_rootssx  -3a79 9+ IFA((*1qbd   rgcR|jr tdt|}|jr|dk\r t |}nt d|z|j }td}|j|jj||z|z ||}|j||S)af Construct a polynomial with n-th powers of roots of ``f``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = Poly(x**4 - x**2 + 1) >>> f.nth_power_roots_poly(2) Poly(x**4 - 2*x**3 + 3*x**2 - 2*x + 1, x, domain='ZZ') >>> f.nth_power_roots_poly(3) Poly(x**4 + 2*x**2 + 1, x, domain='ZZ') >>> f.nth_power_roots_poly(4) Poly(x**4 + 2*x**3 + 3*x**2 + 2*x + 1, x, domain='ZZ') >>> f.nth_power_roots_poly(12) Poly(x**4 - 4*x**3 + 6*x**2 - 4*x + 1, x, domain='ZZ') zmust be a univariate polynomialrz&'n' must an integer and n >= 1, got %sr) rnr=r% is_Integerr(rrr#rrrXr)r_r*rrrrus rdnth_power_roots_polyzPoly.nth_power_roots_polys,  -13 3 AJ <yyArgc|jr td|j}|js&|js|j st d|z|j||jS)a Find roots of a square-free polynomial ``f`` from ``candidates``. Explanation =========== If ``f`` is a square-free polynomial and ``candidates`` is a superset of the roots of ``f``, then ``f.which_real_roots(candidates)`` returns a list containing exactly the set of roots of ``f``. The domain must be :ref:`ZZ`, :ref:`QQ`, or :ref:`QQ(a)` and``f`` must be univariate and square-free. The list ``candidates`` must be a superset of the real roots of ``f`` and ``f.which_real_roots(candidates)`` returns the set of real roots of ``f``. The output preserves the order of the order of ``candidates``. Examples ======== >>> from sympy import Poly, sqrt >>> from sympy.abc import x >>> f = Poly(x**4 - 1) >>> f.which_real_roots([-1, 1, 0, -2, 2]) [-1, 1] >>> f.which_real_roots([-1, 1, 1, 1, 1]) [-1, 1] This method is useful as lifting to rational coefficients produced extraneous roots, which we can filter out with this method. >>> f = Poly(sqrt(2)*x**3 + x**2 - 1, x, extension=True) >>> f.lift() Poly(-2*x**6 + x**4 - 2*x**2 + 1, x, domain='QQ') >>> f.lift().real_roots() [-sqrt(2)/2, sqrt(2)/2] >>> f.which_real_roots(f.lift().real_roots()) [sqrt(2)/2] This procedure is already done internally when calling `.real_roots()` on a polynomial with algebraic coefficients. >>> f.real_roots() [sqrt(2)/2] See Also ======== same_root which_all_roots Must be a univariate polynomialz#root counting not supported over %s) rnr=ris_ZZis_QQis_AlgebraicFieldrq _which_rootsrJ)r_ candidatesrs rdwhich_real_rootszPoly.which_real_rootssnj  -13 3lln SYY#*?*?%5;= =~~j!--/::rgcp|jr td|j||jS)a] Find roots of a square-free polynomial ``f`` from ``candidates``. Explanation =========== If ``f`` is a square-free polynomial and ``candidates`` is a superset of the roots of ``f``, then ``f.which_all_roots(candidates)`` returns a list containing exactly the set of roots of ``f``. The polynomial``f`` must be univariate and square-free. The list ``candidates`` must be a superset of the complex roots of ``f`` and ``f.which_all_roots(candidates)`` returns exactly the set of all complex roots of ``f``. The output preserves the order of the order of ``candidates``. Examples ======== >>> from sympy import Poly, I >>> from sympy.abc import x >>> f = Poly(x**4 - 1) >>> f.which_all_roots([-1, 1, -I, I, 0]) [-1, 1, -I, I] >>> f.which_all_roots([-1, 1, -I, I, I, I]) [-1, 1, -I, I] This method is useful as lifting to rational coefficients produced extraneous roots, which we can filter out with this method. >>> f = Poly(x**2 + I*x - 1, x, extension=True) >>> f.lift() Poly(x**4 - x**2 + 1, x, domain='ZZ') >>> f.lift().all_roots() [CRootOf(x**4 - x**2 + 1, 0), CRootOf(x**4 - x**2 + 1, 1), CRootOf(x**4 - x**2 + 1, 2), CRootOf(x**4 - x**2 + 1, 3)] >>> f.which_all_roots(f.lift().all_roots()) [CRootOf(x**4 - x**2 + 1, 0), CRootOf(x**4 - x**2 + 1, 2)] This procedure is already done internally when calling `.all_roots()` on a polynomial with algebraic coefficients, or polynomials with Gaussian domains. >>> f.all_roots() [CRootOf(x**4 - x**2 + 1, 0), CRootOf(x**4 - x**2 + 1, 2)] See Also ======== same_root which_real_roots r)rnr=rr)r_rs rdwhich_all_rootszPoly.which_all_roots,s7r  -13 3~~j!((*55rgcZd}t|}t||kDrxt|jD]H}||j |d|z}t |j dk\s8|j|J|dz}t||kDrxt|jS)Nr^)maxn)r rrwkeysrrc_precpop)r_r num_rootsprec root_countsruf_rs rdrzPoly._which_rootsksj) +*+**,- 'djjAdFj3s8>>Q&OOA&  ' AID+*K$$&''rgc |jr td|jj}|jj j |}|dz }td|z di\}}}}t|}|dz|dzz fd} | || |} } | j| jz dz| j| jz dzz|kS)a Decide whether two roots of this polynomial are equal. Examples ======== >>> from sympy import Poly, cyclotomic_poly, exp, I, pi >>> f = Poly(cyclotomic_poly(5)) >>> r0 = exp(2*I*pi/5) >>> indices = [i for i, r in enumerate(f.all_roots()) if f.same_root(r, r0)] >>> print(indices) [3] Raises ====== DomainError If the domain of the polynomial is not :ref:`ZZ`, :ref:`QQ`, :ref:`RR`, or :ref:`CC`. MultivariatePolynomialError If the polynomial is not univariate. PolynomialError If the polynomial is of degree < 2. See Also ======== which_real_roots which_all_roots r rrc0tt|S)Nr)rr)rr)s rdriz Poly.same_root..s~&?Q&GHrg) rnr=rjmignotte_sep_bound_squaredrS get_fieldrrrrgrf) r_rr dom_delta_sqdelta_sqeps_sqrurr*evABr)s @rd same_rootzPoly.same_root{s>  -13 3uu779 88%%'00>A1V8Q+ 1a AJ !VA  H !ube1!#qvv&::VCCrgcf|j|\}}}}t|dr|j||}n t|d|sY|jr|j }|\}} } } |j |}|j | } || z || || fStt||S)a Cancel common factors in a rational function ``f/g``. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x)) (1, Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ')) >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x), include=True) (Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ')) cancel)include) rrrr7rrrr7r) r_r`rrrrrrbcpcqrrts rdrz Poly.cancels"!S!Q 1h XXaX1F'84 4!!lln!LBAqb!Bb!Bb5#a&#a&( (S&)* *rgcr|jr|jttfvr t d|j r%|jtk(r|tj fS|j}|j\}}|jt|j|j|fS)aQ Turn any univariate polynomial over :ref:`QQ` or :ref:`ZZ` into a monic polynomial over :ref:`ZZ`, by scaling the roots as necessary. Explanation =========== This operation can be performed whether or not *f* is irreducible; when it is, this can be understood as determining an algebraic integer generating the same field as a root of *f*. Examples ======== >>> from sympy import Poly, S >>> from sympy.abc import x >>> f = Poly(x**2/2 + S(1)/4 * x + S(1)/8, x, domain='QQ') >>> f.make_monic_over_integers_by_scaling_roots() (Poly(x**2 + 2*x + 4, x, domain='ZZ'), 4) Returns ======= Pair ``(g, c)`` g is the polynomial c is the integer by which the roots had to be scaled z,Polynomial must be univariate over ZZ or QQ.) rrSr.r-ris_monicrrrr rWrr)r_fmrrs rd)make_monic_over_integers_by_scaling_rootsz.Poly.make_monic_over_integers_by_scaling_rootss<!((2r(":KL L ::!((b.bff9 B??$DAq<<RVV a088:A= =rgcddlm}m}m}m}|j r$|j r|jttfvr td||||d}t|j} |j} | | kDrtd| d| dkr td| dk(rdd lm} | j d } } n>| d k(rdd lm}|j$d } } n$|j'\}}|| |||\} } |r| n| j)}|| fS)a Compute the Galois group of this polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = Poly(x**4 - 2) >>> G, _ = f.galois_group(by_name=True) >>> print(G) S4TransitiveSubgroups.D4 See Also ======== sympy.polys.numberfields.galoisgroups.galois_group r)_galois_group_degree_3_galois_group_degree_4_lookup(_galois_group_degree_5_lookup_ext_factor_galois_group_degree_6_lookupz>@DAq1a9 JID#D!4!4!6#v rgc.|jjS)a Returns ``True`` if ``f`` is a zero polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(0, x).is_zero True >>> Poly(1, x).is_zero False )rjis_zerors rdrz Poly.is_zero5s"uu}}rgc.|jjS)a Returns ``True`` if ``f`` is a unit polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(0, x).is_one False >>> Poly(1, x).is_one True )rjis_oners rdrz Poly.is_oneH"uu||rgc.|jjS)a  Returns ``True`` if ``f`` is a square-free polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 - 2*x + 1, x).is_sqf False >>> Poly(x**2 - 1, x).is_sqf True )rjr;rs rdr;z Poly.is_sqf[rrgc.|jjS)a  Returns ``True`` if the leading coefficient of ``f`` is one. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x + 2, x).is_monic True >>> Poly(2*x + 2, x).is_monic False )rjrrs rdrz Poly.is_monicns"uu~~rgc.|jjS)a; Returns ``True`` if GCD of the coefficients of ``f`` is one. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(2*x**2 + 6*x + 12, x).is_primitive False >>> Poly(x**2 + 3*x + 6, x).is_primitive True )rj is_primitivers rdrzPoly.is_primitive"uu!!!rgc.|jjS)aJ Returns ``True`` if ``f`` is an element of the ground domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x, x).is_ground False >>> Poly(2, x).is_ground True >>> Poly(y, x).is_ground True )rj is_groundrs rdrzPoly.is_grounds&uurgc.|jjS)a, Returns ``True`` if ``f`` is linear in all its variables. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x + y + 2, x, y).is_linear True >>> Poly(x*y + 2, x, y).is_linear False )rjr{rs rdr{zPoly.is_linears"uurgc.|jjS)a6 Returns ``True`` if ``f`` is quadratic in all its variables. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x*y + 2, x, y).is_quadratic True >>> Poly(x*y**2 + 2, x, y).is_quadratic False )rj is_quadraticrs rdrzPoly.is_quadraticrrgc.|jjS)a% Returns ``True`` if ``f`` is zero or has only one term. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(3*x**2, x).is_monomial True >>> Poly(3*x**2 + 1, x).is_monomial False )rj is_monomialrs rdrzPoly.is_monomials"uu   rgc.|jjS)aZ Returns ``True`` if ``f`` is a homogeneous polynomial. A homogeneous polynomial is a polynomial whose all monomials with non-zero coefficients have the same total degree. If you want not only to check if a polynomial is homogeneous but also compute its homogeneous order, then use :func:`Poly.homogeneous_order`. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + x*y, x, y).is_homogeneous True >>> Poly(x**3 + x*y, x, y).is_homogeneous False )rjis_homogeneousrs rdrzPoly.is_homogeneouss,uu###rgc.|jjS)aG Returns ``True`` if ``f`` has no factors over its domain. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> Poly(x**2 + x + 1, x, modulus=2).is_irreducible True >>> Poly(x**2 + 1, x, modulus=2).is_irreducible False )rjrrs rdrzPoly.is_irreducibles"uu###rgc2t|jdk(S)a Returns ``True`` if ``f`` is a univariate polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + x + 1, x).is_univariate True >>> Poly(x*y**2 + x*y + 1, x, y).is_univariate False >>> Poly(x*y**2 + x*y + 1, x).is_univariate True >>> Poly(x**2 + x + 1, x, y).is_univariate False rrrYrs rdrzPoly.is_univariate *166{argc2t|jdk7S)a Returns ``True`` if ``f`` is a multivariate polynomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x, y >>> Poly(x**2 + x + 1, x).is_multivariate False >>> Poly(x*y**2 + x*y + 1, x, y).is_multivariate True >>> Poly(x*y**2 + x*y + 1, x).is_multivariate False >>> Poly(x**2 + x + 1, x, y).is_multivariate True rrrs rdrnzPoly.is_multivariate$rrgc.|jjS)a Returns ``True`` if ``f`` is a cyclotomic polnomial. Examples ======== >>> from sympy import Poly >>> from sympy.abc import x >>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1 >>> Poly(f).is_cyclotomic False >>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1 >>> Poly(g).is_cyclotomic True )rj is_cyclotomicrs rdrzPoly.is_cyclotomic;s,uu"""rgc"|jSr)rcrs rd__abs__z Poly.__abs__S uuwrgc"|jSr)rers rd__neg__z Poly.__neg__Vrrgc$|j|Srrr_r`s rd__add__z Poly.__add__YuuQxrgc$|j|Srrrs rd__radd__z Poly.__radd__]rrgc$|j|Srrkrs rd__sub__z Poly.__sub__arrgc$|j|Srrrs rd__rsub__z Poly.__rsub__errgc$|j|Srrrs rd__mul__z Poly.__mul__irrgc$|j|Srrrs rd__rmul__z Poly.__rmul__mrrgr*cR|jr|dk\r|j|StS)Nr)rrpr[)r_r*s rd__pow__z Poly.__pow__qs" <_15588AEEZ^8C__rg)returnrWNNr)FF)FTr)rFNT)rz#tuple[Expr, list[tuple[Poly, int]]])FNNNFFNNFFr?rl2T)FF)r^ __module__ __qualname____doc__ __slots__is_commutativery _op_priority__annotations__r classmethodrpropertyrr~rrrrrXrurvrzr{rrrrrrrSrrrrrrrrrrrmrrrrrrrr r%r'rrrr1r3r5r8r:r r?r]rErGrIrLrQrUrWr[r]r_rarcrerrkrrnrprsrwryr{r~rrrr rrrrrrrrrrrrrrrrrrrrr_eval_derivativerrrrrrrrrrrrrrrrrrrrrr r rrrrrrrrr r#r9rJrRrWrZryr}rrrrrrrrrrr;rrrr{rrrrrrnrrrrfrrrrrrrr[rrrrrrrrr r rrrrr __classcell__rs@rdrWrWjs3j INGL H 0>  EE(('(( (( (( (( AA&AA*,(( AA"55<: !!,OONN83j+:0 )"J* *%&!MF2N4"LH D***40 J,)(Q(C "(J #=J 2&)$=L4* *#$J(%T ****04*0>>>04.%N8>%N#J#J(T@(5<:&;* &B> D&2#%J'*I#VI V*:...:**7*.(*%(*(4(:2.!F%>*N:3BN<36JRX#.J=~K:0006Ob6&P?;B=6~( 6Dp#+J%>N4l$$$$""$($""$!!$$$.$$$  ,  ,##.^$"%" ^$'%'^$'%'()"^$%, *`rgrWcbeZdZdZdZfdZedZede dZ dZ dZ xZ S) PurePolyz)Class for representing pure polynomials. c|jfS)z$Allow SymPy to hash Poly instances. )rjrs rdrzPurePoly._hashable_contents{rgc t|Srrrs rdrzPurePoly.__hash__rrgc|jS)aR Free symbols of a polynomial. Examples ======== >>> from sympy import PurePoly >>> from sympy.abc import x, y >>> PurePoly(x**2 + 1).free_symbols set() >>> PurePoly(x**2 + y).free_symbols set() >>> PurePoly(x**2 + y, x).free_symbols {y} )rrs rdrzPurePoly.free_symbolss&***rgr c||}}|js- |j||j|j}t|jt|jk7ry|jj|jjk7rg |jjj|jj|j}|j|}|j|}|j|jk(S#tt t f$rYywxYw#t$rYywxYwr ) ryrrYrr<r8r9rrjrrr:r)rr r_r`rs rdr zPurePoly.__eq__sU1yy KK166!,,.KA qvv;#aff+ % 5599 ! eeiiooaeeii8 S!A S!Auu~$[.A  %  s$,DAD1D.-D.1 D=<D=ct||jxr'|jj|jdSr)rVrrjrrs rdrzPurePoly._strict_eqs-!Q[[)JaeehhquuTh.JJrgctt|}|jsk |jj|j|j|jj |jjj |fSt|jt|jk7rtd|d|t|jtrt|jtstd|d||j|j}|jjj|jj|}|jj|}|jj|}||dffd }||||fS#t $rtd|d|wxYw)Nrrcp|!|d|||dzdz}|s|j|Sj|g|Srrrs rdrzPurePoly._unify..perrrg)r%ryrjrrrr9r:rrYrVr4rrr)r_r`rYrrrrr}s @rdrzPurePoly._unifysB AJyy Luuyy!%% !%%)):N:Nq:Q0RRR qvv;#aff+ %#A$FG G155#&:aeeS+A#A$FG Gkkvveeiiooaeeii. EEMM#  EEMM# 4 'CA~5" L'Q(JKK Ls A)FF7)r^r%r&r'rrr-rrr[r rrr/r0s@rdr2r2sJ3"++(().K rgr2cFtj||}t||Sr)rorp_poly_from_expr)rrYr~rs rdpoly_from_exprr<s#   d +C 4 %%rgc|t|}}t|ts t||||jrU|j j ||}|j|_|j|_|jd|_ ||fS|jr|j}t||\}}|js t|||ttt|j\}}|j}|t||\|_}ntt!|j"|}t%tt||}t&j)||}|jd|_ ||fS)rTrF)r%rVrr?ryrrzrYrSrOexpandrFrwrrr+rrrtrWru)rrorigrDrjrrrSs rdr;r;#sEwt}$D dE " dD11 ~~((s399[[ 99 CISy {{}tS)HC 88 dD11#tCIIK012NFF ZZF ~-f#> Fc&++V45 tC'( )C ??3 $D yy 9rgcFtj||}t||S)(Construct polynomials from expressions. )rorp_parallel_poly_from_expr)exprsrYr~rs rdparallel_poly_from_exprrDKs#   d +C #E3 //rgc jt|dk(r|\}}t|trt|tr|jj ||}|jj ||}|j |\}}|j |_|j|_|jd|_||g|fSt|g}}gg}}d}t|D]\}} t| } t| trL| jr|j|n0|j||jr| j} nd}|j| |rt!|||d|r|D]}||j#||<t%||\} }|j st!|||dddlm} |j D]} t| | st+dgg}} g}g}| D]i}tt-t|j/\}}| j1||j||jt|k|j}|t3| |\|_} ntt5|j6| } |D]} |j| d| | | d} g}t-||D]J\}}t9tt-||}tj;||}|j|L|jt=||_||fS) rArNTFr Piecewisez&Piecewise generators do not make senser)rrVrWrrzrrYrSrOrwrr%rryappendr>r?r]rG$sympy.functions.elementary.piecewiserGr<rrextendr+rrrtrubool)rCrr_r`origs_exprs_polysfailedrrrepsrGrR coeffs_listlengthsr3r1rjrrrSrOrDs rdrBrBRs 5zQ1 a :a#6 &&q#.A &&q#.A771:DAqvvCHCJyy   q63; ;5EFF FU#4t} dE "|| a  a ::;;=DF T  eUD99  *AQx'')E!H *)4ID# 88 eUD99> XXL a #!"JK KLrKJJ$c4 #4566"&!s6{# $ZZF ~"2;C"H K3v00+>? &+bq/*!!"o & Ej*54FF+,-sC( T  yyL #:rgc.t|}||vr|||<|S)z7Add a new ``(key, value)`` pair to arguments ``dict``. )rt)r~rkrBs rd _update_argsrTs :D $S Krgc  t|d}t|dj}|jr|}|jj}n.|j}|s |rt |\}}nt ||\}}|r"|rt j St jS|sT|jr*|jvrt |j\}}|jvrnt j S|jsRt|jdkDr:ttd|dtt|jd|dj|}t!|t"r t%|St jS)a Return the degree of ``f`` in the given variable. The degree of 0 is negative infinity. Examples ======== >>> from sympy import degree >>> from sympy.abc import x, y >>> degree(x**2 + y*x + 1, gen=x) 2 >>> degree(x**2 + y*x + 1, gen=y) 1 >>> degree(0, x) -oo See also ======== sympy.polys.polytools.Poly.total_degree degree_list Trrzj A symbolic generator of interest is required for a multivariate expression like func = z, e.g. degree(func, gen = z)) instead of degree(func, gen = z ). )r% is_Numberryr]r<r ZerorrYrrrrLnextr"rrVr(r)r_r gen_is_NumrisNumrrbs rdrrs86 $AT*44Jyy  %% %a(1%a-1 qvv2 2 22  99AFF*!!))+.DAq aff 66M YY3q~~.2 $wq~~./ $678 8 XXc]F(576?M1;M;MMrgct|}|jr|j}|jr d}t|S|jr|xs |j}t ||j }t|S)a Return the total_degree of ``f`` in the given variables. Examples ======== >>> from sympy import total_degree, Poly >>> from sympy.abc import x, y >>> total_degree(1) 0 >>> total_degree(x + x*y) 2 >>> total_degree(x + x*y, x) 1 If the expression is a Poly and no variables are given then the generators of the Poly will be used: >>> p = Poly(x + x*y, y) >>> total_degree(p) 1 To deal with the underlying expression of the Poly, convert it to an Expr: >>> total_degree(p.as_expr()) 2 This is done automatically if any variables are given: >>> total_degree(p, x) 1 See also ======== degree r)r%ryr]rVrYrWrr)r_rYrrvs rdrrslP  Ayy IIK{{  2; 99>166D !T] ' ' ) 2;rgctj|dg t|g|i|\}}|j }t tt|S#t$r}t dd|d}~wwxYw)z Return a list of degrees of ``f`` in all variables. Examples ======== >>> from sympy import degree_list >>> from sympy.abc import x, y >>> degree_list(x**2 + y*x + 1) (2, 1) rOrrN) ro allowed_flagsr<r?r@rr7rr)r_rYr~rrr|degreess rdrr*ss $ *71D1D13mmoG Wg& '' 7 q#667sA A/ A**A/ctj|dg t|g|i|\}}|j |j S#t$r}t dd|d}~wwxYw)z Return the leading coefficient of ``f``. Examples ======== >>> from sympy import LC >>> from sympy.abc import x, y >>> LC(4*x**2 + 2*x*y**2 + x*y + 3*y) 4 rOrrNr,)ror^r<r?r@rrlr_rYr~rrr|s rdrrEsk $ *.1D1D13 44cii4   .a--.sA A" AA"ctj|dg t|g|i|\}}|j |j }|jS#t$r}t dd|d}~wwxYw)z Return the leading monomial of ``f``. Examples ======== >>> from sympy import LM >>> from sympy.abc import x, y >>> LM(4*x**2 + 2*x*y**2 + x*y + 3*y) x**2 rOrrNr,)ror^r<r?r@rrlr])r_rYr~rrr|rs rdrr^sv $ *.1D1D13 DDsyyD !E ==? .a--.sA A2 A--A2ctj|dg t|g|i|\}}|j |j \}}||jzS#t$r}t dd|d}~wwxYw)z Return the leading term of ``f``. Examples ======== >>> from sympy import LT >>> from sympy.abc import x, y >>> LT(4*x**2 + 2*x*y**2 + x*y + 3*y) 4*x**2 rOrrNr,)ror^r<r?r@rrlr])r_rYr~rrr|rrs rdrrxs $ *.1D1D1344cii4(LE5   .a--.sA A8& A33A8c$tj|dg t||fg|i|\\}}}|j |\}} |j s |j| jfS|| fS#t$r}t dd|d}~wwxYw)z Compute polynomial pseudo-division of ``f`` and ``g``. Examples ======== >>> from sympy import pdiv >>> from sympy.abc import x >>> pdiv(x**2 + 1, 2*x - 4) (2*x + 4, 20) rOrsrN)ror^rDr?r@rsrOr] r_r`rYr~rrrr|rtrus rdrsrss $ *0-q!fDtDtD A 66!9DAq 99yy{AIIK''!t  03//0sA44 B= B  Bctj|dg t||fg|i|\\}}}|j |}|j s|jS|S#t$r}t dd|d}~wwxYw)z Compute polynomial pseudo-remainder of ``f`` and ``g``. Examples ======== >>> from sympy import prem >>> from sympy.abc import x >>> prem(x**2 + 1, 2*x - 4) 20 rOrwrN)ror^rDr?r@rwrOr] r_r`rYr~rrrr|rus rdrwrws $ *0-q!fDtDtD A q A 99yy{ 03//0A A:( A55A:c.tj|dg t||fg|i|\\}}} |j |}|js|jS|S#t$r}t dd|d}~wwxYw#t $r t ||wxYw)z Compute polynomial pseudo-quotient of ``f`` and ``g``. Examples ======== >>> from sympy import pquo >>> from sympy.abc import x >>> pquo(x**2 + 1, 2*x - 4) 2*x + 4 >>> pquo(x**2 - 1, 2*x - 1) 2*x + 1 rOryrN) ror^rDr?r@ryr>rOr] r_r`rYr~rrrr|rts rdryrys" $ *0-q!fDtDtD A( FF1I 99yy{ 03//0 (!!Q''(s"A A> A;) A66A;>Bctj|dg t||fg|i|\\}}}|j |}|j s|jS|S#t$r}t dd|d}~wwxYw)a_ Compute polynomial exact pseudo-quotient of ``f`` and ``g``. Examples ======== >>> from sympy import pexquo >>> from sympy.abc import x >>> pexquo(x**2 - 1, 2*x - 2) 2*x + 2 >>> pexquo(x**2 + 1, 2*x - 4) Traceback (most recent call last): ... ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1 rOr{rN)ror^rDr?r@r{rOr]rjs rdr{r{s( $ *2-q!fDtDtD A  A 99yy{ 2!S112rhc>tj|ddg t||fg|i|\\}}}|j ||j \}} |js |j| jfS|| fS#t$r}t dd|d}~wwxYw)a Compute polynomial division of ``f`` and ``g``. Examples ======== >>> from sympy import div, ZZ, QQ >>> from sympy.abc import x >>> div(x**2 + 1, 2*x - 4, domain=ZZ) (0, x**2 + 1) >>> div(x**2 + 1, 2*x - 4, domain=QQ) (x/2 + 1, 5) rrOr~rNr) ror^rDr?r@r~rrOr]res rdr~r~s" $ 12/-q!fDtDtD A 555 "DAq 99yy{AIIK''!t  /q#../sB B BBctj|ddg t||fg|i|\\}}}|j ||j }|js|jS|S#t$r}t dd|d}~wwxYw)a Compute polynomial remainder of ``f`` and ``g``. Examples ======== >>> from sympy import rem, ZZ, QQ >>> from sympy.abc import x >>> rem(x**2 + 1, 2*x - 4, domain=ZZ) x**2 + 1 >>> rem(x**2 + 1, 2*x - 4, domain=QQ) 5 rrOrrNrm) ror^rDr?r@rrrOr]rgs rdrr4" $ 12/-q!fDtDtD A achhA 99yy{ /q#../A,, B5 BBctj|ddg t||fg|i|\\}}}|j ||j }|js|jS|S#t$r}t dd|d}~wwxYw)z Compute polynomial quotient of ``f`` and ``g``. Examples ======== >>> from sympy import quo >>> from sympy.abc import x >>> quo(x**2 + 1, 2*x - 4) x/2 + 1 >>> quo(x**2 - 1, x - 1) x + 1 rrOrrNrm) ror^rDr?r@rrrOr]rjs rdrrTrorpctj|ddg t||fg|i|\\}}}|j ||j }|js|jS|S#t$r}t dd|d}~wwxYw)aQ Compute polynomial exact quotient of ``f`` and ``g``. Examples ======== >>> from sympy import exquo >>> from sympy.abc import x >>> exquo(x**2 - 1, x - 1) x + 1 >>> exquo(x**2 + 1, 2*x - 4) Traceback (most recent call last): ... ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1 rrOrrNrm) ror^rDr?r@rrrOr]rjs rdrrts( $ 121-q!fDtDtD A !A 99yy{ 1C001rpctj|ddg t||fg|i|\\}}}|j ||j\} } |js | j| jfS| | fS#t$rw}t |j \}\} } |j | | \} } |j| |j| fcYd}~S#t$rtdd|wxYwd}~wwxYw)aT Half extended Euclidean algorithm of ``f`` and ``g``. Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``. Examples ======== >>> from sympy import half_gcdex >>> from sympy.abc import x >>> half_gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4) (3/5 - x/5, x + 1) rrONrrrm) ror^rDr?r+rCrrrqr@rrOr]) r_r`rYr~rrrr|rSrrrrs rdrrs" $ 12 :-q!fDtDtD A <<< )DAq 99yy{AIIK''!t  :)#))4A :$$Q*DAq??1%vq'99 9# :#L!S9 9 : :s5B D C<&C";!C<D"C99C<<DcLtj|ddg t||fg|i|\\}}}|j ||j\} } } |js/| j| j| jfS| | | fS#t$r}t |j \}\} } |j | | \} } } |j| |j| |j| fcYd}~S#t$rtdd|wxYwd}~wwxYw)aZ Extended Euclidean algorithm of ``f`` and ``g``. Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``. Examples ======== >>> from sympy import gcdex >>> from sympy.abc import x >>> gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4) (3/5 - x/5, x**2/5 - 6*x/5 + 2, x + 1) rrONrrrm) ror^rDr?r+rCrrrqr@rrOr])r_r`rYr~rrrr|rSrrrrrs rdrrs" $ 12 N-q!fDtDtD Aggachhg'GAq! 99yy{AIIK44!Qw N)#))4A Nll1a(GAq!??1%vq'96??1;MM M# 5#GQ4 4 5 Ns5B D#D7D 1D>D#DDD#ctj|ddg t||fg|i|\\}}}|j||j} |js| jS| S#t$ra}t |j \}\} } |j |j| | cYd}~S#t$rtdd|wxYwd}~wwxYw)a/ Invert ``f`` modulo ``g`` when possible. Examples ======== >>> from sympy import invert, S, mod_inverse >>> from sympy.abc import x >>> invert(x**2 - 1, 2*x - 1) -4/3 >>> invert(x**2 - 1, x - 1) Traceback (most recent call last): ... NotInvertible: zero divisor For more efficient inversion of Rationals, use the :obj:`sympy.core.intfunc.mod_inverse` function: >>> mod_inverse(3, 5) 2 >>> (S(2)/5).invert(S(7)/3) 5/2 See Also ======== sympy.core.intfunc.mod_inverse rrONrrrm) ror^rDr?r+rCrrrqr@rrOr]) r_r`rYr~rrrr|rSrrrs rdrrs@ $ 126-q!fDtDtD A "A 99yy{ 6)#))4A 6??6==A#67 7" 6#Ha5 5 6 6s/A,, C5C B71C7CCCc tj|dg t||fg|i|\\}}}|j |}|j s|D cgc]} | jc} S|S#t$r}t dd|d}~wwxYwcc} w)z Compute subresultant PRS of ``f`` and ``g``. Examples ======== >>> from sympy import subresultants >>> from sympy.abc import x >>> subresultants(x**2 + 1, x**2 - 1) [x**2 + 1, x**2 - 1, -2] rOrrN)ror^rDr?r@rrOr]) r_r`rYr~rrrr|rbrus rdrrs $ *9-q!fDtDtD A__Q F 99%+, ,,  9C889 -sA-B - B6 BBFrctj|dg t||fg|i|\\}}}|r|j ||\} } n|j |} |j s@|r.| j D cgc]} | jc} fS| jS|r|  fS| S#t$r}t dd|d}~wwxYwcc} w)z Compute resultant of ``f`` and ``g``. Examples ======== >>> from sympy import resultant >>> from sympy.abc import x >>> resultant(x**2 + 1, x**2 - 1) 4 rOrrNr)ror^rDr?r@rrOr]) r_r`rrYr~rrrr|rbrrus rdrr7s $ *5-q!fDtDtD AKKjK9 Q 99 >>#1%=aaiik%== =~~ 19   5 Q445&>sB.;C . C 7 CC ctj|dg t|g|i|\}}|j }|j s|jS|S#t$r}t dd|d}~wwxYw)z Compute discriminant of ``f``. Examples ======== >>> from sympy import discriminant >>> from sympy.abc import x >>> discriminant(x**2 + 2*x + 3) -8 rOrrN)ror^r<r?r@rrOr]r_rYr~rrr|rbs rdrr\s{ $ *81D1D13^^ F 99~~  83778A A4" A//A4c2tj|dg t||fg|i|\\}}}|j |\} } } |js/| j| j| jfS| | | fS#t$r}t |j \}\} } |j | | \} } } |j| |j| |j| fcYd}~S#t$rtdd|wxYwd}~wwxYw)a Compute GCD and cofactors of ``f`` and ``g``. Returns polynomials ``(h, cff, cfg)`` such that ``h = gcd(f, g)``, and ``cff = quo(f, h)`` and ``cfg = quo(g, h)`` are, so called, cofactors of ``f`` and ``g``. Examples ======== >>> from sympy import cofactors >>> from sympy.abc import x >>> cofactors(x**2 - 1, x**2 - 3*x + 2) (x - 1, x + 1, x - 2) rONrr) ror^rDr?r+rCrrrqr@rOr])r_r`rYr~rrrr|rSrrrrrs rdrrzs& $ * R-q!fDtDtD A++a.KAsC 99yy{CKKM3;;=88#s{ R)#))4A R **1a0KAsC??1%vs';V__S=QQ Q# 9#KC8 8 9 Rs5B DD*C71D1D7DDDc0t|}fd}||}||Stjdg t|gi\}}t |dkDrt d|Drn|d}|ddDcgc]}||z j } }t d| Dr4d} | D]} t| | jd} !t|| z S|s)|jstjSt!d| S|d|dd}}|D]!} |j#| }|j$s!n|js|j'S|Scc}w#t$r6} || j}||cYd} ~ Std t || d} ~ wwxYw) z Compute GCD of a list of polynomials. Examples ======== >>> from sympy import gcd_list >>> from sympy.abc import x >>> gcd_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2]) x - 1 csqsot|\}}|s |jS|jrG|d|dd}}|D]'}|j||}|j |s'n|j |SyNrr)r+rrOrrrseqrSnumbersrbnumberr~rYs rdtry_non_polynomial_gcdz(gcd_list..try_non_polynomial_gcdsD.s3OFG{{"$$")!*gabk%F#ZZ7F}}V,  v..rgNrOrc3PK|]}|jxr |j ywr is_algebraic is_irrational.0rs rd zgcd_list..$V3 0 0 FS5F5F FV$&rc34K|]}|jywr is_rationalrfrcs rdrzgcd_list..2s3??2rgcd_listr)r%ror^rDrrratsimpras_numer_denomrcr?rCr@rOr rWrWrrr])rrYr~rrbrOrrrlstlcrr|rDs `` rdrrs #,C&$C (F   $ *?,S@4@4@ s s8a > ?s6>E6EAEE FF4F:FFct|dr||f|z}t|g|i|S| tdtj|dg t ||fg|i|\\}}}t t||f\}}|jrb|jrV|jrJ|jr>||z j} | jrt|| jdz S|j%|} |j*s| j-S| S#t$ra} t| j \} \}} | j#| j%||cYd} ~ S#t&$rt)dd| wxYwd} ~ wwxYw)z Compute GCD of ``f`` and ``g``. Examples ======== >>> from sympy import gcd >>> from sympy.abc import x >>> gcd(x**2 - 1, x**2 - 3*x + 2) x - 1 __iter__Nz2gcd() takes 2 arguments or a sequence of argumentsrOrrr)rrrror^rDrr%rrrrrcrr?r+rCrrrqr@rOr] r_r`rYr~rrrrrrr|rSrbs rdrrsjq* =4$;D)D)D)) LMM $ *3-q!fDtDtD A7QF#1 >>aoo!..Q__Q3--/C1S//1!4455UU1XF 99~~  3)#))4A 3??6::a#34 4" 3#E1c2 2 3 3s1 BD E<E77 EE<E44E77E<ct|}d fd }||}||Stjdg t|gi\}}t |dkDrwt d|Dre|d}|ddDcgc]}||z j } }t d| Dr+d} | D]} t| | jd} !|| zS|s)|jstjStd| S|d |dd}}|D]} |j| }|js|j!S|Scc}w#t$r6} || j}||cYd} ~ Stdt || d} ~ wwxYw) z Compute LCM of a list of polynomials. Examples ======== >>> from sympy import lcm_list >>> from sympy.abc import x >>> lcm_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2]) x**5 - x**4 - 2*x**3 - x**2 + x + 2 csmskt|\}}|s|j|jS|jr4|d|dd}}|D]}|j ||}|j|Syr~)r+rrrOrrs rdtry_non_polynomial_lcmz(lcm_list..try_non_polynomial_lcm=s{D.s3OFGvzz22$$")!*gabk%8F#ZZ7F8v..rgNrOrc3PK|]}|jxr |j ywrrrs rdrzlcm_list..Xrrrc34K|]}|jywrrrs rdrzlcm_list..[rrlcm_listrr)rzOptional[Expr])r%ror^rDrrrrrr?rCr@rOr rrWr])rrYr~rrbrOrrrrrrr|rDs `` rdrr,s #,C $C (F   $ *?,S@4@4@ s s8a > ?s5>E7D;>E;E E? E:E?$E::E?ct|dr||f|z}t|g|i|S| tdtj|dg t ||fg|i|\\}}}t t||f\}}|jrY|jrM|jrA|jr5||z j} | jr|| jdzS|j#|} |j(s| j+S| S#t$ra} t| j\} \}} | j!| j#||cYd} ~ S#t$$rt'dd| wxYwd} ~ wwxYw)z Compute LCM of ``f`` and ``g``. Examples ======== >>> from sympy import lcm >>> from sympy.abc import x >>> lcm(x**2 - 1, x**2 - 3*x + 2) x**3 - 2*x**2 - x + 2 rNz2lcm() takes 2 arguments or a sequence of argumentsrOrrr)rrrror^rDrr%rrrrrr?r+rCrrrqr@rOr]rs rdrrzseq* =4$;D)D)D)) LMM $ *3-q!fDtDtD A7QF#1 >>aoo!..Q__Q3--/C++-a000UU1XF 99~~  3)#))4A 3??6::a#34 4" 3#E1c2 2 3 3s1 BD E3E.. EE3E++E..E3c 6t|}t|tr(tfd|j|jfDSt|t rt d|t|tr |jr|SjddrY|j|jDcgc]}t|gic}}jddd<t|giSjdd}tjdg t!|gi\}}|j\} }|j&j(rZ|j&j*r|j-d \} }|j/\} }|j&j*r|  z} nt0j2} t5t7|j8| D cgc] \} }| |z c}} }t;| d rt0j2} |d k(r|S|rt=| ||j?zSt=| |j?d jA\} }t=| ||zd Scc}w#t"$r} | j$cYd } ~ Sd } ~ wwxYwcc}} w) az Remove GCD of terms from ``f``. If the ``deep`` flag is True, then the arguments of ``f`` will have terms_gcd applied to them. If a fraction is factored out of ``f`` and ``f`` is an Add, then an unevaluated Mul will be returned so that automatic simplification does not redistribute it. The hint ``clear``, when set to False, can be used to prevent such factoring when all coefficients are not fractions. Examples ======== >>> from sympy import terms_gcd, cos >>> from sympy.abc import x, y >>> terms_gcd(x**6*y**2 + x**3*y, x, y) x**3*y*(x**3*y + 1) The default action of polys routines is to expand the expression given to them. terms_gcd follows this behavior: >>> terms_gcd((3+3*x)*(x+x*y)) 3*x*(x*y + x + y + 1) If this is not desired then the hint ``expand`` can be set to False. In this case the expression will be treated as though it were comprised of one or more terms: >>> terms_gcd((3+3*x)*(x+x*y), expand=False) (3*x + 3)*(x*y + x) In order to traverse factors of a Mul or the arguments of other functions, the ``deep`` hint can be used: >>> terms_gcd((3 + 3*x)*(x + x*y), expand=False, deep=True) 3*x*(x + 1)*(y + 1) >>> terms_gcd(cos(x + x*y), deep=True) cos(x*(y + 1)) Rationals are factored out by default: >>> terms_gcd(x + y/2) (2*x + y)/2 Only the y-term had a coefficient that was a fraction; if one does not want to factor out the 1/2 in cases like this, the flag ``clear`` can be set to False: >>> terms_gcd(x + y/2, clear=False) x + y/2 >>> terms_gcd(x*y/2 + y**2, clear=False) y*(x/2 + y) The ``clear`` flag is ignored if all coefficients are fractions: >>> terms_gcd(x/3 + y/2, clear=False) (2*x + 3*y)/6 See Also ======== sympy.core.exprtools.gcd_terms, sympy.core.exprtools.factor_terms c3<K|]}t|giywr)rU)rrr~rYs rdrzterms_gcd..s N!)A555Nsz3Inequalities cannot be used with terms_gcd. Found: deepFr>clearTrONrr)r)!r%rVr!lhsrhsr rr is_Atomrrcr~rUrror^r<r?rrSrrrrr rrrrYrrr] as_coeff_Mul)r_rYr~r?rrrrrr|rdenomrrrterms `` rdrUrUs3F 1:D!XNquu~NOO Az "RSUVV a !))  xxaffAFFCqy2T2T2CD X,t,t,, HHWd #E $ *1D1D13 ;;=DAq zz ::  ~~d~3HE1;;=q ::   UNE #affa.1$!QA1 2DE1 19K 5$qyy{"2335!))+U;HHJHE1 ud1fE 22KD xx 2s*.I.I3J 3 J< J J Jctj|ddg t|g|i|\}}|j t |}|js|jS|S#t$r}t dd|d}~wwxYw)z Reduce ``f`` modulo a constant ``p``. Examples ======== >>> from sympy import trunc >>> from sympy.abc import x >>> trunc(2*x**3 + 3*x**2 + 5*x + 7, 3) -x**3 - x + 1 rrOrrN) ror^r<r?r@rr%rOr])r_rrYr~rrr|rbs rdrr#s $ 1211D1D13WWWQZ F 99~~  1C001sA$$ A?- A::A?ctj|ddg t|g|i|\}}|j |j }|js|jS|S#t$r}t dd|d}~wwxYw)z Divide all coefficients of ``f`` by ``LC(f)``. Examples ======== >>> from sympy import monic >>> from sympy.abc import x >>> monic(3*x**2 + 4*x + 2) x**2 + 4*x/3 + 2/3 rrOrrNrm) ror^r<r?r@rrrOr]rys rdrrAs $ 1211D1D13WW#((W #F 99~~  1C001sA&& B/ A<<Bctj|dg t|g|i|\}}|j S#t$r}t dd|d}~wwxYw)z Compute GCD of coefficients of ``f``. Examples ======== >>> from sympy import content >>> from sympy.abc import x >>> content(6*x**2 + 8*x + 12) 2 rOrrN)ror^r<r?r@rras rdrr_sb $ *31D1D13 99; 3 1c223s; A AActj|dg t|g|i|\}}|j \}}|j s||jfS||fS#t$r}t dd|d}~wwxYw)a Compute content and the primitive form of ``f``. Examples ======== >>> from sympy.polys.polytools import primitive >>> from sympy.abc import x >>> primitive(6*x**2 + 8*x + 12) (2, 3*x**2 + 4*x + 6) >>> eq = (2 + 2*x)*x + 2 Expansion is performed by default: >>> primitive(eq) (2, x**2 + x + 1) Set ``expand`` to False to shut this off. Note that the extraction will not be recursive; use the as_content_primitive method for recursive, non-destructive Rational extraction. >>> primitive(eq, expand=False) (1, x*(2*x + 2) + 2) >>> eq.as_content_primitive() (2, x*(x + 1) + 1) rOrrN)ror^r<r?r@rrOr])r_rYr~rrr|rrbs rdrrxs@ $ *51D1D13;;=LD& 99V^^%%%V| 5 Q445sA A;) A66A;ctj|dg t||fg|i|\\}}}|j |}|j s|jS|S#t$r}t dd|d}~wwxYw)z Compute functional composition ``f(g)``. Examples ======== >>> from sympy import compose >>> from sympy.abc import x >>> compose(x**2 + x, x - 1) x**2 - x rOrrN)ror^rDr?r@rrOr]) r_r`rYr~rrrr|rbs rdrrs $ *3-q!fDtDtD AYYq\F 99~~  3 1c223rhctj|dg t|g|i|\}}|j }|j s|Dcgc]}|jc}S|S#t$r}t dd|d}~wwxYwcc}w)z Compute functional decomposition of ``f``. Examples ======== >>> from sympy import decompose >>> from sympy.abc import x >>> decompose(x**4 + 2*x**3 - x - 1) [x**2 - x - 1, x**2 + x] rOrrN)ror^r<r?r@rrOr]r_rYr~rrr|rbrus rdrrs $ *51D1D13[[]F 99%+, ,,  5 Q445 -sA' B' B0 A==Bc.tj|ddg t|g|i|\}}|j |j }|js|Dcgc]}|jc}S|S#t$r}t dd|d}~wwxYwcc}w)z Compute Sturm sequence of ``f``. Examples ======== >>> from sympy import sturm >>> from sympy.abc import x >>> sturm(x**3 - 2*x**2 + x - 3) [x**3 - 2*x**2 + x - 3, 3*x**2 - 4*x + 1, 2*x/9 + 25/9, -2079/4] rrOrrNrm) ror^r<r?r@rrrOr]rs rdrrs $ 1211D1D13WW#((W #F 99%+, ,,  1C001 -sA4B4 B= B  Bc$tj|dg t|g|i|\}}|j }|j s%|Dcgc]\}}|j|fc}}S|S#t$r}t dd|d}~wwxYwcc}}w)a& Compute a list of greatest factorial factors of ``f``. Note that the input to ff() and rf() should be Poly instances to use the definitions here. Examples ======== >>> from sympy import gff_list, ff, Poly >>> from sympy.abc import x >>> f = Poly(x**5 + 2*x**4 - x**3 - 2*x**2, x) >>> gff_list(f) [(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)] >>> (ff(Poly(x), 1)*ff(Poly(x + 2), 4)) == f True >>> f = Poly(x**12 + 6*x**11 - 11*x**10 - 56*x**9 + 220*x**8 + 208*x**7 - 1401*x**6 + 1090*x**5 + 2715*x**4 - 6720*x**3 - 1092*x**2 + 5040*x, x) >>> gff_list(f) [(Poly(x**3 + 7, x, domain='ZZ'), 2), (Poly(x**2 + 5*x, x, domain='ZZ'), 3)] >>> ff(Poly(x**3 + 7, x), 2)*ff(Poly(x**2 + 5*x, x), 3) == f True rOrrN)ror^r<r?r@rrOr]) r_rYr~rrr|rr`rRs rdrrs@ $ *41D1D13jjlG 99-45TQa 55 4 As334 6sA. B . B 7 BB ctd)z3Compute greatest factorial factorization of ``f``. zsymbolic falling factorialrr_rYr~s rdgffr/s : ;;rgcXtj|dg t|g|i|\}}|j \}}}|D cgc] } t | } } |js!| |j|jfS| ||fS#t$r}t dd|d}~wwxYwcc} w)a Compute square-free norm of ``f``. Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and ``r(x) = Norm(g(x))`` is a square-free polynomial over ``K``, where ``a`` is the algebraic extension of the ground domain. Examples ======== >>> from sympy import sqf_norm, sqrt >>> from sympy.abc import x >>> sqf_norm(x**2 + 1, extension=[sqrt(3)]) ([1], x**2 - 2*sqrt(3)*x + 4, x**4 - 4*x**2 + 16) rOrrN) ror^r<r?r@rrrOr]) r_rYr~rrr|rr`rusis_exprs rdrr5s& $ *41D1D13jjlGAq!$% &bgbk &F & 99qyy{AIIK//q!| 4 As334 'sB B' B$ BB$ctj|dg t|g|i|\}}|j }|j s|jS|S#t$r}t dd|d}~wwxYw)z Compute square-free part of ``f``. Examples ======== >>> from sympy import sqf_part >>> from sympy.abc import x >>> sqf_part(x**3 - 3*x - 2) x**2 - x - 2 rOrrN)ror^r<r?r@rrOr]rys rdrrYsz $ *41D1D13ZZ\F 99~~  4 As334rzc|jj}t|t|jt |j |fS)zSort a list of polys.rjr=rrYrsrS)rDrjs rd_poly_sort_keyrws8 ((   C Hc$))nc$++&6 <.keysCID#((""$CS3tyy>3t{{3CSI Irgc|\}}|jj}t|t|j|t |j |fSrrrs rdrkz_sorted_factors..keysCID#((""$CHc$))nc3t{{3CSI Irgrj)rv)rmethodrks rd_sorted_factorsr}s$  J  J 's ##rgcft|Dcgc]\}}|j|zc}}Scc}}w)z*Multiply a list of ``(expr, exp)`` pairs. )rr])rr_rRs rd_factors_productrs) G4DAqa4 554s- c jtjg}}tj|Dcgc] }t |dr|j n|"}}|D]}|j st|trt|r||z}1|jrj|jtjk7rM|j\}} |j r| j r||z}|j r&|j|| f|tj} } t||\} } t!| |dz} | \} }| tjurV| j"r || | zz}nA| j$r|j| | fn!|j| tjf| tjur|j'|o| j(r+|j'|Dcgc] \}}||| zfc}}g}|D]I\}|j+j$r|j|| zf7|j|fK|jt-|| f|dk(r<|D chc]\} }| c}} Dcgc]t3t4fd|Df}}t7t8}|D]\}|xx|z cc<|t;|j=fScc}wcc}}w#t.$r(}|j|j0| fYd}~d}~wwxYwcc}} wcc}w)z.Helper function for :func:`_symbolic_factor`. _eval_factor_listNr)c34K|]\}}|k(s |ywrr)rr_rrRs rdrz(_symbolic_factor_list..s Atq!!q& As )r rr make_argsrrrVrVr ris_PowbaseExp1r~rHr;r\r is_positiverJ is_integerr]rr?rrrr r(rwr)rrrrrrr~argrrrDrrc_coeff_factorsr_rRr r|r\r2s ` rd_symbolic_factor_listrsUUB7Et$ & !(> :ANN  A &D &,? ==ZT2|C7H SLE  ZZCHH.ID#~~#-- ~~c{+QUU#D ?%dC0GD!4'!12D#v FHQUU">>VS[(E''NNFC=1OOVQUUO4aee|x(x@tq!AcE @A$-DAqyy{..1S5z2 aV, -  0 7=>Y,?Z+2341aQ353 Aw ABAF55 S B1 1  $rxxz" ""m &H A#" , NNCHHc? + + ,<45s/%K+K6K0 3 L*"L06 L'?L""L'c t|trOt|dr|jSt t ||d||\}}t |t|St|dr2|j|jDcgc]}t|||c}St|dr*|j|Dcgc]}t|||c}S|Scc}wcc}w)z%Helper function for :func:`_factor`. rfraction)rr~r) rVr rrrrHrrrcr~_symbolic_factorr)rrrrrrs rdrrs$ 4 '$$& &.xs:/WY\^dew5"27";<< v tyyS#+Cf=STT z "~~TRc/S&ARSS TRs C9Ccftj|ddgtj||}t|}t |t t frHt |t r|d}}nt|j\}}t|||\}}t|||\} } | r|jstd|z|jddi} || fD];} t| D]+\} \}}|jrt|| \}}||f| | <-=t!||}t!| |} |j"sH|Dcgc]\}}|j%|f}}}| Dcgc]\}}|j%|f} }}|| z }|js||fS||| fStd|zcc}}wcc}}w)z>Helper function for :func:`sqf_list` and :func:`factor_list`. fracrOrza polynomial expected, got %sr>T)ror^rpr%rVr rWrHrrrr<clonerryr;rrOr])rrYr~rrnumerrrfprfq_optrrr_rRrrs rd_generic_factor_listrs $ 12   d +C 4=D$t % dD !5E#D>88:LE5&uc6:B&uc6:B chh!"AD"HI Iyy(D)*Bx (G&w/ ( 6Aqyy*1d3DAq"#QGAJ ( ( R ( R (yy/12tq!199;"2B2/12tq!199;"2B22xx"9 "b= =DEE32s <F' F-c|jdd}tj|gtj||}||d<t t |||S)z4Helper function for :func:`sqf` and :func:`factor`. rT)rror^rprr%)rrYr~rrrs rd_generic_factorr sPxx D)H $#   d +CC O GDM3 77rgcddlmd fd }d fd }d}|jjrI||rA|j }|||}|r|d|dd|dfS|||}|r dd|d|dfSy) a! try to transform a polynomial to have rational coefficients try to find a transformation ``x = alpha*y`` ``f(x) = lc*alpha**n * g(y)`` where ``g`` is a polynomial with rational coefficients, ``lc`` the leading coefficient. If this fails, try ``x = y + beta`` ``f(x) = g(y)`` Returns ``None`` if ``g`` not found; ``(lc, alpha, None, g)`` in case of rescaling ``(None, None, beta, g)`` in case of translation Notes ===== Currently it transforms only polynomials without roots larger than 2. Examples ======== >>> from sympy import sqrt, Poly, simplify >>> from sympy.polys.polytools import to_rational_coeffs >>> from sympy.abc import x >>> p = Poly(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}), x, domain='EX') >>> lc, r, _, g = to_rational_coeffs(p) >>> lc, r (7 + 5*sqrt(2), 2 - 2*sqrt(2)) >>> g Poly(x**3 + x**2 - 1/4*x - 1/4, x, domain='QQ') >>> r1 = simplify(1/r) >>> Poly(lc*r**3*(g.as_expr()).subs({x:x*r1}), x, domain='EX') == p True rsimplifyNct|jdk(r|jdjsd|fS|j}|j }|xs|j }|j dd}|Dcgc] } | }}t|dkDr|dr |d|dz }g}tt|D]5} ||||dzzz}|jsy|j|7 d|z } |jd} | |zg} td|dzD]"}| j||dz | ||z zz$t| }t|}|| |fSycc}w)a$ try rescaling ``x -> alpha*x`` to convert f to a polynomial with rational coefficients. Returns ``alpha, f``; if the rescaling is successful, ``alpha`` is the rescaling factor, and ``f`` is the rescaled polynomial; else ``alpha`` is ``None``. rrNr) rrYrrrrr1rrrHr rW) r_f1r*rrcoeffx rescale1_xcoeffs1r rescale_xrr2rs rd _try_rescalez(to_rational_coeffs.._try_rescale;s166{aq ':':7N HHJ TTV  288:$178v(6"88 v;?vbz!&*VBZ"78JG3v;' (!&)JQ,?"?@))v& ( %Qz\2 FF1ITFq!a%8AHHWQU^AAJ678GG9a''%9sE2ct|jdk(r|jdjsd|fS|j}|xs|j }|j dd} |d}|j rP|jsDt|jdd\}}|j| |z }|j|}||fSy)a+ try translating ``x -> x + alpha`` to convert f to a polynomial with rational coefficients. Returns ``alpha, f``; if the translating is successful, ``alpha`` is the translating factor, and ``f`` is the shifted polynomial; else ``alpha`` is ``None``. rrNc|jduSr r)zs rdriz._try_translate..ms!--4/rgTbinary) rrYrrrr1is_AddrrOr~rcr) r_rr*rrratnonratalphaf2rs rd_try_translatez*to_rational_coeffs.._try_translate]s166{aq ':':7N HHJ  288:$ VAY  88AMMqvv/>KCQVVV_$Q&E%B"9 rgc|j}d}|D]}tj|D]}t|j}|j Dcgc]8\}}|j r'|jr|jdk\r |j:}}}|snt|dk(rd}t|dkDsy|Scc}}w)zS Return True if ``f`` is a sum with square roots but no other root FrT) rr rrrrr is_Rationalrtminr) rrhas_sqrrr_rwxrus rd_has_square_rootsz-to_rational_coeffs.._has_square_rootsss !A]]1% !AJ&&'(wwyBeaKKBNNrttqyTTBBq6Q;!Fq6A:  ! ! Bs=C rrr)sympy.simplify.simplifyrrrr)r_rrrrrurs @rdto_rational_coeffsrsL1 D,& ||~ 1! 4 WWY B  Q41tQqT) )q"%AT1Q41-- rgc ddlm}t||d}|j}t |}|sy|\}}}} t | j } |re|| d|z||zz} |d|z } g} | dddD]5}| j||dj||| zi|df7| | fS| d} g} | dddD]/}| j|dj|||z i|df1| | fS)a helper function to factor polynomial using to_rational_coeffs Examples ======== >>> from sympy.polys.polytools import _torational_factor_list >>> from sympy.abc import x >>> from sympy import sqrt, expand, Mul >>> p = expand(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))})) >>> factors = _torational_factor_list(p, x); factors (-2, [(-x*(1 + sqrt(2))/2 + 1, 1), (-x*(1 + sqrt(2)) - 1, 1), (-x*(1 + sqrt(2)) + 1, 1)]) >>> expand(factors[0]*Mul(*[z[0] for z in factors[1]])) == p True >>> p = expand(((x**2-1)*(x-2)).subs({x:x + sqrt(2)})) >>> factors = _torational_factor_list(p, x); factors (1, [(x - 2 + sqrt(2), 1), (x - 1 + sqrt(2), 1), (x + 1 + sqrt(2), 1)]) >>> expand(factors[0]*Mul(*[z[0] for z in factors[1]])) == p True rrEXr$Nr) rrrWrrrr]rHr)rrrp1r*resrrurr`rrr1rrs rd_torational_factor_listrs,,1 a4 B A R C KB1a!))+&G WQZ]1a4' ( ac] Q =A HHhqtyy!QrT34ad; < = q6M AJ Q 4A HHadiiAE +QqT2 3 4 q6Mrgc t|||dS)z Compute a list of square-free factors of ``f``. Examples ======== >>> from sympy import sqf_list >>> from sympy.abc import x >>> sqf_list(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16) (2, [(x + 1, 2), (x + 2, 3)]) r)rrrs rdrrs 4e <>> from sympy import sqf >>> from sympy.abc import x >>> sqf(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16) 2*(x + 1)**2*(x + 2)**3 r)r )rrs rdr)r)s 1dD 77rgc t|||dS)a  Compute a list of irreducible factors of ``f``. Examples ======== >>> from sympy import factor_list >>> from sympy.abc import x, y >>> factor_list(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y) (2, [(x + y, 1), (x**2 + 1, 2)]) r|r r rs rdrrs 4h ??rg)rct|}|ryfd}t||}i}|jtt}|D]5}t |gi}|j s |js+||k7s1|||<7|j|S t|dS#t$r|js t|cYSwxYw)a Compute the factorization of expression, ``f``, into irreducibles. (To factor an integer into primes, use ``factorint``.) There two modes implemented: symbolic and formal. If ``f`` is not an instance of :class:`Poly` and generators are not specified, then the former mode is used. Otherwise, the formal mode is used. In symbolic mode, :func:`factor` will traverse the expression tree and factor its components without any prior expansion, unless an instance of :class:`~.Add` is encountered (in this case formal factorization is used). This way :func:`factor` can handle large or symbolic exponents. By default, the factorization is computed over the rationals. To factor over other domain, e.g. an algebraic or finite field, use appropriate options: ``extension``, ``modulus`` or ``domain``. Examples ======== >>> from sympy import factor, sqrt, exp >>> from sympy.abc import x, y >>> factor(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y) 2*(x + y)*(x**2 + 1)**2 >>> factor(x**2 + 1) x**2 + 1 >>> factor(x**2 + 1, modulus=2) (x + 1)**2 >>> factor(x**2 + 1, gaussian=True) (x - I)*(x + I) >>> factor(x**2 - 2, extension=sqrt(2)) (x - sqrt(2))*(x + sqrt(2)) >>> factor((x**2 - 1)/(x**2 + 4*x + 4)) (x - 1)*(x + 1)/(x + 2)**2 >>> factor((x**2 + 4*x + 4)**10000000*(x**2 + 1)) (x + 2)**20000000*(x**2 + 1) By default, factor deals with an expression as a whole: >>> eq = 2**(x**2 + 2*x + 1) >>> factor(eq) 2**(x**2 + 2*x + 1) If the ``deep`` flag is True then subexpressions will be factored: >>> factor(eq, deep=True) 2**((x + 1)**2) If the ``fraction`` flag is False then rational expressions will not be combined. By default it is True. >>> factor(5*x + 3*exp(2 - 7*x), deep=True) (5*x*exp(7*x) + 3*exp(2))*exp(-7*x) >>> factor(5*x + 3*exp(2 - 7*x), deep=True, fraction=False) 5*x + 3*exp(2)*exp(-7*x) See Also ======== sympy.ntheory.factor_.factorint cZt|gi}|js |jr|S|S)zS Factor, but avoid changing the expression when unable to. )r|is_Mulr)rrxr~rYs rd _try_factorzfactor.._try_factor:s0---CzzSZZ Krgr|r ) r%r(atomsrr r|rrxreplacerr<r)r) r_rrYr~rpartialsmuladdrrxs `` rdr|r|sH  A   a %c" "A*T*T*C cjjcQh!  "zz(##q$X>> Q<   s B B=;B=c t|ds# t|}|j||||||St |d\}} t | j dkDrtt|D]"\} } | jj|| <$|+| jj|}|dkr td|| jj|}|| jj|}t|| j|||||} g} | D]S\\}}}| jj|| jj|}}| j!||f|fU| S#t$rgcYSwxYw) a/ Compute isolating intervals for roots of ``f``. Examples ======== >>> from sympy import intervals >>> from sympy.abc import x >>> intervals(x**2 - 3) [((-2, -1), 1), ((1, 2), 1)] >>> intervals(x**2 - 3, eps=1e-2) [((-26/15, -19/11), 1), ((19/11, 26/15), 1)] rr$r-r$rrr")r%r&r'rr()rrWr;r#rDrrYr=rrjr=rSrrrIrrH)rrr%r&r'rr(r)rOrrrDr#rbrrrs rdr#r#Ws" 1j ! QA{{s#Dc{RR,Qt< s sxx=1 - - ' *GAtxx'')E!H * ?**$$S)Cax !DEE ?**$$S)C ?**$$S)C/szz#f4A ( -OFQG::&&q)3::+>+>q+AqA MMAq67+ , - C  I s E-- E;:E;c t|}t|ts!|jjs t d|j ||||||S#t $rt d|zwxYw)z Refine an isolating interval of a root to the given precision. Examples ======== >>> from sympy import refine_root >>> from sympy.abc import x >>> refine_root(x**2 - 3, 1, 2, eps=1e-2) (19/11, 26/15) generator must be a Symbolz,Cannot refine a root of %s, not a polynomial)r%r:r(r<)rWrVr is_Symbolr<r;r9)r_rrr%r:r(r<rs rdr9r9sz @ G!T"155??"">? ? ==A3e$)= TT @ :Q >@ @@s >> from sympy import count_roots, I >>> from sympy.abc import x >>> count_roots(x**4 - 4, -3, 3) 2 >>> count_roots(x**4 - 4, 0, 1 + 3*I) 1 Fgreedyrz*Cannot count roots of %s, not a polynomialrA)rWrVrrr<r;rJ)r_r&r'rs rdrJrJso(P 5 !!T"155??"">? ? ==Sc= ** PJQNOOPs >AA+Tc t|tr3|r.|jjst|jd}n |}n|rt|d}n t|d}t|ts!|j j s td|j||S#t$rtd|zwxYw)a Returns the real and complex roots of ``f`` with multiplicities. Explanation =========== Finds all real and complex roots of a univariate polynomial with rational coefficients of any degree exactly. The roots are represented in the form given by :func:`~.rootof`. This is equivalent to using :func:`~.rootof` to find each of the indexed roots. Examples ======== >>> from sympy import all_roots >>> from sympy.abc import x, y >>> print(all_roots(x**3 + 1)) [-1, 1/2 - sqrt(3)*I/2, 1/2 + sqrt(3)*I/2] Simple radical formulae are used in some cases but the cubic and quartic formulae are avoided. Instead most non-rational roots will be represented as :class:`~.ComplexRootOf`: >>> print(all_roots(x**3 + x + 1)) [CRootOf(x**3 + x + 1, 0), CRootOf(x**3 + x + 1, 1), CRootOf(x**3 + x + 1, 2)] All roots of any polynomial with rational coefficients of any degree can be represented using :py:class:`~.ComplexRootOf`. The use of :py:class:`~.ComplexRootOf` bypasses limitations on the availability of radical formulae for quintic and higher degree polynomials _[1]: >>> p = x**5 - x - 1 >>> for r in all_roots(p): print(r) CRootOf(x**5 - x - 1, 0) CRootOf(x**5 - x - 1, 1) CRootOf(x**5 - x - 1, 2) CRootOf(x**5 - x - 1, 3) CRootOf(x**5 - x - 1, 4) >>> [r.evalf(3) for r in all_roots(p)] [1.17, -0.765 - 0.352*I, -0.765 + 0.352*I, 0.181 - 1.08*I, 0.181 + 1.08*I] Irrational algebraic coefficients are handled by :func:`all_roots` if `extension=True` is set. >>> from sympy import sqrt, expand >>> p = expand((x - sqrt(2))*(x - sqrt(3))) >>> print(p) x**2 - sqrt(3)*x - sqrt(2)*x + sqrt(6) >>> all_roots(p) Traceback (most recent call last): ... NotImplementedError: sorted roots not supported over EX >>> all_roots(p, extension=True) [sqrt(2), sqrt(3)] Algebraic coefficients can be complex as well. >>> from sympy import I >>> all_roots(x**2 - I, extension=True) [-sqrt(2)/2 - sqrt(2)*I/2, sqrt(2)/2 + sqrt(2)*I/2] >>> all_roots(x**2 - sqrt(2)*I, extension=True) [-2**(3/4)/2 - 2**(3/4)*I/2, 2**(3/4)/2 + 2**(3/4)*I/2] Transcendental coefficients cannot currently be handled by :func:`all_roots`. In the case of algebraic or transcendental coefficients :func:`~.ground_roots` might be able to find some roots by factorisation: >>> from sympy import ground_roots >>> ground_roots(p, x, extension=True) {sqrt(2): 1, sqrt(3): 1} If the coefficients are numeric then :func:`~.nroots` can be used to find all roots approximately: >>> from sympy import nroots >>> nroots(p, 5) [1.4142, 1.732] If the coefficients are symbolic then :func:`sympy.polys.polyroots.roots` or :func:`~.ground_roots` should be used instead: >>> from sympy import roots, ground_roots >>> p = x**2 - 3*x*y + 2*y**2 >>> roots(p, x) {y: 1, 2*y: 1} >>> ground_roots(p, x) {y: 1, 2*y: 1} Parameters ========== f : :class:`~.Expr` or :class:`~.Poly` A univariate polynomial with rational (or ``Float``) coefficients. multiple : ``bool`` (default ``True``). Whether to return a ``list`` of roots or a list of root/multiplicity pairs. radicals : ``bool`` (default ``True``) Use simple radical formulae rather than :py:class:`~.ComplexRootOf` for some irrational roots. extension: ``bool`` (default ``False``) Whether to construct an algebraic extension domain before computing the roots. Setting to ``True`` is necessary for finding roots of a polynomial with (irrational) algebraic coefficients but can be slow. Returns ======= A list of :class:`~.Expr` (usually :class:`~.ComplexRootOf`) representing the roots is returned with each root repeated according to its multiplicity as a root of ``f``. The roots are always uniquely ordered with real roots coming before complex roots. The real roots are in increasing order. Complex roots are ordered by increasing real part and then increasing imaginary part. If ``multiple=False`` is passed then a list of root/multiplicity pairs is returned instead. If ``radicals=False`` is passed then all roots will be represented as either rational numbers or :class:`~.ComplexRootOf`. See also ======== Poly.all_roots: The underlying :class:`Poly` method used by :func:`~.all_roots`. rootof: Compute a single numbered root of a univariate polynomial. real_roots: Compute all the real roots using :func:`~.rootof`. ground_roots: Compute some roots in the ground domain by factorisation. nroots: Compute all roots using approximate numerical techniques. sympy.polys.polyroots.roots: Compute symbolic expressions for roots using radical formulae. References ========== .. [1] https://en.wikipedia.org/wiki/Abel%E2%80%93Ruffini_theorem T extensionFrr1Cannot compute real roots of %s, not a polynomialrUrM) rVrWrSrrrrr<r;rZr_rUrMrrs rdrZrZs`E a !;!;40d+5)!T"155??"">? ? ;;8; << E ?! CE EE BB&&B>c t|tr3|r.|jjst|jd}n |}n|rt|d}n t|d}t|ts!|j j s td|j||S#t$rtd|zwxYw)ax Returns the real roots of ``f`` with multiplicities. Explanation =========== Finds all real roots of a univariate polynomial with rational coefficients of any degree exactly. The roots are represented in the form given by :func:`~.rootof`. This is equivalent to using :func:`~.rootof` or :func:`~.all_roots` and filtering out only the real roots. However if only the real roots are needed then :func:`real_roots` is more efficient than :func:`~.all_roots` because it computes only the real roots and avoids costly complex root isolation routines. Examples ======== >>> from sympy import real_roots >>> from sympy.abc import x, y >>> real_roots(2*x**3 - 7*x**2 + 4*x + 4) [-1/2, 2, 2] >>> real_roots(2*x**3 - 7*x**2 + 4*x + 4, multiple=False) [(-1/2, 1), (2, 2)] Real roots of any polynomial with rational coefficients of any degree can be represented using :py:class:`~.ComplexRootOf`: >>> p = x**9 + 2*x + 2 >>> print(real_roots(p)) [CRootOf(x**9 + 2*x + 2, 0)] >>> [r.evalf(3) for r in real_roots(p)] [-0.865] All rational roots will be returned as rational numbers. Roots of some simple factors will be expressed using radical or other formulae (unless ``radicals=False`` is passed). All other roots will be expressed as :class:`~.ComplexRootOf`. >>> p = (x + 7)*(x**2 - 2)*(x**3 + x + 1) >>> print(real_roots(p)) [-7, -sqrt(2), CRootOf(x**3 + x + 1, 0), sqrt(2)] >>> print(real_roots(p, radicals=False)) [-7, CRootOf(x**2 - 2, 0), CRootOf(x**3 + x + 1, 0), CRootOf(x**2 - 2, 1)] All returned root expressions will numerically evaluate to real numbers with no imaginary part. This is in contrast to the expressions generated by the cubic or quartic formulae as used by :func:`~.roots` which suffer from casus irreducibilis [1]_: >>> from sympy import roots >>> p = 2*x**3 - 9*x**2 - 6*x + 3 >>> [r.evalf(5) for r in roots(p, multiple=True)] [5.0365 - 0.e-11*I, 0.33984 + 0.e-13*I, -0.87636 + 0.e-10*I] >>> [r.evalf(5) for r in real_roots(p, x)] [-0.87636, 0.33984, 5.0365] >>> [r.is_real for r in roots(p, multiple=True)] [None, None, None] >>> [r.is_real for r in real_roots(p)] [True, True, True] Using :func:`real_roots` is equivalent to using :func:`~.all_roots` (or :func:`~.rootof`) and filtering out only the real roots: >>> from sympy import all_roots >>> r = [r for r in all_roots(p) if r.is_real] >>> real_roots(p) == r True If only the real roots are wanted then using :func:`real_roots` is faster than using :func:`~.all_roots`. Using :func:`real_roots` avoids complex root isolation which can be a lot slower than real root isolation especially for polynomials of high degree which typically have many more complex roots than real roots. Irrational algebraic coefficients are handled by :func:`real_roots` if `extension=True` is set. >>> from sympy import sqrt, expand >>> p = expand((x - sqrt(2))*(x - sqrt(3))) >>> print(p) x**2 - sqrt(3)*x - sqrt(2)*x + sqrt(6) >>> real_roots(p) Traceback (most recent call last): ... NotImplementedError: sorted roots not supported over EX >>> real_roots(p, extension=True) [sqrt(2), sqrt(3)] Transcendental coefficients cannot currently be handled by :func:`real_roots`. In the case of algebraic or transcendental coefficients :func:`~.ground_roots` might be able to find some roots by factorisation: >>> from sympy import ground_roots >>> ground_roots(p, x, extension=True) {sqrt(2): 1, sqrt(3): 1} If the coefficients are numeric then :func:`~.nroots` can be used to find all roots approximately: >>> from sympy import nroots >>> nroots(p, 5) [1.4142, 1.732] If the coefficients are symbolic then :func:`sympy.polys.polyroots.roots` or :func:`~.ground_roots` should be used instead. >>> from sympy import roots, ground_roots >>> p = x**2 - 3*x*y + 2*y**2 >>> roots(p, x) {y: 1, 2*y: 1} >>> ground_roots(p, x) {y: 1, 2*y: 1} Parameters ========== f : :class:`~.Expr` or :class:`~.Poly` A univariate polynomial with rational (or ``Float``) coefficients. multiple : ``bool`` (default ``True``). Whether to return a ``list`` of roots or a list of root/multiplicity pairs. radicals : ``bool`` (default ``True``) Use simple radical formulae rather than :py:class:`~.ComplexRootOf` for some irrational roots. extension: ``bool`` (default ``False``) Whether to construct an algebraic extension domain before computing the roots. Setting to ``True`` is necessary for finding roots of a polynomial with (irrational) algebraic coefficients but can be slow. Returns ======= A list of :class:`~.Expr` (usually :class:`~.ComplexRootOf`) representing the real roots is returned. The roots are arranged in increasing order and are repeated according to their multiplicities as roots of ``f``. If ``multiple=False`` is passed then a list of root/multiplicity pairs is returned instead. If ``radicals=False`` is passed then all roots will be represented as either rational numbers or :class:`~.ComplexRootOf`. See also ======== Poly.real_roots: The underlying :class:`Poly` method used by :func:`real_roots`. rootof: Compute a single numbered root of a univariate polynomial. all_roots: Compute all real and non-real roots using :func:`~.rootof`. ground_roots: Compute some roots in the ground domain by factorisation. nroots: Compute all roots using approximate numerical techniques. sympy.polys.polyroots.roots: Compute symbolic expressions for roots using radical formulae. References ========== .. [1] https://en.wikipedia.org/wiki/Casus_irreducibilis TrFrrrr ) rVrWrSrrrrr<r;rWr!s rdrWrWusLE a !;!;40d+5)!T"155??"">? ? <<H< == E ?! CE EEr"c t|d}t|ts!|jjs t d|j |||S#t $rt d|zwxYw)aL Compute numerical approximations of roots of ``f``. Examples ======== >>> from sympy import nroots >>> from sympy.abc import x >>> nroots(x**2 - 3, n=15) [-1.73205080756888, 1.73205080756888] >>> nroots(x**2 - 3, n=30) [-1.73205080756887729352744634151, 1.73205080756887729352744634151] Frrz6Cannot compute numerical roots of %s, not a polynomial)r*r_r`)rWrVrrr<r;ry)r_r*r_r`rs rdryry3sw" J 5 !!T"155??"">? ? 88a(G8 << J Dq HJ JJs >AA,ctj|g t|g|i|\}}t|ts!|j j s td|jS#t$r}tdd|d}~wwxYw)z Compute roots of ``f`` by factorization in the ground domain. Examples ======== >>> from sympy import ground_roots >>> from sympy.abc import x >>> ground_roots(x**6 - 4*x**4 + 4*x**3 - x**2) {0: 2, 1: 2} rr}rN) ror^r<rVrWrrr<r?r@r}ras rdr}r}Rs $#81D1D13!T"155??"">? ? >>  83778sAA++ B4 BBcPtj|g t|g|i|\}}t|ts!|j j s td|j|}|js|jS|S#t$r}tdd|d}~wwxYw)a Construct a polynomial with n-th powers of roots of ``f``. Examples ======== >>> from sympy import nth_power_roots_poly, factor, roots >>> from sympy.abc import x >>> f = x**4 - x**2 + 1 >>> g = factor(nth_power_roots_poly(f, 2)) >>> g (x**2 - x + 1)**2 >>> R_f = [ (r**2).expand() for r in roots(f) ] >>> R_g = roots(g).keys() >>> set(R_f) == set(R_g) True rrrN) ror^r<rVrWrrr<r?r@rrOr])r_r*rYr~rrr|rbs rdrrps0 $#@1D1D13!T"155??"">? ? # #A &F 99~~  @ 63??@sAB B% B  B%) _signsimpcddlm}ddlm}t j |dgt |}|r||}i}d|vr|d|d<t|tsO|js t|tst|ts|St|d}|j\}}nt|dk(ry|\}}t|trCt|tr3|j |d<|j"|d <|j%dd|d<|j'|j'}}nt)d |zdd lm |j/r t1|||fg|i|\} \} } | j2s3t|ts|j5St6j8||fS d| jE| c} \}}|j%ddrd|vr| jX|d<t|ts$| |j'|j'z zS|j'|j'}}|j%dds| ||fS| t|g|i|t|g|i|fS#t0$rJ} |j:r|j/s t1| |j<s |j>rjtA|jBfd d \} }|Dcgc] }tE|ncc}w}}|jFtE|jF| g|cYd} ~ Sg}tI|}tK||D]_}t|tLst|ts$ |jO|tE|f|jQR#tR$rY]wxYw|jUtW|cYd} ~ Sd} ~ wwxYw)a[ Cancel common factors in a rational function ``f``. Examples ======== >>> from sympy import cancel, sqrt, Symbol, together >>> from sympy.abc import x >>> A = Symbol('A', commutative=False) >>> cancel((2*x**2 - 2)/(x**2 - 2*x + 1)) (2*x + 2)/(x - 1) >>> cancel((sqrt(3) + sqrt(15)*A)/(sqrt(2) + sqrt(10)*A)) sqrt(6)/2 Note: due to automatic distribution of Rationals, a sum divided by an integer will appear as a sum. To recover a rational form use `together` on the result: >>> cancel(x/2 + 1) x/2 + 1 >>> together(_) (x + 2)/2 r)signsimp)sringrOT)radicalrrYrSzunexpected argument: %srFcH|jduxr|j Sr )r)has)rrGs rdrizcancel..s'  D(Ay1A-ArgrNrF)-rr)sympy.polys.ringsr*ror^r%rVrrVr r rrrrWrYrSrr]rrIrGr-r<ngensr>r rr)rrrOr~rrcr'rXr)rHskiprqrrtr)r_r'rYr~r)r*rrrtrrrmsgrncrrPpoter rrGs @rdrrsV21' $ * A QK C$G}G a  ;;*Q 3:a;NH D )!1 Q11 a :a#6&&CKHHCM777D1CLyy{AIIK12Q677>* 55 !# #1a&04040 6Aqwwa'xxz!uua{" :188A;IAv1 www6#4iiF a !))+aiik)**yy{AIIK1www&a7Nd1+t+s+T!-Bd-Bc-BB BG *  AEE)$4!#& & 88qxx"BEAr&((&)((B(166&,2r2 2D$Q'C I a-Z45HKKF1I/HHJ*  ::d4j) )-*sbAI8I88 O A"O$K760O&O ,>O+,NO N$!O#N$$OO O cZtj|ddg t|gt|zg|i|\}}|j }d}|jr;|jr/|js#|jd|ji}d}dd l m } | |j|j |j\} } t!|D]L\} } | j#|j j$j'} | j)| || <N|dj+|d d\}}|Dcgc]!}t,j/t1||#}}t,j/t1||}|r3 |Dcgc]}|j3c}|j3}}||}}|j6s.|Dcgc]}|j9c}|j9fS||fS#t$r}t dd|d}~wwxYwcc}wcc}w#t4$rYqwxYwcc}w) a< Reduces a polynomial ``f`` modulo a set of polynomials ``G``. Given a polynomial ``f`` and a set of polynomials ``G = (g_1, ..., g_n)``, computes a set of quotients ``q = (q_1, ..., q_n)`` and the remainder ``r`` such that ``f = q_1*g_1 + ... + q_n*g_n + r``, where ``r`` vanishes or ``r`` is a completely reduced polynomial with respect to ``G``. Examples ======== >>> from sympy import reduced >>> from sympy.abc import x, y >>> reduced(2*x**4 + y**2 - x**2 + y**3, [x**3 - x, y**3 - y]) ([2*x, 1], x**2 + y**2 + y) rOrreducedrNFrSTxringr)ror^rDrwr?r@rSrrrrrr.r8rYrlrrrjrrr~rWrurtrr9rOr])r_rrYr~rOrr|rSr%r8_ringrrrDrrurt_Q_rs rdr6r6s( $& 123,aS47]JTJTJ sZZFG xxFNN6??ii6#3#3#567'SXXszz3995HE1U#)4szz*..668??4(a) 8<<ab "DAq0121a# &2A2 Q%A +,-aaiik-qyy{BrqA 99%&' '44!t C 3 1c223& 3 .    (sGG17&HHHHH(1 H : HH H H%$H%c t|g|i|S)a Computes the reduced Groebner basis for a set of polynomials. Use the ``order`` argument to set the monomial ordering that will be used to compute the basis. Allowed orders are ``lex``, ``grlex`` and ``grevlex``. If no order is specified, it defaults to ``lex``. For more information on Groebner bases, see the references and the docstring of :func:`~.solve_poly_system`. Examples ======== Example taken from [1]. >>> from sympy import groebner >>> from sympy.abc import x, y >>> F = [x*y - 2*y, 2*y**2 - x**2] >>> groebner(F, x, y, order='lex') GroebnerBasis([x**2 - 2*y**2, x*y - 2*y, y**3 - 2*y], x, y, domain='ZZ', order='lex') >>> groebner(F, x, y, order='grlex') GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y, domain='ZZ', order='grlex') >>> groebner(F, x, y, order='grevlex') GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y, domain='ZZ', order='grevlex') By default, an improved implementation of the Buchberger algorithm is used. Optionally, an implementation of the F5B algorithm can be used. The algorithm can be set using the ``method`` flag or with the :func:`sympy.polys.polyconfig.setup` function. >>> F = [x**2 - x - 1, (2*x - 1) * y - (x**10 - (1 - x)**10)] >>> groebner(F, x, y, method='buchberger') GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex') >>> groebner(F, x, y, method='f5b') GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex') References ========== 1. [Buchberger01]_ 2. [Cox97]_ ) GroebnerBasisrrYr~s rdr1r1<sf  *T *T **rgc4t|g|i|jS)a[ Checks if the ideal generated by a Groebner basis is zero-dimensional. The algorithm checks if the set of monomials not divisible by the leading monomial of any element of ``F`` is bounded. References ========== David A. Cox, John B. Little, Donal O'Shea. Ideals, Varieties and Algorithms, 3rd edition, p. 230 )r=is_zero_dimensionalr>s rdr@r@rs  *T *T * > >>rgceZdZdZdZedZedZedZ edZ edZ edZ ed Z d Zd Zd Zd ZdZdZedZdZddZdZy)r=z%Represents a reduced Groebner basis. c"tj|ddg t|g|i|\}}ddlm}||j|j|j}|D cgc].} | s|j| jj0}} t|||j} | D cgc]} t j#| |} } |j%| |S#t$r}t dt ||d}~wwxYwcc} wcc} w)z>Compute a reduced Groebner basis for a system of polynomials. rOrr1Nr)PolyRingr )ror^rDr?r@rr.rCrYrSrlrrjr _groebnerrrWru_new) r}rrYr~rOrr|rCringrDrr`s rdrzGroebnerBasis.__new__sdWh$78 =0BTBTBJE3 /#**cii8@EN 0 0 23NN eT#** 5./ 0T__Q $ 0 0xx3" =#JA< < = O 1s)C D&+D/D  D)C??Dc^tj|}t||_||_|Sr)rrr7_basis_options)r}basisrors rdrEzGroebnerBasis._news'mmC 5\   rgcpd|jD}t|t|jjfS)Nc3<K|]}|jywrr)rrs rdrz%GroebnerBasis.args..s22s)rHrrIrY)rrJs rdr~zGroebnerBasis.argss.2dkk2u udmm&8&89::rgc\|jDcgc]}|jc}Scc}wr)rHr]rrDs rdrCzGroebnerBasis.exprss +/;;74 777s)c,t|jSr)rwrHrs rdrOzGroebnerBasis.polyssDKK  rgc.|jjSr)rIrYrs rdrYzGroebnerBasis.genss}}!!!rgc.|jjSr)rIrSrs rdrSzGroebnerBasis.domains}}###rgc.|jjSr)rIrlrs rdrlzGroebnerBasis.orders}}"""rgc,t|jSr)rrHrs rd__len__zGroebnerBasis.__len__s4;;rgc|jjrt|jSt|jSr)rIrOiterrCrs rdrzGroebnerBasis.__iter__s- ==   # # # #rgcr|jjr|j}||S|j}||Sr)rIrOrC)ritemrJs rd __getitem__zGroebnerBasis.__getitem__s9 ==  JJET{JJET{rgcrt|jt|jj fSr)hashrHr7rIrrs rdrzGroebnerBasis.__hash__s(T[[% (;(;(=">?@@rgct||jr4|j|jk(xr|j|jk(St |r2|j t |k(xs|jt |k(Sy)NF)rVrrHrIrNrOrwrCrr s rdr zGroebnerBasis.__eq__sd eT^^ ,;;%,,.R4==ENN3R R e_::e,I d5k0I Irgc||k( Srrr]s rdrzGroebnerBasis.__ne__s5=  rgcd}tdgt|jz}|jj}|j D]"}|j |}||s||z}$t|S)a{ Checks if the ideal generated by a Groebner basis is zero-dimensional. The algorithm checks if the set of monomials not divisible by the leading monomial of any element of ``F`` is bounded. References ========== David A. Cox, John B. Little, Donal O'Shea. Ideals, Varieties and Algorithms, 3rd edition, p. 230 c:ttt|dk(Sr)sumrrK)monomials rd single_varz5GroebnerBasis.is_zero_dimensional..single_varss4*+q0 0rgrr,)r2rrYrIrlrOrr)rrcrrlrDrbs rdr@z!GroebnerBasis.is_zero_dimensionalsr 1aSTYY/0  ##JJ &DwwUw+H(#X%  &9~rgc |j}|j}t|}||k(r|S|js t dt |j }|j}|j|j|d}ddl m }||j|j|\}} t|D]L\} } | j|jjj!} |j#| || <Nt%|||} | D cgc]!} t&j)t+| |#} } |j,s)| D cgc]} | j/dd} } ||_|j1| |Scc} wcc} w)a Convert a Groebner basis from one ordering to another. The FGLM algorithm converts reduced Groebner bases of zero-dimensional ideals from one ordering to another. This method is often used when it is infeasible to compute a Groebner basis with respect to a particular ordering directly. Examples ======== >>> from sympy.abc import x, y >>> from sympy import groebner >>> F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1] >>> G = groebner(F, x, y, order='grlex') >>> list(G.fglm('lex')) [2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7] >>> list(groebner(F, x, y, order='lex')) [2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7] References ========== .. [1] J.C. Faugere, P. Gianni, D. Lazard, T. Mora (1994). Efficient Computation of Zero-dimensional Groebner Bases by Change of Ordering z?Cannot convert Groebner bases of ideals with positive dimension)rSrlrr7Trr)rIrlr3r@rqrwrHrSrrr.r8rYrrrjrrr0rWrurtrrrE)rrlr src_order dst_orderrOrSr8r9rrrDrr`s rdfglmzGroebnerBasis.fglmsc>mmII  '  !K''%&gh hT[[!ii&&(   ,3::y9q ' -GAt??3::.22::>> from sympy import groebner, expand, Poly >>> from sympy.abc import x, y >>> f = 2*x**4 - x**2 + y**3 + y**2 >>> G = groebner([x**3 - x, y**3 - y]) >>> G.reduce(f) ([2*x, 1], x**2 + y**2 + y) >>> Q, r = _ >>> expand(sum(q*g for q, g in zip(Q, G)) + r) 2*x**4 - x**2 + y**3 + y**2 >>> _ == f True # Using Poly input >>> f_poly = Poly(f, x, y) >>> G = groebner([Poly(x**3 - x), Poly(y**3 - y)]) >>> G.reduce(f_poly) ([Poly(2*x, x, y, domain='ZZ'), Poly(1, x, y, domain='ZZ')], Poly(x**2 + y**2 + y, x, y, domain='ZZ')) z;Polynomial generators don't match Groebner basis generatorsFrSTrr7rN)rVrWrYrIrrrSr{rwrHrrrrr.r8rlrrjrrr~rurtrr9rOr])rrrrDrOrrSr%r8r9rrrrurtr:r;s rdrzGroebnerBasis.reduce>sF dD !yyDMM... !^__??4==#7#78D??47Ddkk**mm FNN6??))Xv'7'7'9:;CG+3::syy9q ' -GAt??3::.22::>> from sympy import groebner >>> from sympy.abc import x, y >>> f = 2*x**3 + y**3 + 3*y >>> G = groebner([x**2 + y**2 - 1, x*y - 2]) >>> G.contains(f) True >>> G.contains(f + 1) False rr)rrNs rdcontainszGroebnerBasis.containss&{{4 #q((rgNr)r^r%r&r'rr,rEr-r~rCrOrYrSrlrTrrYrr rr@rgrrjrrgrdr=r=s/ &;;88!!""$$## $ A!>@!DN`)rgr=ctjgfdt|}|jrt |g|iSdvrdd<tj |}||S)z Efficiently transform an expression into a polynomial. Examples ======== >>> from sympy import poly >>> from sympy.abc import x >>> poly(x*(x**2 + x - 1)**2) Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ') c6gg}}tj|D]`}gg}}tj|D]}|jr|j  ||(|j rw|j jra|jjrK|jdk\r<|j  |j |j|j|j ||s|j ||d}|ddD]}|j|}|r?t|}|jr||z}n%|jtj||}|j |c|stj||} na|d} |ddD]}| j|} |r?t|}|jr| |z } n%| jtj||} | j|j!ddi S)NrrrYr)r rrrrHrrrrrprrVrWr{rrr) rrr poly_termsrr poly_factorsr|productrb_polyr~s rdrpzpoly.._polyszMM$' +D$&\G--- +== ''fc(:;]]v{{'9'9 --&**/ ''fkk3/33FJJ?ANN6* +  T"&q/*12.2F%kk&1G2 ']F''6)")++doofc.J"K!!'*; +>__T3/F]F"12 *D) *E{>>dNF#ZZc(BCFv~~swwvr2;d;;rgr>F)ror^r%ryrWrp)rrYr~rrps ` @rdrDrDsr $#2r?r@rAsympy.polys.polyutilsrBrCrDrErFrGsympy.polys.rationaltoolsrHsympy.polys.rootisolationrIsympy.utilitiesrJrKrLsympy.utilities.exceptionsrMsympy.utilities.iterablesrNrOrNrompmath.libmp.libhyperrPrfrWr2r<r;rDrBrTrrrrrrrsrwryr{r~rrrrrrrrrrrrrrrUrrrrrrrrrrrrrrrrrrrrrr)rr|r#r9rJrZrWryr}rrr6r@r=rDrurrgrdrs >"#,#,AAMM*+#776&+0>+.4**;-;*.11    /A55@4 /!JPE`5PE`PE`fJZtZZz&& %P00 [|7N7Nt11h((4!!02!!2::DD>>>D##L##L00f:&+!!H:%%PQQh00fJJZ//dr3r3j::0**Z:::++\<<   F:= $ 6 :#z )FX8|~)X=="88"@@"__D44nUU8++@d=d=Nz>z>z==<:((V#`C`CF88v2+2+j??"\)E\)\)~NNb"-rg