U
    9%e                    @   s  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%d& ZqefG d'd( d(eZrefG d)d* d*erZsefd+d, Ztd-d. Zuefd/d0 Zvd1d2 Zwd3d4 Zxefdd5d6Zyefd7d8 Zzefd9d: Z{efd;d< Z|efd=d> Z}efd?d@ Z~efdAdB ZefdCdD ZefdEdF ZefdGdH ZefdIdJ ZefdKdL ZefdMdN ZefdOdP ZefdQdR ZefdSdT ZefdUdV ZefdWdX ZefdYdZd[d\Zefd]d^ Zefd_d` Zefdadb ZefddcddZefdedf ZefddgdhZefdidj Zefdkdl Zefdmdn Zefdodp Zefdqdr Zefdsdt Zefdudv Zefdwdx Zefdydz Zefd{d| Zefd}d~ Zefdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zefdd Zefdd Zefdd ZefdYdddZefdddZefdddZefdddZefdddZefdddZefdd Zefdd ZefddddZefdd Zefdd ZCefdd ZefG dd deZefdd Zdd 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                    s   t   fdd}|S )Nc                    s   t |}t|tr | |S t|trz| j|f| j }W nT tk
r   |jrZt Y S t	| 
  j}||}|tk	rtdddd | Y S X  | |S ntS d 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)Zdeprecated_since_versionZactive_deprecations_target)r"   
isinstancePolyr   	from_exprgensr8   Z	is_MatrixNotImplementedgetattras_expr__name__rI   )fgZexpr_methodresultfunc T/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/polys/polytools.pywrapperD   s(    




z_polifyit.<locals>.wrapper)r   )rY   r\   rZ   rX   r[   	_polifyitC   s    r]   c                       s^  e Zd ZdZdZdZdZdZdd Ze	dd Z
ed	d
 Zedd Zdd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd  Z fd!d"Zed#d$ Zed%d& Zed'd( Zed)d* Zed+d, Zed-d. Zed/d0 Zd1d2 Z d3d4 Z!dyd6d7Z"d8d9 Z#d:d; Z$d<d= Z%d>d? Z&d@dA Z'dBdC Z(dzdDdEZ)dFdG Z*dHdI Z+dJdK Z,dLdM Z-dNdO Z.dPdQ Z/dRdS Z0d{dTdUZ1d|dVdWZ2d}dXdYZ3d~dZd[Z4dd\d]Z5d^d_ Z6d`da Z7dbdc Z8ddde Z9dfdg Z:ddidjZ;ddkdlZ<dmdn Z=dodp Z>dqdr Z?dsdt Z@ddudvZAdwdx ZBdydz ZCd{d| ZDd}d~ ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdddZTdddZUdddZVdddZWdd ZXdddZYdd ZZdd Z[dd Z\dd Z]dddZ^dd Z_dddZ`dd Zadd ZbdddZcdddZddddZedddZfdddĄZgddƄ ZhddȄ ZidddʄZjdd̄ Zkdd΄ ZlddЄ ZmemZnddd҄ZoddԄ ZpdddքZqddd؄ZrdddڄZsdd܄ Ztddބ ZudddZvdd ZwdddZxdddZydd Zzdd Z{dd Z|dd Z}dddZ~dd Zdd Zdd Zdd Zdd Zdd ZdddZdd  Zdd Zdd Zdd ZdddZdd	d
Zdd Zdd ZdddZdddZdddZdddZdddZdddZdddZdd  Zd!d" Zd#d$ Zdd%d&Zd'd( Zdd*d+Zed,d- Zed.d/ Zed0d1 Zed2d3 Zed4d5 Zed6d7 Zed8d9 Zed:d; Zed<d= Zed>d? Zed@dA ZedBdC ZedDdE ZedFdG ZdHdI ZdJdK ZedLdM ZedNdO ZedPdQ ZedRdS ZedTdU ZedVdW ZedXedYdZ Zed[d\ Zed]d^ Zed_d` Zedadb Zedcdd Zededf Zedgedhdi Zedgedjdk Zedledmdn Zedgedodp Zdqdr ZddsdtZddudvZdwdx Z  ZS (  rN   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

    reprP   Tgn $@c                 O   s   t ||}d|krtdt|ttttfr:| ||S t	|t
drnt|tr\| ||S | t||S n&t|}|jr| ||S | ||S dS )z:Create a new polynomial instance out of something useful. orderz&'order' keyword is not implemented yet)excludeN)optionsbuild_optionsNotImplementedErrorrM   r0   r1   r2   r+   _from_domain_elementrJ   strdict
_from_dict
_from_listlistr!   is_Poly
_from_poly
_from_exprclsr_   rP   argsoptrZ   rZ   r[   __new__   s    
zPoly.__new__c                 G   sT   t |tstd| n"|jt|d kr:td||f t| }||_||_|S )z:Construct :class:`Poly` instance from raw representation. z%invalid polynomial representation: %s   zinvalid arguments: %s, %s)	rM   r0   r8   levlenr   rr   r_   rP   )ro   r_   rP   objrZ   rZ   r[   new   s    

zPoly.newc                 C   s   t | j f| j S N)r>   r_   to_sympy_dictrP   selfrZ   rZ   r[   expr   s    z	Poly.exprc                 C   s   | j f| j S rx   )r|   rP   rz   rZ   rZ   r[   rp      s    z	Poly.argsc                 C   s   | j f| j S rx   r^   rz   rZ   rZ   r[   _hashable_content   s    zPoly._hashable_contentc                 O   s   t ||}| ||S )(Construct a polynomial from a ``dict``. )rb   rc   rh   rn   rZ   rZ   r[   	from_dict   s    zPoly.from_dictc                 O   s   t ||}| ||S )(Construct a polynomial from a ``list``. )rb   rc   ri   rn   rZ   rZ   r[   	from_list   s    zPoly.from_listc                 O   s   t ||}| ||S )*Construct a polynomial from a polynomial. )rb   rc   rl   rn   rZ   rZ   r[   	from_poly   s    zPoly.from_polyc                 O   s   t ||}| ||S +Construct a polynomial from an expression. )rb   rc   rm   rn   rZ   rZ   r[   rO      s    zPoly.from_exprc                 C   sx   |j }|stdt|d }|j}|dkr>t||d\}}n | D ]\}}||||< qF| jt	|||f| S )r~   z0Cannot initialize from 'dict' without generatorsrs   Nrq   )
rP   r7   ru   domainr'   itemsconvertrw   r0   r   )ro   r_   rq   rP   levelr   monomcoeffrZ   rZ   r[   rh      s    zPoly._from_dictc                 C   s~   |j }|stdnt|dkr(tdt|d }|j}|dkrTt||d\}}ntt|j|}| j	t
|||f| S )r   z0Cannot initialize from 'list' without generatorsrs   z#'list' representation not supportedNr   )rP   r7   ru   r9   r   r'   rj   mapr   rw   r0   r   )ro   r_   rq   rP   r   r   rZ   rZ   r[   ri     s    zPoly._from_listc                 C   s   | |j kr| j|jf|j }|j}|j}|j}|rj|j|krjt|jt|kr`| | |S |j	| }d|kr|r|
|}n|dkr| }|S )r   r   T)	__class__rw   r_   rP   fieldr   setrm   rS   reorder
set_domainto_field)ro   r_   rq   rP   r   r   rZ   rZ   r[   rl     s    

zPoly._from_polyc                 C   s   t ||\}}| ||S r   )rB   rh   )ro   r_   rq   rZ   rZ   r[   rm   4  s    zPoly._from_exprc                 C   s>   |j }|j}t|d }||g}| jt|||f| S Nrs   )rP   r   ru   r   rw   r0   r   )ro   r_   rq   rP   r   r   rZ   rZ   r[   re   :  s
    zPoly._from_domain_elementc                    s
   t   S rx   super__hash__rz   r   rZ   r[   r   D  s    zPoly.__hash__c                 C   sP   t  }| j}tt|D ],}|  D ]}|| r$||| jO } qq$q|| jB 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   rP   rangeru   monomsfree_symbolsfree_symbols_in_domain)r{   symbolsrP   ir   rZ   rZ   r[   r   G  s    zPoly.free_symbolsc                 C   sP   | j jt  }}|jr.|jD ]}||jO }qn|jrL|  D ]}||jO }q<|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}

        )r_   domr   is_Compositer   r   is_EXcoeffs)r{   r   r   genr   rZ   rZ   r[   r   f  s    
zPoly.free_symbols_in_domainc                 C   s
   | 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   rP   rz   rZ   rZ   r[   r     s    zPoly.genc                 C   s   |   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_domainrz   rZ   rZ   r[   r     s    zPoly.domainc                 C   s$   | j | j| jj| jjf| j S )z3Return zero polynomial with ``self``'s properties. )rw   r_   zerort   r   rP   rz   rZ   rZ   r[   r     s    z	Poly.zeroc                 C   s$   | j | j| jj| jjf| j S )z2Return one polynomial with ``self``'s properties. )rw   r_   onert   r   rP   rz   rZ   rZ   r[   r     s    zPoly.onec                 C   s$   | j | j| jj| jjf| j S )z3Return unit polynomial with ``self``'s properties. )rw   r_   unitrt   r   rP   rz   rZ   rZ   r[   r     s    z	Poly.unitc                 C   s"   |  |\}}}}||||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)rU   rV   _perFGrZ   rZ   r[   unify  s    z
Poly.unifyc                    s  t js\z(jjjjjjjfW S  tk
rZ   tdf Y nX tjt	rtjt	rt
jj}jjjj|t|d  }j|krtj j|\}}jjkrfdd|D }t	ttt|||}nj}j|krvtj j|\}}jjkrZfdd|D }t	ttt|||}	nj}	ntdf j |d f fdd	}
|
||	fS )NCannot unify %s with %srs   c                    s   g | ]}  |jjqS rZ   r   r_   r   .0c)r   rU   rZ   r[   
<listcomp>  s     zPoly._unify.<locals>.<listcomp>c                    s   g | ]}  |jjqS rZ   r   r   )r   rV   rZ   r[   r     s     c                    sB   |d k	r2|d | ||d d   }|s2| | S  j| f| S r   to_sympyrw   r_   r   rP   removero   rZ   r[   r     s
    
zPoly._unify.<locals>.per)r!   rk   r_   r   r   
from_sympyr5   r6   rM   r0   r@   rP   r   ru   rA   to_dictrg   rj   zipr   r   )rU   rV   rP   rt   Zf_monomsZf_coeffsr   Zg_monomsZg_coeffsr   r   rZ   )ro   r   rU   rV   r[   r     sB    ("    	zPoly._unifyNc                 C   sV   |dkr| j }|dk	rD|d| ||d d  }|sD| jj|S | jj|f| 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')

        Nrs   )rP   r_   r   r   r   rw   )rU   r_   rP   r   rZ   rZ   r[   r   
  s    zPoly.perc                 C   s&   t | jd|i}| | j|jS )z Set the ground domain of ``f``. r   )rb   rc   rP   r   r_   r   r   )rU   r   rq   rZ   rZ   r[   r   '  s    zPoly.set_domainc                 C   s   | j jS )z Get the ground domain of ``f``. )r_   r   rU   rZ   rZ   r[   r   ,  s    zPoly.get_domainc                 C   s   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)

        )rb   ZModulus
preprocessr   r(   )rU   modulusrZ   rZ   r[   set_modulus0  s    zPoly.set_modulusc                 C   s&   |   }|jrt| S tddS )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 fieldN)r   Zis_FiniteFieldr   Zcharacteristicr8   )rU   r   rZ   rZ   r[   get_modulusA  s    zPoly.get_modulusc                 C   sP   || j kr@|jr| ||S z| ||W S  tk
r>   Y nX |  ||S )z)Internal implementation of :func:`subs`. )rP   	is_numberevalreplacer8   rS   subs)rU   oldrw   rZ   rZ   r[   
_eval_subsV  s    
zPoly._eval_subsc                    s4   | j  \ } fddt| jD }| 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                    s   g | ]\}}| kr|qS rZ   rZ   )r   jr   JrZ   r[   r   r  s      z Poly.exclude.<locals>.<listcomp>r   )r_   ra   	enumeraterP   r   )rU   rw   rP   rZ   r   r[   ra   c  s    zPoly.excludec                 K   s   |dkr$| j r| j| }}ntd||ks6|| jkr:| S || jkr|| jkr|  }|jrf||jkrt| j}||||< | j	| j
|dS td||| f dS )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 %s with %s in %s)is_univariater   r8   rP   r   r   r   rj   indexr   r_   )rU   xy_ignorer   rP   rZ   rZ   r[   r   v  s    
zPoly.replacec                 O   s   |   j||S )z-Match expression from Poly. See Basic.match())rS   match)rU   rp   kwargsrZ   rZ   r[   r     s    z
Poly.matchc                 O   s|   t d|}|s t| j|d}nt| jt|kr:tdtttt	| j
 | j| }| jt|| j
jt|d |d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')

        rZ   r   z7generators list can differ only up to order of elementsrs   r   )rb   Optionsr?   rP   r   r8   rg   rj   r   rA   r_   r   r   r0   r   ru   )rU   rP   rp   rq   r_   rZ   rZ   r[   r     s     zPoly.reorderc                 C   s   | j dd}| |}i }| D ]4\}}t|d| rFtd|  ||||d < q"| j|d }| jt|t	|d | j
jf| 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 %srs   )as_dict_gen_to_levelr   anyr8   rP   rw   r0   r   ru   r_   r   )rU   r   r_   r   termsr   r   rP   rZ   rZ   r[   ltrim  s    
z
Poly.ltrimc              	   G   s   t  }|D ]D}z| j|}W n$ tk
rB   td| |f Y q
X || q
|  D ]*}t|D ]\}}||krd|rd  dS qdqXd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

        %s doesn't have %s as generatorFT)r   rP   r   
ValueErrorr=   addr   r   )rU   rP   indicesr   r   r   r   eltrZ   rZ   r[   has_only_gens  s    

zPoly.has_only_gensc                 C   s,   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)hasattrr_   r   r3   r   rU   rW   rZ   rZ   r[   r     s    
zPoly.to_ringc                 C   s,   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   r_   r   r3   r   r   rZ   rZ   r[   r     s    
zPoly.to_fieldc                 C   s,   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   r_   r   r3   r   r   rZ   rZ   r[   r   '  s    
zPoly.to_exactc                 C   s4   t | jdd|| jjpdd\}}| j|| j|d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   Z	compositer   )r'   r   r   r   r   rP   )rU   r   r   r_   rZ   rZ   r[   retract<  s
     

zPoly.retractc                 C   sh   |dkrd||  }}}n
|  |}t|t| }}t| jdrT| j|||}n
t| d| |S )z1Take a continuous subsequence of terms of ``f``. Nr   slice)r   intr   r_   r   r3   r   )rU   r   mnr   rW   rZ   rZ   r[   r   T  s    

z
Poly.slicec                    s    fdd j j|d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                    s   g | ]} j j|qS rZ   r_   r   r   r   r   rZ   r[   r   x  s     zPoly.coeffs.<locals>.<listcomp>r`   )r_   r   rU   r`   rZ   r   r[   r   d  s    zPoly.coeffsc                 C   s   | j j|dS )aU  
        Returns all non-zero monomials from ``f`` in lex order.

        Examples
        ========

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

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

        See Also
        ========
        all_monoms

        r   )r_   r   r   rZ   rZ   r[   r   z  s    zPoly.monomsc                    s    fdd j j|d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                    s"   g | ]\}}| j j|fqS rZ   r   r   r   r   r   rZ   r[   r     s     zPoly.terms.<locals>.<listcomp>r   )r_   r   r   rZ   r   r[   r     s    z
Poly.termsc                    s    fd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                    s   g | ]} j j|qS rZ   r   r   r   rZ   r[   r     s     z#Poly.all_coeffs.<locals>.<listcomp>)r_   
all_coeffsr   rZ   r   r[   r     s    zPoly.all_coeffsc                 C   s
   | 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

        )r_   
all_monomsr   rZ   rZ   r[   r     s    zPoly.all_monomsc                    s    fd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                    s"   g | ]\}}| j j|fqS rZ   r   r   r   rZ   r[   r     s     z"Poly.all_terms.<locals>.<listcomp>)r_   	all_termsr   rZ   r   r[   r     s    zPoly.all_termsc                 O   sr   i }|   D ]L\}}|||}t|tr2|\}}n|}|r||krL|||< qtd| q| j|f|pj| j|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   rM   tupler8   r   rP   )rU   rY   rP   rp   r   r   r   rW   rZ   rZ   r[   termwise  s    



zPoly.termwisec                 C   s   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

        )ru   r   r   rZ   rZ   r[   length  s    zPoly.lengthFc                 C   s$   |r| j j|dS | j j|dS d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   N)r_   r   ry   )rU   r   r   rZ   rZ   r[   r     s    zPoly.as_dictc                 C   s   |r| j  S | j  S dS )z%Switch to a ``list`` representation. N)r_   Zto_listZto_sympy_list)rU   r   rZ   rZ   r[   as_list  s    
zPoly.as_listc              	   G   s   |s
| j S t|dkrt|d tr|d }t| j}| D ]D\}}z||}W n$ tk
rx   t	d| |f Y q>X |||< q>t
| j f| 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

        rs   r   r   )r|   ru   rM   rg   rj   rP   r   r   r   r=   r>   r_   ry   )rU   rP   mappingr   valuer   rZ   rZ   r[   rS   %  s    



zPoly.as_exprc                 O   sB   z&t | f||}|jsW dS |W S W n tk
r<   Y dS X dS )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)rN   rk   r8   )r{   rP   rp   polyrZ   rZ   r[   as_polyK  s    
zPoly.as_polyc                 C   s,   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   r_   r   r3   r   r   rZ   rZ   r[   r   e  s    
z	Poly.liftc                 C   s4   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   r_   r   r3   r   rU   r   rW   rZ   rZ   r[   r   z  s    
zPoly.deflatec                 C   sx   | j j}|jr| S |js$td| t| j dr@| j j|d}n
t| d|r\|j| j	 }n| j	|j }| j
|f| 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)r_   r   is_Numericalrk   r4   r   r   r3   r   rP   rw   )rU   r   r   rW   rP   rZ   rZ   r[   r     s    
zPoly.injectc                 G   s   | j j}|jstd| t|}| jd| |krJ| j|d d }}n4| j| d |krv| jd|  d }}ntd|j| }t| j dr| j j	||d}n
t
| d| j|f| 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ejectr   )r_   r   r   r4   ru   rP   rd   r   r   r  r3   rw   )rU   rP   r   kZ_gensr   rW   rZ   rZ   r[   r    s     

z
Poly.ejectc                 C   s4   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   r_   r  r3   r   r   rZ   rZ   r[   r    s    
zPoly.terms_gcdc                 C   s.   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   r_   r  r3   r   rU   r   rW   rZ   rZ   r[   r    s    
zPoly.add_groundc                 C   s.   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   r_   r  r3   r   r  rZ   rZ   r[   r    s    
zPoly.sub_groundc                 C   s.   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   r_   r  r3   r   r  rZ   rZ   r[   r    s    
zPoly.mul_groundc                 C   s.   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   r_   r  r3   r   r  rZ   rZ   r[   r  2  s    
zPoly.quo_groundc                 C   s.   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   r_   r	  r3   r   r  rZ   rZ   r[   r	  J  s    
zPoly.exquo_groundc                 C   s,   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   r_   r
  r3   r   r   rZ   rZ   r[   r
  d  s    
zPoly.absc                 C   s,   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   r_   r  r3   r   r   rZ   rZ   r[   r  y  s    
zPoly.negc                 C   sT   t |}|js| |S | |\}}}}t| jdrB||}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!   rk   r  r   r   r_   r   r3   rU   rV   r   r   r   r   rW   rZ   rZ   r[   r     s    

zPoly.addc                 C   sT   t |}|js| |S | |\}}}}t| jdrB||}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!   rk   r  r   r   r_   r  r3   r  rZ   rZ   r[   r    s    

zPoly.subc                 C   sT   t |}|js| |S | |\}}}}t| jdrB||}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!   rk   r  r   r   r_   r   r3   r  rZ   rZ   r[   r     s    

zPoly.mulc                 C   s,   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   r_   r  r3   r   r   rZ   rZ   r[   r    s    
zPoly.sqrc                 C   s6   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   r_   r  r3   r   rU   r   rW   rZ   rZ   r[   r    s
    
zPoly.powc                 C   sH   |  |\}}}}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   r_   r  r3   )rU   rV   r   r   r   r   qrrZ   rZ   r[   r     s
    
z	Poly.pdivc                 C   s<   |  |\}}}}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   r_   r  r3   r  rZ   rZ   r[   r  7  s
    
z	Poly.premc                 C   s<   |  |\}}}}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   r_   r  r3   r  rZ   rZ   r[   r  ^  s
    
z	Poly.pquoc              
   C   sx   |  |\}}}}t| jdrfz||}W qp tk
rb } z||  | W 5 d}~X Y qpX n
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   r_   r  r:   rw   rS   r3   )rU   rV   r   r   r   r   rW   excrZ   rZ   r[   r  z  s    (
zPoly.pexquoc                 C   s   |  |\}}}}d}|r<|jr<|js<| |  }}d}t| jdrX||\}}	n
t| d|rz| |	  }
}W n t	k
r   Y nX |
| }}	||||	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   r_   r  r3   r   r5   )rU   rV   autor   r   r   r   r   r  r  QRrZ   rZ   r[   r    s    

zPoly.divc           	      C   s   |  |\}}}}d}|r<|jr<|js<| |  }}d}t| jdrT||}n
t| d|rz| }W n t	k
r   Y nX ||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   r_   r  r3   r   r5   )	rU   rV   r  r   r   r   r   r   r  rZ   rZ   r[   r    s    
zPoly.remc           	      C   s   |  |\}}}}d}|r<|jr<|js<| |  }}d}t| jdrT||}n
t| d|rz| }W n t	k
r   Y nX ||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   r_   r  r3   r   r5   )	rU   rV   r  r   r   r   r   r   r  rZ   rZ   r[   r    s    
zPoly.quoc           
   
   C   s   |  |\}}}}d}|r<|jr<|js<| |  }}d}t| jdrz||}W q tk
r }	 z|	| 	 |	 W 5 d}	~	X Y qX n
t
| d|rz| }W n tk
r   Y nX ||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   r_   r   r:   rw   rS   r3   r   r5   )
rU   rV   r  r   r   r   r   r   r  r  rZ   rZ   r[   r   
  s"    (
z
Poly.exquoc                 C   s   t |trXt| j}| |  kr*|k rDn n|dk r>|| S |S qtd|||f n4z| jt|W S  tk
r   td| Y nX dS )z3Returns level associated with the given generator. r   z -%s <= gen < %s expected, got %sz"a valid generator expected, got %sN)rM   r   ru   rP   r8   r   r!   r   )rU   r   r   rZ   rZ   r[   r   4  s    

zPoly._gen_to_levelr   c                 C   s0   |  |}t| jdr"| j|S t| ddS )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

        degreeN)r   r   r_   r!  r3   )rU   r   r   rZ   rZ   r[   r!  H  s    
zPoly.degreec                 C   s$   t | jdr| j S t| ddS )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_listN)r   r_   r"  r3   r   rZ   rZ   r[   r"  c  s    
zPoly.degree_listc                 C   s$   t | jdr| j S t| ddS )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_degreeN)r   r_   r#  r3   r   rZ   rZ   r[   r#  v  s    
zPoly.total_degreec                 C   s~   t |tstdt| || jkr8| j|}| j}nt| j}| j|f }t| jdrp| j	| j
||dS t| ddS )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_orderN)rM   r    	TypeErrortyperP   r   ru   r   r_   r   r$  r3   )rU   sr   rP   rZ   rZ   r[   r$    s    


zPoly.homogenizec                 C   s$   t | jdr| j S t| ddS )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%  N)r   r_   r%  r3   r   rZ   rZ   r[   r%    s    
zPoly.homogeneous_orderc                 C   sF   |dk	r|  |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   r_   r)  r3   r   r   )rU   r`   rW   rZ   rZ   r[   r)    s    
zPoly.LCc                 C   s0   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   r_   r*  r3   r   r   r   rZ   rZ   r[   r*    s    
zPoly.TCc                 C   s(   t | jdr| |d S t| ddS )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   ECN)r   r_   r   r3   r   rZ   rZ   r[   r,    s    zPoly.ECc                 C   s   | 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.   rP   	exponents)rU   r   rZ   rZ   r[   coeff_monomial  s    #zPoly.coeff_monomialc                 G   sV   t | jdr>t|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   r_   ru   rP   r   r-  rj   r   r   r3   r   r   )rU   NrW   rZ   rZ   r[   r-  +  s    
zPoly.nthrs   c                 C   s   t dd S )NzyEither convert to Expr with `as_expr` method to use Expr's coeff method or else use the `coeff_monomial` method of Polys.rd   )rU   r   r   rightrZ   rZ   r[   r   M  s    z
Poly.coeffc                 C   s   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   rP   r   rZ   rZ   r[   LMY  s    zPoly.LMc                 C   s   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+  r3  r   rZ   rZ   r[   EMm  s    zPoly.EMc                 C   s"   |  |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.   rP   rU   r`   r   r   rZ   rZ   r[   LT}  s    zPoly.LTc                 C   s"   |  |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+  r6  r7  rZ   rZ   r[   ET  s    zPoly.ETc                 C   s0   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   r_   r:  r3   r   r   r   rZ   rZ   r[   r:    s    
zPoly.max_normc                 C   s0   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   r_   r;  r3   r   r   r   rZ   rZ   r[   r;    s    
zPoly.l1_normc                 C   s   | }|j jjstj|fS | }|jr2|j j }t|j drN|j 	 \}}n
t
|d|||| }}|rx|js||fS || fS d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_denomsN)r_   r   r  r   Oner   has_assoc_Ringget_ringr   r<  r3   r   r   r   )r{   r   rU   r   r   rW   rZ   rZ   r[   r<    s    



zPoly.clear_denomsc                 C   sv   | }| |\}}}}||}||}|jr2|js:||fS |jdd\}}|j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<  r  )r{   rV   rU   r   r   abrZ   rZ   r[   rat_clear_denoms  s    

zPoly.rat_clear_denomsc                 O   s   | }| ddr"|jjjr"| }t|jdr|sF||jjddS |j}|D ]8}t|t	rh|\}}n
|d }}|t
|||}qP||S t|ddS )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	integraters   r   N)getr_   r   r  r   r   r   rD  rM   r   r   r   r3   )r{   specsrp   rU   r_   specr   r   rZ   rZ   r[   rD  	  s    



zPoly.integratec                 O   s   | ddst| f||S t| jdr|s@| | jjddS | j}|D ]8}t|trb|\}}n
|d }}|t|| 	|}qJ| |S t
| ddS )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diffrs   rE  N)rF  r   r   r_   r   rJ  rM   r   r   r   r3   )rU   rG  r   r_   rH  r   r   rZ   rZ   r[   rJ  C	  s    



z	Poly.diffc                 C   sR  | }|dkrt |tr<|}| D ]\}}|||}q"|S t |ttfr|}t|t|jkrhtdt	|j|D ]\}}|||}qt|S d| }	}n
|
|}	t|jdst|dz|j||	}
W nv tk
rB   |std||jjf nFt|g\}\}| ||j}||}|||}|j||	}
Y nX |j|
|	d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 %s in %sr   )rM   rg   r   r   r   rj   ru   rP   r   r   r   r   r_   r3   r5   r4   r   r'   r   Zunify_with_symbolsr   r   r   )r{   r   rA  r  rU   r   r   r   valuesr   rW   Za_domainZ
new_domainrZ   rZ   r[   r   k	  s:    



z	Poly.evalc                 G   s
   |  |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   )rU   rL  rZ   rZ   r[   __call__	  s    zPoly.__call__c           	      C   sd   |  |\}}}}|r.|jr.| |  }}t| jdrJ||\}}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   r_   rN  r3   )	rU   rV   r  r   r   r   r   r(  hrZ   rZ   r[   rN  	  s    

zPoly.half_gcdexc           
      C   sl   |  |\}}}}|r.|jr.| |  }}t| jdrL||\}}}	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   r_   rP  r3   )
rU   rV   r  r   r   r   r   r(  trO  rZ   rZ   r[   rP  	  s    

z
Poly.gcdexc                 C   sX   |  |\}}}}|r.|jr.| |  }}t| jdrF||}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   r_   rR  r3   )rU   rV   r  r   r   r   r   rW   rZ   rZ   r[   rR  
  s    

zPoly.invertc                 C   s2   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   r_   rS  r   r3   r   r  rZ   rZ   r[   rS  +
  s    
zPoly.revertc                 C   sB   |  |\}}}}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   r_   rT  r3   rj   r   r  rZ   rZ   r[   rT  M
  s
    
zPoly.subresultantsc           	      C   sv   |  |\}}}}t| jdrB|r6|j||d\}}qL||}n
t| d|rj||ddtt||fS ||d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   rK  )r   r   r_   rU  r3   rj   r   )	rU   rV   rW  r   r   r   r   rW   r  rZ   rZ   r[   rU  f
  s    
zPoly.resultantc                 C   s0   t | jdr| j }n
t| d| j|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   rK  )r   r_   rX  r3   r   r   rZ   rZ   r[   rX  
  s    
zPoly.discriminantc                 C   s   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.dispersionrY  )rU   rV   rY  rZ   rZ   r[   rY  
  s    HzPoly.dispersionsetc                 C   s   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)rZ  r[  )rU   rV   r[  rZ   rZ   r[   r[  
  s    HzPoly.dispersionc           	      C   sP   |  |\}}}}t| jdr0||\}}}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   r_   r\  r3   )	rU   rV   r   r   r   r   rO  cffcfgrZ   rZ   r[   r\  6  s
    
zPoly.cofactorsc                 C   s<   |  |\}}}}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   r_   r_  r3   r  rZ   rZ   r[   r_  S  s
    
zPoly.gcdc                 C   s<   |  |\}}}}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   r_   r`  r3   r  rZ   rZ   r[   r`  j  s
    
zPoly.lcmc                 C   s<   | 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)r_   r   r   r   ra  r3   r   )rU   prW   rZ   rZ   r[   ra    s
    
z
Poly.truncc                 C   sF   | }|r|j jjr| }t|j dr2|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)r_   r   r  r   r   rc  r3   r   r{   r  rU   rW   rZ   rZ   r[   rc    s    
z
Poly.monicc                 C   s0   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   r_   re  r3   r   r   r   rZ   rZ   r[   re    s    
zPoly.contentc                 C   s>   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   r_   rf  r3   r   r   r   )rU   contrW   rZ   rZ   r[   rf    s    
zPoly.primitivec                 C   s<   |  |\}}}}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   r_   rh  r3   r  rZ   rZ   r[   rh    s
    
zPoly.composec                 C   s2   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   r_   ri  r3   rj   r   r   r   rZ   rZ   r[   ri    s    
zPoly.decomposec                 C   s.   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   r_   rj  r3   r   )rU   rA  rW   rZ   rZ   r[   rj    s    
z
Poly.shiftc                 C   s^   | |\}}|  |\}}| |\}}t|jdrJ|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   r_   rk  r3   r   )rU   rb  r  Pr  r   rW   rZ   rZ   r[   rk     s    
zPoly.transformc                 C   sL   | }|r|j jjr| }t|j dr2|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)
r_   r   r  r   r   rm  r3   rj   r   r   rd  rZ   rZ   r[   rm  :  s    
z
Poly.sturmc                    s4   t  jdr j }n
t d fd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                    s   g | ]\}}  ||fqS rZ   r   r   rV   r  r   rZ   r[   r   l  s     z!Poly.gff_list.<locals>.<listcomp>)r   r_   rn  r3   r   rZ   r   r[   rn  W  s    
zPoly.gff_listc                 C   s,   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   r_   rq  r3   r   )rU   r  rZ   rZ   r[   rq  n  s    
z	Poly.normc                 C   s>   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   r_   rr  r3   r   )rU   r(  rV   r  rZ   rZ   r[   rr    s    
zPoly.sqf_normc                 C   s,   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   r_   rs  r3   r   r   rZ   rZ   r[   rs    s    
zPoly.sqf_partc                    sH   t  jdr j|\}}n
t d jj| fd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                    s   g | ]\}}  ||fqS rZ   ro  rp  r   rZ   r[   r     s     z!Poly.sqf_list.<locals>.<listcomp>)r   r_   rt  r3   r   r   )rU   allr   factorsrZ   r   r[   rt    s    
zPoly.sqf_listc                    s6   t  jdr j|}n
t d fd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                    s   g | ]\}}  ||fqS rZ   ro  rp  r   rZ   r[   r     s     z)Poly.sqf_list_include.<locals>.<listcomp>)r   r_   rw  r3   )rU   ru  rv  rZ   r   r[   rw    s    
zPoly.sqf_list_includec                    sp   t  jdrFz j \}}W qP tk
rB   tj dfgf Y S X n
t d jj| fd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_listrs   c                    s   g | ]\}}  ||fqS rZ   ro  rp  r   rZ   r[   r     s     z$Poly.factor_list.<locals>.<listcomp>)	r   r_   rx  r4   r   r=  r3   r   r   )rU   r   rv  rZ   r   r[   rx    s    
zPoly.factor_listc                    sX   t  jdr<z j }W qF tk
r8    dfg Y S X n
t d fd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_includers   c                    s   g | ]\}}  ||fqS rZ   ro  rp  r   rZ   r[   r   7  s     z,Poly.factor_list_include.<locals>.<listcomp>)r   r_   ry  r4   r3   )rU   rv  rZ   r   r[   ry    s    
zPoly.factor_list_includec                 C   s
  |dk	r"t |}|dkr"td|dk	r4t |}|dk	rFt |}t| jdrl| jj||||||d}n
t| d|rdd }|stt||S dd	 }	|\}
}tt||
tt|	|fS d
d }|stt||S dd	 }	|\}
}tt||
tt|	|fS d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ru  epsinfsupfastsqfc                 S   s   | \}}t |t |fS rx   r)   r   )intervalr(  rQ  rZ   rZ   r[   _reale  s    zPoly.intervals.<locals>._realc                 S   s@   | \\}}\}}t |tt |  t |tt |  fS rx   r)   r   r   )	rectangleuvr(  rQ  rZ   rZ   r[   _complexl  s    z Poly.intervals.<locals>._complexc                 S   s$   | \\}}}t |t |f|fS rx   r  )r  r(  rQ  r  rZ   rZ   r[   r  u  s    c                 S   sH   | \\\}}\}}}t |tt |  t |tt |  f|fS rx   r  )r  r  r  r(  rQ  r  rZ   rZ   r[   r  |  s    )	r)   r   r   r   r_   r{  r3   rj   r   )rU   ru  r}  r~  r  r  r  rW   r  r  Z	real_partZcomplex_partrZ   rZ   r[   r{  9  s>    


     
zPoly.intervalsc           	      C   s   |r| j stdt|t| }}|dk	rJt|}|dkrJtd|dk	r\t|}n|dkrhd}t| jdr| jj|||||d\}}n
t	| dt
|t
|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   rz  rs   refine_root)r}  stepsr  )is_sqfr8   r)   r   r   r   r   r_   r  r3   r   )	rU   r(  rQ  r}  r  r  	check_sqfr   TrZ   rZ   r[   r    s    



zPoly.refine_rootc                 C   sL  d\}}|dk	r^t |}|tjkr(d}n6| \}}|sDt|}ntttj||fd }}|dk	rt |}|tjkr~d}n6| \}}|st|}ntttj||fd }}|r|rt	| j
dr| j
j||d}n
t| dn^|r |dk	r |tjf}|r|dk	r|tjf}t	| j
dr:| j
j||d}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r~  r  count_complex_roots)r!   r   NegativeInfinityas_real_imagr)   r   rj   r   Infinityr   r_   r  r3   r   r  r   )rU   r~  r  Zinf_realZsup_realreZimcountrZ   rZ   r[   count_roots  s:    




zPoly.count_rootsc                 C   s   t jjj| ||d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rootoftoolsZrootof)rU   r   r  rZ   rZ   r[   root  s    z	Poly.rootc                 C   s,   t jjjj| |d}|r|S t|ddS 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multipleN)r  r  r  CRootOf
real_rootsrF   )rU   r  r  ZrealsrZ   rZ   r[   r    s    zPoly.real_rootsc                 C   s,   t jjjj| |d}|r|S t|ddS 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  Fr  N)r  r  r  r  	all_rootsrF   )rU   r  r  rootsrZ   rZ   r[   r    s    zPoly.all_roots   2   c                    s  | j rtd|  |  dkr"g S | jjtkrBdd |  D }n| jjtkrdd |  D }t|   fdd|  D }nNfdd|  D }zdd |D }W n$ t	k
r   t
d	| jj Y nX tjj}tj_dd
lm zz>tj|||d|  d d}tttt|fddd}W n| tk
r   z>tj|||d|  d d}tttt|fddd}W n& tk
r   td|f Y nX Y nX W 5 |tj_X |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                 S   s   g | ]}t |qS rZ   r   r   r   rZ   rZ   r[   r   Z  s     zPoly.nroots.<locals>.<listcomp>c                 S   s   g | ]
}|j qS rZ   )r  r  rZ   rZ   r[   r   \  s     c                    s   g | ]}t |  qS rZ   r  r  )facrZ   r[   r   ^  s     c                    s   g | ]}|j  d  qS )r   )r   r  r  r  rZ   r[   r   `  s   c                 S   s   g | ]}t j| qS rZ   )mpmathZmpcr  rZ   rZ   r[   r   c  s     z!Numerical domain expected, got %ssignF
   )maxstepscleanuperrorZ	extraprecc                    s$   | j r
dnd| jt| j  | j fS Nrs   r   imagrealr
  r  r  rZ   r[   <lambda>u      zPoly.nroots.<locals>.<lambda>keyr  c                    s$   | j r
dnd| jt| j  | j fS r  r  r  r  rZ   r[   r  |  r  z7convergence to root failed; try n < %s or maxsteps > %s)is_multivariater9   r!  r_   r   r*   r   r)   r   r&  r4   r  mpdpsZ$sympy.functions.elementary.complexesr  Z	polyrootsrj   r   r!   sortedrL   )rU   r   r  r  r   Zdenomsr  r  rZ   )r  r   r  r[   nroots6  sh    
  

  

 
zPoly.nrootsc                 C   sP   | j rtd|  i }|  d D ](\}}|jr"| \}}||| | < q"|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 %srs   )r  r9   rx  	is_linearr   )rU   r  factorr  rA  rB  rZ   rZ   r[   ground_roots  s    zPoly.ground_rootsc                 C   sr   | j rtdt|}|jr.|dkr.t|}ntd| | j}td}| | j	
|| | ||}|||S )af  
        Construct a polynomial with n-th powers of roots of ``f``.

        Examples
        ========

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

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

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

        zmust be a univariate polynomialrs   z&'n' must an integer and n >= 1, got %srQ  )r  r9   r!   
is_Integerr   r   r   r   rU  r   rO   r   )rU   r   r0  r   rQ  r  rZ   rZ   r[   nth_power_roots_poly  s    
zPoly.nth_power_roots_polyc                    s   | j rtd| j }| j |}|d }td| di \}}}}t|}|d |d    fdd}	|	||	| }
}|
j	|j	 d |
j
|j
 d  |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	   rs      c                    s   t t|  dS )NrE  )r   r   r   rE  rZ   r[   r    r  z Poly.same_root.<locals>.<lambda>)r  r9   r_   Zmignotte_sep_bound_squaredr   	get_fieldr   r   r   r  r  )rU   rA  rB  Zdom_delta_sqZdelta_sqZeps_sqr  r   r   ZevABrZ   rE  r[   	same_root  s    
zPoly.same_rootc                 C   s   |  |\}}}}t|dr,|j||d}n
t| d|s~|jrH| }|\}}	}
}||}||	}	||	 ||
||fS tt||S d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)includeN)	r   r   r  r3   r>  r?  r   r   r   )rU   rV   r  r   r   r   r   rW   cpcqrb  r  rZ   rZ   r[   r    s    



zPoly.cancelc                 C   sh   | j r| jttfkrtd| jr6| jtkr6| tjfS |  }| \}}|	t
|j| |fS d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.N)r   r   r*   r)   r   is_monicr   rc  r<  rk  rN   r   r   )rU   fmr   r   rZ   rZ   r[   )make_monic_over_integers_by_scaling_roots!  s    
z.Poly.make_monic_over_integers_by_scaling_roots   c                 C   s  ddl m}m}m}m} | jr2| jr2| jtt	fkr:t
d||||d}t| }	|  }
|
|	krvt
d|	 dnx|
dk rt
dnf|
dkrdd	lm} |jd
 }}nD|
dkrddlm} |jd }}n"|  \}}||
 |||d\}}|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.rs   z(Constant polynomial has no Galois group.)S1TransitiveSubgroupsTr  )S2TransitiveSubgroupsF)	max_tries	randomize)Z%sympy.polys.numberfields.galoisgroupsr  r  r  r  r   is_irreducibler   r*   r)   r   maxkeysr!  Zsympy.combinatorics.galoisr  ZS1r  ZS2r  Zget_perm_group)rU   Zby_namer  r  r  r  r  r  ZggZmax_supportedr   r  nameZaltr  rV   r   r   rZ   rZ   r[   galois_groupH  s8    
zPoly.galois_groupc                 C   s   | 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

        )r_   is_zeror   rZ   rZ   r[   r  ~  s    zPoly.is_zeroc                 C   s   | 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

        )r_   is_oner   rZ   rZ   r[   r    s    zPoly.is_onec                 C   s   | 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

        )r_   r  r   rZ   rZ   r[   r    s    zPoly.is_sqfc                 C   s   | 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

        )r_   r  r   rZ   rZ   r[   r    s    zPoly.is_monicc                 C   s   | 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

        )r_   is_primitiver   rZ   rZ   r[   r    s    zPoly.is_primitivec                 C   s   | 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

        )r_   	is_groundr   rZ   rZ   r[   r    s    zPoly.is_groundc                 C   s   | 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

        )r_   r  r   rZ   rZ   r[   r    s    zPoly.is_linearc                 C   s   | 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

        )r_   is_quadraticr   rZ   rZ   r[   r    s    zPoly.is_quadraticc                 C   s   | 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

        )r_   is_monomialr   rZ   rZ   r[   r    s    zPoly.is_monomialc                 C   s   | 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

        )r_   is_homogeneousr   rZ   rZ   r[   r  +  s    zPoly.is_homogeneousc                 C   s   | 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

        )r_   r  r   rZ   rZ   r[   r  C  s    zPoly.is_irreduciblec                 C   s   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

        rs   ru   rP   r   rZ   rZ   r[   r   V  s    zPoly.is_univariatec                 C   s   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

        rs   r  r   rZ   rZ   r[   r  m  s    zPoly.is_multivariatec                 C   s   | 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

        )r_   is_cyclotomicr   rZ   rZ   r[   r    s    zPoly.is_cyclotomicc                 C   s   |   S rx   )r
  r   rZ   rZ   r[   __abs__  s    zPoly.__abs__c                 C   s   |   S rx   )r  r   rZ   rZ   r[   __neg__  s    zPoly.__neg__c                 C   s
   |  |S rx   r   rU   rV   rZ   rZ   r[   __add__  s    zPoly.__add__c                 C   s
   | | S rx   r  r  rZ   rZ   r[   __radd__  s    zPoly.__radd__c                 C   s
   |  |S rx   r  r  rZ   rZ   r[   __sub__  s    zPoly.__sub__c                 C   s
   | | S rx   r  r  rZ   rZ   r[   __rsub__  s    zPoly.__rsub__c                 C   s
   |  |S rx   r   r  rZ   rZ   r[   __mul__  s    zPoly.__mul__c                 C   s
   | | S rx   r   r  rZ   rZ   r[   __rmul__  s    zPoly.__rmul__r   c                 C   s    |j r|dkr| |S tS d S )Nr   )r  r  rQ   )rU   r   rZ   rZ   r[   __pow__  s    
zPoly.__pow__c                 C   s
   |  |S rx   r  r  rZ   rZ   r[   
__divmod__  s    zPoly.__divmod__c                 C   s
   | | S rx   r  r  rZ   rZ   r[   __rdivmod__  s    zPoly.__rdivmod__c                 C   s
   |  |S rx   r  r  rZ   rZ   r[   __mod__  s    zPoly.__mod__c                 C   s
   | | S rx   r  r  rZ   rZ   r[   __rmod__  s    zPoly.__rmod__c                 C   s
   |  |S rx   r  r  rZ   rZ   r[   __floordiv__  s    zPoly.__floordiv__c                 C   s
   | | S rx   r  r  rZ   rZ   r[   __rfloordiv__  s    zPoly.__rfloordiv__rV   c                 C   s   |   |   S rx   rS   r  rZ   rZ   r[   __truediv__  s    zPoly.__truediv__c                 C   s   |  |    S rx   r  r  rZ   rZ   r[   __rtruediv__  s    zPoly.__rtruediv__otherc              
   C   sx   | | }}|j sHz|j||j| d}W n tttfk
rF   Y dS X |j|jkrXdS |jj|jjkrldS |j|jkS Nr   F)	rk   r   rP   r   r8   r4   r5   r_   r   )r{   r  rU   rV   rZ   rZ   r[   __eq__  s    
zPoly.__eq__c                 C   s
   | |k S rx   rZ   r  rZ   rZ   r[   __ne__  s    zPoly.__ne__c                 C   s   | j  S rx   )r  r   rZ   rZ   r[   __bool__  s    zPoly.__bool__c                 C   s   |s| |kS |  t|S d S rx   )
_strict_eqr!   rU   rV   strictrZ   rZ   r[   eq  s    zPoly.eqc                 C   s   | j ||d S )Nr  )r  r  rZ   rZ   r[   ne   s    zPoly.nec                 C   s*   t || jo(| j|jko(| jj|jddS NTr  )rM   r   rP   r_   r  r  rZ   rZ   r[   r
    s    zPoly._strict_eq)NN)N)N)N)N)N)N)FF)F)F)T)T)T)T)r   )N)N)rs   F)N)N)N)N)F)NT)T)T)T)F)N)N)T)T)F)F)FNNNFF)NNFF)NN)T)TT)TT)r  r  T)F)Fr  F)F)F)rT   
__module____qualname____doc__	__slots__is_commutativerk   Z_op_priorityrr   classmethodrw   propertyr|   rp   r}   r   r   r   rO   rh   ri   rl   rm   re   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ra   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rS   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   r4  r5  r8  r9  r:  r;  r<  rC  rD  rJ  Z_eval_derivativer   rM  rN  rP  rR  rS  rT  rU  rX  rY  r[  r\  r_  r`  ra  rc  re  rf  rh  ri  rj  rk  rm  rn  rq  rr  rs  rt  rw  rx  ry  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   rQ   r  r  r  r  r  r   r  r  r  r  r  r	  r  r  r
  __classcell__rZ   rZ   r   r[   rN   e   s  5











	






3#$"%&%*''%%*"%"''(&K!"%KK
#!L%?P(3%'6




rN   c                       sV   e Zd ZdZdd Z fddZedd Zede	d	d
 Z
dd Zdd Z  ZS )PurePolyz)Class for representing pure polynomials. c                 C   s   | j fS )z$Allow SymPy to hash Poly instances. )r_   rz   rZ   rZ   r[   r}     s    zPurePoly._hashable_contentc                    s
   t   S rx   r   rz   r   rZ   r[   r     s    zPurePoly.__hash__c                 C   s   | 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   rz   rZ   rZ   r[   r     s    zPurePoly.free_symbolsr  c              
   C   s   | | }}|j sHz|j||j| d}W n tttfk
rF   Y dS X t|jt|jkr`dS |jj	|jj	krz|jj	
|jj	|j}W n tk
r   Y dS X ||}||}|j|jkS r  )rk   r   rP   r   r8   r4   r5   ru   r_   r   r   r6   r   )r{   r  rU   rV   r   rZ   rZ   r[   r  '  s     


zPurePoly.__eq__c                 C   s   t || jo| jj|jddS r  )rM   r   r_   r  r  rZ   rZ   r[   r
  ?  s    zPurePoly._strict_eqc                    s   t |}|js\z(| jj| j| j| j| jj|fW S  tk
rZ   td| |f Y nX t| j	t|j	krtd| |f t
| jtrt
|jtstd| |f | j | j	}| jj|jj|}| j|}|j|}||d f fdd	}||||fS )Nr   c                    sB   |d k	r2|d | ||d d   }|s2| | S  j| f| S r   r   r   r   rZ   r[   r   Y  s
    
zPurePoly._unify.<locals>.per)r!   rk   r_   r   r   r   r5   r6   ru   rP   rM   r0   r   r   r   )rU   rV   rP   r   r   r   r   rZ   r   r[   r   B  s"    (	zPurePoly._unify)rT   r  r  r  r}   r   r  r   r   rQ   r  r
  r   r  rZ   rZ   r   r[   r    s   

r  c                 O   s   t ||}t| |S r   )rb   rc   _poly_from_exprr|   rP   rp   rq   rZ   rZ   r[   poly_from_expre  s    r  c                 C   s  | t |  }} t| ts&t||| nJ| jrb| j| |}|j|_|j|_|j	dkrZd|_	||fS |j
rp| 
 } t| |\}}|jst||| ttt|  \}}|j}|dkrt||d\|_}ntt|j|}ttt||}t||}|j	dkr
d|_	||fS )r   NTr   F)r!   rM   r   r;   rk   r   rl   rP   r   r  expandrB   rj   r   r   r'   r   r   rg   rN   rh   )r|   rq   origr   r_   r   r   r   rZ   rZ   r[   r  l  s2    

r  c                 O   s   t ||}t| |S )(Construct polynomials from expressions. )rb   rc   _parallel_poly_from_expr)exprsrP   rp   rq   rZ   rZ   r[   parallel_poly_from_expr  s    r"  c                 C   s  t | dkr~| \}}t|tr~t|tr~|j||}|j||}||\}}|j|_|j|_|jdkrrd|_||g|fS t	| g  }} g g  }}d}t
|D ]T\}}	t|	}	t|	tr|	jr|| q|| |jr|	 }	nd}| |	 q|rt||| d|r.|D ]}| |  | |< qt| |\}
}|jsRt||| dddlm} |jD ]}t||rdtdqdg g  }}g }g }|
D ]@}t	tt	|  \}}|| || |t | q|j}|dkrt||d\|_}nt	t|j|}|D ]$}||d|  ||d }qg }t||D ]2\}}tt	t||}t||}|| qD|jdkrt||_||fS )	r  r  NTFr   	Piecewisez&Piecewise generators do not make senser   )ru   rM   rN   r   rl   r   rP   r   r  rj   r   r!   r   rk   appendr  r;   rS   rC   $sympy.functions.elementary.piecewiser$  r8   r   r   extendr'   r   r   rg   rh   bool)r!  rq   rU   rV   ZorigsZ_exprsZ_polysfailedr   r|   repsr$  r  Zcoeffs_listlengthsr   r   r_   r   r   r   r  r   rZ   rZ   r[   r     sv    










r   c                 C   s   t | } || kr|| |< | S )z7Add a new ``(key, value)`` pair to arguments ``dict``. )rg   )rp   r  r   rZ   rZ   r[   _update_args  s    r,  c                 C   s   t | dd} t |ddj}| jr0| }| j}n*| j}|sZ|rLt| \}}nt| |\}}|rn| rhtjS tjS |s| jr||jkrt|  \}}||jkrtjS n4| jst	| j
dkrttd| tt| j
|f ||}t|trt|S 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  rs   z
         A symbolic generator of interest is required for a multivariate
         expression like func = %s, e.g. degree(func, gen = %s) instead of
         degree(func, gen = %s).
        )r!   	is_Numberrk   rS   r  r   Zeror  rP   ru   r   r&  rH   nextr   r!  rM   r   r   )rU   r   Z
gen_is_Numrb  ZisNumr   rW   rZ   rZ   r[   r!    s.    

r!  c                 G   sH   t | }|jr| }|jr"d}n| jr2|p0| 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!   rk   rS   r-  rP   rN   r#  r   )rU   rP   rb  rvrZ   rZ   r[   r#  >  s    (
r#  c              
   O   sl   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX | }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"  rs   N)	rb   allowed_flagsr  r;   r<   r"  r   r   r   )rU   rP   rp   r   rq   r  degreesrZ   rZ   r[   r"  s  s    r"  c              
   O   sd   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX |j|jd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)  rs   Nr   )rb   r1  r  r;   r<   r)  r`   rU   rP   rp   r   rq   r  rZ   rZ   r[   r)    s    r)  c              
   O   sl   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX |j|jd}| 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  r4  rs   Nr   )rb   r1  r  r;   r<   r4  r`   rS   )rU   rP   rp   r   rq   r  r   rZ   rZ   r[   r4    s    r4  c              
   O   st   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX |j|jd\}}||  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  r8  rs   Nr   )rb   r1  r  r;   r<   r8  r`   rS   )rU   rP   rp   r   rq   r  r   r   rZ   rZ   r[   r8    s    r8  c           
   
   O   s   t |dg z t| |ff||\\}}}W n. tk
r\ } ztdd|W 5 d}~X Y nX ||\}}	|js| |	 fS ||	fS d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)rb   r1  r"  r;   r<   r  r  rS   
rU   rV   rP   rp   r   r   rq   r  r  r  rZ   rZ   r[   r    s     r  c           	   
   O   s~   t |dg z t| |ff||\\}}}W n. tk
r\ } ztdd|W 5 d}~X Y nX ||}|jsv| S |S d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)rb   r1  r"  r;   r<   r  r  rS   	rU   rV   rP   rp   r   r   rq   r  r  rZ   rZ   r[   r    s     
r  c           	   
   O   s   t |dg z t| |ff||\\}}}W n. tk
r\ } ztdd|W 5 d}~X Y nX z||}W n tk
r   t| |Y nX |js| S |S d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)	rb   r1  r"  r;   r<   r  r:   r  rS   	rU   rV   rP   rp   r   r   rq   r  r  rZ   rZ   r[   r    s     r  c           	   
   O   s~   t |dg z t| |ff||\\}}}W n. tk
r\ } ztdd|W 5 d}~X Y nX ||}|jsv| S |S d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)rb   r1  r"  r;   r<   r  r  rS   r6  rZ   rZ   r[   r  :  s     
r  c           
   
   O   s   t |ddg z t| |ff||\\}}}W n. tk
r^ } ztdd|W 5 d}~X Y nX |j||jd\}}	|js| |	 fS ||	fS d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  )	rb   r1  r"  r;   r<   r  r  r  rS   r4  rZ   rZ   r[   r  ]  s     r  c           	   
   O   s   t |ddg z t| |ff||\\}}}W n. tk
r^ } ztdd|W 5 d}~X Y nX |j||jd}|js~| S |S d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  Nr7  )	rb   r1  r"  r;   r<   r  r  r  rS   r5  rZ   rZ   r[   r  }  s     r  c           	   
   O   s   t |ddg z t| |ff||\\}}}W n. tk
r^ } ztdd|W 5 d}~X Y nX |j||jd}|js~| S |S d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  Nr7  )	rb   r1  r"  r;   r<   r  r  r  rS   r6  rZ   rZ   r[   r    s     r  c           	   
   O   s   t |ddg z t| |ff||\\}}}W n. tk
r^ } ztdd|W 5 d}~X Y nX |j||jd}|js~| S |S d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  Nr7  )	rb   r1  r"  r;   r<   r   r  r  rS   r6  rZ   rZ   r[   r     s     r   c                 O   s   t |ddg z t| |ff||\\}}}W n tk
r } zht|j\}\}	}
z||	|
\}}W n  tk
r   tdd|Y nX |	||	|f W Y S W 5 d}~X Y nX |j||j
d\}}|js| | fS ||fS d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  rN  r  Nr7  )rb   r1  r"  r;   r'   r!  rN  rd   r<   r   r  r  rS   )rU   rV   rP   rp   r   r   rq   r  r   rA  rB  r(  rO  rZ   rZ   r[   rN    s     .rN  c                 O   s  t |ddg z t| |ff||\\}}}W n tk
r } zrt|j\}\}	}
z||	|
\}}}W n  tk
r   tdd|Y n&X |	||	||	|f W Y S W 5 d}~X Y nX |j||j
d\}}}|js| | | fS |||fS d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  rP  r  Nr7  )rb   r1  r"  r;   r'   r!  rP  rd   r<   r   r  r  rS   )rU   rV   rP   rp   r   r   rq   r  r   rA  rB  r(  rQ  rO  rZ   rZ   r[   rP    s     6rP  c                 O   s   t |ddg z t| |ff||\\}}}W nr tk
r } zTt|j\}\}	}
z|||	|
W  W Y &S  tk
r   t	dd|Y nX W 5 d}~X Y nX |j||j
d}|js| S |S d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  rR  r  Nr7  )rb   r1  r"  r;   r'   r!  r   rR  rd   r<   r  r  rS   )rU   rV   rP   rp   r   r   rq   r  r   rA  rB  rO  rZ   rZ   r[   rR  .  s    ! $rR  c           	   
   O   s   t |dg z t| |ff||\\}}}W n. tk
r\ } ztdd|W 5 d}~X Y nX ||}|js|dd |D S |S d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  rT  r  Nc                 S   s   g | ]}|  qS rZ   r  r   r  rZ   rZ   r[   r   |  s     z!subresultants.<locals>.<listcomp>)rb   r1  r"  r;   r<   rT  r  	rU   rV   rP   rp   r   r   rq   r  rW   rZ   rZ   r[   rT  c  s     
rT  FrV  c             
   O   s   t |dg z t| |ff||\\}}}W n. tk
r\ } ztdd|W 5 d}~X Y nX |rv|j||d\}	}
n
||}	|js|r|	 dd |
D fS |	 S |r|	|
fS |	S d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  rU  r  NrV  c                 S   s   g | ]}|  qS rZ   r  r8  rZ   rZ   r[   r     s     zresultant.<locals>.<listcomp>)rb   r1  r"  r;   r<   rU  r  rS   )rU   rV   rW  rP   rp   r   r   rq   r  rW   r  rZ   rZ   r[   rU    s     
rU  c              
   O   st   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX | }|jsl| S |S dS )z
    Compute discriminant of ``f``.

    Examples
    ========

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

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

    r  rX  rs   N)rb   r1  r  r;   r<   rX  r  rS   rU   rP   rp   r   rq   r  rW   rZ   rZ   r[   rX    s    rX  c                 O   s   t |dg z t| |ff||\\}}}W n tk
r } zrt|j\}\}	}
z||	|
\}}}W n  tk
r   tdd|Y n&X |	||	||	|f W Y S W 5 d}~X Y nX ||\}}}|j
s| | | fS |||fS d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  r\  r  N)rb   r1  r"  r;   r'   r!  r\  rd   r<   r   r  rS   )rU   rV   rP   rp   r   r   rq   r  r   rA  rB  rO  r]  r^  rZ   rZ   r[   r\    s     6r\  c              
      s  t | } fdd}|| }|dk	r*|S tdg zt| f\}}t| dkrtdd | D r| d   fd	d
| dd D }tdd |D rd}|D ]}	t||	 d }qt | W S W nT t	k
r$ }
 z4||
j
}|dk	r| W Y S tdt| |
W 5 d}
~
X Y nX |sF|js:tjS td|dS |d |dd  }}|D ]}||}|jr` qq`|js| S |S d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                    sl   sh sht | \}}|s|jS |jrh|d |dd   }}|D ]}|||}||r> q^q>||S d S Nr   rs   )r'   r   r   r_  r  r   seqr   numbersrW   numberrp   rP   rZ   r[   try_non_polynomial_gcd  s    

z(gcd_list.<locals>.try_non_polynomial_gcdNr  rs   c                 s   s   | ]}|j o|jV  qd S rx   is_algebraicis_irrationalr   r   rZ   rZ   r[   	<genexpr>  s     zgcd_list.<locals>.<genexpr>r+  c                    s   g | ]} |   qS rZ   ratsimprE  rA  rZ   r[   r     s     zgcd_list.<locals>.<listcomp>c                 s   s   | ]}|j V  qd S rx   is_rationalr   frcrZ   rZ   r[   rF    s     r   gcd_listr   )r!   rb   r1  r"  ru   ru  r`  as_numer_denomr
  r;   r!  r<   r  r   r.  rN   r_  r  rS   )r=  rP   rp   rA  rW   r  rq   lstlcrM  r  r   rZ   rA  rp   rP   r[   rN    sB    

"

rN  c                 O   sV  t | dr,|dk	r|f| }t| f||S |dkr<tdt|dg zrt| |ff||\\}}}tt| |f\}}|jr|j	r|jr|j	r|| 
 }	|	jrt||	 d  W S W nv tk
r2 }
 zVt|
j\}\}}z||||W  W Y (S  tk
r    tdd|
Y nX W 5 d}
~
X Y nX ||}|jsN| S |S d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   rN  r&  rb   r1  r"  r   r!   rC  rD  rH  rK  r
  rO  r;   r'   r!  r   r_  rd   r<   r  rS   rU   rV   rP   rp   r   r   rq   rA  rB  rM  r  r   rW   rZ   rZ   r[   r_  B  s0    

$
r_  c              
      s  t | } tt dfdd}|| }|dk	r4|S tdg zt| f\}}t| dkrtdd | D r| d	   fd
d| dd	 D }tdd |D rd}|D ]}	t||		 d }q | W S W nT t
k
r* }
 z4||
j}|dk	r
| W Y S tdt| |
W 5 d}
~
X Y nX |sL|js@tjS td|dS |d |dd  }}|D ]}||}qf|js| S |S d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                    sd   s` s`t | \}}|s$||jS |jr`|d |dd   }}|D ]}|||}qD||S d S r;  )r'   r   r   r   r`  r<  r@  rZ   r[   try_non_polynomial_lcm  s    
z(lcm_list.<locals>.try_non_polynomial_lcmNr  rs   c                 s   s   | ]}|j o|jV  qd S rx   rB  rE  rZ   rZ   r[   rF    s     zlcm_list.<locals>.<genexpr>r+  c                    s   g | ]} |   qS rZ   rG  rE  rI  rZ   r[   r     s     zlcm_list.<locals>.<listcomp>c                 s   s   | ]}|j V  qd S rx   rJ  rL  rZ   rZ   r[   rF    s     lcm_listr   r   )r!   r   r   rb   r1  r"  ru   ru  r`  rO  r;   r!  r<   r  r   r=  rN   rS   )r=  rP   rp   rV  rW   r  rq   rP  rQ  rM  r  r   rZ   rR  r[   rW  v  s>    

"rW  c                 O   sR  t | dr,|dk	r|f| }t| f||S |dkr<tdt|dg znt| |ff||\\}}}tt| |f\}}|jr|j	r|jr|j	r|| 
 }	|	jr||	 d  W S W nv tk
r. }
 zVt|
j\}\}}z||||W  W Y (S  tk
r   tdd|
Y nX W 5 d}
~
X Y nX ||}|jsJ| S |S d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

    rS  Nz2lcm() takes 2 arguments or a sequence of argumentsr  rs   r`  r  )r   rW  r&  rb   r1  r"  r   r!   rC  rD  rH  rK  rO  r;   r'   r!  r   r`  rd   r<   r  rS   rT  rZ   rZ   r[   r`    s0    

$
r`  c              
      s  t | }t| tr2t fdd| j| jfD  S t| trJtd| f t| trZ| jr^|S  	ddr| j
 fdd| jD  } d d d< t|f S  d	d
}t dg zt| f \}}W n. tk
r } z|j W Y S d}~X Y nX | \}	} |jjrZ|jjr:| jd
d\}
} |  \}} |jjr`||
 }ntj}tdd t| j|	D  }t|drtj}|dkr|S |rt|||   S t||  dd  \}} t|||  d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   s   | ]}t |f V  qd S rx   r  )r   r(  r@  rZ   r[   rF  =  s     zterms_gcd.<locals>.<genexpr>z5Inequalities cannot be used with terms_gcd. Found: %sdeepFc                    s   g | ]}t |f qS rZ   rX  )r   rA  r@  rZ   r[   r   E  s     zterms_gcd.<locals>.<listcomp>r  clearTr  Nr@  c                 S   s   g | ]\}}|| qS rZ   rZ   )r   r   r   rZ   rZ   r[   r   _  s     rs   )rZ  )!r!   rM   r   lhsrhsr   r&  r   is_AtomrF  rY   rp   popr  rb   r1  r  r;   r|   r   r  r  r<  rf  r   r=  r   r   rP   r   r   rS   Zas_coeff_Mul)rU   rP   rp   r  rw   rZ  r   rq   r  r   denomr   termrZ   r@  r[   r    sF    C
 






r  c              
   O   s|   t |ddg zt| f||\}}W n. tk
rV } ztdd|W 5 d}~X Y nX |t|}|jst| S |S d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  ra  rs   N)	rb   r1  r  r;   r<   ra  r!   r  rS   )rU   rb  rP   rp   r   rq   r  rW   rZ   rZ   r[   ra  m  s    ra  c              
   O   s|   t |ddg zt| f||\}}W n. tk
rV } ztdd|W 5 d}~X Y nX |j|jd}|jst| S |S d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  rc  rs   Nr7  )	rb   r1  r  r;   r<   rc  r  r  rS   r:  rZ   rZ   r[   rc    s    rc  c              
   O   s^   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX | 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  re  rs   N)rb   r1  r  r;   r<   re  r3  rZ   rZ   r[   re    s    re  c              
   O   s   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX | \}}|jst|| fS ||fS d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  rf  rs   N)rb   r1  r  r;   r<   rf  r  rS   )rU   rP   rp   r   rq   r  rg  rW   rZ   rZ   r[   rf    s     rf  c           	   
   O   s~   t |dg z t| |ff||\\}}}W n. tk
r\ } ztdd|W 5 d}~X Y nX ||}|jsv| S |S d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  rh  r  N)rb   r1  r"  r;   r<   rh  r  rS   r9  rZ   rZ   r[   rh    s     
rh  c              
   O   sz   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX | }|jsrdd |D S |S d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  ri  rs   Nc                 S   s   g | ]}|  qS rZ   r  r8  rZ   rZ   r[   r   '  s     zdecompose.<locals>.<listcomp>)rb   r1  r  r;   r<   ri  r  r:  rZ   rZ   r[   ri    s    ri  c              
   O   s   t |ddg zt| f||\}}W n. tk
rV } ztdd|W 5 d}~X Y nX |j|jd}|jszdd |D S |S d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  rm  rs   Nr7  c                 S   s   g | ]}|  qS rZ   r  r8  rZ   rZ   r[   r   E  s     zsturm.<locals>.<listcomp>)rb   r1  r  r;   r<   rm  r  r  r:  rZ   rZ   r[   rm  ,  s    rm  c              
   O   sz   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX | }|jsrdd |D S |S d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  rn  rs   Nc                 S   s   g | ]\}}|  |fqS rZ   r  rp  rZ   rZ   r[   r   t  s     zgff_list.<locals>.<listcomp>)rb   r1  r  r;   r<   rn  r  )rU   rP   rp   r   rq   r  rv  rZ   rZ   r[   rn  J  s     rn  c                 O   s   t ddS )z3Compute greatest factorial factorization of ``f``. zsymbolic falling factorialNr1  rU   rP   rp   rZ   rZ   r[   gffy  s    rb  c           	   
   O   s   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX | \}}}|jst|| | fS t|||fS d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  rr  rs   N)	rb   r1  r  r;   r<   rr  r  r   rS   )	rU   rP   rp   r   rq   r  r(  rV   r  rZ   rZ   r[   rr    s    rr  c              
   O   st   t |dg zt| f||\}}W n. tk
rT } ztdd|W 5 d}~X Y nX | }|jsl| S |S d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  rs  rs   N)rb   r1  r  r;   r<   rs  r  rS   r:  rZ   rZ   r[   rs    s    rs  c                 C   s&   |dkrdd }ndd }t | |dS )z&Sort a list of ``(expr, exp)`` pairs. r  c                 S   s.   | \}}|j j }|t|t|jt|j|fS rx   r_   ru   rP   rf   r   rv   r   expr_   rZ   rZ   r[   r    s    z_sorted_factors.<locals>.keyc                 S   s.   | \}}|j j }t|t|j|t|j|fS rx   rc  rd  rZ   rZ   r[   r    s    r  )r  )rv  methodr  rZ   rZ   r[   _sorted_factors  s    
rg  c                 C   s   t dd | D  S )z*Multiply a list of ``(expr, exp)`` pairs. c                 S   s   g | ]\}}|  | qS rZ   r  r   rU   r  rZ   rZ   r[   r     s     z$_factors_product.<locals>.<listcomp>)r   rv  rZ   rZ   r[   _factors_product  s    rj  c                    s
  t jg  }dd t| D }|D ]}|jsBt|trNt|rN||9 }q$nV|jr|j	t j
kr|j\} |jr jr||9 }q$|jr| f q$n|t j } zt||\}}W n2 tk
r }	 z|	j f W 5 d}	~	X Y q$X t||d }
|
 \}}|t jk	rL jr$||  9 }n(|jr<| f n||t jf  t jkrd| q$ jr fdd|D  q$g }|D ]8\}}| jr||  f n|||f qt| f q$|dkrfdddd	 D D |fS )
z.Helper function for :func:`_symbolic_factor`. c                 S   s"   g | ]}t |d r| n|qS )_eval_factor)r   rk  r   r   rZ   rZ   r[   r     s   z)_symbolic_factor_list.<locals>.<listcomp>NZ_listc                    s   g | ]\}}||  fqS rZ   rZ   rh  )re  rZ   r[   r     s     r  c                    s(   g | ]  t t fd dD  fqS )c                 3   s   | ]\}}| kr|V  qd S rx   rZ   )r   rU   r   r  rZ   r[   rF    s      z3_symbolic_factor_list.<locals>.<listcomp>.<genexpr>)r   r   )r   ri  rm  r[   r     s   c                 S   s   h | ]\}}|qS rZ   rZ   )r   r   r   rZ   rZ   r[   	<setcomp>	  s     z(_symbolic_factor_list.<locals>.<setcomp>)r   r=  r   	make_argsr-  rM   r   r   is_PowbaseZExp1rp   r%  r  r;   r|   rR   r  Zis_positiver'  
is_integerrS   rj  )r|   rq   rf  r   rp   argrq  r   r   r  rY   Z_coeffZ_factorsr  rU   r  rZ   )re  rv  r[   _symbolic_factor_list  sX    

"


rt  c                    s   t | trFt| dr|  S tt| d d \}}t|t|S t| drl| j fdd| j	D  S t| dr| 
 fdd| D S | S d	S )
z%Helper function for :func:`_factor`. rk  fraction)ru  rp   c                    s   g | ]}t | qS rZ   _symbolic_factorr   rs  rf  rq   rZ   r[   r     s     z$_symbolic_factor.<locals>.<listcomp>rS  c                    s   g | ]}t | qS rZ   rv  rx  ry  rZ   r[   r     s     N)rM   r   r   rk  rt  rD   r   rj  rY   rp   r   )r|   rq   rf  r   rv  rZ   ry  r[   rw    s    



rw  c                 C   sN  t |ddg t ||}t| } t| ttfr>t| trJ| d }}nt|  \}}t	|||\}}t	|||\}	}
|
r|j
std|  |ddi}||
fD ]:}t|D ],\}\}}|jst||\}}||f||< qqt||}t|
|}
|jsdd |D }d	d |
D }
||	 }|j
s2||fS |||
fS ntd|  d
S )z>Helper function for :func:`sqf_list` and :func:`factor_list`. fracr  rs   za polynomial expected, got %sr  Tc                 S   s   g | ]\}}|  |fqS rZ   r  rh  rZ   rZ   r[   r   <  s     z(_generic_factor_list.<locals>.<listcomp>c                 S   s   g | ]\}}|  |fqS rZ   r  rh  rZ   rZ   r[   r   =  s     N)rb   r1  rc   r!   rM   r   rN   rD   rO  rt  rz  r8   cloner   rk   r  rg  r  )r|   rP   rp   rf  rq   Znumerr_  r  fpr  ZfqZ_optrv  r   rU   r  r   r   rZ   rZ   r[   _generic_factor_list  s6    



r}  c                 C   s<   | dd}t|g  t||}||d< tt| ||S )z4Helper function for :func:`sqf` and :func:`factor`. ru  T)r^  rb   r1  rc   rw  r!   )r|   rP   rp   rf  ru  rq   rZ   rZ   r[   _generic_factorI  s
    r~  c                    s   ddl m  d fdd	}d fdd	}dd	 }|  jr|| r|  }|| |}|rp|d |d
 d|d fS || |}|rd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                    s@  t | jdkr| jd js"d| fS |  }|  }|p<| }| dd } fdd|D }t |dkr<|d r< |d |d  }g }tt |D ]2} || ||d   }|js q<|	| q d| }	| jd }
|
| g}td|d D ]"}|	||d  |
||    qt
| } 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``.
        rs   r   Nc                    s   g | ]} |qS rZ   rZ   )r   coeffxr  rZ   r[   r     s     z<to_rational_coeffs.<locals>._try_rescale.<locals>.<listcomp>r+  )ru   rP   r]  r!  r)  rc  r   r   rK  r%  r	   rN   )rU   f1r   rQ  r   Z
rescale1_xZcoeffs1r   r  Z	rescale_xr   r  r  rZ   r[   _try_rescalez  s0    

 
z(to_rational_coeffs.<locals>._try_rescalec           	         s   t | jdkr| jd js"d| fS |  }|p4| }| dd } |d }|jr|jst|j	dd dd\}}|j
|  | }||}||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``.
        rs   r   Nc                 S   s
   | j dkS NTrJ  )zrZ   rZ   r[   r    r  z<to_rational_coeffs.<locals>._try_translate.<locals>.<lambda>Tbinary)ru   rP   r]  r!  rc  r   is_AddrK  rK   rp   rY   rj  )	rU   r  r   r   r   ratZnonratalphaf2r  rZ   r[   _try_translate  s     

z*to_rational_coeffs.<locals>._try_translatec                 S   sp   |   }d}|D ]Z}t|D ]J}t|j}dd | D }|sDqt|dkrTd}t|dkr  dS qq|S )zS
        Return True if ``f`` is a sum with square roots but no other root
        Fc                 S   s,   g | ]$\}}|j r|jr|jd kr|jqS )r  )r   Zis_Rationalr  )r   rB  ZwxrZ   rZ   r[   r     s
      
zAto_rational_coeffs.<locals>._has_square_roots.<locals>.<listcomp>r  T)r   r	   ro  r   rv  r   minr  )rb  r   Zhas_sqr   r   rU   r  rZ   rZ   r[   _has_square_roots  s    
z-to_rational_coeffs.<locals>._has_square_rootsrs   r  )N)N)sympy.simplify.simplifyr  r   r   rc  )rU   r  r  r  r  r  rZ   r  r[   to_rational_coeffsR  s    &"

r  c              	   C   s  ddl m} t| |dd}| }t|}|s2dS |\}}}}	t|	 }
|r||
d | ||  }|d| }g }|
dd d D ],}|||d ||| i|d f qnF|
d }g }|
dd d D ](}||d ||| i|d f q||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  ZEXr   Nrs   )	r  r  rN   r!  r  rx  rS   r%  r   )rb  r   r  p1r   resrQ  r  rQ  rV   rv  r   r1rA  r  rZ   rZ   r[   _torational_factor_list  s&    ,&r  c                 O   s   t | ||d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)])

    r  rf  r}  ra  rZ   rZ   r[   rt    s    rt  c                 O   s   t | ||d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

    r  r  )r~  ra  rZ   rZ   r[   r    s    r  c                 O   s   t | ||d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  ra  rZ   rZ   r[   rx  !  s    rx  )rY  c          
   
      s   t | } |rt fdd}t| |} i }| tt}|D ]0}t|f }|jsX|jr8||kr8|||< q8| |S zt	|  ddW S  t
k
r }	 z"| jst|  W Y S t
|	W 5 d}	~	X Y nX dS )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                    s$   t | f }|js|jr |S | S )zS
            Factor, but avoid changing the expression when unable to.
            )r  is_Mulrp  )r|   r  r@  rZ   r[   _try_factory  s    zfactor.<locals>._try_factorr  r  N)r!   r$   Zatomsr   r	   r  r  rp  xreplacer~  r8   r  r   )
rU   rY  rP   rp   r  ZpartialsZmuladdrb  r  msgrZ   r@  r[   r  3  s"    D	


r  c              	   C   s4  t | dsFzt| } W n tk
r.   g  Y S X | j||||||dS t| dd\}}	t|	jdkrhtt|D ]\}
}|j	j	||
< qp|dk	r|	j
|}|dkrtd|dk	r|	j
|}|dk	r|	j
|}t||	j
|||||d	}g }|D ]8\\}}}|	j
||	j
| }}|||f|f q|S d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)]

    rS  r|  r)   r   rs   Nr   rz  )r}  r~  r  r  r  )r   rN   r7   r{  r"  ru   rP   r9   r   r_   r   r   r   rE   r   r%  )r   ru  r}  r~  r  r  r  r  r  rq   r   r   r{  rW   r(  rQ  r   rZ   rZ   r[   r{    s>    

    r{  c                 C   s^   z&t | }t| t s$|jjs$tdW n  tk
rF   td|  Y nX |j||||||d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)r}  r  r  r  )rN   rM   r   	is_Symbolr8   r7   r  )rU   r(  rQ  r}  r  r  r  r   rZ   rZ   r[   r    s    
r  c                 C   sZ   z*t | dd}t| t s(|jjs(tdW n  tk
rJ   td|  Y nX |j||d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Zgreedyr  z*Cannot count roots of %s, not a polynomialr  )rN   rM   r   r  r8   r7   r  )rU   r~  r  r   rZ   rZ   r[   r    s    r  Tc                 C   sX   z*t | dd}t| t s(|jjs(tdW n  tk
rJ   td|  Y nX |j|d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  r  z1Cannot compute real roots of %s, not a polynomialr  )rN   rM   r   r  r8   r7   r  )rU   r  r   rZ   rZ   r[   r    s    
r  r  r  c                 C   s\   z*t | dd}t| t s(|jjs(tdW n  tk
rJ   td|  Y nX |j|||d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  r  z6Cannot compute numerical roots of %s, not a polynomial)r   r  r  )rN   rM   r   r  r8   r7   r  )rU   r   r  r  r   rZ   rZ   r[   r  (  s    
r  c              
   O   sv   t |g  z2t| f||\}}t| ts<|jjs<tdW n. tk
rl } zt	dd|W 5 d}~X Y nX |
 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}

    r  r  rs   N)rb   r1  r  rM   rN   r   r  r8   r;   r<   r  r3  rZ   rZ   r[   r  G  s    r  c              
   O   s   t |g  z2t| f||\}}t| ts<|jjs<tdW n. tk
rl } zt	dd|W 5 d}~X Y nX |
|}|js| S |S d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

    r  r  rs   N)rb   r1  r  rM   rN   r   r  r8   r;   r<   r  r  rS   )rU   r   rP   rp   r   rq   r  rW   rZ   rZ   r[   r  e  s    
r  )	_signsimpc                   sT  ddl m} ddlm} t|dg t| } |r:|| } i }d|krR|d |d< t| tt	fs| j
szt| tszt| ts~| S t| dd} |  \}}nt| dkr| \}}t|trt|tr|j|d< |j|d	< |dd|d< | |  }}n t| t	rt| S td
|  ddlm  zd|  r8t |||ff||\}	\}
}|	jst| tt	fsv|  W S tj||fW S W n  tk
r }  z| jr|  st|| js| j rt!| j" fdddd\}}dd |D }| j#t$| j#| f|  W Y S g }t%| }t&| |D ]R}t|tt	t'frHq.z|(|t$|f |)  W n t*k
r|   Y nX q.| +t,| W Y S W 5 d}~X Y nX d|
$| }\}}|ddrd|kr|	j-|d< t| tt	fs|| |   S | |  }}|dds.|||fS |t|f||t|f||fS d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  rP   r   zunexpected argument: %sr#  c                    s   | j dko|   S r  )r  hasr  r#  rZ   r[   r    s    zcancel.<locals>.<lambda>r  c                 S   s   g | ]}t |qS rZ   )r  rl  rZ   rZ   r[   r     s     zcancel.<locals>.<listcomp>Nrs   F).r  r  sympy.polys.ringsr  rb   r1  r!   rM   r   r
   r-  r   r   r   rO  ru   rN   rP   r   rF  rS   r   r&  r$  r  r8   Zngensr  r   r=  r  r  r  rK   rp   rY   r  r#   r/  r%   r%  skiprd   r  rg   r   )rU   r  rP   rp   r  r  rq   rb  r  r  r   r   r  r   ncr*  poterl  r  rZ   r#  r[   r    s~    



"
(

r  c              
      s  t |ddg z"t| gt| f||\} W n. tk
r` } ztdd|W 5 d}~X Y nX  j}d} jr|jr|j	s 
d| i d}dd	lm} | j j j\}	}
t|D ](\}}| jj }|	|||< q|d |d
d \}} fdd|D }tt| }|rhzdd |D |  }}W n tk
r\   Y nX || }} jsdd |D | fS ||fS d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xringrs   c                    s   g | ]}t t| qS rZ   rN   rh   rg   r   r  r   rZ   r[   r   #  s     zreduced.<locals>.<listcomp>c                 S   s   g | ]}|  qS rZ   r   r  rZ   rZ   r[   r   (  s     c                 S   s   g | ]}|  qS rZ   r  r  rZ   rZ   r[   r   /  s     )rb   r1  r"  rj   r;   r<   r   r  r  r  r{  r  r  r  rP   r`   r   r   r_   r   r   r  rN   rh   rg   r   r5   r  rS   )rU   r   rP   rp   r  r  r   r   r  _ringr   r   r   r  r  _Q_rrZ   r   r[   r    s6    "
r  c                 O   s   t | f||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   rP   rp   rZ   rZ   r[   r-   4  s    3r-   c                 O   s   t | f||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  rZ   rZ   r[   r  j  s    r  c                   @   s   e Zd ZdZdd Zedd Zedd Zedd	 Z	ed
d Z
edd Zedd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zedd Zd d! Zd(d#d$Zd%d& Zd'S ))r  z%Represents a reduced Groebner basis. c              
      s   t |ddg zt|f||\} W n2 tk
rZ } ztdt||W 5 d}~X Y nX ddlm} | j j	 j
fdd|D }t| jd	} fd
d|D }| | S )z>Compute a reduced Groebner basis for a system of polynomials. r  rf  r-   Nr   )PolyRingc                    s    g | ]}|r  |j qS rZ   )r   r_   r   r   r   )ringrZ   r[   r     s      z)GroebnerBasis.__new__.<locals>.<listcomp>r  c                    s   g | ]}t | qS rZ   )rN   rh   r   rV   r   rZ   r[   r     s     )rb   r1  r"  r;   r<   ru   r  r  rP   r   r`   	_groebnerrf  _new)ro   r   rP   rp   r  r  r  r   rZ   )rq   r  r[   rr     s    "zGroebnerBasis.__new__c                 C   s   t | }t||_||_|S rx   )r   rr   r   _basis_options)ro   basisrb   rv   rZ   rZ   r[   r    s    

zGroebnerBasis._newc                 C   s$   dd | j D }t| t| jj fS )Nc                 s   s   | ]}|  V  qd S rx   r  )r   rb  rZ   rZ   r[   rF    s     z%GroebnerBasis.args.<locals>.<genexpr>)r  r
   r  rP   )r{   r  rZ   rZ   r[   rp     s    zGroebnerBasis.argsc                 C   s   dd | j D S )Nc                 S   s   g | ]}|  qS rZ   r  r  rZ   rZ   r[   r     s     z'GroebnerBasis.exprs.<locals>.<listcomp>)r  rz   rZ   rZ   r[   r!    s    zGroebnerBasis.exprsc                 C   s
   t | jS rx   )rj   r  rz   rZ   rZ   r[   r    s    zGroebnerBasis.polysc                 C   s   | j jS rx   )r  rP   rz   rZ   rZ   r[   rP     s    zGroebnerBasis.gensc                 C   s   | j jS rx   )r  r   rz   rZ   rZ   r[   r     s    zGroebnerBasis.domainc                 C   s   | j jS rx   )r  r`   rz   rZ   rZ   r[   r`     s    zGroebnerBasis.orderc                 C   s
   t | jS rx   )ru   r  rz   rZ   rZ   r[   __len__  s    zGroebnerBasis.__len__c                 C   s    | j jrt| jS t| jS d S rx   )r  r  iterr!  rz   rZ   rZ   r[   rS    s    
zGroebnerBasis.__iter__c                 C   s   | j jr| j}n| j}|| S rx   )r  r  r!  )r{   itemr  rZ   rZ   r[   __getitem__  s    zGroebnerBasis.__getitem__c                 C   s   t | jt| j fS rx   )hashr  r   r  r   rz   rZ   rZ   r[   r     s    zGroebnerBasis.__hash__c                 C   sP   t || jr$| j|jko"| j|jkS t|rH| jt|kpF| jt|kS dS d S )NF)rM   r   r  r  rJ   r  rj   r!  r{   r  rZ   rZ   r[   r    s
    zGroebnerBasis.__eq__c                 C   s
   | |k S rx   rZ   r  rZ   rZ   r[   r    s    zGroebnerBasis.__ne__c                 C   sT   dd }t dgt| j }| jj}| jD ] }|j|d}||r*||9 }q*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                 S   s   t tt| dkS r   )sumr   r(  )monomialrZ   rZ   r[   
single_var  s    z5GroebnerBasis.is_zero_dimensional.<locals>.single_varr   r   )r.   ru   rP   r  r`   r  r4  ru  )r{   r  r.  r`   r   r  rZ   rZ   r[   r    s    

z!GroebnerBasis.is_zero_dimensionalc                    s   | j   j}t|}||kr | S | js.tdt| j} j} |	 |d ddl
m} | j j|\}}t|D ](\}	}
|
 jj }
||
||	< qzt|||} fdd|D }|jsd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   r`   r   r  c                    s   g | ]}t t| qS rZ   r  r  r   rZ   r[   r   .  s     z&GroebnerBasis.fglm.<locals>.<listcomp>c                 S   s   g | ]}|j d dd qS )Tr@  rs   )r<  r  rZ   rZ   r[   r   1  s     )r  r`   r/   r  rd   rj   r  r   r{  r  r  r  rP   r   r   r_   r   r   r,   r  r  )r{   r`   Z	src_orderZ	dst_orderr  r   r  r  r   r   r   r   rZ   r   r[   fglm  s0    
zGroebnerBasis.fglmTc                    sR  t || j}|gt| j }| j  j}d}|rT|jrT|jsT d|	 i d}ddl
m} | j j j\}}	t|D ](\}
}| jj }||||
< q~|d |dd \}} fdd	|D }t t| }|r(zd
d	 |D |  }}W n tk
r   Y nX || }} jsFdd	 |D | fS ||fS d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   r  rs   Nc                    s   g | ]}t t| qS rZ   r  r  r   rZ   r[   r   g  s     z(GroebnerBasis.reduce.<locals>.<listcomp>c                 S   s   g | ]}|  qS rZ   r  r  rZ   rZ   r[   r   l  s     c                 S   s   g | ]}|  qS rZ   r  r  rZ   rZ   r[   r   s  s     )rN   rm   r  rj   r  r   r  r  r{  r  r  r  rP   r`   r   r   r_   r   r   r  rh   rg   r   r5   r  rS   )r{   r|   r  r   r  r   r   r  r  r   r   r  r  r  r  rZ   r   r[   r   6  s2    
zGroebnerBasis.reducec                 C   s   |  |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

        rs   r   )r   )r{   r   rZ   rZ   r[   containsw  s    zGroebnerBasis.containsN)T)rT   r  r  r  rr   r  r  r  rp   r!  r  rP   r   r`   r  rS  r  r   r  r  r  r  r   r  rZ   rZ   rZ   r[   r  |  s6   







B
Ar  c                    s^   t g   fdd t| } | jr8t| f|S dkrHdd< t |} | |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           
         s  g g  }}t | D ]}g g  }}t|D ]b}|jrH| || q,|jr|jjr|jjr|jdkr| |j|	|j q,|| q,|s|| q|d }|dd  D ]}|
|}q|rt| }|jr|
|}n|
t||}|| q|st| |}	nZ|d }	|dd  D ]}|	|}	q(|rnt | }|jr\|	|}	n|	t||}	|	j|ddS )Nr   rs   rP   rZ   )r	   ro  r   r  r%  rp  rq  re  r  r  r   r-  rN   rm   r   r   rF  )
r|   rq   r   Z
poly_termsr`  rv  Zpoly_factorsr  productrW   _polyrp   rZ   r[   r    sJ    

zpoly.<locals>._polyr  F)rb   r1  r!   rk   rN   rc   r  rZ   r  r[   r     s    4r   c           	      C   s   | dk rt d|| f |d |dd  }}|dkrFt|dd\}}t|t| f||f |}|dkr|t|td}nt||}|r|S | 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 %s of index %srs   NT)r   r   )	r   r'   r0   r   r  rw   r   rN   rS   )	r   rU   Kr  r   r  headtailr   rZ   rZ   r[   
named_poly  s    r  )r   )N)N)FNNNFFF)NNFF)NN)T)r  r  T)r  	functoolsr   r   operatorr   typingr   Z
sympy.corer   r   r	   r
   Zsympy.core.basicr   Zsympy.core.decoratorsr   Zsympy.core.exprtoolsr   r   r   Zsympy.core.evalfr   r   r   r   r   Zsympy.core.functionr   Zsympy.core.mulr   r   Zsympy.core.numbersr   r   r   r   Zsympy.core.relationalr   r   Zsympy.core.sortingr   Zsympy.core.symbolr   r    Zsympy.core.sympifyr!   r"   Zsympy.core.traversalr#   r$   Zsympy.logic.boolalgr%   Zsympy.polysr&   rb   Zsympy.polys.constructorr'   Zsympy.polys.domainsr(   r)   r*   Z!sympy.polys.domains.domainelementr+   Zsympy.polys.fglmtoolsr,   Zsympy.polys.groebnertoolsr-   r  Zsympy.polys.monomialsr.   Zsympy.polys.orderingsr/   Zsympy.polys.polyclassesr0   r1   r2   Zsympy.polys.polyerrorsr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   Zsympy.polys.polyutilsr>   r?   r@   rA   rB   rC   Zsympy.polys.rationaltoolsrD   Zsympy.polys.rootisolationrE   Zsympy.utilitiesrF   rG   rH   Zsympy.utilities.exceptionsrI   Zsympy.utilities.iterablesrJ   rK   r  r  Zmpmath.libmp.libhyperrL   r]   rN   r  r  r  r"  r   r,  r!  r#  r"  r)  r4  r8  r  r  r  r  r  r  r  r   rN  rP  rR  rT  rU  rX  r\  rN  r_  rW  r`  r  ra  rc  re  rf  rh  ri  rm  rn  rb  rr  rs  rg  rj  rt  rw  r}  r~  r  r  rt  r  rx  r  r{  r  r  r  r  r  r  r  r  r  r  r   r  rZ   rZ   rZ   r[   <module>   s  4
 "                                 B]
(
^
:
4






"
"



"
&
&
4
$

(
T3
M2
u



-



.

!
:,	,


b7 

+f
;
5
  
Q