B
    [                @   s  d Z ddlmZmZ ddlmZmZmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z? ddl@mAZAmBZBmCZCmDZD ddlEZFddlGZGddlHmIZI ddlJmKZKmLZLmMZM ddlNmOZO ddlEmPZQ ddlRmSZSmTZTmUZU eCG dd deZVeCG dd deVZWeCd d! ZXd"d# ZYeCd$d% ZZd&d' Z[d(d) Z\eCdd*d+Z]eCd,d- Z^eCd.d/ Z_eCd0d1 Z`eCd2d3 ZaeCd4d5 ZbeCd6d7 ZceCd8d9 ZdeCd:d; ZeeCd<d= ZfeCd>d? ZgeCd@dA ZheCdBdC ZieCdDdE ZjeCdFdG ZkeCdHdI ZleCdJdK ZmeCdLdM ZneCdNdO ZoeCdPdQ ZpeCdRdS ZqeCdTdU ZreCddVdWZseCdXdY ZteCddZd[ZueCd\d] ZveCd^d_ ZweCd`da ZxeCdbdc ZyeCddde ZzeCdfdg Z{eCdhdi Z|eCdjdk Z}eCdldm Z~eCdndo ZeCdpdq ZeCdrds Zdtdu Zdvdw Zdxdy Zdzd{ Zd|d} Zd~d Zdd Zdd ZeCdd ZeCdd ZeCdd ZeCdd ZeCdddZeCdddZeCdddZeCdddZeCdddZeCdd ZeCdd ZeCdd ZeCdd ZeCdd Z,eCdd ZeCG dd deZeCdd ZdS )z8User-friendly public interface to polynomial functions.     )print_functiondivision)	SBasicExprIIntegerAddMulDummyTuple)_keep_coeff)Symbol)preorder_traversal)
Relational)sympify)
_sympifyit)
Derivative)BooleanAtom)DMP)basic_from_dict
_sort_gens_unify_gens_dict_reorder_dict_from_expr_parallel_dict_from_expr)together)dup_isolate_real_roots_list)groebner)matrix_fglm)Monomial)monomial_key)OperationNotSupportedDomainErrorCoercionFailedUnificationFailedGeneratorsNeededPolynomialErrorMultivariatePolynomialErrorExactQuotientFailedPolificationFailedComputationFailedGeneratorsError)groupsiftpublic
filldedentN)NoConvergence)FFQQZZ)construct_domain)polyoptions)iterablerangeorderedc                   st  e Zd ZdZddgZd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e	dd Ze	dd Z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*d+ Zed,d- Zd.d/ Zd0d1 Zdkd3d4Z d5d6 Z!d7d8 Z"d9d: Z#d;d< Z$d=d> Z%d?d@ Z&dldAdBZ'dCdD Z(dEdF Z)dGdH Z*dIdJ Z+dKdL Z,dMdN Z-dmdOdPZ.dndQdRZ/dodSdTZ0dpdUdVZ1dqdWdXZ2dYdZ Z3d[d\ Z4d]d^ Z5d_d` Z6dadb Z7drdddeZ8dsdfdgZ9dhdi Z:djdk Z;dldm Z<dtdndoZ=dpdq Z>drds Z?dtdu Z@dvdw ZAdxdy ZBdzd{ ZCd|d} ZDd~d ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOduddZPdvddZQdwddZRdxddZSdd ZTdyddZUdd ZVdd ZWdd ZXdd ZYdzddZZdd Z[d{ddZ\dd Z]dd Z^d|ddZ_d}ddZ`d~ddZadddZbdddZcdd Zddd ZedddÄZfddń ZgddǄ ZhddɄ ZieiZjeiZkddd˄Zldd̈́ ZmdddτZndddфZodddӄZpddՄ Zqddׄ ZrdddلZsddۄ Ztddd݄Zuddd߄Zvdd Zwdd Zxdd Zydd ZzdddZ{dd Z|dd Z}dd Z~dd Zdd Zdd ZdddZdd Zdd Zdd Zdd 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dZedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Zed4d5 Zed6d7 Zed8d9 Zd:d; Zd<d= Zed>ed?d@ Zed>edAdB Zed>edCdD Zed>edEdF Zed>edGdH Zed>edIdJ ZedKedLdM Zed>edNdO Zed>edPdQ Zed>edRdS Zed>edTdU Zed>edVdW Zed>edXdY Zed>edZd[ Zed>ed\d] ZeZeZed^ed_d` Zed>edadb Zdcdd ZeZddedfZddgdhZdidj Z  ZS (  Polya  
    Generic class for representing and operating on polynomial expressions.
    Subclasses Expr class.

    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

    repgensTgn $@c             O   sz   t ||}d|krtdt|tdrPt|tr>| ||S | t	||S n&t
|}|jrj| ||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NotImplementedErrorr7   str
isinstancedict
_from_dict
_from_listlistr   is_Poly
_from_poly
_from_expr)clsr;   r<   argsopt rN   4lib/python3.7/site-packages/sympy/polys/polytools.py__new__o   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)	rC   r   r'   levlenr   rP   r;   r<   )rK   r;   r<   objrN   rN   rO   new   s    

zPoly.newc             O   s   t ||}| ||S )z(Construct a polynomial from a ``dict``. )r?   r@   rE   )rK   r;   r<   rL   rM   rN   rN   rO   	from_dict   s    zPoly.from_dictc             O   s   t ||}| ||S )z(Construct a polynomial from a ``list``. )r?   r@   rF   )rK   r;   r<   rL   rM   rN   rN   rO   	from_list   s    zPoly.from_listc             O   s   t ||}| ||S )z*Construct a polynomial from a polynomial. )r?   r@   rI   )rK   r;   r<   rL   rM   rN   rN   rO   	from_poly   s    zPoly.from_polyc             O   s   t ||}| ||S )z+Construct a polynomial from an expression. )r?   r@   rJ   )rK   r;   r<   rL   rM   rN   rN   rO   	from_expr   s    zPoly.from_exprc             C   s|   |j }|stdt|d }|j}|dkr>t||d\}}n$x"| D ]\}}||||< qHW | jt	|||f| S )z(Construct a polynomial from a ``dict``. z/can't initialize from 'dict' without generatorsrQ   N)rM   )
r<   r&   rS   domainr5   itemsconvertrU   r   rV   )rK   r;   rM   r<   levelrZ   monomcoeffrN   rN   rO   rE      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 )z(Construct a polynomial from a ``list``. z/can't initialize from 'list' without generatorsrQ   z#'list' representation not supportedN)rM   )r<   r&   rS   r(   rZ   r5   rG   mapr\   rU   r   rW   )rK   r;   rM   r<   r]   rZ   rN   rN   rO   rF      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 )z*Construct a polynomial from a polynomial. rZ   T)	__class__rU   r;   r<   fieldrZ   setrJ   as_exprreorder
set_domainto_field)rK   r;   rM   r<   rb   rZ   rN   rN   rO   rI      s    

zPoly._from_polyc             C   s   t ||\}}| ||S )z+Construct a polynomial from an expression. )r   rE   )rK   r;   rM   rN   rN   rO   rJ      s    zPoly._from_exprc             C   s   | j | jfS )z$Allow SymPy to hash Poly instances. )r;   r<   )selfrN   rN   rO   _hashable_content   s    zPoly._hashable_contentc                s   t t|  S )N)superr:   __hash__)rh   )ra   rN   rO   rk      s    zPoly.__hash__c             C   sV   t  }| j}x>tt|D ].}x(|  D ]}|| r(||| jO }P q(W qW || 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}

        )rc   r<   r8   rS   monomsfree_symbolsfree_symbols_in_domain)rh   symbolsr<   ir^   rN   rN   rO   rm      s    
zPoly.free_symbolsc             C   sX   | j jt  }}|jr2x<|jD ]}||jO }qW n"|jrTx|  D ]}||jO }qBW |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;   domrc   is_Compositero   rm   is_EXcoeffs)rh   rZ   ro   genr_   rN   rN   rO   rn     s    zPoly.free_symbols_in_domainc             C   s
   |   fS )z
        Don't mess up with the core.

        Examples
        ========

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

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

        )rd   )rh   rN   rN   rO   rL   6  s    z	Poly.argsc             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   )r<   )rh   rN   rN   rO   ru   G  s    zPoly.genc             C   s   |   S )z#Get the ground domain of ``self``. )
get_domain)rh   rN   rN   rO   rZ   X  s    zPoly.domainc             C   s$   | j | j| jj| jjf| j S )z3Return zero polynomial with ``self``'s properties. )rU   r;   zerorR   rq   r<   )rh   rN   rN   rO   rw   ]  s    z	Poly.zeroc             C   s$   | j | j| jj| jjf| j S )z2Return one polynomial with ``self``'s properties. )rU   r;   onerR   rq   r<   )rh   rN   rN   rO   rx   b  s    zPoly.onec             C   s$   | j | j| jj| jjf| j S )z3Return unit polynomial with ``self``'s properties. )rU   r;   unitrR   rq   r<   )rh   rN   rN   rO   ry   g  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)fg_perFGrN   rN   rO   unifyl  s    z
Poly.unifyc                s  t jsZy&jjjjjjjfS  tk
rX   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rttj j|\}}jjkrXfdd|D }t	ttt|||}	nj}	ntdf j |d f fdd	}
|
||	fS )Nzcan't unify %s with %srQ   c                s   g | ]}  |jjqS rN   )r\   r;   rq   ).0c)rq   r{   rN   rO   
<listcomp>  s    zPoly._unify.<locals>.<listcomp>c                s   g | ]}  |jjqS rN   )r\   r;   rq   )r   r   )rq   r|   rN   rO   r     s    c                sB   |d k	r2|d | ||d d   }|s2| | S  j| f| S )NrQ   )to_sympyrU   )r;   rq   r<   remove)rK   rN   rO   r~     s
    
zPoly._unify.<locals>.per)r   rH   r;   rq   r~   
from_sympyr$   r%   rC   r   r   r<   r   rS   r   to_dictrD   rG   zipr\   ra   )r{   r|   r<   rR   Zf_monomsZf_coeffsr   Zg_monomsZg_coeffsr   r~   rN   )rK   rq   r{   r|   rO   rz     s6    &"	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')

        NrQ   )r<   r;   rq   r   ra   rU   )r{   r;   r<   r   rN   rN   rO   r~     s    zPoly.perc             C   s&   t | jd|i}| | j|jS )z Set the ground domain of ``f``. rZ   )r?   r@   r<   r~   r;   r\   rZ   )r{   rZ   rM   rN   rN   rO   rf     s    zPoly.set_domainc             C   s   | j jS )z Get the ground domain of ``f``. )r;   rq   )r{   rN   rN   rO   rv     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)

        )r?   ZModulusZ
preprocessrf   r2   )r{   modulusrN   rN   rO   set_modulus  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)rv   Zis_FiniteFieldr   Zcharacteristicr'   )r{   rZ   rN   rN   rO   get_modulus  s    zPoly.get_modulusc             C   sN   || j kr>|jr| ||S y| ||S  tk
r<   Y nX |  ||S )z)Internal implementation of :func:`subs`. )r<   	is_numberevalreplacer'   rd   subs)r{   oldrU   rN   rN   rO   
_eval_subs  s    
zPoly._eval_subsc             C   sP   | j  \}}g }x.tt| jD ]}||kr"|| j|  q"W | 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')

        )r<   )r;   r>   r8   rS   r<   appendr~   )r{   JrU   r<   jrN   rN   rO   r>     s    zPoly.excludec             C   s   |dkr$| j r| j| }}ntd||kr0| S || jkr|| jkr|  }|jr\||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 case)r<   zcan't replace %s with %s in %s)is_univariateru   r'   r<   rv   rr   ro   rG   indexr~   r;   )r{   xyrq   r<   rN   rN   rO   r   +  s    
zPoly.replacec             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')

        rN   )rM   z7generators list can differ only up to order of elementsrQ   )r<   )r?   ZOptionsr   r<   rc   r'   rD   rG   r   r   r;   r   r~   r   rq   rS   )r{   r<   rL   rM   r;   rN   rN   rO   re   M  s     zPoly.reorderc             C   s   | j dd}| |}i }xJ| D ]>\}}tdd |d| D rRtd|  ||||d < q$W | 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)nativec             s   s   | ]
}|V  qd S )NrN   )r   rp   rN   rN   rO   	<genexpr>  s    zPoly.ltrim.<locals>.<genexpr>Nzcan't left trim %srQ   )as_dict_gen_to_levelr[   anyr'   r<   rU   r   rV   rS   r;   rq   )r{   ru   r;   r   termsr^   r_   r<   rN   rN   rO   ltrimg  s    
z
Poly.ltrimc          	   G   s   t  }xL|D ]D}y| j|}W n$ tk
rD   td| |f Y qX || qW x6|  D ]*}x$t|D ]\}}||krl|rldS qlW q^W dS )aJ  
        Return ``True`` if ``Poly(f, *gens)`` retains ground domain.

        Examples
        ========

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

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

        z%s doesn't have %s as generatorFT)rc   r<   r   
ValueErrorr,   addrl   	enumerate)r{   r<   indicesru   r   r^   rp   eltrN   rN   rO   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   r"   r~   )r{   resultrN   rN   rO   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')

        rg   )r   r;   rg   r"   r~   )r{   r   rN   rN   rO   rg     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   r"   r~   )r{   r   rN   rN   rO   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)rw   N)rb   Z	composite)rZ   )r5   r   rZ   rr   rV   r<   )r{   rb   rq   r;   rN   rN   rO   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   r"   r~   )r{   r   mnr   r   rN   rN   rO   r     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 rN   )r;   rq   r   )r   r   )r{   rN   rO   r   (  s    zPoly.coeffs.<locals>.<listcomp>)r=   )r;   rt   )r{   r=   rN   )r{   rO   rt     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;   rl   )r{   r=   rN   rN   rO   rl   *  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 rN   )r;   rq   r   )r   r   r   )r{   rN   rO   r   P  s    zPoly.terms.<locals>.<listcomp>)r=   )r;   r   )r{   r=   rN   )r{   rO   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 rN   )r;   rq   r   )r   r   )r{   rN   rO   r   `  s    z#Poly.all_coeffs.<locals>.<listcomp>)r;   
all_coeffs)r{   rN   )r{   rO   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_monoms)r{   rN   rN   rO   r   b  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 rN   )r;   rq   r   )r   r   r   )r{   rN   rO   r     s    z"Poly.all_terms.<locals>.<listcomp>)r;   	all_terms)r{   rN   )r{   rO   r   v  s    zPoly.all_termsc             O   sv   i }xX|   D ]L\}}|||}t|tr4|\}}n|}|r||krN|||< qtd| qW | j|f|pn| 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   rC   tupler'   rV   r<   )r{   funcr<   rL   r   r^   r_   r   rN   rN   rO   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

        )rS   r   )r{   rN   rN   rO   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}

        )rw   N)r;   r   to_sympy_dict)r{   r   rw   rN   rN   rO   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)r{   r   rN   rN   rO   as_list  s    
zPoly.as_listc          	   G   s   |s| j }n~t|dkrt|d tr|d }t| j }xP| D ]D\}}y||}W n$ tk
r|   td| |f Y qBX |||< qBW 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

        rQ   r   z%s doesn't have %s as generator)r<   rS   rC   rD   rG   r[   r   r   r,   r   r;   r   )r{   r<   mappingru   valuer   rN   rN   rO   rd     s    
zPoly.as_exprc             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   r"   r~   )r{   r   rN   rN   rO   r     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   r"   r~   )r{   r   r   rN   rN   rO   r     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can't inject generators over %sinject)front)r;   rq   is_NumericalrH   r#   r   r   r"   ro   r<   rU   )r{   r   rq   r   r<   rN   rN   rO   r   $  s    
zPoly.injectc             G   s   | j j}|jstd| t| jt| }}| jd| |krV| j|d d }}n4| j| d |kr| 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can't eject generators over %sNTFz'can only eject front or back generatorseject)r   )r;   rq   r   r#   rS   r<   rA   r   r   r   r"   rU   )r{   r<   rq   r   kZ_gensr   r   rN   rN   rO   r   I  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   r"   r~   )r{   r   r   rN   rN   rO   r   s  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   r"   r~   )r{   r_   r   rN   rN   rO   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   r"   r~   )r{   r_   r   rN   rN   rO   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   r"   r~   )r{   r_   r   rN   rN   rO   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   r"   r~   )r{   r_   r   rN   rN   rO   r     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   r"   r~   )r{   r_   r   rN   rN   rO   r     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   r"   r~   )r{   r   rN   rN   rO   r     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   r"   r~   )r{   r   rN   rN   rO   r     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   rH   r   rz   r   r;   r   r"   )r{   r|   r}   r~   r   r   r   rN   rN   rO   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   rH   r   rz   r   r;   r   r"   )r{   r|   r}   r~   r   r   r   rN   rN   rO   r   E  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')

        mul)r   rH   r   rz   r   r;   r   r"   )r{   r|   r}   r~   r   r   r   rN   rN   rO   r   d  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   r"   r~   )r{   r   rN   rN   rO   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   r"   r~   )r{   r   r   rN   rN   rO   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)rz   r   r;   r   r"   )r{   r|   r}   r~   r   r   qrrN   rN   rO   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)rz   r   r;   r   r"   )r{   r|   r}   r~   r   r   r   rN   rN   rO   r     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)rz   r   r;   r   r"   )r{   r|   r}   r~   r   r   r   rN   rN   rO   r     s
    
z	Poly.pquoc          
   C   sx   |  |\}}}}t| jdrfy||}W qp tk
rb } z||  | W d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)rz   r   r;   r   r)   rU   rd   r"   )r{   r|   r}   r~   r   r   r   excrN   rN   rO   r     s    (
zPoly.pexquoc             C   s   |  |\}}}}d}|r<|jr<|js<| |  }}d}t| jdrX||\}}	n
t| d|ry| |	  }
}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)
rz   is_Ringis_Fieldrg   r   r;   r   r"   r   r$   )r{   r|   autorq   r~   r   r   r   r   r   QRrN   rN   rO   r   .  s    

zPoly.divc       	      C   s   |  |\}}}}d}|r<|jr<|js<| |  }}d}t| jdrT||}n
t| d|ry| }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)
rz   r   r   rg   r   r;   r   r"   r   r$   )	r{   r|   r   rq   r~   r   r   r   r   rN   rN   rO   r   U  s    
zPoly.remc       	      C   s   |  |\}}}}d}|r<|jr<|js<| |  }}d}t| jdrT||}n
t| d|ry| }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)
rz   r   r   rg   r   r;   r   r"   r   r$   )	r{   r|   r   rq   r~   r   r   r   r   rN   rN   rO   r   z  s    
zPoly.quoc       
   
   C   s   |  |\}}}}d}|r<|jr<|js<| |  }}d}t| jdry||}W q tk
r }	 z|	| 	 |	 W dd}	~	X Y qX n
t
| d|ry| }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)rz   r   r   rg   r   r;   r   r)   rU   rd   r"   r   r$   )
r{   r|   r   rq   r~   r   r   r   r   r   rN   rN   rO   r     s"    (
z
Poly.exquoc             C   s   t |trXt| j}| |  kr*|k rDn n|dk r>|| S |S qtd|||f n2y| jt|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)rC   r   rS   r<   r'   r   r   r   )r{   ru   r   rN   rN   rO   r     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   r"   )r{   ru   r   rN   rN   rO   r     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   r"   )r{   rN   rN   rO   r     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   r"   )r{   rN   rN   rO   r     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
homogenize)r<   homogeneous_orderN)rC   r   	TypeErrortyper<   r   rS   r   r;   r~   r   r"   )r{   srp   r<   rN   rN   rO   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   r"   )r{   rN   rN   rO   r   B  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)rt   r   r;   r   r"   rq   r   )r{   r=   r   rN   rN   rO   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   r"   rq   r   )r{   r   rN   rN   rO   r   s  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

        rt   ECN)r   r;   rt   r"   )r{   r=   rN   rN   rO   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    r<   	exponents)r{   r^   rN   rN   rO   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;   rS   r<   r   r   rG   r`   r   r"   rq   r   )r{   Nr   rN   rN   rO   r     s    
zPoly.nthrQ   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.)rA   )r{   r   r   rightrN   rN   rO   r_     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    rl   r<   )r{   r=   rN   rN   rO   LM  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   )r    rl   r<   )r{   r=   rN   rN   rO   EM  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    r<   )r{   r=   r^   r_   rN   rN   rO   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   )r   r    r<   )r{   r=   r^   r_   rN   rN   rO   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   r"   rq   r   )r{   r   rN   rN   rO   r   8  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   r"   rq   r   )r{   r   rN   rN   rO   r   M  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;   rq   r   r   Onerv   has_assoc_Ringget_ringr   r   r"   r   r~   r   )rh   r\   r{   rq   r_   r   rN   rN   rO   r   b  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\   )rz   r   r   r   r   )rh   r|   r{   rq   r~   abrN   rN   rO   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}xB|D ]:}t|t	krl|\}}n
|d }}|t
|||}qRW ||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	integraterQ   )r   N)getr;   rq   r   rg   r   r~   r   r   r   r   r   r"   )rh   specsrL   r{   r;   specru   r   rN   rN   rO   r     s    



zPoly.integratec             O   s   | ddst| f||S t| jdr|s@| | jjddS | j}xB|D ]:}t|tkrf|\}}n
|d }}|t|| 	|}qLW | |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')

        ZevaluateTdiffrQ   )r   N)r   r   r   r;   r~   r   r   r   r   r   r"   )r{   r   kwargsr;   r   ru   r   rN   rN   rO   r     s    



z	Poly.diffc             C   s\  | }|dkrt |tr@|}x | D ]\}}|||}q$W |S t |ttfr|}t|t|jkrltdx$t	|j|D ]\}}|||}qzW |S d| }	}n
|
|}	t|jdst|dy|j||	}
W nx tk
rL   |st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can't evaluate at %s in %s)r   )rC   rD   r[   r   r   rG   rS   r<   r   r   r   r   r;   r"   r$   r#   rq   r5   rv   Zunify_with_symbolsrf   r\   r~   )rh   r   r   r   r{   r   ru   r   valuesr   r   Za_domainZ
new_domainrN   rN   rO   r   	  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   )r{   r  rN   rN   rO   __call__L	  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)rz   r   rg   r   r;   r  r"   )	r{   r|   r   rq   r~   r   r   r   hrN   rN   rO   r  b	  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)rz   r   rg   r   r;   r  r"   )
r{   r|   r   rq   r~   r   r   r   tr  rN   rN   rO   r  	  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)rz   r   rg   r   r;   r  r"   )r{   r|   r   rq   r~   r   r   r   rN   rN   rO   r  	  s    

zPoly.invertc             C   s2   t | jdr| jt|}n
t| d| |S )ac  
        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 - 1, x).revert(1)
        Traceback (most recent call last):
        ...
        NotReversible: only unity is 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;   r	  r   r"   r~   )r{   r   r   rN   rN   rO   r	  	  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)rz   r   r;   r
  r"   rG   r`   )r{   r|   r}   r~   r   r   r   rN   rN   rO   r
  	  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   )r   )rz   r   r;   r  r"   rG   r`   )	r{   r|   r  r}   r~   r   r   r   r   rN   rN   rO   r  	  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   )r   )r   r;   r  r"   r~   )r{   r   rN   rN   rO   r  !
  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.dispersionr  )r{   r|   r  rN   rN   rO   r  6
  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)r  r  )r{   r|   r  rN   rN   rO   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)rz   r   r;   r  r"   )	r{   r|   r}   r~   r   r   r  cffcfgrN   rN   rO   r  
  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)rz   r   r;   r  r"   )r{   r|   r}   r~   r   r   r   rN   rN   rO   r  
  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)rz   r   r;   r  r"   )r{   r|   r}   r~   r   r   r   rN   rN   rO   r     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;   rq   r\   r   r  r"   r~   )r{   pr   rN   rN   rO   r    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;   rq   r   rg   r   r  r"   r~   )rh   r   r{   r   rN   rN   rO   r  .  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;   r  r"   rq   r   )r{   r   rN   rN   rO   r  K  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;   r  r"   rq   r   r~   )r{   contr   rN   rN   rO   r  `  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)rz   r   r;   r  r"   )r{   r|   r}   r~   r   r   r   rN   rN   rO   r  u  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;   r  r"   rG   r`   r~   )r{   r   rN   rN   rO   r    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;   r  r"   r~   )r{   r   r   rN   rN   rO   r    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;   r  r"   r~   )r{   r  r   Pr   r   r   rN   rN   rO   r    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;   rq   r   rg   r   r!  r"   rG   r`   r~   )rh   r   r{   r   rN   rN   rO   r!    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 rN   )r~   )r   r|   r   )r{   rN   rO   r     s    z!Poly.gff_list.<locals>.<listcomp>)r   r;   r"  r"   )r{   r   rN   )r{   rO   r"    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;   r#  r"   r~   )r{   r   rN   rN   rO   r#    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;   r$  r"   r~   )r{   r   r|   r   rN   rN   rO   r$  '  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;   r%  r"   r~   )r{   r   rN   rN   rO   r%  F  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 rN   )r~   )r   r|   r   )r{   rN   rO   r   v  s    z!Poly.sqf_list.<locals>.<listcomp>)r   r;   r&  r"   rq   r   )r{   allr_   factorsrN   )r{   rO   r&  [  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 rN   )r~   )r   r|   r   )r{   rN   rO   r     s    z)Poly.sqf_list_include.<locals>.<listcomp>)r   r;   r)  r"   )r{   r'  r(  rN   )r{   rO   r)  x  s    
zPoly.sqf_list_includec                sl   t  jdrBy j \}}W qL tk
r>   tj dfgf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_listrQ   c                s   g | ]\}}  ||fqS rN   )r~   )r   r|   r   )r{   rN   rO   r     s    z$Poly.factor_list.<locals>.<listcomp>)	r   r;   r*  r#   r   r   r"   rq   r   )r{   r_   r(  rN   )r{   rO   r*    s    
zPoly.factor_listc                sT   t  jdr8y j }W qB tk
r4    dfg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_includerQ   c                s   g | ]\}}  ||fqS rN   )r~   )r   r|   r   )r{   rN   rO   r     s    z,Poly.factor_list_include.<locals>.<listcomp>)r   r;   r+  r#   r"   )r{   r(  rN   )r{   rO   r+    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   z!'eps' must be a positive rational	intervals)r'  epsinfsupfastsqfc             S   s   | \}}t |t |fS )N)r3   r   )intervalr   r  rN   rN   rO   _real  s    zPoly.intervals.<locals>._realc             S   s@   | \\}}\}}t |tt |  t |tt |  fS )N)r3   r   r   )	rectangleuvr   r  rN   rN   rO   _complex  s    z Poly.intervals.<locals>._complexc             S   s$   | \\}}}t |t |f|fS )N)r3   r   )r2  r   r  r   rN   rN   rO   r3    s    c             S   sH   | \\\}}\}}}t |tt |  t |tt |  f|fS )N)r3   r   r   )r4  r5  r6  r   r  r   rN   rN   rO   r7    s    )	r3   r\   r   r   r;   r,  r"   rG   r`   )r{   r'  r-  r.  r/  r0  r1  r   r3  r7  Z	real_partZcomplex_partrN   rN   rO   r,    s2    



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   z!'eps' must be a positive rationalrQ   refine_root)r-  stepsr0  )is_sqfr'   r3   r\   r   r   r   r;   r8  r"   r   )	r{   r   r  r-  r9  r0  	check_sqfr   TrN   rN   rO   r8    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_imagr3   r\   rG   r`   ZInfinityr   r;   r=  r"   rw   r>  r   )r{   r.  r/  Zinf_realZsup_realreZimcountrN   rN   rO   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)r{   r   rD  rN   rN   rO   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)]

        )rD  F)multipleN)rE  rF  rG  CRootOf
real_rootsr-   )r{   rI  rD  ZrealsrN   rN   rO   rK    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)]

        )rD  F)rI  N)rE  rF  rG  rJ  	all_rootsr-   )r{   rI  rD  rootsrN   rN   rO   rL    s    zPoly.all_roots   2   c       	         sp  ddl m | jrtd|  |  dkr.g S | jjtkrNdd |  D }n| jjt	krdd |  D }ddl
m} ||   fdd|  D }nNfd	d|  D }yd
d |D }W n$ tk
r   td| jj Y nX tjj}tj_zjy>tj|||d|  d d}tttt|fddd}W n& tk
r\   td|f Y nX W d|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]

        r   )signz#can't compute numerical roots of %sc             S   s   g | ]}t |qS rN   )r   )r   r_   rN   rN   rO   r     s    zPoly.nroots.<locals>.<listcomp>c             S   s   g | ]
}|j qS rN   )r   )r   r_   rN   rN   rO   r     s    )ilcmc                s   g | ]}t |  qS rN   )r   )r   r_   )facrN   rO   r     s    c                s   g | ]}|j  d  qS ))r   )Zevalfr@  )r   r_   )r   rN   rO   r     s   c             S   s   g | ]}t j| qS rN   )mpmathZmpc)r   r_   rN   rN   rO   r     s    z!Numerical domain expected, got %sF
   )maxstepscleanuperrorZ	extraprecc                s$   | j r
dnd| jt| j  | j fS )NrQ   r   )imagrealr   )r   )rP  rN   rO   <lambda>  s    zPoly.nroots.<locals>.<lambda>)keyz7convergence to root failed; try n < %s or maxsteps > %sN)Z$sympy.functions.elementary.complexesrP  is_multivariater(   r   r;   rq   r4   r   r3   Zsympy.core.numbersrQ  r   r#   rS  ZmpdpsZ	polyrootsrG   r`   r   sortedr1   )	r{   r   rU  rV  rt   ZdenomsrQ  r]  rM  rN   )rR  r   rP  rO   nroots  sB    


zPoly.nrootsc             C   sT   | j rtd|  i }x8|  d D ](\}}|jr$| \}}||| | < q$W |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 can't compute ground roots of %srQ   )r\  r(   r*  	is_linearr   )r{   rM  factorr   r   r   rN   rN   rO   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 polynomialrQ   z&'n' must an integer and n >= 1, got %sr  )r\  r(   r   
is_Integerr   r   ru   r   r  ra   rY   r   )r{   r   r   r   r  r   rN   rN   rO   nth_power_roots_poly1  s    
zPoly.nth_power_roots_polyc             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)	rz   r   re  r"   r   r   r   r   r`   )r{   r|   rf  rq   r~   r   r   r   cpcqr  r   rN   rN   rO   re  Y  s    



zPoly.cancelc             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_zero)r{   rN   rN   rO   ri  ~  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_one)r{   rN   rN   rO   rj    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{   rN   rN   rO   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;   is_monic)r{   rN   rN   rO   rk    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_primitive)r{   rN   rN   rO   rl    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_ground)r{   rN   rN   rO   rm    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{   rN   rN   rO   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_quadratic)r{   rN   rN   rO   rn    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_monomial)r{   rN   rN   rO   ro    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_homogeneous)r{   rN   rN   rO   rp  +  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;   is_irreducible)r{   rN   rN   rO   rq  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

        rQ   )rS   r<   )r{   rN   rN   rO   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

        rQ   )rS   r<   )r{   rN   rN   rO   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_cyclotomic)r{   rN   rN   rO   rr    s    zPoly.is_cyclotomicc             C   s   |   S )N)r   )r{   rN   rN   rO   __abs__  s    zPoly.__abs__c             C   s   |   S )N)r   )r{   rN   rN   rO   __neg__  s    zPoly.__neg__r|   c             C   sD   |j s:y| j|f| j }W n tk
r8   |  | S X | |S )N)rH   ra   r<   r'   rd   r   )r{   r|   rN   rN   rO   __add__  s    zPoly.__add__c             C   sD   |j s:y| j|f| j }W n tk
r8   ||   S X || S )N)rH   ra   r<   r'   rd   r   )r{   r|   rN   rN   rO   __radd__  s    zPoly.__radd__c             C   sD   |j s:y| j|f| j }W n tk
r8   |  | S X | |S )N)rH   ra   r<   r'   rd   r   )r{   r|   rN   rN   rO   __sub__  s    zPoly.__sub__c             C   sD   |j s:y| j|f| j }W n tk
r8   ||   S X || S )N)rH   ra   r<   r'   rd   r   )r{   r|   rN   rN   rO   __rsub__  s    zPoly.__rsub__c             C   sD   |j s:y| j|f| j }W n tk
r8   |  | S X | |S )N)rH   ra   r<   r'   rd   r   )r{   r|   rN   rN   rO   __mul__  s    zPoly.__mul__c             C   sD   |j s:y| j|f| j }W n tk
r8   ||   S X || S )N)rH   ra   r<   r'   rd   r   )r{   r|   rN   rN   rO   __rmul__  s    zPoly.__rmul__r   c             C   s(   |j r|dkr| |S |  | S d S )Nr   )rc  r   rd   )r{   r   rN   rN   rO   __pow__  s    
zPoly.__pow__c             C   s"   |j s| j|f| j }| |S )N)rH   ra   r<   r   )r{   r|   rN   rN   rO   
__divmod__  s    zPoly.__divmod__c             C   s"   |j s| j|f| j }|| S )N)rH   ra   r<   r   )r{   r|   rN   rN   rO   __rdivmod__  s    zPoly.__rdivmod__c             C   s"   |j s| j|f| j }| |S )N)rH   ra   r<   r   )r{   r|   rN   rN   rO   __mod__  s    zPoly.__mod__c             C   s"   |j s| j|f| j }|| S )N)rH   ra   r<   r   )r{   r|   rN   rN   rO   __rmod__  s    zPoly.__rmod__c             C   s"   |j s| j|f| j }| |S )N)rH   ra   r<   r   )r{   r|   rN   rN   rO   __floordiv__  s    zPoly.__floordiv__c             C   s"   |j s| j|f| j }|| S )N)rH   ra   r<   r   )r{   r|   rN   rN   rO   __rfloordiv__  s    zPoly.__rfloordiv__c             C   s   |   |   S )N)rd   )r{   r|   rN   rN   rO   __div__  s    zPoly.__div__c             C   s   |  |    S )N)rd   )r{   r|   rN   rN   rO   __rdiv__  s    zPoly.__rdiv__otherc          
   C   s   | | }}|j sFy|j||j| d}W n tttfk
rD   dS X |j|jkrVdS |jj|jjkry|jj	|jj|j}W n t
k
r   dS X ||}||}|j|jkS )N)rZ   F)rH   ra   r<   rv   r'   r#   r$   r;   rq   r   r%   rf   )rh   r  r{   r|   rq   rN   rN   rO   __eq__  s     


zPoly.__eq__c             C   s
   | |k S )NrN   )r{   r|   rN   rN   rO   __ne__2  s    zPoly.__ne__c             C   s   | j  S )N)ri  )r{   rN   rN   rO   __nonzero__6  s    zPoly.__nonzero__c             C   s   |s| |kS |  t|S d S )N)
_strict_eqr   )r{   r|   strictrN   rN   rO   eq;  s    zPoly.eqc             C   s   | j ||d S )N)r  )r  )r{   r|   r  rN   rN   rO   neA  s    zPoly.nec             C   s*   t || jo(| j|jko(| jj|jddS )NT)r  )rC   ra   r<   r;   r  )r{   r|   rN   rN   rO   r  D  s    zPoly._strict_eq)NN)N)N)N)N)N)N)FF)F)F)T)T)T)T)r   )N)N)rQ   F)N)N)N)N)F)NT)T)T)T)F)N)N)T)T)F)F)FNNNFF)NNFF)NN)T)TT)TT)rN  rO  T)F)F)F)__name__
__module____qualname____doc__	__slots__is_commutativerH   Z_op_priorityrP   classmethodrU   rV   rW   rX   rY   rE   rF   rI   rJ   ri   rk   propertyrm   rn   rL   ru   rZ   rw   rx   ry   r   rz   r~   rf   rv   r   r   r   r>   r   re   r   r   r   rg   r   r   r   rt   rl   r   r   r   r   r   r   r   r   rd   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   r   r   r   r   r   r   r   Z_eval_derivativeZ
_eval_diffr   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,  r8  rC  rH  rK  rL  r_  rb  rd  re  ri  rj  r:  rk  rl  rm  r`  rn  ro  rp  rq  r   r\  rr  rs  rt  r   NotImplementedru  rv  rw  rx  ry  rz  r{  r|  r}  r~  r  r  r  r  r  __truediv____rtruediv__r  r  r  __bool__r  r  r  __classcell__rN   rN   )ra   rO   r:   =   sp  *3"$"%%%*''%%*"%"''(&K!"%KK#!L%?J(%





r:   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;   )rh   rN   rN   rO   ri   L  s    zPurePoly._hashable_contentc                s   t t|  S )N)rj   r  rk   )rh   )ra   rN   rO   rk   P  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}

        )rn   )rh   rN   rN   rO   rm   S  s    zPurePoly.free_symbolsr  c          
   C   s   | | }}|j sFy|j||j| d}W n tttfk
rD   dS X t|jt|jkr^dS |jj	|jj	kry|jj	
|jj	|j}W n tk
r   dS X ||}||}|j|jkS )N)rZ   F)rH   ra   r<   rv   r'   r#   r$   rS   r;   rq   r   r%   rf   )rh   r  r{   r|   rq   rN   rN   rO   r  h  s     


zPurePoly.__eq__c             C   s   t || jo| jj|jddS )NT)r  )rC   ra   r;   r  )r{   r|   rN   rN   rO   r    s    zPurePoly._strict_eqc                s   t |}|jsZy&| jj| j| j| j| jj|fS  tk
rX   td| |f Y nX t| j	t|j	kr~t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 )Nzcan't unify %s with %sc                sB   |d k	r2|d | ||d d   }|s2| | S  j| f| S )NrQ   )r   rU   )r;   rq   r<   r   )rK   rN   rO   r~     s
    
zPurePoly._unify.<locals>.per)r   rH   r;   rq   r~   r   r$   r%   rS   r<   rC   r   ra   r   r\   )r{   r|   r<   rq   r   r   r~   rN   )rK   rO   rz     s"    &	zPurePoly._unify)r  r  r  r  ri   rk   r  rm   r   r  r  r  rz   r  rN   rN   )ra   rO   r  H  s   r  c             O   s   t ||}t| |S )z+Construct a polynomial from an expression. )r?   r@   _poly_from_expr)exprr<   rL   rM   rN   rN   rO   poly_from_expr  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 )z+Construct a polynomial from an expression. NT)rM   F)r   rC   r   r*   rH   ra   rI   r<   rZ   rF  expandr   rG   r   r[   r5   r`   r   rD   r:   rE   )r  rM   origpolyr;   rl   rt   rZ   rN   rN   rO   r    s2    

r  c             O   s   t ||}t| |S )z(Construct polynomials from expressions. )r?   r@   _parallel_poly_from_expr)exprsr<   rL   rM   rN   rN   rO   parallel_poly_from_expr  s    r  c             C   s  ddl m} t| dkr| \}}t|trt|tr|j||}|j||}||\}}|j|_|j	|_	|j
dkr~d|_
||g|fS t| g  }} g g  }}d}x`t|D ]T\}	}
t|
}
t|
tr|
jr||	 q||	 |jr|
 }
nd}| |
 qW |rt||| d|rBx|D ]}	| |	  | |	< q(W t| |\}}|jsft||| dx$|jD ]}t||rntdqnW g g  }}g }g }xH|D ]@}ttt|  \}}|| || |t| qW |j	}|dkrt||d\|_	}ntt|j|}x,|D ]$}||d|  ||d }q"W g }x@t||D ]2\}}ttt||}t||}|| qZW |j
dkrt||_
||fS )	z(Construct polynomials from expressions. r   )	Piecewise   NTFz&Piecewise generators do not make sense)rM   )$sympy.functions.elementary.piecewiser  rS   rC   r:   ra   rI   r   r<   rZ   rF  rG   r   r   r   rH   r   r  r*   rd   r   r'   r   r[   extendr5   r`   r   rD   rE   bool)r  rM   r  r{   r|   ZorigsZ_exprsZ_polysZfailedrp   r  repsr   Zcoeffs_listZlengthsr   r   r;   rl   rt   rZ   rF  r  rN   rN   rO   r    sv    












r  c             C   s   t | } || kr|| |< | S )z7Add a new ``(key, value)`` pair to arguments ``dict``. )rD   )rL   r[  r   rN   rN   rO   _update_args;  s    r  c             C   s   t | dd} | jr"| }| j}n| j}|s8t| \}}|rL| rFtjS tjS t |ddjs| jrz||jkrzt|  \}}||jkrtjS n4| jst	| j
dkrttd| tt| j
|f t||S )ax  
    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
    ========
    total_degree
    degree_list
    T)r  rQ   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   rH   rd   	is_Numberr  r   Zeror?  r<   rS   rm   r   r0   nextr9   r   r   )r{   ru   r  ZisNumr}   rN   rN   rO   r   E  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, z

    >>> 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   rH   rd   r  r<   r:   r   r   )r{   r<   r  rvrN   rN   rO   r   {  s    (
r   c          
   O   sl   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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)

    rF  r   rQ   N)	r?   allowed_flagsr  r*   r+   r   r   r`   r   )r{   r<   rL   r   rM   r   ZdegreesrN   rN   rO   r     s    r   c          
   O   sd   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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

    rF  r   rQ   N)r=   )r?   r  r  r*   r+   r   r=   )r{   r<   rL   r   rM   r   rN   rN   rO   r     s    r   c          
   O   sl   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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

    rF  r   rQ   N)r=   )r?   r  r  r*   r+   r   r=   rd   )r{   r<   rL   r   rM   r   r^   rN   rN   rO   r     s    r   c          
   O   st   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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

    rF  r   rQ   N)r=   )r?   r  r  r*   r+   r   r=   rd   )r{   r<   rL   r   rM   r   r^   r_   rN   rN   rO   r     s    r   c       
   
   O   s   t |dg y t| |ff||\\}}}W n. tk
r\ } ztdd|W d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)

    rF  r   r  N)r?   r  r  r*   r+   r   rF  rd   )
r{   r|   r<   rL   r   r   rM   r   r   r   rN   rN   rO   r     s     r   c       	   
   O   s~   t |dg y t| |ff||\\}}}W n. tk
r\ } ztdd|W d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

    rF  r   r  N)r?   r  r  r*   r+   r   rF  rd   )	r{   r|   r<   rL   r   r   rM   r   r   rN   rN   rO   r   6  s     
r   c       	   
   O   s   t |dg y t| |ff||\\}}}W n. tk
r\ } ztdd|W dd}~X Y nX y||}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

    rF  r   r  N)	r?   r  r  r*   r+   r   r)   rF  rd   )	r{   r|   r<   rL   r   r   rM   r   r   rN   rN   rO   r   T  s     r   c       	   
   O   s~   t |dg y t| |ff||\\}}}W n. tk
r\ } ztdd|W d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

    rF  r   r  N)r?   r  r  r*   r+   r   rF  rd   )	r{   r|   r<   rL   r   r   rM   r   r   rN   rN   rO   r   w  s     
r   c       
   
   O   s   t |ddg y t| |ff||\\}}}W n. tk
r^ } ztdd|W d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   rF  r   r  N)r   )	r?   r  r  r*   r+   r   r   rF  rd   )
r{   r|   r<   rL   r   r   rM   r   r   r   rN   rN   rO   r     s     r   c       	   
   O   s   t |ddg y t| |ff||\\}}}W n. tk
r^ } ztdd|W d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   rF  r   r  N)r   )	r?   r  r  r*   r+   r   r   rF  rd   )	r{   r|   r<   rL   r   r   rM   r   r   rN   rN   rO   r     s     r   c       	   
   O   s   t |ddg y t| |ff||\\}}}W n. tk
r^ } ztdd|W d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   rF  r   r  N)r   )	r?   r  r  r*   r+   r   r   rF  rd   )	r{   r|   r<   rL   r   r   rM   r   r   rN   rN   rO   r     s     r   c       	   
   O   s   t |ddg y t| |ff||\\}}}W n. tk
r^ } ztdd|W d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   rF  r   r  N)r   )	r?   r  r  r*   r+   r   r   rF  rd   )	r{   r|   r<   rL   r   r   rM   r   r   rN   rN   rO   r     s     r   c             O   s   t |ddg y t| |ff||\\}}}W n~ tk
r } z`t|j\}\}	}
y||	|
\}}W n  tk
r   tdd|Y nX |	||	|fS W dd}~X Y nX |j||j
d\}}|js| | fS ||fS dS )aU  
    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)
    (-x/5 + 3/5, x + 1)

    r   rF  r  r  N)r   )r?   r  r  r*   r5   r  r  rA   r+   r   r   rF  rd   )r{   r|   r<   rL   r   r   rM   r   rZ   r   r   r   r  rN   rN   rO   r    s     &r  c             O   s   t |ddg y t| |ff||\\}}}W n tk
r } zjt|j\}\}	}
y||	|
\}}}W n  tk
r   tdd|Y nX |	||	||	|fS W dd}~X Y nX |j||j
d\}}}|js| | | fS |||fS d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 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)
    (-x/5 + 3/5, x**2/5 - 6*x/5 + 2, x + 1)

    r   rF  r  r  N)r   )r?   r  r  r*   r5   r  r  rA   r+   r   r   rF  rd   )r{   r|   r<   rL   r   r   rM   r   rZ   r   r   r   r  r  rN   rN   rO   r  D  s     .r  c             O   s   t |ddg y t| |ff||\\}}}W nh tk
r } zJt|j\}\}	}
y|||	|
S  tk
r   t	dd|Y nX W d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
    >>> from sympy.core.numbers import 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 ``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   rF  r  r  N)r   )r?   r  r  r*   r5   r  r   r  rA   r+   r   rF  rd   )r{   r|   r<   rL   r   r   rM   r   rZ   r   r   r  rN   rN   rO   r  k  s      $r  c       	   
   O   s   t |dg y t| |ff||\\}}}W n. tk
r\ } ztdd|W d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]

    rF  r
  r  Nc             S   s   g | ]}|  qS rN   )rd   )r   r   rN   rN   rO   r     s    z!subresultants.<locals>.<listcomp>)r?   r  r  r*   r+   r
  rF  )	r{   r|   r<   rL   r   r   rM   r   r   rN   rN   rO   r
    s     
r
  c          
   O   s   | dd}t|dg y t| |ff||\\}}}W n. tk
rh } ztdd|W dd}~X Y nX |r|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  FrF  r  r  N)r  c             S   s   g | ]}|  qS rN   )rd   )r   r   rN   rN   rO   r     s    zresultant.<locals>.<listcomp>)	popr?   r  r  r*   r+   r  rF  rd   )r{   r|   r<   rL   r  r   r   rM   r   r   r   rN   rN   rO   r    s      
r  c          
   O   st   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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

    rF  r  rQ   N)r?   r  r  r*   r+   r  rF  rd   )r{   r<   rL   r   rM   r   r   rN   rN   rO   r    s    r  c             O   s   t |dg y t| |ff||\\}}}W n tk
r } zjt|j\}\}	}
y||	|
\}}}W n  tk
r   tdd|Y nX |	||	||	|fS W d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)

    rF  r  r  N)r?   r  r  r*   r5   r  r  rA   r+   r   rF  rd   )r{   r|   r<   rL   r   r   rM   r   rZ   r   r   r  r  r  rN   rN   rO   r    s     .r  c          
      s  t | } fdd}|| }|dk	r*|S tdg yt| f\}}t| dkrtdd | D r| d   fd	d
| dd D }tdd |D rd}x|D ]}	t||	 d }qW  | S W nJ tk
r }
 z*||
j	}|dk	r|S t
dt| |
W dd}
~
X Y nX |s:|js.tjS td|dS |d |dd  }}x"|D ]}||}|jrVP qVW |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                sn   sj sjt | \}}|s|jS |jrj|d |dd   }}x$|D ]}|||}||r@P q@W ||S d S )Nr   rQ   )r5   rw   r   r  rj  r   )seqrZ   numbersr   number)rL   r<   rN   rO   try_non_polynomial_gcd;  s    


z(gcd_list.<locals>.try_non_polynomial_gcdNrF  rQ   c             s   s   | ]}|j o|jV  qd S )N)is_algebraicis_irrational)r   r   rN   rN   rO   r   Y  s    zgcd_list.<locals>.<genexpr>r   c                s   g | ]} |   qS rN   )ratsimp)r   r   )r   rN   rO   r   [  s    zgcd_list.<locals>.<listcomp>c             s   s   | ]}|j V  qd S )N)is_rational)r   frcrN   rN   rO   r   \  s    r   gcd_list)rM   )r   r?   r  r  rS   r'  r  as_numer_denomr*   r  r+   rF  r   r  r:   r  rj  rd   )r  r<   rL   r  r   rF  rM   lstlcr  r   r  rN   )r   rL   r<   rO   r  *  sB    

"

r  c             O   sF  t | dr,|dk	r|f| }t| f||S |dkr<tdt|dg ylt| |ff||\\}}}tt| |f\}}|jr|j	r|jr|j	r|| 
 }	|	jr||	 d  S W nl tk
r" }
 zLt|
j\}\}}y||||S  tk
r   tdd|
Y nX W dd}
~
X Y nX ||}|js>| 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 argumentsrF  r   r  r  )r   r  r   r?   r  r  r`   r   r  r  r  r  r  r*   r5   r  r   r  rA   r+   rF  rd   )r{   r|   r<   rL   r   r   rM   r   r   r  r   rZ   r   rN   rN   rO   r  ~  s0    

$
r  c          
      s  t | } fdd}|| }|dk	r*|S tdg yt| f\}}t| dkrtdd | D r| d   fd	d
| dd D }tdd |D rd}x|D ]}	t||	 d }qW  | S W nJ tk
r }
 z*||
j	}|dk	r|S t
dt| |
W dd}
~
X Y nX |s:|js.tjS td|dS |d |dd  }}x|D ]}||}qVW |jsz| 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

    c                sb   s^ s^t | \}}|s|jS |jr^|d |dd   }}x|D ]}|||}q@W ||S d S )Nr   rQ   )r5   rx   r   r  r   )r  rZ   r  r   r  )rL   r<   rN   rO   try_non_polynomial_lcm  s    

z(lcm_list.<locals>.try_non_polynomial_lcmNrF  rQ   c             s   s   | ]}|j o|jV  qd S )N)r  r  )r   r   rN   rN   rO   r     s    zlcm_list.<locals>.<genexpr>r   c                s   g | ]} |   qS rN   )r  )r   r   )r   rN   rO   r     s    zlcm_list.<locals>.<listcomp>c             s   s   | ]}|j V  qd S )N)r  )r   r  rN   rN   rO   r     s    lcm_list)rM   r   )r   r?   r  r  rS   r'  r  r  r*   r  r+   rF  r   r   r:   rd   )r  r<   rL   r  r   rF  rM   r  r  r  r   r  rN   )r   rL   r<   rO   r    s>    

"
r  c             O   sF  t | dr,|dk	r|f| }t| f||S |dkr<tdt|dg ylt| |ff||\\}}}tt| |f\}}|jr|j	r|jr|j	r|| 
 }	|	jr||	 d  S W nl tk
r" }
 zLt|
j\}\}}y||||S  tk
r   tdd|
Y nX W dd}
~
X Y nX ||}|js>| 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

    r  Nz2lcm() takes 2 arguments or a sequence of argumentsrF  rQ   r  r  )r   r  r   r?   r  r  r`   r   r  r  r  r  r  r*   r5   r  r   r  rA   r+   rF  rd   )r{   r|   r<   rL   r   r   rM   r   r   r  r   rZ   r   rN   rN   rO   r    s0    

$
r  c          
      s  ddl m} t| }t| tr$| jr(|S  ddrr| j fdd| jD  } 	d d d< t
|f S t| |r| S  	dd	}t d
g yt| f \}}W n$ tk
r }	 z|	jS d}	~	X Y nX |
 \}
} |jjr(|jjr| jd	d\}} |  \}} |jjr.|| }ntj}tdd t| j|
D  }|dkrftj}|dkrf|S |r~t|||   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

    r   )EqualitydeepFc                s   g | ]}t |f qS rN   )r   )r   r   )rL   r<   rN   rO   r   {  s    zterms_gcd.<locals>.<listcomp>r  clearTrF  N)r\   c             S   s   g | ]\}}|| qS rN   rN   )r   r   r   rN   rN   rO   r     s    rQ   )r  )sympy.core.relationalr  r   rC   r   is_Atomr   r   rL   r  r   r?   r  r  r*   r  rZ   r   r   r   r  r   r   r
   r   r<   r   rd   Zas_coeff_Mul)r{   r<   rL   r  r  rU   r  r   rM   r   r   denomr_   termrN   )rL   r<   rO   r   2  sD    B







r   c          
   O   s|   t |ddg yt| f||\}}W n. tk
rV } ztdd|W d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   rF  r  rQ   N)	r?   r  r  r*   r+   r  r   rF  rd   )r{   r  r<   rL   r   rM   r   r   rN   rN   rO   r    s    r  c          
   O   s|   t |ddg yt| f||\}}W n. tk
rV } ztdd|W d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   rF  r  rQ   N)r   )	r?   r  r  r*   r+   r  r   rF  rd   )r{   r<   rL   r   rM   r   r   rN   rN   rO   r    s    r  c          
   O   s^   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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

    rF  r  rQ   N)r?   r  r  r*   r+   r  )r{   r<   rL   r   rM   r   rN   rN   rO   r    s    r  c          
   O   s   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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)

    rF  r  rQ   N)r?   r  r  r*   r+   r  rF  rd   )r{   r<   rL   r   rM   r   r  r   rN   rN   rO   r    s     r  c       	   
   O   s~   t |dg y t| |ff||\\}}}W n. tk
r\ } ztdd|W d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

    rF  r  r  N)r?   r  r  r*   r+   r  rF  rd   )	r{   r|   r<   rL   r   r   rM   r   r   rN   rN   rO   r  )  s     
r  c          
   O   sz   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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]

    rF  r  rQ   Nc             S   s   g | ]}|  qS rN   )rd   )r   r   rN   rN   rO   r   `  s    zdecompose.<locals>.<listcomp>)r?   r  r  r*   r+   r  rF  )r{   r<   rL   r   rM   r   r   rN   rN   rO   r  G  s    r  c          
   O   s   t |ddg yt| f||\}}W n. tk
rV } ztdd|W d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   rF  r!  rQ   N)r   c             S   s   g | ]}|  qS rN   )rd   )r   r   rN   rN   rO   r   ~  s    zsturm.<locals>.<listcomp>)r?   r  r  r*   r+   r!  r   rF  )r{   r<   rL   r   rM   r   r   rN   rN   rO   r!  e  s    r!  c          
   O   sz   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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)).expand() == 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

    rF  r"  rQ   Nc             S   s   g | ]\}}|  |fqS rN   )rd   )r   r|   r   rN   rN   rO   r     s    zgff_list.<locals>.<listcomp>)r?   r  r  r*   r+   r"  rF  )r{   r<   rL   r   rM   r   r(  rN   rN   rO   r"    s     r"  c             O   s   t ddS )z3Compute greatest factorial factorization of ``f``. zsymbolic falling factorialN)rA   )r{   r<   rL   rN   rN   rO   gff  s    r  c       	   
   O   s   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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)

    rF  r$  rQ   N)	r?   r  r  r*   r+   r$  rF  r   rd   )	r{   r<   rL   r   rM   r   r   r|   r   rN   rN   rO   r$    s    r$  c          
   O   st   t |dg yt| f||\}}W n. tk
rT } ztdd|W d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

    rF  r%  rQ   N)r?   r  r  r*   r+   r%  rF  rd   )r{   r<   rL   r   rM   r   r   rN   rN   rO   r%    s    r%  c             C   s&   |dkrdd }ndd }t | |dS )z&Sort a list of ``(expr, exp)`` pairs. r1  c             S   s&   | \}}|j j }|t|t|j|fS )N)r;   rS   r<   )rT   r  expr;   rN   rN   rO   r[    s    z_sorted_factors.<locals>.keyc             S   s&   | \}}|j j }t|t|j||fS )N)r;   rS   r<   )rT   r  r  r;   rN   rN   rO   r[     s    )r[  )r^  )r(  methodr[  rN   rN   rO   _sorted_factors  s    
r  c             C   s   t dd | D  S )z*Multiply a list of ``(expr, exp)`` pairs. c             S   s   g | ]\}}|  | qS rN   )rd   )r   r{   r   rN   rN   rO   r   
  s    z$_factors_product.<locals>.<listcomp>)r
   )r(  rN   rN   rO   _factors_product  s    r  c                s  t jg  }}dd t| D }x|D ]}|jr>||9 }q(|jrR||j q(|jr|j\} |jrx jrx||9 }q(|jr|	| f q(n|t j } yt
||\}}	W n2 tk
r }
 z|	|
j f W dd}
~
X Y q(X t||d }| \}}|t jk	rD jr||  9 }n(|jr4|	| f n|	|t jf  t jkr\|| q( jr~| fdd|D  q(g }x@|D ]8\}}| jr|	||  f n|	||f qW |	t| f q(W ||fS )z.Helper function for :func:`_symbolic_factor`. c             S   s"   g | ]}t |d r| n|qS )_eval_factor)r   r  )r   rp   rN   rN   rO   r     s   z)_symbolic_factor_list.<locals>.<listcomp>NZ_listc                s   g | ]\}}||  fqS rN   rN   )r   r{   r   )r  rN   rO   r   8  s    )r   r   r
   	make_argsr  is_Mulr  rL   is_Powr   r  r*   r  getattrrc  Zis_positive
is_integerrd   r  )r  rM   r  r_   r(  rL   argbaser  r}   r   r   Z_coeffZ_factorsr  r{   r   rN   )r  rO   _symbolic_factor_list  sT    
"
r  c                s   t | trD| jsDt| dr"|  S tt|  \}}t|t|S t| drj| j	 fdd| j
D  S t| dr|  fdd| D S | S dS )z%Helper function for :func:`_factor`. r  rL   c                s   g | ]}t | qS rN   )_symbolic_factor)r   r  )r  rM   rN   rO   r   O  s    z$_symbolic_factor.<locals>.<listcomp>r  c                s   g | ]}t | qS rN   )r  )r   r  )r  rM   rN   rO   r   Q  s    N)rC   r   is_Relationalr   r  r  r   r   r  r   rL   ra   )r  rM   r  r_   r(  rN   )r  rM   rO   r  G  s    


r  c             C   sF  t |ddg t ||}t| } t| tr6| js6t|  \}}t	|||\}}t	|||\}	}
|
r~|j
s~td|  |tdd}xJ||
fD ]>}x8t|D ],\}\}}|jst||\}}||f||< qW qW t||}t|
|}
|jsdd |D }dd |
D }
||	 }|j
s*||fS |||
fS ntd|  d	S )
z>Helper function for :func:`sqf_list` and :func:`factor_list`. fracrF  za polynomial expected, got %sT)r  c             S   s   g | ]\}}|  |fqS rN   )rd   )r   r{   r   rN   rN   rO   r   r  s    z(_generic_factor_list.<locals>.<listcomp>c             S   s   g | ]\}}|  |fqS rN   )rd   )r   r{   r   rN   rN   rO   r   s  s    N)r?   r  r@   r   rC   r   r  r   r  r  r  r'   clonerD   r   rH   r  r  rF  )r  r<   rL   r  rM   Znumerr  rg  fprh  ZfqZ_optr(  rp   r{   r   r}   r_   rN   rN   rO   _generic_factor_listV  s2    


r  c             C   s(   t |g  t ||}tt| ||S )z4Helper function for :func:`sqf` and :func:`factor`. )r?   r  r@   r  r   )r  r<   rL   r  rM   rN   rN   rO   _generic_factor  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*sqrt(2) + 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                sB  ddl m} t| jdkr&| jd js.d| fS |  }|  }|pH| }| dd } fdd|D }|d r> |d |d  }g }xt	t|D ].} || ||d   }	|	j
sP ||	 qW  d| }
| jd }|| g}x4t	d|d D ]"}|||d  |||    qW || } t| } ||
| fS dS )	a$  
        try rescaling ``x -> alpha*x`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the rescaling is successful,
        ``alpha`` is the rescaling factor, and ``f`` is the rescaled
        polynomial; else ``alpha`` is ``None``.
        r   )r	   rQ   Nc                s   g | ]} |qS rN   rN   )r   coeffx)r  rN   rO   r     s    z<to_rational_coeffs.<locals>._try_rescale.<locals>.<listcomp>r   )sympy.core.addr	   rS   r<   r  r   r   r  r   r8   r  r   r:   )r{   f1r	   r   r  rt   Z
rescale1_xZcoeffs1rp   r  Z	rescale_xr   r6  )r  rN   rO   _try_rescale  s2    


"
z(to_rational_coeffs.<locals>._try_rescalec                s   ddl m} t| jdkr&| jd js.d| fS |  }|p@| }| dd } |d }|r|js|}|j	r|j
}|j}n|g}t|dd dd\}}	||	  | }
||
}|
|fS dS )	a+  
        try translating ``x -> x + alpha`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the translating is successful,
        ``alpha`` is the translating factor, and ``f`` is the shifted
        polynomial; else ``alpha`` is ``None``.
        r   )r	   rQ   Nc             S   s   | j S )N)r  )zrN   rN   rO   rZ    s    z<to_rational_coeffs.<locals>._try_translate.<locals>.<lambda>T)binary)r  r	   rS   r<   r  r   r  r   r  is_AddrL   r   r.   r  )r{   r  r	   r   rt   r   r   rL   Zc1Zc2Zalphaf2)r  rN   rO   _try_translate  s$    

z*to_rational_coeffs.<locals>._try_translatec             S   s   ddl m} |  }d}xb|D ]Z}xTt|D ]F}||j}dd | D }|sTq.t|dkrdd}t|dkr.dS q.W qW |S )zS
        Return True if ``f`` is a sum with square roots but no other root
        r   )FactorsFc             S   s,   g | ]$\}}|j r|jr|jd kr|jqS )r  )r   Zis_Rationalr   )r   r   ZwxrN   rN   rO   r     s    zAto_rational_coeffs.<locals>._has_square_roots.<locals>.<listcomp>r  T)	sympy.core.exprtoolsr  rt   r	   r  r(  r[   minmax)r  r  rt   Zhas_sqr   r   r{   r   rN   rN   rO   _has_square_roots  s    

z-to_rational_coeffs.<locals>._has_square_rootsrQ   r  )N)N)sympy.simplify.simplifyr  rv   rs   r  )r{   r  r  r  r  r   rN   )r  rO   to_rational_coeffs  s    &#

r  c          	   C   s  ddl m} t| |dd}| }t|}|s2dS |\}}}}	t|	 }
|r||
d | ||  }|d| }g }x|
dd d D ],}|||d ||| i|d f qW nJ|
d }g }x<|
dd d D ](}||d ||| i|d f qW ||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EX)rZ   NrQ   )	r  r  r:   r   r  r*  rd   r   r   )r  r   r  Zp1r   Zresr  r   r  r|   r(  r   Zr1r   r  rN   rN   rO   _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)])

    r1  )r  )r  )r{   r<   rL   rN   rN   rO   r&  9  s    r&  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

    r1  )r  )r  )r{   r<   rL   rN   rN   rO   r1  K  s    r1  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)])

    ra  )r  )r  )r{   r<   rL   rN   rN   rO   r*  ]  s    r*  c       	   
   O   s   t | } |ddrhi }| tt}x8|D ]0}t|f||}|jsJ|jr*||kr*|||< q*W | |S yt	| ||ddS  t
k
r } z&| jsddlm} || S t
|W d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
    >>> 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)

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

    r  Fra  )r  r   )	factor_ncN)r   r  Zatomsr
   r	   ra  r  r  xreplacer  r'   r  r  r  )	r{   r<   rL   ZpartialsZmuladdr  rR  msgr  rN   rN   rO   ra  o  s     <

ra  Fc          	   C   s8  t | dsByt| } W n tk
r*   g S X | j||||||dS t| dd\}}	t|	jdkrdtx t|D ]\}
}|j	j	||
< qnW |dk	r|	j
|}|dkrtd|dk	r|	j
|}|dk	r|	j
|}t||	j
|||||d	}g }x@|D ]8\\}}}|	j
||	j
| }}|||f|f qW |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)]

    r  )r'  r-  r.  r/  r0  r1  r3   )rZ   rQ   Nr   z!'eps' must be a positive rational)r-  r.  r/  r  r0  )r   r:   r&   r,  r  rS   r<   r(   r   r;   rZ   r\   r   r   r   r   )r   r'  r-  r.  r/  r  r0  r1  rF  rM   rp   r  r,  r   r   r  r   rN   rN   rO   r,    s4    
r,  c             C   sD   yt | }W n  tk
r,   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)

    z+can't refine a root of %s, not a polynomial)r-  r9  r0  r;  )r:   r&   r'   r8  )r{   r   r  r-  r9  r0  r;  r   rN   rN   rO   r8    s    r8  c             C   s@   yt | dd}W n  tk
r0   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)greedyz)can't count roots of %s, not a polynomial)r.  r/  )r:   r&   r'   rC  )r{   r.  r/  r   rN   rN   rO   rC    s
    rC  Tc             C   s>   yt | dd}W n  tk
r0   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]

    F)r  z0can't compute real roots of %s, not a polynomial)rI  )r:   r&   r'   rK  )r{   rI  r   rN   rN   rO   rK  +  s    rK  rN  rO  c             C   sB   yt | dd}W n  tk
r0   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]

    F)r  z5can't compute numerical roots of %s, not a polynomial)r   rU  rV  )r:   r&   r'   r_  )r{   r   rU  rV  r   rN   rN   rO   r_  C  s    r_  c          
   O   s\   t |g  yt| f||\}}W n. tk
rR } ztdd|W d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}

    rb  rQ   N)r?   r  r  r*   r+   rb  )r{   r<   rL   r   rM   r   rN   rN   rO   rb  ]  s    rb  c          
   O   st   t |g  yt| f||\}}W n. tk
rR } ztdd|W dd}~X Y nX ||}|jsl| 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

    rd  rQ   N)r?   r  r  r*   r+   rd  rF  rd   )r{   r   r<   rL   r   rM   r   r   rN   rN   rO   rd  v  s    
rd  c                sp  ddl m} ddlm  t|dg t| } t| tt	fst| j
sVt| tsVt| tsZ| S || dd} |  \}}n4t| dkr| \}}nt| t	r|| S td|  y"t||ff||\\}}}W nH tk
r    t| tt	fs| S tj||fS Y n tk
r }	 z| jr.|  s.t|	| js>| jrt| j fd	d
dd\}
}dd |D }| jt| j|
f| S g }t| }t| xZ|D ]R}t|tt	t frqy|!|t|f |"  W n t#k
r   Y nX qW | $t%|S W dd}	~	X Y nX ||\}
}}t| tt	fsH|
|& |&   S |j'sb|
|& |& fS |
||fS dS )ad  
    Cancel common factors in a rational function ``f``.

    Examples
    ========

    >>> from sympy import cancel, sqrt, Symbol
    >>> 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
    r   )factor_terms)r  rF  T)Zradicalr  zunexpected argument: %sc                s   | j dko|   S )NT)r  has)r   )r  rN   rO   rZ    s    zcancel.<locals>.<lambda>)r  c             S   s   g | ]}t |qS rN   )re  )r   rp   rN   rN   rO   r     s    zcancel.<locals>.<listcomp>N)(r  r  r  r  r?   r  r   rC   r   r   r  r   r   r  rS   r   r  r*   r   r   r'   r  r  r  r  r.   rL   r   re  Z
_from_argsr   r  r   r   skiprA   r  rD   rd   rF  )r{   r<   rL   r  r  r   r   r   rM   r  r   Zncr  Zpoter   r   rN   )r  rO   re    s\    

"
 re  c          
      s  t |ddg y"t| gt| f||\} W n. tk
r` } ztdd|W dd}~X Y nX  j}d} jr|jr|j	s 
t| d d}dd	lm} | j j j\}	}
x4t|D ](\}}| jj }|	|||< qW |d |d
d \}} fdd|D }tt| }|rnydd |D |  }}W n tk
rb   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)

    rF  r   reducedr   NF)rZ   T)xringrQ   c                s   g | ]}t t| qS rN   )r:   rE   rD   )r   r   )rM   rN   rO   r     s    zreduced.<locals>.<listcomp>c             S   s   g | ]}|  qS rN   )r   )r   r   rN   rN   rO   r     s    c             S   s   g | ]}|  qS rN   )rd   )r   r   rN   rN   rO   r   #  s    )r?   r  r  rG   r*   r+   rZ   r   r   r   r  rD   	get_fieldsympy.polys.ringsr  r<   r=   r   rf   r;   r   rV   r   r:   rE   r   r$   rF  rd   )r{   r   r<   rL   rF  r   rZ   r   r  _ringr}   rp   r  r   r   _Q_rrN   )rM   rO   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 `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 ``method`` flag or with the :func:`setup`
    function from :mod:`sympy.polys.polyconfig`:

    >>> 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   r<   rL   rN   rN   rO   r   (  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_dimensional)r   r<   rL   rN   rN   rO   r  ^  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 yt|f||\} W n2 tk
rZ } ztdt||W d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. rF  r  r   Nr   )PolyRingc                s    g | ]}|r  |j qS rN   )rV   r;   r   )r   r  )ringrN   rO   r     s    z)GroebnerBasis.__new__.<locals>.<listcomp>)r  c                s   g | ]}t | qS rN   )r:   rE   )r   r|   )rM   rN   rO   r     s    )r?   r  r  r*   r+   rS   r  r   r<   rZ   r=   	_groebnerr  _new)rK   r   r<   rL   rF  r   r   r   rN   )rM   r  rO   rP   t  s    "zGroebnerBasis.__new__c             C   s   t | }t||_||_|S )N)r   rP   r   _basis_options)rK   basisr?   rT   rN   rN   rO   r    s    

zGroebnerBasis._newc             C   s   t | j t | jj fS )N)r   r  r  r<   )rh   rN   rN   rO   rL     s    zGroebnerBasis.argsc             C   s   dd | j D S )Nc             S   s   g | ]}|  qS rN   )rd   )r   r  rN   rN   rO   r     s    z'GroebnerBasis.exprs.<locals>.<listcomp>)r  )rh   rN   rN   rO   r    s    zGroebnerBasis.exprsc             C   s
   t | jS )N)rG   r  )rh   rN   rN   rO   rF    s    zGroebnerBasis.polysc             C   s   | j jS )N)r  r<   )rh   rN   rN   rO   r<     s    zGroebnerBasis.gensc             C   s   | j jS )N)r  rZ   )rh   rN   rN   rO   rZ     s    zGroebnerBasis.domainc             C   s   | j jS )N)r  r=   )rh   rN   rN   rO   r=     s    zGroebnerBasis.orderc             C   s
   t | jS )N)rS   r  )rh   rN   rN   rO   __len__  s    zGroebnerBasis.__len__c             C   s    | j jrt| jS t| jS d S )N)r  rF  iterr  )rh   rN   rN   rO   r    s    
zGroebnerBasis.__iter__c             C   s   | j jr| j}n| j}|| S )N)r  rF  r  )rh   itemr  rN   rN   rO   __getitem__  s    zGroebnerBasis.__getitem__c             C   s   t | jt| j fS )N)hashr  r   r  r[   )rh   rN   rN   rO   rk     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)rC   ra   r  r  r7   rF  rG   r  )rh   r  rN   rN   rO   r    s
    zGroebnerBasis.__eq__c             C   s
   | |k S )NrN   )rh   r  rN   rN   rO   r    s    zGroebnerBasis.__ne__c             C   sX   dd }t dgt| j }| jj}x*| jD ] }|j|d}||r,||9 }q,W 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 )NrQ   )sumr`   r  )monomialrN   rN   rO   
single_var  s    z5GroebnerBasis.is_zero_dimensional.<locals>.single_varr   )r=   )r    rS   r<   r  r=   rF  r   r'  )rh   r  r   r=   r  r  rN   rN   rO   r    s    z!GroebnerBasis.is_zero_dimensionalc                s   | j   j}t|}||kr | S | js.tdt| j} j} t	|
 |d ddlm} | j j|\}}x4t|D ](\}	}
|
 jj }
||
||	< q~W t|||} 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
        ==========

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

        z>can't convert Groebner bases of ideals with positive dimension)rZ   r=   r   )r  c                s   g | ]}t t| qS rN   )r:   rE   rD   )r   r|   )rM   rN   rO   r   !  s    z&GroebnerBasis.fglm.<locals>.<listcomp>c             S   s   g | ]}|j d dd qS )T)r\   rQ   )r   )r   r|   rN   rN   rO   r   $  s    )r  r=   r!   r  rA   rG   r  rZ   r  rD   r  r  r  r<   r   rf   r;   r   rV   r   r   r  )rh   r=   Z	src_orderZ	dst_orderrF  rZ   r  r  r}   rp   r  r   rN   )rM   rO   fglm  s.    

zGroebnerBasis.fglmTc                sX  t || j}|gt| j }| j  j}d}|rV|jrV|jsV t	|
 d d}ddlm} | j j j\}}	x4t|D ](\}
}| jj }||||
< qW |d |dd \}} fdd	|D }t t	| }|r.yd
d	 |D |  }}W n tk
r"   Y nX || }} jsLd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

        F)rZ   Tr   )r  rQ   Nc                s   g | ]}t t| qS rN   )r:   rE   rD   )r   r   )rM   rN   rO   r   Z  s    z(GroebnerBasis.reduce.<locals>.<listcomp>c             S   s   g | ]}|  qS rN   )r   )r   r   rN   rN   rO   r   _  s    c             S   s   g | ]}|  qS rN   )rd   )r   r   rN   rN   rO   r   f  s    )r:   rJ   r  rG   r  rZ   r   r   r  rD   r  r  r  r<   r=   r   rf   r;   r   rV   r   rE   r   r$   rF  rd   )rh   r  r   r  rF  rZ   r   r  r  r}   rp   r   r   r  r  rN   )rM   rO   reduce)  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

        rQ   r   )r  )rh   r  rN   rN   rO   containsj  s    zGroebnerBasis.containsN)T)r  r  r  r  rP   r  r  r  rL   r  rF  r<   rZ   r=   r  r  r
  rk   r  r  r  r  r  r  rN   rN   rN   rO   r  p  s&   	 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  }}xt | D ]}g g  }}xpt|D ]b}|jrN| || q2|jr|jjr|jjr|jdkr| |j|	|j q2|| q2W |s|| q|d }x|dd  D ]}|
|}qW |rt| }|jr|
|}n|
t||}|| qW |s$t| |}	n^|d }	x |dd  D ]}|	|}	q:W |rt | }|jrp|	|}	n|	t||}	|	j|ddS )Nr   rQ   r<   rN   )r	   r  r
   r  r   r  r  r  rc  r   r   r  r:   rJ   r   re   r   )
r  rM   r   Z
poly_termsr  r(  Zpoly_factorsra  productr   )_polyrL   rN   rO   r    sB    

zpoly.<locals>._polyr  F)r?   r  r   rH   r:   r@   )r  r<   rL   rM   rN   )r  rL   rO   r    s    4r  )r   )N)N)FNNNFFF)NNFF)NN)T)rN  rO  T)r  Z
__future__r   r   Z
sympy.corer   r   r   r   r   r	   r
   r   r   Zsympy.core.mulr   Zsympy.core.symbolr   Zsympy.core.basicr   r  r   Zsympy.core.sympifyr   Zsympy.core.decoratorsr   Zsympy.core.functionr   Zsympy.logic.boolalgr   Zsympy.polys.polyclassesr   Zsympy.polys.polyutilsr   r   r   r   r   r   Zsympy.polys.rationaltoolsr   Zsympy.polys.rootisolationr   Zsympy.polys.groebnertoolsr   r  Zsympy.polys.fglmtoolsr   Zsympy.polys.monomialsr    Zsympy.polys.orderingsr!   Zsympy.polys.polyerrorsr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   Zsympy.utilitiesr-   r.   r/   r0   Zsympy.polysrE  rS  Zmpmath.libmp.libhyperr1   Zsympy.polys.domainsr2   r3   r4   Zsympy.polys.constructorr5   r6   r?   Zsympy.core.compatibilityr7   r8   r9   r:   r  r  r  r  r  r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r!  r"  r  r$  r%  r  r  r  r  r  r  r  r  r&  r1  r*  ra  r,  r8  rC  rK  r_  rb  rd  re  r  r  r  r  rN   rN   rN   rO   <module>   s  , 	4                                *](_
55##   #''4&)T2N2t./":) ,P7'O<6  