o
    8Va/                    @   sD  d Z ddlmZmZ ddl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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# ddl$m%Z% ddl&m'Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z;m<Z<m=Z= ddl>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZI ddlJmKZKmLZLmMZMmNZNmOZOmPZP ddlQmRZR ddlSmTZT ddlUmVZVmWZWmXZXmYZY ddlZm[Z[ ddl&Z\ddl]Z]ddl^m_Z_ d d! Z`eXG d"d# d#eZaeXG d$d% d%eaZbeXd&d' Zcd(d) ZdeXd*d+ Zed,d- Zfd.d/ ZgeXdd0d1ZheXd2d3 ZieXd4d5 ZjeXd6d7 ZkeXd8d9 ZleXd:d; ZmeXd<d= ZneXd>d? ZoeXd@dA ZpeXdBdC ZqeXdDdE ZreXdFdG ZseXdHdI ZteXdJdK ZueXdLdM ZveXdNdO ZweXdPdQ ZxeXdRdS ZyeXdTdUdVdWZzeXdXdY Z{eXdZd[ Z|eXd\d] Z}eXdd^d_Z~eXd`da ZeXddbdcZeXddde ZeXdfdg ZeXdhdi ZeXdjdk ZeXdldm ZeXdndo ZeXdpdq ZeXdrds ZeXdtdu ZeXdvdw ZeXdxdy ZeXdzd{ Zd|d} Zd~d Zdd Zdd Zdd Zdd Zdd Zdd ZeXdd ZeXdd ZeXdd ZeXdTdddZeXdddZeXdddZeXdddZeXdddZeXdddZeXdd ZeXdd ZeXdd ZeXdd ZeXdd Z4eXdd ZeXG dd deZeXdd ZdS )z8User-friendly public interface to polynomial functions.     )wrapsreducemul)	SBasicExprIIntegerAddMulDummyTuple)preorder_traversal)iterableordered)
_sympifyit)pure_complex)
Derivative)_keep_coeff)
Relational)Symbol)sympify_sympify)BooleanAtom)polyoptions)construct_domain)FFQQZZ)DomainElement)matrix_fglm)groebner)Monomial)monomial_key)DMPDMFANP)OperationNotSupportedDomainErrorCoercionFailedUnificationFailedGeneratorsNeededPolynomialErrorMultivariatePolynomialErrorExactQuotientFailedPolificationFailedComputationFailedGeneratorsError)basic_from_dict
_sort_gens_unify_gens_dict_reorder_dict_from_expr_parallel_dict_from_expr)together)dup_isolate_real_roots_list)groupsiftpublic
filldedent)SymPyDeprecationWarningN)NoConvergencec                    s   t   fdd}|S )Nc              	      s   t |}t|tr | |S t|trQz| j|g| jR  }W n, tyK   |jr-t Y S t	| 
  j}||}|turGtddddd  | Y S w  | |S tS )Nz@Mixing Poly with non-polynomial expressions in binary operationsiH  z1.6z.the as_expr or as_poly method to convert types)ZfeatureZissueZdeprecated_since_versionZ
useinstead)r   
isinstancePolyr   	from_exprgensr-   Z	is_MatrixNotImplementedgetattras_expr__name__r?   warn)fgZexpr_methodresultfunc 7/usr/lib/python3/dist-packages/sympy/polys/polytools.pywrapper=   s.   



z_polifyit.<locals>.wrapper)r   )rN   rQ   rO   rM   rP   	_polifyit<   s   rR   c                       s6  e Zd ZdZdZdZdZdZdd Ze	dd Z
ed	d
 Zedd Zdd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd  Z fd!d"Zed#d$ Zed%d& Zed'd( Zed)d* Zed+d, Zed-d. Zed/d0 Zd1d2 Z d3d4 Z!drd6d7Z"d8d9 Z#d:d; Z$d<d= Z%d>d? Z&d@dA Z'dBdC Z(dsdDdEZ)dFdG Z*dHdI Z+dJdK Z,dLdM Z-dNdO Z.dPdQ Z/dRdS Z0dsdTdUZ1dsdVdWZ2dsdXdYZ3dsdZd[Z4dsd\d]Z5d^d_ Z6d`da Z7dbdc Z8ddde Z9dfdg Z:dtdidjZ;dudkdlZ<dmdn Z=dodp Z>dqdr Z?dsdt Z@dududvZAdwdx ZBdydz ZCd{d| ZDd}d~ ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdvddZTdvddZUdvddZVdvddZWdd ZXdwddZYdd ZZdd Z[dd Z\dd Z]dsddZ^dd Z_dsddZ`dd Zadd ZbdxddZcdsddZddsddZedsddZfdsddĄZgddƄ ZhddȄ ZiduddʄZjdd̄ Zkdd΄ ZlddЄ ZmemZndydd҄ZoddԄ ZpdvddքZqdvdd؄ZrdvddڄZsdd܄ Ztddބ ZududdZvdd ZwdsddZxdsddZydd Zzdd Z{dd Z|dd Z}dvddZ~dd Zdd Zdd Zdd Zdd Zdd ZdvddZdd  Zdd Zdd Zdd ZduddZdud	d
Zdd Zdd ZdzddZd{ddZdrddZdvddZd|ddZd|ddZd}ddZdd  Zd!d" Zdud#d$Zed%d& Zed'd( Zed)d* Zed+d, Zed-d. Zed/d0 Zed1d2 Zed3d4 Zed5d6 Zed7d8 Zed9d: Zed;d< Zed=d> Zed?d@ ZdAdB ZdCdD ZedEdF ZedGdH ZedIdJ ZedKdL ZedMdN ZedOdP ZedQedRdS ZedTdU ZedVdW ZedXdY ZedZd[ Zed\d] Zed^d_ Zed`edadb Zed`edcdd Zedeedfdg Zed`edhdi Zdjdk ZdudldmZdudndoZdpdq Z  ZS (~  rB   ad  
    Generic class for representing and operating on polynomial expressions.

    See :ref:`polys-docs` for general documentation.

    Poly is a subclass of Basic rather than Expr but instances can be
    converted to Expr with the :py:meth:`~.Poly.as_expr` method.

    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

    reprD   Tgn $@c                 O   s   t ||}d|v rtdt|ttttfr| ||S t	|t
dr6t|tr.| ||S | t||S t|}|jrC| ||S | ||S )z:Create a new polynomial instance out of something useful. orderz&'order' keyword is not implemented yet)exclude)optionsbuild_optionsNotImplementedErrorrA   r%   r&   r'   r    _from_domain_elementr   strdict
_from_dict
_from_listlistr   is_Poly
_from_poly
_from_exprclsrT   rD   argsoptrO   rO   rP   __new__   s   
zPoly.__new__c                 G   sR   t |tstd| |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)	rA   r%   r-   levlenr   rg   rT   rD   )rd   rT   rD   objrO   rO   rP   new   s   

zPoly.newc                 C   s   t | j g| jR  S N)r3   rT   to_sympy_dictrD   selfrO   rO   rP   expr   s   z	Poly.exprc                 C      | j f| j S rm   )rq   rD   ro   rO   rO   rP   re      s   z	Poly.argsc                 C   rr   rm   rS   ro   rO   rO   rP   _hashable_content   s   zPoly._hashable_contentc                 O      t ||}| ||S )(Construct a polynomial from a ``dict``. )rW   rX   r]   rc   rO   rO   rP   	from_dict      zPoly.from_dictc                 O   rt   )(Construct a polynomial from a ``list``. )rW   rX   r^   rc   rO   rO   rP   	from_list   rw   zPoly.from_listc                 O   rt   )*Construct a polynomial from a polynomial. )rW   rX   ra   rc   rO   rO   rP   	from_poly   rw   zPoly.from_polyc                 O   rt   +Construct a polynomial from an expression. )rW   rX   rb   rc   rO   rO   rP   rC      rw   zPoly.from_exprc                 C   sz   |j }|s	tdt|d }|j}|du rt||d\}}n| D ]\}}||||< q#| jt	|||g|R  S )ru   z/can't initialize from 'dict' without generatorsrh   Nrf   )
rD   r,   rj   domainr   itemsconvertrl   r%   rv   )rd   rT   rf   rD   levelr   monomcoeffrO   rO   rP   r]      s   zPoly._from_dictc                 C   s~   |j }|s	tdt|dkrtdt|d }|j}|du r)t||d\}}ntt|j|}| j	t
|||g|R  S )rx   z/can't initialize from 'list' without generatorsrh   z#'list' representation not supportedNr~   )rD   r,   rj   r.   r   r   r_   mapr   rl   r%   ry   )rd   rT   rf   rD   r   r   rO   rO   rP   r^      s   zPoly._from_listc                 C   s   | |j kr| j|jg|jR  }|j}|j}|j}|r6|j|kr6t|jt|kr1| | |S |j	| }d|v rC|rC|
|}|S |du rK| }|S )rz   r   T)	__class__rl   rT   rD   fieldr   setrb   rG   reorder
set_domainto_field)rd   rT   rf   rD   r   r   rO   rO   rP   ra     s   


zPoly._from_polyc                 C   s   t ||\}}| ||S r|   )r7   r]   )rd   rT   rf   rO   rO   rP   rb   "  s   zPoly._from_exprc                 C   s@   |j }|j}t|d }||g}| jt|||g|R  S Nrh   )rD   r   rj   r   rl   r%   ry   )rd   rT   rf   rD   r   r   rO   rO   rP   rZ   (  s
   zPoly._from_domain_elementc                    
   t   S rm   super__hash__ro   r   rO   rP   r   2     
zPoly.__hash__c                 C   sP   t  }| j}tt|D ]}|  D ]}|| r!||| jO } nqq|| jB S )a  
        Free symbols of a polynomial expression.

        Examples
        ========

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

        >>> Poly(x**2 + 1).free_symbols
        {x}
        >>> Poly(x**2 + y).free_symbols
        {x, y}
        >>> Poly(x**2 + y, x).free_symbols
        {x, y}
        >>> Poly(x**2 + y, x, z).free_symbols
        {x, y}

        )r   rD   rangerj   monomsfree_symbolsfree_symbols_in_domain)rp   symbolsrD   ir   rO   rO   rP   r   5  s   
zPoly.free_symbolsc                 C   sR   | j jt }}|jr|jD ]}||jO }q|S |jr'|  D ]}||jO }q|S )aj  
        Free symbols of the domain of ``self``.

        Examples
        ========

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

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

        )rT   domr   is_Compositer   r   is_EXcoeffs)rp   r   r   genr   rO   rO   rP   r   T  s   
zPoly.free_symbols_in_domainc                 C   s
   | j d S )z
        Return the principal generator.

        Examples
        ========

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

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

        r   rD   ro   rO   rO   rP   r   r  s   
zPoly.genc                 C      |   S )a  Get the ground domain of a :py:class:`~.Poly`

        Returns
        =======

        :py:class:`~.Domain`:
            Ground domain of the :py:class:`~.Poly`.

        Examples
        ========

        >>> from sympy import Poly, Symbol
        >>> x = Symbol('x')
        >>> p = Poly(x**2 + x)
        >>> p
        Poly(x**2 + x, x, domain='ZZ')
        >>> p.domain
        ZZ
        )
get_domainro   rO   rO   rP   r     s   zPoly.domainc                 C   &   | j | j| jj| jjg| jR  S )z3Return zero polynomial with ``self``'s properties. )rl   rT   zerori   r   rD   ro   rO   rO   rP   r        &z	Poly.zeroc                 C   r   )z2Return one polynomial with ``self``'s properties. )rl   rT   oneri   r   rD   ro   rO   rO   rP   r     r   zPoly.onec                 C   r   )z3Return unit polynomial with ``self``'s properties. )rl   rT   unitri   r   rD   ro   rO   rO   rP   r     r   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)rJ   rK   _perFGrO   rO   rP   unify  s   z
Poly.unifyc                    s  t js+zjjjjjjjfW S  ty*   tdf w tjt	rtjt	rt
jj}jjjj|t|d }j|kr~tj j|\}}jjkrpfdd|D }t	ttt|||}nj}j|krtj j|\}}jjkrfdd|D }t	ttt|||}	nj}	ntdf j |d f fdd	}
|
||	fS )Ncan't unify %s with %srh   c                       g | ]
}  |jjqS rO   r   rT   r   .0c)r   rJ   rO   rP   
<listcomp>      zPoly._unify.<locals>.<listcomp>c                    r   rO   r   r   )r   rK   rO   rP   r     r   c                    D   |d ur|d | ||d d   }|s| | S  j| g|R  S r   to_sympyrl   rT   r   rD   removerd   rO   rP   r     
   
zPoly._unify.<locals>.per)r   r`   rT   r   r   
from_sympyr*   r+   rA   r%   r5   rD   r   rj   r6   to_dictr\   r_   zipr   r   )rJ   rK   rD   ri   Zf_monomsZf_coeffsr   Zg_monomsZg_coeffsr   r   rO   )rd   r   rJ   rK   rP   r     s<   ("

	zPoly._unifyNc                 C   sX   |du r| j }|dur"|d| ||d d  }|s"| jj|S | jj|g|R  S )ab  
        Create a Poly out of the given representation.

        Examples
        ========

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

        >>> from sympy.polys.polyclasses import DMP

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

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

        Nrh   )rD   rT   r   r   r   rl   )rJ   rT   rD   r   rO   rO   rP   r     s   zPoly.perc                 C   s&   t | jd|i}| | j|jS )z Set the ground domain of ``f``. r   )rW   rX   rD   r   rT   r   r   )rJ   r   rf   rO   rO   rP   r     s   zPoly.set_domainc                 C      | j jS )z Get the ground domain of ``f``. )rT   r   rJ   rO   rO   rP   r        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)

        )rW   ZModulusZ
preprocessr   r   )rJ   modulusrO   rO   rP   set_modulus  s   zPoly.set_modulusc                 C   s"   |   }|jrt| S td)z
        Get the modulus of ``f``.

        Examples
        ========

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

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

        z$not a polynomial over a Galois field)r   Zis_FiniteFieldr
   Zcharacteristicr-   )rJ   r   rO   rO   rP   get_modulus/  s   zPoly.get_modulusc                 C   sN   || j v r|jr| ||S z| ||W S  ty   Y nw |  ||S )z)Internal implementation of :func:`subs`. )rD   	is_numberevalreplacer-   rG   subs)rJ   oldrl   rO   rO   rP   
_eval_subsD  s   
zPoly._eval_subsc                 C   sL   | j  \}}g }tt| jD ]}||vr|| j|  q| 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   )rT   rV   r   rj   rD   appendr   )rJ   Jrl   rD   jrO   rO   rP   rV   Q  s   zPoly.excludec                 K   s   |du r| j r| j|}}ntd||ks|| jvr| S || jv rG|| jvrG|  }|jr3||jvrGt| j}||||< | j	| j
|dS td||| f )a  
        Replace ``x`` with ``y`` in generators list.

        Examples
        ========

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

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

        Nz(syntax supported only in univariate caser   zcan't replace %s with %s in %s)is_univariater   r-   rD   r   r   r   r_   indexr   rT   )rJ   xy_ignorer   rD   rO   rO   rP   r   h  s   
zPoly.replacec                 O   s   |   j|i |S )z-Match expression from Poly. See Basic.match())rG   match)rJ   re   kwargsrO   rO   rP   r     s   z
Poly.matchc                 O   s|   t d|}|st| j|d}nt| jt|krtdtttt	| j
 | j| }| jt|| j
jt|d |dS )a  
        Efficiently apply new order of generators.

        Examples
        ========

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

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

        rO   r~   z7generators list can differ only up to order of elementsrh   r   )rW   ZOptionsr4   rD   r   r-   r\   r_   r   r6   rT   r   r   r%   r   rj   )rJ   rD   re   rf   rT   rO   rO   rP   r     s    "zPoly.reorderc                 C   s   | j dd}| |}i }| D ]\}}t|d| r#td|  ||||d < q| j|d }| jt|t	|d | j
jg|R  S )a(  
        Remove dummy generators from ``f`` that are to the left of
        specified ``gen`` in the generators as ordered. When ``gen``
        is an integer, it refers to the generator located at that
        position within the tuple of generators of ``f``.

        Examples
        ========

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

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

        T)nativeNzcan't left trim %srh   )as_dict_gen_to_levelr   anyr-   rD   rl   r%   rv   rj   rT   r   )rJ   r   rT   r   termsr   r   rD   rO   rO   rP   ltrim  s   
(z
Poly.ltrimc              	   G   s   t  }|D ]}z| j|}W n ty   td| |f w || q|  D ]}t|D ]\}}||vr=|r=  dS q/q)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

        %s doesn't have %s as generatorFT)r   rD   r   
ValueErrorr2   addr   	enumerate)rJ   rD   indicesr   r   r   r   eltrO   rO   rP   has_only_gens  s"   
zPoly.has_only_gensc                 C   ,   t | jdr| j }nt| d| |S )z
        Make the ground domain a ring.

        Examples
        ========

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

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

        to_ring)hasattrrT   r   r(   r   rJ   rL   rO   rO   rP   r        

zPoly.to_ringc                 C   r   )z
        Make the ground domain a field.

        Examples
        ========

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

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

        r   )r   rT   r   r(   r   r   rO   rO   rP   r     r   zPoly.to_fieldc                 C   r   )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   rT   r   r(   r   r   rO   rO   rP   r     r   zPoly.to_exactc                 C   s4   t | jdd|| jjpdd\}}| j|| j|dS )a  
        Recalculate the ground domain of a polynomial.

        Examples
        ========

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

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

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

        Tr   N)r   Z	compositer   )r   r   r   r   rv   rD   )rJ   r   r   rT   rO   rO   rP   retract.  s   
zPoly.retractc                 C   sh   |du rd||}}}n|  |}t|t|}}t| jdr*| j|||}nt| d| |S )z1Take a continuous subsequence of terms of ``f``. Nr   slice)r   intr   rT   r   r(   r   )rJ   r   mnr   rL   rO   rO   rP   r   F  s   


z
Poly.slicec                        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                       g | ]	} j j|qS rO   rT   r   r   r   r   rO   rP   r   j      zPoly.coeffs.<locals>.<listcomp>rU   )rT   r   rJ   rU   rO   r   rP   r   V  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   )rT   r   r   rO   rO   rP   r   l  s   zPoly.monomsc                    r   )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                    "   g | ]\}}| j j|fqS rO   r   r   r   r   r   rO   rP   r        " zPoly.terms.<locals>.<listcomp>r   )rT   r   r   rO   r   rP   r     s   z
Poly.termsc                        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                    r   rO   r   r   r   rO   rP   r     r   z#Poly.all_coeffs.<locals>.<listcomp>)rT   
all_coeffsr   rO   r   rP   r        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

        )rT   
all_monomsr   rO   rO   rP   r     s   
zPoly.all_monomsc                    r   )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                    r   rO   r   r   r   rO   rP   r     r   z"Poly.all_terms.<locals>.<listcomp>)rT   	all_termsr   rO   r   rP   r     r   zPoly.all_termsc                 O   sx   i }|   D ]&\}}|||}t|tr|\}}n|}|r,||vr&|||< qtd| q| j|g|p5| jR i |S )ah  
        Apply a function to all terms of ``f``.

        Examples
        ========

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

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

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

        z%s monomial was generated twice)r   rA   tupler-   rv   rD   )rJ   rN   rD   re   r   r   r   rL   rO   rO   rP   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

        )rj   r   r   rO   rO   rP   length  s   zPoly.lengthFc                 C   s    |r	| j j|dS | j j|dS )a  
        Switch to a ``dict`` representation.

        Examples
        ========

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

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

        r   )rT   r   rn   )rJ   r   r   rO   rO   rP   r     s   zPoly.as_dictc                 C   s   |r| j  S | j  S )z%Switch to a ``list`` representation. )rT   Zto_listZto_sympy_list)rJ   r   rO   rO   rP   as_list  s   

zPoly.as_listc              	   G   s   |s| j S t|dkr?t|d tr?|d }t| j}| D ]\}}z||}W n ty9   t	d| |f w |||< qt
| j g|R  S )ar  
        Convert a Poly instance to an Expr instance.

        Examples
        ========

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

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

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

        rh   r   r   )rq   rj   rA   r\   r_   rD   r   r   r   r2   r3   rT   rn   )rJ   rD   mappingr   valuer   rO   rO   rP   rG     s   


zPoly.as_exprc                 O   s>   zt | g|R i |}|jsW dS |W S  ty   Y dS w )a{  Converts ``self`` to a polynomial or returns ``None``.

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

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

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

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

        N)rB   r`   r-   )rp   rD   re   polyrO   rO   rP   as_poly=  s   zPoly.as_polyc                 C   r   )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   rT   r  r(   r   r   rO   rO   rP   r  W  r   z	Poly.liftc                 C   4   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   rT   r  r(   r   rJ   r   rL   rO   rO   rP   r  l     
zPoly.deflatec                 C   sz   | 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
|g|R  S )a  
        Inject ground domain generators into ``f``.

        Examples
        ========

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

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

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

        zcan't inject generators over %sinjectfront)rT   r   is_Numericalr`   r)   r   r	  r(   r   rD   rl   )rJ   r  r   rL   rD   rO   rO   rP   r	    s   
zPoly.injectc                 G   s   | j j}|jstd| t|}| jd| |kr%| j|d d}}n| j| d |kr;| jd|  d}}ntd|j| }t| j drS| j j	||d}nt
| d| j|g|R  S )a  
        Eject selected generators into the ground domain.

        Examples
        ========

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

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

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

        zcan't eject generators over %sNTFz'can only eject front or back generatorsejectr
  )rT   r   r  r)   rj   rD   rY   r	  r   r  r(   rl   )rJ   rD   r   kZ_gensr  rL   rO   rO   rP   r    s    

z
Poly.ejectc                 C   r  )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   rT   r  r(   r   r  rO   rO   rP   r    r  zPoly.terms_gcdc                 C   .   t | jdr| j|}nt| d| |S )z
        Add an element of the ground domain to ``f``.

        Examples
        ========

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

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

        
add_ground)r   rT   r  r(   r   rJ   r   rL   rO   rO   rP   r       

zPoly.add_groundc                 C   r  )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   rT   r  r(   r   r  rO   rO   rP   r    r  zPoly.sub_groundc                 C   r  )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   rT   r  r(   r   r  rO   rO   rP   r    r  zPoly.mul_groundc                 C   r  )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   rT   r  r(   r   r  rO   rO   rP   r  $  s   

zPoly.quo_groundc                 C   r  )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   rT   r  r(   r   r  rO   rO   rP   r  <  s   

zPoly.exquo_groundc                 C   r   )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   rT   r  r(   r   r   rO   rO   rP   r  V  r   zPoly.absc                 C   r   )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   rT   r  r(   r   r   rO   rO   rP   r  k     

zPoly.negc                 C   R   t |}|js| |S | |\}}}}t| jdr$||}||S t| d)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   r`   r  r   r   rT   r   r(   rJ   rK   r   r   r   r   rL   rO   rO   rP   r        


zPoly.addc                 C   r  )a`  
        Subtract two polynomials ``f`` and ``g``.

        Examples
        ========

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

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

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

        sub)r   r`   r  r   r   rT   r  r(   r  rO   rO   rP   r    r  zPoly.subc                 C   r  )ap  
        Multiply two polynomials ``f`` and ``g``.

        Examples
        ========

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

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

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

        r   )r   r`   r  r   r   rT   r   r(   r  rO   rO   rP   r     r  zPoly.mulc                 C   r   )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   rT   r  r(   r   r   rO   rO   rP   r    r  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   rT   r   r(   r   rJ   r   rL   rO   rO   rP   r     s
   

zPoly.powc                 C   sH   |  |\}}}}t| jdr||\}}nt| d||||fS )a#  
        Polynomial pseudo-division of ``f`` by ``g``.

        Examples
        ========

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

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

        pdiv)r   r   rT   r"  r(   )rJ   rK   r   r   r   r   qrrO   rO   rP   r"    s
   
z	Poly.pdivc                 C   :   |  |\}}}}t| jdr||}||S t| d)aN  
        Polynomial pseudo-remainder of ``f`` by ``g``.

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

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

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


        Examples
        ========

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

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

        prem)r   r   rT   r&  r(   r  rO   rO   rP   r&  )  s
   

z	Poly.premc                 C   r%  )a  
        Polynomial pseudo-quotient of ``f`` by ``g``.

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

        Examples
        ========

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

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

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

        pquo)r   r   rT   r'  r(   r  rO   rO   rP   r'  P  s
   

z	Poly.pquoc              
   C   sj   |  |\}}}}t| jdr0z
||}W ||S  ty/ } z
||  | d}~ww t| d)a  
        Polynomial exact pseudo-quotient of ``f`` by ``g``.

        Examples
        ========

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

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

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

        pexquoN)r   r   rT   r(  r/   rl   rG   r(   )rJ   rK   r   r   r   r   rL   excrO   rO   rP   r(  l  s   
zPoly.pexquoc                 C   s   |  |\}}}}d}|r|jr|js| | }}d}t| jdr,||\}}	nt| d|rMz| |	 }
}W n	 t	yG   Y nw |
|}}	||||	fS )a  
        Polynomial division with remainder of ``f`` by ``g``.

        Examples
        ========

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

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

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

        FTdiv)
r   is_Ringis_Fieldr   r   rT   r*  r(   r   r*   )rJ   rK   autor   r   r   r   r   r#  r$  QRrO   rO   rP   r*    s    

zPoly.divc           	      C      |  |\}}}}d}|r|jr|js| | }}d}t| jdr*||}nt| d|rGz	| }W ||S  t	yF   Y ||S w ||S )ao  
        Computes the polynomial remainder of ``f`` by ``g``.

        Examples
        ========

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

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

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

        FTrem)
r   r+  r,  r   r   rT   r1  r(   r   r*   )	rJ   rK   r-  r   r   r   r   r   r$  rO   rO   rP   r1    "   

zPoly.remc           	      C   r0  )aa  
        Computes polynomial quotient of ``f`` by ``g``.

        Examples
        ========

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

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

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

        FTquo)
r   r+  r,  r   r   rT   r3  r(   r   r*   )	rJ   rK   r-  r   r   r   r   r   r#  rO   rO   rP   r3    r2  zPoly.quoc           
   
   C   s   |  |\}}}}d}|r|jr|js| | }}d}t| jdrBz||}W n tyA }	 z
|	| 	 |	 d}	~	ww t
| d|r_z	| }W ||S  ty^   Y ||S w ||S )a  
        Computes polynomial exact quotient of ``f`` by ``g``.

        Examples
        ========

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

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

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

        FTexquoN)r   r+  r,  r   r   rT   r4  r/   rl   rG   r(   r   r*   )
rJ   rK   r-  r   r   r   r   r   r#  r)  rO   rO   rP   r4    s,   

z
Poly.exquoc                 C   s   t |tr*t| j}| |  kr|k r!n n
|dk r|| S |S td|||f z	| jt|W S  ty@   td| w )z3Returns level associated with the given generator. r   z -%s <= gen < %s expected, got %sz"a valid generator expected, got %s)rA   r   rj   rD   r-   r   r   r   )rJ   r   r   rO   rO   rP   r   &  s    

zPoly._gen_to_levelr   c                 C   s,   |  |}t| jdr| j|S t| d)au  
        Returns degree of ``f`` in ``x_j``.

        The degree of 0 is negative infinity.

        Examples
        ========

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

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

        degree)r   r   rT   r5  r(   )rJ   r   r   rO   rO   rP   r5  :  s   

zPoly.degreec                 C       t | jdr| j S t| d)z
        Returns a list of degrees of ``f``.

        Examples
        ========

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

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

        degree_list)r   rT   r7  r(   r   rO   rO   rP   r7  U  s   

zPoly.degree_listc                 C   r6  )a  
        Returns the total degree of ``f``.

        Examples
        ========

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

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

        total_degree)r   rT   r8  r(   r   rO   rO   rP   r8  h  s   

zPoly.total_degreec                 C   sz   t |tstdt| || jv r| j|}| j}nt| j}| j|f }t| jdr8| j	| j
||dS t| d)a  
        Returns the homogeneous polynomial of ``f``.

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

        Examples
        ========

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

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

        z``Symbol`` expected, got %s
homogenizer   homogeneous_order)rA   r   	TypeErrortyperD   r   rj   r   rT   r   r9  r(   )rJ   sr   rD   rO   rO   rP   r9  }  s   



zPoly.homogenizec                 C   r6  )a-  
        Returns the homogeneous order of ``f``.

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

        Examples
        ========

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

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

        r:  )r   rT   r:  r(   r   rO   rO   rP   r:    s   

zPoly.homogeneous_orderc                 C   sF   |dur|  |d S t| jdr| j }nt| d| jj|S )z
        Returns the leading coefficient of ``f``.

        Examples
        ========

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

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

        Nr   LC)r   r   rT   r>  r(   r   r   )rJ   rU   rL   rO   rO   rP   r>    s   
zPoly.LCc                 C   0   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   rT   r@  r(   r   r   r   rO   rO   rP   r@       
zPoly.TCc                 C   s$   t | jdr| |d S t| d)z
        Returns the last non-zero coefficient of ``f``.

        Examples
        ========

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

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

        r   EC)r   rT   r   r(   r   rO   rO   rP   rC    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#   rD   	exponents)rJ   r   rO   rO   rP   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

        rD  z,exponent of each generator must be specified)r   rT   rj   rD   r   rD  r_   r   r   r(   r   r   )rJ   NrL   rO   rO   rP   rD    s   
zPoly.nthrh   c                 C      t d)NzyEither convert to Expr with `as_expr` method to use Expr's coeff method or else use the `coeff_monomial` method of Polys.rY   )rJ   r   r   rightrO   rO   rP   r   ?  s   z
Poly.coeffc                 C      t | |d | jS )a  
        Returns the leading monomial of ``f``.

        The Leading monomial signifies the monomial having
        the highest power of the principal generator in the
        expression f.

        Examples
        ========

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

        >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LM()
        x**2*y**0

        r   r#   r   rD   r   rO   rO   rP   LMK  s   zPoly.LMc                 C   rK  )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

        rB  rL  r   rO   rO   rP   EM_  s   zPoly.EMc                 C   "   |  |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#   rD   rJ   rU   r   r   rO   rO   rP   LTo  s   zPoly.LTc                 C   rO  )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)

        rB  rP  rQ  rO   rO   rP   ET  s   zPoly.ETc                 C   r?  )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   rT   rT  r(   r   r   r   rO   rO   rP   rT    rA  zPoly.max_normc                 C   r?  )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   rT   rU  r(   r   r   r   rO   rO   rP   rU    rA  zPoly.l1_normc                 C   s   | }|j jjstj|fS | }|jr|j j }t|j dr'|j 	 \}}nt
|d||||}}|r<|js@||fS || fS )a  
        Clear denominators, but keep the ground domain.

        Examples
        ========

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

        >>> f = Poly(x/2 + S(1)/3, x, domain=QQ)

        >>> f.clear_denoms()
        (6, Poly(3*x + 2, x, domain='QQ'))
        >>> f.clear_denoms(convert=True)
        (6, Poly(3*x + 2, x, domain='ZZ'))

        clear_denoms)rT   r   r,  r   Oner   has_assoc_Ringget_ringr   rV  r(   r   r   r   )rp   r   rJ   r   r   rL   rO   rO   rP   rV    s   



zPoly.clear_denomsc                 C   sv   | }| |\}}}}||}||}|jr|js||fS |jdd\}}|jdd\}}||}||}||fS )a  
        Clear denominators in a rational function ``f/g``.

        Examples
        ========

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

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

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

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

        Tr   )r   r,  rX  rV  r  )rp   rK   rJ   r   r   abrO   rO   rP   rat_clear_denoms  s   

zPoly.rat_clear_denomsc                 O   s   | }| ddr|jjjr| }t|jdrK|s#||jjddS |j}|D ]}t|t	u r5|\}}n|d}}|t
|||}q(||S t|d)a  
        Computes indefinite integral of ``f``.

        Examples
        ========

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

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

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

        r-  T	integraterh   r   )getrT   r   r+  r   r   r   r^  r<  r   r   r   r(   )rp   specsre   rJ   rT   specr   r   rO   rO   rP   r^  	  s   



zPoly.integratec                 O   s   | ddst| g|R i |S t| jdrK|s#| | jjddS | j}|D ]}t|tu r5|\}}n|d}}|t|| 	|}q(| |S t
| d)aX  
        Computes partial derivative of ``f``.

        Examples
        ========

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

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

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

        ZevaluateTdiffrh   r_  )r`  r   r   rT   r   rc  r<  r   r   r   r(   )rJ   ra  r   rT   rb  r   r   rO   rO   rP   rc  5	  s   



z	Poly.diffc                 C   sL  | }|du rMt |tr|}| D ]
\}}|||}q|S t |ttfrG|}t|t|jkr4tdt	|j|D ]
\}}|||}q:|S d|}	}n|
|}	t|jds]t|dz	|j||	}
W n8 ty   |sytd||jjf t|g\}\}| ||j}||}|||}|j||	}
Y nw |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   )rA   r\   r   r   r   r_   rj   rD   r   r   r   r   rT   r(   r*   r)   r   r   r   Zunify_with_symbolsr   r   r   )rp   r   r[  r-  rJ   r   r   r  valuesr   rL   Za_domainZ
new_domainrO   rO   rP   r   ]	  s<   



z	Poly.evalc                 G   
   |  |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   )rJ   re  rO   rO   rP   __call__	  s   
zPoly.__call__c           	      C   sd   |  |\}}}}|r|jr| | }}t| jdr%||\}}nt| d||||fS )a  
        Half extended Euclidean algorithm of ``f`` and ``g``.

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

        Examples
        ========

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

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

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

        
half_gcdex)r   r+  r   r   rT   rh  r(   )	rJ   rK   r-  r   r   r   r   r=  hrO   rO   rP   rh  	  s   

zPoly.half_gcdexc           
      C   sl   |  |\}}}}|r|jr| | }}t| jdr&||\}}}	nt| d||||||	fS )a  
        Extended Euclidean algorithm of ``f`` and ``g``.

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

        Examples
        ========

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

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

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

        gcdex)r   r+  r   r   rT   rj  r(   )
rJ   rK   r-  r   r   r   r   r=  tri  rO   rO   rP   rj  	  s   

z
Poly.gcdexc                 C   sV   |  |\}}}}|r|jr| | }}t| jdr&||}||S t| d)a  
        Invert ``f`` modulo ``g`` when possible.

        Examples
        ========

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

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

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

        invert)r   r+  r   r   rT   rl  r(   )rJ   rK   r-  r   r   r   r   rL   rO   rO   rP   rl  	  s   


zPoly.invertc                 C   s2   t | jdr| jt|}nt| d| |S )ad  
        Compute ``f**(-1)`` mod ``x**n``.

        Examples
        ========

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

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

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

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

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

        revert)r   rT   rm  r   r(   r   r!  rO   rO   rP   rm  
  s   

zPoly.revertc                 C   sB   |  |\}}}}t| jdr||}nt| dtt||S )ad  
        Computes the subresultant PRS of ``f`` and ``g``.

        Examples
        ========

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

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

        subresultants)r   r   rT   rn  r(   r_   r   r  rO   rO   rP   rn  ?
  s
   
zPoly.subresultantsc           	      C   sv   |  |\}}}}t| jdr!|r|j||d\}}n||}nt| d|r5||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   rd  )r   r   rT   ro  r(   r_   r   )	rJ   rK   rq  r   r   r   r   rL   r/  rO   rO   rP   ro  X
  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   rd  )r   rT   rr  r(   r   r   rO   rO   rP   rr  }
  rA  zPoly.discriminantc                 C      ddl m} || |S )a  Compute the *dispersion set* of two polynomials.

        For two polynomials `f(x)` and `g(x)` with `\deg f > 0`
        and `\deg g > 0` the dispersion set `\operatorname{J}(f, g)` is defined as:

        .. math::
            \operatorname{J}(f, g)
            & := \{a \in \mathbb{N}_0 | \gcd(f(x), g(x+a)) \neq 1\} \\
            &  = \{a \in \mathbb{N}_0 | \deg \gcd(f(x), g(x+a)) \geq 1\}

        For a single polynomial one defines `\operatorname{J}(f) := \operatorname{J}(f, f)`.

        Examples
        ========

        >>> from sympy import poly
        >>> from sympy.polys.dispersion import dispersion, dispersionset
        >>> from sympy.abc import x

        Dispersion set and dispersion of a simple polynomial:

        >>> fp = poly((x - 3)*(x + 3), x)
        >>> sorted(dispersionset(fp))
        [0, 6]
        >>> dispersion(fp)
        6

        Note that the definition of the dispersion is not symmetric:

        >>> fp = poly(x**4 - 3*x**2 + 1, x)
        >>> gp = fp.shift(-3)
        >>> sorted(dispersionset(fp, gp))
        [2, 3, 4]
        >>> dispersion(fp, gp)
        4
        >>> sorted(dispersionset(gp, fp))
        []
        >>> dispersion(gp, fp)
        -oo

        Computing the dispersion also works over field extensions:

        >>> from sympy import sqrt
        >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')
        >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
        >>> sorted(dispersionset(fp, gp))
        [2]
        >>> sorted(dispersionset(gp, fp))
        [1, 4]

        We can even perform the computations for polynomials
        having symbolic coefficients:

        >>> from sympy.abc import a
        >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)
        >>> sorted(dispersionset(fp))
        [0, 1]

        See Also
        ========

        dispersion

        References
        ==========

        1. [ManWright94]_
        2. [Koepf98]_
        3. [Abramov71]_
        4. [Man93]_
        r   )dispersionset)sympy.polys.dispersionrt  )rJ   rK   rt  rO   rO   rP   rt  
     H
zPoly.dispersionsetc                 C   rs  )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)ru  rw  )rJ   rK   rw  rO   rO   rP   rw  
  rv  zPoly.dispersionc           	      C   sP   |  |\}}}}t| jdr||\}}}nt| d||||||fS )a#  
        Returns the GCD of ``f`` and ``g`` and their cofactors.

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

        Examples
        ========

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

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

        	cofactors)r   r   rT   rx  r(   )	rJ   rK   r   r   r   r   ri  cffcfgrO   rO   rP   rx  (  s
   
zPoly.cofactorsc                 C   r%  )a  
        Returns the polynomial GCD of ``f`` and ``g``.

        Examples
        ========

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

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

        gcd)r   r   rT   r{  r(   r  rO   rO   rP   r{  E  
   

zPoly.gcdc                 C   r%  )a  
        Returns polynomial LCM of ``f`` and ``g``.

        Examples
        ========

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

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

        lcm)r   r   rT   r}  r(   r  rO   rO   rP   r}  \  r|  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)rT   r   r   r   r~  r(   r   )rJ   prL   rO   rO   rP   r~  s  s
   

z
Poly.truncc                 C   sF   | }|r|j jjr| }t|j dr|j  }nt|d||S )az  
        Divides all coefficients by ``LC(f)``.

        Examples
        ========

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

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

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

        monic)rT   r   r+  r   r   r  r(   r   rp   r-  rJ   rL   rO   rO   rP   r    s   

z
Poly.monicc                 C   r?  )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   rT   r  r(   r   r   r   rO   rO   rP   r    rA  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   rT   r  r(   r   r   r   )rJ   contrL   rO   rO   rP   r    s   
zPoly.primitivec                 C   r%  )a  
        Computes the functional composition of ``f`` and ``g``.

        Examples
        ========

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

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

        compose)r   r   rT   r  r(   r  rO   rO   rP   r    r|  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   rT   r  r(   r_   r   r   r   rO   rO   rP   r    s   
zPoly.decomposec                 C   r  )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   rT   r  r(   r   )rJ   r[  rL   rO   rO   rP   r    r  z
Poly.shiftc                 C   s^   | |\}}|  |\}}| |\}}t|jdr%|j|j|j}nt|d||S )a3  
        Efficiently evaluate the functional transformation ``q**n * f(p/q)``.


        Examples
        ========

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

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

        	transform)r   r   rT   r  r(   r   )rJ   r  r#  Pr.  r   rL   rO   rO   rP   r    s   

zPoly.transformc                 C   sL   | }|r|j jjr| }t|j dr|j  }nt|dtt|j	|S )a  
        Computes the Sturm sequence of ``f``.

        Examples
        ========

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

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

        sturm)
rT   r   r+  r   r   r  r(   r_   r   r   r  rO   rO   rP   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                       g | ]\}}  ||fqS rO   r   r   rK   r  r   rO   rP   r   ^      z!Poly.gff_list.<locals>.<listcomp>)r   rT   r  r(   r   rO   r   rP   r  I  s   
zPoly.gff_listc                 C   r   )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   rT   r  r(   r   )rJ   r$  rO   rO   rP   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   rT   r  r(   r   )rJ   r=  rK   r$  rO   rO   rP   r    s   
zPoly.sqf_normc                 C   r   )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   rT   r  r(   r   r   rO   rO   rP   r    r   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                    r  rO   r  r  r   rO   rP   r     r  z!Poly.sqf_list.<locals>.<listcomp>)r   rT   r  r(   r   r   )rJ   allr   factorsrO   r   rP   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                    r  rO   r  r  r   rO   rP   r     r  z)Poly.sqf_list_include.<locals>.<listcomp>)r   rT   r  r(   )rJ   r  r  rO   r   rP   r    s   
zPoly.sqf_list_includec                    sl   t  jdr!z	 j \}}W n ty    tj dfgf Y S w 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_listrh   c                    r  rO   r  r  r   rO   rP   r     r  z$Poly.factor_list.<locals>.<listcomp>)	r   rT   r  r)   r   rW  r(   r   r   )rJ   r   r  rO   r   rP   r    s   
 zPoly.factor_listc                    sT   t  jdrz j }W n ty    dfg Y S w 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_includerh   c                    r  rO   r  r  r   rO   rP   r   )  r  z,Poly.factor_list_include.<locals>.<listcomp>)r   rT   r  r)   r(   )rJ   r  rO   r   rP   r    s   
zPoly.factor_list_includec                 C   s  |durt |}|dkrtd|durt |}|dur#t |}t| jdr6| jj||||||d}nt| d|r`dd }|sJtt||S dd	 }	|\}
}tt||
tt|	|fS d
d }|smtt||S dd	 }	|\}
}tt||
tt|	|fS )a  
        Compute isolating intervals for roots of ``f``.

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

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

        Examples
        ========

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

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

        Nr   !'eps' must be a positive rational	intervalsr  epsinfsupfastsqfc                 S   s   | \}}t |t |fS rm   r   r   )intervalr=  rk  rO   rO   rP   _realW  s   zPoly.intervals.<locals>._realc                 S   s@   | \\}}\}}t |tt |  t |tt |  fS rm   r   r   r	   )	rectangleuvr=  rk  rO   rO   rP   _complex^  s   z Poly.intervals.<locals>._complexc                 S   s$   | \\}}}t |t |f|fS rm   r  )r  r=  rk  r  rO   rO   rP   r  g  s   c                 S   sH   | \\\}}\}}}t |tt |  t |tt |  f|fS rm   r  )r  r  r  r=  rk  r  rO   rO   rP   r  n  s   )	r   r   r   r   rT   r  r(   r_   r   )rJ   r  r  r  r  r  r  rL   r  r  Z	real_partZcomplex_partrO   rO   rP   r  +  s4   



zPoly.intervalsc           	      C   s   |r	| j s	tdt|t|}}|dur%t|}|dkr%td|dur.t|}n|du r4d}t| jdrH| 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   r  rh   refine_root)r  stepsr  )is_sqfr-   r   r   r   r   r   rT   r  r(   r   )	rJ   r=  rk  r  r  r  	check_sqfr   TrO   rO   rP   r  w  s   



zPoly.refine_rootc                 C   sD  d\}}|dur/t |}|tju rd}n| \}}|s"t|}ntttj||fd}}|durZt |}|tju r?d}n| \}}|sMt|}ntttj||fd}}|ru|rut	| j
drp| j
j||d}t|S t| d|r|dur|tjf}|r|dur|tjf}t	| j
dr| j
j||d}t|S t| d)a<  
        Return the number of roots of ``f`` in ``[inf, sup]`` interval.

        Examples
        ========

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

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

        TTNFcount_real_rootsr  r  count_complex_roots)r   r   NegativeInfinityas_real_imagr   r   r_   r   ZInfinityr   rT   r  r(   r   r  r
   )rJ   r  r  Zinf_realZsup_realreZimcountrO   rO   rP   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)rJ   r   r  rO   rO   rP   root  s   z	Poly.rootc                 C   (   t jjjj| |d}|r|S t|ddS )aL  
        Return a list of real roots with multiplicities.

        Examples
        ========

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

        >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).real_roots()
        [-1/2, 2, 2]
        >>> Poly(x**3 + x + 1).real_roots()
        [CRootOf(x**3 + x + 1, 0)]

        r  Fmultiple)r  r  r  CRootOf
real_rootsr;   )rJ   r  r  ZrealsrO   rO   rP   r    s   zPoly.real_rootsc                 C   r  )a  
        Return a list of real and complex roots with multiplicities.

        Examples
        ========

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

        >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).all_roots()
        [-1/2, 2, 2]
        >>> Poly(x**3 + x + 1).all_roots()
        [CRootOf(x**3 + x + 1, 0),
         CRootOf(x**3 + x + 1, 1),
         CRootOf(x**3 + x + 1, 2)]

        r  Fr  )r  r  r  r  	all_rootsr;   )rJ   r  r  rootsrO   rO   rP   r    s   zPoly.all_roots   2   c           	         sh  ddl m | jrtd|  |  dkrg S | jjtu r'dd |  D }nI| jjt	u rLdd |  D }ddl
m} ||   fdd|  D }n$fd	d|  D }z	d
d |D }W n tyo   td| jj w tjj}tj_z6ztj|||d|  d d}tttt|fddd}W n ty   td|f w W |tj_|S |tj_w )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      g | ]}t |qS rO   r   r   r   rO   rO   rP   r   M      zPoly.nroots.<locals>.<listcomp>c                 S   s   g | ]}|j qS rO   )r#  r  rO   rO   rP   r   O  s    )ilcmc                    s   g | ]}t |  qS rO   r  r  )facrO   rP   r   R      c                    s   g | ]
}|j  d  qS )r   )Zevalfr  r  r  rO   rP   r   T  s    c                 S   s   g | ]}t j| qS rO   )mpmathZmpcr  rO   rO   rP   r   W  s    z!Numerical domain expected, got %sF
   )maxstepscleanuperrorZ	extraprecc                    s$   | j rdnd| jt| j  | j fS )Nrh   r   )imagrealr  )r$  r  rO   rP   <lambda>h  s   $ zPoly.nroots.<locals>.<lambda>keyz7convergence to root failed; try n < %s or maxsteps > %s)Z$sympy.functions.elementary.complexesr  is_multivariater.   r5  rT   r   r   r   r   Zsympy.core.numbersr  r;  r)   r  ZmpdpsZ	polyrootsr_   r   r   sortedr@   )	rJ   r   r  r  r   Zdenomsr  r  r  rO   )r  r   r  rP   nroots(  sX   


zPoly.nrootsc                 C   sP   | j r	td|  i }|  d D ]\}}|jr%| \}}||| | < q|S )a  
        Compute roots of ``f`` by factorization in the ground domain.

        Examples
        ========

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

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

        z can't compute ground roots of %srh   )r  r.   r  	is_linearr   )rJ   r  factorr  r[  r\  rO   rO   rP   ground_rootsr  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 polynomialrh   z&'n' must an integer and n >= 1, got %srk  )r  r.   r   
is_Integerr   r   r   r   ro  r   rC   r   )rJ   r   rG  r   rk  r$  rO   rO   rP   nth_power_roots_poly  s   
zPoly.nth_power_roots_polyc                 C   s   |  |\}}}}t|dr|j||d}nt| d|s?|jr$| }|\}}	}
}||}||	}	||	 ||
||fS tt||S )a  
        Cancel common factors in a rational function ``f/g``.

        Examples
        ========

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

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

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

        cancel)include)	r   r   r  r(   rX  rY  r   r   r   )rJ   rK   r  r   r   r   r   rL   cpcqr  r#  rO   rO   rP   r    s   



zPoly.cancelc                 C   r   )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

        )rT   is_zeror   rO   rO   rP   r       zPoly.is_zeroc                 C   r   )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

        )rT   is_oner   rO   rO   rP   r    r  zPoly.is_onec                 C   r   )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

        )rT   r  r   rO   rO   rP   r     r  zPoly.is_sqfc                 C   r   )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

        )rT   is_monicr   rO   rO   rP   r    r  zPoly.is_monicc                 C   r   )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

        )rT   is_primitiver   rO   rO   rP   r  &  r  zPoly.is_primitivec                 C   r   )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

        )rT   	is_groundr   rO   rO   rP   r  9  s   zPoly.is_groundc                 C   r   )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

        )rT   r  r   rO   rO   rP   r  N  r  zPoly.is_linearc                 C   r   )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

        )rT   is_quadraticr   rO   rO   rP   r  a  r  zPoly.is_quadraticc                 C   r   )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

        )rT   is_monomialr   rO   rO   rP   r  t  r  zPoly.is_monomialc                 C   r   )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

        )rT   is_homogeneousr   rO   rO   rP   r       zPoly.is_homogeneousc                 C   r   )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

        )rT   is_irreducibler   rO   rO   rP   r    r  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

        rh   rj   rD   r   rO   rO   rP   r        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

        rh   r  r   rO   rO   rP   r    r  zPoly.is_multivariatec                 C   r   )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

        )rT   is_cyclotomicr   rO   rO   rP   r    r  zPoly.is_cyclotomicc                 C   r   rm   )r  r   rO   rO   rP   __abs__     zPoly.__abs__c                 C   r   rm   )r  r   rO   rO   rP   __neg__  r  zPoly.__neg__c                 C   rf  rm   r   rJ   rK   rO   rO   rP   __add__     
zPoly.__add__c                 C   
   | | S rm   r  r  rO   rO   rP   __radd__  r  zPoly.__radd__c                 C   rf  rm   r  r  rO   rO   rP   __sub__  r  zPoly.__sub__c                 C   r  rm   r  r  rO   rO   rP   __rsub__
  r  zPoly.__rsub__c                 C   rf  rm   r   r  rO   rO   rP   __mul__  r  zPoly.__mul__c                 C   r  rm   r   r  rO   rO   rP   __rmul__  r  zPoly.__rmul__r   c                 C   s   |j r|dkr| |S tS )Nr   )r  r   rE   )rJ   r   rO   rO   rP   __pow__  s   
zPoly.__pow__c                 C   rf  rm   r*  r  rO   rO   rP   
__divmod__  r  zPoly.__divmod__c                 C   r  rm   r  r  rO   rO   rP   __rdivmod__!  r  zPoly.__rdivmod__c                 C   rf  rm   r1  r  rO   rO   rP   __mod__%  r  zPoly.__mod__c                 C   r  rm   r  r  rO   rO   rP   __rmod__)  r  zPoly.__rmod__c                 C   rf  rm   r3  r  rO   rO   rP   __floordiv__-  r  zPoly.__floordiv__c                 C   r  rm   r
  r  rO   rO   rP   __rfloordiv__1  r  zPoly.__rfloordiv__rK   c                 C   s   |   |   S rm   rG   r  rO   rO   rP   __truediv__5     zPoly.__truediv__c                 C   s   |  |    S rm   r  r  rO   rO   rP   __rtruediv__9  r  zPoly.__rtruediv__otherc              
   C   sv   | |}}|j s#z|j||j| d}W n tttfy"   Y dS w |j|jkr+dS |jj|jjkr5dS |j|jkS Nr   F)	r`   r   rD   r   r-   r)   r*   rT   r   )rp   r  rJ   rK   rO   rO   rP   __eq__=  s   
zPoly.__eq__c                 C   
   | |k S rm   rO   r  rO   rO   rP   __ne__O  r  zPoly.__ne__c                 C   s   | j  S rm   )r  r   rO   rO   rP   __bool__S  r  zPoly.__bool__c                 C   s   |s| |kS |  t|S rm   )
_strict_eqr   rJ   rK   strictrO   rO   rP   eqV  s   zPoly.eqc                 C   s   | j ||d S )Nr  )r  r  rO   rO   rP   ne\  s   zPoly.nec                 C   s*   t || jo| j|jko| jj|jddS NTr  )rA   r   rD   rT   r  r  rO   rO   rP   r  _  s   *zPoly._strict_eqNNrm   )FF)FTr   )rh   FNT)FNNNFFNNFFr  r  r  T)rH   
__module____qualname____doc__	__slots__is_commutativer`   Z_op_priorityrg   classmethodrl   propertyrq   re   rs   rv   ry   r{   rC   r]   r^   ra   rb   rZ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rV   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   r  r  r  r	  r  r  r  r  r  r  r  r  r  r   r  r   r  r   r"  r&  r'  r(  r*  r1  r3  r4  r   r5  r7  r8  r9  r:  r>  r@  rC  rF  rD  r   rM  rN  rR  rS  rT  rU  rV  r]  r^  rc  Z_eval_derivativer   rg  rh  rj  rl  rm  rn  ro  rr  rt  rw  rx  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  rR   r  r  r  r   r  r  r   rE   r  r  r  r  r	  r  r  r  r  r  r  r  r  r  r  __classcell__rO   rO   r   rP   rB   Y   s   /











	






3#$"%&%*''%%*"%"''(&K!"%KK
#!L%?J(%




rB   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. )rT   ro   rO   rO   rP   rs   g  r   zPurePoly._hashable_contentc                    r   rm   r   ro   r   rO   rP   r   k  r   zPurePoly.__hash__c                 C   s   | j S )aR  
        Free symbols of a polynomial.

        Examples
        ========

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

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

        )r   ro   rO   rO   rP   r   n  s   zPurePoly.free_symbolsr  c              
   C   s   | |}}|j s#z|j||j| d}W n tttfy"   Y dS w t|jt|jkr/dS |jj	|jj	krYz|jj	
|jj	|j}W n
 tyN   Y dS w ||}||}|j|jkS r  )r`   r   rD   r   r-   r)   r*   rj   rT   r   r   r+   r   )rp   r  rJ   rK   r   rO   rO   rP   r    s$   


zPurePoly.__eq__c                 C   s   t || jo| jj|jddS r  )rA   r   rT   r  r  rO   rO   rP   r    s   zPurePoly._strict_eqc                    s   t |}|js+z| jj| j| j| j| jj|fW S  ty*   td| |f w t| j	t|j	kr=td| |f t
| jtrIt
|jtsQtd| |f | j | j	}| jj|jj|}| j|}|j|}||d f fdd	}||||fS )Nr   c                    r   r   r   r   r   rO   rP   r     r   zPurePoly._unify.<locals>.per)r   r`   rT   r   r   r   r*   r+   rj   rD   rA   r%   r   r   r   )rJ   rK   rD   r   r   r   r   rO   r   rP   r     s$   (	zPurePoly._unify)rH   r$  r%  r&  rs   r   r*  r   r   rE   r  r  r   r+  rO   rO   r   rP   r,  c  s    

r,  c                 O      t ||}t| |S r|   )rW   rX   _poly_from_exprrq   rD   re   rf   rO   rO   rP   poly_from_expr     
r0  c                 C   s  | t | }} t| tst||| | jr0| j| |}|j|_|j|_|j	du r,d|_	||fS |j
r7| 
 } t| |\}}|jsGt||| ttt|  \}}|j}|du rdt||d\|_}ntt|j|}ttt||}t||}|j	du rd|_	||fS )r}   NTr~   F)r   rA   r   r0   r`   r   ra   rD   r   r  expandr7   r_   r   r   r   r   r   r\   rB   r]   )rq   rf   origr  rT   r   r   r   rO   rO   rP   r.    s2   


r.  c                 O   r-  )(Construct polynomials from expressions. )rW   rX   _parallel_poly_from_expr)exprsrD   re   rf   rO   rO   rP   parallel_poly_from_expr  r1  r7  c                 C   s  ddl m} t| dkrE| \}}t|trEt|trE|j||}|j||}||\}}|j|_|j	|_	|j
du r?d|_
||g|fS t| g }} g g }}d}t|D ]*\}	}
t|
}
t|
trz|
jrm||	 n||	 |jry|
 }
nd}| |
 qW|rt||| d|r|D ]
}	| |	  | |	< qt| |\}}|jst||| d|jD ]}t||rtdqg g }}g }g }|D ]}ttt|  \}}|| || |t| q|j	}|du rt||d\|_	}ntt|j|}|D ]}||d|  ||d }q g }t||D ]\}}ttt||}t||}|| q|j
du r?t||_
||fS )	r4  r   	Piecewise   NTFz&Piecewise generators do not make senser~   )$sympy.functions.elementary.piecewiser9  rj   rA   rB   r   ra   r   rD   r   r  r_   r   r   r   r`   r   r2  r0   rG   r8   r-   r   r   extendr   r   r   r\   r]   bool)r6  rf   r9  rJ   rK   ZorigsZ_exprsZ_polysZfailedr   rq   repsr  Zcoeffs_listZlengthsr   r   rT   r   r   r   r  r  rO   rO   rP   r5    sz   









r5  c                 C   s   t | } || vr|| |< | S )z7Add a new ``(key, value)`` pair to arguments ``dict``. )r\   )re   r  r  rO   rO   rP   _update_argsV  s   r?  c                 C   s   t | dd} t |ddj}| jr| }| j}n| j}|s-|r&t| \}}nt| |\}}|r7| r4tjS tjS |sR| jrI||jvrIt|  \}}||jvrQtjS n| jslt	| j
dkrlttd| tt| j
|f ||}t|trzt|S tjS )a  
    Return the degree of ``f`` in the given variable.

    The degree of 0 is negative infinity.

    Examples
    ========

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

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

    See also
    ========

    sympy.polys.polytools.Poly.total_degree
    degree_list
    Tr  rh   z
         A symbolic generator of interest is required for a multivariate
         expression like func = %s, e.g. degree(func, gen = %s) instead of
         degree(func, gen = %s).
        )r   	is_Numberr`   rG   r0  r   Zeror  rD   rj   r   r;  r>   nextr   r5  rA   r   r
   )rJ   r   Z
gen_is_Numr  ZisNumr   rL   rO   rO   rP   r5  `  s0   

r5  c                 G   sN   t | }|jr| }|jrd}t|S | jr|p| j}t|| }t|S )a  
    Return the total_degree of ``f`` in the given variables.

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

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

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

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

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

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

    This is done automatically if any variables are given:

    >>> total_degree(p, x)
    1

    See also
    ========
    degree
    r   )r   r`   rG   r@  rD   rB   r8  r
   )rJ   rD   r  ZrvrO   rO   rP   r8    s   (
r8  c              
   O   sh   t |dg zt| g|R i |\}}W n ty( } ztdd|d}~ww | }ttt|S )z
    Return a list of degrees of ``f`` in all variables.

    Examples
    ========

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

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

    r  r7  rh   N)	rW   allowed_flagsr0  r0   r1   r7  r   r   r
   )rJ   rD   re   r   rf   r)  ZdegreesrO   rO   rP   r7    s   r7  c              
   O   s`   t |dg zt| g|R i |\}}W n ty( } ztdd|d}~ww |j|jdS )z
    Return the leading coefficient of ``f``.

    Examples
    ========

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

    >>> LC(4*x**2 + 2*x*y**2 + x*y + 3*y)
    4

    r  r>  rh   Nr   )rW   rC  r0  r0   r1   r>  rU   rJ   rD   re   r   rf   r)  rO   rO   rP   r>    s   r>  c              
   O   sh   t |dg zt| g|R i |\}}W n ty( } ztdd|d}~ww |j|jd}| S )z
    Return the leading monomial of ``f``.

    Examples
    ========

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

    >>> LM(4*x**2 + 2*x*y**2 + x*y + 3*y)
    x**2

    r  rM  rh   Nr   )rW   rC  r0  r0   r1   rM  rU   rG   )rJ   rD   re   r   rf   r)  r   rO   rO   rP   rM    s   rM  c              
   O   sp   t |dg zt| g|R i |\}}W n ty( } ztdd|d}~ww |j|jd\}}||  S )z
    Return the leading term of ``f``.

    Examples
    ========

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

    >>> LT(4*x**2 + 2*x*y**2 + x*y + 3*y)
    4*x**2

    r  rR  rh   Nr   )rW   rC  r0  r0   r1   rR  rU   rG   )rJ   rD   re   r   rf   r)  r   r   rO   rO   rP   rR    s   rR  c           
   
   O   s   t |dg zt| |fg|R i |\\}}}W n ty, } ztdd|d}~ww ||\}}	|js?| |	 fS ||	fS )z
    Compute polynomial pseudo-division of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import pdiv
    >>> from sympy.abc import x

    >>> pdiv(x**2 + 1, 2*x - 4)
    (2*x + 4, 20)

    r  r"  r:  N)rW   rC  r7  r0   r1   r"  r  rG   
rJ   rK   rD   re   r   r   rf   r)  r#  r$  rO   rO   rP   r"  8  s   &r"  c           	   
   O   v   t |dg zt| |fg|R i |\\}}}W n ty, } ztdd|d}~ww ||}|js9| S |S )z
    Compute polynomial pseudo-remainder of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import prem
    >>> from sympy.abc import x

    >>> prem(x**2 + 1, 2*x - 4)
    20

    r  r&  r:  N)rW   rC  r7  r0   r1   r&  r  rG   	rJ   rK   rD   re   r   r   rf   r)  r$  rO   rO   rP   r&  V     &
r&  c           	   
   O   s   t |dg zt| |fg|R i |\\}}}W n ty, } ztdd|d}~ww z||}W n ty@   t| |w |jsH| S |S )z
    Compute polynomial pseudo-quotient of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import pquo
    >>> from sympy.abc import x

    >>> pquo(x**2 + 1, 2*x - 4)
    2*x + 4
    >>> pquo(x**2 - 1, 2*x - 1)
    2*x + 1

    r  r'  r:  N)	rW   rC  r7  r0   r1   r'  r/   r  rG   	rJ   rK   rD   re   r   r   rf   r)  r#  rO   rO   rP   r'  t  s   &
r'  c           	   
   O   rF  )a_  
    Compute polynomial exact pseudo-quotient of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import pexquo
    >>> from sympy.abc import x

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

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

    r  r(  r:  N)rW   rC  r7  r0   r1   r(  r  rG   rI  rO   rO   rP   r(    s   &
r(  c           
   
   O   s   t |ddg zt| |fg|R i |\\}}}W n ty- } ztdd|d}~ww |j||jd\}}	|jsC| |	 fS ||	fS )a  
    Compute polynomial division of ``f`` and ``g``.

    Examples
    ========

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

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

    r-  r  r*  r:  Nr-  )	rW   rC  r7  r0   r1   r*  r-  r  rG   rE  rO   rO   rP   r*    s   &r*  c           	   
   O   ~   t |ddg zt| |fg|R i |\\}}}W n ty- } ztdd|d}~ww |j||jd}|js=| S |S )a  
    Compute polynomial remainder of ``f`` and ``g``.

    Examples
    ========

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

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

    r-  r  r1  r:  NrJ  )	rW   rC  r7  r0   r1   r1  r-  r  rG   rG  rO   rO   rP   r1       &r1  c           	   
   O   rK  )z
    Compute polynomial quotient of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import quo
    >>> from sympy.abc import x

    >>> quo(x**2 + 1, 2*x - 4)
    x/2 + 1
    >>> quo(x**2 - 1, x - 1)
    x + 1

    r-  r  r3  r:  NrJ  )	rW   rC  r7  r0   r1   r3  r-  r  rG   rI  rO   rO   rP   r3    rL  r3  c           	   
   O   rK  )aQ  
    Compute polynomial exact quotient of ``f`` and ``g``.

    Examples
    ========

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

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

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

    r-  r  r4  r:  NrJ  )	rW   rC  r7  r0   r1   r4  r-  r  rG   rI  rO   rO   rP   r4    s   &r4  c                 O   s   t |ddg zt| |fg|R i |\\}}}W n= tyX } z1t|j\}\}	}
z
||	|
\}}W n tyC   tdd|w |	||	|fW  Y d}~S d}~ww |j||j
d\}}|jsn| | fS ||fS )aT  
    Half extended Euclidean algorithm of ``f`` and ``g``.

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

    Examples
    ========

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

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

    r-  r  rh  r:  NrJ  )rW   rC  r7  r0   r   r6  rh  rY   r1   r   r-  r  rG   )rJ   rK   rD   re   r   r   rf   r)  r   r[  r\  r=  ri  rO   rO   rP   rh  =  s"   & 
rh  c                 O   s   t |ddg zt| |fg|R i |\\}}}W nB ty] } z6t|j\}\}	}
z||	|
\}}}W n tyD   tdd|w |	||	||	|fW  Y d}~S d}~ww |j||j
d\}}}|jsw| | | fS |||fS )aZ  
    Extended Euclidean algorithm of ``f`` and ``g``.

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

    Examples
    ========

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

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

    r-  r  rj  r:  NrJ  )rW   rC  r7  r0   r   r6  rj  rY   r1   r   r-  r  rG   )rJ   rK   rD   re   r   r   rf   r)  r   r[  r\  r=  rk  ri  rO   rO   rP   rj  d  s"   &(

rj  c                 O   s   t |ddg zt| |fg|R i |\\}}}W n3 tyN } z't|j\}\}	}
z|||	|
W W  Y d}~S  tyI   t	dd|w d}~ww |j||j
d}|js^| S |S )aE  
    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 :obj:`~.mod_inverse` function:

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

    See Also
    ========

    sympy.core.numbers.mod_inverse

    r-  r  Nrl  r:  rJ  )rW   rC  r7  r0   r   r6  r   rl  rY   r1   r-  r  rG   )rJ   rK   rD   re   r   r   rf   r)  r   r[  r\  ri  rO   rO   rP   rl    s    "& rl  c           	   
   O   s|   t |dg zt| |fg|R i |\\}}}W n ty, } ztdd|d}~ww ||}|js<dd |D S |S )z
    Compute subresultant PRS of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import subresultants
    >>> from sympy.abc import x

    >>> subresultants(x**2 + 1, x**2 - 1)
    [x**2 + 1, x**2 - 1, -2]

    r  rn  r:  Nc                 S      g | ]}|  qS rO   r  r   r$  rO   rO   rP   r     r  z!subresultants.<locals>.<listcomp>)rW   rC  r7  r0   r1   rn  r  	rJ   rK   rD   re   r   r   rf   r)  rL   rO   rO   rP   rn    s   &
rn  Frp  c             
   O   s   t |dg zt| |fg|R i |\\}}}W n ty, } ztdd|d}~ww |r9|j||d\}	}
n||}	|jsR|rN|	 dd |
D fS |	 S |rX|	|
fS |	S )z
    Compute resultant of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import resultant
    >>> from sympy.abc import x

    >>> resultant(x**2 + 1, x**2 - 1)
    4

    r  ro  r:  Nrp  c                 S   rM  rO   r  rN  rO   rO   rP   r     r  zresultant.<locals>.<listcomp>)rW   rC  r7  r0   r1   ro  r  rG   )rJ   rK   rq  rD   re   r   r   rf   r)  rL   r/  rO   rO   rP   ro    s"   &
ro  c              
   O   l   t |dg zt| g|R i |\}}W n ty( } ztdd|d}~ww | }|js4| S |S )z
    Compute discriminant of ``f``.

    Examples
    ========

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

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

    r  rr  rh   N)rW   rC  r0  r0   r1   rr  r  rG   rJ   rD   re   r   rf   r)  rL   rO   rO   rP   rr       rr  c                 O   s   t |dg zt| |fg|R i |\\}}}W nB ty\ } z6t|j\}\}	}
z||	|
\}}}W n tyC   tdd|w |	||	||	|fW  Y d}~S d}~ww ||\}}}|j
ss| | | fS |||fS )a  
    Compute GCD and cofactors of ``f`` and ``g``.

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

    Examples
    ========

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

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

    r  rx  r:  N)rW   rC  r7  r0   r   r6  rx  rY   r1   r   r  rG   )rJ   rK   rD   re   r   r   rf   r)  r   r[  r\  ri  ry  rz  rO   rO   rP   rx  "  s"   &(

rx  c              
      s  t | } fdd}|| }|dur|S tdg zOt| gR i \}}t| dkrjtdd | D rj| d   fd	d
| dd D }tdd |D rjd}|D ]}	t||	 d }qWt | W S W n% t	y }
 z||
j
}|dur|W  Y d}
~
S tdt| |
d}
~
ww |s|jstjS td|dS |d |dd }}|D ]}||}|jr nq|js| S |S )z
    Compute GCD of a list of polynomials.

    Examples
    ========

    >>> from sympy import gcd_list
    >>> from sympy.abc import x

    >>> gcd_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2])
    x - 1

    c                    sl   s4 s4t | \}}|s|jS |jr4|d |dd  }}|D ]}|||}||r. nq||S d S Nr   rh   )r   r   r  r{  r  r   seqr   ZnumbersrL   Znumberre   rD   rO   rP   try_non_polynomial_gcd\  s   

z(gcd_list.<locals>.try_non_polynomial_gcdNr  rh   c                 s       | ]	}|j o	|jV  qd S rm   is_algebraicis_irrationalr   r   rO   rO   rP   	<genexpr>z      zgcd_list.<locals>.<genexpr>rB  c                       g | ]} |   qS rO   ratsimpr\  r[  rO   rP   r   |  r  zgcd_list.<locals>.<listcomp>c                 s       | ]}|j V  qd S rm   is_rationalr   frcrO   rO   rP   r]  }      r   gcd_listr~   )r   rW   rC  r7  rj   r  r}  as_numer_denomr  r0   r6  r1   r  r   rA  rB   r{  r  rG   )rU  rD   re   rW  rL   r  rf   lstlcrg  r)  r  rO   r[  re   rD   rP   ri  K  sJ   

ri  c                 O   sL  t | dr|dur|f| }t| g|R i |S |du r!tdt|dg z<t| |fg|R i |\\}}}tt| |f\}}|jrc|j	rc|jrc|j	rc|| 
 }	|	jrct||	 d  W S W n3 ty }
 z't|
j\}\}}z||||W W  Y d}
~
S  ty   tdd|
w d}
~
ww ||}|js| S |S )z
    Compute GCD of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import gcd
    >>> from sympy.abc import x

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

    __iter__Nz2gcd() takes 2 arguments or a sequence of argumentsr  r   r{  r:  )r   ri  r;  rW   rC  r7  r   r   rZ  r[  ra  re  r  rj  r0   r   r6  r   r{  rY   r1   r  rG   rJ   rK   rD   re   r   r   rf   r[  r\  rg  r)  r   rL   rO   rO   rP   r{    s8   

" 
r{  c              
      sv  t | } fdd}|| }|dur|S tdg zMt| gR i \}}t| dkrhtdd | D rh| d   fd	d
| dd D }tdd |D rhd}|D ]}	t||	 d }qW | W S W n% ty }
 z||
j	}|dur|W  Y d}
~
S t
dt| |
d}
~
ww |s|jstjS td|dS |d |dd }}|D ]}||}q|js| S |S )z
    Compute LCM of a list of polynomials.

    Examples
    ========

    >>> from sympy import lcm_list
    >>> from sympy.abc import x

    >>> lcm_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2])
    x**5 - x**4 - 2*x**3 - x**2 + x + 2

    c                    s^   s- s-t | \}}|s|jS |jr-|d |dd  }}|D ]}|||}q||S d S rS  )r   r   r  r}  r   rT  rV  rO   rP   try_non_polynomial_lcm  s   
z(lcm_list.<locals>.try_non_polynomial_lcmNr  rh   c                 s   rX  rm   rY  r\  rO   rO   rP   r]     r^  zlcm_list.<locals>.<genexpr>rB  c                    r_  rO   r`  r\  rb  rO   rP   r     r  zlcm_list.<locals>.<listcomp>c                 s   rc  rm   rd  rf  rO   rO   rP   r]    rh  lcm_listr~   r   )r   rW   rC  r7  rj   r  r}  rj  r0   r6  r1   r  r   rW  rB   rG   )rU  rD   re   rp  rL   r  rf   rk  rl  rg  r)  r  rO   rm  rP   rq    sD   

rq  c                 O   sH  t | dr|dur|f| }t| g|R i |S |du r!tdt|dg z:t| |fg|R i |\\}}}tt| |f\}}|jra|j	ra|jra|j	ra|| 
 }	|	jra||	 d  W S W n3 ty }
 z't|
j\}\}}z||||W W  Y d}
~
S  ty   tdd|
w d}
~
ww ||}|js| S |S )z
    Compute LCM of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import lcm
    >>> from sympy.abc import x

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

    rn  Nz2lcm() takes 2 arguments or a sequence of argumentsr  rh   r}  r:  )r   rq  r;  rW   rC  r7  r   r   rZ  r[  ra  re  rj  r0   r   r6  r   r}  rY   r1   r  rG   ro  rO   rO   rP   r}  "  s8   

" 
r}  c              
      s  ddl m} t| }t| |r| fdd| j| jfD  S t| tr+td| f t| tr3| j	r5|S  
ddr]| j fdd	| jD  } d d d
< t|gR i  S  dd}t dg zt| gR i  \}}W n ty }	 z	|	jW  Y d}	~	S d}	~	ww | \}
} |jjr|jjr| jdd\}} |  \}} |jjr|| }ntj}tdd	 t| j|
D  }|dkrtj}|dkr|S |rt|||   S t||  dd  \}} t|||  ddS )az  
    Remove GCD of terms from ``f``.

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

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

    Examples
    ========

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

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

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

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

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

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

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

    Rationals are factored out by default:

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

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

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

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

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

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

    r   )Equalityc                 3   s&    | ]}t |gR i  V  qd S rm   r  )r   r=  rV  rO   rP   r]    s   $ zterms_gcd.<locals>.<genexpr>z6Inequalities can not be used with terms_gcd. Found: %sdeepFc                    s"   g | ]}t |gR i  qS rO   rs  )r   r[  rV  rO   rP   r     r   zterms_gcd.<locals>.<listcomp>r2  clearTr  NrZ  c                 S   s   g | ]\}}|| qS rO   rO   )r   r   r   rO   rO   rP   r     r  rh   )ru  )!sympy.core.relationalrr  r   rA   ZlhsZrhsr   r;  r   is_Atomr`  rN   re   popr  rW   rC  r0  r0   rq   r   r+  r,  rV  r  r   rW  r   r   rD   r   rG   Zas_coeff_Mul)rJ   rD   re   rr  r3  rl   ru  r   rf   r)  r   denomr   termrO   rV  rP   r  U  sN   B
 

r  c              
   O   st   t |ddg zt| g|R i |\}}W n ty) } ztdd|d}~ww |t|}|js8| S |S )z
    Reduce ``f`` modulo a constant ``p``.

    Examples
    ========

    >>> from sympy import trunc
    >>> from sympy.abc import x

    >>> trunc(2*x**3 + 3*x**2 + 5*x + 7, 3)
    -x**3 - x + 1

    r-  r  r~  rh   N)	rW   rC  r0  r0   r1   r~  r   r  rG   )rJ   r  rD   re   r   rf   r)  rL   rO   rO   rP   r~       r~  c              
   O   st   t |ddg zt| g|R i |\}}W n ty) } ztdd|d}~ww |j|jd}|js8| S |S )z
    Divide all coefficients of ``f`` by ``LC(f)``.

    Examples
    ========

    >>> from sympy import monic
    >>> from sympy.abc import x

    >>> monic(3*x**2 + 4*x + 2)
    x**2 + 4*x/3 + 2/3

    r-  r  r  rh   NrJ  )	rW   rC  r0  r0   r1   r  r-  r  rG   rQ  rO   rO   rP   r    r{  r  c              
   O   sX   t |dg zt| g|R i |\}}W | S  ty+ } ztdd|d}~ww )z
    Compute GCD of coefficients of ``f``.

    Examples
    ========

    >>> from sympy import content
    >>> from sympy.abc import x

    >>> content(6*x**2 + 8*x + 12)
    2

    r  r  rh   N)rW   rC  r0  r0   r1   r  rD  rO   rO   rP   r    s   r  c              
   O   sx   t |dg zt| g|R i |\}}W n ty( } ztdd|d}~ww | \}}|js8|| fS ||fS )a  
    Compute content and the primitive form of ``f``.

    Examples
    ========

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

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

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

    Expansion is performed by default:

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

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

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

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

    r  r  rh   N)rW   rC  r0  r0   r1   r  r  rG   )rJ   rD   re   r   rf   r)  r  rL   rO   rO   rP   r  !  s    r  c           	   
   O   rF  )z
    Compute functional composition ``f(g)``.

    Examples
    ========

    >>> from sympy import compose
    >>> from sympy.abc import x

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

    r  r  r:  N)rW   rC  r7  r0   r1   r  r  rG   rO  rO   rO   rP   r  O  rH  r  c              
   O   r   t |dg zt| g|R i |\}}W n ty( } ztdd|d}~ww | }|js7dd |D S |S )z
    Compute functional decomposition of ``f``.

    Examples
    ========

    >>> from sympy import decompose
    >>> from sympy.abc import x

    >>> decompose(x**4 + 2*x**3 - x - 1)
    [x**2 - x - 1, x**2 + x]

    r  r  rh   Nc                 S   rM  rO   r  rN  rO   rO   rP   r     r  zdecompose.<locals>.<listcomp>)rW   rC  r0  r0   r1   r  r  rQ  rO   rO   rP   r  m  s   r  c              
   O   sz   t |ddg zt| g|R i |\}}W n ty) } ztdd|d}~ww |j|jd}|js;dd |D S |S )	z
    Compute Sturm sequence of ``f``.

    Examples
    ========

    >>> from sympy import sturm
    >>> from sympy.abc import x

    >>> sturm(x**3 - 2*x**2 + x - 3)
    [x**3 - 2*x**2 + x - 3, 3*x**2 - 4*x + 1, 2*x/9 + 25/9, -2079/4]

    r-  r  r  rh   NrJ  c                 S   rM  rO   r  rN  rO   rO   rP   r     r  zsturm.<locals>.<listcomp>)rW   rC  r0  r0   r1   r  r-  r  rQ  rO   rO   rP   r    s   r  c              
   O   r|  )a&  
    Compute a list of greatest factorial factors of ``f``.

    Note that the input to ff() and rf() should be Poly instances to use the
    definitions here.

    Examples
    ========

    >>> from sympy import gff_list, ff, Poly
    >>> from sympy.abc import x

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

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

    >>> (ff(Poly(x), 1)*ff(Poly(x + 2), 4)) == f
    True

    >>> f = Poly(x**12 + 6*x**11 - 11*x**10 - 56*x**9 + 220*x**8 + 208*x**7 -         1401*x**6 + 1090*x**5 + 2715*x**4 - 6720*x**3 - 1092*x**2 + 5040*x, x)

    >>> gff_list(f)
    [(Poly(x**3 + 7, x, domain='ZZ'), 2), (Poly(x**2 + 5*x, x, domain='ZZ'), 3)]

    >>> ff(Poly(x**3 + 7, x), 2)*ff(Poly(x**2 + 5*x, x), 3) == f
    True

    r  r  rh   Nc                 S      g | ]
\}}|  |fqS rO   r  r  rO   rO   rP   r     r   zgff_list.<locals>.<listcomp>)rW   rC  r0  r0   r1   r  r  )rJ   rD   re   r   rf   r)  r  rO   rO   rP   r    s    r  c                 O   rH  )z3Compute greatest factorial factorization of ``f``. zsymbolic falling factorialrI  rJ   rD   re   rO   rO   rP   gff  s   r  c           	   
   O   s   t |dg zt| g|R i |\}}W n ty( } ztdd|d}~ww | \}}}|js>t|| | fS t|||fS )a  
    Compute square-free norm of ``f``.

    Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and
    ``r(x) = Norm(g(x))`` is a square-free polynomial over ``K``,
    where ``a`` is the algebraic extension of the ground domain.

    Examples
    ========

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

    >>> sqf_norm(x**2 + 1, extension=[sqrt(3)])
    (1, x**2 - 2*sqrt(3)*x + 4, x**4 - 4*x**2 + 16)

    r  r  rh   N)	rW   rC  r0  r0   r1   r  r  r
   rG   )	rJ   rD   re   r   rf   r)  r=  rK   r$  rO   rO   rP   r    s   r  c              
   O   rP  )z
    Compute square-free part of ``f``.

    Examples
    ========

    >>> from sympy import sqf_part
    >>> from sympy.abc import x

    >>> sqf_part(x**3 - 3*x - 2)
    x**2 - x - 2

    r  r  rh   N)rW   rC  r0  r0   r1   r  r  rG   rQ  rO   rO   rP   r     rR  r  c                 C   s&   |dkr	dd }ndd }t | |dS )z&Sort a list of ``(expr, exp)`` pairs. r  c                 S   s&   | \}}|j j }|t|t|j|fS rm   rT   rj   rD   rk   r  exprT   rO   rO   rP   r  !     z_sorted_factors.<locals>.keyc                 S   s&   | \}}|j j }t|t|j||fS rm   r  r  rO   rO   rP   r  &  r  r  )r  )r  methodr  rO   rO   rP   _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 rO   r  r   rJ   r  rO   rO   rP   r   0  r   z$_factors_product.<locals>.<listcomp>)r   r  rO   rO   rP   _factors_product.  s   r  c                    s  t jg }dd t| D }|D ]}|js t|tr%t|r%||9 }q|jrJ|j	t j
krJ|j\} |jr> jr>||9 }q|jrI| f qn|t j} z	t||\}}W n tys }	 z|	j f W Y d}	~	qd}	~	ww t||d }
|
 \}}|t jur jr||  9 }n|jr| f n||t jf  t ju r| q jr fdd|D  qg }|D ]\}}| jrԈ||  f q|||f qt| f q|dkrfdddd	 D D |fS )
z.Helper function for :func:`_symbolic_factor`. c                 S   s"   g | ]}t |d r| n|qS )_eval_factor)r   r  r   r   rO   rO   rP   r   7  s    z)_symbolic_factor_list.<locals>.<listcomp>NZ_listc                    s   g | ]
\}}||  fqS rO   rO   r  )r  rO   rP   r   [  r   r  c                    s(   g | ] t t fd dD  fqS )c                 3   s     | ]\}}| kr|V  qd S rm   rO   )r   rJ   r   r  rO   rP   r]  g  s    z3_symbolic_factor_list.<locals>.<listcomp>.<genexpr>)r   r   )r   r  r  rP   r   g  s     c                 S   s   h | ]\}}|qS rO   rO   )r   r   r   rO   rO   rP   	<setcomp>h  r  z(_symbolic_factor_list.<locals>.<setcomp>)r   rW  r   	make_argsr@  rA   r   r   is_PowbaseZExp1re   r   r.  r0   rq   rF   r  Zis_positiver<  
is_integerrG   r  )rq   rf   r  r   re   argr  r  r   r)  rN   Z_coeffZ_factorsr  rJ   r  rO   )r  r  rP   _symbolic_factor_list3  s^   





r  c                    s   t | tr#t| dr|  S tt| d d \}}t|t|S t| dr6| j fdd| j	D  S t| drH| 
 fdd| D S | S )	z%Helper function for :func:`_factor`. r  fraction)r  re   c                       g | ]}t | qS rO   _symbolic_factorr   r  r  rf   rO   rP   r   u  r  z$_symbolic_factor.<locals>.<listcomp>rn  c                    r  rO   r  r  r  rO   rP   r   w  r  )rA   r   r   r  r  r9   r   r  rN   re   r   )rq   rf   r  r   r  rO   r  rP   r  m  s   



r  c                 C   sD  t |ddg t ||}t| } t| ttfrt| tr$| d}}nt|  \}}t	|||\}}t	|||\}	}
|
rG|j
sGtd|  |tdd}||
fD ]}t|D ]\}\}}|jsot||\}}||f||< qYqSt||}t|
|}
|jsdd |D }d	d |
D }
||	 }|j
s||fS |||
fS td|  )
z>Helper function for :func:`sqf_list` and :func:`factor_list`. fracr  rh   za polynomial expected, got %sT)r2  c                 S   r}  rO   r  r  rO   rO   rP   r     r   z(_generic_factor_list.<locals>.<listcomp>c                 S   r}  rO   r  r  rO   rO   rP   r     r   )rW   rC  rX   r   rA   r   rB   r9   rj  r  r  r-   cloner\   r   r`   r.  r  r  )rq   rD   re   r  rf   Znumerry  r  fpr  ZfqZ_optr  r   rJ   r  r   r   rO   rO   rP   _generic_factor_list|  s:   




r  c                 C   s<   | dd}t|g  t||}||d< tt| ||S )z4Helper function for :func:`sqf` and :func:`factor`. r  T)rx  rW   rC  rX   r  r   )rq   rD   re   r  r  rf   rO   rO   rP   _generic_factor  s
   r  c                    s   ddl m  d fdd	}d fdd	}dd	 }|  jrI|| rI|  }|| |}|r8|d |d
 d|d fS || |}|rIdd|d |d
 fS dS )a!  
    try to transform a polynomial to have rational coefficients

    try to find a transformation ``x = alpha*y``

    ``f(x) = lc*alpha**n * g(y)`` where ``g`` is a polynomial with
    rational coefficients, ``lc`` the leading coefficient.

    If this fails, try ``x = y + beta``
    ``f(x) = g(y)``

    Returns ``None`` if ``g`` not found;
    ``(lc, alpha, None, g)`` in case of rescaling
    ``(None, None, beta, g)`` in case of translation

    Notes
    =====

    Currently it transforms only polynomials without roots larger than 2.

    Examples
    ========

    >>> from sympy import sqrt, Poly, simplify
    >>> from sympy.polys.polytools import to_rational_coeffs
    >>> from sympy.abc import x
    >>> p = Poly(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}), x, domain='EX')
    >>> lc, r, _, g = to_rational_coeffs(p)
    >>> lc, r
    (7 + 5*sqrt(2), 2 - 2*sqrt(2))
    >>> g
    Poly(x**3 + x**2 - 1/4*x - 1/4, x, domain='QQ')
    >>> r1 = simplify(1/r)
    >>> Poly(lc*r**3*(g.as_expr()).subs({x:x*r1}), x, domain='EX') == p
    True

    r   simplifyNc                    sH  ddl m} t| jdkr| jd jsd| fS |  }|  }|p$| }| dd } fdd|D }t|dkr|d r |d |d  }g }t	t|D ]} || ||d   }	|	j
sf dS ||	 qR d| }
| jd }|| g}t	d|d D ]}|||d  |||    q|| } 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   rh   Nc                    s   g | ]} |qS rO   rO   )r   coeffxr  rO   rP   r     r  z<to_rational_coeffs.<locals>._try_rescale.<locals>.<listcomp>rB  )Zsympy.core.addr   rj   rD   rw  r5  r>  r  r   r   re  r   rB   )rJ   f1r   r   rl  r   Z
rescale1_xZcoeffs1r   r  Z	rescale_xr   r  r  rO   rP   _try_rescale  s4   

 
z(to_rational_coeffs.<locals>._try_rescalec           	         s   t | jdkr| jd jsd| fS |  }|p| }| dd } |d }|jrL|jsLt|j	dd dd\}}|j
|  | }||}||fS dS )a+  
        try translating ``x -> x + alpha`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the translating is successful,
        ``alpha`` is the translating factor, and ``f`` is the shifted
        polynomial; else ``alpha`` is ``None``.
        rh   r   Nc                 S   s
   | j du S r!  rd  )zrO   rO   rP   r    s   
 z<to_rational_coeffs.<locals>._try_translate.<locals>.<lambda>Tbinary)rj   rD   rw  r5  r  r   is_Addre  r<   re   rN   r  )	rJ   r  r   r   r   ZratZnonratZalphaf2r  rO   rP   _try_translate  s   

z*to_rational_coeffs.<locals>._try_translatec                 S   s|   ddl m} |  }d}|D ]-}t|D ]%}||j}dd | D }|s(qt|dkr0d}t|dkr:  dS qq|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wxrO   rO   rP   r     s    zAto_rational_coeffs.<locals>._has_square_roots.<locals>.<listcomp>r:  T)	sympy.core.exprtoolsr  r   r   r  r  r   minmax)r  r  r   Zhas_sqr   r   rJ   r$  rO   rO   rP   _has_square_roots  s    

z-to_rational_coeffs.<locals>._has_square_rootsrh   r:  rm   )sympy.simplify.simplifyr  r   r   r  )rJ   r  r  r  r  r$  rO   r  rP   to_rational_coeffs  s   &#

r  c              	   C   s
  ddl m} t| |dd}| }t|}|sdS |\}}}}	t|	 }
|r^||
d | ||  }|d| }g }|
dd d D ]}|||d ||| i|d f qC||fS |
d }g }|
dd d D ]}||d ||| i|d f ql||fS )a  
    helper function to factor polynomial using to_rational_coeffs

    Examples
    ========

    >>> from sympy.polys.polytools import _torational_factor_list
    >>> from sympy.abc import x
    >>> from sympy import sqrt, expand, Mul
    >>> p = expand(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}))
    >>> factors = _torational_factor_list(p, x); factors
    (-2, [(-x*(1 + sqrt(2))/2 + 1, 1), (-x*(1 + sqrt(2)) - 1, 1), (-x*(1 + sqrt(2)) + 1, 1)])
    >>> expand(factors[0]*Mul(*[z[0] for z in factors[1]])) == p
    True
    >>> p = expand(((x**2-1)*(x-2)).subs({x:x + sqrt(2)}))
    >>> factors = _torational_factor_list(p, x); factors
    (1, [(x - 2 + sqrt(2), 1), (x - 1 + sqrt(2), 1), (x + 1 + sqrt(2), 1)])
    >>> expand(factors[0]*Mul(*[z[0] for z in factors[1]])) == p
    True

    r   r  ZEXr   Nrh   )	r  r  rB   r5  r  r  rG   r   r   )r  r   r  Zp1r   resrl  r$  rk  rK   r  r   Zr1r[  r  rO   rO   rP   _torational_factor_list2  s(   *&r  c                 O      t | ||ddS )z
    Compute a list of square-free factors of ``f``.

    Examples
    ========

    >>> from sympy import sqf_list
    >>> from sympy.abc import x

    >>> sqf_list(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16)
    (2, [(x + 1, 2), (x + 2, 3)])

    r  r  r  r~  rO   rO   rP   r  ^     r  c                 O   r  )z
    Compute square-free factorization of ``f``.

    Examples
    ========

    >>> from sympy import sqf
    >>> from sympy.abc import x

    >>> sqf(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16)
    2*(x + 1)**2*(x + 2)**3

    r  r  )r  r~  rO   rO   rP   r  p  r  r  c                 O   r  )a  
    Compute a list of irreducible factors of ``f``.

    Examples
    ========

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

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

    r  r  r  r~  rO   rO   rP   r    r  r  )rt  c             
      s   t | } |rCddlm}  fdd}|| |} i }| tt}|D ]}t|gR i  }	|	js5|	jr=|	|kr=|	||< q"| 	|S z	t
|  ddW S  tyo }
 z| jsgddlm} || W  Y d}
~
S t|
d}
~
ww )	a  
    Compute the factorization of expression, ``f``, into irreducibles. (To
    factor an integer into primes, use ``factorint``.)

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

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

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

    Examples
    ========

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

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

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

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

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

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

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

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

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

    If the ``fraction`` flag is False then rational expressions
    won't be combined. By default it is True.

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

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

    r   )	bottom_upc                    s*   t | gR i  }|js|jr|S | S )zS
            Factor, but avoid changing the expression when unable to.
            )r  is_Mulr  )rq   r  rV  rO   rP   _try_factor  s   zfactor.<locals>._try_factorr  r  )	factor_ncN)r   r  r  Zatomsr   r   r  r  r  xreplacer  r-   r(  r  r  )rJ   rt  rD   re   r  r  ZpartialsZmuladdr  r  msgr  rO   rV  rP   r    s,   D
	
r  c              	   C   s.  t | ds"zt| } W n ty   g  Y S w | j||||||dS t| dd\}}	t|	jdkr3tt|D ]
\}
}|j	j	||
< q7|durT|	j
|}|dkrTtd|dur^|	j
|}|durh|	j
|}t||	j
|||||d	}g }|D ]\\}}}|	j
||	j
|}}|||f|f qx|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)]

    rn  r  r   r   rh   Nr   r  )r  r  r  r  r  )r   rB   r,   r  r7  rj   rD   r.   r   rT   r   r   r   r:   r   r   )r   r  r  r  r  r  r  r  r  rf   r   r  r  rL   r=  rk  r   rO   rO   rP   r    s8   

r  c                 C   sX   zt | }t| t s|jjstdW n ty    td|  w |j||||||dS )z
    Refine an isolating interval of a root to the given precision.

    Examples
    ========

    >>> from sympy import refine_root
    >>> from sympy.abc import x

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

    generator must be a Symbolz+can't refine a root of %s, not a polynomial)r  r  r  r  )rB   rA   r   	is_Symbolr-   r,   r  )rJ   r=  rk  r  r  r  r  r   rO   rO   rP   r  1  s   r  c                 C   sT   zt | dd}t| t s|jjstdW n ty"   td|  w |j||dS )a  
    Return the number of roots of ``f`` in ``[inf, sup]`` interval.

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

    Examples
    ========

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

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

    FZgreedyr  z)can't count roots of %s, not a polynomialr  )rB   rA   r   r  r-   r,   r  )rJ   r  r  r   rO   rO   rP   r  N  s   r  Tc                 C   sR   zt | dd}t| t s|jjstdW n ty"   td|  w |j|dS )z
    Return a list of real roots with multiplicities of ``f``.

    Examples
    ========

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

    >>> real_roots(2*x**3 - 7*x**2 + 4*x + 4)
    [-1/2, 2, 2]
    Fr  r  z0can't compute real roots of %s, not a polynomialr  )rB   rA   r   r  r-   r,   r  )rJ   r  r   rO   rO   rP   r  o  s   r  r  r  c                 C   sV   zt | dd}t| t s|jjstdW n ty"   td|  w |j|||dS )aL  
    Compute numerical approximations of roots of ``f``.

    Examples
    ========

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

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

    Fr  r  z5can't compute numerical roots of %s, not a polynomial)r   r  r  )rB   rA   r   r  r-   r,   r  )rJ   r   r  r  r   rO   rO   rP   r    s   r  c              
   O   sz   t |g  z$t| g|R i |\}}t| ts!|jjs&tdW |
 S W |
 S  ty< } zt	dd|d}~ww )z
    Compute roots of ``f`` by factorization in the ground domain.

    Examples
    ========

    >>> from sympy import ground_roots
    >>> from sympy.abc import x

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

    r  r  rh   N)rW   rC  r0  rA   rB   r   r  r-   r0   r1   r  rD  rO   rO   rP   r    s   r  c              
   O   s   t |g  zt| g|R i |\}}t| ts!|jjs!tdW n ty4 } zt	dd|d}~ww |
|}|jsA| S |S )a  
    Construct a polynomial with n-th powers of roots of ``f``.

    Examples
    ========

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

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

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

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

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

    r  r  rh   N)rW   rC  r0  rA   rB   r   r  r-   r0   r1   r  r  rG   )rJ   r   rD   re   r   rf   r)  rL   rO   rO   rP   r    s   
r  c                    sB  ddl m} ddlm  ddlm} t|dg t| } i }d|v r)|d |d< t	| t
tfsL| js=t	| ts=t	| ts?| S || dd} |  \}}n?t| dkr|| \}}t	|trrt	|trr|j|d	< |j|d
< |dd|d< | | }}nt	| tr|| S td|  z1|  rt |||fg|R i |\}\}	}
|jst	| t
tfs|  W S tj||fW S W n tyG } z~| jr|  st|| js| jrt | j! fdddd\}}dd |D }| j"t#| j"| g|R  W  Y d}~S g }t$| }t%| |D ](}t	|t
tt&frqz|'|t#|f |(  W q t)y5   Y qw | *t+|W  Y d}~S d}~ww d|	#|
}\}}|ddrcd	|vrc|j,|d	< t	| t
tfsu|| |   S | | }}|dds|||fS |t|g|R i |t|g|R i |fS )a[  
    Cancel common factors in a rational function ``f``.

    Examples
    ========

    >>> from sympy import cancel, sqrt, Symbol, together
    >>> from sympy.abc import x
    >>> A = Symbol('A', commutative=False)

    >>> cancel((2*x**2 - 2)/(x**2 - 2*x + 1))
    (2*x + 2)/(x - 1)
    >>> cancel((sqrt(3) + sqrt(15)*A)/(sqrt(2) + sqrt(10)*A))
    sqrt(6)/2

    Note: due to automatic distribution of Rationals, a sum divided by an integer
    will appear as a sum. To recover a rational form use `together` on the result:

    >>> cancel(x/2 + 1)
    x/2 + 1
    >>> together(_)
    (x + 2)/2
    r   )factor_termsr8  )sringr  T)Zradicalr:  rD   r   zunexpected argument: %sc                    s   | j du o
|   S r!  )r(  has)r   r8  rO   rP   r  7  s   zcancel.<locals>.<lambda>r  c                 S   r  rO   )r  r  rO   rO   rP   r   :  r  zcancel.<locals>.<listcomp>Nrh   F)-r  r  r;  r9  sympy.polys.ringsr  rW   rC  r   rA   r   r   r@  r   r   rj  rj   rB   rD   r   r`  rG   r   r  r-   Zngensr2  r   rW  r(  r  r  r<   re   rN   r  r   rB  r   r   skiprY   r  r\   r   )rJ   rD   re   r  r  rf   r  r#  r/  r   r   r  r   Zncr>  Zpoter  r.  rO   r8  rP   r    s   



"

(

.r  c              
      s  t |ddg zt| gt| g|R i |\} W n ty. } ztdd|d}~ww  j}d} jrI|jrI|j	sI 
t| d d}dd	lm} | j j j\}	}
t|D ]\}}| jj }|	|||< q^|d |d
d \}} fdd|D }tt| }|rzdd |D | }}W n	 ty   Y nw ||}} jsdd |D | fS ||fS )a<  
    Reduces a polynomial ``f`` modulo a set of polynomials ``G``.

    Given a polynomial ``f`` and a set of polynomials ``G = (g_1, ..., g_n)``,
    computes a set of quotients ``q = (q_1, ..., q_n)`` and the remainder ``r``
    such that ``f = q_1*g_1 + ... + q_n*g_n + r``, where ``r`` vanishes or ``r``
    is a completely reduced polynomial with respect to ``G``.

    Examples
    ========

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

    >>> reduced(2*x**4 + y**2 - x**2 + y**3, [x**3 - x, y**3 - y])
    ([2*x, 1], x**2 + y**2 + y)

    r  r-  reducedr   NFr   Txringrh   c                       g | ]
}t t| qS rO   rB   r]   r\   r   r#  r~   rO   rP   r     r   zreduced.<locals>.<listcomp>c                 S   rM  rO   r   r  rO   rO   rP   r     r  c                 S   rM  rO   r  r  rO   rO   rP   r     r  )rW   rC  r7  r_   r0   r1   r   r-  r+  r,  r  r\   	get_fieldr  r  rD   rU   r   r   rT   r   rv   r*  rB   r]   r   r*   r  rG   )rJ   r   rD   re   r  r)  r   r   r  _ringr   r   r  r.  r$  _Q_rrO   r~   rP   r  Y  s<   (
r  c                 O   s   t | g|R i |S )a  
    Computes the reduced Groebner basis for a set of polynomials.

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

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

    Examples
    ========

    Example taken from [1].

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

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

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

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

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

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

    References
    ==========

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

    )GroebnerBasisr   rD   re   rO   rO   rP   r"     s   3r"   c                 O   s   t | g|R i |jS )a[  
    Checks if the ideal generated by a Groebner basis is zero-dimensional.

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

    References
    ==========

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

    )r  is_zero_dimensionalr  rO   rO   rP   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 zt|g|R i |\} W n ty+ } ztdt||d}~ww ddlm} | j j	 j
fdd|D }t| jd	} fd
d|D }| | S )z>Compute a reduced Groebner basis for a system of polynomials. r  r  r"   Nr   )PolyRingc                    s    g | ]}|r  |j qS rO   )rv   rT   r   r   r  )ringrO   rP   r     s     z)GroebnerBasis.__new__.<locals>.<listcomp>r  c                    s   g | ]}t | qS rO   )rB   r]   r   rK   r~   rO   rP   r     r  )rW   rC  r7  r0   r1   rj   r  r  rD   r   rU   	_groebnerr  _new)rd   r   rD   re   r  r)  r  r   rO   )rf   r  rP   rg     s   zGroebnerBasis.__new__c                 C   s   t | }t||_||_|S rm   )r   rg   r   _basis_options)rd   basisrW   rk   rO   rO   rP   r    s   

zGroebnerBasis._newc                 C   s$   dd | j D }t| t| jj fS )Nc                 s   s    | ]}|  V  qd S rm   r  )r   r  rO   rO   rP   r]    s    z%GroebnerBasis.args.<locals>.<genexpr>)r  r   r  rD   )rp   r  rO   rO   rP   re     s   zGroebnerBasis.argsc                 C   s   dd | j D S )Nc                 S   rM  rO   r  r  rO   rO   rP   r     r  z'GroebnerBasis.exprs.<locals>.<listcomp>)r  ro   rO   rO   rP   r6    r  zGroebnerBasis.exprsc                 C   
   t | jS rm   )r_   r  ro   rO   rO   rP   r    r  zGroebnerBasis.polysc                 C   r   rm   )r  rD   ro   rO   rO   rP   rD   
  r   zGroebnerBasis.gensc                 C   r   rm   )r  r   ro   rO   rO   rP   r     r   zGroebnerBasis.domainc                 C   r   rm   )r  rU   ro   rO   rO   rP   rU     r   zGroebnerBasis.orderc                 C   r  rm   )rj   r  ro   rO   rO   rP   __len__  r   zGroebnerBasis.__len__c                 C   s   | j jr	t| jS t| jS rm   )r  r  iterr6  ro   rO   rO   rP   rn    s   

zGroebnerBasis.__iter__c                 C   s$   | j jr| j}|| S | j}|| S rm   )r  r  r6  )rp   itemr  rO   rO   rP   __getitem__  s
   zGroebnerBasis.__getitem__c                 C   s   t | jt| j fS rm   )hashr  r   r  r   ro   rO   rO   rP   r   '  s   zGroebnerBasis.__hash__c                 C   sL   t || jr| j|jko| j|jkS t|r$| jt|kp#| jt|kS dS )NF)rA   r   r  r  r   r  r_   r6  rp   r  rO   rO   rP   r  *  s
   zGroebnerBasis.__eq__c                 C   r  rm   rO   r  rO   rO   rP   r  2  r   zGroebnerBasis.__ne__c                 C   sT   dd }t dgt| j }| jj}| jD ]}|j|d}||r%||9 }qt|S )a{  
        Checks if the ideal generated by a Groebner basis is zero-dimensional.

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

        References
        ==========

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

        c                 S   s   t tt| dkS r   )sumr   r=  )monomialrO   rO   rP   
single_varD  s   z5GroebnerBasis.is_zero_dimensional.<locals>.single_varr   r   )r#   rj   rD   r  rU   r  rM  r  )rp   r  rE  rU   r  r  rO   rO   rP   r  5  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|\}}t|D ]\}	}
|
 jj }
||
||	< q>t|||} fdd|D }|jsodd |D }| _| | S )a  
        Convert a Groebner basis from one ordering to another.

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

        Examples
        ========

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

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

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

        References
        ==========

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

        z>can't convert Groebner bases of ideals with positive dimension)r   rU   r   r  c                    r  rO   r  r  r~   rO   rP   r     r   z&GroebnerBasis.fglm.<locals>.<listcomp>c                 S   s   g | ]
}|j d dd qS )TrZ  rh   )rV  r  rO   rO   rP   r     r   )r  rU   r$   r  rY   r_   r  r   r  r\   r  r  r  rD   r   r   rT   r   rv   r!   r,  r  )rp   rU   Z	src_orderZ	dst_orderr  r   r  r  r   r   r  r   rO   r~   rP   fglmU  s0   
zGroebnerBasis.fglmTc                    sH  t || j}|gt| j }| j  j}d}|r+|jr+|js+ t	|
 d d}ddlm} | j j j\}}	t|D ]\}
}| jj }||||
< q@|d |dd \}} fdd	|D }t t	| }|rzd
d	 |D | }}W n	 ty   Y nw ||}} jsdd	 |D | fS ||fS )a#  
        Reduces a polynomial modulo a Groebner basis.

        Given a polynomial ``f`` and a set of polynomials ``G = (g_1, ..., g_n)``,
        computes a set of quotients ``q = (q_1, ..., q_n)`` and the remainder ``r``
        such that ``f = q_1*f_1 + ... + q_n*f_n + r``, where ``r`` vanishes or ``r``
        is a completely reduced polynomial with respect to ``G``.

        Examples
        ========

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

        >>> f = 2*x**4 - x**2 + y**3 + y**2
        >>> G = groebner([x**3 - x, y**3 - y])

        >>> G.reduce(f)
        ([2*x, 1], x**2 + y**2 + y)
        >>> Q, r = _

        >>> expand(sum(q*g for q, g in zip(Q, G)) + r)
        2*x**4 - x**2 + y**3 + y**2
        >>> _ == f
        True

        Fr   Tr   r  rh   Nc                    r  rO   r  r  r~   rO   rP   r     r   z(GroebnerBasis.reduce.<locals>.<listcomp>c                 S   rM  rO   r  r  rO   rO   rP   r     r  c                 S   rM  rO   r  r  rO   rO   rP   r     r  )rB   rb   r  r_   r  r   r+  r,  r  r\   r  r  r  rD   rU   r   r   rT   r   rv   r*  r]   r   r*   r  rG   )rp   rq   r-  r  r  r   r   r  r  r   r   r.  r$  r  r  rO   r~   rP   r     s4   
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

        rh   r   )r   )rp   r  rO   rO   rP   contains  s   zGroebnerBasis.containsNr  )rH   r$  r%  r&  rg   r)  r  r*  re   r6  r  rD   r   rU   r  rn  r  r   r  r  r  r  r   r  rO   rO   rO   rP   r    s8    








BAr  c                    sd   t g   fdd t| } | jrt| g|R i S dvr'dd< t |} | |S )z
    Efficiently transform an expression into a polynomial.

    Examples
    ========

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

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

    c           
         s~  g g }}t | D ]u}g g }}t|D ]1}|jr$| || q|jrB|jjrB|jjrB|jdkrB| |j|	|j q|| q|sP|| q
|d }|dd  D ]}|
|}qZ|rzt| }|jrq|
|}n	|
t||}|| q
|st| |}	n*|d }	|dd  D ]}|	|}	q|rt | }|jr|	|}	n	|	t||}	|	j|ddi S )Nr   rh   rD   rO   )r   r  r   r  r   r  r  r  r  r   r   r@  rB   rb   r   r   r`  )
rq   rf   r   Z
poly_termsrz  r  Zpoly_factorsr  productrL   _polyre   rO   rP   r    sJ   

zpoly.<locals>._polyr2  F)rW   rC  r   r`   rB   rX   r/  rO   r  rP   r    s   4
r  r   rm   )FNNNFFFr"  r  r  r#  )r&  	functoolsr   r   operatorr   Z
sympy.corer   r   r   r	   r
   r   r   r   r   Zsympy.core.basicr   Zsympy.core.compatibilityr   r   Zsympy.core.decoratorsr   Zsympy.core.evalfr   Zsympy.core.functionr   Zsympy.core.mulr   rv  r   Zsympy.core.symbolr   Zsympy.core.sympifyr   r   Zsympy.logic.boolalgr   Zsympy.polysr   rW   Zsympy.polys.constructorr   Zsympy.polys.domainsr   r   r   Z!sympy.polys.domains.domainelementr    Zsympy.polys.fglmtoolsr!   Zsympy.polys.groebnertoolsr"   r  Zsympy.polys.monomialsr#   Zsympy.polys.orderingsr$   Zsympy.polys.polyclassesr%   r&   r'   Zsympy.polys.polyerrorsr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   Zsympy.polys.polyutilsr3   r4   r5   r6   r7   r8   Zsympy.polys.rationaltoolsr9   Zsympy.polys.rootisolationr:   Zsympy.utilitiesr;   r<   r=   r>   Zsympy.utilities.exceptionsr?   r  r  Zmpmath.libmp.libhyperr@   rR   rB   r,  r0  r.  r7  r5  r?  r5  r8  r7  r>  rM  rR  r"  r&  r'  r(  r*  r1  r3  r4  rh  rj  rl  rn  ro  rr  rx  ri  r{  rq  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  rO   rO   rO   rP   <module>   s   ,4 
                                )]
(
_
:
4






"
"



"
&
&
5
$

(
T3
M2
v



-



.

!
:,	 ,


d7 

+
d
;
5
  