
    \h?                      S r SSKJr  SSKJrJr  SSKJr  SSKJ	r	  SSK
JrJr  SSKJrJrJrJr  SSKJr  SS	KJr  SS
KJrJrJr  SSKJrJrJrJrJr  SSK J!r!  SSK"J#r#J$r$  SSK%J&r&  SSK'J(r(J)r)J*r*  SSK+J,r,J-r-  SSK.J/r/  SSK0J1r1J2r2  SSK3J4r4J5r5  SSK6J7r7J8r8  SSK9J:r:  SSK;J<r=  SSK>J?r?  SSK@JArAJBrBJCrC  SSKDJErE  SSKFJGrG  SSKHJIrJ  SSKKJLrL  SSKMJNrN  SSKOJPrPJQrQJRrR  SSKSJTrTJUrUJVrVJWrWJXrXJYrYJZrZJ[r[J\r\J]r]J^r^  SS K_J`r`JaraJbrbJcrcJdrdJere  SS!KfJgrg  SS"KhJiri  SS#KjJkrkJlrlJmrm  SS$KnJoro  SS%KpJqrqJrrr  SS&K;rsSS&KtrtSS'KuJvrv  S( rw\l " S) S*\5      5       rx\l " S+ S,\x5      5       ry\lS- 5       rzS. r{\lS/ 5       r|S0 r}S1 r~\lSyS2 j5       r\lS3 5       r\lS4 5       r\lS5 5       r\lS6 5       r\lS7 5       r\lS8 5       r\lS9 5       r\lS: 5       r\lS; 5       r\lS< 5       r\lS= 5       r\lS> 5       r\lS? 5       r\lS@ 5       r\lSA 5       r\lSB 5       r\lSC 5       r\lSDSE.SF j5       r\lSG 5       r\lSH 5       r\lSI 5       r\lSzSJ j5       r\lSK 5       r\lSzSL j5       r\lSM 5       r\lSN 5       r\lSO 5       r\lSP 5       r\lSQ 5       r\lSR 5       r\lSS 5       r\lST 5       r\lSU 5       r\lSV 5       r\lSW 5       r\lSX 5       rSY rSZ rS[ rS\ rS] rS^ rS_ rS` rSa r\lSb 5       r\lSc 5       r\lSd 5       r\lSDSe.Sf j5       r\lS{Sg j5       r\lS|Sh j5       r\lS}Si j5       r\lS~Sk j5       r\lS~Sl j5       r\lSSm j5       r\lSn 5       r\lSo 5       r\lSjSp.Sq j5       r\lSr 5       r\lSs 5       rI\lSt 5       r\l " Su Sv\5      5       r\lSw 5       rSx rg&)z8User-friendly public interface to polynomial functions.     )annotations)wrapsreducemul)Optional)Counterdefaultdict)SExprAddTuple)Basic)
_sympifyit)Factors	factor_ncfactor_terms)pure_complexevalffastlog_evalf_with_bounded_errorquad_to_mpmath)
Derivative)Mul_keep_coeff)ilcm)IIntegerequal_valued)
RelationalEquality)ordered)DummySymbol)sympify_sympify)preorder_traversal	bottom_up)BooleanAtom)polyoptions)construct_domain)FFQQZZ)DomainElement)matrix_fglm)groebner)Monomial)monomial_key)DMPDMFANP)OperationNotSupportedDomainErrorCoercionFailedUnificationFailedGeneratorsNeededPolynomialErrorMultivariatePolynomialErrorExactQuotientFailedPolificationFailedComputationFailedGeneratorsError)basic_from_dict
_sort_gens_unify_gens_dict_reorder_dict_from_expr_parallel_dict_from_expr)together)dup_isolate_real_roots_list)grouppublic
filldedent)sympy_deprecation_warning)iterablesiftN)NoConvergencec                0   ^  [        T 5      U 4S j5       nU$ )Nc                :  > [        U5      n[        U[        5      (       a  T" X5      $ [        U[        5      (       a2  U R                  " U/U R
                  Q7SU R                  06nT" X5      $ [        U[        5      (       a&   U R                  " U/U R
                  Q76 nT" X5      $ [        $ ! [         a^    UR                  (       a  [        s $ [        U R                  5       TR                  5      nU" U5      nU[        La  [        SSSS9  Us $ f = f)Ndomaina@  
                        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().
                        z1.6z)deprecated-poly-nonpoly-binary-operations)deprecated_since_versionactive_deprecations_target)r&   
isinstancePolyr   	from_exprgensrS   r   r<   	is_MatrixNotImplementedgetattras_expr__name__rM   )fgexpr_methodresultfuncs       M/var/www/auris/envauris/lib/python3.13/site-packages/sympy/polys/polytools.pywrapper_polifyit.<locals>.wrapperF   s    QKa:7##A88qxx8A:4  "KK+AFF+& Az!!!) # ;;))%aiik4==A$Q/- 273^	 !s   B2 2!DADD)r   )rc   re   s   ` rd   	_polifyitrg   E   s     
4[" "> N    c                    ^  \ rS rSr% SrSrSrSrSrS\	S'   S\	S	'   SS
 jr
\S 5       r\S 5       r\S 5       rS r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rU 4S jr\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r S r!S  r"SS! jr#S" r$S# r%S$ r&S% r'S& r(S' r)SS( jr*S) r+S* r,S+ r-S, r.S- r/S. r0S/ r1SS0 jr2SS1 jr3SS2 jr4SS3 jr5SS4 jr6S5 r7S6 r8S7 r9S8 r:S9 r;SS: jr<SS; jr=S< r>S= r?S> r@S? rASS@ jrBSA rCSB rDSC rESD rFSE rGSF rHSG rISH rJSI rKSJ rLSK rMSL rNSM rOSN rPSO rQSP rRSQ rSSR rTSSS jrUSST jrVSSU jrWSSV jrXSW rYSSX jrZSY r[SZ r\S[ r]S\ r^SS] jr_S^ r`SS_ jraS` rbSa rcSSb jrdSSc jreSSd jrfSSe jrgSSf jrhSg riSh rjSSi jrkSj rlSk rmSl rn\nroSSm jrpSn rqSSo jrrSSp jrsSSq jrtSr ruSs rvSSt jrwSu rxSSv jrySSw jrzSx r{Sy r|Sz r}S{ r~SS| jrS} rS~ rS rS rS rS rS rSS jrS rS rS rS rSS jrSS jrSS jrS rSS jrSS jrSS jrSS jrSS jrSS jrSS jrS rS rS rS rS rS rSS jrS rSS jr\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS rS r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\" S\5      S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\" S\5      S 5       r\" S\5      S 5       r\" S\5      S 5       r\" S\5      S 5       rS rSS jrSS jrS rSrU =r$ )rW   j   a  
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

reprY   Tgn $@r4   rl   ztuple[Expr, ...]rY   c                   [         R                  " X#5      nSU;   a  [        S5      e[        U[        [
        [        [        45      (       a  U R                  X5      $ [        U[        S9(       aA  [        U[        5      (       a  U R                  X5      $ U R                  [        U5      U5      $ [        U[!        U5      [        LS9nUR"                  (       a  U R%                  X5      $ U R'                  X5      $ )z:Create a new polynomial instance out of something useful. orderz&'order' keyword is not implemented yet)exclude)evaluate)optionsbuild_optionsNotImplementedErrorrV   r4   r5   r6   r/   _from_domain_elementrN   strdict
_from_dict
_from_listlistr%   typeis_Poly
_from_poly
_from_exprclsrl   rY   argsopts        rd   __new__Poly.__new__   s    ##D/c>%&NOOcCc=9::++C55c3'#t$$~~c//~~d3i55#S	(<=C{{~~c//~~c//rh   c                    [        U[        5      (       d  [        SU-  5      eUR                  [	        U5      S-
  :w  a  [        SU< SU< 35      e[
        R                  " U 5      nXl        X#l        U$ )z:Construct :class:`Poly` instance from raw representation. z%invalid polynomial representation: %s   zinvalid arguments: z, )	rV   r4   r<   levlenr   r   rl   rY   )r   rl   rY   objs       rd   newPoly.new   sg     #s##!7#=? ?WWD	A%!d"KLLmmC 
rh   c                ^    [        U R                  R                  5       /U R                  Q76 $ N)rB   rl   to_sympy_dictrY   selfs    rd   expr	Poly.expr   s#    txx557D$))DDrh   c                6    U R                   4U R                  -   $ r   )r   rY   r   s    rd   r   	Poly.args   s    		|dii''rh   c                6    U R                   4U R                  -   $ r   rk   r   s    rd   _hashable_contentPoly._hashable_content   s    {TYY&&rh   c                P    [         R                  " X#5      nU R                  X5      $ )(Construct a polynomial from a ``dict``. )rq   rr   rw   r~   s        rd   	from_dictPoly.from_dict   #     ##D/~~c''rh   c                P    [         R                  " X#5      nU R                  X5      $ )(Construct a polynomial from a ``list``. )rq   rr   rx   r~   s        rd   	from_listPoly.from_list   r   rh   c                P    [         R                  " X#5      nU R                  X5      $ )*Construct a polynomial from a polynomial. )rq   rr   r|   r~   s        rd   	from_polyPoly.from_poly   r   rh   c                P    [         R                  " X#5      nU R                  X5      $ +Construct a polynomial from an expression. )rq   rr   r}   r~   s        rd   rX   Poly.from_expr   r   rh   c                8   UR                   nU(       d  [        S5      e[        U5      S-
  nUR                  nUc  [	        XS9u  pQO,UR                  5        H  u  pgUR                  U5      X'   M     U R                  " [        R                  " XU5      /UQ76 $ )r   z0Cannot initialize from 'dict' without generatorsr   r   )
rY   r;   r   rS   r+   itemsconvertr   r4   r   )r   rl   r   rY   levelrS   monomcoeffs           rd   rw   Poly._from_dict   s     xx"BD D D	A>*38KFC #		#^^E2
 !, wws}}S8@4@@rh   c                R   UR                   nU(       d  [        S5      e[        U5      S:w  a  [        S5      e[        U5      S-
  nUR                  nUc  [        XS9u  pQO[        [        UR                  U5      5      nU R                  " [        R                  " XU5      /UQ76 $ )r   z0Cannot initialize from 'list' without generatorsr   z#'list' representation not supportedr   )rY   r;   r   r=   rS   r+   ry   mapr   r   r4   r   )r   rl   r   rY   r   rS   s         rd   rx   Poly._from_list  s     xx"BD DY!^-57 7 D	A>*38KFCs6>>3/0Cwws}}S8@4@@rh   c                   XR                   :w  a'  U R                  " UR                  /UR                  Q76 nUR                  nUR                  nUR
                  nU(       aa  UR                  U:w  aQ  [        UR                  5      [        U5      :w  a   U R                  UR                  5       U5      $ UR                  " U6 nSU;   a  U(       a  UR                  U5      nU$ USL a  UR                  5       nU$ )r   rS   T)	__class__r   rl   rY   fieldrS   setr}   r]   reorder
set_domainto_field)r   rl   r   rY   r   rS   s         rd   r|   Poly._from_poly%  s     --''#''-CHH-Cxx		CHH$388}D	)~~ckkmS99kk4(s?v..(C 
 d],,.C
rh   c                >    [        X5      u  pU R                  X5      $ r   )rF   rw   )r   rl   r   s      rd   r}   Poly._from_expr<  s     #3,~~c''rh   c                    UR                   nUR                  n[        U5      S-
  nUR                  U5      /nU R                  " [
        R                  " XU5      /UQ76 $ Nr   )rY   rS   r   r   r   r4   r   )r   rl   r   rY   rS   r   s         rd   rt   Poly._from_domain_elementB  sQ    xxD	A~~c"#wws}}S8@4@@rh   c                    > [         TU ]  5       $ r   super__hash__r   r   s    rd   r   Poly.__hash__L      w!!rh   c                    [        5       nU R                  n[        [        U5      5       H7  nU R	                  5        H   nXC   (       d  M  XU   R
                  -  n  M5     M9     XR                  -  $ )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}

)r   rY   ranger   monomsfree_symbolsfree_symbols_in_domain)r   symbolsrY   ir   s        rd   r   Poly.free_symbolsO  sd    * %yys4y!A88Aw333G ' " 4444rh   c                   U R                   R                  [        5       p!UR                  (       a#  UR                   H  nX#R
                  -  nM     U$ UR                  (       a%  U R                  5        H  nX$R
                  -  nM     U$ )a
  
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}

)rl   domr   is_Compositer   r   is_EXcoeffs)r   rS   r   genr   s        rd   r   Poly.free_symbols_in_domainn  so    & ((,,~~+++ & 	 \\--- ' rh   c                     U R                   S   $ )z
Return the principal generator.

Examples
========

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).gen
x

r   rY   r   s    rd   r   Poly.gen  s     yy|rh   c                "    U R                  5       $ )a  Get 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_domainr   s    rd   rS   Poly.domain  s    *   rh   c                    U R                   " U R                  R                  U R                  R                  U R                  R                  5      /U R
                  Q76 $ )z3Return zero polynomial with ``self``'s properties. )r   rl   zeror   r   rY   r   s    rd   r   	Poly.zero  s;     xxdhhllDHHLLANDIINNrh   c                    U R                   " U R                  R                  U R                  R                  U R                  R                  5      /U R
                  Q76 $ )z2Return one polynomial with ``self``'s properties. )r   rl   oner   r   rY   r   s    rd   r   Poly.one  s;     xxTXX\\488<<@M499MMrh   c                J    U R                  U5      u  p#pEU" U5      U" U5      4$ )aS  
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`   _perFGs         rd   unify
Poly.unify  s'    2 xx{1vs1v~rh   c           
       ^ [        U5      nUR                  (       dm   U R                  R                  R	                  U5      nU R                  R                  U R
                  U R                  U R                  R                  U5      4$ [        U R                  [        5      (       GaB  [        UR                  [        5      (       Ga"  [        U R                  UR                  5      nU R                  R                  R                  UR                  R                  U5      [        U5      S-
  pTU R                  U:w  a  [        U R                  R!                  5       U R                  U5      u  pgU R                  R                  U:w  a4  U Vs/ s H'  oR#                  XR                  R                  5      PM)     nn[        R$                  " ['        [)        [+        Xg5      5      5      XT5      n	OU R                  R#                  U5      n	UR                  U:w  a  [        UR                  R!                  5       UR                  U5      u  pUR                  R                  U:w  a4  U Vs/ s H'  oR#                  XR                  R                  5      PM)     nn[        R$                  " ['        [)        [+        X5      5      5      XT5      nO/UR                  R#                  U5      nO[        SU < SU< 35      eU R,                  mXCS 4U4S jjnXMX4$ ! [         a    [        SU < SU< 35      ef = fs  snf s  snf )NCannot unify  with r   c                z   > Ub%  US U X#S-   S  -   nU(       d  UR                  U 5      $ TR                  " U /UQ76 $ r   to_sympyr   rl   r   rY   remover   s       rd   r   Poly._unify.<locals>.per  H    !GV}tQJK'88<<,,773&&&rh   )r%   r{   rl   r   
from_sympyr   
ground_newr9   r:   rV   r4   rD   rY   r   r   rE   to_dictr   r   rv   ry   zipr   )r_   r`   g_coeffrY   r   r   f_monomsf_coeffscr   g_monomsg_coeffsr   r   r   s                 @rd   r   Poly._unify  sD   AJyyJ%%))..q1 uuyy!%%0@0@0IIIaeeS!!j&<&<qvvqvv.Duuyyquuyy$7TQvv~%2EEMMOQVVT&3" 5599#CKL8aAuuyy 98HLMM$tC,C'D"EsPEEMM#&vv~%2EEMMOQVVT&3" 5599#CKL8aAuuyy 98HLMM$tC,C'D"EsPEEMM#&#A$FGGkk 	' ~[ " L'Q(JKKL  M  Ms   %L .L;..M L8c                    Uc  U R                   nUb9  USU X#S-   S -   nU(       d%  U R                  R                  R                  U5      $ U R                  R
                  " U/UQ76 $ )a  
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   )rY   rl   r   r   r   r   )r_   rl   rY   r   s       rd   r   Poly.per  se    $ <66D=4
#44Duuyy))#..{{s*T**rh   c                    [         R                  " U R                  SU05      nU R                  U R                  R                  UR                  5      5      $ )z Set the ground domain of ``f``. rS   )rq   rr   rY   r   rl   r   rS   )r_   rS   r   s      rd   r   Poly.set_domain,  s=    ##AFFXv,>?uuQUU]]3::.//rh   c                .    U R                   R                  $ )z Get the ground domain of ``f``. )rl   r   r_   s    rd   r   Poly.get_domain1  s    uuyyrh   c                t    [         R                  R                  U5      nU R                  [	        U5      5      $ )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)

)rq   Modulus
preprocessr   r,   )r_   moduluss     rd   set_modulusPoly.set_modulus5  s+     //,,W5||BwK((rh   c                    U R                  5       nUR                  (       a  [        UR                  5       5      $ [	        S5      e)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)r   is_FiniteFieldr   characteristicr<   )r_   rS   s     rd   get_modulusPoly.get_modulusF  s8       600233!"HIIrh   c                    XR                   ;   a4  UR                  (       a  U R                  X5      $  U R                  X5      $ U R                  5       R                  X5      $ ! [         a     N+f = f)z)Internal implementation of :func:`subs`. )rY   	is_numberevalreplacer<   r]   subs)r_   oldr   s      rd   
_eval_subsPoly._eval_subs[  sb    &&=}}vvc''99S.. yy{)) ' s   A# #
A0/A0c                    U R                   R                  5       u  p[        U R                  5       VVs/ s H  u  p4X1;  d  M  UPM     nnnU R	                  X%S9$ s  snnf )z
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   )rl   ro   	enumeraterY   r   )r_   Jr   jr   rY   s         rd   ro   Poly.excludeh  sQ     "+AFF"3B"3qz"3BuuSu$$ Cs   AAc                   Uc*  U R                   (       a  U R                  Up!O[        S5      eX:X  d  XR                  ;  a  U $ XR                  ;   a  X R                  ;  ar  U R	                  5       nUR
                  (       a  X$R                  ;  aB  [        U R                  5      nX%UR                  U5      '   U R                  U R                  US9$ [        SU< SU< SU < 35      e)z
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 caser   zCannot replace r    in )is_univariater   r<   rY   r   r   r   ry   indexr   rl   )r_   xy_ignorer   rY   s         rd   r  Poly.replace{  s     9uua1%>@ @ 6Qff_H;1FF?,,.C##q';AFF|&'TZZ]#uuQUUu..1aKLLrh   c                B    U R                  5       R                  " U0 UD6$ )z-Match expression from Poly. See Basic.match())r]   match)r_   r   kwargss      rd   r'  
Poly.match  s    yy{  $1&11rh   c                   [         R                  " SU5      nU(       d  [        U R                  US9nO-[	        U R                  5      [	        U5      :w  a  [        S5      e[        [        [        [        U R                  R                  5       U R                  U5      6 5      5      nU R                  [        R                  " U[        U5      S-
  U R                  R                   5      US9$ )z
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')

 r   z7generators list can differ only up to order of elementsr   r   )rq   OptionsrC   rY   r   r<   rv   ry   r   rE   rl   r   r   r4   r   r   r   )r_   rY   r   r   rl   s        rd   r   Poly.reorder  s     oob$'aff#.D[CI%!IK K 4]155==?AFFDIJKLuuS]]3D	AquuyyAuMMrh   c                v   U R                  SS9nU R                  U5      n0 nUR                  5        H,  u  pV[        USU 5      (       a  [	        SU -  5      eXdXSS '   M.     U R
                  US nU R                  " [        R                  " U[        U5      S-
  U R                  R                  5      /UQ76 $ )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)nativeNzCannot left trim %sr   )as_dict_gen_to_levelr   anyr<   rY   r   r4   r   r   rl   r   )r_   r   rl   r  termsr   r   rY   s           rd   ltrim
Poly.ltrim  s    & iiti$OOC IIKLE5!9~~%&;a&?@@$%) ( vvabzuuS]]5#d)a-CKdKKrh   c                >   [        5       nU H0  n U R                  R                  U5      nUR                  U5        M2     U R                  5        H(  n[        U5       H  u  pgXb;  d  M  U(       d  M      g   M*     g! [         a    [        U < SU< S35      ef = f)z
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)r   rY   r!  add
ValueErrorrA   r   r  )r_   rY   indicesr   r!  r   r   elts           rd   has_only_gensPoly.has_only_gens  s      %C#S)
 E"  XXZE#E*#  +  
   B%9:C@B BBs   A>>Bc                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )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)hasattrrl   r@  r7   r   r_   rb   s     rd   r@  Poly.to_ring  s?     155)$$UU]]_F'955uuV}rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )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   )rA  rl   r   r7   r   rB  s     rd   r   Poly.to_field  @     155*%%UU^^%F':66uuV}rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )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)rA  rl   rH  r7   r   rB  s     rd   rH  Poly.to_exact,  rF  rh   c                    [        U R                  SS9XR                  R                  =(       d    SS9u  p#U R	                  X0R
                  US9$ )a8  
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')

Tr   N)r   	compositerS   )r+   r0  rS   r   r   rY   )r_   r   r   rl   s       rd   retractPoly.retractA  sG    ( $AII4I$888#8#8#@DB{{3s{33rh   c                   Uc  SXp2nOU R                  U5      n[        U5      [        U5      p2[        U R                  S5      (       a  U R                  R	                  X#U5      nO[        U S5      eU R                  U5      $ )z1Take a continuous subsequence of terms of ``f``. r   slice)r1  intrA  rl   rQ  r7   r   )r_   r"  mnr  rb   s         rd   rQ  
Poly.sliceY  so    9!A!"A1vs1v1155'""UU[[q)F'733uuV}rh   c                    U R                   R                  US9 Vs/ s H'  o R                   R                  R                  U5      PM)     sn$ s  snf )z
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

rn   )rl   r   r   r   )r_   rn   r   s      rd   r   Poly.coeffsi  s>    ( 01uu||%|/HI/H!		""1%/HIIIs   .Ac                4    U R                   R                  US9$ )z
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

rW  )rl   r   r_   rn   s     rd   r   Poly.monoms  s    $ uu||%|((rh   c                    U R                   R                  US9 VVs/ s H+  u  p#X R                   R                  R                  U5      4PM-     snn$ s  snnf )a  
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

rW  )rl   r3  r   r   )r_   rn   rS  r   s       rd   r3  
Poly.terms  sE    $ 89uu{{{7OP7OtqEEII&&q)*7OPPPs   2Ac                    U R                   R                  5        Vs/ s H'  oR                   R                  R                  U5      PM)     sn$ s  snf )z
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]

)rl   
all_coeffsr   r   )r_   r   s     rd   r_  Poly.all_coeffs  s<     01uu/?/?/AB/A!		""1%/ABBBs   .Ac                6    U R                   R                  5       $ )z
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

)rl   
all_monomsr  s    rd   rb  Poly.all_monoms  s    $ uu!!rh   c                    U R                   R                  5        VVs/ s H+  u  pXR                   R                  R                  U5      4PM-     snn$ s  snnf )z
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)]

)rl   	all_termsr   r   )r_   rS  r   s      rd   re  Poly.all_terms  sA     89uu7HI7HtqEEII&&q)*7HIIIs   2Ac                   0 nU R                  5        HI  u  pVU" XV5      n[        U[        5      (       a  Uu  pVOUnU(       d  M2  XT;  a  XdU'   M=  [        SU-  5      e   U R                  " U/U=(       d    U R
                  Q70 UD6$ )a  
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)r3  rV   tupler<   r   rY   )r_   rc   rY   r   r3  r   r   rb   s           rd   termwisePoly.termwise  s    $ GGILE%'F&%((%uu%#(%L)9EAC C & {{5<DNAFF<t<<rh   c                4    [        U R                  5       5      $ )z
Returns the number of non-zero terms in ``f``.

Examples
========

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 2*x - 1).length()
3

)r   r0  r  s    rd   lengthPoly.length   s     199;rh   c                t    U(       a  U R                   R                  US9$ U R                   R                  US9$ )z
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}

rK  )rl   r   r   )r_   r/  r   s      rd   r0  Poly.as_dict  s4     55==d=++55&&D&11rh   c                x    U(       a  U R                   R                  5       $ U R                   R                  5       $ )z%Switch to a ``list`` representation. )rl   to_listto_sympy_list)r_   r/  s     rd   as_listPoly.as_list#  s(    55==?"55&&((rh   c                   U(       d  U R                   $ [        U5      S:X  aa  [        US   [        5      (       aI  US   n[	        U R
                  5      nUR                  5        H  u  p4 UR                  U5      nXAU'   M     [        U R                  R                  5       /UQ76 $ ! [         a    [        U < SU< S35      ef = f)a
  
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

r   r   r7  r8  )r   r   rV   rv   ry   rY   r   r!  r:  rA   rB   rl   r   )r_   rY   mappingr   valuer!  s         rd   r]   Poly.as_expr*  s    ( 66Mt9>ja$771gG<D%mmo
( JJsOE
 #(K . quu224<t<< " F)=>DF FFs   +B((Cc                j     [        U /UQ70 UD6nUR                  (       d  gU$ ! [         a     gf = f)a3  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)rW   r{   r<   )r   rY   r   polys       rd   as_polyPoly.as_polyP  s<     	,t,t,D<< 		s    % % 
22c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )z
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)rA  rl   r~  r7   r   rB  s     rd   r~  	Poly.liftj  s?     155&!!UUZZ\F'622uuV}rh   c                    [        U R                  S5      (       a  U R                  R                  5       u  pO[        U S5      eXR	                  U5      4$ )z
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)rA  rl   r  r7   r   r_   r  rb   s      rd   r  Poly.deflate  sF     155)$$IAv'955%%-rh   c                   U R                   R                  nUR                  (       a  U $ UR                  (       d  [	        SU-  5      e[        U R                   S5      (       a  U R                   R                  US9nO[        U S5      eU(       a  UR                  U R                  -   nOU R                  UR                  -   nU R                  " U/UQ76 $ )a7  
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 %sinjectfront)rl   r   is_Numericalr{   r8   rA  r  r7   r   rY   r   )r_   r  r   rb   rY   s        rd   r  Poly.inject  s    $ eeiiH@3FGG155(##UU\\\.F'844;;'D66CKK'DuuV#d##rh   c                   U R                   R                  nUR                  (       d  [        SU-  5      e[	        U5      nU R
                  SU U:X  a  U R
                  US SpTO1U R
                  U* S U:X  a  U R
                  SU*  SpTO[        S5      eUR                  " U6 n[        U R                   S5      (       a  U R                   R                  X%S9nO[        U S5      eU R                  " U/UQ76 $ )a6  
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]')

zCannot eject generators over %sNTFz'can only eject front or back generatorsejectr  )rl   r   r  r8   r   rY   rs   r  rA  r  r7   r   )r_   rY   r   k_gensr  rb   s          rd   r  
Poly.eject  s    $ eeii?#EFFI66"1:66!":t5VVQBC[D 66#A2;5%9; ; jj$155'""UU[[[2F'733uuV$e$$rh   c                    [        U R                  S5      (       a  U R                  R                  5       u  pO[        U S5      eXR	                  U5      4$ )z
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)rA  rl   r  r7   r   r  s      rd   r  Poly.terms_gcd  sG     155+&&)IAv';77%%-rh   c                    [        U R                  S5      (       a  U R                  R                  U5      nO[        U S5      eU R	                  U5      $ )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)rA  rl   r  r7   r   r_   r   rb   s      rd   r  Poly.add_ground  D     155,''UU%%e,F'<88uuV}rh   c                    [        U R                  S5      (       a  U R                  R                  U5      nO[        U S5      eU R	                  U5      $ )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)rA  rl   r  r7   r   r  s      rd   r  Poly.sub_ground  r  rh   c                    [        U R                  S5      (       a  U R                  R                  U5      nO[        U S5      eU R	                  U5      $ )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)rA  rl   r  r7   r   r  s      rd   r  Poly.mul_ground"  r  rh   c                    [        U R                  S5      (       a  U R                  R                  U5      nO[        U S5      eU R	                  U5      $ )z
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)rA  rl   r  r7   r   r  s      rd   r  Poly.quo_ground7  sD    " 155,''UU%%e,F'<88uuV}rh   c                    [        U R                  S5      (       a  U R                  R                  U5      nO[        U S5      eU R	                  U5      $ )aC  
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)rA  rl   r  r7   r   r  s      rd   r  Poly.exquo_groundO  sD    & 155.))UU''.F'>::uuV}rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )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)rA  rl   r  r7   r   rB  s     rd   r  Poly.absi  s?     155%  UUYY[F'511uuV}rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )z
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)rA  rl   r  r7   r   rB  s     rd   r  Poly.neg~  ?    " 155%  UUYY[F'511uuV}rh   c                   [        U5      nUR                  (       d  U R                  U5      $ U R                  U5      u  p#pE[	        U R
                  S5      (       a  UR                  U5      nO[        U S5      eU" U5      $ )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')

r9  )r%   r{   r  r   rA  rl   r9  r7   r_   r`   r   r   r   r   rb   s          rd   r9  Poly.add  h    " AJyy<<?"xx{155%  UU1XF'5116{rh   c                   [        U5      nUR                  (       d  U R                  U5      $ U R                  U5      u  p#pE[	        U R
                  S5      (       a  UR                  U5      nO[        U S5      eU" U5      $ )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%   r{   r  r   rA  rl   r  r7   r  s          rd   r  Poly.sub  r  rh   c                   [        U5      nUR                  (       d  U R                  U5      $ U R                  U5      u  p#pE[	        U R
                  S5      (       a  UR                  U5      nO[        U S5      eU" U5      $ )a   
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%   r{   r  r   rA  rl   r   r7   r  s          rd   r   Poly.mul  r  rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )z
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)rA  rl   r  r7   r   rB  s     rd   r  Poly.sqr  r  rh   c                    [        U5      n[        U R                  S5      (       a  U R                  R                  U5      nO[	        U S5      eU R                  U5      $ )a  
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)rR  rA  rl   r  r7   r   r_   rT  rb   s      rd   r  Poly.pow  sJ    " F155%  UUYYq\F'511uuV}rh   c                    U R                  U5      u  p#pE[        U R                  S5      (       a  UR                  U5      u  pgO[	        U S5      eU" U5      U" U5      4$ )z
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)r   rA  rl   r  r7   )r_   r`   r   r   r   r   qrs           rd   r  	Poly.pdiv%  sV     xx{155&!!66!9DAq'6221vs1v~rh   c                    U R                  U5      u  p#pE[        U R                  S5      (       a  UR                  U5      nO[	        U S5      eU" U5      $ )a  
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)r   rA  rl   r  r7   r  s          rd   r  	Poly.prem<  sJ    < xx{155&!!VVAYF'6226{rh   c                    U R                  U5      u  p#pE[        U R                  S5      (       a  UR                  U5      nO[	        U S5      eU" U5      $ )aD  
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)r   rA  rl   r  r7   r  s          rd   r  	Poly.pquoc  sJ    & xx{155&!!VVAYF'6226{rh   c                .   U R                  U5      u  p#pE[        U R                  S5      (       a   UR                  U5      nO[        U S5      eU" U5      $ ! [         a3  nUR                  U R                  5       UR                  5       5      eSnAff = f)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)r   rA  rl   r  r>   r   r]   r7   )r_   r`   r   r   r   r   rb   excs           rd   r  Poly.pexquo  s    & xx{155(##8! (8446{ ' 8ggaiik199;778s   A 
B!.BBc                   U R                  U5      u  p4pVSnU(       aC  UR                  (       a2  UR                  (       d!  UR                  5       UR                  5       peSn[	        U R
                  S5      (       a  UR                  U5      u  pO[        U S5      eU(       a"   UR                  5       U	R                  5       pXpU" U5      U" U	5      4$ ! [         a     Nf = f)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'))

FTdiv)
r   is_Ringis_Fieldr   rA  rl   r  r7   r@  r9   )r_   r`   autor   r   r   r   rN  r  r  QRs               rd   r  Poly.div  s    " !!CKK::<qG155%  558DAq'511yy{AIIK1 11vs1v~ " s   $C 
C"!C"c                   U R                  U5      u  p4pVSnU(       aC  UR                  (       a2  UR                  (       d!  UR                  5       UR                  5       peSn[	        U R
                  S5      (       a  UR                  U5      nO[        U S5      eU(       a   UR                  5       nU" U5      $ ! [         a     Nf = f)a  
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')

FTrem)
r   r  r  r   rA  rl   r  r7   r@  r9   )	r_   r`   r  r   r   r   r   rN  r  s	            rd   r  Poly.rem      " !!CKK::<qG155%  aA'511IIK 1v "    "B: :
CCc                   U R                  U5      u  p4pVSnU(       aC  UR                  (       a2  UR                  (       d!  UR                  5       UR                  5       peSn[	        U R
                  S5      (       a  UR                  U5      nO[        U S5      eU(       a   UR                  5       nU" U5      $ ! [         a     Nf = f)a  
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')

FTquo)
r   r  r  r   rA  rl   r  r7   r@  r9   )	r_   r`   r  r   r   r   r   rN  r  s	            rd   r  Poly.quo  r  r  c                   U R                  U5      u  p4pVSnU(       aC  UR                  (       a2  UR                  (       d!  UR                  5       UR                  5       peSn[	        U R
                  S5      (       a   UR                  U5      nO[        U S5      eU(       a   UR                  5       nU" U5      $ ! [         a3  n	U	R                  U R                  5       UR                  5       5      eSn	A	ff = f! [         a     NTf = f)aX  
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

FTexquoN)r   r  r  r   rA  rl   r  r>   r   r]   r7   r@  r9   )
r_   r`   r  r   r   r   r   rN  r  r  s
             rd   r  
Poly.exquo  s    & !!CKK::<qG155'""8GGAJ (733IIK 1v ' 8ggaiik199;778 " s*   =B; #C; ;
C8.C33C8;
DDc                @   [        U[        5      (       aI  [        U R                  5      nU* Us=::  a  U:  a  O  OUS:  a  X!-   $ U$ [	        SU< SU< SU< 35      e U R                  R                  [        U5      5      $ ! [         a    [	        SU-  5      ef = f)z3Returns level associated with the given generator. r   -z
 <= gen < z expected, got z"a valid generator expected, got %s)rV   rR  r   rY   r<   r!  r%   r:  )r_   r   rl  s      rd   r1  Poly._gen_to_level9  s    c3[Fw#&&7!<'J%'-vs'< = =@vv||GCL11 @%83>@ @@s    #B Bc                    U R                  U5      n[        U R                  S5      (       a3  U R                  R                  U5      nUS:  a  [        R
                  nU$ [        U S5      e)a  
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   )r1  rA  rl   r  r   NegativeInfinityr7   )r_   r   r  ds       rd   r  Poly.degreeM  sX    ( OOC 155(##QA1u&&H'844rh   c                    [        U R                  S5      (       a  U R                  R                  5       $ [        U S5      e)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)rA  rl   r  r7   r  s    rd   r  Poly.degree_listk  s5     155-((55$$&&'=99rh   c                    [        U R                  S5      (       a  U R                  R                  5       $ [        U S5      e)z
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)rA  rl   r  r7   r  s    rd   r  Poly.total_degree~  s5      155.))55%%'''>::rh   c                   [        U[        5      (       d  [        S[        U5      -  5      eXR                  ;   a(  U R                  R                  U5      nU R                  nO%[        U R                  5      nU R                  U4-   n[        U R                  S5      (       a)  U R                  U R                  R                  U5      US9$ [        U S5      e)ab  
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
homogenizer   homogeneous_order)rV   r$   	TypeErrorrz   rY   r!  r   rA  rl   r   r  r7   )r_   sr   rY   s       rd   r  Poly.homogenize  s    , !V$$9DGCDD;QA66DAFFA66QD=D155,''55))!,4588#A':;;rh   c                    [        U R                  S5      (       a  U R                  R                  5       $ [        U S5      e)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  )rA  rl   r  r7   r  s    rd   r  Poly.homogeneous_order  s7    ( 155-..55**,,'+>??rh   c                    Ub  U R                  U5      S   $ [        U R                  S5      (       a  U R                  R                  5       nO[	        U S5      eU R                  R
                  R                  U5      $ )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

r   LC)r   rA  rl   r  r7   r   r   )r_   rn   rb   s      rd   r  Poly.LC  sc     88E?1%%155$UUXXZF'400uuyy!!&))rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R                  R                  R                  U5      $ )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)rA  rl   r  r7   r   r   rB  s     rd   r  Poly.TC  sJ     155$UUXXZF'400uuyy!!&))rh   c                x    [        U R                  S5      (       a  U R                  U5      S   $ [        U S5      e)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

r   EC)rA  rl   r   r7   rZ  s     rd   r  Poly.EC  s5     155(##88E?2&&'400rh   c                Z    U R                   " [        XR                  5      R                  6 $ )a}  
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

)nthr2   rY   	exponents)r_   r   s     rd   coeff_monomialPoly.coeff_monomial  s#    F uuhuff-7788rh   c                V   [        U R                  S5      (       a^  [        U5      [        U R                  5      :w  a  [	        S5      eU R                  R
                  " [        [        [        U5      5      6 nO[        U S5      eU R                  R                  R                  U5      $ )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

r  z,exponent of each generator must be specified)rA  rl   r   rY   r:  r  ry   r   rR  r7   r   r   )r_   Nrb   s      rd   r  Poly.nth3  sx    2 155%  1vQVV$ !OPPUUYYSa[ 12F'511uuyy!!&))rh   c                    [        S5      e)NzyEither convert to Expr with `as_expr` method to use Expr's coeff method or else use the `coeff_monomial` method of Polys.rs   )r_   r"  rT  rights       rd   r   
Poly.coeffU  s     "01 	1rh   c                R    [        U R                  U5      S   U R                  5      $ )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

r   r2   r   rY   rZ  s     rd   LMPoly.LMa  s"    $ *AFF33rh   c                R    [        U R                  U5      S   U R                  5      $ )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

r  r	  rZ  s     rd   EMPoly.EMu  s"     +QVV44rh   c                \    U R                  U5      S   u  p#[        X R                  5      U4$ )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)

r   r3  r2   rY   r_   rn   r   r   s       rd   LTPoly.LT  s,    $ wwu~a(vv&--rh   c                \    U R                  U5      S   u  p#[        X R                  5      U4$ )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)

r  r  r  s       rd   ETPoly.ET  s,     wwu~b)vv&--rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R                  R                  R                  U5      $ )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)rA  rl   r  r7   r   r   rB  s     rd   r  Poly.max_norm  sK     155*%%UU^^%F':66uuyy!!&))rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R                  R                  R                  U5      $ )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)rA  rl   r  r7   r   r   rB  s     rd   r  Poly.l1_norm  J     155)$$UU]]_F'955uuyy!!&))rh   c                    U nUR                   R                  R                  (       d  [        R                  U4$ UR                  5       nUR                  (       a$  UR                   R                  R                  5       n[        UR                   S5      (       a  UR                   R                  5       u  pEO[        US5      eUR                  U5      UR                  U5      p$U(       a  UR                  (       d  XB4$ XBR                  5       4$ )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)rl   r   r  r   Oner   has_assoc_Ringget_ringrA  r  r7   r   r   r@  )r   r   r_   r   r   rb   s         rd   r  Poly.clear_denoms  s    $ uuyy!!55!8Olln%%))$$&C155.))EE..0ME6'>::<<&fqc008O))+%%rh   c                &   U nUR                  U5      u  p4p!U" U5      nU" U5      nUR                  (       a  UR                  (       d  X!4$ UR                  SS9u  pRUR                  SS9u  paUR	                  U5      nUR	                  U5      nX!4$ )a2  
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]')

Tr   )r   r  r!  r  r  )r   r`   r_   r   r   abs          rd   rat_clear_denomsPoly.rat_clear_denoms  s    * !!FF!3!34K~~d~+~~d~+LLOLLOtrh   c                $   U nUR                  SS5      (       a5  UR                  R                  R                  (       a  UR	                  5       n[        UR                  S5      (       a  U(       d(  UR                  UR                  R                  SS95      $ UR                  nU HJ  n[        U[        5      (       a  Uu  pgOUSpvUR                  [        U5      UR                  U5      5      nML     UR                  U5      $ [        US5      e)a2  
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')

r  T	integrater   rS  )getrl   r   r  r   rA  r   r+  rV   rh  rR  r1  r7   )r   specsr   r_   rl   specr   rS  s           rd   r+  Poly.integrate#	  s    " 88FD!!aeeii&7&7

A155+&&uuQUU__q_122%%CdE**!FC!1mmCFAOOC,@A  55:';77rh   c                   UR                  SS5      (       d  [        U /UQ70 UD6$ [        U R                  S5      (       a  U(       d(  U R	                  U R                  R                  SS95      $ U R                  nU HJ  n[        U[        5      (       a  Uu  pVOUSpeUR                  [        U5      U R                  U5      5      nML     U R	                  U5      $ [        U S5      e)a  
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')

rp   Tdiffr   r,  )r-  r   rA  rl   r   r2  rV   rh  rR  r1  r7   )r_   r.  r(  rl   r/  r   rS  s          rd   r2  	Poly.diffK	  s    " zz*d++a2%2622155&!!uuQUUZZ!Z_--%%CdE**!FC!1hhs1vqs';<  55:'622rh   c                   U nUc  [        U[        5      (       a.  UnUR                  5        H  u  pgUR                  Xg5      nM     U$ [        U[        [
        45      (       aa  Un[        U5      [        UR                  5      :  a  [        S5      e[        UR                  U5       H  u  pgUR                  Xg5      nM     U$ SUp)OUR                  U5      n	[        UR                  S5      (       d  [        US5      e UR                  R                  X)5      n
UR-                  XS9$ ! [         a    U(       d'  [        SU< SUR                  R                   < 35      e[#        U/5      u  nu  nUR%                  5       R'                  XR                  5      nUR)                  U5      nUR+                  X+5      nUR                  R                  X)5      n
 Nf = f)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')

ztoo many values providedr   r  zCannot evaluate at r  r   )rV   rv   r   r  rh  ry   r   rY   r:  r   r1  rA  rl   r7   r9   r8   r   r+   r   unify_with_symbolsr   r   r   )r   r"  r&  r  r_   rv  r   rw  valuesr  rb   a_domain
new_domains                rd   r  	Poly.evals	  s   > 9!T""")--/JCs*A #2 At}--v;QVV,$%?@@"%afff"5JCs*A #6 !1"Aquuf%%'622	*UUZZ%F uuVu&&  
	*!1aeeii"PQQ 0! 5#1\\^>>xP
LL,&&q3A)
	*s   D, ,B/GGc                $    U R                  U5      $ )a  
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_   r7  s     rd   __call__Poly.__call__	  s    ( vvf~rh   c                .   U R                  U5      u  p4pVU(       a0  UR                  (       a  UR                  5       UR                  5       pe[        U R                  S5      (       a  UR                  U5      u  pxO[        U S5      eU" U5      U" U5      4$ )ay  
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)r   r  r   rA  rl   r?  r7   )	r_   r`   r  r   r   r   r   r  hs	            rd   r?  Poly.half_gcdex	  sr    & !!CKK::<q155,''<<?DAq'<881vs1v~rh   c                >   U R                  U5      u  p4pVU(       a0  UR                  (       a  UR                  5       UR                  5       pe[        U R                  S5      (       a  UR                  U5      u  pxn	O[        U S5      eU" U5      U" U5      U" U	5      4$ )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)r   r  r   rA  rl   rC  r7   )
r_   r`   r  r   r   r   r   r  tr@  s
             rd   rC  
Poly.gcdex	  sy    * !!CKK::<q155'""ggajGA!'7331vs1vs1v%%rh   c                   U R                  U5      u  p4pVU(       a0  UR                  (       a  UR                  5       UR                  5       pe[        U R                  S5      (       a  UR                  U5      nO[        U S5      eU" U5      $ )a2  
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)r   r  r   rA  rl   rG  r7   )r_   r`   r  r   r   r   r   rb   s           rd   rG  Poly.invert
  sf    & !!CKK::<q155(##XXa[F'8446{rh   c                    [        U R                  S5      (       a%  U R                  R                  [        U5      5      nO[	        U S5      eU R                  U5      $ )a  
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)rA  rl   rJ  rR  r7   r   r  s      rd   rJ  Poly.revert3
  sF    6 155(##UU\\#a&)F'844uuV}rh   c                    U R                  U5      u  p#pE[        U R                  S5      (       a  UR                  U5      nO[	        U S5      e[        [        X65      5      $ )a  
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)r   rA  rl   rM  r7   ry   r   r  s          rd   rM  Poly.subresultantsU
  sQ      xx{155/**__Q'F'?;;C$%%rh   c                "   U R                  U5      u  p4pV[        U R                  S5      (       a+  U(       a  UR                  XbS9u  pxOUR                  U5      nO[	        U S5      eU(       a  U" USS9[        [        UW5      5      4$ U" USS9$ )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
includePRSr   r5  )r   rA  rl   rP  r7   ry   r   )	r_   r`   rR  r   r   r   r   rb   r  s	            rd   rP  Poly.resultantn
  s    . xx{155+&&KKKA	Q';77q)4C+<==6!$$rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  USS9$ )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

discriminantr   r5  )rA  rl   rU  r7   r   rB  s     rd   rU  Poly.discriminant
  sG     155.))UU'')F'>::uuVAu&&rh   c                    SSK Jn  U" X5      $ )aY  Compute 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<sqrt(5)>')
>>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
>>> 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.dispersionrX  )r_   r`   rX  s      rd   rX  Poly.dispersionset
  s    P 	9Q""rh   c                    SSK Jn  U" X5      $ )aG  Compute 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<sqrt(5)>')
>>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
>>> 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)rY  r\  )r_   r`   r\  s      rd   r\  Poly.dispersion
  s    P 	6!rh   c                    U R                  U5      u  p#pE[        U R                  S5      (       a  UR                  U5      u  pgnO[	        U S5      eU" U5      U" U5      U" U5      4$ )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)r   rA  rl   r_  r7   )	r_   r`   r   r   r   r   r@  cffcfgs	            rd   r_  Poly.cofactors>  s]    ( xx{155+&&++a.KAC';771vs3xS))rh   c                    U R                  U5      u  p#pE[        U R                  S5      (       a  UR                  U5      nO[	        U S5      eU" U5      $ )z
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)r   rA  rl   rd  r7   r  s          rd   rd  Poly.gcd[  J     xx{155%  UU1XF'5116{rh   c                    U R                  U5      u  p#pE[        U R                  S5      (       a  UR                  U5      nO[	        U S5      eU" U5      $ )z
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)r   rA  rl   rh  r7   r  s          rd   rh  Poly.lcmr  rf  rh   c                    U R                   R                  R                  U5      n[        U R                   S5      (       a  U R                   R	                  U5      nO[        U S5      eU R                  U5      $ )z
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)rl   r   r   rA  rk  r7   r   )r_   prb   s      rd   rk  
Poly.trunc  sY     EEIIa 155'""UU[[^F'733uuV}rh   c                $   U nU(       a5  UR                   R                  R                  (       a  UR                  5       n[	        UR                   S5      (       a  UR                   R                  5       nO[        US5      eUR                  U5      $ )a*  
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)rl   r   r  r   rA  ro  r7   r   r   r  r_   rb   s       rd   ro  
Poly.monic  sb    " AEEII%%

A155'""UU[[]F'733uuV}rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R                  R                  R                  U5      $ )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)rA  rl   rs  r7   r   r   rB  s     rd   rs  Poly.content  r  rh   c                    [        U R                  S5      (       a  U R                  R                  5       u  pO[        U S5      eU R                  R                  R                  U5      U R                  U5      4$ )z
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)rA  rl   rv  r7   r   r   r   )r_   contrb   s      rd   rv  Poly.primitive  s\     155+&&55??,LD&';77uuyy!!$'v66rh   c                    U R                  U5      u  p#pE[        U R                  S5      (       a  UR                  U5      nO[	        U S5      eU" U5      $ )z
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)r   rA  rl   rz  r7   r  s          rd   rz  Poly.compose  sJ     xx{155)$$YYq\F'9556{rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      e[	        [        U R                  U5      5      $ )z
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)rA  rl   r}  r7   ry   r   r   rB  s     rd   r}  Poly.decompose  sH     155+&&UU__&F';77Cv&''rh   c                V    U R                  U R                  R                  U5      5      $ )a  
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.
)r   rl   shiftr_   r&  s     rd   r  
Poly.shift  s    $ uuQUU[[^$$rh   c                V    U R                  U R                  R                  U5      5      $ )a  
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.
)r   rl   
shift_listr  s     rd   r  Poly.shift_list'  s"    $ uuQUU%%a())rh   c                F   UR                  U5      u  p4U R                  U5      u  pSUR                  U5      u  pT[        UR                  S5      (       a1  UR                  R                  UR                  UR                  5      nO[	        US5      eUR                  U5      $ )z
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)r   rA  rl   r  r7   r   )r_   rl  r  Pr  r   rb   s          rd   r  Poly.transform;  sy     wwqzwwqzwwqz155+&&UU__QUUAEE2F';77uuV}rh   c                @   U nU(       a5  UR                   R                  R                  (       a  UR                  5       n[	        UR                   S5      (       a  UR                   R                  5       nO[        US5      e[        [        UR                  U5      5      $ )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)
rl   r   r  r   rA  r  r7   ry   r   r   rp  s       rd   r  
Poly.sturmU  sj    " AEEII%%

A155'""UU[[]F'733Cv&''rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU VVs/ s H  u  p#U R	                  U5      U4PM     snn$ s  snnf )a  
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)rA  rl   r  r7   r   )r_   rb   r`   r  s       rd   r  Poly.gff_listr  sX      155*%%UU^^%F':66*01&$!q1&111s   A+c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )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)rA  rl   r  r7   r   )r_   r  s     rd   r  	Poly.norm  s>    8 155&!!

A'622uuQxrh   c                    [        U R                  S5      (       a  U R                  R                  5       u  pnO[        U S5      eXR	                  U5      U R	                  U5      4$ )a  
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<sqrt(3)>')
>>> r
Poly(x**4 - 4*x**2 + 16, x, domain='QQ')

sqf_norm)rA  rl   r  r7   r   )r_   r  r`   r  s       rd   r  Poly.sqf_norm  sQ    0 155*%%eenn&GA!':66%%(AEE!H$$rh   c                    [        U R                  S5      (       a  U R                  R                  5       nO[        U S5      eU R	                  U5      $ )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)rA  rl   r  r7   r   rB  s     rd   r  Poly.sqf_part  rF  rh   c                2   [        U R                  S5      (       a  U R                  R                  U5      u  p#O[        U S5      eU R                  R                  R                  U5      U VVs/ s H  u  pEU R                  U5      U4PM     snn4$ s  snnf )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)rA  rl   r  r7   r   r   r   )r_   allr   factorsr`   r  s         rd   r  Poly.sqf_list  sv    , 155*%%UU^^C0NE7':66uuyy!!%(W*MWTQAEE!Ha=W*MMM*Ms   /Bc                    [        U R                  S5      (       a  U R                  R                  U5      nO[        U S5      eU VVs/ s H  u  p4U R	                  U5      U4PM     snn$ s  snnf )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)rA  rl   r  r7   r   )r_   r  r  r`   r  s        rd   r  Poly.sqf_list_include  s^    4 155,--ee,,S1G'+=>>*12'$!q1'222s   	A,c                   [        U R                  S5      (       a   U R                  R                  5       u  pO[        U S5      eU R                  R                  R                  U5      U VVs/ s H  u  p4U R                  U5      U4PM     snn4$ ! [         a@    U R	                  5       S:X  a  U R                  5       / 4s $ [        R                  U S4/4s $ f = fs  snnf )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_listr   r   )rA  rl   r  r8   r  r]   r   r   r7   r   r   r   )r_   r   r  r`   r  s        rd   r  Poly.factor_list  s    " 155-((+!"!2!2!4w (=99uuyy!!%(W*MWTQAEE!Ha=W*MMM  +88:?99;?*55Aq6(?*	+ +Ns   B /C 0CCCc                   [        U R                  S5      (       a   U R                  R                  5       nO[	        U S5      eU VVs/ s H  u  p#U R                  U5      U4PM     snn$ ! [         a    U S4/s $ f = fs  snnf )a1  
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   )rA  rl   r  r8   r7   r   )r_   r  r`   r  s       rd   r  Poly.factor_list_include<  s    " 155/00 %%335 (+@AA*12'$!q1'22   Ax 
 3s   A, 	B,A>=A>c           	        Ub'  [         R                  " U5      nUS::  a  [        S5      eUb  [         R                  " U5      nUb  [         R                  " U5      n[        U R                  S5      (       a  U R                  R                  XX4XVS9nO[        U S5      eU(       aM  S nU(       d  [        [        X5      5      $ S n	Uu  p[        [        X5      5      [        [        X5      5      4$ S nU(       d  [        [        X5      5      $ S n	Uu  p[        [        X5      5      [        [        X5      5      4$ )	aA  
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	intervalsr  epsinfsupfastsqfc                b    U u  p[         R                  " U5      [         R                  " U5      4$ r   r-   r   )intervalr  rD  s      rd   _realPoly.intervals.<locals>._real  s$    AA77rh   c                    U u  u  pu  p4[         R                  " U5      [        [         R                  " U5      -  -   [         R                  " U5      [        [         R                  " U5      -  -   4$ r   r-   r   r   )	rectangleuvr  rD  s        rd   _complex Poly.intervals.<locals>._complex  sS    !*A2;;q>)99A2;;q>)99; ;rh   c                l    U u  u  pn[         R                  " U5      [         R                  " U5      4U4$ r   r  )r  r  rD  r  s       rd   r  r    s-    $	QQ8!<<rh   c                    U u  u  u  pu  p4n[         R                  " U5      [        [         R                  " U5      -  -   [         R                  " U5      [        [         R                  " U5      -  -   4U4$ r   r  )r  r  r  r  rD  r  s         rd   r  r    sc    &/# !!!Q!BKKN*::Q!BKKN*::<=>@ @rh   )	r-   r   r:  rA  rl   r  r7   ry   r   )r_   r  r  r  r  r  r  rb   r  r  	real_partcomplex_parts               rd   r  Poly.intervalsW  s    4 ?**S/Cax !DEE?**S/C?**S/C155+&&UU__c % HF (;778 C.//;
 '-#IE-.S5P0QQQ= C.//@
 '-#IE-.S5P0QQQrh   c                   U(       a  U R                   (       d  [        S5      e[        R                  " U5      [        R                  " U5      p!Ub'  [        R                  " U5      nUS::  a  [	        S5      eUb  [        U5      nOUc  Sn[        U R                  S5      (       a  U R                  R                  XX4US9u  pxO[        U S5      e[        R                  " U5      [        R                  " U5      4$ )z
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 supportedr   r  r   refine_root)r  stepsr  )is_sqfr<   r-   r   r:  rR  rA  rl   r  r7   r   )	r_   r  rD  r  r  r  	check_sqfr   Ts	            rd   r  Poly.refine_root  s     QXX!"JKKzz!}bjjm1?**S/Cax !DEEJE[E155-((55$$Qsd$KDAq'=99{{1~r{{1~--rh   c                   Su  p4Ubv  [        U5      nU[        R                  L a  SnOUUR                  5       u  pVU(       d  [        R
                  " U5      nO%[        [        [        R
                  XV45      5      Sp1Ubv  [        U5      nU[        R                  L a  SnOUUR                  5       u  pVU(       d  [        R
                  " U5      nO%[        [        [        R
                  XV45      5      SpBU(       aH  U(       aA  [        U R                  S5      (       a  U R                  R                  XS9nO[        U S5      eU(       a  Ub  U[        R                  4nU(       a  Ub  U[        R                  4n[        U R                  S5      (       a  U R                  R                  XS9nO[        U S5      e[        U5      $ )z
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

TTNFcount_real_rootsr  r  count_complex_roots)r%   r   r  as_real_imagr-   r   ry   r   InfinityrA  rl   r  r7   r   r  r   )r_   r  r  inf_realsup_realreimcounts           rd   count_rootsPoly.count_roots  sc     (?#,Ca((())+**S/C$(RZZ")B$CU?#,Cajj ))+**S/C$(RZZ")B$CUquu011..3.@+A/ABBCOBGGnCOBGGnquu34411c1C+A/DEEu~rh   c                R    [         R                  R                  R                  XUS9$ )as  
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)sympypolysrootoftoolsrootof)r_   r!  r  s      rd   root	Poly.root  s$    6 {{&&--a-JJrh   c                    [         R                  R                  R                  R	                  XS9nU(       a  U$ [        USS9$ )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)]
r  Fmultiple)r  r  r  CRootOf
real_rootsrJ   )r_   r  r  realss       rd   r  Poly.real_roots$  s<    " ''//::1:PL//rh   c                    [         R                  R                  R                  R	                  XS9nU(       a  U$ [        USS9$ )ai  
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)]

r  Fr  )r  r  r  r  	all_rootsrJ   )r_   r  r  rootss       rd   r  Poly.all_roots<  s<    ( ''//99!9OL//rh   c                  ^
 U R                   (       a  [        SU -  5      eU R                  5       S::  a  / $ U R                  R                  [
        L a*  U R                  5        Vs/ s H  n[        U5      PM     nnOU R                  R                  [        L a]  U R                  5        Vs/ s H  oDR                  PM     nn[        U6 nU R                  5        Vs/ s H  n[        XG-  5      PM     nnOU R                  5        Vs/ s H   nUR                  US9R                  5       PM"     nn[        R                  " U5          U Vs/ s H  n[        R                  " U6 PM     nn SSS5        [        R$                  R&                  nU[        R$                  l        SSKJm
   [        R,                  " XRUSU R                  5       S-  S	9n	[/        [1        [2        [5        U	U
4S
 jS95      5      n	U[        R$                  l        U	$ s  snf s  snf s  snf s  snf s  snf ! [          a#    [#        SU R                  R                  -  5      ef = f! , (       d  f       N= f! [6         ar     [        R,                  " XRUSU R                  5       S-  S	9n	[/        [1        [2        [5        U	U
4S jS95      5      n	 N! [6         a    [7        SU< SU< 35      ef = ff = f! U[        R$                  l        f = f)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   )rT  z!Numerical domain expected, got %sN)signF
   )maxstepscleanuperror	extraprecc                   > U R                   (       a  SOSU R                  [        U R                   5      T" U R                   5      4$ Nr   r   imagrealr  r  r  s    rd   <lambda>Poly.nroots.<locals>.<lambda>  s/    !&&QaQVVVZ[\[a[aVb,crh   key   c                   > U R                   (       a  SOSU R                  [        U R                   5      T" U R                   5      4$ r  r  r  s    rd   r  r    s2    aff!QVVSQRQWQW[Z^_`_e_eZf0grh   z$convergence to root failed; try n < z or maxsteps > )is_multivariater=   r  rl   r   r.   r_  rR  r-   r  r   r   r  mpmathworkdpsmpcr  r8   mpdps$sympy.functions.elementary.complexesr  	polyrootsry   r   r%   sortedrP   )r_   rT  r  r  r   r   denomsfacr  r  r  s             @rd   nrootsPoly.nrootsW  s   2 -6:< < 88:?I
 5599?./lln=nUc%jnF=FUUYY"_+,<<>:>%gg>F:-C23,,.A.c%)n.FAF "#1!/ kkAk&335!/  1"'>DEfUfjj%0fFEF # iimm		=	  $$V#5AHHJrMKE
 Wu"cdf gE  FIIMW >:A1 F  '%&IEEII'& ' '' #"*  
	"	"((#5AHHJrMKS5&ghj k  "#x!" ""
	"  FIIMsz   &H-HH
'H 	IH*H%,H*-AI+ %H**-II
I(+
K'6AKK* K##K''K* *Lc                    U R                   (       a  [        SU -  5      e0 nU R                  5       S    H2  u  p#UR                  (       d  M  UR	                  5       u  pEX1U* U-  '   M4     U$ )z
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   )r  r=   r  	is_linearr_  )r_   r  factorr  r&  r'  s         rd   ground_rootsPoly.ground_roots  sp     -3a79 9 +IF((*qbd ,
 rh   c                b   U R                   (       a  [        S5      e[        U5      nUR                  (       a  US:  a  [	        U5      nO[        SU-  5      eU R                  n[        S5      nU R                  U R                  R                  X1-  U-
  X45      5      nUR                  XC5      $ )a  
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')

zmust be a univariate polynomialr   z&'n' must an integer and n >= 1, got %srD  )r  r=   r%   
is_IntegerrR  r:  r   r#   rP  r   rX   r  )r_   rT  r  r"  rD  r  s         rd   nth_power_roots_polyPoly.nth_power_roots_poly  s    , -13 3 AJ<<AFAAEIJJEE#JKK--adQh=>yyrh   c                   U R                   (       a  [        S5      eU R                  5       nUR                  (       d0  UR                  (       d  UR
                  (       d  [        SU-  5      eU R                  XR                  5       5      $ )aN  
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)	r  r=   r   is_ZZis_QQis_AlgebraicFieldrs   _which_rootsr  )r_   
candidatesr   s      rd   which_real_rootsPoly.which_real_roots  sl    j -13 3 lln		SYY#*?*?%5;= = ~~j--/::rh   c                x    U R                   (       a  [        S5      eU R                  XR                  5       5      $ )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  )r  r=   r  r  )r_   r  s     rd   which_all_rootsPoly.which_all_roots,  s5    r -13 3 ~~j((*55rh   c                b   Sn[        U5      n[        U5      U:  a{  [        UR                  5       5       HH  nU " U5      R	                  USU-  S9n[        U5      R                  S:  d  M7  UR                  U5        MJ     US-  n[        U5      U:  a  M{  [        UR                  5       5      $ )Nr     )maxn)r	   r   ry   keysr   r  _precpop)r_   r  	num_rootsprecroot_countsr  f_rs          rd   r  Poly._which_rootsk  s    j)+*+**,-djjAdFj3s8>>Q&OOA&	 . AID +* K$$&''rh   c                  ^ U R                   (       a  [        S5      eU R                  R                  5       nU R                  R                  5       R                  U5      nUS-  n[        SU-  S0 5      u  n    n[        U5      nUS-  US-  -   mU4S jn	U	" U5      U	" U5      pU
R                  UR                  -
  S-  U
R                  UR                  -
  S-  -   U:  $ )aL  
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  	   r   r   c                *   > [        [        U TS95      $ )Nr,  )r   r   )r"  rS  s    rd   r   Poly.same_root.<locals>.<lambda>  s    ~&?Q&GHrh   )r  r=   rl   mignotte_sep_bound_squaredrS   	get_fieldr   r   r   r  r  )r_   r&  r'  dom_delta_sqdelta_sqeps_sqr  r   rT  evABrS  s               @rd   	same_rootPoly.same_root{  s    > -13 3 uu77988%%'00> A1V8Q+
1aAJ!VAH !ube1!#qvv&::VCCrh   c                p   U R                  U5      u  p4pV[        US5      (       a  UR                  XbS9nO[        U S5      eU(       d[  UR                  (       a  UR                  5       nUu  ppUR                  U5      nUR                  U	5      n	X-  U" U
5      U" U5      4$ [        [        XG5      5      $ )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)	r   rA  r9  r7   r!  r"  r   rh  r   )r_   r`   r:  r   r   r   r   rb   cpcqrl  r  s               rd   r9  Poly.cancel  s    " !!1hXXaX1F'844!!lln!LBAb!Bb!B5#a&#a&((S)**rh   c                   U R                   (       a  U R                  [        [        4;  a  [	        S5      eU R
                  (       a&  U R                  [        :X  a  U [        R                  4$ U R                  5       nUR                  5       u  p#UR                  [        UR                  5      U5      R                  5       U4$ )a  
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.)r   rS   r.   r-   r:  is_monicr   ro  r  r  rW   r   r@  )r_   fmr   r   s       rd   )make_monic_over_integers_by_scaling_roots.Poly.make_monic_over_integers_by_scaling_roots  s    < !((2r(":KLL::!((b.bff9B??$DA<<RVVa088:A==rh   c                0   SSK JnJnJnJn  U R
                  (       a+  U R                  (       a  U R                  [        [        4;  a  [        S5      eUUUUS.n[        UR                  5       5      n	U R                  5       n
X:  a  [        SU	 S35      eU
S:  a  [        S5      eU
S:X  a  SS	KJn  UR                   S
pO8U
S:X  a  SSKJn  UR$                  SpOU R'                  5       u  nnX   " XUS9u  pU(       a  UOUR)                  5       nUU4$ )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<Polynomial must be irreducible and univariate over ZZ or QQ.)            zOnly polynomials up to degree z are supported.r   z(Constant polynomial has no Galois group.)S1TransitiveSubgroupsTr   )S2TransitiveSubgroupsF)	max_tries	randomize)%sympy.polys.numberfields.galoisgroupsrD  rE  rF  rG  r   is_irreduciblerS   r.   r-   r:  maxr"  r  sympy.combinatorics.galoisrL  S1rM  S2rA  get_perm_group)r_   by_namerN  rO  rD  rE  rF  rG  ggmax_supportedrT  rL  namealtrM  r`   r   r   s                     rd   galois_groupPoly.galois_group  s   (	
 	

 ##xxBx'[\\%,7,	
 BGGIHHJ=m_O\]]UGHH!VH-00$#!VH-00%#>>@DAqa	JIDD!4!4!6#vrh   c                .    U R                   R                  $ )z
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

)rl   is_zeror  s    rd   r_  Poly.is_zero5  s    " uu}}rh   c                .    U R                   R                  $ )z
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

)rl   is_oner  s    rd   rb  Poly.is_oneH      " uu||rh   c                .    U R                   R                  $ )z
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

)rl   r  r  s    rd   r  Poly.is_sqf[  rd  rh   c                .    U R                   R                  $ )z
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

)rl   r?  r  s    rd   r?  Poly.is_monicn  s    " uu~~rh   c                .    U R                   R                  $ )z
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

)rl   is_primitiver  s    rd   rj  Poly.is_primitive      " uu!!!rh   c                .    U R                   R                  $ )z
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

)rl   	is_groundr  s    rd   rn  Poly.is_ground  s    & uurh   c                .    U R                   R                  $ )z
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

)rl   r  r  s    rd   r  Poly.is_linear  s    " uurh   c                .    U R                   R                  $ )z
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

)rl   is_quadraticr  s    rd   rs  Poly.is_quadratic  rl  rh   c                .    U R                   R                  $ )z
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

)rl   is_monomialr  s    rd   rv  Poly.is_monomial  s    " uu   rh   c                .    U R                   R                  $ )a  
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

)rl   is_homogeneousr  s    rd   ry  Poly.is_homogeneous  s    , uu###rh   c                .    U R                   R                  $ )z
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

)rl   rQ  r  s    rd   rQ  Poly.is_irreducible  s    " uu###rh   c                2    [        U R                  5      S:H  $ )aK  
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

r   r   rY   r  s    rd   r   Poly.is_univariate      * 166{arh   c                2    [        U R                  5      S:g  $ )aU  
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

r   r~  r  s    rd   r  Poly.is_multivariate$  r  rh   c                .    U R                   R                  $ )a3  
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

)rl   is_cyclotomicr  s    rd   r  Poly.is_cyclotomic;  s    , uu"""rh   c                "    U R                  5       $ r   )r  r  s    rd   __abs__Poly.__abs__S      uuwrh   c                "    U R                  5       $ r   )r  r  s    rd   __neg__Poly.__neg__V  r  rh   c                $    U R                  U5      $ r   r9  r_   r`   s     rd   __add__Poly.__add__Y      uuQxrh   c                $    UR                  U 5      $ r   r  r  s     rd   __radd__Poly.__radd__]  r  rh   c                $    U R                  U5      $ r   r  r  s     rd   __sub__Poly.__sub__a  r  rh   c                $    UR                  U 5      $ r   r  r  s     rd   __rsub__Poly.__rsub__e  r  rh   c                $    U R                  U5      $ r   r   r  s     rd   __mul__Poly.__mul__i  r  rh   c                $    UR                  U 5      $ r   r   r  s     rd   __rmul__Poly.__rmul__m  r  rh   rT  c                ^    UR                   (       a  US:  a  U R                  U5      $ [        $ )Nr   )r  r  r[   )r_   rT  s     rd   __pow__Poly.__pow__q  s"    <<AF558O!!rh   c                $    U R                  U5      $ r   r  r  s     rd   
__divmod__Poly.__divmod__x  r  rh   c                $    UR                  U 5      $ r   r  r  s     rd   __rdivmod__Poly.__rdivmod__|  r  rh   c                $    U R                  U5      $ r   r  r  s     rd   __mod__Poly.__mod__  r  rh   c                $    UR                  U 5      $ r   r  r  s     rd   __rmod__Poly.__rmod__  r  rh   c                $    U R                  U5      $ r   r  r  s     rd   __floordiv__Poly.__floordiv__  r  rh   c                $    UR                  U 5      $ r   r  r  s     rd   __rfloordiv__Poly.__rfloordiv__  r  rh   r`   c                D    U R                  5       UR                  5       -  $ r   r]   r  s     rd   __truediv__Poly.__truediv__      yy{199;&&rh   c                D    UR                  5       U R                  5       -  $ r   r  r  s     rd   __rtruediv__Poly.__rtruediv__  r  rh   otherc                v   Xp2UR                   (       d)   UR                  X2R                  UR                  5       S9nUR                  UR                  :w  a  gUR                  R                  UR                  R                  :w  a  gUR                  UR                  :H  $ ! [        [
        [        4 a     gf = fNrM  F)	r{   r   rY   r   r<   r8   r9   rl   r   )r   r  r_   r`   s       rd   __eq__Poly.__eq__  s    1yyKK66!,,.KA 66QVV5599		!uu~ $[.A s   (B   B87B8c                    X:X  + $ r   r+  r  s     rd   __ne__Poly.__ne__  s
    zrh   c                $    U R                   (       + $ r   )r_  r  s    rd   __bool__Poly.__bool__  s    99}rh   c                L    U(       d  X:H  $ U R                  [        U5      5      $ r   )
_strict_eqr%   r_   r`   stricts      rd   eqPoly.eq  s    6M<<
++rh   c                *    U R                  XS9(       + $ )Nr  )r  r  s      rd   nePoly.ne  s    444)))rh   c                    [        XR                  5      =(       aD    U R                  UR                  :H  =(       a$    U R                  R	                  UR                  SS9$ NTr  )rV   r   rY   rl   r  r  s     rd   r  Poly._strict_eq  s>    ![[)_aff.>_15588AEEZ^8C__rh   r+  )returnrW   NNr   )FF)FTr   )r   FNT)r  z#tuple[Expr, list[tuple[Poly, int]]])FNNNFFNNFFr  r  2   T)F   F)r^   
__module____qualname____firstlineno____doc__	__slots__is_commutativer{   _op_priority__annotations__r   classmethodr   propertyr   r   r   r   r   r   rX   rw   rx   r|   r}   rt   r   r   r   r   rS   r   r   r   r   r   r   r   r
  r  r  ro   r  r'  r   r4  r=  r@  r   rH  rN  rQ  r   r   r3  r_  rb  re  ri  rl  r0  rs  r]   r{  r~  r  r  r  r  r  r  r  r  r  r  r  r9  r  r   r  r  r  r  r  r  r  r  r  r  r1  r  r  r  r  r  r  r  r  r  r  r   r
  r  r  r  r  r  r  r(  r+  r2  _eval_derivativer  r<  r?  rC  rG  rJ  rM  rP  rU  rX  r\  r_  rd  rh  rk  ro  rs  rv  rz  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r6  r9  rA  r\  r_  rb  r  r?  rj  rn  r  rs  rv  ry  rQ  r   r  r  r  r  rg   r  r  r  r  r  r  r   r[   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __static_attributes____classcell__r   s   @rd   rW   rW   j   s   3j  INGL	H
0>   E E ( (' ( (
 ( (
 ( (
 ( (
 A A& A A*  , ( (
 A A" 5 5<  :    ! !, O O N N83j+:0
)"J**%&!MF2N4"LH D***40 J,)(Q(C "(J #=J  2&)$=L4* *#$J(%T ****04*0>>>04.%N8>%N#J#J(T@(5<:&;* <D@2*0**1&#9J *D
14(5 .*."****%&N%N&8P$3L I'V,>&B> D&2#%J'*I#VI V*:...:**7*.(*%(*(4(:2.!F%>*N:3BN<36JRX#.J=~K:0006Ob6&P?;B=6~( 6Dp#+J%>N4l  $  $  $  $ " "$  (  $ " "$ ! !$ $ $. $ $$    ,    , # #.             ^$" %"             ^$' %' ^$' %' ( )" ^$ %,*` `rh   rW   c                  l   ^  \ rS rSrSrS rU 4S jr\S 5       r\	" S\
5      S 5       rS rS	 rS
rU =r$ )PurePolyi  z)Class for representing pure polynomials. c                    U R                   4$ )z$Allow SymPy to hash Poly instances. )rl   r   s    rd   r   PurePoly._hashable_content  s    {rh   c                    > [         TU ]  5       $ r   r   r   s    rd   r   PurePoly.__hash__  r   rh   c                    U R                   $ )z
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}

)r   r   s    rd   r   PurePoly.free_symbols  s    & ***rh   r  c                   Xp2UR                   (       d)   UR                  X2R                  UR                  5       S9n[        UR                  5      [        UR                  5      :w  a  gUR                  R                  UR                  R                  :w  ag   UR                  R                  R                  UR                  R                  UR                  5      nUR                  U5      nUR                  U5      nUR                  UR                  :H  $ ! [        [
        [        4 a     gf = f! [         a     gf = fr  )r{   r   rY   r   r<   r8   r9   r   rl   r   r   r:   r   )r   r  r_   r`   r   s        rd   r  PurePoly.__eq__  s    1yyKK66!,,.KA qvv;#aff+%5599		!eeiiooaeeii8 S!AS!Auu~ $[.A  % s$   (D AD3 D0/D03
E ?E c                    [        XR                  5      =(       a$    U R                  R                  UR                  SS9$ r  )rV   r   rl   r  r  s     rd   r  PurePoly._strict_eq  s+    ![[)JaeehhquuTh.JJrh   c                  ^ [        U5      nUR                  (       dk   U R                  R                  U R                  U R                  U R                  R	                  U R                  R                  R                  U5      5      4$ [        U R                  5      [        UR                  5      :w  a  [        SU < SU< 35      e[        U R                  [        5      (       a  [        UR                  [        5      (       d  [        SU < SU< 35      eU R                  mU R                  nU R                  R                  R                  UR                  R                  U5      nU R                  R                  U5      nUR                  R                  U5      nX2S 4U4S jjnX6XE4$ ! [         a    [        SU < SU< 35      ef = f)Nr   r   c                z   > Ub%  US U X#S-   S  -   nU(       d  UR                  U 5      $ TR                  " U /UQ76 $ r   r   r   s       rd   r   PurePoly._unify.<locals>.per  r   rh   )r%   r{   rl   r   r   r   r9   r:   r   rY   rV   r4   r   r   r   )r_   r`   rY   r   r   r   r   r   s          @rd   r   PurePoly._unify  sB   AJyyLuuyy!%%		!%%)):N:Nq:Q0RRR qvv;#aff+%#A$FGG155#&&:aeeS+A+A#A$FGGkkvveeiiooaeeii.EEMM#EEMM# 	' ~5 " L'Q(JKKLs   A)F/ /Gr+  )r^   r  r  r  r  r   r   r  r   r   r[   r  r  r   r  r  r  s   @rd   r  r    sO    3" + +( ( ).K   rh   r  c                D    [         R                  " X5      n[        X5      $ r   )rq   rr   _poly_from_expr)r   rY   r   r   s       rd   poly_from_exprr    s     


+C4%%rh   c                "   U [        U 5      p[        U [        5      (       d  [        XU 5      eU R                  (       aT  U R
                  R                  X5      nUR                  Ul        UR                  Ul        UR                  c  SUl	        X14$ UR                  (       a  U R                  5       n [        X5      u  pAUR                  (       d  [        XU 5      e[        [        [        UR                  5       5      6 5      u  pVUR                  nUc  [        XaS9u  Ul        nO[        [!        UR"                  U5      5      n[%        [        [        XV5      5      5      n[&        R)                  XA5      nUR                  c  SUl	        X14$ )r   Tr   F)r%   rV   r   r?   r{   r   r|   rY   rS   r  expandrF   ry   r   r   r+   r   r   rv   rW   rw   )r   r   origrz  rl   r   r   rS   s           rd   r  r  #  s0   wt}$dE"" D11	~~((399[[
99CIy	{{}t)HC88 D11#tCIIK012NFZZF~-f>
Fc&++V45
tC'(
)C??3$D
yy	9rh   c                D    [         R                  " X5      n[        X5      $ )(Construct polynomials from expressions. )rq   rr   _parallel_poly_from_expr)exprsrY   r   r   s       rd   parallel_poly_from_exprr  K  s     


+C#E//rh   c           	        [        U 5      S:X  a  U u  p#[        U[        5      (       a  [        U[        5      (       a  UR                  R	                  X!5      nUR                  R	                  X15      nUR                  U5      u  p#UR                  Ul        UR                  Ul        UR                  c  SUl        X#/U4$ [        U 5      / p/ / peSn[        U5       H  u  p[        U	5      n	[        U	[        5      (       aV  U	R                  (       a  UR                  U5        O5UR                  U5        UR                  (       a  U	R                  5       n	OSnU R                  U	5        M     U(       a  [!        XU S5      eU(       a  U H  nX   R#                  5       X'   M     [%        X5      u  pUR                  (       d  [!        XU S5      eSSKJn  UR                   H  n[        X5      (       d  M  [+        S5      e   / / p/ n/ nU
 Hj  n[        [-        [        UR/                  5       5      6 5      u  nnUR1                  U5        UR                  U5        UR                  [        U5      5        Ml     UR                  nUc  [3        XS9u  Ul        nO[        [5        UR6                  U5      5      nU H  nUR                  USU 5        XS nM     / n[-        UU5       HK  u  nn[9        [        [-        UU5      5      5      n[        R;                  UU5      nUR                  U5        MM     UR                  c  [=        U5      Ul        UU4$ )	r	  r   NTFr   	Piecewisez&Piecewise generators do not make senser   )r   rV   rW   r   r|   r   rY   rS   r  ry   r  r%   r   r{   appendr  r?   r]   rG   $sympy.functions.elementary.piecewiser  r<   r   r   extendr+   r   r   rv   rw   bool)r  r   r_   r`   origs_exprs_polysfailedr   r   repsr  r  coeffs_listlengthsrb  r_  rl   r   r   rS   r  rz  s                          rd   r
  r
  R  s   
5zQa:a#6#6&&q.A&&q.A771:DAvvCHCJyy  	63;;5FFU#t}dE""||a a ::;;=DFT $   UD99Ax'')EH  )4ID88 UD99>XXa##!"JKK  rJJc4		#4566"&!s6{#  ZZF~"2;"H
K3v00+>?+bq/*!"o  Ej*54FF+,-sC(T 6
 yyL	#:rh   c                .    [        U 5      n X;  a  X U'   U $ )z7Add a new ``(key, value)`` pair to arguments ``dict``. )rv   )r   r  rw  s      rd   _update_argsr    s    :D
S	Krh   c                x   [        U SS9n [        USS9R                  nU R                  (       a  U nUR                  5       R                  nO5U R                  nU(       d"  U(       a  [	        U 5      u  p5O[	        X5      u  p5U(       a'  U (       a  [
        R                  $ [
        R                  $ U(       d]  U R                  (       a+  UWR                  ;  a  [	        U R                  5       5      u  p5UWR                  ;  a  [
        R                  $ OgU R                  (       dV  [        U R                  5      S:  a=  [        [        SU < S[        [        U R                  5      5      < SU< S35      5      eWR                  U5      n[!        U["        5      (       a  [%        U5      $ [
        R                  $ )aP  
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
Tr  r   zj
         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_Numberr{   r]   r  r   Zeror  rY   r   r   r  rL   nextr"   r  rV   rR  r   )r_   r   
gen_is_Numrl  isNumr   rb   s          rd   r  r    s:   6 	$AT*44Jyy		%%%a(1%a-qvv2 2 2299AFF*!!))+.DAaff66M YY3q~~.2
 $wq~~./	$6 7 8 	8
 XXc]F(5576?M1;M;MMrh   c                   [        U 5      nUR                  (       a  UR                  5       nUR                  (       a  SnO?U R                  (       a  U=(       d    U R                  n[        X!5      R                  5       n[        U5      $ )aV  
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%   r{   r]   r  rY   rW   r  r   )r_   rY   rl  rvs       rd   r  r    s\    P 	
AyyIIK{{99>166D!]'')2;rh   c                    [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  5       n[        [        [        U5      5      $ ! [         a  n[	        SSU5      eSnAff = f)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)

r  r  r   N)	rq   allowed_flagsr  r?   r@   r  rh  r   r   )r_   rY   r   r   r   r  degreess          rd   r  r  *  sq     $	*71D1D1 mmoGWg&''  7q#667   A 
A0A++A0c                    [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  UR                  S9$ ! [         a  n[	        SSU5      eSnAff = f)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

r  r  r   NrW  )rq   r&  r  r?   r@   r  rn   r_   rY   r   r   r   r  s         rd   r  r  E  si     $	*.1D1D1 44cii4    .a--.s   A 
A AA c                    [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  UR                  S9nUR                  5       $ ! [         a  n[	        SSU5      eSnAff = f)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

r  r
  r   NrW  )rq   r&  r  r?   r@   r
  rn   r]   )r_   rY   r   r   r   r  r   s          rd   r
  r
  ^  st     $	*.1D1D1 DDsyyD!E==?	  .a--.r(  c                    [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  UR                  S9u  pgXvR                  5       -  $ ! [         a  n[	        SSU5      eSnAff = f)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

r  r  r   NrW  )rq   r&  r  r?   r@   r  rn   r]   )r_   rY   r   r   r   r  r   r   s           rd   r  r  x  sy     $	*.1D1D1 44cii4(LE  	  .a--.s   A 
A4"A//A4c                *   [         R                  " US/5         [        X4/UQ70 UD6u  u  pEnUR                  U5      u  pUR                  (       d   UR                  5       U	R                  5       4$ X4$ ! [         a  n[	        SSU5      eSnAff = f)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)

r  r  r   N)rq   r&  r  r?   r@   r  r  r]   
r_   r`   rY   r   r   r   r   r  r  r  s
             rd   r  r    s     $	*0-qfDtDtD 66!9DA99yy{AIIK''t  03//0s   A6 6
B BBc                   [         R                  " US/5         [        X4/UQ70 UD6u  u  pEnUR                  U5      nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)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

r  r  r   N)rq   r&  r  r?   r@   r  r  r]   	r_   r`   rY   r   r   r   r   r  r  s	            rd   r  r    s     $	*0-qfDtDtD 	
q	A99yy{  03//0   A# #
A?-A::A?c                8   [         R                  " US/5         [        X4/UQ70 UD6u  u  pEn UR                  U5      nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f! [         a    [        X5      ef = f)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

r  r  r   N)	rq   r&  r  r?   r@   r  r>   r  r]   	r_   r`   rY   r   r   r   r   r  r  s	            rd   r  r    s    " $	*0-qfDtDtD(FF1I 99yy{  03//0
  (!!''(s"   A$ B $
B .A;;B Bc                   [         R                  " US/5         [        X4/UQ70 UD6u  u  pEnUR                  U5      nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)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

r  r  r   N)rq   r&  r  r?   r@   r  r  r]   r3  s	            rd   r  r    s    ( $	*2-qfDtDtD 	
A99yy{  2!S112r1  c                <   [         R                  " USS/5         [        X4/UQ70 UD6u  u  pEnUR                  XVR                  S9u  pUR                  (       d   UR                  5       U	R                  5       4$ X4$ ! [         a  n[	        SSU5      eSnAff = f)z
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)

r  r  r  r   Nr  )	rq   r&  r  r?   r@   r  r  r  r]   r.  s
             rd   r  r    s    " $ 12/-qfDtDtD 555"DA99yy{AIIK''t  /q#../s   A? ?
B	BBc                   [         R                  " USS/5         [        X4/UQ70 UD6u  u  pEnUR                  XVR                  S9nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)z
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

r  r  r  r   Nr6  )	rq   r&  r  r?   r@   r  r  r  r]   r0  s	            rd   r  r  4      " $ 12/-qfDtDtD 	
ahhA99yy{  /q#../   A, ,
B6BBc                   [         R                  " USS/5         [        X4/UQ70 UD6u  u  pEnUR                  XVR                  S9nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)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

r  r  r  r   Nr6  )	rq   r&  r  r?   r@   r  r  r  r]   r3  s	            rd   r  r  T  r8  r9  c                   [         R                  " USS/5         [        X4/UQ70 UD6u  u  pEnUR                  XVR                  S9nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)a!  
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

r  r  r  r   Nr6  )	rq   r&  r  r?   r@   r  r  r  r]   r3  s	            rd   r  r  t  s    ( $ 121-qfDtDtD 	
!A99yy{  1C001r9  c                   [         R                  " USS/5         [        X4/UQ70 UD6u  u  pEnUR                  XVR                  S9u  pUR                  (       d   UR                  5       UR                  5       4$ X4$ ! [         au  n[	        UR
                  5      u  nu  p UR                  X5      u  pUR                  U5      UR                  U5      4s SnA$ ! [         a    [        SSU5      ef = fSnAff = f)a0  
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)

r  r  Nr?  r   r6  )rq   r&  r  r?   r+   r  r?  r   rs   r@   r  r  r]   )r_   r`   rY   r   r   r   r   r  rS   r&  r'  r  r@  s                rd   r?  r?    s    " $ 12
:-qfDtDtD <<<)DA99yy{AIIK''t  :)#))4	:$$Q*DA ??1%vq'999 # 	:#L!S99	::s5   A? ?
C>	C9$C7!C9C>C66C99C>c                F   [         R                  " USS/5         [        X4/UQ70 UD6u  u  pEnUR                  XVR                  S9u  pnUR                  (       d/  UR                  5       UR                  5       UR                  5       4$ XU4$ ! [         a  n[	        UR
                  5      u  nu  p UR                  X5      u  pnUR                  U5      UR                  U5      UR                  U5      4s SnA$ ! [         a    [        SSU5      ef = fSnAff = f)a6  
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)

r  r  NrC  r   r6  )rq   r&  r  r?   r+   r  rC  r   rs   r@   r  r  r]   )r_   r`   rY   r   r   r   r   r  rS   r&  r'  r  rD  r@  s                 rd   rC  rC    s   " $ 12
N-qfDtDtD ggahhg'GA!99yy{AIIK44Qw  N)#))4	Nll1(GA! ??1%vq'96??1;MMM # 	5#GQ44	5Ns5   B 
D D5D 	1D:D  DDD c                   [         R                  " USS/5         [        X4/UQ70 UD6u  u  pEnUR                  XVR                  S9nUR                  (       d  UR                  5       $ U$ ! [         a`  n[	        UR
                  5      u  nu  p UR                  UR                  X5      5      s SnA$ ! [         a    [        SSU5      ef = fSnAff = f)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

r  r  NrG  r   r6  )rq   r&  r  r?   r+   r  r   rG  rs   r@   r  r  r]   )r_   r`   rY   r   r   r   r   r  rS   r&  r'  r@  s               rd   rG  rG    s    @ $ 126-qfDtDtD 	
"A99yy{  6)#))4	6??6==#677" 	6#Ha55	66s/   A, ,
C6CB60C6CCCc                .   [         R                  " US/5         [        X4/UQ70 UD6u  u  pEnUR                  U5      nUR                  (       d   U V	s/ s H  oR                  5       PM     sn	$ U$ ! [         a  n[	        SSU5      eSnAff = fs  sn	f )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]

r  rM  r   N)rq   r&  r  r?   r@   rM  r  r]   )
r_   r`   rY   r   r   r   r   r  rb   r  s
             rd   rM  rM    s     $	*9-qfDtDtD __QF99%+,V		V,,  9C889 -s   A3 B3
B=B

BFrQ  c                  [         R                  " US/5         [        X4/UQ70 UD6u  u  pVnU(       a  UR                  XbS9u  pOUR                  U5      n	UR                  (       dG  U(       a0  U	R                  5       W
 Vs/ s H  oR                  5       PM     sn4$ U	R                  5       $ U(       a  U	W
4$ U	$ ! [         a  n[	        SSU5      eSnAff = fs  snf )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

r  rP  r   NrQ  )rq   r&  r  r?   r@   rP  r  r]   )r_   r`   rR  rY   r   r   r   r   r  rb   r  r  s               rd   rP  rP  7  s     $	*5-qfDtDtD KKK9	Q99>>#1%=1aiik1%===~~19  5Q445 &>s   B> C>
CCCc                    [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  5       nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)z
Compute discriminant of ``f``.

Examples
========

>>> from sympy import discriminant
>>> from sympy.abc import x

>>> discriminant(x**2 + 2*x + 3)
-8

r  rU  r   N)rq   r&  r  r?   r@   rU  r  r]   r_   rY   r   r   r   r  rb   s          rd   rU  rU  \  sy     $	*81D1D1 ^^F99~~  83778   A 
A:(A55A:c                4   [         R                  " US/5         [        X4/UQ70 UD6u  u  pEnUR                  U5      u  pnUR                  (       d/  UR                  5       UR                  5       UR                  5       4$ XU4$ ! [         a  n[	        UR
                  5      u  nu  p UR                  X5      u  pnUR                  U5      UR                  U5      UR                  U5      4s SnA$ ! [         a    [        SSU5      ef = fSnAff = f)aa  
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)

r  Nr_  r   )rq   r&  r  r?   r+   r  r_  r   rs   r@   r  r]   )r_   r`   rY   r   r   r   r   r  rS   r&  r'  r@  r`  ra  s                 rd   r_  r_  z  s   & $	*
R-qfDtDtD ++a.KAC99yy{CKKM3;;=88s{  R)#))4	R **10KAC ??1%vs';V__S=QQQ # 	9#KC88	9Rs5   B 
DD,C7 1D1D7DDDc                v  ^^ [        U 5      n UU4S jnU" U 5      nUb  U$ [        R                  " TS/5         [        U /TQ70 TD6u  pV[	        U 5      S:  a  [        S U  5       5      (       au  U S   nU SS  Vs/ s H  oU-  R                  5       PM     n	n[        S U	 5       5      (       a4  Sn
U	 H  n[        XR                  5       S   5      n
M!     [        Xz-  5      $ U(       d+  UR                  (       d  [        R                  $ [!        SUS
9$ US   USS pTU H'  nUR#                  U5      nUR$                  (       d  M'    O   UR                  (       d  UR'                  5       $ U$ s  snf ! [         a7  nU" UR                  5      nUb  Us SnA$ [        S	[	        U 5      U5      eSnAff = f)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

c                  > T(       d  T(       d}  [        U 5      u  pU(       d  UR                  $ UR                  (       aL  US   USS  p#U H,  nUR                  X45      nUR	                  U5      (       d  M,    O   UR                  U5      $ g Nr   r   )r+   r   r  rd  rb  r   seqrS   numbersrb   numberr   rY   s        rd   try_non_polynomial_gcd(gcd_list.<locals>.try_non_polynomial_gcd  sy    D.s3OF{{"$$")!*gabk%F#ZZ7F}}V,,	 & v..rh   Nr  r   c              3  ^   #    U  H#  oR                   =(       a    UR                  v   M%     g 7fr   is_algebraicis_irrational.0r<  s     rd   	<genexpr>gcd_list.<locals>.<genexpr>  $     VRU3 0 0 FS5F5F FRU   +-r  c              3  8   #    U  H  oR                   v   M     g 7fr   is_rationalrS  frcs     rd   rT  rU         2cs??c   r   gcd_listr   )r%   rq   r&  r  r   r  ratsimprh  as_numer_denomr  r?   r  r@   r  r   r  rW   rd  rb  r]   )rI  rY   r   rL  rb   r  r   r&  r<  lstlcr\  r  rz  s    ``           rd   r_  r_    s    #,C& $C(F$	*?,S@4@4@
 s8a<CVRUVVVBA14Sb;#sUOO%C;2c222CR!3!3!5a!89B  14y  yy66Ms##!HeABiED!===	  99~~C <  ?'		2M#JC#>>?s7   AE7 >E2AE7 2E7 7
F8F3F8F33F8c                >   [        U S5      (       a  Ub  U4U-   n[        U /UQ70 UD6$ Uc  [        S5      e[        R                  " US/5         [        X4/UQ70 UD6u  u  pEn[        [        X45      u  pxUR                  (       at  UR                  (       ac  UR                  (       aR  UR                  (       aA  Xx-  R                  5       n	U	R                  (       a  [        XyR                  5       S   -  5      $ UR%                  U5      nUR*                  (       d  UR-                  5       $ U$ ! [         a`  n
[        U
R                   5      u  nu  px UR#                  UR%                  Xx5      5      s Sn
A
$ ! [&         a    [)        SSU
5      ef = fSn
A
ff = f)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 argumentsr  r   rd  r   )rA  r_  r  rq   r&  r  r   r%   rP  rQ  r`  rZ  r  ra  r?   r+   r  r   rd  rs   r@   r  r]   r_   r`   rY   r   r   r   r   r&  r'  r\  r  rS   rb   s                rd   rd  rd    s]    q*=4$;D)D)D))	
LMM$	*3-qfDtDtD 7QF#>>aoo!..Q__3--/C1//1!4455 UU1XF99~~  3)#))4	3??6::a#344" 	3#E1c22	33s1   B,D2 2
F<FE<6F<FFFc                B  ^^ [        U 5      n SUU4S jjnU" U 5      nUb  U$ [        R                  " TS/5         [        U /TQ70 TD6u  pV[	        U 5      S:  a  [        S U  5       5      (       al  U S   nU SS  Vs/ s H  oU-  R                  5       PM     n	n[        S U	 5       5      (       a+  Sn
U	 H  n[        XR                  5       S   5      n
M!     Xz-  $ U(       d+  UR                  (       d  [        R                  $ [        SUS	9$ US
   USS pTU H  nUR                  U5      nM     UR                  (       d  UR!                  5       $ U$ s  snf ! [         a7  nU" UR                  5      nUb  Us SnA$ [        S[	        U 5      U5      eSnAff = f)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

c                
  > T(       d{  T(       dt  [        U 5      u  pU(       d  UR                  UR                  5      $ UR                  (       a4  US   USS  p#U H  nUR	                  X45      nM     UR                  U5      $ g rG  )r+   r   r   r  rh  rH  s        rd   try_non_polynomial_lcm(lcm_list.<locals>.try_non_polynomial_lcm=  sp    D.s3OFvzz22$$")!*gabk%F#ZZ7F & v..rh   Nr  r   c              3  ^   #    U  H#  oR                   =(       a    UR                  v   M%     g 7fr   rO  rR  s     rd   rT  lcm_list.<locals>.<genexpr>X  rV  rW  r  c              3  8   #    U  H  oR                   v   M     g 7fr   rY  r[  s     rd   rT  rl  [  r]  r^  lcm_listr   r   )r  zOptional[Expr])r%   rq   r&  r  r   r  r`  rh  ra  r?   r  r@   r  r   r   rW   r]   )rI  rY   r   ri  rb   r  r   r&  r<  rb  rc  r\  r  rz  s    ``           rd   rn  rn  ,  s    #,C   $C(F$	*?,S@4@4@
 s8a<CVRUVVVBA14Sb;#sUOO%C;2c222CR!3!3!5a!89B t yy55Ls##!HeABiED!  99~~; <  ?'		2M#JC#>>?s7   AE  EAE E 
F'F=FFFc                ,   [        U S5      (       a  Ub  U4U-   n[        U /UQ70 UD6$ Uc  [        S5      e[        R                  " US/5         [        X4/UQ70 UD6u  u  pEn[        [        X45      u  pxUR                  (       ak  UR                  (       aZ  UR                  (       aI  UR                  (       a8  Xx-  R                  5       n	U	R                  (       a  XyR                  5       S   -  $ UR#                  U5      nUR(                  (       d  UR+                  5       $ U$ ! [         a`  n
[        U
R                  5      u  nu  px UR!                  UR#                  Xx5      5      s Sn
A
$ ! [$         a    ['        SSU
5      ef = fSn
A
ff = f)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

re  Nz2lcm() takes 2 arguments or a sequence of argumentsr  r   rh  r   )rA  rn  r  rq   r&  r  r   r%   rP  rQ  r`  rZ  ra  r?   r+   r  r   rh  rs   r@   r  r]   rf  s                rd   rh  rh  z  sX    q*=4$;D)D)D))	
LMM$	*3-qfDtDtD 7QF#>>aoo!..Q__3--/C++-a000 UU1XF99~~  3)#))4	3??6::a#344" 	3#E1c22	33s1   B#D) )
F3FE3-F3FFFc           
       ^^ [        U 5      n[        U [        5      (       a)  [        UU4S jU R                  U R                  4 5       6 $ [        U [
        5      (       a  [        SU < 35      e[        U [        5      (       a  U R                  (       a  U$ TR                  SS5      (       a\  U R                  " U R                   Vs/ s H  n[        U/TQ70 TD6PM     sn6 nTR                  S5        STS'   [        U/TQ70 TD6$ TR                  SS5      n[        R                  " TS/5         [!        U /TQ70 TD6u  pxUR                  5       u  pUR&                  R(                  (       a_  UR&                  R*                  (       a  U R-                  SS
9u  pU R/                  5       u  pUR&                  R*                  (       a  UW-  nO[0        R2                  n[5        [7        U R8                  U
5       VVs/ s H	  u  pX-  PM     snn6 n[;        US5      (       a  [0        R2                  nUS:X  a  U$ U(       a  [=        XU R?                  5       -  5      $ [=        XR?                  5       SS9RA                  5       u  p[=        XU -  SS9$ s  snf ! ["         a  n	U	R$                  s S	n	A	$ S	n	A	ff = fs  snnf )a  
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

c              3  B   >#    U  H  n[        U/TQ70 TD6v   M     g 7fr   )r  )rS  r  r   rY   s     rd   rT  terms_gcd.<locals>.<genexpr>  s      N~!)A555~s   z3Inequalities cannot be used with terms_gcd. Found: deepFr  clearTr  Nr%  r   )rt  )!r%   rV   r!   lhsrhsr    r  r   is_Atomr-  rc   r   r  r$  rq   r&  r  r?   r   rS   r  r  r  rv  r   r   r   r   rY   r   r   r]   as_coeff_Mul)r_   rY   r   r  r&  r   rt  r   r   r  r  denomr   r"  r  terms    ``             rd   r  r    s5   F 1:D!XNquu~NOO	Az	"	"RSUVVa!))xxffAFFCFqy2T2T2FCDX,t,t,,HHWd#E$	*1D1D1 ;;=DA
zz::~~d~3HE;;=::UNE#affa.1.$!.12DE119K5qyy{"233 5))+U;HHJHEu1fE22K D  xx  2s*   	J6J J?

J<&J71J<7J<c                   [         R                  " USS/5         [        U /UQ70 UD6u  pEUR                  [        U5      5      nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)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

r  r  rk  r   N)	rq   r&  r  r?   r@   rk  r%   r  r]   )r_   rl  rY   r   r   r   r  rb   s           rd   rk  rk  #  s     $ 1211D1D1 WWWQZ F99~~  1C001s   A) )
B3B  Bc                   [         R                  " USS/5         [        U /UQ70 UD6u  p4UR                  UR                  S9nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)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

r  r  ro  r   Nr6  )	rq   r&  r  r?   r@   ro  r  r  r]   rB  s          rd   ro  ro  A  s     $ 1211D1D1 WW#((W#F99~~  1C001s   A( (
B2A??Bc                    [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  5       $ ! [         a  n[	        SSU5      eSnAff = f)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

r  rs  r   N)rq   r&  r  r?   r@   rs  r*  s         rd   rs  rs  _  s`     $	*31D1D1 99;  3	1c223s   ; 
AAAc                   [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  5       u  pgUR                  (       d  XgR                  5       4$ Xg4$ ! [         a  n[	        SSU5      eSnAff = f)aI  
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)

r  rv  r   N)rq   r&  r  r?   r@   rv  r  r]   )r_   rY   r   r   r   r  rw  rb   s           rd   rv  rv  x  s    @ $	*51D1D1 ;;=LD99^^%%%|  5Q445s   A" "
A>,A99A>c                   [         R                  " US/5         [        X4/UQ70 UD6u  u  pEnUR                  U5      nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)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

r  rz  r   N)rq   r&  r  r?   r@   rz  r  r]   )	r_   r`   rY   r   r   r   r   r  rb   s	            rd   rz  rz    s     $	*3-qfDtDtD YYq\F99~~  3	1c223r1  c                $   [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  5       nUR                  (       d   U Vs/ s H  owR                  5       PM     sn$ U$ ! [         a  n[	        SSU5      eSnAff = fs  snf )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]

r  r}  r   N)rq   r&  r  r?   r@   r}  r  r]   r_   rY   r   r   r   r  rb   r  s           rd   r}  r}    s     $	*51D1D1 [[]F99%+,V		V,,  5Q445 -s   A. B.
B
8BB
c                8   [         R                  " USS/5         [        U /UQ70 UD6u  p4UR                  UR                  S9nUR                  (       d   U Vs/ s H  owR                  5       PM     sn$ U$ ! [         a  n[	        SSU5      eSnAff = fs  snf )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]

r  r  r  r   Nr6  )	rq   r&  r  r?   r@   r  r  r  r]   r  s           rd   r  r    s     $ 1211D1D1 WW#((W#F99%+,V		V,,  1C001 -s   A8 B8
BBBc                4   [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  5       nUR                  (       d'  U VVs/ s H  u  pxUR                  5       U4PM     snn$ U$ ! [         a  n[	        SSU5      eSnAff = fs  snnf )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

r  r  r   N)rq   r&  r  r?   r@   r  r  r]   )	r_   rY   r   r   r   r  r  r`   r  s	            rd   r  r     s    @ $	*41D1D1 jjlG99-45WTQa W55  4
As334 6s   A5 B5
B?BBc                    [        S5      e)z3Compute greatest factorial factorization of ``f``. zsymbolic falling factorialr  r_   rY   r   s      rd   gffr  /  s     :
;;rh   c                d   [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  5       u  pgnU V	s/ s H  n	[        U	5      PM     n
n	UR                  (       d   XR                  5       UR                  5       4$ XU4$ ! [         a  n[	        SSU5      eSnAff = fs  sn	f )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)

r  r  r   N)	rq   r&  r  r?   r@   r  r   r  r]   )r_   rY   r   r   r   r  r  r`   r  sis_exprs              rd   r  r  5  s    & $	*41D1D1 jjlGA!$%&AbgbkAF&99yy{AIIK//!|  4
As334
 's   B B-
B*B%%B*c                    [         R                  " US/5         [        U /UQ70 UD6u  p4UR                  5       nUR                  (       d  UR                  5       $ U$ ! [         a  n[	        SSU5      eSnAff = f)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

r  r  r   N)rq   r&  r  r?   r@   r  r  r]   rB  s          rd   r  r  Y  sx     $	*41D1D1 ZZ\F99~~  4
As334rC  c                    U R                   R                  5       n[        U5      [        U R                  5      [	        U R
                  5      U4$ )zSort a list of polys.rl   rq  r   rY   ru   rS   )rz  rl   s     rd   _poly_sort_keyr  w  s8    
((


CHc$))nc$++&6<<rh   c                .    US:X  a  S nOS n[        XS9$ )z&Sort a list of ``(expr, exp)`` pairs. r  c                    U u  pUR                   R                  5       nU[        U5      [        UR                  5      [	        UR
                  5      U4$ r   r  r   rz  exprl   s       rd   r  _sorted_factors.<locals>.key  sA    ID((""$CS3tyy>3t{{3CSIIrh   c                    U u  pUR                   R                  5       n[        U5      [        UR                  5      U[	        UR
                  5      U4$ r   r  r  s       rd   r  r    sA    ID((""$CHc$))nc3t{{3CSIIrh   r  )r  )r  methodr  s      rd   _sorted_factorsr  }  s"    	J
	J
 '##rh   c                j    [        U  VVs/ s H  u  pUR                  5       U-  PM     snn6 $ s  snnf )z*Multiply a list of ``(expr, exp)`` pairs. )r   r]   )r  r_   r  s      rd   _factors_productr    s+    G4GDAaG4554s   /
c           
     
  ^ [         R                  / pC[        R                  " U 5       Vs/ s H&  n[	        US5      (       a  UR                  5       OUPM(     nnU GHM  nUR                  (       d%  [        U[        5      (       a  [        U5      (       a  X7-  nM@  UR                  (       az  UR                  [         R                  :w  a\  UR                  u  pUR                  (       a  U	R                  (       a  X7-  nM  UR                  (       a  UR                  X45        M  OU[         R                  p [        X5      u  p[!        XS-   5      nU" 5       u  pU[         R                  La^  U	R"                  (       a  X=U	-  -  nOEUR$                  (       a  UR                  X45        O!UR                  U[         R                  45        U	[         R                  L a  UR'                  U5        GM  U	R(                  (       a/  UR'                  U VVs/ s H  u  nnUUU	-  4PM     snn5        GM  / nU HP  u  nmUR+                  5       R$                  (       a  UR                  UTU	-  45        M=  UR                  UT45        MR     UR                  [-        U5      U	45        GMP     US:X  aB  U VVs1 s H  u  pUiM	     snn V^s/ s H  m[3        [4        U4S jU 5       5      T4PM!     nn[7        [8        5      nU H  u  mnUT==   U-  ss'   M     U[;        UR=                  5       5      4$ s  snf s  snnf ! [.         a)  nUR                  UR0                  U	45         SnAGM  SnAff = fs  snnf s  snf )z.Helper function for :func:`_symbolic_factor`. _eval_factor_listNr  c              3  <   >#    U  H  u  pUT:X  d  M  Uv   M     g 7fr   r+  )rS  r_   r   r  s      rd   rT  (_symbolic_factor_list.<locals>.<genexpr>  s      Awtq!q&ws   	)r   r   r   	make_argsrA  r  r  rV   r   r   is_PowbaseExp1r   r  r  r\   r  is_positiver  
is_integerr]   r  r?   r   r   r   r
   rR  ry   r   )r   r   r  r   r  r   r   argr  r  rz  r   rc   _coeff_factorsr_   r  r  r  r$  r  s                   `    rd   _symbolic_factor_listr    s   UUB7 t$&$A !(> : :ANNA$ 	 &==ZT22|C7H7HLEZZCHH.ID~~#--~~{+  QUU#	?%d0GD 4'!12D#vFQUU">>S[(E''NNF=1OOVQUUO4aee|x(x@xtq!AcE
x@A$DAqyy{..1S5z2aV,	 %  0 7=>Y Z +23741Q7353q 3 Aw ABAF3 	 5 
S	B1
1
 $rxxz"""m&H  A# " 	,NNCHHc?++	,< 45s/   -L99ML>9M:&N 
M7M22M7c           
        [        U [        5      (       aN  [        U S5      (       a  U R                  5       $ [	        [        XS   S9X5      u  p4[        U[        U5      5      $ [        U S5      (       a4  U R                  " U R                   Vs/ s H  n[        XQU5      PM     sn6 $ [        U S5      (       a,  U R                  U  Vs/ s H  n[        XQU5      PM     sn5      $ U $ s  snf s  snf )z%Helper function for :func:`_factor`. r  fraction)r  r   re  )rV   r   rA  r  r  rH   r   r  rc   r   _symbolic_factorr   )r   r   r  r   r  r  s         rd   r  r    s    $4''$$&&.x:/WY\e5"27";<<	v		yyS#+Cf=STT	z	"	"~~TRTc/&ATRSS	 TRs   C(	C-c                   [         R                  " USS/5        [         R                  " X5      n[        U 5      n [	        U [
        [        45      (       Ga`  [	        U [        5      (       a  U SpeO[        U 5      R                  5       u  pV[        XTU5      u  px[        XdU5      u  pU
(       a  UR                  (       d  [        SU -  5      eUR                  SS05      nX4 H@  n[        U5       H.  u  nu  pUR                  (       a  M  [        X5      u  nnX4X'   M0     MB     [!        X5      n[!        X5      n
UR"                  (       dL  U VVs/ s H  u  pUR%                  5       U4PM     nnnU
 VVs/ s H  u  pUR%                  5       U4PM     n
nnXy-  nUR                  (       d  UU4$ UX4$ [        SU -  5      es  snnf s  snnf )z>Helper function for :func:`sqf_list` and :func:`factor_list`. fracr  r   za polynomial expected, got %sr  T)rq   r&  rr   r%   rV   r   rW   rH   ra  r  r  r<   cloner  r{   r  r  r  r]   )r   rY   r   r  r   numerry  r;  fpr<  fq_optr  r   r_   r  r   r   s                     rd   _generic_factor_listr    s   $ 12



+C4=D$t%%dD!!5#D>88:LE&u6:&u6:chh!"AD"HIIyy(D)*xG&w/	6Ayyy*13DAq"#GJ 0   R(R(yy/12rtq199;"rB2/12rtq199;"rB2xx"9"= =DEE 32s   G9Gc                    UR                  SS5      n[        R                  " U/ 5        [        R                  " X5      nXES'   [	        [        U 5      XS5      $ )z4Helper function for :func:`sqf` and :func:`factor`. r  T)r$  rq   r&  rr   r  r%   )r   rY   r   r  r  r   s         rd   _generic_factorr  
  sJ    xx
D)H$#



+C
OGDM377rh   c                "  ^ SSK Jm  S	U4S jjnS	U4S jjnS nU R                  5       R                  (       aV  U" U 5      (       aI  U R	                  5       nU" X5      nU(       a  US   US   SUS   4$ U" X5      nU(       a  SSUS   US   4$ g)
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

r   simplifyNc                  > [        U R                  5      S:X  a  U R                  S   R                  (       d  SU 4$ U R                  5       nU R	                  5       nU=(       d    UR                  5       nUR                  5       SS nU Vs/ s H  nT" U5      PM     nn[        U5      S:  a  US   (       a  T" US   US   -  5      n/ n[        [        U5      5       H9  nT" XH   XhS-   -  -  5      nUR                  (       d    gUR                  U5        M;     T" SU-  5      n	U R                  S   n
X-  /n[        SUS-   5       H!  nUR                  XxS-
     XU-
  -  -  5        M#     [        U6 n [        U 5      n X9U 4$ gs  snf )z
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``.
r   r   Nr  )r   rY   rw  r  r  ro  r_  r   rZ  r  r   rW   )r_   f1rT  rc  r   coeffx
rescale1_xcoeffs1r   	rescale_xr"  r  r  s               rd   _try_rescale(to_rational_coeffs.<locals>._try_rescale;  sr    166{aq	':':7NHHJTTV288:$178v(6"8v;?vbz!&*VBZ"78JG3v;'!&)JQ,?"?@))  v&	 ( %Qz\2	FF1ITFq!a%AHHWU^AAJ67 )GGa''% 9s   F	c                  > [        U R                  5      S:X  a  U R                  S   R                  (       d  SU 4$ U R                  5       nU=(       d    UR	                  5       nUR                  5       SS nT	" US   5      nUR                  (       aP  UR                  (       d?  [        UR                  S SS9u  pVUR                  " U6 * U-  nUR                  U5      nXx4$ g)z
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``.
r   r   Nc                    U R                   SL $ r  rY  )zs    rd   r  <to_rational_coeffs.<locals>._try_translate.<locals>.<lambda>m  s    !--4/rh   Tbinary)r   rY   rw  r  ro  r_  is_AddrZ  rO   r   rc   r  )
r_   r  rT  r   r   ratnonratalphaf2r  s
            rd   _try_translate*to_rational_coeffs.<locals>._try_translate]  s     166{aq	':':7NHHJ288:$VAY88AMMqvv/>KCVVV_$Q&E%B9rh   c                   U R                  5       nSnU H  n[        R                  " U5       H  n[        U5      R                  nUR                  5        VVs/ s HI  u  pgUR                  (       d  M  UR                  (       d  M+  UR                  S:  d  M=  UR                  PMK     nnnU(       d  M  [        U5      S:X  a  Sn[        U5      S:  d  M      g   M     U$ s  snnf )zC
Return True if ``f`` is a sum with square roots but no other root
Fr   T)r   r   r  r   r  r   r  is_Rationalr  minrR  )	rl  r   has_sqr#  r"  r_   r'  wxr  s	            rd   _has_square_roots-to_rational_coeffs.<locals>._has_square_rootss  s     A]]1%AJ&&'(wwy ByeaKK $&NN 79ttqy RTTy Bq6Q;!Fq6A:  &  Bs   C"
4C"
C"
C"
r   r   r   )sympy.simplify.simplifyr  r   r   ro  )r_   r  r  r  r  r  r  s         @rd   to_rational_coeffsr    s    L 1 D,& 	||~ 1! 4 4WWYQ41tQqT))q%AT1Q41--rh   c           
        SSK Jn  [        XSS9nUR                  5       n[	        U5      nU(       d  gUu  pgp[        U	R                  5       5      n
U(       ad  U" U
S   U-  Xt-  -  5      nU" SU-  5      n/ nU
SS S    H5  nUR                  U" US   R                  XU-  05      5      US   45        M7     X4$ U
S   n/ nU
SS S    H/  nUR                  US   R                  XU-
  05      US   45        M1     X4$ )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

r   r  EXrM  Nr   )	r  r  rW   r  r  r  r]   r  r  )rl  r"  r  p1rT  resrc  r  rD  r`   r  r   r1r&  r  s                  rd   _torational_factor_listr    s   , 1	a4	 B
		A
R
 CKB1!))+&G	WQZ]14'(ac]QAHHhqtyy!rT34ad;<   6M	 AJQAHHadiiE
+QqT23  6Mrh   c                    [        XUSS9$ )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  r  r  r  s      rd   r  r    s      e<<rh   c                    [        XUSS9$ )z
Compute square-free factorization of ``f``.

Examples
========

>>> 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  )r  r  s      rd   r  r    s     1D77rh   c                    [        XUSS9$ )z
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  r  s      rd   r  r    s      h??rh   )rs  c                 ^^ [        U 5      n U(       a  UU4S jn[        X5      n 0 nU R                  [        [        5      nU HA  n[        U/TQ70 TD6nUR                  (       d  UR                  (       d  M6  X:w  d  M=  XU'   MC     U R                  U5      $  [        U TTSS9$ ! [         a     U R                  (       d  [        U 5      s $ e f = f)aR  
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

c                n   > [        U /TQ70 TD6nUR                  (       d  UR                  (       a  U$ U $ )z;
Factor, but avoid changing the expression when unable to.
)r  is_Mulr  )r   r  r   rY   s     rd   _try_factorfactor.<locals>._try_factor:  s0     ---CzzSZZ
Krh   r  r  )r%   r(   atomsr   r   r  r  r  xreplacer  r<   r  r   )	r_   rs  rY   r   r  partialsmuladdrl  r  s	     ``     rd   r  r    s    H 	
A	 a% c"A*T*T*C

cjjjch!  zz(##q$X>> Q<	s   B( (&CCc           
        [        U S5      (       d   [        U 5      n U R                  XX4XgS9$ [	        U SS9u  p[        U	R                  5      S:  a  [        e[        U5       H!  u  pUR                  R                  5       X'   M#     Ub,  U	R                  R                  U5      nUS::  a  [        S5      eUb  U	R                  R                  U5      nUb  U	R                  R                  U5      n[        XR                  X#XEUS9n/ nU HQ  u  u  pnU	R                  R                  U5      U	R                  R                  U5      pUR!                  X4U45        MS     U$ ! [         a    / s $ f = f)	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)]

re  r  r-   rM  r   r   r  )r  r  r  r  r  )rA  rW   r;   r  r  r   rY   r=   r  rl   rq  rS   r   r:  rI   r   r  )r   r  r  r  r  r  r  r  r  r   r   rz  r  rb   r  rD  r;  s                    rd   r  r  W  sd   " 1j!!	QA {{sD{RR,Qt<
sxx=1-- 'GAxx'')EH ( ?**$$S)Cax !DEE?**$$S)C?**$$S)C/zz#4A	 (OFQG::&&q)3::+>+>q+AqMMA67+,  ) C   	I	s   E& &E54E5c           	          [        U 5      n[        U [         5      (       d&  UR                  R                  (       d  [	        S5      eUR                  XX4XVS9$ ! [
         a    [	        SU -  5      ef = f)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  )rW   rV   r   	is_Symbolr<   r;   r  )r_   r  rD  r  r  r  r  r   s           rd   r  r    sw    	@G!T""155?? "">??
 ==3$=TT	  @:Q>@ 	@@s   AA A2c                     [        U SS9n[        U [         5      (       d&  UR                  R                  (       d  [	        S5      eUR                  XS9$ ! [
         a    [	        SU -  5      ef = f)an  
Return the number of roots of ``f`` in ``[inf, sup]`` interval.

If one of ``inf`` or ``sup`` is complex, it will return the number of roots
in the complex rectangle with corners at ``inf`` and ``sup``.

Examples
========

>>> 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

Fgreedyr  z*Cannot count roots of %s, not a polynomialr  )rW   rV   r   r  r<   r;   r  )r_   r  r  r   s       rd   r  r    sp    (P5!!T""155?? "">?? ==S=**  PJQNOOPs   AA A/Tc                    [        U [        5      (       a:  U(       a0  U R                  R                  (       d  [        U R                  SS9nOU nOU(       a  [        U SS9nO
[        U SS9n[        U [        5      (       d&  UR
                  R                  (       d  [        S5      eUR                  XS9$ ! [         a    [        SU -  5      ef = f)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	extensionFr  r  1Cannot compute real roots of %s, not a polynomialr  r  )
rV   rW   rS   r  r   r   r  r<   r;   r  r_   r  r  r  r   s        rd   r  r    s    `Ea!;!;40d+5)!T""155?? "">??
 ;;;<<	  E?!CE 	EE   B&B7 7Cc                    [        U [        5      (       a:  U(       a0  U R                  R                  (       d  [        U R                  SS9nOU nOU(       a  [        U SS9nO
[        U SS9n[        U [        5      (       d&  UR
                  R                  (       d  [        S5      eUR                  XS9$ ! [         a    [        SU -  5      ef = f)al  
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
Tr  Fr  r  r  r  )
rV   rW   rS   r  r   r   r  r<   r;   r  r  s        rd   r  r  u  s    LEa!;!;40d+5)!T""155?? "">??
 <<<==	  E?!CE 	EEr  c                     [        U SS9n[        U [         5      (       d&  UR                  R                  (       d  [	        S5      eUR                  XUS9$ ! [
         a    [	        SU -  5      ef = f)a$  
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]

Fr  r  z6Cannot compute numerical roots of %s, not a polynomial)rT  r  r  )rW   rV   r   r  r<   r;   r  )r_   rT  r  r  r   s        rd   r  r  3  sx    "	J5!!T""155?? "">??
 88aG8<<	  JDqHJ 	JJs   AA A0c                (   [         R                  " U/ 5         [        U /UQ70 UD6u  p4[        U [        5      (       d&  UR
                  R                  (       d  [        S5      eUR                  5       $ ! [         a  n[        SSU5      eSnAff = f)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}

r  r  r   N)rq   r&  r  rV   rW   r   r  r<   r?   r@   r  r*  s         rd   r  r  R  s     $#81D1D1!T""155?? "">?? >>  83778s   AA5 5
B?BBc                p   [         R                  " U/ 5         [        U /UQ70 UD6u  pE[        U [        5      (       d&  UR
                  R                  (       d  [        S5      eUR                  U5      nUR                  (       d  UR                  5       $ U$ ! [         a  n[        SSU5      eSnAff = f)an  
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

r  r  r   N)rq   r&  r  rV   rW   r   r  r<   r?   r@   r  r  r]   )r_   rT  rY   r   r   r   r  rb   s           rd   r  r  p  s    0 $#@1D1D1!T""155?? "">?? ##A&F99~~  @ 63??@s   AB 
B5#B00B5)	_signsimpc               b  ^ SSK Jn  SSKJn  [        R
                  " US/5        [        U 5      n U(       a  U" U 5      n 0 nSU;   a  US   US'   [        U [        5      (       dZ  U R                  (       d*  [        U [        5      (       d  [        U [        5      (       d  U $ [        U SS9n U R                  5       u  pxO[        U 5      S:X  a  U u  px[        U[        5      (       aH  [        U[        5      (       a3  UR                   US'   UR"                  US	'   UR%                  SS5      US'   UR'                  5       UR'                  5       pO[)        S
U -  5      eSSKJm   U R/                  T5      (       a
  [1        5       eU" Xx4/UQ70 UD6u  n	u  pU	R2                  (       d7  [        U [        5      (       d  U R5                  5       $ [6        R8                  Xx4$  SU
RE                  U5      snu  nnUR%                  SS5      (       a  SU;  a  U	RX                  US'   [        U [        5      (       d$  UUR'                  5       UR'                  5       -  -  $ UR'                  5       UR'                  5       nnUR%                  SS5      (       d  UUU4$ U[        U/UQ70 UD6[        U/UQ70 UD64$ ! [0         Gam  nU R:                  (       a!  U R/                  T5      (       d  [1        U5      eU R<                  (       d  U R>                  (       aj  [A        U RB                  U4S jSS9u  pU Vs/ s H  n[E        U5      PM     Os  snf nnU RF                  " [E        U RF                  " U6 5      /UQ76 s SnA$ / n[I        U 5      n[K        U5        U Hm  n[        U[L        5      (       d  [        U[        5      (       d  M/   URO                  U[E        U5      45        URQ                  5         M^  ! [R         a     Mk  f = f   U RU                  [W        U5      5      s SnA$ SnAff = f)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)sringr  T)radicalr   rY   rS   zunexpected argument: %sr  c                Z   > U R                   SL =(       a    U R                  T5      (       + $ r  )r  has)r"  r  s    rd   r  cancel.<locals>.<lambda>  s%      D(Ay1A-AArh   r  Nr   F)-r  r  sympy.polys.ringsr  rq   r&  r%   rV   r   r  r    r   r   ra  r   rW   rY   rS   r-  r]   r:  r  r  r  r<   ngensr  r   r   r  r  r  rO   r   r9  rc   r'   r   r)   r  skiprs   r  rv   r   )r_   r  rY   r   r  r  r   rl  r  r  r   r   msgr   ncr   r  poter  r  r  s                        @rd   r9  r9    si   2 1'$	*
AQK
C$G}Ga;;*Q
33:a;N;NHD)!1	Q1a:a#6#6&&CKHHCM777D1CLyy{AIIK12Q677>*55!##1&04040	6Awwa''xxz!uua{"	 : 188A;IAv1
www6#4iiFa!))+aiik)**yy{AIIK1www&&a7Nd1+t+s+T!-Bd-Bc-BBBG  *AEE)$4$4!#&&88qxx "BEA &((R&)R(B(66&,2r22D$Q'CIa--Z45H5HKKF1I/HHJ*   ::d4j))-*sc   $A'J6 J6 6P.A3P)4M
	0P)9P.?A	P)	,O75P)7
PP)PP)#P.)P.c                   [         R                  " USS/5         [        U /[        U5      -   /UQ70 UD6u  pEUR                  nSnUR                  (       aE  UR                  (       a4  UR                  (       d#  UR                  SUR                  5       05      nSnSS	KJn	  U	" UR                  UR                  UR                  5      u  p[!        U5       HK  u  pUR#                  UR                  5      R$                  R'                  5       nU
R)                  U5      XL'   MM     US   R+                  US
S 5      u  pU Vs/ s H"  n[,        R/                  [1        U5      U5      PM$     nn[,        R/                  [1        U5      U5      nU(       a5   U Vs/ s H  nUR3                  5       PM     snUR3                  5       nnUUpUR6                  (       d1  U Vs/ s H  nUR9                  5       PM     snUR9                  5       4$ X4$ ! [         a  n[        SSU5      eSnAff = fs  snf s  snf ! [4         a     Nzf = fs  snf )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)

r  r  reducedr   NFrS   Txringr   )rq   r&  r  ry   r?   r@   rS   r  r  r  r  r/  r  r  rY   rn   r  r   rl   r   r   r  rW   rw   rv   r@  r9   r  r]   )r_   r   rY   r   r  r   r  rS   rN  r  _ringr   r   rz  r  r  r  _Q_rs                      rd   r  r     s   ( $& 123,aS47]JTJTJ
 ZZFG
xxFNN6??ii6#3#3#567'SXXszz3995HEU#szz*..668??4( $ 8<<ab	"DA0121a#	&A2Q%A	+,-1aaiik1-qyy{B rq99%&'Q		Q'44tC  3	1c223& 	3
 . 		 (sG   H )H.H8 H34H8 I
H+H&&H+3H8 8
IIc                     [        U /UQ70 UD6$ )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]_

)GroebnerBasisr   rY   r   s      rd   r1   r1   <  s    f *T*T**rh   c                4    [        U /UQ70 UD6R                  $ );  
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      rd   r  r  r  s     *T*T*>>>rh   c                      \ rS rSrSrS r\S 5       r\S 5       r	\S 5       r
\S 5       r\S 5       r\S	 5       r\S
 5       rS rS rS rS rS rS r\S 5       rS rSS jrS rSrg)r  i  z%Represents a reduced Groebner basis. c                .   [         R                  " USS/5         [        U/UQ70 UD6u  pESSKJn  U" UR                  UR                  UR                  5      nU V	s/ s H4  o(       d  M  UR                  U	R                  R                  5       5      PM6     nn	[        XHUR                  S9n
U
 Vs/ s H  n[         R#                  X5      PM     n
nU R%                  X5      $ ! [         a  n[	        S[        U5      U5      eSnAff = fs  sn	f s  snf )z>Compute a reduced Groebner basis for a system of polynomials. r  r  r1   Nr   )PolyRingr  )rq   r&  r  r?   r@   r   r  r  rY   rS   rn   r   rl   r   	_groebnerr  rW   rw   _new)r   r   rY   r   r  r   r  r  ringrz  r   r`   s               rd   r   GroebnerBasis.__new__  s    dWh$78	=0BTBTBJE 	/#**cii8@EN3 0 0 23Ne#**5./0aT__Q$a0xx " 	=#JA<<	= O 1s)   C% 
D,-D3D%
D
/DD
c                ^    [         R                  " U 5      n[        U5      Ul        X#l        U$ r   )r   r   rh  _basis_options)r   basisrq   r   s       rd   r  GroebnerBasis._new  s%    mmC 5\

rh   c                p    S U R                    5       n[        U6 [        U R                  R                  6 4$ )Nc              3  @   #    U  H  oR                  5       v   M     g 7fr   r  )rS  rl  s     rd   rT  %GroebnerBasis.args.<locals>.<genexpr>  s     2kks   )r   r   r!  rY   )r   r"  s     rd   r   GroebnerBasis.args  s.    2dkk2uudmm&8&89::rh   c                `    U R                    Vs/ s H  oR                  5       PM     sn$ s  snf r   )r   r]   r   rz  s     rd   r  GroebnerBasis.exprs  s"    +/;;7;4;777s   +c                ,    [        U R                  5      $ r   )ry   r   r   s    rd   r  GroebnerBasis.polys  s    DKK  rh   c                .    U R                   R                  $ r   )r!  rY   r   s    rd   rY   GroebnerBasis.gens  s    }}!!!rh   c                .    U R                   R                  $ r   )r!  rS   r   s    rd   rS   GroebnerBasis.domain  s    }}###rh   c                .    U R                   R                  $ r   )r!  rn   r   s    rd   rn   GroebnerBasis.order  s    }}"""rh   c                ,    [        U R                  5      $ r   )r   r   r   s    rd   __len__GroebnerBasis.__len__  s    4;;rh   c                    U R                   R                  (       a  [        U R                  5      $ [        U R                  5      $ r   )r!  r  iterr  r   s    rd   re  GroebnerBasis.__iter__  s-    ==

##

##rh   c                x    U R                   R                  (       a  U R                  nX!   $ U R                  nX!   $ r   )r!  r  r  )r   itemr"  s      rd   __getitem__GroebnerBasis.__getitem__  s5    ==JJE { JJE{rh   c                r    [        U R                  [        U R                  R	                  5       5      45      $ r   )hashr   rh  r!  r   r   s    rd   r   GroebnerBasis.__hash__  s(    T[[%(;(;(=">?@@rh   c                8   [        XR                  5      (       a9  U R                  UR                  :H  =(       a    U R                  UR                  :H  $ [	        U5      (       a7  U R
                  [        U5      :H  =(       d    U R                  [        U5      :H  $ g)NF)rV   r   r   r!  rN   r  ry   r  r   r  s     rd   r  GroebnerBasis.__eq__  sg    e^^,,;;%,,.R4==ENN3RRe__::e,I

d5k0IIrh   c                    X:X  + $ r   r+  rA  s     rd   r  GroebnerBasis.__ne__  s      rh   c                    S n[        S/[        U R                  5      -  5      nU R                  R                  nU R
                   H%  nUR                  US9nU" U5      (       d  M!  X%-  nM'     [        U5      $ )r  c                :    [        [        [        U 5      5      S:H  $ r   )sumr   r  )monomials    rd   
single_var5GroebnerBasis.is_zero_dimensional.<locals>.single_var  s    s4*+q00rh   r   rW  )r2   r   rY   r!  rn   r  r
  r  )r   rI  r  rn   rz  rH  s         rd   r  !GroebnerBasis.is_zero_dimensional  sn    	1 aSTYY/0	##JJDwwUw+H(##%		  9~rh   c                0   U R                   nUR                  n[        U5      nX4:X  a  U $ U R                  (       d  [	        S5      e[        U R                  5      nUR                  nUR                  UR                  5       US.5      nSSK
Jn  U" UR                  UR                  U5      u  p[        U5       HK  u  pUR                  UR                  5      R                  R!                  5       nUR#                  U5      XZ'   MM     [%        XXU5      nU Vs/ s H"  n[&        R)                  [+        U5      U5      PM$     nnUR,                  (       d'  U Vs/ s H  oR/                  SS9S   PM     nnXbl        U R1                  X5      $ s  snf s  snf )a1  
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)rS   rn   r   r  Tr%  r   )r!  rn   r3   r  rs   ry   r   rS   r  r/  r  r  rY   r  r   rl   r   r   r0   rW   rw   rv   r  r  r  )r   rn   r   	src_order	dst_orderr  rS   r  r  r   r   rz  r   r`   s                 rd   fglmGroebnerBasis.fglm  sP   > mmII	 '	!K''%&ghhT[[!ii&&(
 
 	,3::y9 'GA??3::.22::<Dt,EH ( i0456AqT__T!Wc*A6:;<!Q-a0!A<Jyy   7 =s   )FFc                0   [        U[        5      (       aU  UR                  U R                  R                  :w  a  [	        S5      eUR                  U R                  R                  5      nO[        R                  XR                  5      nU/[        U R                  5      -   nU R                  nUR                  nSnU(       aE  UR                  (       a4  UR                  (       d#  UR                  SUR                  5       05      nSnSSKJn  U" UR                  UR                  UR                   5      u  p[#        U5       HK  u  pUR                  UR                  5      R$                  R'                  5       nU	R)                  U5      XK'   MM     US   R+                  USS 5      u  pU Vs/ s H"  n[        R-                  [/        U5      U5      PM$     nn[        R-                  [/        U5      U5      nU(       a4   U Vs/ s H  oR1                  5       PM     snUR1                  5       nnUUpUR4                  (       d0  U Vs/ s H  oR7                  5       PM     snUR7                  5       4$ X4$ s  snf s  snf ! [2         a     NZf = fs  snf )	ap  
Reduces a polynomial modulo a Groebner basis.

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*f_1 + ... + q_n*f_n + r``, where ``r`` vanishes or ``r``
is a completely reduced polynomial with respect to ``G``.

Examples
========

>>> 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 generatorsFrS   Tr   r  r   N)rV   rW   rY   r!  r:  r   rS   r}   ry   r   r  r  r  r/  r  r  rn   r  rl   r   r   r  rw   rv   r@  r9   r  r]   )r   r   r  rz  r  r   rS   rN  r  r  r   r   r  r  r  r  r  s                    rd   r   GroebnerBasis.reduce>  s   F dD!!yyDMM... !^__??4==#7#78D ??47Ddkk**mmFNN6??))Xv'7'7'9:;CG+3::syy9 'GA??3::.22::<Dt,EH ( Qx||E!"I&456AqT__T!Wc*A6OODGS)/01q!))+q1199;B 21yy)*+AIIK+QYY[884K 7
 2!  ,s0   0)I9J I>J 
J>J 
JJc                0    U R                  U5      S   S:H  $ )a  
Check if ``poly`` belongs the ideal generated by ``self``.

Examples
========

>>> 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

r   r   )r   r)  s     rd   containsGroebnerBasis.contains  s    & {{4 #q((rh   r+  Nr  )r^   r  r  r  r  r   r  r  r  r   r  r  rY   rS   rn   r4  re  r;  r   r  r  r  rO  r   rT  r  r+  rh   rd   r  r    s    / &   ; ; 8 8 ! ! " " $ $ # # $A!  >@!DN`)rh   r  c                   ^^ [         R                  " T/ 5        UU4S jm[        U 5      n U R                  (       a  [	        U /UQ70 TD6$ ST;  a  STS'   [         R
                  " UT5      nT" X5      $ )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')

c                  > / / p2[         R                  " U 5       GH  n/ / pe[        R                  " U5       H  nUR                  (       a  UR	                  T
" Xq5      5        M-  UR
                  (       a  UR                  R                  (       ah  UR                  R                  (       aM  UR                  S:  a=  UR	                  T
" UR                  U5      R                  UR                  5      5        M  UR	                  U5        M     U(       d  UR	                  U5        GM  US   nUSS   H  nUR                  U5      nM     U(       aB  [        U6 nUR                  (       a  X-  nO$UR                  [        R                  Xq5      5      nUR	                  U5        GM     U(       d  [        R                  X5      n	OkUS   n	USS   H  nU	R                  U5      n	M     U(       aB  [        U6 nUR                  (       a  X-  n	O$U	R                  [        R                  XA5      5      n	U	R                  " UR!                  SS5      0 TD6$ )Nr   r   rY   r+  )r   r  r   r  r  r  r  r  r  r  r   r  rW   r}   r9  r   r-  )r   r   r3  
poly_termsrz  r  poly_factorsr  productrb   _polyr   s             rd   r[  poly.<locals>._poly  s   zMM$'D$&\---== ''f(:;]]v{{'9'9

--&**/ ''fkk3/33FJJ?A NN6* .  T"&q/*12.F%kk&1G /  ']F'')")++doof.J"K!!'*; (> __T/F]F"12D) ' E{>>NF#ZZ(BCF~~swwvr2;d;;rh   r  F)rq   r&  r%   r{   rW   rr   )r   rY   r   r   r[  s     ` @rd   rz  rz    sp     $#2<h 4=D||D(4(4((tX


d
+Crh   c                B   U S:  a  [        SU< SU < 35      eUS   USS pvUc  [        USS9u  p'[        U" [        U 5      /UQUP76 U5      nUc   [        R                  U[        S5      5      nO[        R                  X5      nU(       a  U$ UR                  5       $ )	a:  Common interface to the low-level polynomial generating functions
in orthopolys and appellseqs.

Parameters
==========

n : int
    Index of the polynomial, which may or may not equal its degree.
f : callable
    Low-level generating function to use.
K : Domain or None
    Domain in which to perform the computations. If None, use the smallest
    field containing the rationals and the extra parameters of x (see below).
name : str
    Name of an arbitrary individual polynomial in the sequence generated
    by f, only used in the error message for invalid n.
x : seq
    The first element of this argument is the main variable of all
    polynomials in this sequence. Any further elements are extra
    parameters required by f.
polys : bool, optional
    If True, return a Poly, otherwise (default) return an expression.
r   zCannot generate z
 of index r   NT)r   r"  )	r:  r+   r4   rR  r  r   r#   rW   r]   )	rT  r_   KrZ  r"  r  headtailrz  s	            rd   
named_polyra    s    0 	1uT1EFF1qu$y"4t4qQ"$""A&D|||D%*-xx#4,dlln,rh   r  r   )FNNNFFFr  r  )TTFr  )r  
__future__r   	functoolsr   r   operatorr   typingr   collectionsr	   r
   
sympy.corer   r   r   r   sympy.core.basicr   sympy.core.decoratorsr   sympy.core.exprtoolsr   r   r   sympy.core.evalfr   r   r   r   r   sympy.core.functionr   sympy.core.mulr   r   sympy.core.intfuncr   sympy.core.numbersr   r   r   sympy.core.relationalr    r!   sympy.core.sortingr"   sympy.core.symbolr#   r$   sympy.core.sympifyr%   r&   sympy.core.traversalr'   r(   sympy.logic.boolalgr)   sympy.polysr*   rq   sympy.polys.constructorr+   sympy.polys.domainsr,   r-   r.   !sympy.polys.domains.domainelementr/   sympy.polys.fglmtoolsr0   sympy.polys.groebnertoolsr1   r  sympy.polys.monomialsr2   sympy.polys.orderingsr3   sympy.polys.polyclassesr4   r5   r6   sympy.polys.polyerrorsr7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   sympy.polys.polyutilsrB   rC   rD   rE   rF   rG   sympy.polys.rationaltoolsrH   sympy.polys.rootisolationrI   sympy.utilitiesrJ   rK   rL   sympy.utilities.exceptionsrM   sympy.utilities.iterablesrN   rO   r  r  mpmath.libmp.libhyperrP   rg   rW   r  r  r  r  r
  r  r  r  r  r  r
  r  r  r  r  r  r  r  r  r  r?  rC  rG  rM  rP  rU  r_  r_  rd  rn  rh  r  rk  ro  rs  rv  rz  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r9  r  r  r  rz  ra  r+  rh   rd   <module>r     s   > " #   ,  # , A AM M * + # 7 7 6 & + 0 > + . 4 * * ; - ; * . 1 1	 	 	 	  / A 5 5 @ 4   /!J PE`5 PE` PE`fJ Zt Z Zz & &%P 0 0[| 7N 7Nt 1 1h ( (4 ! !0  2 ! !2  :  :  D  D  >  >  >  D # #L # #L 0 0f  : &+ ! !H  : % %P Q Qh 0 0f J JZ / /d r3 r3j  :  :  0 * *Z  :  :  : + +\ < <
    F  :=$ 6
:#z)FX8|~)X = =" 8 8" @ @"  _ _D 4 4n U U8 + +@ d= d=N z> z>z = =<  : ( (V # `C `CF 8 8v 2+ 2+j ? ?" \)E \) \)~ N Nb"-rh   