
    Rie                        d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZmZmZmZ dd
lmZ ddlmZmZ ddl m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) ddl*m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z7 ddl8m9Z9 ddl:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZD ddlEmFZF ddlGmHZH ddlImJZJmKZKmLZL ddlMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZX ddlYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_ ddl`maZa ddlbmcZc dd ldmeZemfZfmgZg dd!lhmiZi dd"ljmkZkmlZl dd#l5Zmdd#lnZndd$lompZp d% Zqef G d& d'e                      Zref G d( d)er                      Zsefd*             Ztd+ Zuefd,             Zvd- Zwd. Zxefdvd/            Zyefd0             Zzefd1             Z{efd2             Z|efd3             Z}efd4             Z~efd5             Zefd6             Zefd7             Zefd8             Zefd9             Zefd:             Zefd;             Zefd<             Zefd=             Zefd>             Zefd?             Zefd@             ZefdAdBdC            ZefdD             ZefdE             ZefdF             ZefdwdG            ZefdH             ZefdwdI            ZefdJ             ZefdK             ZefdL             ZefdM             ZefdN             ZefdO             ZefdP             ZefdQ             ZefdR             ZefdS             ZefdT             ZefdU             ZdV ZdW ZdX ZdY ZdZ Zd[ Zd\ Zd] Zefd^             Zefd_             Zefd`             ZefdAdadb            Zefdxdc            Zefdydd            Zefdzde            Zefd{dg            Zefd|dj            Zefdk             Zefdl             Zefdfdmdn            Zefdo             Zefdp             ZCefdq             Zef G dr dse                      Zefdt             Zdu Zd#S )}z8User-friendly public interface to polynomial functions.     )wrapsreducemul)Optional)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                 <     t                      fd            }|S )Nc                    t          |          }t          |t                    r | |          S t          |t                    r	  | j        |g| j        R  } | |          S # t          $ rb |j        r	t          cY S t          | 
                                j                  } ||          }|t          urt          ddd           |cY S w xY wt          S )Na@  
                        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gensr9   	is_MatrixNotImplementedgetattras_expr__name__rJ   )fgexpr_methodresultfuncs       5lib/python3.11/site-packages/sympy/polys/polytools.pywrapperz_polifyit.<locals>.wrapperD   s   QKKa 	"41::4   	""AK+AF+++& tAqzz!% #   ; *))))%aiikk4=AA$Q//- 273^	 	 	 	 !( "!s   A' 'CACC)r   )r_   ra   s   ` r`   	_polifyitrb   C   s3    
4[[" " " " ["8 N    c                   L    e Zd ZdZdZdZdZdZd Ze	d             Z
ed             Zed             Zd	 Ze	d
             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Z fdZed             Zed             Zed             Zed             Zed             Zed             Zed             Zd Z d Z!ddZ"d Z#d  Z$d! Z%d" Z&d# Z'd$ Z(dd%Z)d& Z*d' Z+d( Z,d) Z-d* Z.d+ Z/d, Z0dd-Z1dd.Z2dd/Z3dd0Z4dd1Z5d2 Z6d3 Z7d4 Z8d5 Z9d6 Z:dd8Z;dd9Z<d: Z=d; Z>d< Z?d= Z@dd>ZAd? ZBd@ ZCdA ZDdB ZEdC ZFdD ZGdE ZHdF ZIdG ZJdH ZKdI ZLdJ ZMdK ZNdL ZOdM ZPdN ZQdO ZRdP ZSddQZTddRZUddSZVddTZWdU ZXddWZYdX ZZdY Z[dZ Z\d[ Z]dd\Z^d] Z_dd^Z`d_ Zad` ZbddbZcddcZddddZeddeZfddfZgdg Zhdh ZiddiZjdj Zkdk Zldl ZmemZnddmZodn ZpddoZqddpZrddqZsdr Ztds ZuddtZvdu ZwddvZxddwZydx Zzdy Z{dz Z|d{ Z}dd|Z~d} Zd~ Zd Zd Zd Zd ZddZd Zd Zd Zd ZddZddZd Zd ZddZddZddZddZddZddZddZd Zd Zd ZddZd ZddZed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zd Zd Zed             Zed             Zed             Zed             Zed             Zed             Z ede          d             Zed             Zed             Zed             Zed             Zed             Zed             Z ede          d             Z ede          d             Z ede          d             Z ede          d             Zd ZddZddZdÄ Z xZS )rS   aP  
    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

    reprU   Tgn $@c                    t          j        ||          }d|v rt          d          t          |t          t
          t          t          f          r|                     ||          S t          |t                    rNt          |t                    r|                     ||          S |                     t          |          |          S t          |          }|j        r|                     ||          S |                     ||          S )z:Create a new polynomial instance out of something useful. orderz&'order' keyword is not implemented yet)exclude)optionsbuild_optionsNotImplementedErrorrR   r1   r2   r3   r,   _from_domain_elementrK   strdict
_from_dict
_from_listlistr"   is_Poly
_from_poly
_from_exprclsrf   rU   argsopts        r`   __new__zPoly.__new__   s    #D$//c>>%&NOOOcCc=9:: 	0++C555c3''' 	0#t$$ 6~~c3///~~d3ii555#,,C{ 0~~c3///~~c3///rc   c                     t          |t                    st          d|z            |j        t	          |          dz
  k    rt          d|d|          t          j        |           }||_        ||_        |S )z:Construct :class:`Poly` instance from raw representation. z%invalid polynomial representation: %s   zinvalid arguments: z, )	rR   r1   r9   levlenr   rz   rf   rU   )rw   rf   rU   objs       r`   newzPoly.new   s     #s## 	M!7#=? ? ?WD		A%%!/dd"KLLLmC  
rc   c                 T    t          | j                                        g| j        R  S N)r?   rf   to_sympy_dictrU   selfs    r`   exprz	Poly.expr   s(    tx5577D$)DDDDrc   c                 "    | j         f| j        z   S r   )r   rU   r   s    r`   rx   z	Poly.args   s    	|di''rc   c                 "    | j         f| j        z   S r   re   r   s    r`   _hashable_contentzPoly._hashable_content   s    {TY&&rc   c                 X    t          j        ||          }|                     ||          S )(Construct a polynomial from a ``dict``. )rj   rk   rp   rv   s        r`   	from_dictzPoly.from_dict   *     #D$//~~c3'''rc   c                 X    t          j        ||          }|                     ||          S )(Construct a polynomial from a ``list``. )rj   rk   rq   rv   s        r`   	from_listzPoly.from_list   r   rc   c                 X    t          j        ||          }|                     ||          S )*Construct a polynomial from a polynomial. )rj   rk   rt   rv   s        r`   	from_polyzPoly.from_poly   r   rc   c                 X    t          j        ||          }|                     ||          S +Construct a polynomial from an expression. )rj   rk   ru   rv   s        r`   rT   zPoly.from_expr   r   rc   c                 :   |j         }|st          d          t          |          dz
  }|j        }|t	          ||          \  }}n2|                                D ]\  }}|                    |          ||<    | j        t          j	        |||          g|R  S )r   z0Cannot initialize from 'dict' without generatorsr|   Nry   )
rU   r8   r~   domainr(   itemsconvertr   r1   r   )rw   rf   ry   rU   levelr   monomcoeffs           r`   rp   zPoly._from_dict   s     x 	D"BD D D D		A>*3C888KFCC #		 3 3u#^^E22E

sws}S%88@4@@@@rc   c                 ^   |j         }|st          d          t          |          dk    rt          d          t          |          dz
  }|j        }|t          ||          \  }}n"t          t          |j        |                    } | j	        t          j        |||          g|R  S )r   z0Cannot initialize from 'list' without generatorsr|   z#'list' representation not supportedNr   )rU   r8   r~   r:   r   r(   rr   mapr   r   r1   r   )rw   rf   ry   rU   r   r   s         r`   rq   zPoly._from_list  s     x 	7"BD D DYY!^^-57 7 7 D		A>*3C888KFCCs6>3//00Csws}S%88@4@@@@rc   c                    | |j         k    r | j        |j        g|j        R  }|j        }|j        }|j        }|rb|j        |k    rWt          |j                  t          |          k    r(|                     |                                |          S  |j	        | }d|v r|r|
                    |          }n|du r|                                }|S )r   r   T)	__class__r   rf   rU   fieldr   setru   rY   reorder
set_domainto_field)rw   rf   ry   rU   r   r   s         r`   rt   zPoly._from_poly  s     #-#'#'-CH---Cx	 	)CH$$38}}D		))~~ckkmmS999!ck4(s??v?..((CCd]],,..C
rc   c                 T    t          ||          \  }}|                     ||          S r   )rC   rp   )rw   rf   ry   s      r`   ru   zPoly._from_expr4  s+     #3,,S~~c3'''rc   c                     |j         }|j        }t          |          dz
  }|                    |          g} | j        t          j        |||          g|R  S Nr|   )rU   r   r~   r   r   r1   r   )rw   rf   ry   rU   r   r   s         r`   rm   zPoly._from_domain_element:  s[    xD		A~~c""#sws}S%88@4@@@@rc   c                 D    t                                                      S r   super__hash__r   r   s    r`   r   zPoly.__hash__D      ww!!!rc   c                     t                      }| j        }t          t          |                    D ]3}|                                 D ]}||         r|||         j        z  } n4|| j        z  S )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   rU   ranger~   monomsfree_symbolsfree_symbols_in_domain)r   symbolsrU   ir   s        r`   r   zPoly.free_symbolsG  s    * %%ys4yy!! 	 	A  8 tAw33GE 444rc   c                     | j         j        t                      }}|j        r|j        D ]}||j        z  }n(|j        r!|                                 D ]}||j        z  }|S )aj  
        Free symbols of the domain of ``self``.

        Examples
        ========

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

        >>> Poly(x**2 + 1).free_symbols_in_domain
        set()
        >>> Poly(x**2 + y).free_symbols_in_domain
        set()
        >>> Poly(x**2 + y, x).free_symbols_in_domain
        {y}

        )rf   domr   is_Compositer   r   is_EXcoeffs)r   r   r   genr   s        r`   r   zPoly.free_symbols_in_domainf  sz    & (, 	.~ , ,3++,\ 	. . .5--rc   c                     | j         d         S )z
        Return the principal generator.

        Examples
        ========

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

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

        r   rU   r   s    r`   r   zPoly.gen  s     y|rc   c                 *    |                                  S )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    r`   r   zPoly.domain  s    *    rc   c                      | j         | j                            | j        j        | j        j                  g| j        R  S )z3Return zero polynomial with ``self``'s properties. )r   rf   zeror}   r   rU   r   s    r`   r   z	Poly.zero  8     txdhlDHLAANDINNNNrc   c                      | j         | j                            | j        j        | j        j                  g| j        R  S )z2Return one polynomial with ``self``'s properties. )r   rf   oner}   r   rU   r   s    r`   r   zPoly.one  s8     txTX\48<@@M49MMMMrc   c                      | j         | j                            | j        j        | j        j                  g| j        R  S )z3Return unit polynomial with ``self``'s properties. )r   rf   unitr}   r   rU   r   s    r`   r   z	Poly.unit  r   rc   c                 b    |                      |          \  }}}} ||           ||          fS )a  
        Make ``f`` and ``g`` belong to the same domain.

        Examples
        ========

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

        >>> f, g = Poly(x/2 + 1), Poly(2*x + 1)

        >>> f
        Poly(1/2*x + 1, x, domain='QQ')
        >>> g
        Poly(2*x + 1, x, domain='ZZ')

        >>> F, G = f.unify(g)

        >>> F
        Poly(1/2*x + 1, x, domain='QQ')
        >>> G
        Poly(2*x + 1, x, domain='QQ')

        )_unify)r[   r\   _perFGs         r`   unifyz
Poly.unify  s5    2 xx{{31s1vvss1vv~rc   c           
      F    t                    j        ss	  j        j         j         j         j                             j        j                                                fS # t          $ r t          d d          w xY wt           j        t                    rt          j        t                    rt           j        j                  } j        j                            j        j        |          t          |          dz
  c} j        |k    rt           j                                         j        |          \  }} j        j        k    r fd|D             }t          t!          t#          t%          ||                              |          }n j                                      }j        |k    rt          j                                        j        |          \  }}j        j        k    rfd|D             }t          t!          t#          t%          ||                              |          }	n0j                                      }	nt          d d           j        |d ffd	}
|
||	fS )NCannot unify  with r|   c                 P    g | ]"}                     |j        j                  #S  r   rf   r   ).0cr   r[   s     r`   
<listcomp>zPoly._unify.<locals>.<listcomp>  +    LLLaAquy 9 9LLLrc   c                 P    g | ]"}                     |j        j                  #S r   r   )r   r   r   r\   s     r`   r   zPoly._unify.<locals>.<listcomp>  r   rc   c                     |/|d |         ||dz   d          z   }|s|                     |           S  j        | g|R  S r   to_sympyr   rf   r   rU   removerw   s       r`   r   zPoly._unify.<locals>.per  \    !GVG}tFQJKK'88 -<<,,,373&&&&&rc   )r"   rs   rf   r   r   
from_sympyr6   r7   rR   r1   rA   rU   r   r~   rB   to_dictro   rr   zipr   r   )r[   r\   rU   r}   f_monomsf_coeffsr   g_monomsg_coeffsr   r   rw   r   s   ``         @@r`   r   zPoly._unify  s   AJJy 	LLuy!%		!%):N:Nq:Q:Q0R0RRR! L L L''QQQ(JKKKL aeS!! 	Hj&<&< 	Hqvqv..Duyquy$77TQHCv~~%2EMMOOQVT&3 &3"( 59##LLLLL8LLLHT#h"9"9::;;S#FFEMM#&&v~~%2EMMOOQVT&3 &3"( 59##LLLLL8LLLHT#h"9"9::;;S#FFEMM#&&##AA$FGGGk4 	' 	' 	' 	' 	' 	' CA~s   AA+ + BNc                     || j         }|9|d|         ||dz   d         z   }|s| j        j                            |          S  | j        j        |g|R  S )ab  
        Create a Poly out of the given representation.

        Examples
        ========

        >>> from sympy import Poly, ZZ
        >>> from sympy.abc import x, y

        >>> from sympy.polys.polyclasses import DMP

        >>> a = Poly(x**2 + 1)

        >>> a.per(DMP([ZZ(1), ZZ(1)], ZZ), gens=[y])
        Poly(y + 1, y, domain='ZZ')

        Nr|   )rU   rf   r   r   r   r   )r[   rf   rU   r   s       r`   r   zPoly.per
  sq    $ <6D=4
#44D /uy))#...q{s*T****rc   c                     t          j        | j        d|i          }|                     | j                            |j                            S )z Set the ground domain of ``f``. r   )rj   rk   rU   r   rf   r   r   )r[   r   ry   s      r`   r   zPoly.set_domain'  s=    #AFXv,>??uuQU]]3:..///rc   c                     | j         j        S )z Get the ground domain of ``f``. )rf   r   r[   s    r`   r   zPoly.get_domain,  s    uyrc   c                     t           j                            |          }|                     t	          |                    S )z
        Set the modulus of ``f``.

        Examples
        ========

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

        >>> Poly(5*x**2 + 2*x - 1, x).set_modulus(2)
        Poly(x**2 + 1, x, modulus=2)

        )rj   Modulus
preprocessr   r)   )r[   moduluss     r`   set_moduluszPoly.set_modulus0  s1     /,,W55||BwKK(((rc   c                     |                                  }|j        r!t          |                                          S t	          d          )z
        Get the modulus of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + 1, modulus=2).get_modulus()
        2

        z$not a polynomial over a Galois field)r   is_FiniteFieldr   characteristicr9   )r[   r   s     r`   get_moduluszPoly.get_modulusA  sF       	J60022333!"HIIIrc   c                     || j         v rD|j        r|                     ||          S 	 |                     ||          S # t          $ r Y nw xY w|                                                     ||          S )z)Internal implementation of :func:`subs`. )rU   	is_numberevalreplacer9   rY   subs)r[   oldr   s      r`   
_eval_subszPoly._eval_subsV  s    !&==} vvc3'''99S#...&   D yy{{S)))s   > 
A
Ac                     | j                                         \  }fdt          | j                  D             }|                     ||          S )a  
        Remove unnecessary generators from ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import a, b, c, d, x

        >>> Poly(a + x, a, b, c, d, x).exclude()
        Poly(a + x, a, x, domain='ZZ')

        c                 "    g | ]\  }}|v	|S r   r   )r   jr   Js      r`   r   z Poly.exclude.<locals>.<listcomp>r  s"    BBB3qzzzzzrc   r   )rf   ri   	enumeraterU   r   )r[   r   rU   r   s      @r`   ri   zPoly.excludec  sQ     3BBBB)AF"3"3BBBuuStu$$$rc   c                    | | j         r
| j        |}}nt          d          ||k    s	|| j        vr| S || j        v ru|| j        vrl|                                 }|j        r	||j        vrHt          | j                  }|||                    |          <   | 	                    | j
        |          S t          d|d|d|           )a  
        Replace ``x`` with ``y`` in generators list.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).replace(x, y)
        Poly(y**2 + 1, y, domain='ZZ')

        Nz(syntax supported only in univariate caser   zCannot replace r    in )is_univariater   r9   rU   r   r   r   rr   indexr   rf   )r[   xy_ignorer   rU   s         r`   r   zPoly.replacev  s     9 @ua1%>@ @ @ 66Qaf__H;;1AF??,,..C# /q';';AF||&'TZZ]]#uuQUu...o111aaaKLLLrc   c                 @     |                                  j        |i |S )z-Match expression from Poly. See Basic.match())rY   match)r[   rx   kwargss      r`   r	  z
Poly.match  s"     qyy{{ $1&111rc   c                    t          j        d|          }|st          | j        |          }n4t	          | j                  t	          |          k    rt          d          t          t          t          t          | j
                                        | j        |                               }|                     t          || j
        j        t          |          dz
            |          S )a  
        Efficiently apply new order of generators.

        Examples
        ========

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

        >>> Poly(x**2 + x*y**2, x, y).reorder(y, x)
        Poly(y**2*x + x**2, y, x, domain='ZZ')

        r   r   z7generators list can differ only up to order of elementsr|   r   )rj   Optionsr@   rU   r   r9   ro   rr   r   rB   rf   r   r   r1   r   r~   )r[   rU   rx   ry   rf   s        r`   r   zPoly.reorder  s     ob$'' 	Kaf#...DD[[CII%%!IK K K 4]15==??AFDIIJKKLLuuSaeiTQ77duCCCrc   c                    |                      d          }|                     |          }i }|                                D ];\  }}t          |d|                   rt	          d| z            ||||d         <   <| j        |d         } | j        t          j        |t          |          dz
  | j
        j                  g|R  S )a(  
        Remove dummy generators from ``f`` that are to the left of
        specified ``gen`` in the generators as ordered. When ``gen``
        is an integer, it refers to the generator located at that
        position within the tuple of generators of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> Poly(y**2 + y*z**2, x, y, z).ltrim(y)
        Poly(y**2 + y*z**2, y, z, domain='ZZ')
        >>> Poly(z, x, y, z).ltrim(-1)
        Poly(z, z, domain='ZZ')

        T)nativeNzCannot left trim %sr|   )as_dict_gen_to_levelr   anyr9   rU   r   r1   r   r~   rf   r   )r[   r   rf   r   termsr   r   rU   s           r`   ltrimz
Poly.ltrim  s    & iiti$$OOC  IIKK 	% 	%LE55!9~~ A%&;a&?@@@$E%)vabbzquS]5#d))a-CCKdKKKKrc   c                 >   t                      }|D ]U}	 | j                            |          }|                    |           3# t          $ r t          | d|d          w xY w|                                 D ]!}t          |          D ]\  }}||vr|r  dS "dS )aJ  
        Return ``True`` if ``Poly(f, *gens)`` retains ground domain.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> Poly(x*y + 1, x, y, z).has_only_gens(x, y)
        True
        >>> Poly(x*y + z, x, y, z).has_only_gens(x, y)
        False

         doesn't have  as generatorFT)r   rU   r  add
ValueErrorr>   r   r   )r[   rU   indicesr   r  r   r   elts           r`   has_only_genszPoly.has_only_gens  s      %% 	# 	#C#S))
 E""""	  B B B%9:CCC@B B BB XXZZ 	! 	!E#E** ! !3G### 555! ts   A A$c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Make the ground domain a ring.

        Examples
        ========

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

        >>> Poly(x**2 + 1, domain=QQ).to_ring()
        Poly(x**2 + 1, x, domain='ZZ')

        to_ring)hasattrrf   r  r4   r   r[   r^   s     r`   r  zPoly.to_ring  sI     15)$$ 	6U]]__FF'9555uuV}}rc   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Make the ground domain a field.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x, domain=ZZ).to_field()
        Poly(x**2 + 1, x, domain='QQ')

        r   )r  rf   r   r4   r   r  s     r`   r   zPoly.to_field  K     15*%% 	7U^^%%FF':666uuV}}rc   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Make the ground domain exact.

        Examples
        ========

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

        >>> Poly(x**2 + 1.0, x, domain=RR).to_exact()
        Poly(x**2 + 1, x, domain='QQ')

        to_exact)r  rf   r#  r4   r   r  s     r`   r#  zPoly.to_exact'  r!  rc   c                     t          |                     d          || j        j        pd          \  }}|                     || j        |          S )a  
        Recalculate the ground domain of a polynomial.

        Examples
        ========

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

        >>> f = Poly(x**2 + 1, x, domain='QQ[y]')
        >>> f
        Poly(x**2 + 1, x, domain='QQ[y]')

        >>> f.retract()
        Poly(x**2 + 1, x, domain='ZZ')
        >>> f.retract(field=True)
        Poly(x**2 + 1, x, domain='QQ')

        Tr   N)r   	compositer   )r(   r  r   r   r   rU   )r[   r   r   rf   s       r`   retractzPoly.retract<  sX    ( $AII4I$8$818#8#@DB B BS{{3s{333rc   c                 (   |d||}}}n|                      |          }t          |          t          |          }}t          | j        d          r| j                            |||          }nt          | d          |                     |          S )z1Take a continuous subsequence of terms of ``f``. Nr   slice)r  intr  rf   r*  r4   r   )r[   r  mnr   r^   s         r`   r*  z
Poly.sliceT  s    9A!qAA""A1vvs1vv115'"" 	4U[[Aq))FF'7333uuV}}rc   c                 R      fd j                             |          D             S )aQ  
        Returns all non-zero coefficients from ``f`` in lex order.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x + 3, x).coeffs()
        [1, 2, 3]

        See Also
        ========
        all_coeffs
        coeff_monomial
        nth

        c                 N    g | ]!}j         j                            |          "S r   rf   r   r   r   r   r[   s     r`   r   zPoly.coeffs.<locals>.<listcomp>x  s+    III!	""1%%IIIrc   rh   )rf   r   r[   rh   s   ` r`   r   zPoly.coeffsd  s0    ( JIIIqu||%|/H/HIIIIrc   c                 8    | j                             |          S )aU  
        Returns all non-zero monomials from ``f`` in lex order.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).monoms()
        [(2, 0), (1, 2), (1, 1), (0, 1)]

        See Also
        ========
        all_monoms

        r2  )rf   r   r3  s     r`   r   zPoly.monomsz  s    $ u||%|(((rc   c                 R      fd j                             |          D             S )ac  
        Returns all non-zero terms from ``f`` in lex order.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).terms()
        [((2, 0), 1), ((1, 2), 2), ((1, 1), 1), ((0, 1), 3)]

        See Also
        ========
        all_terms

        c                 X    g | ]&\  }}|j         j                            |          f'S r   r0  r   r,  r   r[   s      r`   r   zPoly.terms.<locals>.<listcomp>  s4    PPPtq!AEI&&q))*PPPrc   r2  )rf   r  r3  s   ` r`   r  z
Poly.terms  s0    $ QPPPqu{{{7O7OPPPPrc   c                 N      fd j                                         D             S )a  
        Returns all coefficients from a univariate polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x - 1, x).all_coeffs()
        [1, 0, 2, -1]

        c                 N    g | ]!}j         j                            |          "S r   r0  r1  s     r`   r   z#Poly.all_coeffs.<locals>.<listcomp>  s+    BBB!	""1%%BBBrc   )rf   
all_coeffsr   s   `r`   r:  zPoly.all_coeffs  s.     CBBBqu/?/?/A/ABBBBrc   c                 4    | j                                         S )a?  
        Returns all monomials from a univariate polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x - 1, x).all_monoms()
        [(3,), (2,), (1,), (0,)]

        See Also
        ========
        all_terms

        )rf   
all_monomsr   s    r`   r<  zPoly.all_monoms  s    $ u!!!rc   c                 N      fd j                                         D             S )a  
        Returns all terms from a univariate polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x - 1, x).all_terms()
        [((3,), 1), ((2,), 0), ((1,), 2), ((0,), -1)]

        c                 X    g | ]&\  }}|j         j                            |          f'S r   r0  r7  s      r`   r   z"Poly.all_terms.<locals>.<listcomp>  s4    IIItq!AEI&&q))*IIIrc   )rf   	all_termsr   s   `r`   r?  zPoly.all_terms  s,     JIIIqu7H7HIIIIrc   c                     i }|                                  D ]L\  }} |||          }t          |t                    r|\  }}n|}|r||vr|||<   :t          d|z            M | j        |g|p| j        R i |S )ah  
        Apply a function to all terms of ``f``.

        Examples
        ========

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

        >>> def func(k, coeff):
        ...     k = k[0]
        ...     return coeff//10**(2-k)

        >>> Poly(x**2 + 20*x + 400).termwise(func)
        Poly(x**2 + 2*x + 4, x, domain='ZZ')

        z%s monomial was generated twice)r  rR   tupler9   r   rU   )r[   r_   rU   rx   r  r   r   r^   s           r`   termwisezPoly.termwise  s    $ GGII 	C 	CLE5T%''F&%(( %uu C%%#(E%LL)9EAC C C	C q{5<DNAF<<<t<<<rc   c                 D    t          |                                           S )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~   r  r   s    r`   lengthzPoly.length  s     199;;rc   Fc                 r    |r| j                             |          S | j                             |          S )a  
        Switch to a ``dict`` representation.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x*y**2 - y, x, y).as_dict()
        {(0, 1): -1, (1, 2): 2, (2, 0): 1}

        r%  )rf   r   r   )r[   r  r   s      r`   r  zPoly.as_dict  s;      	25==d=+++5&&D&111rc   c                 j    |r| j                                         S | j                                         S )z%Switch to a ``list`` representation. )rf   to_listto_sympy_list)r[   r  s     r`   as_listzPoly.as_list  s.     	)5==??"5&&(((rc   c                    |s| j         S t          |          dk    rt          |d         t                    rt|d         }t	          | j                  }|                                D ]C\  }}	 |                    |          }|||<   !# t          $ r t          | d|d          w xY wt          | j                                        g|R  S )ar  
        Convert a Poly instance to an Expr instance.

        Examples
        ========

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

        >>> f = Poly(x**2 + 2*x*y**2 - y, x, y)

        >>> f.as_expr()
        x**2 + 2*x*y**2 - y
        >>> f.as_expr({x: 5})
        10*y**2 - y + 25
        >>> f.as_expr(5, 6)
        379

        r|   r   r  r  )r   r~   rR   ro   rr   rU   r   r  r  r>   r?   rf   r   )r[   rU   mappingr   valuer  s         r`   rY   zPoly.as_expr%  s    (  	6Mt99>>ja$77>1gG<<D%mmoo ( (
U( JJsOOE
 #(DKK	 " F F F)=>QQDF F FF qu2244<t<<<<s   .B		 B)c                 \    	 t          | g|R i |}|j        sdS |S # t          $ r Y dS w xY w)a{  Converts ``self`` to a polynomial or returns ``None``.

        >>> from sympy import sin
        >>> from sympy.abc import x, y

        >>> print((x**2 + x*y).as_poly())
        Poly(x**2 + x*y, x, y, domain='ZZ')

        >>> print((x**2 + x*y).as_poly(x, y))
        Poly(x**2 + x*y, x, y, domain='ZZ')

        >>> print((x**2 + sin(y)).as_poly(x, y))
        None

        N)rS   rs   r9   )r   rU   rx   polys       r`   as_polyzPoly.as_polyK  sZ     	,t,,,t,,D< t 	 	 	44	s     
++c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )a  
        Convert algebraic coefficients to rationals.

        Examples
        ========

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

        >>> Poly(x**2 + I*x + 1, x, extension=I).lift()
        Poly(x**4 + 3*x**2 + 1, x, domain='QQ')

        lift)r  rf   rQ  r4   r   r  s     r`   rQ  z	Poly.lifte  sI     15&!! 	3UZZ\\FF'6222uuV}}rc   c                     t          | j        d          r| j                                        \  }}nt          | d          ||                     |          fS )a+  
        Reduce degree of ``f`` by mapping ``x_i**m`` to ``y_i``.

        Examples
        ========

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

        >>> Poly(x**6*y**2 + x**3 + 1, x, y).deflate()
        ((3, 2), Poly(x**2*y + x + 1, x, y, domain='ZZ'))

        deflate)r  rf   rS  r4   r   r[   r   r^   s      r`   rS  zPoly.deflatez  sR     15)$$ 	6IAvv'9555!%%--rc   c                 >   | j         j        }|j        r| S |j        st	          d|z            t          | j         d          r| j                             |          }nt          | d          |r|j        | j	        z   }n| j	        |j        z   } | j
        |g|R  S )a  
        Inject ground domain generators into ``f``.

        Examples
        ========

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

        >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x)

        >>> f.inject()
        Poly(x**2*y + x*y**3 + x*y + 1, x, y, domain='ZZ')
        >>> f.inject(front=True)
        Poly(y**3*x + y*x**2 + y*x + 1, y, x, domain='ZZ')

        z Cannot inject generators over %sinjectfront)rf   r   is_Numericalrs   r5   r  rV  r4   r   rU   r   )r[   rX  r   r^   rU   s        r`   rV  zPoly.inject  s    $ ei 	HH 	H@3FGGG15(## 	5U\\\..FF'8444 	(;'DD6CK'DquV#d####rc   c                    | j         j        }|j        st          d|z            t	          |          }| j        d|         |k    r| j        |d         d}}n6| j        | d         |k    r| j        d|          d}}nt          d           |j        | }t          | j         d          r| j         	                    ||          }nt          | d           | j        |g|R  S )a  
        Eject selected generators into the ground domain.

        Examples
        ========

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

        >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x, y)

        >>> f.eject(x)
        Poly(x*y**3 + (x**2 + x)*y + 1, y, domain='ZZ[x]')
        >>> f.eject(y)
        Poly(y*x**2 + (y**3 + y)*x + 1, x, domain='ZZ[y]')

        zCannot eject generators over %sNTFz'can only eject front or back generatorsejectrW  )rf   r   rY  r5   r~   rU   rl   rV  r  r[  r4   r   )r[   rU   r   k_gensrX  r^   s          r`   r[  z
Poly.eject  s   $ ei 	G?#EFFFII6"1":6!"":t5EEVQBCC[D  6#A2#;5EE%9; ; ; cj$15'"" 	4U[[E[22FF'7333quV$e$$$$rc   c                     t          | j        d          r| j                                        \  }}nt          | d          ||                     |          fS )a  
        Remove GCD of terms from the polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x**6*y**2 + x**3*y, x, y).terms_gcd()
        ((3, 1), Poly(x**3*y + 1, x, y, domain='ZZ'))

        	terms_gcd)r  rf   r_  r4   r   rT  s      r`   r_  zPoly.terms_gcd  sT     15+&& 	8))IAvv';777!%%--rc   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )z
        Add an element of the ground domain to ``f``.

        Examples
        ========

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

        >>> Poly(x + 1).add_ground(2)
        Poly(x + 3, x, domain='ZZ')

        
add_ground)r  rf   ra  r4   r   r[   r   r^   s      r`   ra  zPoly.add_ground  O     15,'' 	9U%%e,,FF'<888uuV}}rc   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )z
        Subtract an element of the ground domain from ``f``.

        Examples
        ========

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

        >>> Poly(x + 1).sub_ground(2)
        Poly(x - 1, x, domain='ZZ')

        
sub_ground)r  rf   re  r4   r   rb  s      r`   re  zPoly.sub_ground  rc  rc   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )z
        Multiply ``f`` by a an element of the ground domain.

        Examples
        ========

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

        >>> Poly(x + 1).mul_ground(2)
        Poly(2*x + 2, x, domain='ZZ')

        
mul_ground)r  rf   rg  r4   r   rb  s      r`   rg  zPoly.mul_ground  rc  rc   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )aO  
        Quotient of ``f`` by a an element of the ground domain.

        Examples
        ========

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

        >>> Poly(2*x + 4).quo_ground(2)
        Poly(x + 2, x, domain='ZZ')

        >>> Poly(2*x + 3).quo_ground(2)
        Poly(x + 1, x, domain='ZZ')

        
quo_ground)r  rf   ri  r4   r   rb  s      r`   ri  zPoly.quo_ground2  sO    " 15,'' 	9U%%e,,FF'<888uuV}}rc   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )a  
        Exact quotient of ``f`` by a an element of the ground domain.

        Examples
        ========

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

        >>> Poly(2*x + 4).exquo_ground(2)
        Poly(x + 2, x, domain='ZZ')

        >>> Poly(2*x + 3).exquo_ground(2)
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2 does not divide 3 in ZZ

        exquo_ground)r  rf   rk  r4   r   rb  s      r`   rk  zPoly.exquo_groundJ  sO    & 15.)) 	;U''..FF'>:::uuV}}rc   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Make all coefficients in ``f`` positive.

        Examples
        ========

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

        >>> Poly(x**2 - 1, x).abs()
        Poly(x**2 + 1, x, domain='ZZ')

        abs)r  rf   rm  r4   r   r  s     r`   rm  zPoly.absd  sI     15%   	2UYY[[FF'5111uuV}}rc   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )a4  
        Negate all coefficients in ``f``.

        Examples
        ========

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

        >>> Poly(x**2 - 1, x).neg()
        Poly(-x**2 + 1, x, domain='ZZ')

        >>> -Poly(x**2 - 1, x)
        Poly(-x**2 + 1, x, domain='ZZ')

        neg)r  rf   ro  r4   r   r  s     r`   ro  zPoly.negy  I    " 15%   	2UYY[[FF'5111uuV}}rc   c                    t          |          }|j        s|                     |          S |                     |          \  }}}}t	          | j        d          r|                    |          }nt          | d           ||          S )a[  
        Add two polynomials ``f`` and ``g``.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).add(Poly(x - 2, x))
        Poly(x**2 + x - 1, x, domain='ZZ')

        >>> Poly(x**2 + 1, x) + Poly(x - 2, x)
        Poly(x**2 + x - 1, x, domain='ZZ')

        r  )r"   rs   ra  r   r  rf   r  r4   r[   r\   r   r   r   r   r^   s          r`   r  zPoly.add      " AJJy 	#<<??"xx{{3115%   	2UU1XXFF'5111s6{{rc   c                    t          |          }|j        s|                     |          S |                     |          \  }}}}t	          | j        d          r|                    |          }nt          | d           ||          S )a`  
        Subtract two polynomials ``f`` and ``g``.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).sub(Poly(x - 2, x))
        Poly(x**2 - x + 3, x, domain='ZZ')

        >>> Poly(x**2 + 1, x) - Poly(x - 2, x)
        Poly(x**2 - x + 3, x, domain='ZZ')

        sub)r"   rs   re  r   r  rf   ru  r4   rr  s          r`   ru  zPoly.sub  rs  rc   c                    t          |          }|j        s|                     |          S |                     |          \  }}}}t	          | j        d          r|                    |          }nt          | d           ||          S )ap  
        Multiply two polynomials ``f`` and ``g``.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).mul(Poly(x - 2, x))
        Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ')

        >>> Poly(x**2 + 1, x)*Poly(x - 2, x)
        Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ')

        r   )r"   rs   rg  r   r  rf   r   r4   rr  s          r`   r   zPoly.mul  rs  rc   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )a3  
        Square a polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x - 2, x).sqr()
        Poly(x**2 - 4*x + 4, x, domain='ZZ')

        >>> Poly(x - 2, x)**2
        Poly(x**2 - 4*x + 4, x, domain='ZZ')

        sqr)r  rf   rx  r4   r   r  s     r`   rx  zPoly.sqr  rp  rc   c                     t          |          }t          | j        d          r| j                            |          }nt	          | d          |                     |          S )aX  
        Raise ``f`` to a non-negative power ``n``.

        Examples
        ========

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

        >>> Poly(x - 2, x).pow(3)
        Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')

        >>> Poly(x - 2, x)**3
        Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')

        pow)r+  r  rf   rz  r4   r   r[   r-  r^   s      r`   rz  zPoly.pow  sV    " FF15%   	2UYYq\\FF'5111uuV}}rc   c                     |                      |          \  }}}}t          | j        d          r|                    |          \  }}nt	          | d           ||           ||          fS )a#  
        Polynomial pseudo-division of ``f`` by ``g``.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).pdiv(Poly(2*x - 4, x))
        (Poly(2*x + 4, x, domain='ZZ'), Poly(20, x, domain='ZZ'))

        pdiv)r   r  rf   r}  r4   )r[   r\   r   r   r   r   qrs           r`   r}  z	Poly.pdiv   sn     xx{{3115&!! 	366!99DAqq'6222s1vvss1vv~rc   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )aN  
        Polynomial pseudo-remainder of ``f`` by ``g``.

        Caveat: The function prem(f, g, x) can be safely used to compute
          in Z[x] _only_ subresultant polynomial remainder sequences (prs's).

          To safely compute Euclidean and Sturmian prs's in Z[x]
          employ anyone of the corresponding functions found in
          the module sympy.polys.subresultants_qq_zz. The functions
          in the module with suffix _pg compute prs's in Z[x] employing
          rem(f, g, x), whereas the functions with suffix _amv
          compute prs's in Z[x] employing rem_z(f, g, x).

          The function rem_z(f, g, x) differs from prem(f, g, x) in that
          to compute the remainder polynomials in Z[x] it premultiplies
          the divident times the absolute value of the leading coefficient
          of the divisor raised to the power degree(f, x) - degree(g, x) + 1.


        Examples
        ========

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

        >>> Poly(x**2 + 1, x).prem(Poly(2*x - 4, x))
        Poly(20, x, domain='ZZ')

        prem)r   r  rf   r  r4   rr  s          r`   r  z	Poly.prem7  s^    < xx{{3115&!! 	3VVAYYFF'6222s6{{rc   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )a  
        Polynomial pseudo-quotient of ``f`` by ``g``.

        See the Caveat note in the function prem(f, g).

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).pquo(Poly(2*x - 4, x))
        Poly(2*x + 4, x, domain='ZZ')

        >>> Poly(x**2 - 1, x).pquo(Poly(2*x - 2, x))
        Poly(2*x + 2, x, domain='ZZ')

        pquo)r   r  rf   r  r4   rr  s          r`   r  z	Poly.pquo^  s^    & xx{{3115&!! 	3VVAYYFF'6222s6{{rc   c                 \   |                      |          \  }}}}t          | j        d          rc	 |                    |          }n\# t          $ r?}|                    |                                 |                                          d}~ww xY wt          | d           ||          S )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   r  rf   r  r;   r   rY   r4   )r[   r\   r   r   r   r   r^   excs           r`   r  zPoly.pexquoz  s    & xx{{3115(## 	58!& 8 8 8ggaiikk199;;7778 (8444s6{{s   A 
B:BBc                    |                      |          \  }}}}d}|r8|j        r1|j        s*|                                |                                }}d}t	          | j        d          r|                    |          \  }}	nt          | d          |r>	 |                                |	                                }}
|
|}	}n# t          $ r Y nw xY w ||           ||	          fS )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   r  rf   r  r4   r  r6   )r[   r\   autor   r   r   r   r(  r~  r  QRs               r`   r  zPoly.div  s   " !S!Q 	CK 	 	::<<qAG15%   	25588DAqq'5111 	yy{{AIIKK1 !1 "   
 s1vvss1vv~s   (C 
CCc                    |                      |          \  }}}}d}|r8|j        r1|j        s*|                                |                                }}d}t	          | j        d          r|                    |          }nt          | d          |r&	 |                                }n# t          $ r Y nw xY w ||          S )ao  
        Computes the polynomial remainder of ``f`` by ``g``.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x))
        Poly(5, x, domain='ZZ')

        >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x), auto=False)
        Poly(x**2 + 1, x, domain='ZZ')

        FTrem)
r   r  r  r   r  rf   r  r4   r  r6   )	r[   r\   r  r   r   r   r   r(  r  s	            r`   r  zPoly.rem      " !S!Q 	CK 	 	::<<qAG15%   	2aAA'5111 	IIKK!    s1vv   B* *
B76B7c                    |                      |          \  }}}}d}|r8|j        r1|j        s*|                                |                                }}d}t	          | j        d          r|                    |          }nt          | d          |r&	 |                                }n# t          $ r Y nw xY w ||          S )aa  
        Computes polynomial quotient of ``f`` by ``g``.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).quo(Poly(2*x - 4, x))
        Poly(1/2*x + 1, x, domain='QQ')

        >>> Poly(x**2 - 1, x).quo(Poly(x - 1, x))
        Poly(x + 1, x, domain='ZZ')

        FTquo)
r   r  r  r   r  rf   r  r4   r  r6   )	r[   r\   r  r   r   r   r   r(  r~  s	            r`   r  zPoly.quo  r  r  c                 $   |                      |          \  }}}}d}|r8|j        r1|j        s*|                                |                                }}d}t	          | j        d          rc	 |                    |          }n\# t          $ r?}	|	                    | 	                                |	                                          d}	~	ww xY wt          | d          |r&	 |                                }n# t          $ r Y nw xY w ||          S )a  
        Computes polynomial exact quotient of ``f`` by ``g``.

        Examples
        ========

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

        >>> Poly(x**2 - 1, x).exquo(Poly(x - 1, x))
        Poly(x + 1, x, domain='ZZ')

        >>> Poly(x**2 + 1, x).exquo(Poly(2*x - 4, x))
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

        FTexquoN)r   r  r  r   r  rf   r  r;   r   rY   r4   r  r6   )
r[   r\   r  r   r   r   r   r(  r~  r  s
             r`   r  z
Poly.exquo
  s"   & !S!Q 	CK 	 	::<<qAG15'"" 	48GGAJJ& 8 8 8ggaiikk199;;7778 (7333 	IIKK!    s1vvs*   -B 
C:CC"C7 7
DDc                 P   t          |t                    rJt          | j                  }| |cxk    r|k     rn n|dk     r||z   S |S t	          d|d|d|          	 | j                            t          |                    S # t          $ r t	          d|z            w xY w)z3Returns level associated with the given generator. r   -z
 <= gen < z expected, got z"a valid generator expected, got %s)rR   r+  r~   rU   r9   r  r"   r  )r[   r   rD  s      r`   r  zPoly._gen_to_level4  s    c3 	@[[Fw#&&&&&&&&&77!C<'J%o'-vvvvvss'< = = =@v||GCLL111 @ @ @%83>@ @ @@s   !&B B%r   c                     |                      |          }t          | j        d          r| j                            |          S t	          | d          )au  
        Returns degree of ``f`` in ``x_j``.

        The degree of 0 is negative infinity.

        Examples
        ========

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

        >>> Poly(x**2 + y*x + 1, x, y).degree()
        2
        >>> Poly(x**2 + y*x + y, x, y).degree(y)
        1
        >>> Poly(0, x).degree()
        -oo

        degree)r  r  rf   r  r4   )r[   r   r   s      r`   r  zPoly.degreeH  sK    ( OOC  15(## 	55<<??"'8444rc   c                 ~    t          | j        d          r| j                                        S t          | d          )z
        Returns a list of degrees of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + y*x + 1, x, y).degree_list()
        (2, 1)

        degree_list)r  rf   r  r4   r   s    r`   r  zPoly.degree_listc  s<     15-(( 	:5$$&&&'=999rc   c                 ~    t          | j        d          r| j                                        S t          | d          )a  
        Returns the total degree of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + y*x + 1, x, y).total_degree()
        2
        >>> Poly(x + y**5, x, y).total_degree()
        5

        total_degree)r  rf   r  r4   r   s    r`   r  zPoly.total_degreev  s<      15.)) 	;5%%''''>:::rc   c                    t          |t                    st          dt          |          z            || j        v r"| j                            |          }| j        }nt          | j                  }| j        |fz   }t          | j        d          r/| 	                    | j        
                    |          |          S t          | d          )a  
        Returns the homogeneous polynomial of ``f``.

        A homogeneous polynomial is a polynomial whose all monomials with
        non-zero coefficients have the same total degree. If you only
        want to check if a polynomial is homogeneous, then use
        :func:`Poly.is_homogeneous`. If you want not only to check if a
        polynomial is homogeneous but also compute its homogeneous order,
        then use :func:`Poly.homogeneous_order`.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> f = Poly(x**5 + 2*x**2*y**2 + 9*x*y**3)
        >>> f.homogenize(z)
        Poly(x**5 + 2*x**2*y**2*z + 9*x*y**3*z, x, y, z, domain='ZZ')

        z``Symbol`` expected, got %s
homogenizer   homogeneous_order)rR   r!   	TypeErrortyperU   r  r~   r  rf   r   r  r4   )r[   sr   rU   s       r`   r  zPoly.homogenize  s    , !V$$ 	E9DGGCDDD;;QA6DDAFA6QD=D15,'' 	955))!,,45888#A':;;;rc   c                 ~    t          | j        d          r| j                                        S t          | d          )a-  
        Returns the homogeneous order of ``f``.

        A homogeneous polynomial is a polynomial whose all monomials with
        non-zero coefficients have the same total degree. This degree is
        the homogeneous order of ``f``. If you only want to check if a
        polynomial is homogeneous, then use :func:`Poly.is_homogeneous`.

        Examples
        ========

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

        >>> f = Poly(x**5 + 2*x**3*y**2 + 9*x*y**4)
        >>> f.homogeneous_order()
        5

        r  )r  rf   r  r4   r   s    r`   r  zPoly.homogeneous_order  s?    ( 15-.. 	@5**,,,'+>???rc   c                     ||                      |          d         S t          | j        d          r| j                                        }nt	          | d          | j        j                            |          S )z
        Returns the leading coefficient of ``f``.

        Examples
        ========

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

        >>> Poly(4*x**3 + 2*x**2 + 3*x, x).LC()
        4

        Nr   LC)r   r  rf   r  r4   r   r   )r[   rh   r^   s      r`   r  zPoly.LC  sl     88E??1%%15$ 	1UXXZZFF'4000uy!!&)))rc   c                     t          | j        d          r| j                                        }nt          | d          | j        j                            |          S )z
        Returns the trailing coefficient of ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x**2 + 3*x, x).TC()
        0

        TC)r  rf   r  r4   r   r   r  s     r`   r  zPoly.TC  sQ     15$ 	1UXXZZFF'4000uy!!&)))rc   c                     t          | j        d          r|                     |          d         S t          | d          )z
        Returns the last non-zero coefficient of ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x**2 + 3*x, x).EC()
        3

        r   EC)r  rf   r   r4   r3  s     r`   r  zPoly.EC  s=     15(## 	188E??2&&'4000rc   c                 F     | j         t          || j                  j         S )aE  
        Returns the coefficient of ``monom`` in ``f`` if there, else None.

        Examples
        ========

        >>> from sympy import Poly, exp
        >>> from sympy.abc import x, y

        >>> p = Poly(24*x*y*exp(8) + 23*x, x, y)

        >>> p.coeff_monomial(x)
        23
        >>> p.coeff_monomial(y)
        0
        >>> p.coeff_monomial(x*y)
        24*exp(8)

        Note that ``Expr.coeff()`` behaves differently, collecting terms
        if possible; the Poly must be converted to an Expr to use that
        method, however:

        >>> p.as_expr().coeff(x)
        24*y*exp(8) + 23
        >>> p.as_expr().coeff(y)
        24*x*exp(8)
        >>> p.as_expr().coeff(x*y)
        24*exp(8)

        See Also
        ========
        nth: more efficient query using exponents of the monomial's generators

        )nthr/   rU   	exponents)r[   r   s     r`   coeff_monomialzPoly.coeff_monomial  s#    F quhuaf--788rc   c                 R   t          | j        d          rdt          |          t          | j                  k    rt	          d           | j        j        t          t          t          |                     }nt          | d          | j        j
                            |          S )a.  
        Returns the ``n``-th coefficient of ``f`` where ``N`` are the
        exponents of the generators in the term of interest.

        Examples
        ========

        >>> from sympy import Poly, sqrt
        >>> from sympy.abc import x, y

        >>> Poly(x**3 + 2*x**2 + 3*x, x).nth(2)
        2
        >>> Poly(x**3 + 2*x*y**2 + y**2, x, y).nth(1, 2)
        2
        >>> Poly(4*sqrt(x)*y)
        Poly(4*y*(sqrt(x)), y, sqrt(x), domain='ZZ')
        >>> _.nth(1, 1)
        4

        See Also
        ========
        coeff_monomial

        r  z,exponent of each generator must be specified)r  rf   r~   rU   r  r  rr   r   r+  r4   r   r   )r[   Nr^   s      r`   r  zPoly.nth+  s    2 15%   	21vvQV$$ !OPPPQUYSa[[ 1 12FF'5111uy!!&)))rc   r|   c                      t          d          )NzyEither convert to Expr with `as_expr` method to use Expr's coeff method or else use the `coeff_monomial` method of Polys.rl   )r[   r  r-  rights       r`   r   z
Poly.coeffM  s     "01 1 	1rc   c                 ^    t          |                     |          d         | j                  S )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   r/   r   rU   r3  s     r`   LMzPoly.LMY  s%    $ *AF333rc   c                 ^    t          |                     |          d         | j                  S )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  r3  s     r`   EMzPoly.EMm  s%     +QV444rc   c                 l    |                      |          d         \  }}t          || j                  |fS )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   r  r/   rU   r[   rh   r   r   s       r`   LTzPoly.LT}  s3    $ wwu~~a(uqv&&--rc   c                 l    |                      |          d         \  }}t          || j                  |fS )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       r`   ETzPoly.ET  s3     wwu~~b)uqv&&--rc   c                     t          | j        d          r| j                                        }nt          | d          | j        j                            |          S )z
        Returns maximum norm of ``f``.

        Examples
        ========

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

        >>> Poly(-x**2 + 2*x - 3, x).max_norm()
        3

        max_norm)r  rf   r  r4   r   r   r  s     r`   r  zPoly.max_norm  sS     15*%% 	7U^^%%FF':666uy!!&)))rc   c                     t          | j        d          r| j                                        }nt          | d          | j        j                            |          S )z
        Returns l1 norm of ``f``.

        Examples
        ========

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

        >>> Poly(-x**2 + 2*x - 3, x).l1_norm()
        6

        l1_norm)r  rf   r  r4   r   r   r  s     r`   r  zPoly.l1_norm  Q     15)$$ 	6U]]__FF'9555uy!!&)))rc   c                    | }|j         j        j        st          j        |fS |                                }|j        r|j         j                                        }t          |j         d          r|j         	                                \  }}nt          |d          |                    |          |                    |          }}|r|j        s||fS ||                                fS )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)rf   r   r  r   Oner   has_assoc_Ringget_ringr  r  r4   r   r   r  )r   r   r[   r   r   r^   s         r`   r  zPoly.clear_denoms  s    $ uy! 	5!8Ollnn 	'%)$$&&C15.)) 	;E..00ME66'>:::<<&&fq 	&c0 	&!8O!))++%%rc   c                 J   | }|                     |          \  }}}} ||          } ||          }|j        r|j        s||fS |                    d          \  }}|                    d          \  }}|                    |          }|                    |          }||fS )a  
        Clear denominators in a rational function ``f/g``.

        Examples
        ========

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

        >>> f = Poly(x**2/y + 1, x)
        >>> g = Poly(x**3 + y, x)

        >>> p, q = f.rat_clear_denoms(g)

        >>> p
        Poly(x**2 + y, x, domain='ZZ[y]')
        >>> q
        Poly(y*x**3 + y**2, x, domain='ZZ[y]')

        Tr   )r   r  r  r  rg  )r   r\   r[   r   r   abs          r`   rat_clear_denomszPoly.rat_clear_denoms  s    * !S!QCFFCFF 	!3 	a4K~~d~++1~~d~++1LLOOLLOO!trc   c                    | }|                     dd          r%|j        j        j        r|                                }t          |j        d          r|s.|                    |j                            d                    S |j        }|D ]W}t          |t                    r|\  }}n|d}}|                    t          |          |                    |                    }X|                    |          S t          |d          )a  
        Computes indefinite integral of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x + 1, x).integrate()
        Poly(1/3*x**3 + x**2 + x, x, domain='QQ')

        >>> Poly(x*y**2 + x, x, y).integrate((0, 1), (1, 0))
        Poly(1/2*x**2*y**2 + 1/2*x**2, x, y, domain='QQ')

        r  T	integrater|   r,  )getrf   r   r  r   r  r   r  rR   rA  r+  r  r4   )r   specsrx   r[   rf   specr   r,  s           r`   r  zPoly.integrate	  s    " 88FD!! 	aei&7 	

A15+&& 	8 3uuQU__q_11222%C B BdE** %!FC!1CmmCFFAOOC,@,@AA55::';777rc   c                    |                     dd          st          | g|R i |S t          | j        d          r|s.|                     | j                            d                    S | j        }|D ]W}t          |t                    r|\  }}n|d}}|                    t          |          | 	                    |                    }X|                     |          S t          | d          )aX  
        Computes partial derivative of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x + 1, x).diff()
        Poly(2*x + 2, x, domain='ZZ')

        >>> Poly(x*y**2 + x, x, y).diff((0, 0), (1, 1))
        Poly(2*x*y, x, y, domain='ZZ')

        evaluateTdiffr|   r  )r  r   r  rf   r   r  rR   rA  r+  r  r4   )r[   r  r
  rf   r  r   r,  s          r`   r  z	Poly.diffC	  s    " zz*d++ 	3a2%222622215&!! 	3 .uuQUZZ!Z__---%C = =dE** %!FC!1Chhs1vvqs';';<<55::'6222rc   c                    | }|t          |t                    r4|}|                                D ]\  }}|                    ||          }|S t          |t          t
          f          ri|}t          |          t          |j                  k    rt          d          t          |j        |          D ]\  }}|                    ||          }|S d|}}	n|
                    |          }	t          |j        d          st          |d          	 |j                            ||	          }
n# t          $ r |st          d|d|j        j                  t#          |g          \  }\  }|                                                    ||j                  }|                    |          }|                    ||          }|j                            ||	          }
Y nw xY w|                    |
|	          S )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')

        Nztoo many values providedr   r   zCannot evaluate at r  r   )rR   ro   r   r   rA  rr   r~   rU   r  r   r  r  rf   r4   r6   r5   r   r(   r   unify_with_symbolsr   r   r   )r   r  r  r  r[   rK  r   rL  valuesr   r^   a_domain
new_domains                r`   r   z	Poly.evalk	  s   > 9!T"" ")--// + +JCsE**AAAt}-- v;;QV,,$%?@@@"%aff"5"5 + +JCsE**AA!1""Aquf%% 	3'6222	*UZZ1%%FF 
	* 
	* 
	* 	*!k111aeii"PQQQ 0! 5 5#1\\^^>>xPP
LL,,&&q(33Aq))
	* uuVAu&&&s   D/ /B3G%$G%c                 ,    |                      |          S )az  
        Evaluate ``f`` at the give values.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z)

        >>> f(2)
        Poly(5*y + 2*z + 6, y, z, domain='ZZ')
        >>> f(2, 5)
        Poly(2*z + 31, z, domain='ZZ')
        >>> f(2, 5, 7)
        45

        )r   )r[   r  s     r`   __call__zPoly.__call__	  s    ( vvf~~rc   c                 @   |                      |          \  }}}}|r/|j        r(|                                |                                }}t          | j        d          r|                    |          \  }}nt          | d           ||           ||          fS )a  
        Half extended Euclidean algorithm of ``f`` and ``g``.

        Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``.

        Examples
        ========

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

        >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
        >>> g = x**3 + x**2 - 4*x - 4

        >>> Poly(f).half_gcdex(Poly(g))
        (Poly(-1/5*x + 3/5, x, domain='QQ'), Poly(x + 1, x, domain='QQ'))

        
half_gcdex)r   r  r   r  rf   r  r4   )	r[   r\   r  r   r   r   r   r  hs	            r`   r  zPoly.half_gcdex	  s    & !S!Q 	.CK 	.::<<qA15,'' 	9<<??DAqq'<888s1vvss1vv~rc   c                 V   |                      |          \  }}}}|r/|j        r(|                                |                                }}t          | j        d          r|                    |          \  }}}	nt          | d           ||           ||           ||	          fS )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   r  rf   r  r4   )
r[   r\   r  r   r   r   r   r  tr  s
             r`   r  z
Poly.gcdex	  s    * !S!Q 	.CK 	.::<<qA15'"" 	4ggajjGAq!!'7333s1vvss1vvss1vv%%rc   c                 $   |                      |          \  }}}}|r/|j        r(|                                |                                }}t          | j        d          r|                    |          }nt          | d           ||          S )a  
        Invert ``f`` modulo ``g`` when possible.

        Examples
        ========

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

        >>> Poly(x**2 - 1, x).invert(Poly(2*x - 1, x))
        Poly(-4/3, x, domain='QQ')

        >>> Poly(x**2 - 1, x).invert(Poly(x - 1, x))
        Traceback (most recent call last):
        ...
        NotInvertible: zero divisor

        invert)r   r  r   r  rf   r  r4   )r[   r\   r  r   r   r   r   r^   s           r`   r  zPoly.invert
  s    & !S!Q 	.CK 	.::<<qA15(## 	5XXa[[FF'8444s6{{rc   c                     t          | j        d          r(| j                            t          |                    }nt	          | d          |                     |          S )ad  
        Compute ``f**(-1)`` mod ``x**n``.

        Examples
        ========

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

        >>> Poly(1, x).revert(2)
        Poly(1, x, domain='ZZ')

        >>> Poly(1 + x, x).revert(1)
        Poly(1, x, domain='ZZ')

        >>> Poly(x**2 - 2, x).revert(2)
        Traceback (most recent call last):
        ...
        NotReversible: only units are reversible in a ring

        >>> Poly(1/x, x).revert(1)
        Traceback (most recent call last):
        ...
        PolynomialError: 1/x contains an element of the generators set

        revert)r  rf   r  r+  r4   r   r{  s      r`   r  zPoly.revert+
  sS    6 15(## 	5U\\#a&&))FF'8444uuV}}rc   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d          t          t          ||                    S )ad  
        Computes the subresultant PRS of ``f`` and ``g``.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).subresultants(Poly(x**2 - 1, x))
        [Poly(x**2 + 1, x, domain='ZZ'),
         Poly(x**2 - 1, x, domain='ZZ'),
         Poly(-2, x, domain='ZZ')]

        subresultants)r   r  rf   r  r4   rr   r   rr  s          r`   r  zPoly.subresultantsM
  sj      xx{{3115/** 	<__Q''FF'?;;;CV$$%%%rc   c                 X   |                      |          \  }}}}t          | j        d          r3|r|                    ||          \  }}n&|                    |          }nt	          | d          |r* ||d          t          t          ||                    fS  ||d          S )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   r  )r   r  rf   r  r4   rr   r   )	r[   r\   r  r   r   r   r   r^   r  s	            r`   r  zPoly.resultantf
  s    . xx{{3115+&& 	8 (KKjKAA	Q';777 	>Cq)))4C+<+<==s6!$$$$rc   c                     t          | j        d          r| j                                        }nt          | d          |                     |d          S )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   r  )r  rf   r  r4   r   r  s     r`   r  zPoly.discriminant
  sS     15.)) 	;U''))FF'>:::uuVAu&&&rc   c                 &    ddl m}  || |          S )a  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.dispersionr  )r[   r\   r  s      r`   r  zPoly.dispersionset
  s)    P 	988888}Q"""rc   c                 &    ddl m}  || |          S )a  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)r  r  )r[   r\   r  s      r`   r  zPoly.dispersion
  s)    P 	655555z!Qrc   c                     |                      |          \  }}}}t          | j        d          r|                    |          \  }}}nt	          | d           ||           ||           ||          fS )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   r  rf   r  r4   )	r[   r\   r   r   r   r   r  cffcfgs	            r`   r  zPoly.cofactors6  s{    ( xx{{3115+&& 	8++a..KAsCC';777s1vvss3xxS))rc   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )a  
        Returns the polynomial GCD of ``f`` and ``g``.

        Examples
        ========

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

        >>> Poly(x**2 - 1, x).gcd(Poly(x**2 - 3*x + 2, x))
        Poly(x - 1, x, domain='ZZ')

        gcd)r   r  rf   r  r4   rr  s          r`   r  zPoly.gcdS  ^     xx{{3115%   	2UU1XXFF'5111s6{{rc   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )a  
        Returns polynomial LCM of ``f`` and ``g``.

        Examples
        ========

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

        >>> Poly(x**2 - 1, x).lcm(Poly(x**2 - 3*x + 2, x))
        Poly(x**3 - 2*x**2 - x + 2, x, domain='ZZ')

        lcm)r   r  rf   r  r4   rr  s          r`   r  zPoly.lcmj  r  rc   c                     | j         j                            |          }t          | j         d          r| j                             |          }nt          | d          |                     |          S )a  
        Reduce ``f`` modulo a constant ``p``.

        Examples
        ========

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

        >>> Poly(2*x**3 + 3*x**2 + 5*x + 7, x).trunc(3)
        Poly(-x**3 - x + 1, x, domain='ZZ')

        trunc)rf   r   r   r  r  r4   r   )r[   pr^   s      r`   r  z
Poly.trunc  sb     EIa  15'"" 	4U[[^^FF'7333uuV}}rc   c                     | }|r%|j         j        j        r|                                }t	          |j         d          r|j                                         }nt          |d          |                    |          S )az  
        Divides all coefficients by ``LC(f)``.

        Examples
        ========

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

        >>> Poly(3*x**2 + 6*x + 9, x, domain=ZZ).monic()
        Poly(x**2 + 2*x + 3, x, domain='QQ')

        >>> Poly(3*x**2 + 4*x + 2, x, domain=ZZ).monic()
        Poly(x**2 + 4/3*x + 2/3, x, domain='QQ')

        monic)rf   r   r  r   r  r  r4   r   r   r  r[   r^   s       r`   r  z
Poly.monic  sq    "  	AEI% 	

A15'"" 	4U[[]]FF'7333uuV}}rc   c                     t          | j        d          r| j                                        }nt          | d          | j        j                            |          S )z
        Returns the GCD of polynomial coefficients.

        Examples
        ========

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

        >>> Poly(6*x**2 + 8*x + 12, x).content()
        2

        content)r  rf   r  r4   r   r   r  s     r`   r  zPoly.content  r  rc   c                     t          | j        d          r| j                                        \  }}nt          | d          | j        j                            |          |                     |          fS )a  
        Returns the content and a primitive form of ``f``.

        Examples
        ========

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

        >>> Poly(2*x**2 + 8*x + 12, x).primitive()
        (2, Poly(x**2 + 4*x + 6, x, domain='ZZ'))

        	primitive)r  rf   r	  r4   r   r   r   )r[   contr^   s      r`   r	  zPoly.primitive  sf     15+&& 	85??,,LD&&';777uy!!$''v66rc   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )a  
        Computes the functional composition of ``f`` and ``g``.

        Examples
        ========

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

        >>> Poly(x**2 + x, x).compose(Poly(x - 1, x))
        Poly(x**2 - x, x, domain='ZZ')

        compose)r   r  rf   r  r4   rr  s          r`   r  zPoly.compose  s^     xx{{3115)$$ 	6YYq\\FF'9555s6{{rc   c                     t          | j        d          r| j                                        }nt          | d          t	          t          | j        |                    S )a=  
        Computes a functional decomposition of ``f``.

        Examples
        ========

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

        >>> Poly(x**4 + 2*x**3 - x - 1, x, domain='ZZ').decompose()
        [Poly(x**2 - x - 1, x, domain='ZZ'), Poly(x**2 + x, x, domain='ZZ')]

        	decompose)r  rf   r  r4   rr   r   r   r  s     r`   r  zPoly.decompose  sU     15+&& 	8U__&&FF';777Cv&&'''rc   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )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')

        shift)r  rf   r  r4   r   )r[   r  r^   s      r`   r  z
Poly.shift  sK     15'"" 	4U[[^^FF'7333uuV}}rc   c                 R   |                     |          \  }}|                      |          \  }}|                     |          \  }}t          |j        d          r&|j                            |j        |j                  }nt	          |d          |                    |          S )a3  
        Efficiently evaluate the functional transformation ``q**n * f(p/q)``.


        Examples
        ========

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

        >>> Poly(x**2 - 2*x + 1, x).transform(Poly(x + 1, x), Poly(x - 1, x))
        Poly(4, x, domain='ZZ')

        	transform)r   r  rf   r  r4   r   )r[   r  r~  Pr  r   r^   s          r`   r  zPoly.transform   s     wwqzz1wwqzz1wwqzz115+&& 	8U__QUAE22FF';777uuV}}rc   c                    | }|r%|j         j        j        r|                                }t	          |j         d          r|j                                         }nt          |d          t          t          |j	        |                    S )a  
        Computes the Sturm sequence of ``f``.

        Examples
        ========

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

        >>> Poly(x**3 - 2*x**2 + x - 3, x).sturm()
        [Poly(x**3 - 2*x**2 + x - 3, x, domain='QQ'),
         Poly(3*x**2 - 4*x + 1, x, domain='QQ'),
         Poly(2/9*x + 25/9, x, domain='QQ'),
         Poly(-2079/4, x, domain='QQ')]

        sturm)
rf   r   r  r   r  r  r4   rr   r   r   r  s       r`   r  z
Poly.sturm:  s{    "  	AEI% 	

A15'"" 	4U[[]]FF'7333Cv&&'''rc   c                      t           j        d          r j                                        }nt           d           fd|D             S )aI  
        Computes greatest factorial factorization of ``f``.

        Examples
        ========

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

        >>> f = x**5 + 2*x**4 - x**3 - 2*x**2

        >>> Poly(f).gff_list()
        [(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)]

        gff_listc                 D    g | ]\  }}                     |          |fS r   r   r   r\   r\  r[   s      r`   r   z!Poly.gff_list.<locals>.<listcomp>l  s+    111$!Qq1111rc   )r  rf   r  r4   r  s   ` r`   r  zPoly.gff_listW  sV      15*%% 	7U^^%%FF':6661111&1111rc   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )a  
        Computes the product, ``Norm(f)``, of the conjugates of
        a polynomial ``f`` defined over a number field ``K``.

        Examples
        ========

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

        >>> a, b = sqrt(2), sqrt(3)

        A polynomial over a quadratic extension.
        Two conjugates x - a and x + a.

        >>> f = Poly(x - a, x, extension=a)
        >>> f.norm()
        Poly(x**2 - 2, x, domain='QQ')

        A polynomial over a quartic extension.
        Four conjugates x - a, x - a, x + a and x + a.

        >>> f = Poly(x - a, x, extension=(a, b))
        >>> f.norm()
        Poly(x**4 - 4*x**2 + 4, x, domain='QQ')

        norm)r  rf   r  r4   r   )r[   r  s     r`   r  z	Poly.normn  sH    8 15&!! 	3

AA'6222uuQxxrc   c                     t          | j        d          r| j                                        \  }}}nt          | d          ||                     |          |                     |          fS )af  
        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)r  rf   r  r4   r   )r[   r  r\   r  s       r`   r  zPoly.sqf_norm  sb    0 15*%% 	7enn&&GAq!!':666!%%((AEE!HH$$rc   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Computes square-free part of ``f``.

        Examples
        ========

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

        >>> Poly(x**3 - 3*x - 2, x).sqf_part()
        Poly(x**2 - x - 2, x, domain='ZZ')

        sqf_part)r  rf   r   r4   r   r  s     r`   r   zPoly.sqf_part  r!  rc   c                      t           j        d          r j                            |          \  }}nt           d           j        j                            |           fd|D             fS )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_listc                 D    g | ]\  }}                     |          |fS r   r  r  s      r`   r   z!Poly.sqf_list.<locals>.<listcomp>  +    *M*M*MTQAEE!HHa=*M*M*Mrc   )r  rf   r"  r4   r   r   )r[   allr   factorss   `   r`   r"  zPoly.sqf_list  st    , 15*%% 	7U^^C00NE77':666uy!!%((*M*M*M*MW*M*M*MMMrc   c                      t           j        d          r j                            |          }nt           d           fd|D             S )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_includec                 D    g | ]\  }}                     |          |fS r   r  r  s      r`   r   z)Poly.sqf_list_include.<locals>.<listcomp>  +    222$!Qq1222rc   )r  rf   r(  r4   )r[   r%  r&  s   `  r`   r(  zPoly.sqf_list_include  s\    4 15,-- 	?e,,S11GG'+=>>>2222'2222rc   c                 &    t           j        d          r?	  j                                        \  }}n1# t          $ r t          j         dfgfcY S w xY wt           d           j        j                            |           fd|D             fS )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|   c                 D    g | ]\  }}                     |          |fS r   r  r  s      r`   r   z$Poly.factor_list.<locals>.<listcomp>  r$  rc   )	r  rf   r,  r5   r   r  r4   r   r   )r[   r   r&  s   `  r`   r,  zPoly.factor_list  s    " 15-(( 	:'!"!2!2!4!4ww ' ' 'u1vh&&&' (=999uy!!%((*M*M*M*MW*M*M*MMMs   5 AAc                      t           j        d          r0	  j                                        }n%# t          $ r  dfgcY S w xY wt	           d           fd|D             S )a  
        Returns a list of irreducible factors of ``f``.

        Examples
        ========

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

        >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y

        >>> Poly(f).factor_list_include()
        [(Poly(2*x + 2*y, x, y, domain='ZZ'), 1),
         (Poly(x**2 + 1, x, y, domain='ZZ'), 2)]

        factor_list_includer|   c                 D    g | ]\  }}                     |          |fS r   r  r  s      r`   r   z,Poly.factor_list_include.<locals>.<listcomp>7  r*  rc   )r  rf   r/  r5   r4   )r[   r&  s   ` r`   r/  zPoly.factor_list_include  s    " 15/00 	B %3355      Ax  (+@AAA2222'2222s   2 AAc                    |)t          j        |          }|dk    rt          d          |t          j        |          }|t          j        |          }t          | j        d          r!| j                            ||||||          }nt          | d          |rdd }|st          t          ||                    S d }	|\  }
}t          t          ||
                    t          t          |	|                    fS d }|st          t          ||                    S d	 }	|\  }
}t          t          ||
                    t          t          |	|                    fS )
a  
        Compute isolating intervals for roots of ``f``.

        For real roots the Vincent-Akritas-Strzebonski (VAS) continued fractions method is used.

        References
        ==========
        .. [#] Alkiviadis G. Akritas and Adam W. Strzebonski: A Comparative Study of Two Real Root
            Isolation Methods . Nonlinear Analysis: Modelling and Control, Vol. 10, No. 4, 297-304, 2005.
        .. [#] Alkiviadis G. Akritas, Adam W. Strzebonski and Panagiotis S. Vigklas: Improving the
            Performance of the Continued Fractions Method Using new Bounds of Positive Roots. Nonlinear
            Analysis: Modelling and Control, Vol. 13, No. 3, 265-279, 2008.

        Examples
        ========

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

        >>> Poly(x**2 - 3, x).intervals()
        [((-2, -1), 1), ((1, 2), 1)]
        >>> Poly(x**2 - 3, x).intervals(eps=1e-2)
        [((-26/15, -19/11), 1), ((19/11, 26/15), 1)]

        Nr   !'eps' must be a positive rational	intervalsr%  epsinfsupfastsqfc                 \    | \  }}t          j        |          t          j        |          fS r   r*   r   )intervalr  r  s      r`   _realzPoly.intervals.<locals>._reale  s&    1AA77rc   c                     | \  \  }}\  }}t          j        |          t          t          j        |          z  z   t          j        |          t          t          j        |          z  z   fS r   r*   r   r   )	rectangleuvr  r  s        r`   _complexz Poly.intervals.<locals>._complexl  sW    !*AAA2;q>>)99A2;q>>)99; ;rc   c                 f    | \  \  }}}t          j        |          t          j        |          f|fS r   r;  )r<  r  r  r\  s       r`   r=  zPoly.intervals.<locals>._realu  s/    $	AQQ8!<<rc   c                     | \  \  \  }}\  }}}t          j        |          t          t          j        |          z  z   t          j        |          t          t          j        |          z  z   f|fS r   r?  )r@  rA  rB  r  r  r\  s         r`   rC  z Poly.intervals.<locals>._complex|  sg    &/# !Q!Q!Q!BKNN*::Q!BKNN*::<=>@ @rc   )	r*   r   r  r  rf   r3  r4   rr   r   )r[   r%  r5  r6  r7  r8  r9  r^   r=  rC  	real_partcomplex_parts               r`   r3  zPoly.intervals9  s   4 ?*S//Caxx !DEEE?*S//C?*S//C15+&& 	8U__Scs3 % H HFF (;777 	R8 8 8  0Cv..///; ; ;
 '-#I|E9--..S<5P5P0Q0QQQ= = =  0Cv..///@ @ @
 '-#I|E9--..S<5P5P0Q0QQQrc   c                    |r| j         st          d          t          j        |          t          j        |          }}|)t          j        |          }|dk    rt	          d          |t          |          }n|d}t          | j        d          r#| j                            |||||          \  }}nt          | d          t          j
        |          t          j
        |          fS )a  
        Refine an isolating interval of a root to the given precision.

        Examples
        ========

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

        >>> Poly(x**2 - 3, x).refine_root(1, 2, eps=1e-2)
        (19/11, 26/15)

        z&only square-free polynomials supportedNr   r2  r|   refine_root)r5  stepsr8  )is_sqfr9   r*   r   r  r+  r  rf   rI  r4   r   )	r[   r  r  r5  rJ  r8  	check_sqfr   Ts	            r`   rI  zPoly.refine_root  s      	LQX 	L!"JKKKz!}}bjmm1?*S//Caxx !DEEEJJEE[E15-(( 	:5$$Qs%d$KKDAqq'=999{1~~r{1~~--rc   c                 n   d\  }}|yt          |          }|t          j        u rd}nY|                                \  }}|st	          j        |          }n+t          t          t          j        ||f                    d}}|yt          |          }|t          j        u rd}nY|                                \  }}|st	          j        |          }n+t          t          t          j        ||f                    d}}|rD|rBt          | j
        d          r| j
                            ||          }nvt          | d          |r||t          j        f}|r||t          j        f}t          | j
        d          r| j
                            ||          }nt          | d          t          |          S )a<  
        Return the number of roots of ``f`` in ``[inf, sup]`` interval.

        Examples
        ========

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

        >>> Poly(x**4 - 4, x).count_roots(-3, 3)
        2
        >>> Poly(x**4 - 4, x).count_roots(0, 1 + 3*I)
        1

        TTNFcount_real_rootsr6  r7  count_complex_roots)r"   r   NegativeInfinityas_real_imagr*   r   rr   r   Infinityr  rf   rP  r4   r   rR  r   )r[   r6  r7  inf_realsup_realreimcounts           r`   count_rootszPoly.count_roots  s     ((?#,,Ca((())++B K*S//CC$(RZ"b)B)B$C$CUC?#,,Caj  ))++B K*S//CC$(RZ"b)B)B$C$CUC 	F 	Fqu011 C..3C.@@+A/ABBB %COBGn %COBGnqu344 F11cs1CC+A/DEEEu~~rc   c                 P    t           j        j                            | ||          S )a  
        Get an indexed root of a polynomial.

        Examples
        ========

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

        >>> f = Poly(2*x**3 - 7*x**2 + 4*x + 4)

        >>> f.root(0)
        -1/2
        >>> f.root(1)
        2
        >>> f.root(2)
        2
        >>> f.root(3)
        Traceback (most recent call last):
        ...
        IndexError: root index out of [-3, 2] range, got 3

        >>> Poly(x**5 + x + 1).root(0)
        CRootOf(x**3 - x**2 + 1, 0)

        radicals)sympypolysrootoftoolsrootof)r[   r  r^  s      r`   rootz	Poly.root  s$    6 {&--a-JJJrc   c                     t           j        j        j                            | |          }|r|S t          |d          S )aL  
        Return a list of real roots with multiplicities.

        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`  ra  CRootOf
real_rootsrG   )r[   rf  r^  realss       r`   rh  zPoly.real_roots  sD      '/::1x:PP 	0L////rc   c                     t           j        j        j                            | |          }|r|S t          |d          S )a  
        Return a list of real and complex roots with multiplicities.

        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]  Fre  )r_  r`  ra  rg  	all_rootsrG   )r[   rf  r^  rootss       r`   rk  zPoly.all_roots  sD    $ '/99!h9OO 	0L////rc      2   c                   	 | j         rt          d| z            |                                 dk    rg S | j        j        t
          u rd |                                 D             }n| j        j        t          u rHd |                                 D             }t          | fd|                                 D             }nXfd|                                 D             }	 d |D             }n*# t          $ r t          d| j        j        z            w xY wt          j        j        }t          j        _        dd	lm	 	 t          j        |||d
|                                 dz            }t#          t%          t&          t)          |	fd                              }n# t*          $ r 	 t          j        |||d
|                                 dz            }t#          t%          t&          t)          |	fd                              }n## t*          $ r t+          dd|          w xY wY nw xY w|t          j        _        n# |t          j        _        w xY w|S )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   c                 ,    g | ]}t          |          S r   r+  r   r   s     r`   r   zPoly.nroots.<locals>.<listcomp>Z  s    ===Uc%jj===rc   c                     g | ]	}|j         
S r   )r~  rr  s     r`   r   zPoly.nroots.<locals>.<listcomp>\  s    :::%eg:::rc   c                 4    g | ]}t          |z            S r   rq  )r   r   facs     r`   r   zPoly.nroots.<locals>.<listcomp>^  s#    AAAc%)nnAAArc   c                 `    g | ]*}|                                                                +S ))r-  )r   rT  )r   r   r-  s     r`   r   zPoly.nroots.<locals>.<listcomp>`  sC     1 1 1 kkAk&&3355 1 1 1rc   c                 *    g | ]}t          j        | S r   )mpmathmpcrr  s     r`   r   zPoly.nroots.<locals>.<listcomp>c  s    AAA&*e,AAArc   z!Numerical domain expected, got %s)signF
   )maxstepscleanuperror	extraprecc                 l    | j         rdnd| j        t          | j                    | j                   fS Nr|   r   imagrealrm  r  rz  s    r`   <lambda>zPoly.nroots.<locals>.<lambda>u  s5    !&-?QQaQVVZVZ[\[aVbVb,c rc   keyrm  c                 l    | j         rdnd| j        t          | j                    | j                   fS r  r  r  s    r`   r  zPoly.nroots.<locals>.<lambda>|  s7    af1C!QVSQRQW[[Z^Z^_`_eZfZf0g rc   z$convergence to root failed; try n < z or maxsteps > )is_multivariater:   r  rf   r   r+   r:  r*   r   r  r5   rx  mpdps$sympy.functions.elementary.complexesrz  	polyrootsrr   r   r"   sortedrM   )
r[   r-  r|  r}  r   denomsr  rl  ru  rz  s
    `      @@r`   nrootszPoly.nroots6  s   2  	<-6:< < < 88::??I
 59??==allnn===FFUY"__::1<<>>:::F-CAAAA!,,..AAAFF1 1 1 1!"1 1 1F#AA&AAA # # #!"E	#" # # ## im	======	  $Vh#5AHHJJrMK K KE
 Wu"c"c"c"cdddf f g gEE 
	" 
	" 
	"	"((#5AHHJJrMK K KS5&g&g&g&ghhhj j k k  " " "#mAAxx!" " "" 
	"  FIMMCFIMsP   %C2 2'DA"F( 'I (
H<3A"HH< H66H<9I ;H<<I I$c                     | j         rt          d| z            i }|                                 d         D ],\  }}|j        r |                                \  }}||| |z  <   -|S )a  
        Compute roots of ``f`` by factorization in the ground domain.

        Examples
        ========

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

        >>> Poly(x**6 - 4*x**4 + 4*x**3 - x**2).ground_roots()
        {0: 2, 1: 2}

        z!Cannot compute ground roots of %sr|   )r  r:   r,  	is_linearr:  )r[   rl  factorr\  r  r  s         r`   ground_rootszPoly.ground_roots  s      	9-3a79 9 9 + 	  	 IFA  ((**1qbdrc   c                 l   | j         rt          d          t          |          }|j        r|dk    rt	          |          }nt          d|z            | j        }t          d          }|                     | j	        
                    ||z  |z
  ||                    }|                    ||          S )af  
        Construct a polynomial with n-th powers of roots of ``f``.

        Examples
        ========

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

        >>> f = Poly(x**4 - x**2 + 1)

        >>> f.nth_power_roots_poly(2)
        Poly(x**4 - 2*x**3 + 3*x**2 - 2*x + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(3)
        Poly(x**4 + 2*x**2 + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(4)
        Poly(x**4 + 2*x**3 + 3*x**2 + 2*x + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(12)
        Poly(x**4 - 4*x**3 + 6*x**2 - 4*x + 1, x, domain='ZZ')

        zmust be a univariate polynomialr|   z&'n' must an integer and n >= 1, got %sr  )r  r:   r"   
is_Integerr+  r  r   r    r  r   rT   r   )r[   r-  r  r  r  r  s         r`   nth_power_roots_polyzPoly.nth_power_roots_poly  s    ,  	3-13 3 3 AJJ< 	KAFFAAAEIJJJE#JJKK--adQh1==>>yyArc   c                    | j         rt          d          | j                                        }| j                                                            |          }|dz  }t          d|z  di           \  }}}}t          |          }|dz  |dz  z   fd}	 |	|           |	|          }}
|
j	        |j	        z
  dz  |
j
        |j
        z
  dz  z   |k     S )a  
        Decide whether two roots of this polynomial are equal.

        Examples
        ========

        >>> from sympy import Poly, cyclotomic_poly, exp, I, pi
        >>> f = Poly(cyclotomic_poly(5))
        >>> r0 = exp(2*I*pi/5)
        >>> indices = [i for i, r in enumerate(f.all_roots()) if f.same_root(r, r0)]
        >>> print(indices)
        [3]

        Raises
        ======

        DomainError
            If the domain of the polynomial is not :ref:`ZZ`, :ref:`QQ`,
            :ref:`RR`, or :ref:`CC`.
        MultivariatePolynomialError
            If the polynomial is not univariate.
        PolynomialError
            If the polynomial is of degree < 2.

        zMust be a univariate polynomial	   r|      c                 @    t          t          |                     S )Nr  )r   r   )r  r,  s    r`   r  z Poly.same_root.<locals>.<lambda>  s    ~&?Q&G&G&GHH rc   )r  r:   rf   mignotte_sep_bound_squaredr   	get_fieldr   r   r   r  r  )r[   r  r  dom_delta_sqdelta_sqeps_sqr  r   r-  evABr,  s               @r`   	same_rootzPoly.same_root  s    4  	3-13 3 3 u77998%%''00>> A1V8Q++
1aAJJ!VAHHHH r!uubbee1!#qv&::VCCrc   c                    |                      |          \  }}}}t          |d          r|                    ||          }nt          | d          |sf|j        r|                                }|\  }}	}
}|                    |          }|                    |	          }	||	z   ||
           ||          fS t          t          ||                    S )a  
        Cancel common factors in a rational function ``f/g``.

        Examples
        ========

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

        >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x))
        (1, Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ'))

        >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x), include=True)
        (Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ'))

        cancel)include)	r   r  r  r4   r  r  r   rA  r   )r[   r\   r  r   r   r   r   r^   cpcqr  r~  s               r`   r  zPoly.cancel  s    " !S!Q1h 	5XXaX11FF'8444 	+! %llnn!LBAqb!!Bb!!Bb5##a&&##a&&((S&))***rc   c                 p   | j         r| j        t          t          fvrt	          d          | j        r| j        t          k    r| t          j        fS |                                 }|                                \  }}|	                    t          |j                  |                                          |fS )aQ  
        Turn any univariate polynomial over :ref:`QQ` or :ref:`ZZ` into a monic
        polynomial over :ref:`ZZ`, by scaling the roots as necessary.

        Explanation
        ===========

        This operation can be performed whether or not *f* is irreducible; when
        it is, this can be understood as determining an algebraic integer
        generating the same field as a root of *f*.

        Examples
        ========

        >>> from sympy import Poly, S
        >>> from sympy.abc import x
        >>> f = Poly(x**2/2 + S(1)/4 * x + S(1)/8, x, domain='QQ')
        >>> f.make_monic_over_integers_by_scaling_roots()
        (Poly(x**2 + 2*x + 4, x, domain='ZZ'), 4)

        Returns
        =======

        Pair ``(g, c)``
            g is the polynomial

            c is the integer by which the roots had to be scaled

        z,Polynomial must be univariate over ZZ or QQ.)r  r   r+   r*   r  is_monicr   r  r  r  rS   r   r  )r[   fmr   r   s       r`   )make_monic_over_integers_by_scaling_rootsz.Poly.make_monic_over_integers_by_scaling_roots!  s    <  	M!(2r(":":KLLL: 	>!(b..bf9B??$$DAq<<RVa0088::A==rc      c                 >   ddl m}m}m}m} | j        r| j        r| j        t          t          fvrt          d          ||||d}t          |                                          }	|                                 }
|
|	k    rt          d|	 d          |
dk     rt          d          |
dk    rdd	lm} |j        d
}}nD|
dk    rddlm} |j        d}}n.|                                 \  }} ||
         |||          \  }}|r|n|                                }||fS )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.galoisgroupsr  r  r  r  r  is_irreducibler   r+   r*   r  maxkeysr  sympy.combinatorics.galoisr  S1r  S2r  get_perm_group)r[   by_namer  r  r  r  r  r  ggmax_supportedr-  r  namealtr  r\   r   r   s                     r`   galois_groupzPoly.galois_groupH  s   (	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	

  	]#	]xBx''[\\\%,7,	
 
 BGGIIHHJJ}\m\\\]]]UUGHHH!VVHHHHHH-0$#DD!VVHHHHHH-0%#DD>>@@DAq1a9	JJJID#6DD!4!4!6!6#vrc   c                     | j         j        S )a  
        Returns ``True`` if ``f`` is a zero polynomial.

        Examples
        ========

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

        >>> Poly(0, x).is_zero
        True
        >>> Poly(1, x).is_zero
        False

        )rf   is_zeror   s    r`   r  zPoly.is_zero~  s    " u}rc   c                     | j         j        S )a  
        Returns ``True`` if ``f`` is a unit polynomial.

        Examples
        ========

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

        >>> Poly(0, x).is_one
        False
        >>> Poly(1, x).is_one
        True

        )rf   is_oner   s    r`   r  zPoly.is_one      " u|rc   c                     | j         j        S )a   
        Returns ``True`` if ``f`` is a square-free polynomial.

        Examples
        ========

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

        >>> Poly(x**2 - 2*x + 1, x).is_sqf
        False
        >>> Poly(x**2 - 1, x).is_sqf
        True

        )rf   rK  r   s    r`   rK  zPoly.is_sqf  r  rc   c                     | j         j        S )a   
        Returns ``True`` if the leading coefficient of ``f`` is one.

        Examples
        ========

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

        >>> Poly(x + 2, x).is_monic
        True
        >>> Poly(2*x + 2, x).is_monic
        False

        )rf   r  r   s    r`   r  zPoly.is_monic  s    " u~rc   c                     | j         j        S )a;  
        Returns ``True`` if GCD of the coefficients of ``f`` is one.

        Examples
        ========

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

        >>> Poly(2*x**2 + 6*x + 12, x).is_primitive
        False
        >>> Poly(x**2 + 3*x + 6, x).is_primitive
        True

        )rf   is_primitiver   s    r`   r  zPoly.is_primitive      " u!!rc   c                     | j         j        S )aJ  
        Returns ``True`` if ``f`` is an element of the ground domain.

        Examples
        ========

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

        >>> Poly(x, x).is_ground
        False
        >>> Poly(2, x).is_ground
        True
        >>> Poly(y, x).is_ground
        True

        )rf   	is_groundr   s    r`   r  zPoly.is_ground  s    & urc   c                     | j         j        S )a,  
        Returns ``True`` if ``f`` is linear in all its variables.

        Examples
        ========

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

        >>> Poly(x + y + 2, x, y).is_linear
        True
        >>> Poly(x*y + 2, x, y).is_linear
        False

        )rf   r  r   s    r`   r  zPoly.is_linear  s    " urc   c                     | j         j        S )a6  
        Returns ``True`` if ``f`` is quadratic in all its variables.

        Examples
        ========

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

        >>> Poly(x*y + 2, x, y).is_quadratic
        True
        >>> Poly(x*y**2 + 2, x, y).is_quadratic
        False

        )rf   is_quadraticr   s    r`   r  zPoly.is_quadratic  r  rc   c                     | j         j        S )a%  
        Returns ``True`` if ``f`` is zero or has only one term.

        Examples
        ========

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

        >>> Poly(3*x**2, x).is_monomial
        True
        >>> Poly(3*x**2 + 1, x).is_monomial
        False

        )rf   is_monomialr   s    r`   r  zPoly.is_monomial  s    " u  rc   c                     | j         j        S )aZ  
        Returns ``True`` if ``f`` is a homogeneous polynomial.

        A homogeneous polynomial is a polynomial whose all monomials with
        non-zero coefficients have the same total degree. If you want not
        only to check if a polynomial is homogeneous but also compute its
        homogeneous order, then use :func:`Poly.homogeneous_order`.

        Examples
        ========

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

        >>> Poly(x**2 + x*y, x, y).is_homogeneous
        True
        >>> Poly(x**3 + x*y, x, y).is_homogeneous
        False

        )rf   is_homogeneousr   s    r`   r  zPoly.is_homogeneous+  s    , u##rc   c                     | j         j        S )aG  
        Returns ``True`` if ``f`` has no factors over its domain.

        Examples
        ========

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

        >>> Poly(x**2 + x + 1, x, modulus=2).is_irreducible
        True
        >>> Poly(x**2 + 1, x, modulus=2).is_irreducible
        False

        )rf   r  r   s    r`   r  zPoly.is_irreducibleC  s    " u##rc   c                 2    t          | j                  dk    S )a  
        Returns ``True`` if ``f`` is a univariate polynomial.

        Examples
        ========

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

        >>> Poly(x**2 + x + 1, x).is_univariate
        True
        >>> Poly(x*y**2 + x*y + 1, x, y).is_univariate
        False
        >>> Poly(x*y**2 + x*y + 1, x).is_univariate
        True
        >>> Poly(x**2 + x + 1, x, y).is_univariate
        False

        r|   r~   rU   r   s    r`   r  zPoly.is_univariateV      * 16{{arc   c                 2    t          | j                  dk    S )a  
        Returns ``True`` if ``f`` is a multivariate polynomial.

        Examples
        ========

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

        >>> Poly(x**2 + x + 1, x).is_multivariate
        False
        >>> Poly(x*y**2 + x*y + 1, x, y).is_multivariate
        True
        >>> Poly(x*y**2 + x*y + 1, x).is_multivariate
        False
        >>> Poly(x**2 + x + 1, x, y).is_multivariate
        True

        r|   r  r   s    r`   r  zPoly.is_multivariatem  r  rc   c                     | j         j        S )a  
        Returns ``True`` if ``f`` is a cyclotomic polnomial.

        Examples
        ========

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

        >>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1

        >>> Poly(f).is_cyclotomic
        False

        >>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1

        >>> Poly(g).is_cyclotomic
        True

        )rf   is_cyclotomicr   s    r`   r  zPoly.is_cyclotomic  s    , u""rc   c                 *    |                                  S r   )rm  r   s    r`   __abs__zPoly.__abs__      uuwwrc   c                 *    |                                  S r   )ro  r   s    r`   __neg__zPoly.__neg__  r  rc   c                 ,    |                      |          S r   r  r[   r\   s     r`   __add__zPoly.__add__      uuQxxrc   c                 ,    |                     |           S r   r  r  s     r`   __radd__zPoly.__radd__  r  rc   c                 ,    |                      |          S r   ru  r  s     r`   __sub__zPoly.__sub__  r  rc   c                 ,    |                     |           S r   r  r  s     r`   __rsub__zPoly.__rsub__  r  rc   c                 ,    |                      |          S r   r   r  s     r`   __mul__zPoly.__mul__  r  rc   c                 ,    |                     |           S r   r   r  s     r`   __rmul__zPoly.__rmul__  r  rc   r-  c                 T    |j         r|dk    r|                     |          S t          S )Nr   )r  rz  rW   )r[   r-  s     r`   __pow__zPoly.__pow__  s)    < 	"AFF5588O!!rc   c                 ,    |                      |          S r   r  r  s     r`   
__divmod__zPoly.__divmod__  r  rc   c                 ,    |                     |           S r   r  r  s     r`   __rdivmod__zPoly.__rdivmod__  r  rc   c                 ,    |                      |          S r   r  r  s     r`   __mod__zPoly.__mod__  r  rc   c                 ,    |                     |           S r   r  r  s     r`   __rmod__zPoly.__rmod__  r  rc   c                 ,    |                      |          S r   r  r  s     r`   __floordiv__zPoly.__floordiv__  r  rc   c                 ,    |                     |           S r   r  r  s     r`   __rfloordiv__zPoly.__rfloordiv__  r  rc   r\   c                 T    |                                  |                                 z  S r   rY   r  s     r`   __truediv__zPoly.__truediv__      yy{{199;;&&rc   c                 T    |                                 |                                  z  S r   r  r  s     r`   __rtruediv__zPoly.__rtruediv__  r  rc   otherc                 2   | |}}|j         sO	 |                    ||j        |                                          }n# t          t
          t          f$ r Y dS w xY w|j        |j        k    rdS |j        j        |j        j        k    rdS |j        |j        k    S Nr'  F)	rs   r   rU   r   r9   r5   r6   rf   r   )r   r  r[   r\   s       r`   __eq__zPoly.__eq__  s    U1y 	KK16!,,..KAA#[.A   uu 6QV559	!!5u~s   /= AAc                     | |k     S r   r   r  s     r`   __ne__zPoly.__ne__  s    6zrc   c                     | j          S r   )r  r   s    r`   __bool__zPoly.__bool__  s    9}rc   c                 V    |s| |k    S |                      t          |                    S r   )
_strict_eqr"   r[   r\   stricts      r`   eqzPoly.eq  s+     	,6M<<

+++rc   c                 2    |                      ||           S )Nr  )r  r  s      r`   nezPoly.ne   s    44&4))))rc   c                     t          || j                  o0| j        |j        k    o | j                            |j        d          S NTr  )rR   r   rU   rf   r  r  s     r`   r  zPoly._strict_eq  s<    !Q[))_af.>_1588AEZ^8C_C__rc   NNr   )FF)FTr   )r|   FNT)FNNNFFNNFFrO  rm  rn  T)Fr  F)rZ   
__module____qualname____doc__	__slots__is_commutativers   _op_priorityrz   classmethodr   propertyr   rx   r   r   r   r   rT   rp   rq   rt   ru   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ri   r   r	  r   r  r  r  r   r#  r(  r*  r   r   r  r:  r<  r?  rB  rD  r  rI  rY   rO  rQ  rS  rV  r[  r_  ra  re  rg  ri  rk  rm  ro  r  ru  r   rx  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  r  r  r  _eval_derivativer   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"  r(  r,  r/  r3  rI  r[  rc  rh  rk  r  r  r  r  r  r  r  r  r  rK  r  r  r  r  r  r  r  r  r  r  r  r  r  rb   r  r  r  r  r  r  r   rW   r  r  r   r  r  r  r
  r  r  r  r  r  r  r   r  __classcell__r   s   @r`   rS   rS   e   s       3 3j  INGL0 0 0>   [ E E XE ( ( X(' ' ' ( ( [(
 ( ( [(
 ( ( [(
 ( ( [(
 A A [A& A A [A*   [, ( ( [(
 A A [A" " " " " 5 5 X5<   X:   X  ! ! X!, O O XO N N XN O O XO  81 1 1f+ + + +:0 0 0
  ) ) )"J J J** * *% % %&!M !M !M !MF2 2 2D D D4"L "L "LH     D  *  *  *4 4 4 40    J J J J,) ) ) )(Q Q Q Q(C C C " " "(J J J #= #= #=J      2 2 2 2&) ) ) )$= $= $=L  4  *     *#$ #$ #$ #$J(% (% (%T     *  *  *  *  0  4  *  0  >  >  >  0  4  .% % %N  8  >% % % %N# # # #J# # # #J( ( ( (T@ @ @(5 5 5 56: : :&; ; ;* <  <  <D@ @ @2* * * *0* * **1 1 1 1&#9 #9 #9J *  *  *D
1 
1 
1 
14 4 4 4(5 5 5 5 . . . .*. . . ."* * *** * **%& %& %& %&N% % %N&8 &8 &8P$3 $3 $3L I' I' I' I'V  ,   >& & & &B   >     D& & &2#% #% #% #%J' ' '*I# I# I# I#VI  I  I  I V* * *:  .  .  .   :* * **7 7 7*  .( ( (*  *  4( ( ( (:2 2 2.! ! !F% % %>  *N N N N:3 3 3 3BN N N63 3 36JR JR JR JRX#. #. #. #.J= = = =~K K K K:0 0 0 0.0 0 0 02N N N N`  6& & &P1D 1D 1Df#+ #+ #+ #+J%> %> %>N4 4 4 4l   X$   X$   X$   X$ " " X"$   X(   X$ " " X"$ ! ! X!$ $ $ X$. $ $ X$$     X ,     X , # # X#.       Y   Y   Y   Y   Y   Y Z^$$" " %$"   Y   Y   Y   Y   Y   Y Z^$$' ' %$' Z^$$' ' %$' Z((  )(" Z^$$  %$  , , , ,* * * *` ` ` ` ` ` `rc   rS   c                   t     e Zd ZdZd Z fdZed             Z ede	          d             Z
d Zd Z xZS )	PurePolyz)Class for representing pure polynomials. c                     | j         fS )z$Allow SymPy to hash Poly instances. )rf   r   s    r`   r   zPurePoly._hashable_content  s    {rc   c                 D    t                                                      S r   r   r   s    r`   r   zPurePoly.__hash__  r   rc   c                     | j         S )aR  
        Free symbols of a polynomial.

        Examples
        ========

        >>> from sympy import PurePoly
        >>> from sympy.abc import x, y

        >>> PurePoly(x**2 + 1).free_symbols
        set()
        >>> PurePoly(x**2 + y).free_symbols
        set()
        >>> PurePoly(x**2 + y, x).free_symbols
        {y}

        )r   r   s    r`   r   zPurePoly.free_symbols  s    & **rc   r  c                 :   | |}}|j         sO	 |                    ||j        |                                          }n# t          t
          t          f$ r Y dS w xY wt          |j                  t          |j                  k    rdS |j        j	        |j        j	        k    rl	 |j        j	        
                    |j        j	        |j                  }n# t          $ r Y dS w xY w|                    |          }|                    |          }|j        |j        k    S r  )rs   r   rU   r   r9   r5   r6   r~   rf   r   r   r7   r   )r   r  r[   r\   r   s        r`   r  zPurePoly.__eq__'  s   U1y 	KK16!,,..KAA#[.A   uu qv;;#af++%%559	!!eiooaei88$   uu S!!AS!!Au~s!   /= AA"/C 
C C c                 n    t          || j                  o | j                            |j        d          S r"  )rR   r   rf   r  r  s     r`   r  zPurePoly._strict_eq?  s-    !Q[))JaehhquTh.J.JJrc   c                    t          |          }|j        ss	 | j        j        | j        | j        | j                            | j        j                            |                    fS # t          $ r t          d| d|          w xY wt          | j	                  t          |j	                  k    rt          d| d|          t          | j        t                    rt          |j        t                    st          d| d|          | j        | j	        }| j        j                            |j        j        |          }| j                            |          }|j                            |          }||d ffd	}||||fS )Nr   r   c                     |/|d |         ||dz   d          z   }|s|                     |           S  j        | g|R  S r   r   r   s       r`   r   zPurePoly._unify.<locals>.perY  r   rc   )r"   rs   rf   r   r   r   r6   r7   r~   rU   rR   r1   r   r   r   )r[   r\   rU   r   r   r   r   rw   s          @r`   r   zPurePoly._unifyB  s   AJJy 	LLuy!%		!%):N:Nq:Q:Q0R0RRR! L L L''QQQ(JKKKL qv;;#af++%%##AA$FGGG15#&& 	H:aeS+A+A 	H##AA$FGGGkveiooaei..EMM#EMM#4 	' 	' 	' 	' 	' 	' CA~s   AA( ( B)rZ   r)  r*  r+  r   r   r0  r   r   rW   r  r  r   r2  r3  s   @r`   r5  r5    s        33  " " " " " + + X+( Z((  )(.K K K             rc   r5  c                 L    t          j        ||          }t          | |          S r   )rj   rk   _poly_from_expr)r   rU   rx   ry   s       r`   poly_from_exprr?  e  s&     
d
+
+C4%%%rc   c                 <   | t          |           } }t          | t                    st          |||           | j        rE| j                            | |          }|j        |_        |j        |_        |j	        d|_	        ||fS |j
        r| 
                                } t          | |          \  }}|j        st          |||           t          t          t          |                                                     \  }}|j        }|t          ||          \  |_        }n"t          t!          |j        |                    }t%          t          t          ||                              }t&                              ||          }|j	        d|_	        ||fS )r   NTr   F)r"   rR   r   r<   rs   r   rt   rU   r   r`  expandrC   rr   r   r   r(   r   r   ro   rS   rp   )r   ry   origrN  rf   r   r   r   s           r`   r>  r>  l  sy   wt}}$DdE""  dD111	 ~((s339[
9CISy	 {{}}tS))HC8 2 dD111#tCIIKK00122NFFZF~-f#>>>
FFc&+V4455
tC''((
)
)C??3$$D
y	9rc   c                 L    t          j        ||          }t          | |          S )(Construct polynomials from expressions. )rj   rk   _parallel_poly_from_expr)exprsrU   rx   ry   s       r`   parallel_poly_from_exprrG    s&     
d
+
+C#E3///rc   c           	      0   t          |           dk    r| \  }}t          |t                    rt          |t                    rz|j                            ||          }|j                            ||          }|                    |          \  }}|j        |_        |j        |_        |j        d|_        ||g|fS t          |           g } }g g }}d}t          |          D ]\  }}	t          |	          }	t          |	t                    rN|	j        r|                    |           n3|                    |           |j        r|	                                }	nd}|                     |	           |rt!          ||| d          |r"|D ]}| |                                         | |<    t%          | |          \  }
}|j        st!          ||| d          ddlm} |j        D ]!}t          ||          rt+          d          "g g }}g }g }|
D ]}t          t-          t          |                                                     \  }}|                    |           |                    |           |                    t          |                     |j        }|t3          ||          \  |_        }n"t          t5          |j        |                    }|D ])}|                    |d|                    ||d         }*g }t-          ||          D ]_\  }}t9          t          t-          ||                              }t                              ||          }|                    |           `|j        t=          |          |_        ||fS )	rD  r  NTFr   	Piecewisez&Piecewise generators do not make senser   )r~   rR   rS   r   rt   r   rU   r   r`  rr   r   r"   r   rs   appendrA  r<   rY   rD   $sympy.functions.elementary.piecewiserJ  r9   r   r   extendr(   r   r   ro   rp   bool)rF  ry   r[   r\   origs_exprs_polysfailedr   r   repsrJ  r\  coeffs_listlengthsr<  r:  rf   r   r   r   r`  rN  s                          r`   rE  rE    s   
5zzQ1a 	:a#6#6 	&&q#..A&&q#..A771::DAqvCHCJy  	q63;;;5EFFFU##  4t}}dE"" 		| )a    a   : );;==DFT : eUD999 * 	* 	*AQx''))E!HH(44ID#8 : eUD999>>>>>>X L La## 	L!"JKKK	L rKJJ $ $c4		#4#45666"""&!!!s6{{####ZF~"2;C"H"H"H
KK3v0+>>?? & &+bqb/***!!""oEj*55  4FF++,,--sC((T
yLL	#:rc   c                 6    t          |           } || vr|| |<   | S )z7Add a new ``(key, value)`` pair to arguments ``dict``. )ro   )rx   r  rL  s      r`   _update_argsrW    s$    ::D
$S	Krc   c                    t          | d          } t          |d          j        }| j        r| }|                                j        }n1| j        }|s(|rt	          |           \  }}nt	          | |          \  }}|r| rt
          j        nt
          j        S |sJ| j        r-||j        vr$t	          |                                           \  }}||j        vrt
          j        S nd| j        s]t          | j
                  dk    rEt          t          d| dt          t          | j
                            d|d                    |                    |          }t!          |t"                    rt%          |          nt
          j        S )a  
    Return the degree of ``f`` in the given variable.

    The degree of 0 is negative infinity.

    Examples
    ========

    >>> from sympy import degree
    >>> from sympy.abc import x, y

    >>> degree(x**2 + y*x + 1, gen=x)
    2
    >>> degree(x**2 + y*x + 1, gen=y)
    1
    >>> degree(0, x)
    -oo

    See also
    ========

    sympy.polys.polytools.Poly.total_degree
    degree_list
    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_Numberrs   rY   r?  r   ZerorS  rU   r~   r   r  rI   nextr   r  rR   r+  r   )r[   r   
gen_is_Numr  isNumr   r^   s          r`   r  r    s   6 	$AT***4Jy 	.		% 	. .%a((11%a--1 32qvv 22 89 	/AF**!!))++..DAqaf6M Y 83q~..22

 qq$wq~..////	$6 7 7 8 8 	8
 XXc]]F(55M76???1;MMrc   c                     t          |           }|j        r|                                }|j        rd}n2| j        r	|p| j        }t          ||                                          }t          |          S )a  
    Return the total_degree of ``f`` in the given variables.

    Examples
    ========
    >>> from sympy import total_degree, Poly
    >>> from sympy.abc import x, y

    >>> total_degree(1)
    0
    >>> total_degree(x + x*y)
    2
    >>> total_degree(x + x*y, x)
    1

    If the expression is a Poly and no variables are given
    then the generators of the Poly will be used:

    >>> p = Poly(x + x*y, y)
    >>> total_degree(p)
    1

    To deal with the underlying expression of the Poly, convert
    it to an Expr:

    >>> total_degree(p.as_expr())
    2

    This is done automatically if any variables are given:

    >>> total_degree(p, x)
    1

    See also
    ========
    degree
    r   )r"   rs   rY   rY  rU   rS   r  r   )r[   rU   r  rvs       r`   r  r  >  sr    P 	

Ay IIKK{ *9 	">16D!T]]''))2;;rc   c                 
   t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }t          t          t          |                    S )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)	rj   allowed_flagsr?  r<   r=   r  rA  r   r   )r[   rU   rx   r   ry   r  degreess          r`   r  r  s  s     $	***71D111D1133 7 7 7q#6667 mmooGWg&&'''   , 
AAAc                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  S )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|   Nr2  )rj   ra  r?  r<   r=   r  rh   r[   rU   rx   r   ry   r  s         r`   r  r    s     $	***.1D111D1133 . . .a---. 44ci4   rc  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  }|                                S )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|   Nr2  )rj   ra  r?  r<   r=   r  rh   rY   )r[   rU   rx   r   ry   r  r   s          r`   r  r    s     $	***.1D111D1133 . . .a---. DDsyD!!E==??rc  c                    t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  \  }}||                                z  S )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|   Nr2  )rj   ra  r?  r<   r=   r  rh   rY   )r[   rU   rx   r   ry   r  r   r   s           r`   r  r    s     $	***.1D111D1133 . . .a---. 44ci4((LE5  rc  c                 >   t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          \  }}	|j        s(|                                |	                                fS ||	fS )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)rj   ra  rG  r<   r=   r}  r`  rY   
r[   r\   rU   rx   r   r   ry   r  r~  r  s
             r`   r}  r}    s     $	***0-q!fDtDDDtDDA 0 0 03///0 66!99DAq9 yy{{AIIKK''!t   1 
AAAc                    t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          }|j        s|                                S |S )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)rj   ra  rG  r<   r=   r  r`  rY   	r[   r\   rU   rx   r   r   ry   r  r  s	            r`   r  r    s     $	***0-q!fDtDDDtDDA 0 0 03///0 	
q		A9 yy{{rj  c                 L   t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w	 |                    |          }n# t          $ r t          | |          w xY w|j        s|                                S |S )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)	rj   ra  rG  r<   r=   r  r;   r`  rY   	r[   r\   rU   rx   r   r   ry   r  r~  s	            r`   r  r    s    " $	***0-q!fDtDDDtDDA 0 0 03///0(FF1II ( ( (!!Q'''( 9 yy{{s    1 
AAAA+ +Bc                    t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          }|j        s|                                S |S )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)rj   ra  rG  r<   r=   r  r`  rY   rn  s	            r`   r  r  :  s    ( $	***2-q!fDtDDDtDDA 2 2 2!S1112 	
A9 yy{{rj  c                 N   t          j        |ddg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    ||j                  \  }}	|j        s(|                                |	                                fS ||	fS )a  
    Compute polynomial division of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import div, ZZ, QQ
    >>> from sympy.abc import x

    >>> div(x**2 + 1, 2*x - 4, domain=ZZ)
    (0, x**2 + 1)
    >>> div(x**2 + 1, 2*x - 4, domain=QQ)
    (x/2 + 1, 5)

    r  r`  r  r  Nr  )	rj   ra  rG  r<   r=   r  r  r`  rY   ri  s
             r`   r  r  ]  s    " $ 1222/-q!fDtDDDtDDA / / /q#.../ 555""DAq9 yy{{AIIKK''!t   2 
AAAc                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    ||j                  }|j        s|                                S |S )a  
    Compute polynomial remainder of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import rem, ZZ, QQ
    >>> from sympy.abc import x

    >>> rem(x**2 + 1, 2*x - 4, domain=ZZ)
    x**2 + 1
    >>> rem(x**2 + 1, 2*x - 4, domain=QQ)
    5

    r  r`  r  r  Nrq  )	rj   ra  rG  r<   r=   r  r  r`  rY   rl  s	            r`   r  r  }      " $ 1222/-q!fDtDDDtDDA / / /q#.../ 	
achA9 yy{{rr  c                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    ||j                  }|j        s|                                S |S )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  Nrq  )	rj   ra  rG  r<   r=   r  r  r`  rY   rn  s	            r`   r  r    rt  rr  c                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    ||j                  }|j        s|                                S |S )aQ  
    Compute polynomial exact quotient of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import exquo
    >>> from sympy.abc import x

    >>> exquo(x**2 - 1, x - 1)
    x + 1

    >>> exquo(x**2 + 1, 2*x - 4)
    Traceback (most recent call last):
    ...
    ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

    r  r`  r  r  Nrq  )	rj   ra  rG  r<   r=   r  r  r`  rY   rn  s	            r`   r  r    s    ( $ 12221-q!fDtDDDtDDA 1 1 1C0001 	
!!A9 yy{{rr  c                 0   t          j        |ddg           	 t          | |fg|R i |\  \  }}}n# t          $ r}t	          |j                  \  }\  }	}
	 |                    |	|
          \  }}|                    |          |                    |          fcY d}~S # t          $ r t          dd|          w xY wd}~ww xY w|                    ||j
                  \  }}|j        s(|                                |                                fS ||fS )aT  
    Half extended Euclidean algorithm of ``f`` and ``g``.

    Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``.

    Examples
    ========

    >>> from sympy import half_gcdex
    >>> from sympy.abc import x

    >>> half_gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4)
    (3/5 - x/5, x + 1)

    r  r`  Nr  r  rq  )rj   ra  rG  r<   r(   rF  r  r   rl   r=   r  r`  rY   )r[   r\   rU   rx   r   r   ry   r  r   r  r  r  r  s                r`   r  r    sN   " $ 1222
:-q!fDtDDDtDDA : : :)#)44A	:$$Q**DAq ??1%%vq'9'99999999 # 	: 	: 	:#L!S999	:: <<<))DAq9 yy{{AIIKK''!ts2   2 
CB>B0)B>CB;;B>>Cc                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}n# t          $ r}t	          |j                  \  }\  }	}
	 |                    |	|
          \  }}}|                    |          |                    |          |                    |          fcY d}~S # t          $ r t          dd|          w xY wd}~ww xY w|                    ||j
                  \  }}}|j        s;|                                |                                |                                fS |||fS )aZ  
    Extended Euclidean algorithm of ``f`` and ``g``.

    Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.

    Examples
    ========

    >>> from sympy import gcdex
    >>> from sympy.abc import x

    >>> gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4)
    (3/5 - x/5, x**2/5 - 6*x/5 + 2, x + 1)

    r  r`  Nr  r  rq  )rj   ra  rG  r<   r(   rF  r  r   rl   r=   r  r`  rY   )r[   r\   rU   rx   r   r   ry   r  r   r  r  r  r  r  s                 r`   r  r    sp   " $ 1222
N-q!fDtDDDtDDA N N N)#)44A	Nll1a((GAq! ??1%%vq'9'96??1;M;MMMMMMMM # 	5 	5 	5#GQ444	5N ggachg''GAq!9 yy{{AIIKK44!Qws2   2 
CCB41=C.C4CCCc                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}nz# t          $ rm}t	          |j                  \  }\  }	}
	 |                    |                    |	|
                    cY d}~S # t          $ r t          dd|          w xY wd}~ww xY w|                    ||j
                  }|j        s|                                S |S )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:`~.mod_inverse` function:

    >>> mod_inverse(3, 5)
    2
    >>> (S(2)/5).invert(S(7)/3)
    5/2

    See Also
    ========

    sympy.core.numbers.mod_inverse

    r  r`  Nr  r  rq  )rj   ra  rG  r<   r(   rF  r   r  rl   r=   r  r`  rY   )r[   r\   rU   rx   r   r   ry   r  r   r  r  r  s               r`   r  r  .  s   B $ 12226-q!fDtDDDtDDA 6 6 6)#)44A	6??6==A#6#677777777" 	6 	6 	6#Ha555	66 	
""A9 yy{{s,   2 
B)B$(B?B)B!!B$$B)c                     t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          }|j        sd |D             S |S )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`  r  r  Nc                 6    g | ]}|                                 S r   r  r   r  s     r`   r   z!subresultants.<locals>.<listcomp>|       ,,,		,,,rc   )rj   ra  rG  r<   r=   r  r`  	r[   r\   rU   rx   r   r   ry   r  r^   s	            r`   r  r  c  s     $	***9-q!fDtDDDtDDA 9 9 9C8889 __QF9 ,,V,,,,rj  Fr  c                   t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|r|                    ||          \  }	}
n|                    |          }	|j        s6|r |	                                d |
D             fS |	                                S |r|	|
fS |	S )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`  r  r  Nr  c                 6    g | ]}|                                 S r   r  r|  s     r`   r   zresultant.<locals>.<listcomp>  s     %=%=%=aaiikk%=%=%=rc   )rj   ra  rG  r<   r=   r  r`  rY   )r[   r\   r  rU   rx   r   r   ry   r  r^   r  s              r`   r  r    s    $	***5-q!fDtDDDtDDA 5 5 5Q4445   KKjK99	Q9  	>>>##%=%=1%=%=%===~~ 	19rj  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }|j        s|                                S |S )z
    Compute discriminant of ``f``.

    Examples
    ========

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

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

    r`  r  r|   N)rj   ra  r?  r<   r=   r  r`  rY   r[   rU   rx   r   ry   r  r^   s          r`   r  r    s     $	***81D111D1133 8 8 837778 ^^F9 ~~rc  c                 t   t          j        |dg           	 t          | |fg|R i |\  \  }}}n# t          $ r}t	          |j                  \  }\  }	}
	 |                    |	|
          \  }}}|                    |          |                    |          |                    |          fcY d}~S # t          $ r t          dd|          w xY wd}~ww xY w|                    |          \  }}}|j
        s;|                                |                                |                                fS |||fS )a  
    Compute GCD and cofactors of ``f`` and ``g``.

    Returns polynomials ``(h, cff, cfg)`` such that ``h = gcd(f, g)``, and
    ``cff = quo(f, h)`` and ``cfg = quo(g, h)`` are, so called, cofactors
    of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import cofactors
    >>> from sympy.abc import x

    >>> cofactors(x**2 - 1, x**2 - 3*x + 2)
    (x - 1, x + 1, x - 2)

    r`  Nr  r  )rj   ra  rG  r<   r(   rF  r  r   rl   r=   r`  rY   )r[   r\   rU   rx   r   r   ry   r  r   r  r  r  r  r  s                 r`   r  r    sh   & $	***
R-q!fDtDDDtDDA R R R)#)44A	R **1a00KAsC ??1%%vs';';V__S=Q=QQQQQQQQ # 	9 	9 	9#KC888	9R ++a..KAsC9 yy{{CKKMM3;;==88#s{s2   1 
CCB30=C-C3CCCc                 p   t          |           } fd} ||           }||S t          j        dg           	 t          | gR i \  }}t	          |           dk    rt          d | D                       rx| d         fd| dd         D             }t          d |D                       rAd}|D ]*}	t          ||	                                d	                   }+t          |z            S nI# t          $ r<}
 ||
j
                  }||cY d}
~
S t          d
t	          |           |
          d}
~
ww xY w|s$|j        st          j        S t          d	|          S |d	         |dd         }}|D ] }|                    |          }|j        r n!|j        s|                                S |S )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                    s}s{t          |           \  }}|s|j        S |j        rY|d         |dd          }}|D ]/}|                    ||          }|                    |          r n0|                    |          S d S Nr   r|   )r(   r   rY  r  r  r   seqr   numbersr^   numberrx   rU   s        r`   try_non_polynomial_gcdz(gcd_list.<locals>.try_non_polynomial_gcd  s     	/D 	/.s33OFG /{"$ 	/")!*gabbk%  F#ZZ77F}}V,,  v...trc   Nr`  r|   c              3   2   K   | ]}|j         o|j        V  d S r   is_algebraicis_irrationalr   r  s     r`   	<genexpr>zgcd_list.<locals>.<genexpr>  -      VV3 0 FS5FVVVVVVrc   r  c                 >    g | ]}|z                                   S r   ratsimpr   r  r  s     r`   r   zgcd_list.<locals>.<listcomp>  '    ;;;#QsUOO%%;;;rc   c              3   $   K   | ]}|j         V  d S r   is_rationalr   frcs     r`   r  zgcd_list.<locals>.<genexpr>  $      22s3?222222rc   r   gcd_listr   )r"   rj   ra  rG  r~   r%  r  as_numer_denomrm  r<   rF  r=   r`  r   rZ  rS   r  r  rY   )r  rU   rx   r  r^   r`  ry   lstlcr  r  rN  r  s    ``         @r`   r  r    s;    #,,C     & $#C((F$	***?,S@4@@@4@@
s s88a<<CVVRUVVVVV<BA;;;;SbS;;;C22c22222 ! : :CR!3!3!5!5a!899BB1R4yy  ? ? ?''	22MMMMMM#JC#>>>?  $y 	$6Ms####!HeABBiEF  D!!= 	E	 9 ~~s$   B6C7 7
D=D8D=D88D=c                 (   t          | d          r||f|z   }t          | g|R i |S |t          d          t          j        |dg           	 t          | |fg|R i |\  \  }}}t          t          | |f          \  }}|j        r]|j	        rV|j        rO|j	        rH||z  
                                }	|	j        r*t          ||	                                d         z            S nz# t          $ rm}
t          |
j                  \  }\  }}	 |                    |                    ||                    cY d}
~
S # t&          $ r t)          dd|
          w xY wd}
~
ww xY w|                    |          }|j        s|                                S |S )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   r  r  )r  r  r  rj   ra  rG  r   r"   r  r  r  r  rm  r  r<   r(   rF  r   r  rl   r=   r`  rY   r[   r\   rU   rx   r   r   ry   r  r  r  r  r   r^   s                r`   r  r  B  s    q* N=4$;D)D)))D)))	
LMMM$	***3-q!fDtDDDtDDA 7QF##1> 	6ao 	6!. 	6Q_ 	6Q3--//C 61S//11!44555 3 3 3)#)44A	3??6::a#3#344444444" 	3 	3 	3#E1c222	33 UU1XXF9 ~~s1   BC( (
E2E(D;5E;EEEc                 j   t          |           } dt          t                   ffd} ||           }||S t          j        dg           	 t          | gR i \  }}t          |           dk    rt          d | D                       rk| d         fd| dd         D             }t          d	 |D                       r4d}|D ]*}	t          ||		                                d                   }+|z  S nI# t          $ r<}
 ||
j                  }||cY d}
~
S t          d
t          |           |
          d}
~
ww xY w|s$|j        st          j        S t!          d|          S |d         |dd         }}|D ]}|                    |          }|j        s|                                S |S )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

    returnc                     syswt          |           \  }}|s|                    |j                  S |j        rB|d         |dd          }}|D ]}|                    ||          }|                    |          S d S r  )r(   r   r   rY  r  r  s        r`   try_non_polynomial_lcmz(lcm_list.<locals>.try_non_polynomial_lcm  s     	/D 	/.s33OFG /vz222$ /")!*gabbk% 8 8F#ZZ77FFv...trc   Nr`  r|   c              3   2   K   | ]}|j         o|j        V  d S r   r  r  s     r`   r  zlcm_list.<locals>.<genexpr>  r  rc   r  c                 >    g | ]}|z                                   S r   r  r  s     r`   r   zlcm_list.<locals>.<listcomp>  r  rc   c              3   $   K   | ]}|j         V  d S r   r  r  s     r`   r  zlcm_list.<locals>.<genexpr>  r  rc   lcm_listr   r   )r"   r   r	   rj   ra  rG  r~   r%  r  r  r<   rF  r=   r`  r   r  rS   rY   )r  rU   rx   r  r^   r`  ry   r  r  r  r  rN  r  s    ``         @r`   r  r  v  s0    #,,Cx~         $#C((F$	***?,S@4@@@4@@
s s88a<<CVVRUVVVVV<BA;;;;SbS;;;C22c22222  : :CR!3!3!5!5a!899BBt ? ? ?''	22MMMMMM#JC#>>>?  $y 	$5Ls####!HeABBiEF " "D!!9 ~~s%   B)C= =
ED>E D>>Ec                    t          | d          r||f|z   }t          | g|R i |S |t          d          t          j        |dg           	 t          | |fg|R i |\  \  }}}t          t          | |f          \  }}|j        rP|j	        rI|j        rB|j	        r;||z  
                                }	|	j        r||	                                d         z  S nz# t          $ rm}
t          |
j                  \  }\  }}	 |                    |                    ||                    cY d}
~
S # t$          $ r t'          dd|
          w xY wd}
~
ww xY w|                    |          }|j        s|                                S |S )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

    r  Nz2lcm() takes 2 arguments or a sequence of argumentsr`  r|   r  r  )r  r  r  rj   ra  rG  r   r"   r  r  r  r  r  r<   r(   rF  r   r  rl   r=   r`  rY   r  s                r`   r  r    s    q* N=4$;D)D)))D)))	
LMMM$	***3-q!fDtDDDtDDA 7QF##1> 	1ao 	1!. 	1Q_ 	1Q3--//C 1++--a000 3 3 3)#)44A	3??6::a#3#344444444" 	3 	3 	3#E1c222	33 UU1XXF9 ~~s1   BC 
E%E (D.(E.E

EEc                 
   t          |           }t          | t                    r"t          fd| j        | j        fD              S t          | t
                    rt          d|           t          | t                    r| j        r|S 	                    dd          rF | j
        fd| j        D              }                    d           dd<   t          |gR i S                     dd          }t          j        d	g           	 t!          | gR i \  }}n# t"          $ r}|j        cY d
}~S d
}~ww xY w|                                \  }	} |j        j        rN|j        j        r|                     d          \  }
} |                                 \  }} |j        j        r||
z  }nt0          j        }t5          d t7          | j        |	          D              }t;          |d          rt0          j        }|dk    r|S |r%t=          |||                                 z            S t=          ||                                 d                                           \  }} t=          ||| z  d          S )az  
    Remove GCD of terms from ``f``.

    If the ``deep`` flag is True, then the arguments of ``f`` will have
    terms_gcd applied to them.

    If a fraction is factored out of ``f`` and ``f`` is an Add, then
    an unevaluated Mul will be returned so that automatic simplification
    does not redistribute it. The hint ``clear``, when set to False, can be
    used to prevent such factoring when all coefficients are not fractions.

    Examples
    ========

    >>> from sympy import terms_gcd, cos
    >>> from sympy.abc import x, y
    >>> terms_gcd(x**6*y**2 + x**3*y, x, y)
    x**3*y*(x**3*y + 1)

    The default action of polys routines is to expand the expression
    given to them. terms_gcd follows this behavior:

    >>> terms_gcd((3+3*x)*(x+x*y))
    3*x*(x*y + x + y + 1)

    If this is not desired then the hint ``expand`` can be set to False.
    In this case the expression will be treated as though it were comprised
    of one or more terms:

    >>> terms_gcd((3+3*x)*(x+x*y), expand=False)
    (3*x + 3)*(x*y + x)

    In order to traverse factors of a Mul or the arguments of other
    functions, the ``deep`` hint can be used:

    >>> terms_gcd((3 + 3*x)*(x + x*y), expand=False, deep=True)
    3*x*(x + 1)*(y + 1)
    >>> terms_gcd(cos(x + x*y), deep=True)
    cos(x*(y + 1))

    Rationals are factored out by default:

    >>> terms_gcd(x + y/2)
    (2*x + y)/2

    Only the y-term had a coefficient that was a fraction; if one
    does not want to factor out the 1/2 in cases like this, the
    flag ``clear`` can be set to False:

    >>> terms_gcd(x + y/2, clear=False)
    x + y/2
    >>> terms_gcd(x*y/2 + y**2, clear=False)
    y*(x/2 + y)

    The ``clear`` flag is ignored if all coefficients are fractions:

    >>> terms_gcd(x/3 + y/2, clear=False)
    (2*x + 3*y)/6

    See Also
    ========
    sympy.core.exprtools.gcd_terms, sympy.core.exprtools.factor_terms

    c              3   8   K   | ]}t          |gR i V  d S r   r_  )r   r  rx   rU   s     r`   r  zterms_gcd.<locals>.<genexpr>=  s;      NN!)A555555NNNNNNrc   z3Inequalities cannot be used with terms_gcd. Found: deepFc                 0    g | ]}t          |gR i S r   r  )r   r  rx   rU   s     r`   r   zterms_gcd.<locals>.<listcomp>E  s1    CCCqy2T222T22CCCrc   rA  clearTr`  Nr  c                     g | ]
\  }}||z  S r   r   )r   r  r   s      r`   r   zterms_gcd.<locals>.<listcomp>_  s     111$!QA111rc   r|   )r  )!r"   rR   r   lhsrhsr   r  r	   is_Atomr  r_   rx   popr_  rj   ra  r?  r<   r   r   r  r  r  r	  r   r  r   r   rU   r   r   rY   as_coeff_Mul)r[   rU   rx   rB  r   r  r   ry   r  r   denomr   terms    ``          r`   r_  r_    s   F 1::D!X WNNNNNqu~NNNOO	Az	"	" WiRSRSUVVVa !) xx -afCCCCCAFCCCDX,t,,,t,,,HHWd##E$	***1D111D1133   x ;;==DAq
z 	: 	4~~d~33HE1;;==q: 	UNE11#afa..1112DE1 199K 45$qyy{{"2333 5!))++U;;;HHJJHE1ud1fE2222s   D+ +
E5E;EEc                    t          j        |ddg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    t          |                    }|j        s|                                S |S )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`  r  r|   N)	rj   ra  r?  r<   r=   r  r"   r`  rY   )r[   r  rU   rx   r   ry   r  r^   s           r`   r  r  m  s     $ 122211D111D1133 1 1 1C0001 WWWQZZ  F9 ~~   - 
AAAc                    t          j        |ddg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  }|j        s|                                S |S )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`  r  r|   Nrq  )	rj   ra  r?  r<   r=   r  r  r`  rY   r  s          r`   r  r    s     $ 122211D111D1133 1 1 1C0001 WW#(W##F9 ~~r  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                S )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`  r  r|   N)rj   ra  r?  r<   r=   r  re  s         r`   r  r    s     $	***31D111D1133 3 3 3	1c2223 99;;rc  c                    t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                \  }}|j        s||                                fS ||fS )a  
    Compute content and the primitive form of ``f``.

    Examples
    ========

    >>> from sympy.polys.polytools import primitive
    >>> from sympy.abc import x

    >>> primitive(6*x**2 + 8*x + 12)
    (2, 3*x**2 + 4*x + 6)

    >>> eq = (2 + 2*x)*x + 2

    Expansion is performed by default:

    >>> primitive(eq)
    (2, x**2 + x + 1)

    Set ``expand`` to False to shut this off. Note that the
    extraction will not be recursive; use the as_content_primitive method
    for recursive, non-destructive Rational extraction.

    >>> primitive(eq, expand=False)
    (1, x*(2*x + 2) + 2)

    >>> eq.as_content_primitive()
    (2, x*(x + 1) + 1)

    r`  r	  r|   N)rj   ra  r?  r<   r=   r	  r`  rY   )r[   rU   rx   r   ry   r  r
  r^   s           r`   r	  r	    s    @ $	***51D111D1133 5 5 5Q4445 ;;==LD&9 V^^%%%%V|rc  c                    t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          }|j        s|                                S |S )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`  r  r  N)rj   ra  rG  r<   r=   r  r`  rY   r~  s	            r`   r  r    s     $	***3-q!fDtDDDtDDA 3 3 3	1c2223 YYq\\F9 ~~rj  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }|j        sd |D             S |S )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|   Nc                 6    g | ]}|                                 S r   r  r|  s     r`   r   zdecompose.<locals>.<listcomp>'  r}  rc   )rj   ra  r?  r<   r=   r  r`  r  s          r`   r  r    s     $	***51D111D1133 5 5 5Q4445 [[]]F9 ,,V,,,,rc  c                     t          j        |ddg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  }|j        sd |D             S |S )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|   Nrq  c                 6    g | ]}|                                 S r   r  r|  s     r`   r   zsturm.<locals>.<listcomp>E  r}  rc   )rj   ra  r?  r<   r=   r  r  r`  r  s          r`   r  r  ,  s     $ 122211D111D1133 1 1 1C0001 WW#(W##F9 ,,V,,,,r  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }|j        sd |D             S |S )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|   Nc                 @    g | ]\  }}|                                 |fS r   r  )r   r\   r\  s      r`   r   zgff_list.<locals>.<listcomp>t  s)    555TQa 555rc   )rj   ra  r?  r<   r=   r  r`  )r[   rU   rx   r   ry   r  r&  s          r`   r  r  J  s    @ $	***41D111D1133 4 4 4
As3334 jjllG9 55W5555rc  c                      t          d          )z3Compute greatest factorial factorization of ``f``. zsymbolic falling factorialr  r[   rU   rx   s      r`   gffr  y  s     :
;
;;rc   c                 l   t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                \  }}}|j        s6t          |          |                                |                                fS t          |          ||fS )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)	rj   ra  r?  r<   r=   r  r`  r   rY   )	r[   rU   rx   r   ry   r  r  r\   r  s	            r`   r  r    s    & $	***41D111D1133 4 4 4
As3334 jjllGAq!9  qzz199;;		33qzz1arc  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }|j        s|                                S |S )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)rj   ra  r?  r<   r=   r   r`  rY   r  s          r`   r   r     s     $	***41D111D1133 4 4 4
As3334 ZZ\\F9 ~~rc  c                 >    |dk    rd }nd }t          | |          S )z&Sort a list of ``(expr, exp)`` pairs. r9  c                     | \  }}|j         j         }|t          |          t          |j                  t          |j                  |fS r   rf   r~   rU   rn   r   r   rN  exprf   s       r`   r  z_sorted_factors.<locals>.key  s=    ID#(,CS3ty>>3t{3C3CSIIrc   c                     | \  }}|j         j         }t          |          t          |j                  |t          |j                  |fS r   r  r  s       r`   r  z_sorted_factors.<locals>.key  s=    ID#(,CHHc$)nnc3t{3C3CSIIrc   r  )r  )r&  methodr  s      r`   _sorted_factorsr    sJ    	J 	J 	J 	J
	J 	J 	J
 's####rc   c                 (    t          d | D              S )z*Multiply a list of ``(expr, exp)`` pairs. c                 B    g | ]\  }}|                                 |z  S r   r  r   r[   r\  s      r`   r   z$_factors_product.<locals>.<listcomp>  s(    444DAqa444rc   )r   )r&  s    r`   _factors_productr    s    44G44455rc   c                    t           j        g c}d t          j        |           D             }|D ]+}|j        s$t          |t                    rt          |          r||z  }4|j        rS|j	        t           j
        k    r>|j        \  }|j        rj        r||z  }n|j        r                    |f           n|t           j        c}	 t          ||          \  }}t          ||dz             }	 |	            \  }
}|
t           j        urPj        r	||
z  z  }n@|
j        r                    |
f           n!|                    |
t           j        f           t           j        u r                    |           Tj        r#                    fd|D                        ~g }|D ]P\  }}|                                j        r                    ||z  f           9|                    ||f           Q                    t)          |          f           # t*          $ r'}                    |j        f           Y d}~%d}~ww xY w|dk    rfdd D             D             |fS )z.Helper function for :func:`_symbolic_factor`. c                 Z    g | ](}t          |d           r|                                n|)S )_eval_factor)r  r  r   r   s     r`   r   z)_symbolic_factor_list.<locals>.<listcomp>  sF     & & & !(> : :AANN & & &rc   _listc                 $    g | ]\  }}||z  fS r   r   )r   r[   r\  r  s      r`   r   z)_symbolic_factor_list.<locals>.<listcomp>  s%    @@@tq!AcE
@@@rc   Nr9  c                 X    g | ]%t          t          fd D                       f&S )c              3   .   K   | ]\  }}|k    |V  d S r   r   )r   r[   r   r\  s      r`   r  z3_symbolic_factor_list.<locals>.<listcomp>.<genexpr>  s+       A Atq!!q&&&&&& A Arc   )r   r   )r   r\  r&  s    @r`   r   z)_symbolic_factor_list.<locals>.<listcomp>  sN     5 5 5 3 A A A Aw A A ABBAF 5 5 5rc   c                     h | ]\  }}|S r   r   )r   r   r   s      r`   	<setcomp>z(_symbolic_factor_list.<locals>.<setcomp>	  s    33341aQ333rc   )r   r  r   	make_argsrY  rR   r	   r   is_PowbaseExp1rx   rK  r>  rX   r  is_positiverM  
is_integerrY   r  r<   r   )r   ry   r  r   rx   argr  rN  r   r_   _coeff_factorsr  r[   r\  r  r  r&  s                   @@r`   _symbolic_factor_listr    s   UBNE7& &t$$& & &D ,? ,?= 	#ZT22 	#|C7H7H 	#SLEZ 		#CH..ID#~ #- ~ c{+++ QUID#	?%dC00GD! 4'!122D#tvvFHQU""> 5VS[(EE' 5NNFC=1111OOVQUO444ae||x(((( ?@@@@x@@@AAAA$ - -DAqyy{{. -1S5z2222aV,,,, 0 7 7=>>>>7 " 	, 	, 	,NNCHc?++++++++	,8 5 5 5 53373335 5 5 '>s   H++
I5IIc                    t          | t                    rjt          | d          r|                                 S t	          t          | d                             \  }}t          |t          |                    S t          | d          r | j        fd| j	        D              S t          | d          r"| 
                    fd| D                       S | S )z%Helper function for :func:`_factor`. r  fraction)r  rx   c                 2    g | ]}t          |          S r   _symbolic_factorr   r  r  ry   s     r`   r   z$_symbolic_factor.<locals>.<listcomp>  s&    SSS#+Cf==SSSrc   r  c                 2    g | ]}t          |          S r   r  r  s     r`   r   z$_symbolic_factor.<locals>.<listcomp>  s&    RRRc/S&AARRRrc   )rR   r	   r  r  r  rE   r   r  r_   rx   r   )r   ry   r  r   r&  s    ``  r`   r  r    s    $ 
4'' 	'$$&&&.xs:/W/W/WY\^deew5"27";";<<<	v		 tySSSSSSSSTT	z	"	" ~~RRRRRTRRRSSSrc   c                 .   t          j        |ddg           t          j        ||          }t          |           } t	          | t
          t          f          r,t	          | t                    r| d}}n$t          |                                           \  }}t          |||          \  }}t          |||          \  }	}
|
r|j
        st          d| z            |                    ddi          }||
fD ];}t          |          D ])\  }\  }}|j        st          ||          \  }}||f||<   *<t!          ||          }t!          |
|          }
|j        sd |D             }d |
D             }
||	z  }|j
        s||fS |||
fS t          d| z            )	z>Helper function for :func:`sqf_list` and :func:`factor_list`. fracr`  r|   za polynomial expected, got %srA  Tc                 @    g | ]\  }}|                                 |fS r   r  r  s      r`   r   z(_generic_factor_list.<locals>.<listcomp><  )    222tq!199;;"222rc   c                 @    g | ]\  }}|                                 |fS r   r  r  s      r`   r   z(_generic_factor_list.<locals>.<listcomp>=  r  rc   )rj   ra  rk   r"   rR   r	   rS   rE   r  r  r  r9   cloner   rs   r>  r  r`  )r   rU   rx   r  ry   numerr  r  fpr  fq_optr&  r   r[   r\  r   r   s                     r`   _generic_factor_listr    s   $ 1222

d
+
+C4==D$t%% "FdD!! 	;5EE#D>>88::LE5&uc6::B&uc6::B 	Jch 	J!"AD"HIIIyy(D)**Bx 	( 	(G&w// ( (	6Aqy (*1d33DAq"#QGAJ(
 R((R((y 	322r222B22r222B2x 	!"9"b= =DEEErc   c                     |                     dd          }t          j        |g            t          j        ||          }||d<   t	          t          |           ||          S )z4Helper function for :func:`sqf` and :func:`factor`. r  T)r  rj   ra  rk   r  r"   )r   rU   rx   r  r  ry   s         r`   _generic_factorr   I  s[    xx
D))H$###

d
+
+CC
OGDMM3777rc   c                 .   ddl m d	fd	}d	fd	}d }|                                 j        re ||           rZ|                                 } || |          }|r|d         |d         d|d         fS  || |          }|rdd|d         |d         fS dS )
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                 F   t          | j                  dk    r| j        d         j        sd| fS |                                 }|                                 }|p|                                }|                                dd         }fd|D             }t          |          dk    r|d         r |d         |d         z            }g }t          t          |                    D ]:} ||         ||dz   z  z            }|j        s n|	                    |           ; d|z            }	| j        d         }
|
|z  g}t          d|dz             D ])}|	                    ||dz
           |
||z
  z  z             *t          | } t          |           } ||	| fS dS )a$  
        try rescaling ``x -> alpha*x`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the rescaling is successful,
        ``alpha`` is the rescaling factor, and ``f`` is the rescaled
        polynomial; else ``alpha`` is ``None``.
        r|   r   Nc                 &    g | ]} |          S r   r   )r   coeffxr  s     r`   r   z<to_rational_coeffs.<locals>._try_rescale.<locals>.<listcomp>  s#    888v((6""888rc   r  )r~   rU   r  r  r  r  r:  r   r  rK  r
   rS   )r[   f1r-  r  r   
rescale1_xcoeffs1r   r  	rescale_xr  rB  r  s               r`   _try_rescalez(to_rational_coeffs.<locals>._try_rescalez  s    16{{aq	':7NHHJJTTVV288::$8888888v;;??vbz?!&*VBZ"788JG3v;;'' ( (!&)JQ,?"?@@) Ev&&&&$HQz\22	F1ITFq!a% 8 8AHHWQU^AAJ67777GGG9a''trc   c                    t          | j                  dk    r| j        d         j        sd| fS |                                 }|p|                                }|                                dd         } 	|d                   }|j        rI|j        sBt          |j	        d d          \  }} |j
        |  |z  }|                    |          }||fS dS )a+  
        try translating ``x -> x + alpha`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the translating is successful,
        ``alpha`` is the translating factor, and ``f`` is the shifted
        polynomial; else ``alpha`` is ``None``.
        r|   r   Nc                     | j         du S r&  r  )zs    r`   r  z<to_rational_coeffs.<locals>._try_translate.<locals>.<lambda>  s    !-4/ rc   Tbinary)r~   rU   r  r  r  r:  is_Addr  rL   rx   r_   r  )
r[   r  r-  r   r   ratnonratalphaf2r  s
            r`   _try_translatez*to_rational_coeffs.<locals>._try_translate  s     16{{aq	':7NHHJJ288::$HVAY8 	AM 	qv//> > >KCQVV_$Q&E%B"9trc   c                 ,   |                                  }d}|D ]z}t          j        |          D ]c}t          |          j        }d |                                D             }|s7t          |          dk    rd}t          |          dk    r  dS d{|S )zS
        Return True if ``f`` is a sum with square roots but no other root
        Fc                 T    g | ]%\  }}|j         r|j        r|j        d k    |j        &S )r  )r   is_Rationalr~  )r   r  wxs      r`   r   zAto_rational_coeffs.<locals>._has_square_roots.<locals>.<listcomp>  sI     B B BeaKB$&NB79tqyy T7@yyrc   r  T)r   r
   r  r   r&  r   minr  )r  r   has_sqr  r  r[   r  s          r`   _has_square_rootsz-to_rational_coeffs.<locals>._has_square_roots  s      
	! 
	!A]1%% 	! 	!AJJ&B Bqwwyy B B B q66Q;;!Fq66A:: 555 	! rc   r|   r  r   )sympy.simplify.simplifyr  r   r   r  )r[   r  r  r  r  r  r  s         @r`   to_rational_coeffsr   R  s   L 100000           D     ,  & 	||~~ . 1 1! 4 4 .WWYYLB 	.Q41tQqT))q"%%A .T1Q41--4rc   c           
         ddl m} t          | |d          }|                                }t	          |          }|sdS |\  }}}}	t          |	                                          }
|r ||
d         |z  ||z  z            } |d|z            }g }|
dd         d         D ]F}|                     ||d                             |||z  i                    |d         f           GnX|
d         }g }|
dd         d         D ]=}|                    |d                             |||z
  i          |d         f           >||fS )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  EXr'  Nr|   )	r  r  rS   r  r   r,  rY   rK  r   )r  r  r  p1r-  resr  r  r  r\   r&  r   r1r  r  s                  r`   _torational_factor_listr&    s{   , 100000	a4	 	 	 B
		A
R
 
 C tKB1a!))++&&G	 
4HWQZ]1a4'((Xac]]Q 	= 	=AHHhhqtyy!QrT3344ad;<<<<	= AJQ 	4 	4AHHadiiAE
++QqT23333q6Mrc   c                 (    t          | ||d          S )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)])

    r9  r  r  r  s      r`   r"  r"    s      4e<<<<rc   c                 (    t          | ||d          S )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

    r9  r(  )r   r  s      r`   r9  r9    s     1dD7777rc   c                 (    t          | ||d          S )a  
    Compute a list of irreducible factors of ``f``.

    Examples
    ========

    >>> from sympy import factor_list
    >>> from sympy.abc import x, y

    >>> factor_list(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y)
    (2, [(x + y, 1), (x**2 + 1, 2)])

    r  r(  r)  r  s      r`   r,  r,  !  s      4h????rc   )r  c                   t          |           } |r{fd}t          | |          } i }|                     t          t                    }|D ]+}t          |gR i }|j        s|j        r||k    r|||<   ,|                     |          S 	 t          | d          S # t          $ r/}	| j        st          |           cY d}	~	S t          |	          d}	~	ww xY w)a  
    Compute the factorization of expression, ``f``, into irreducibles. (To
    factor an integer into primes, use ``factorint``.)

    There two modes implemented: symbolic and formal. If ``f`` is not an
    instance of :class:`Poly` and generators are not specified, then the
    former mode is used. Otherwise, the formal mode is used.

    In symbolic mode, :func:`factor` will traverse the expression tree and
    factor its components without any prior expansion, unless an instance
    of :class:`~.Add` is encountered (in this case formal factorization is
    used). This way :func:`factor` can handle large or symbolic exponents.

    By default, the factorization is computed over the rationals. To factor
    over other domain, e.g. an algebraic or finite field, use appropriate
    options: ``extension``, ``modulus`` or ``domain``.

    Examples
    ========

    >>> from sympy import factor, sqrt, exp
    >>> from sympy.abc import x, y

    >>> factor(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y)
    2*(x + y)*(x**2 + 1)**2

    >>> factor(x**2 + 1)
    x**2 + 1
    >>> factor(x**2 + 1, modulus=2)
    (x + 1)**2
    >>> factor(x**2 + 1, gaussian=True)
    (x - I)*(x + I)

    >>> factor(x**2 - 2, extension=sqrt(2))
    (x - sqrt(2))*(x + sqrt(2))

    >>> factor((x**2 - 1)/(x**2 + 4*x + 4))
    (x - 1)*(x + 1)/(x + 2)**2
    >>> factor((x**2 + 4*x + 4)**10000000*(x**2 + 1))
    (x + 2)**20000000*(x**2 + 1)

    By default, factor deals with an expression as a whole:

    >>> eq = 2**(x**2 + 2*x + 1)
    >>> factor(eq)
    2**(x**2 + 2*x + 1)

    If the ``deep`` flag is True then subexpressions will
    be factored:

    >>> factor(eq, deep=True)
    2**((x + 1)**2)

    If the ``fraction`` flag is False then rational expressions
    will not be combined. By default it is True.

    >>> factor(5*x + 3*exp(2 - 7*x), deep=True)
    (5*x*exp(7*x) + 3*exp(2))*exp(-7*x)
    >>> factor(5*x + 3*exp(2 - 7*x), deep=True, fraction=False)
    5*x + 3*exp(2)*exp(-7*x)

    See Also
    ========
    sympy.ntheory.factor_.factorint

    c                 H    t          | gR i }|j        s|j        r|S | S )zS
            Factor, but avoid changing the expression when unable to.
            )r  is_Mulr  )r   ru  rx   rU   s     r`   _try_factorzfactor.<locals>._try_factory  s?     ------Cz SZ 
Krc   r  r(  N)r"   r%   atomsr   r
   r  r.  r  xreplacer   r9   r-  r   )
r[   r  rU   rx   r/  partialsmuladdr  ru  msgs
     ``      r`   r  r  3  s5   H 	

A $	 	 	 	 	 	 a%% c"" 	" 	"A*T***T**C
 "cj "cQhh!zz(###'q$X>>>> ' ' ' 	'Q<<!#&&&	's$   B# #
C-CCCCc           	         t          | d          s>	 t          |           } n# t          $ r g cY S w xY w|                     ||||||          S t	          | d          \  }}	t          |	j                  dk    rt          t          |          D ]\  }
}|j	        j	        ||
<   |/|	j
                            |          }|dk    rt          d          ||	j
                            |          }||	j
                            |          }t          ||	j
        |||||	          }g }|D ]U\  \  }}}|	j
                            |          |	j
                            |          }}|                    ||f|f           V|S )
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)]

    r  r4  r*   r'  r|   Nr   r2  )r5  r6  r7  r  r8  )r  rS   r8   r3  rG  r~   rU   r:   r   rf   r   r   r  rF   r   rK  )r   r%  r5  r6  r7  r  r8  r9  r`  ry   r   rN  r3  r^   r  r  r  s                    r`   r3  r3    s   " 1j!! $	QAA 	 	 	III	 {{s#Dc{RRR,Qt<<<
ssx==1-- '' 	$ 	$GAtx|E!HH?*$$S))Caxx !DEEE?*$$S))C?*$$S))C/sz#f4A A A	 ( 	- 	-OFQG:&&q))3:+>+>q+A+AqAMMAq67+,,,,s   " 11c                     	 t          |           }t          | t                     s|j        j        st	          d          n # t
          $ r t	          d| z            w xY w|                    ||||||          S )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)r5  rJ  r8  rL  )rS   rR   r   	is_Symbolr9   r8   rI  )r[   r  r  r5  rJ  r8  rL  r   s           r`   rI  rI    s    	@GG!T"" 	@15? 	@ "">??? @ @ @:Q>@ @ 	@@ ==A3e$)=TTTs   ?A Ac                     	 t          | d          }t          | t                     s|j        j        st	          d          n # t
          $ r t	          d| z            w xY w|                    ||          S )a  
    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greedyr7  z*Cannot count roots of %s, not a polynomialrQ  )rS   rR   r   r8  r9   r8   r[  )r[   r6  r7  r   s       r`   r[  r[    s    (P5!!!!T"" 	@15? 	@ "">??? P P PJQNOOOP ==Sc=***   AA A!Tc                     	 t          | d          }t          | t                     s|j        j        st	          d          n # t
          $ r t	          d| z            w xY w|                    |          S )z
    Return a list of real roots with multiplicities of ``f``.

    Examples
    ========

    >>> from sympy import real_roots
    >>> from sympy.abc import x

    >>> real_roots(2*x**3 - 7*x**2 + 4*x + 4)
    [-1/2, 2, 2]
    Fr:  r7  z1Cannot compute real roots of %s, not a polynomialre  )rS   rR   r   r8  r9   r8   rh  )r[   rf  r   s      r`   rh  rh    s    	E5!!!!T"" 	@15? 	@ "">??? E E E?!CE E 	EE <<<***r<  rm  rn  c                     	 t          | d          }t          | t                     s|j        j        st	          d          n # t
          $ r t	          d| z            w xY w|                    |||          S )aL  
    Compute numerical approximations of roots of ``f``.

    Examples
    ========

    >>> from sympy import nroots
    >>> from sympy.abc import x

    >>> nroots(x**2 - 3, n=15)
    [-1.73205080756888, 1.73205080756888]
    >>> nroots(x**2 - 3, n=30)
    [-1.73205080756887729352744634151, 1.73205080756887729352744634151]

    Fr:  r7  z6Cannot compute numerical roots of %s, not a polynomial)r-  r|  r}  )rS   rR   r   r8  r9   r8   r  )r[   r-  r|  r}  r   s        r`   r  r  (  s    "	J5!!!!T"" 	@15? 	@ "">??? J J JDqHJ J 	JJ 88a(G8<<<r<  c                 $   t          j        |g            	 t          | g|R i |\  }}t          | t                    s|j        j        st          d          n## t          $ r}t          dd|          d}~ww xY w|
                                S )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}

    r7  r  r|   N)rj   ra  r?  rR   rS   r   r8  r9   r<   r=   r  re  s         r`   r  r  G  s     $###81D111D113!T"" 	@15? 	@ "">??? 8 8 837778 >>   AA 
A;%A66A;c                 `   t          j        |g            	 t          | g|R i |\  }}t          | t                    s|j        j        st          d          n## t          $ r}t          dd|          d}~ww xY w|
                    |          }|j        s|                                S |S )a  
    Construct a polynomial with n-th powers of roots of ``f``.

    Examples
    ========

    >>> from sympy import nth_power_roots_poly, factor, roots
    >>> from sympy.abc import x

    >>> f = x**4 - x**2 + 1
    >>> g = factor(nth_power_roots_poly(f, 2))

    >>> g
    (x**2 - x + 1)**2

    >>> R_f = [ (r**2).expand() for r in roots(f) ]
    >>> R_g = roots(g).keys()

    >>> set(R_f) == set(R_g)
    True

    r7  r  r|   N)rj   ra  r?  rR   rS   r   r8  r9   r<   r=   r  r`  rY   )r[   r-  rU   rx   r   ry   r  r^   s           r`   r  r  e  s    0 $###@1D111D113!T"" 	@15? 	@ "">??? @ @ @ 63???@ ##A&&F9 ~~r@  )	_signsimpc                   ddl m} ddlm} t	          j        |dg           t          |           } |r ||           } i }d|v r|d         |d<   t          | t          t          f          s\| j
        s*t          | t                    st          | t                    s| S t          | d          } |                                 \  }}nt          |           dk    r| \  }}t          |t                     rBt          |t                     r-|j        |d<   |j        |d	<   |                    dd          |d<   |                                |                                }}n6t          | t                    rt          |           S t+          d
| z            ddlm 	 |                               rt3                       |||fg|R i |\  }	\  }
}|	j        s?t          | t          t          f          s|                                 S t8          j        ||fS nU# t2          $ rG}| j        r$|                               st3          |          | j        s| j         rQtC          | j"        fdd          \  }}d |D             } | j#        tI           | j#        |           g|R  cY d}~S g }tK          |           }tM          |           |D ]n}t          |t          t          tN          f          r%	 |(                    |tI          |          f           |)                                 _# tT          $ r Y kw xY w| +                    tY          |                    cY d}~S d}~ww xY wd|
$                    |          c}\  }}|                    dd          rd|vr
|	j-        |d<   t          | t          t          f          s,||                                |                                z  z  S |                                |                                }}|                    dd          s|||fS |t!          |g|R i |t!          |g|R i |fS )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  rU   r   zunexpected argument: %srI  c                 B    | j         du o|                                S r&  )r-  has)r  rJ  s    r`   r  zcancel.<locals>.<lambda>  s'     D(Ay1A1A-A rc   r  c                 ,    g | ]}t          |          S r   )r  r  s     r`   r   zcancel.<locals>.<listcomp>  s    (((&))(((rc   Nr|   F).r  rD  sympy.polys.ringsrE  rj   ra  r"   rR   rA  r   rY  r   r	   r   r  r~   rS   rU   r   r  rY   r  rL  rJ  rH  r9   ngensrA  r   r  r-  r  r.  rL   rx   r_   r  r$   r[  r&   rK  skiprl   r1  ro   r   )r[   rB  rU   rx   rD  rE  ry   r  r~  r  r   r   r4  r   ncrS  poter  r  rJ  s                       @r`   r  r    s   2 100000''''''$	***

A HQKK
C$G}Ga%(( 8; 	*Q
33 	:a;N;N 	HD)))!!11	Q11a 	2:a#6#6 	2&CKHCM777D11CLyy{{AIIKK1	Au		 8A2Q6777>>>>>> *55 	$!###E1a&04000400	6Aqw 	#a%00 #xxzz!ua{"		#
  * * * 	'AEE)$4$4 	'!#&&&8 	*qx 	* "B "B "B "B  EAr )(R(((B16&,,2r222222222D$Q''CIII  a%!<== KKF1II///HHJJJJ*   D::d4jj))))))))/*2 188A;;IAv1
www  6#4#4iFa%(( C!))++aiikk)**yy{{AIIKK1www&& 	Ca7Nd1+t+++s++T!-Bd-B-B-Bc-B-BBBsW   A-H H M/(BM*,M/2AM*:8L32M*3
M =M*?M  $M*$M/*M/c                    t          j        |ddg           	 t          | gt          |          z   g|R i |\  }n## t          $ r}t          dd|          d}~ww xY wj        }d}j        r9|j        r2|j	        s+
                    d|                                i          d}dd	lm}  |j        j        j                  \  }	}
t!          |          D ]N\  }}|                    j                  j                                        }|	                    |          ||<   O|d                             |d
d                   \  }}fd|D             }t,                              t1          |                    }|r6	 d |D             |                                }}||}}n# t4          $ r Y nw xY wj        s d |D             |                                fS ||fS )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   NFr   Txringr|   c                 `    g | ]*}t                               t          |                    +S r   rS   rp   ro   r   r~  ry   s     r`   r   zreduced.<locals>.<listcomp>#  s-    2221a#	&	&222rc   c                 6    g | ]}|                                 S r   r  r   r~  s     r`   r   zreduced.<locals>.<listcomp>(  s     ---aaiikk---rc   c                 6    g | ]}|                                 S r   r  rY  s     r`   r   zreduced.<locals>.<listcomp>/  s     '''		'''rc   )rj   ra  rG  rr   r<   r=   r   r  r  r  r  r  rJ  rS  rU   rh   r   r   rf   r   r   r  rS   rp   ro   r  r6   r`  rY   )r[   r   rU   rx   r`  r  r   r(  rS  _ringr   r   rN  r  r  _Q_rry   s                    @r`   rQ  rQ    s:   ( $& 12223,aS477]JTJJJTJJ
ss 3 3 3	1c2223 ZFG
x FN 6? ii6#3#3#5#5677''''''uSXsz3955HE1U## ) )4sz**.6688??4((a8<<abb	""DAq2222222AQ%%A 	--1---qyy{{B rqAA  	 	 	D	
 9 ''Q'''44!ts'   $? 
A	AA F2 2
F?>F?c                 "    t          | g|R i |S )a  
    Computes the reduced Groebner basis for a set of polynomials.

    Use the ``order`` argument to set the monomial ordering that will be
    used to compute the basis. Allowed orders are ``lex``, ``grlex`` and
    ``grevlex``. If no order is specified, it defaults to ``lex``.

    For more information on Groebner bases, see the references and the docstring
    of :func:`~.solve_poly_system`.

    Examples
    ========

    Example taken from [1].

    >>> from sympy import groebner
    >>> from sympy.abc import x, y

    >>> F = [x*y - 2*y, 2*y**2 - x**2]

    >>> groebner(F, x, y, order='lex')
    GroebnerBasis([x**2 - 2*y**2, x*y - 2*y, y**3 - 2*y], x, y,
                  domain='ZZ', order='lex')
    >>> groebner(F, x, y, order='grlex')
    GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y,
                  domain='ZZ', order='grlex')
    >>> groebner(F, x, y, order='grevlex')
    GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y,
                  domain='ZZ', order='grevlex')

    By default, an improved implementation of the Buchberger algorithm is
    used. Optionally, an implementation of the F5B algorithm can be used. The
    algorithm can be set using the ``method`` flag or with the
    :func:`sympy.polys.polyconfig.setup` function.

    >>> F = [x**2 - x - 1, (2*x - 1) * y - (x**10 - (1 - x)**10)]

    >>> groebner(F, x, y, method='buchberger')
    GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex')
    >>> groebner(F, x, y, method='f5b')
    GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex')

    References
    ==========

    1. [Buchberger01]_
    2. [Cox97]_

    )GroebnerBasisr   rU   rx   s      r`   r.   r.   4  s#    f *T***T***rc   c                 ,    t          | g|R i |j        S )a[  
    Checks if the ideal generated by a Groebner basis is zero-dimensional.

    The algorithm checks if the set of monomials not divisible by the
    leading monomial of any element of ``F`` is bounded.

    References
    ==========

    David A. Cox, John B. Little, Donal O'Shea. Ideals, Varieties and
    Algorithms, 3rd edition, p. 230

    )r_  is_zero_dimensionalr`  s      r`   rb  rb  j  s%     *T***T**>>rc   c                       e Zd ZdZd Zed             Zed             Zed             Z	ed             Z
ed             Zed             Zed	             Zd
 Zd Zd Zd Zd Zd Zed             Zd ZddZd ZdS )r_  z%Represents a reduced Groebner basis. c                   	 t          j        |ddg           	 t          |g|R i |\  }n0# t          $ r#}t	          dt          |          |          d}~ww xY wddlm}  |j        j	        j
                  		fd|D             }t          |	j                  }fd	|D             }|                     |          S )
z>Compute a reduced Groebner basis for a system of polynomials. r`  r  r.   Nr   )PolyRingc                 l    g | ]0}|                     |j                                                  1S r   )r   rf   r   )r   rN  rings     r`   r   z)GroebnerBasis.__new__.<locals>.<listcomp>  s8    NNNN 0 0 2 233NNNrc   r(  c                 F    g | ]}t                               |          S r   )rS   rp   r   r\   ry   s     r`   r   z)GroebnerBasis.__new__.<locals>.<listcomp>  s'    000T__Q$$000rc   )rj   ra  rG  r<   r=   r~   rJ  re  rU   r   rh   	_groebnerr  _new)
rw   r   rU   rx   r`  r  re  r   ry   rg  s
           @@r`   rz   zGroebnerBasis.__new__  s   dWh$7888	=0BTBBBTBBJE33! 	= 	= 	=#JA<<<	= 	/.....x#*ci88NNNNNNNeT#*5550000a000xx3s   / 
AAAc                 d    t          j        |           }t          |          |_        ||_        |S r   )r   rz   rA  _basis_options)rw   basisrj   r   s       r`   rk  zGroebnerBasis._new  s*    mC  5\\

rc   c                 \    d | j         D             }t          | t          | j        j         fS )Nc              3   >   K   | ]}|                                 V  d S r   r  )r   r  s     r`   r  z%GroebnerBasis.args.<locals>.<genexpr>  s*      22222222rc   )rm  r   rn  rU   )r   ro  s     r`   rx   zGroebnerBasis.args  s0    22dk222uudm&89::rc   c                 $    d | j         D             S )Nc                 6    g | ]}|                                 S r   r  )r   rN  s     r`   r   z'GroebnerBasis.exprs.<locals>.<listcomp>  s     7774777rc   )rm  r   s    r`   rF  zGroebnerBasis.exprs  s    774;7777rc   c                 *    t          | j                  S r   )rr   rm  r   s    r`   r`  zGroebnerBasis.polys  s    DK   rc   c                     | j         j        S r   )rn  rU   r   s    r`   rU   zGroebnerBasis.gens  s    }!!rc   c                     | j         j        S r   )rn  r   r   s    r`   r   zGroebnerBasis.domain  s    }##rc   c                     | j         j        S r   )rn  rh   r   s    r`   rh   zGroebnerBasis.order  s    }""rc   c                 *    t          | j                  S r   )r~   rm  r   s    r`   __len__zGroebnerBasis.__len__  s    4;rc   c                 j    | j         j        rt          | j                  S t          | j                  S r   )rn  r`  iterrF  r   s    r`   r  zGroebnerBasis.__iter__  s/    = 	$
###
###rc   c                 H    | j         j        r| j        }n| j        }||         S r   )rn  r`  rF  )r   itemro  s      r`   __getitem__zGroebnerBasis.__getitem__  s)    = 	JEEJET{rc   c                 v    t          | j        t          | j                                                  f          S r   )hashrm  rA  rn  r   r   s    r`   r   zGroebnerBasis.__hash__  s-    T[%(;(;(=(=">">?@@@rc   c                     t          || j                  r | j        |j        k    o| j        |j        k    S t	          |          r0| j        t          |          k    p| j        t          |          k    S dS )NF)rR   r   rm  rn  rK   r`  rr   rF  r   r  s     r`   r  zGroebnerBasis.__eq__  sk    eT^,, 	;%,.R4=EN3RRe__ 	:e,I
d5kk0II5rc   c                     | |k     S r   r   r  s     r`   r  zGroebnerBasis.__ne__  s    5=  rc   c                     d }t          dgt          | j                  z            }| j        j        }| j        D ](}|                    |          } ||          r||z  })t          |          S )a{  
        Checks if the ideal generated by a Groebner basis is zero-dimensional.

        The algorithm checks if the set of monomials not divisible by the
        leading monomial of any element of ``F`` is bounded.

        References
        ==========

        David A. Cox, John B. Little, Donal O'Shea. Ideals, Varieties and
        Algorithms, 3rd edition, p. 230

        c                 N    t          t          t          |                     dk    S r   )sumr   rN  )monomials    r`   
single_varz5GroebnerBasis.is_zero_dimensional.<locals>.single_var  s    s4**++q00rc   r   r2  )r/   r~   rU   rn  rh   r`  r  r%  )r   r  r  rh   rN  r  s         r`   rb  z!GroebnerBasis.is_zero_dimensional  s    	1 	1 	1 aSTY/00	#J 	& 	&DwwUw++Hz(## &X%	
 9~~rc   c                    | j         j        }t          |          }||k    r| S | j        st	          d          t          | j                  }j        }                    |	                                |d          ddl
m}  |j        j        |          \  }}t          |          D ]N\  }	}
|
                    j                  j                                        }
|                    |
          ||	<   Ot%          |||          }fd|D             }|j        sd |D             }|_        |                     |          S )a  
        Convert a Groebner basis from one ordering to another.

        The FGLM algorithm converts reduced Groebner bases of zero-dimensional
        ideals from one ordering to another. This method is often used when it
        is infeasible to compute a Groebner basis with respect to a particular
        ordering directly.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> from sympy import groebner

        >>> F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1]
        >>> G = groebner(F, x, y, order='grlex')

        >>> list(G.fglm('lex'))
        [2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7]
        >>> list(groebner(F, x, y, order='lex'))
        [2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7]

        References
        ==========

        .. [1] J.C. Faugere, P. Gianni, D. Lazard, T. Mora (1994). Efficient
               Computation of Zero-dimensional Groebner Bases by Change of
               Ordering

        z?Cannot convert Groebner bases of ideals with positive dimension)r   rh   r   rR  c                 `    g | ]*}t                               t          |                    +S r   rU  ri  s     r`   r   z&GroebnerBasis.fglm.<locals>.<listcomp>.  -    666qT__T!WWc**666rc   c                 F    g | ]}|                     d           d         S )Tr  r|   )r  )r   r\   s     r`   r   z&GroebnerBasis.fglm.<locals>.<listcomp>1  s+    <<<Q--a0<<<rc   )rn  rh   r0   rb  rl   rr   rm  r   r  r  rJ  rS  rU   r   r   rf   r   r   r-   r  rk  )r   rh   	src_order	dst_orderr`  r   rS  r[  r   r   rN  r   ry   s               @r`   fglmzGroebnerBasis.fglm  su   > mI	 ''		!!K' 	i%&ghhhT[!!ii&&((
 
  
 	,+++++53:y99q '' 	- 	-GAt??3:..2::<<Dt,,E!HHui006666A666 	 <<!<<<ACJyyC   rc   Tc                    t                               || j                  }|gt          | j                  z   }| j        j        }d}|r9|j        r2|j        s+                    d|	                                i          d}ddl
m}  |j        j        j                  \  }}	t          |          D ]N\  }
}|                    j                  j                                        }|                    |          ||
<   O|d                             |dd                   \  }}fd|D             }t                               t+          |                    }|r6	 d	 |D             |                                }}||}}n# t.          $ r Y nw xY wj        s d
 |D             |                                fS ||fS )a#  
        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
        >>> 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

        Fr   Tr   rR  r|   Nc                 `    g | ]*}t                               t          |                    +S r   rU  rV  s     r`   r   z(GroebnerBasis.reduce.<locals>.<listcomp>g  r  rc   c                 6    g | ]}|                                 S r   rX  rY  s     r`   r   z(GroebnerBasis.reduce.<locals>.<listcomp>l  s     111!!))++111rc   c                 6    g | ]}|                                 S r   r  rY  s     r`   r   z(GroebnerBasis.reduce.<locals>.<listcomp>s  s     +++AAIIKK+++rc   )rS   ru   rn  rr   rm  r   r  r  r  r  rJ  rS  rU   rh   r   r   rf   r   r   r  rp   ro   r  r6   r`  rY   )r   r   r  rN  r`  r   r(  rS  r[  r   r   r  r  r\  r]  ry   s                  @r`   r   zGroebnerBasis.reduce6  s   8 tT]33dk***m 	FN 	6? 	))Xv'7'7'9'9:;;CG++++++53:sy99q '' 	- 	-GAt??3:..2::<<Dt,,E!HHQx||E!""I&&16666A666OODGGS)) 	11q111199;;B 21 "   
 y 	+++++QYY[[88a4Ks   ' F 
FFc                 @    |                      |          d         dk    S )am  
        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   rN  s     r`   containszGroebnerBasis.containsw  s     & {{4  #q((rc   Nr$  )rZ   r)  r*  r+  rz   r/  rk  r0  rx   rF  r`  rU   r   rh   ry  r  r~  r   r  r  rb  r  r   r  r   rc   r`   r_  r_  |  s       //     &   [ ; ; X; 8 8 X8 ! ! X! " " X" $ $ X$ # # X#     $ $ $  A A A  ! ! !   X>@! @! @!D? ? ? ?B) ) ) ) )rc   r_  c                     t          j        g            fdt          |           } | j        rt	          | g|R i S dvrdd<   t          j        |          } | |          S )z
    Efficiently transform an expression into a polynomial.

    Examples
    ========

    >>> from sympy import poly
    >>> from sympy.abc import x

    >>> poly(x*(x**2 + x - 1)**2)
    Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ')

    c                    g g }}t          j        |           D ]n}g g }}t          j        |          D ]}|j        r |                     
||                     )|j        r`|j        j        rT|j        j        rH|j        dk    r=|                     
|j        |          	                    |j                             |                    |           |s|                    |           |d         }|dd          D ]}|
                    |          }|rTt          | }|j        r|
                    |          }n.|
                    t                              ||                    }|                    |           p|st                              | |          }	n|d         }	|dd          D ]}|	                    |          }	|rTt          | }|j        r|	                    |          }	n.|	                    t                              ||                    }	 |	j        |                    dd          i S )Nr   r|   rU   r   )r
   r  r   r  rK  r  r  r  r  rz  r   rY  rS   ru   r  r   r  )r   ry   r  
poly_termsr  r&  poly_factorsr  productr^   _polyrx   s             r`   r  zpoly.<locals>._poly  sx   zM$'' 	+ 	+D$&\G--- + += + ''fc(:(:;;;;] +v{'9 +
-+28*// ''fk3//33FJ??A A A A NN6**** +T""""&q/*122. 2 2F%kk&11GG L ']F' L")++f"5"5")++doofc.J.J"K"K!!'**** 	D__T3//FF]F"122 * *D)) DE{> D#ZZ--FF#ZZc(B(BCCFv~swwvr22;d;;;rc   rA  F)rj   ra  r"   rs   rS   rk   )r   rU   rx   ry   r  s     ` @r`   rN  rN    s     $###2< 2< 2< 2< 2< 2<h 4==D| )D(4(((4(((tX

d
+
+C5src   c                    | dk     rt          d|d|           |d         |dd         }}|t          |d          \  }}t           |t          |           g||R  |          }|)t                              |t          d                    }nt                              ||          }|r|n|                                S )	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(   r1   r+  r5  r   r    rS   rY   )	r-  r[   Kr  r  r`  headtailrN  s	            r`   
named_polyr    s    0 	1uujTTT11EFFF1qu$Dy"4t4444qqQ"$""""A&&D|||D%**--xxd##,44dllnn,rc   r%  r   )FNNNFFFr'  r#  r$  r(  )r+  	functoolsr   r   operatorr   typingr   
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.numbersr   r   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'   rj   sympy.polys.constructorr(   sympy.polys.domainsr)   r*   r+   !sympy.polys.domains.domainelementr,   sympy.polys.fglmtoolsr-   sympy.polys.groebnertoolsr.   rj  sympy.polys.monomialsr/   sympy.polys.orderingsr0   sympy.polys.polyclassesr1   r2   r3   sympy.polys.polyerrorsr4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   sympy.polys.polyutilsr?   r@   rA   rB   rC   rD   sympy.polys.rationaltoolsrE   sympy.polys.rootisolationrF   sympy.utilitiesrG   rH   rI   sympy.utilities.exceptionsrJ   sympy.utilities.iterablesrK   rL   r_  rx  mpmath.libmp.libhyperrM   rb   rS   r5  r?  r>  rG  rE  rW  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  r  r  r	  r  r  r  r  r  r  r   r  r  r  r  r  r   r   r&  r"  r9  r,  r  r3  rI  r[  rh  r  r  r  r  rQ  rb  r_  rN  r  r   rc   r`   <module>r     s   > > $ # # # # # # #                        # " " " " " , , , , , , A A A A A A A A A AM M M M M M M M M M M M M M * * * * * * + + + + + + + + = = = = = = = = = = = = 6 6 6 6 6 6 6 6 & & & & & & + + + + + + + + 0 0 0 0 0 0 0 0 > > > > > > > > + + + + + + . . . . . . 4 4 4 4 4 4 * * * * * * * * * * ; ; ; ; ; ; - - - - - - ; ; ; ; ; ; * * * * * * . . . . . . 1 1 1 1 1 1 1 1 1 1	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	                / . . . . . A A A A A A 5 5 5 5 5 5 5 5 5 5 @ @ @ @ @ @ 4 4 4 4 4 4 4 4      / / / / / /  D ^B` ^B` ^B` ^B` ^B`5 ^B` ^B` ^B`BE Z Z Z Z Zt Z Z Zz & & &% % %P 0 0 0[ [ [|   7N 7N 7N 7Nt 1 1 1h ( ( (4 ! ! !0   2 ! ! !2   :   :   D   D   >   >   >   D # # #L # # #L 1 1 1h   : &+ ! ! ! ! !H   : % % %P Q Q Qh 0 0 0 0f J J JZ / / / /d r3 r3 r3j   :   :   0 * * *Z   :   :   : + + +\ < < <
      B   :$ $ $ 6 6 6
7 7 7t  )F )F )FX8 8 8| | |~) ) )X = = =" 8 8 8" @ @ @"  _' _' _' _' _'D 4 4 4 4n U U U U8 + + + +@ + + + +6 = = = =<   : ( ( (V # cC cC cC cC cCL 8 8 8v 2+ 2+ 2+j ? ? ?" M) M) M) M) M)E M) M) M)` N N Nb"- "- "- "- "-rc   