a
    RG5d^                    @   s  d Z ddlmZmZ ddlmZmZmZm	Z	m
Z
mZ 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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$ 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/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z6m7Z8m9Z9 ddl:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE eAe.fddZFeAe.fddZGeAe.fddZHeAd d! ZId"d# ZJi ZKG d$d% d%e?eZLG d&d' d'e&e?eeMZNd(S ))zSparse polynomial rings.     )AnyDict)addmulltlegtge)reduce)GeneratorType)Expr)igcdoo)Symbolsymbols)CantSympifysympify)multinomial_coefficients)IPolys)construct_domain)dmp_to_dictdmp_from_dict)DomainElementPolynomialRingheugcd)MonomialOps)lex)CoercionFailedGeneratorsErrorExactQuotientFailedMultivariatePolynomialError)DomainOrderbuild_options)expr_from_dict_dict_reorder_parallel_dict_from_expr)DefaultPrinting)public)is_sequence)pollutec                 C   s   t | ||}|f|j S )a  Construct a polynomial ring returning ``(ring, x_1, ..., x_n)``.

    Parameters
    ==========

    symbols : str
        Symbol/Expr or sequence of str, Symbol/Expr (non-empty)
    domain : :class:`~.Domain` or coercible
    order : :class:`~.MonomialOrder` or coercible, optional, defaults to ``lex``

    Examples
    ========

    >>> from sympy.polys.rings import ring
    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.orderings import lex

    >>> R, x, y, z = ring("x,y,z", ZZ, lex)
    >>> R
    Polynomial ring in x, y, z over ZZ with lex order
    >>> x + y + z
    x + y + z
    >>> type(_)
    <class 'sympy.polys.rings.PolyElement'>

    PolyRinggensr   domainorder_ring r4   M/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/polys/rings.pyring#   s    r6   c                 C   s   t | ||}||jfS )a  Construct a polynomial ring returning ``(ring, (x_1, ..., x_n))``.

    Parameters
    ==========

    symbols : str
        Symbol/Expr or sequence of str, Symbol/Expr (non-empty)
    domain : :class:`~.Domain` or coercible
    order : :class:`~.MonomialOrder` or coercible, optional, defaults to ``lex``

    Examples
    ========

    >>> from sympy.polys.rings import xring
    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.orderings import lex

    >>> R, (x, y, z) = xring("x,y,z", ZZ, lex)
    >>> R
    Polynomial ring in x, y, z over ZZ with lex order
    >>> x + y + z
    x + y + z
    >>> type(_)
    <class 'sympy.polys.rings.PolyElement'>

    r-   r0   r4   r4   r5   xringB   s    r7   c                 C   s(   t | ||}tdd |jD |j |S )a  Construct a polynomial ring and inject ``x_1, ..., x_n`` into the global namespace.

    Parameters
    ==========

    symbols : str
        Symbol/Expr or sequence of str, Symbol/Expr (non-empty)
    domain : :class:`~.Domain` or coercible
    order : :class:`~.MonomialOrder` or coercible, optional, defaults to ``lex``

    Examples
    ========

    >>> from sympy.polys.rings import vring
    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.orderings import lex

    >>> vring("x,y,z", ZZ, lex)
    Polynomial ring in x, y, z over ZZ with lex order
    >>> x + y + z # noqa:
    x + y + z
    >>> type(_)
    <class 'sympy.polys.rings.PolyElement'>

    c                 S   s   g | ]
}|j qS r4   )name).0symr4   r4   r5   
<listcomp>}       zvring.<locals>.<listcomp>)r.   r,   r   r/   r0   r4   r4   r5   vringa   s    r=   c           
         s   d}t | s| gd } }ttt| } t||}t| |\}}|jdu rtdd |D g }t||d\|_}t	t
||  fdd|D }t|j|j|j}tt|j|}	|r||	d fS ||	fS dS )	ad  Construct a ring deriving generators and domain from options and input expressions.

    Parameters
    ==========

    exprs : :class:`~.Expr` or sequence of :class:`~.Expr` (sympifiable)
    symbols : sequence of :class:`~.Symbol`/:class:`~.Expr`
    options : keyword arguments understood by :class:`~.Options`

    Examples
    ========

    >>> from sympy import sring, symbols

    >>> x, y, z = symbols("x,y,z")
    >>> R, f = sring(x + 2*y + 3*z)
    >>> R
    Polynomial ring in x, y, z over ZZ with lex order
    >>> f
    x + 2*y + 3*z
    >>> type(_)
    <class 'sympy.polys.rings.PolyElement'>

    FTNc                 S   s   g | ]}t | qS r4   listvaluesr9   repr4   r4   r5   r;      r<   zsring.<locals>.<listcomp>)optc                    s"   g | ]} fd d|  D qS )c                    s   i | ]\}}| | qS r4   r4   )r9   mcZ	coeff_mapr4   r5   
<dictcomp>   r<   z$sring.<locals>.<listcomp>.<dictcomp>)itemsrA   rF   r4   r5   r;      r<   r   )r+   r?   mapr   r%   r(   r1   sumr   dictzipr.   r/   r2   	from_dict)
exprsr   optionssinglerC   repscoeffs
coeffs_domr3   polysr4   rF   r5   sring   s     

rU   c                 C   sr   t | tr| rt| ddS dS t | tr.| fS t| rftdd | D rPt| S tdd | D rf| S tdd S )NT)seqr4   c                 s   s   | ]}t |tV  qd S N)
isinstancestrr9   sr4   r4   r5   	<genexpr>   r<   z!_parse_symbols.<locals>.<genexpr>c                 s   s   | ]}t |tV  qd S rW   )rX   r   rZ   r4   r4   r5   r\      r<   zbexpected a string, Symbol or expression or a non-empty sequence of strings, Symbols or expressions)rX   rY   _symbolsr   r+   allr    r   r4   r4   r5   _parse_symbols   s    

r`   c                   @   s8  e Zd ZdZefddZdd Zdd Zdd	 Zd
d Z	dd Z
dd ZdEddZdd Zedd Zedd ZdFddZdd Zdd Zdd  ZeZdGd!d"ZdHd#d$Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Z ed7d8 Z!ed9d: Z"d;d< Z#d=d> Z$d?d@ Z%dAdB Z&dCdD Z'dS )Ir.   z*Multivariate distributed polynomial ring. c                    s  t t|}t|}t|}t  | j||| f}t|}|d u r|j	rlt
|t
|j@ rltdt| }||_t||_tdtfd|i|_||_||_||_ |_d| |_| |_t
|j|_|j|jfg|_|r8t|}| |_ |! |_"|# |_$|% |_&|' |_(|) |_*|+ |_,n6dd }||_ ||_"dd |_$||_&||_(||_*||_, t-u rt.|_/n fdd|_/t0|j|jD ]4\}	}
t1|	t2r|	j3}t4||st5|||
 q|t|< |S )	Nz7polynomial ring and it's ground domain share generatorsPolyElementr6   r   c                 S   s   dS Nr4   r4   )abr4   r4   r5   <lambda>   r<   z"PolyRing.__new__.<locals>.<lambda>c                 S   s   dS rc   r4   )rd   re   rE   r4   r4   r5   rf      r<   c                    s   t |  dS )Nkey)maxfr2   r4   r5   rf      r<   )6tupler`   len	DomainOpt
preprocessOrderOpt__name___ring_cachegetis_Compositesetr   r    object__new___hash_tuplehash_hashtypera   dtypengensr1   r2   
zero_monom_gensr/   	_gens_setone_oner   r   monomial_mulpowmonomial_powmulpowmonomial_mulpowldivmonomial_ldivdivmonomial_divlcmmonomial_lcmgcdmonomial_gcdr   ri   leading_expvrL   rX   r   r8   hasattrsetattr)clsr   r1   r2   r~   ry   objZcodegenZmonunitsymbol	generatorr8   r4   rl   r5   rx      s`    















zPolyRing.__new__c                 C   sF   | j j}g }t| jD ]&}| |}| j}|||< || qt|S )z(Return a list of polynomial generators. )r1   r   ranger~   monomial_basiszeroappendrm   )selfr   r   iexpvpolyr4   r4   r5   r     s    
zPolyRing._gensc                 C   s   | j | j| jfS rW   )r   r1   r2   r   r4   r4   r5   __getnewargs__  s    zPolyRing.__getnewargs__c                 C   s6   | j  }|d= | D ]\}}|dr||= q|S )Nr   Z	monomial_)__dict__copyrH   
startswith)r   staterh   valuer4   r4   r5   __getstate__  s    

zPolyRing.__getstate__c                 C   s   | j S rW   )r{   r   r4   r4   r5   __hash__   s    zPolyRing.__hash__c                 C   s2   t |to0| j| j| j| jf|j|j|j|jfkS rW   )rX   r.   r   r1   r~   r2   r   otherr4   r4   r5   __eq__#  s
    
zPolyRing.__eq__c                 C   s
   | |k S rW   r4   r   r4   r4   r5   __ne__(  s    zPolyRing.__ne__Nc                 C   s    |  |p| j|p| j|p| jS rW   )	__class__r   r1   r2   )r   r   r1   r2   r4   r4   r5   clone+  s    zPolyRing.clonec                 C   s   dg| j  }d||< t|S )zReturn the ith-basis element. r      )r~   rm   )r   r   basisr4   r4   r5   r   .  s    zPolyRing.monomial_basisc                 C   s   |   S rW   )r}   r   r4   r4   r5   r   4  s    zPolyRing.zeroc                 C   s   |  | jS rW   )r}   r   r   r4   r4   r5   r   8  s    zPolyRing.onec                 C   s   | j ||S rW   )r1   convertr   elementorig_domainr4   r4   r5   
domain_new<  s    zPolyRing.domain_newc                 C   s   |  | j|S rW   )term_newr   )r   coeffr4   r4   r5   
ground_new?  s    zPolyRing.ground_newc                 C   s    |  |}| j}|r|||< |S rW   )r   r   )r   monomr   r   r4   r4   r5   r   B  s
    
zPolyRing.term_newc                 C   s   t |trF| |jkr|S t | jtr<| jj|jkr<| |S tdn|t |trZtdnht |trn| 	|S t |t
rz| |W S  ty   | | Y S 0 nt |tr| |S | |S d S )N
conversionparsing)rX   ra   r6   r1   r   r   NotImplementedErrorrY   rK   rM   r?   
from_terms
ValueError	from_listr   	from_exprr   r   r4   r4   r5   ring_newI  s$    










zPolyRing.ring_newc                 C   s8   | j }| j}| D ]\}}|||}|r|||< q|S rW   )r   r   rH   )r   r   r   r   r   r   r   r4   r4   r5   rM   a  s    

zPolyRing.from_dictc                 C   s   |  t||S rW   )rM   rK   r   r4   r4   r5   r   l  s    zPolyRing.from_termsc                 C   s   |  t|| jd | jS Nr   )rM   r   r~   r1   r   r4   r4   r5   r   o  s    zPolyRing.from_listc                    s$   j  fdd  t|S )Nc                    s    | }|d ur|S | jr2tttt | jS | jrNtttt | jS | 	 \}}|j
rx|dkrx |t| S | S d S r   )rt   is_Addr
   r   r?   rI   argsis_Mulr   as_base_exp
is_Integerintr   r   )exprr   baseexp_rebuildr1   mappingr   r4   r5   r   u  s    
z(PolyRing._rebuild_expr.<locals>._rebuild)r1   r   )r   r   r   r4   r   r5   _rebuild_exprr  s    zPolyRing._rebuild_exprc                 C   sX   t tt| j| j}z| ||}W n" tyH   td| |f Y n0 | |S d S )Nz@expected an expression convertible to a polynomial in %s, got %s)	rK   r?   rL   r   r/   r   r   r   r   )r   r   r   r   r4   r4   r5   r     s    zPolyRing.from_exprc                 C   s   |du r| j rd}qd}nt|trj|}d|kr<|| j k r<q| j  |kr\|dkr\| d }qtd| nt|| jrz| j|}W q ty   td| Y q0 nHt|trz| j|}W q ty   td| Y q0 ntd| |S )z+Compute index of ``gen`` in ``self.gens``. Nr   r   zinvalid generator index: %szinvalid generator: %szEexpected a polynomial generator, an integer, a string or None, got %s)	r~   rX   r   r   r}   r/   indexrY   r   )r   genr   r4   r4   r5   r     s.    

zPolyRing.indexc                    sB   t t| j|  fddt| jD }|s2| jS | j|dS dS )z,Remove specified generators from this ring. c                    s   g | ]\}}| vr|qS r4   r4   r9   r   r[   indicesr4   r5   r;     r<   z!PolyRing.drop.<locals>.<listcomp>r_   N)rv   rI   r   	enumerater   r1   r   r   r/   r   r4   r   r5   drop  s
    zPolyRing.dropc                 C   s$   | j | }|s| jS | j|dS d S )Nr_   )r   r1   r   )r   rh   r   r4   r4   r5   __getitem__  s    
zPolyRing.__getitem__c                 C   s6   | j jst| j dr$| j| j j dS td| j  d S )Nr1   r1   z%s is not a composite domain)r1   ru   r   r   r   r   r4   r4   r5   	to_ground  s    zPolyRing.to_groundc                 C   s   t | S rW   r   r   r4   r4   r5   	to_domain  s    zPolyRing.to_domainc                 C   s   ddl m} || j| j| jS )Nr   )	FracField)sympy.polys.fieldsr   r   r1   r2   )r   r   r4   r4   r5   to_field  s    zPolyRing.to_fieldc                 C   s   t | jdkS r   rn   r/   r   r4   r4   r5   is_univariate  s    zPolyRing.is_univariatec                 C   s   t | jdkS r   r   r   r4   r4   r5   is_multivariate  s    zPolyRing.is_multivariatec                 G   s8   | j }|D ](}t|tdr*|| j| 7 }q
||7 }q
|S )aw  
        Add a sequence of polynomials or containers of polynomials.

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> R, x = ring("x", ZZ)
        >>> R.add([ x**2 + 2*i + 3 for i in range(4) ])
        4*x**2 + 24
        >>> _.factor_list()
        (4, [(x**2 + 6, 1)])

        include)r   r+   r   r   r   objspr   r4   r4   r5   r     s    
zPolyRing.addc                 G   s8   | j }|D ](}t|tdr*|| j| 9 }q
||9 }q
|S )a  
        Multiply a sequence of polynomials or containers of polynomials.

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> R, x = ring("x", ZZ)
        >>> R.mul([ x**2 + 2*i + 3 for i in range(4) ])
        x**8 + 24*x**6 + 206*x**4 + 744*x**2 + 945
        >>> _.factor_list()
        (1, [(x**2 + 3, 1), (x**2 + 5, 1), (x**2 + 7, 1), (x**2 + 9, 1)])

        r   )r   r+   r   r   r   r4   r4   r5   r     s    
zPolyRing.mulc                    s`   t t| j|  fddt| jD } fddt| jD }|sH| S | j|| j| dS dS )zd
        Remove specified generators from the ring and inject them into
        its domain.
        c                    s   g | ]\}}| vr|qS r4   r4   r   r   r4   r5   r;     r<   z+PolyRing.drop_to_ground.<locals>.<listcomp>c                    s   g | ]\}}| vr|qS r4   r4   )r9   r   r   r   r4   r5   r;     r<   r   r1   N)rv   rI   r   r   r   r/   r   r   r   r4   r   r5   drop_to_ground  s    zPolyRing.drop_to_groundc                 C   s6   | |kr.t | jt |j}| jt|dS | S dS )z+Add the generators of ``other`` to ``self``r_   Nrv   r   unionr   r?   )r   r   symsr4   r4   r5   compose  s    zPolyRing.composec                 C   s$   t | jt |}| jt|dS )z9Add the elements of ``symbols`` as generators to ``self``r_   r   )r   r   r   r4   r4   r5   add_gens&  s    zPolyRing.add_gens)NNN)N)N)N)(rr   
__module____qualname____doc__r   rx   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   __call__rM   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r4   r4   r4   r5   r.      sN   A










r.   c                   @   s"  e Zd ZdZdd Zdd Zdd ZdZd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdddZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zed7d8 Z ed9d: Z!ed;d< Z"ed=d> Z#ed?d@ Z$edAdB Z%edCdD Z&edEdF Z'edGdH Z(edIdJ Z)edKdL Z*edMdN Z+edOdP Z,edQdR Z-edSdT Z.edUdV Z/edWdX Z0dYdZ Z1d[d\ Z2d]d^ Z3d_d` Z4dadb Z5dcdd Z6dedf Z7dgdh Z8didj Z9dkdl Z:dmdn Z;dodp Z<dqdr Z=dsdt Z>dudv Z?dwdx Z@dydz ZAd{d| ZBeAZCeBZDd}d~ ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdddZLdd ZMdddZNdd ZOdd ZPdd ZQdd ZRdd ZSedd ZTedd ZUdd ZVedd ZWdd ZXdd ZYdddZZdddZ[dddZ\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd Zddd Zedd Zfdd ZgddĄ ZhddƄ ZiddȄ Zjddʄ Zkdd̄ ZlelZmdd΄ ZnddЄ Zodd҄ ZpddԄ Zqddք Zrdd؄ Zsddڄ Ztdd܄ Zuddބ Zvdd Zwdd Zxdd Zydd Zzdd Z{dd Z|dd Z}dd Z~dd Zd ddZd!ddZd"ddZdd Zdd Zdd Zdd Zdd  Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zd#ddZdd ZdS ($  ra   z5Element of multivariate distributed polynomial ring. c                 C   s
   |  |S rW   )r   )r   initr4   r4   r5   new/  s    zPolyElement.newc                 C   s
   | j  S rW   )r6   r   r   r4   r4   r5   parent2  s    zPolyElement.parentc                 C   s   | j t|  fS rW   )r6   r?   	itertermsr   r4   r4   r5   r   5  s    zPolyElement.__getnewargs__Nc                 C   s.   | j }|d u r*t| jt|  f | _ }|S rW   )r{   rz   r6   	frozensetrH   )r   r{   r4   r4   r5   r   :  s    zPolyElement.__hash__c                 C   s
   |  | S )a  Return a copy of polynomial self.

        Polynomials are mutable; if one is interested in preserving
        a polynomial, and one plans to use inplace operations, one
        can copy the polynomial. This method makes a shallow copy.

        Examples
        ========

        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.rings import ring

        >>> R, x, y = ring('x, y', ZZ)
        >>> p = (x + y)**2
        >>> p1 = p.copy()
        >>> p2 = p
        >>> p[R.zero_monom] = 3
        >>> p
        x**2 + 2*x*y + y**2 + 3
        >>> p1
        x**2 + 2*x*y + y**2
        >>> p2
        x**2 + 2*x*y + y**2 + 3

        )r   r   r4   r4   r5   r   E  s    zPolyElement.copyc                 C   sZ   | j |kr| S | j j|jkrFttt| | j j|j }||| j jS || | j jS d S rW   )r6   r   r?   rL   r'   r   r1   rM   )r   new_ringtermsr4   r4   r5   set_ringa  s    
zPolyElement.set_ringc                 G   sJ   |r.t || jjkr.td| jjt |f n| jj}t|  g|R  S )Nz&not enough symbols, expected %s got %s)rn   r6   r~   r   r   r&   as_expr_dict)r   r   r4   r4   r5   as_exprj  s    zPolyElement.as_exprc                    s    | j jj  fdd|  D S )Nc                    s   i | ]\}}| |qS r4   r4   r9   r   r   to_sympyr4   r5   rG   t  r<   z,PolyElement.as_expr_dict.<locals>.<dictcomp>)r6   r1   r  r   r   r4   r   r5   r   r  s    
zPolyElement.as_expr_dictc                    sx   | j j}|jr|js|j| fS | }|j |j}|j}|  D ]}| || q@| 	 fdd| 
 D } |fS )Nc                    s   g | ]\}}||  fqS r4   r4   )r9   kvcommonr4   r5   r;     r<   z,PolyElement.clear_denoms.<locals>.<listcomp>)r6   r1   is_Fieldhas_assoc_Ringr   get_ringr   denomr@   r   rH   )r   r1   Zground_ringr   r	  r   r   r4   r  r5   clear_denomsv  s    
zPolyElement.clear_denomsc                 C   s$   t |  D ]\}}|s| |= qdS )z+Eliminate monomials with zero coefficient. Nr?   rH   )r   r  r  r4   r4   r5   
strip_zero  s    zPolyElement.strip_zeroc                 C   sR   |s
|  S t |tr,|j| jkr,t| |S t| dkr<dS | | jj|kS dS )aP  Equality test for polynomials.

        Examples
        ========

        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.rings import ring

        >>> _, x, y = ring('x, y', ZZ)
        >>> p1 = (x + y)**2 + (x - y)**2
        >>> p1 == 4*x*y
        False
        >>> p1 == 2*(x**2 + y**2)
        True

        r   FN)rX   ra   r6   rK   r   rn   rt   r   p1p2r4   r4   r5   r     s    zPolyElement.__eq__c                 C   s
   | |k S rW   r4   r  r4   r4   r5   r     s    zPolyElement.__ne__c                 C   s   | j }t||jrbt|  t| kr.dS |jj}|  D ]}|| | || |s> dS q>dS t| dkrrdS z|j|}W n t	y   Y dS 0 |j| 
 ||S dS )z+Approximate equality test for polynomials. FTr   N)r6   rX   r}   rv   keysr1   almosteqrn   r   r   const)r  r  	tolerancer6   r  r  r4   r4   r5   r    s     zPolyElement.almosteqc                 C   s   t | |  fS rW   )rn   r   r   r4   r4   r5   sort_key  s    zPolyElement.sort_keyc                 C   s(   t || jjr ||  | S tS d S rW   )rX   r6   r}   r  NotImplemented)r  r  opr4   r4   r5   _cmp  s    zPolyElement._cmpc                 C   s   |  |tS rW   )r  r   r  r4   r4   r5   __lt__  s    zPolyElement.__lt__c                 C   s   |  |tS rW   )r  r   r  r4   r4   r5   __le__  s    zPolyElement.__le__c                 C   s   |  |tS rW   )r  r   r  r4   r4   r5   __gt__  s    zPolyElement.__gt__c                 C   s   |  |tS rW   )r  r	   r  r4   r4   r5   __ge__  s    zPolyElement.__ge__c                 C   sH   | j }||}|jdkr$||jfS t|j}||= ||j|dfS d S )Nr   r_   )r6   r   r~   r1   r?   r   r   r   r   r6   r   r   r4   r4   r5   _drop  s    



zPolyElement._dropc                 C   s   |  |\}}| jjdkr8| jr*| dS td| nP|j}|  D ]<\}}|| dkrvt|}||= ||t	|< qFtd| qF|S d S )Nr   zCannot drop %sr   )
r  r6   r~   	is_groundr   r   r   rH   r?   rm   )r   r   r   r6   r   r  r  Kr4   r4   r5   r     s    
zPolyElement.dropc                 C   s6   | j }||}t|j}||= ||j||| dfS )Nr   )r6   r   r?   r   r   r  r4   r4   r5   _drop_to_ground  s
    

zPolyElement._drop_to_groundc                 C   s   | j jdkrtd| |\}}|j}|jjd }|  D ]b\}}|d | ||d d   }||vr|||  |||< q<||  |||  |7  < q<|S )Nr   z$Cannot drop only generator to groundr   )	r6   r~   r   r   r   r1   r/   r   
mul_ground)r   r   r   r6   r   r   r   monr4   r4   r5   r     s     zPolyElement.drop_to_groundc                 C   s   t | | jjd | jjS r   )r   r6   r~   r1   r   r4   r4   r5   to_dense  s    zPolyElement.to_densec                 C   s   t | S rW   )rK   r   r4   r4   r5   to_dict  s    zPolyElement.to_dictc                 C   s  | s| | jjjS |d }|d }| j}|j}|j}	|j}
g }|  D ]2\}}|j|}|rfdnd}|	| ||
kr| |}|r|
dr|dd  }n,|r| }|| jjkr|j||dd}nd	}g }t|	D ]}|| }|sq|j|| |dd}|dkrL|t|ks"|d
k r4|j||dd}n|}|	|||f  q|	d|  q|rl|g| }|	|| qH|d
 dv r|d
}|dkr|d
d d	|S )NMulAtom -  + -r   T)strict r   Fz%s)r(  r'  )_printr6   r1   r   r   r~   r   r   is_negativer   r   r   Zparenthesizer   r   joinpopinsert)r   printer
precedenceZexp_patternZ
mul_symbolZprec_mulZ	prec_atomr6   r   r~   zmZsexpvsr   r   negativesignZscoeffZsexpvr   r   r   sexpheadr4   r4   r5   rY     sT    





zPolyElement.strc                 C   s   | | j jv S rW   )r6   r   r   r4   r4   r5   is_generatorC  s    zPolyElement.is_generatorc                 C   s   |  pt | dko| jj| v S r   )rn   r6   r   r   r4   r4   r5   r  G  s    zPolyElement.is_groundc                 C   s   |  pt | dko| jdkS r   )rn   LCr   r4   r4   r5   is_monomialK  s    zPolyElement.is_monomialc                 C   s   t | dkS r   )rn   r   r4   r4   r5   is_termO  s    zPolyElement.is_termc                 C   s   | j j| jS rW   )r6   r1   r-  r9  r   r4   r4   r5   r-  S  s    zPolyElement.is_negativec                 C   s   | j j| jS rW   )r6   r1   is_positiver9  r   r4   r4   r5   r<  W  s    zPolyElement.is_positivec                 C   s   | j j| jS rW   )r6   r1   is_nonnegativer9  r   r4   r4   r5   r=  [  s    zPolyElement.is_nonnegativec                 C   s   | j j| jS rW   )r6   r1   is_nonpositiver9  r   r4   r4   r5   r>  _  s    zPolyElement.is_nonpositivec                 C   s   |  S rW   r4   rj   r4   r4   r5   is_zeroc  s    zPolyElement.is_zeroc                 C   s   | | j jkS rW   )r6   r   rj   r4   r4   r5   is_oneg  s    zPolyElement.is_onec                 C   s   | j j| jS rW   )r6   r1   r@  r9  rj   r4   r4   r5   is_monick  s    zPolyElement.is_monicc                 C   s   | j j|  S rW   )r6   r1   r@  contentrj   r4   r4   r5   is_primitiveo  s    zPolyElement.is_primitivec                 C   s   t dd |  D S )Nc                 s   s   | ]}t |d kV  qdS r   NrJ   r9   r   r4   r4   r5   r\   u  r<   z(PolyElement.is_linear.<locals>.<genexpr>r^   
itermonomsrj   r4   r4   r5   	is_linears  s    zPolyElement.is_linearc                 C   s   t dd |  D S )Nc                 s   s   | ]}t |d kV  qdS )   NrE  rF  r4   r4   r5   r\   y  r<   z+PolyElement.is_quadratic.<locals>.<genexpr>rG  rj   r4   r4   r5   is_quadraticw  s    zPolyElement.is_quadraticc                 C   s   | j jsdS | j | S NT)r6   r~   	dmp_sqf_prj   r4   r4   r5   is_squarefree{  s    zPolyElement.is_squarefreec                 C   s   | j jsdS | j | S rL  )r6   r~   dmp_irreducible_prj   r4   r4   r5   is_irreducible  s    zPolyElement.is_irreduciblec                 C   s    | j jr| j | S tdd S )Nzcyclotomic polynomial)r6   r   dup_cyclotomic_pr"   rj   r4   r4   r5   is_cyclotomic  s    zPolyElement.is_cyclotomicc                 C   s   |  dd |  D S )Nc                 S   s   g | ]\}}|| fqS r4   r4   r   r4   r4   r5   r;     r<   z'PolyElement.__neg__.<locals>.<listcomp>)r   r   r   r4   r4   r5   __neg__  s    zPolyElement.__neg__c                 C   s   | S rW   r4   r   r4   r4   r5   __pos__  s    zPolyElement.__pos__c           
      C   s@  |s|   S | j}t||jrl|   }|j}|jj}| D ]*\}}|||| }|r`|||< q<||= q<|S t|trt|jt	r|jj|jkrn*t|jjt	r|jjj|kr|
| S tS z||}W n ty   t Y S 0 |   }|s|S |j}	|	|  vr|||	< n(|||	  kr(||	= n||	  |7  < |S dS )a  Add two polynomials.

        Examples
        ========

        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.rings import ring

        >>> _, x, y = ring('x, y', ZZ)
        >>> (x + y)**2 + (x - y)**2
        2*x**2 + 2*y**2

        N)r   r6   rX   r}   rt   r1   r   rH   ra   r   __radd__r  r   r   r   r  )
r  r  r6   r   rt   r   r  r  cp2r3  r4   r4   r5   __add__  sB    




zPolyElement.__add__c                 C   s   |   }|s|S | j}z||}W n ty:   t Y S 0 |j}||  vrX|||< n&|||  krn||= n||  |7  < |S d S rW   )r   r6   r   r   r  r   r  )r  nr   r6   r3  r4   r4   r5   rU    s    

zPolyElement.__radd__c           	      C   s8  |s|   S | j}t||jrl|   }|j}|jj}| D ]*\}}|||| }|r`|||< q<||= q<|S t|trt|jt	r|jj|jkrn*t|jjt	r|jjj|kr|
| S tS z||}W n ty   t Y S 0 |   }|j}||  vr
| ||< n&||| kr ||= n||  |8  < |S dS )a.  Subtract polynomial p2 from p1.

        Examples
        ========

        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.rings import ring

        >>> _, x, y = ring('x, y', ZZ)
        >>> p1 = x + y**2
        >>> p2 = x*y + y**2
        >>> p1 - p2
        -x*y + x

        N)r   r6   rX   r}   rt   r1   r   rH   ra   r   __rsub__r  r   r   r   r  )	r  r  r6   r   rt   r   r  r  r3  r4   r4   r5   __sub__  s>    



zPolyElement.__sub__c                 C   sZ   | j }z||}W n ty*   t Y S 0 |j}| D ]}| |  ||< q6||7 }|S dS )a#  n - p1 with n convertible to the coefficient domain.

        Examples
        ========

        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.rings import ring

        >>> _, x, y = ring('x, y', ZZ)
        >>> p = x + y
        >>> 4 - p
        -x - y + 4

        N)r6   r   r   r  r   )r  rX  r6   r   r   r4   r4   r5   rY    s    
zPolyElement.__rsub__c                 C   s:  | j }|j}| r|s|S t||jr|j}|jj}|j}t| }|  D ]6\}}	|D ](\}
}|||
}||||	|  ||< qXqL|	  |S t|t
rt|jtr|jj |j krn*t|j jtr|j jj |kr|| S tS z||}W n ty   t Y S 0 |  D ] \}}	|	| }|r|||< q|S dS )a!  Multiply two polynomials.

        Examples
        ========

        >>> from sympy.polys.domains import QQ
        >>> from sympy.polys.rings import ring

        >>> _, x, y = ring('x, y', QQ)
        >>> p1 = x + y
        >>> p2 = x - y
        >>> p1*p2
        x**2 - y**2

        N)r6   r   rX   r}   rt   r1   r   r?   rH   r  ra   r   __rmul__r  r   r   )r  r  r6   r   rt   r   r   Zp2itexp1v1exp2v2r   r  r4   r4   r5   __mul__/  s<    



zPolyElement.__mul__c                 C   sf   | j j}|s|S z|j |}W n ty6   t Y S 0 |  D ]\}}|| }|r@|||< q@|S dS )a  p2 * p1 with p2 in the coefficient domain of p1.

        Examples
        ========

        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.rings import ring

        >>> _, x, y = ring('x, y', ZZ)
        >>> p = x + y
        >>> 4 * p
        4*x + 4*y

        N)r6   r   r   r   r  rH   )r  r  r   r\  r]  r  r4   r4   r5   r[  a  s    

zPolyElement.__rmul__c                 C   s   | j }|s| r|jS tdnXt| dkrvt|  d \}}|j}|dkr^|||||< n|| ||||< |S t|}|dk rtdnT|dkr| 	 S |dkr| 
 S |dkr| | 
  S t| dkr| |S | |S dS )	a(  raise polynomial to power `n`

        Examples
        ========

        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.rings import ring

        >>> _, x, y = ring('x, y', ZZ)
        >>> p = x + y**2
        >>> p**3
        x**3 + 3*x**2*y**2 + 3*x*y**4 + y**6

        z0**0r   r   zNegative exponentrJ        N)r6   r   r   rn   r?   rH   r   r   r   r   square_pow_multinomial_pow_generic)r   rX  r6   r   r   r   r4   r4   r5   __pow__~  s0    


zPolyElement.__pow__c                 C   s@   | j j}| }|d@ r*|| }|d8 }|s*q<| }|d }q|S )Nr   rJ  )r6   r   rc  )r   rX  r   rE   r4   r4   r5   re    s    
zPolyElement._pow_genericc                 C   s   t t| | }| jj}| jj}|  }| jjj}| jj}|D ]|\}}	|}
|	}t||D ](\}\}}|rZ||
||}
||| 9 }qZt	|
}|}|
||| }|r|||< q@||v r@||= q@|S rW   )r   rn   rH   r6   r   r   r1   r   rL   rm   rt   )r   rX  Zmultinomialsr   r   r   r   r   multinomialZmultinomial_coeffZproduct_monomZproduct_coeffr   r   r   r4   r4   r5   rd    s*    

zPolyElement._pow_multinomialc                 C   s   | j }|j}|j}t|  }|jj}|j}tt|D ]N}|| }| | }	t|D ]0}
||
 }|||}||||	| |   ||< qTq8|	d}|j}| 
 D ](\}}|||}||||d  ||< q|  |S )a  square of a polynomial

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y = ring('x, y', ZZ)
        >>> p = x + y**2
        >>> p.square()
        x**2 + 2*x*y**2 + y**4

        rJ  )r6   r   rt   r?   r  r1   r   r   rn   imul_numrH   r  )r   r6   r   rt   r  r   r   r   k1pkjk2r   r  r  r4   r4   r5   rc    s(    


zPolyElement.squarec                 C   s   | j }|stdnft||jr*| |S t|trzt|jtrP|jj |j krPn*t|j jtrv|j jj |krv|| S t	S z|
|}W n ty   t	 Y S 0 | || |fS d S Npolynomial division)r6   ZeroDivisionErrorrX   r}   r   ra   r1   r   __rdivmod__r  r   r   
quo_ground
rem_groundr  r  r6   r4   r4   r5   
__divmod__  s     




zPolyElement.__divmod__c                 C   s   t S rW   r  r  r4   r4   r5   rp    s    zPolyElement.__rdivmod__c                 C   s   | j }|stdnft||jr*| |S t|trzt|jtrP|jj |j krPn*t|j jtrv|j jj |krv|| S t	S z|
|}W n ty   t	 Y S 0 | |S d S rm  )r6   ro  rX   r}   remra   r1   r   __rmod__r  r   r   rr  rs  r4   r4   r5   __mod__  s     




zPolyElement.__mod__c                 C   s   t S rW   ru  r  r4   r4   r5   rw  .  s    zPolyElement.__rmod__c                 C   s   | j }|stdnzt||jr>|jr2| |d  S | |S nPt|trt|jtrd|jj |j krdn*t|j jtr|j jj |kr|	| S t
S z||}W n ty   t
 Y S 0 | |S d S )Nrn  r   )r6   ro  rX   r}   r:  quora   r1   r   __rtruediv__r  r   r   rq  rs  r4   r4   r5   __truediv__1  s$    



zPolyElement.__truediv__c                 C   s   t S rW   ru  r  r4   r4   r5   rz  J  s    zPolyElement.__rtruediv__c                    sJ   | j j| j j}|j | j j|jr6 fdd}n fdd}|S )Nc                    sF   | \}}|\}}|kr|}n
||}|d ur>| ||fS d S d S rW   r4   Z	a_lm_a_lcZ	b_lm_b_lca_lma_lcb_lmb_lcr   Z
domain_quor   r3  r4   r5   term_divY  s    
z'PolyElement._term_div.<locals>.term_divc                    sN   | \}}|\}}|kr|}n
||}|d u sF|| sF| ||fS d S d S rW   r4   r|  r  r4   r5   r  e  s    
)r6   r   r1   ry  r   r  )r   r1   r  r4   r  r5   	_term_divR  s    zPolyElement._term_divc                    s  | j  d}t|trd}|g}t|s.td| sL|rB j jfS g  jfS |D ]}|j  krPtdqPt|} fddt|D }| 	 } j}| 
 }dd |D }	|rnd}
d}|
|k rH|dkrH| }|||| f|	|
 ||
 |	|
  f}|d	ur>|\}}||
 ||f||
< |||
 || f}d
}q|
d
7 }
q|s| }|||| f}||= q| jkr||7 }|r|s j|fS |d |fS n||fS d	S )aU  Division algorithm, see [CLO] p64.

        fv array of polynomials
           return qv, r such that
           self = sum(fv[i]*qv[i]) + r

        All polynomials are required not to be Laurent polynomials.

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y = ring('x, y', ZZ)
        >>> f = x**3
        >>> f0 = x - y**2
        >>> f1 = x - y
        >>> qv, r = f.div((f0, f1))
        >>> qv[0]
        x**2 + x*y**2 + y**4
        >>> qv[1]
        0
        >>> r
        y**6

        FTrn  z"self and f must have the same ringc                    s   g | ]
} j qS r4   )r   )r9   r   r6   r4   r5   r;     r<   z#PolyElement.div.<locals>.<listcomp>c                 S   s   g | ]}|  qS r4   )r   )r9   fxr4   r4   r5   r;     r<   r   Nr   )r6   rX   ra   r^   ro  r   r   rn   r   r   r  r   _iadd_monom_iadd_poly_monomr   )r   fvZ
ret_singlerk   r[   Zqvr   rr  Zexpvsr   Zdivoccurredr   termZexpv1rE   r4   r  r5   r   s  sV    



&


zPolyElement.divc                 C   s@  | }t |tr|g}t|s$td|j}|j}|j}|j}|j}| }|j	}	|
 }|j}
|r<|D ]}||	|j	}|d urh|\}}| D ]8\}}|||}|
||||  }|s||= q|||< q| }|d ur||| f}	 q^qh|	\}}||v r||  |7  < n|||< ||= | }|d ur^||| f}	q^|S rm  )rX   ra   r^   ro  r6   r1   r   r   r  LTr   rt   r   r   )r   Grk   r6   r1   r   r   r  r  ltfrt   gtqrD   rE   mgcgm1c1ZltmZltcr4   r4   r5   rv    sL    



zPolyElement.remc                 C   s   |  |d S Nr   )r   )rk   r  r4   r4   r5   ry    s    zPolyElement.quoc                 C   s$   |  |\}}|s|S t| |d S rW   )r   r!   )rk   r  qr  r4   r4   r5   exquo  s    zPolyElement.exquoc                 C   s^   | | j jv r|  }n| }|\}}||}|du r>|||< n||7 }|rT|||< n||= |S )a  add to self the monomial coeff*x0**i0*x1**i1*...
        unless self is a generator -- then just return the sum of the two.

        mc is a tuple, (monom, coeff), where monomial is (i0, i1, ...)

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y = ring('x, y', ZZ)
        >>> p = x**4 + 2*y
        >>> m = (1, 2)
        >>> p1 = p._iadd_monom((m, 5))
        >>> p1
        x**4 + 5*x*y**2 + 2*y
        >>> p1 is p
        True
        >>> p = x
        >>> p1 = p._iadd_monom((m, 5))
        >>> p1
        5*x*y**2 + x
        >>> p1 is p
        False

        N)r6   r   r   rt   )r   mcZcpselfr   r   rE   r4   r4   r5   r    s    



zPolyElement._iadd_monomc                 C   s~   | }||j jv r| }|\}}|j}|j jj}|j j}| D ]8\}	}
||	|}||||
|  }|rr|||< q@||= q@|S )aE  add to self the product of (p)*(coeff*x0**i0*x1**i1*...)
        unless self is a generator -- then just return the sum of the two.

        mc is a tuple, (monom, coeff), where monomial is (i0, i1, ...)

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y, z = ring('x, y, z', ZZ)
        >>> p1 = x**4 + 2*y
        >>> p2 = y + z
        >>> m = (1, 2, 3)
        >>> p1 = p1._iadd_poly_monom(p2, (m, 3))
        >>> p1
        x**4 + 3*x*y**3*z**3 + 3*x*y**2*z**4 + 2*y

        )r6   r   r   rt   r1   r   r   rH   )r   r  r  r  rD   rE   rt   r   r   r  r  kar   r4   r4   r5   r  #  s    


zPolyElement._iadd_poly_monomc                    s@   | j | | st S  dk r"dS t fdd|  D S dS )z
        The leading degree in ``x`` or the main variable.

        Note that the degree of 0 is negative infinity (the SymPy object -oo).

        r   c                    s   g | ]}|  qS r4   r4   rF  r   r4   r5   r;   V  r<   z&PolyElement.degree.<locals>.<listcomp>N)r6   r   r   ri   rH  rk   xr4   r  r5   degreeH  s    zPolyElement.degreec                 C   s2   | st  f| jj S ttttt|   S dS )z
        A tuple containing leading degrees in all variables.

        Note that the degree of 0 is negative infinity (the SymPy object -oo)

        N)	r   r6   r~   rm   rI   ri   r?   rL   rH  rj   r4   r4   r5   degreesX  s    zPolyElement.degreesc                    s@   | j | | st S  dk r"dS t fdd|  D S dS )z
        The tail degree in ``x`` or the main variable.

        Note that the degree of 0 is negative infinity (the SymPy object -oo)

        r   c                    s   g | ]}|  qS r4   r4   rF  r  r4   r5   r;   r  r<   z+PolyElement.tail_degree.<locals>.<listcomp>N)r6   r   r   minrH  r  r4   r  r5   tail_degreed  s    zPolyElement.tail_degreec                 C   s2   | st  f| jj S ttttt|   S dS )z
        A tuple containing tail degrees in all variables.

        Note that the degree of 0 is negative infinity (the SymPy object -oo)

        N)	r   r6   r~   rm   rI   r  r?   rL   rH  rj   r4   r4   r5   tail_degreest  s    zPolyElement.tail_degreesc                 C   s   | r| j | S dS dS )aT  Leading monomial tuple according to the monomial ordering.

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y, z = ring('x, y, z', ZZ)
        >>> p = x**4 + x**3*y + x**2*z**2 + z**7
        >>> p.leading_expv()
        (4, 0, 0)

        N)r6   r   r   r4   r4   r5   r     s    zPolyElement.leading_expvc                 C   s   |  || jjjS rW   )rt   r6   r1   r   r   r   r4   r4   r5   
_get_coeff  s    zPolyElement._get_coeffc                 C   sp   |dkr|  | jjS t|| jjr`t| }t|dkr`|d \}}|| jjj	kr`|  |S t
d| dS )a  
        Returns the coefficient that stands next to the given monomial.

        Parameters
        ==========

        element : PolyElement (with ``is_monomial = True``) or 1

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y, z = ring("x,y,z", ZZ)
        >>> f = 3*x**2*y - x*y*z + 7*z**3 + 23

        >>> f.coeff(x**2*y)
        3
        >>> f.coeff(x*y)
        0
        >>> f.coeff(1)
        23

        r   r   zexpected a monomial, got %sN)r  r6   r   rX   r}   r?   r   rn   r1   r   r   )r   r   r   r   r   r4   r4   r5   r     s    
zPolyElement.coeffc                 C   s   |  | jjS )z!Returns the constant coeffcient. )r  r6   r   r   r4   r4   r5   r    s    zPolyElement.constc                 C   s   |  |  S rW   )r  r   r   r4   r4   r5   r9    s    zPolyElement.LCc                 C   s    |   }|d u r| jjS |S d S rW   )r   r6   r   r  r4   r4   r5   LM  s    zPolyElement.LMc                 C   s&   | j j}|  }|r"| j jj||< |S )a  
        Leading monomial as a polynomial element.

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y = ring('x, y', ZZ)
        >>> (3*x*y + y**2).leading_monom()
        x*y

        )r6   r   r   r1   r   r   r   r   r4   r4   r5   leading_monom  s
    zPolyElement.leading_monomc                 C   s4   |   }|d u r"| jj| jjjfS || |fS d S rW   )r   r6   r   r1   r   r  r  r4   r4   r5   r    s    zPolyElement.LTc                 C   s(   | j j}|  }|dur$| | ||< |S )a  Leading term as a polynomial element.

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y = ring('x, y', ZZ)
        >>> (3*x*y + y**2).leading_term()
        3*x*y

        N)r6   r   r   r  r4   r4   r5   leading_term  s
    zPolyElement.leading_termc                    sP    d u r| j j n
t   tu r6t|dd ddS t| fddddS d S )Nc                 S   s   | d S r  r4   r   r4   r4   r5   rf     r<   z%PolyElement._sorted.<locals>.<lambda>T)rh   reversec                    s    | d S r  r4   r  rl   r4   r5   rf     r<   )r6   r2   rq   rp   r   sorted)r   rV   r2   r4   rl   r5   _sorted  s    

zPolyElement._sortedc                 C   s   dd |  |D S )a  Ordered list of polynomial coefficients.

        Parameters
        ==========

        order : :class:`~.MonomialOrder` or coercible, optional

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.orderings import lex, grlex

        >>> _, x, y = ring("x, y", ZZ, lex)
        >>> f = x*y**7 + 2*x**2*y**3

        >>> f.coeffs()
        [2, 1]
        >>> f.coeffs(grlex)
        [1, 2]

        c                 S   s   g | ]\}}|qS r4   r4   )r9   _r   r4   r4   r5   r;      r<   z&PolyElement.coeffs.<locals>.<listcomp>r   r   r2   r4   r4   r5   rR     s    zPolyElement.coeffsc                 C   s   dd |  |D S )a
  Ordered list of polynomial monomials.

        Parameters
        ==========

        order : :class:`~.MonomialOrder` or coercible, optional

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.orderings import lex, grlex

        >>> _, x, y = ring("x, y", ZZ, lex)
        >>> f = x*y**7 + 2*x**2*y**3

        >>> f.monoms()
        [(2, 3), (1, 7)]
        >>> f.monoms(grlex)
        [(1, 7), (2, 3)]

        c                 S   s   g | ]\}}|qS r4   r4   )r9   r   r  r4   r4   r5   r;   :  r<   z&PolyElement.monoms.<locals>.<listcomp>r  r  r4   r4   r5   monoms"  s    zPolyElement.monomsc                 C   s   |  t|  |S )a  Ordered list of polynomial terms.

        Parameters
        ==========

        order : :class:`~.MonomialOrder` or coercible, optional

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ
        >>> from sympy.polys.orderings import lex, grlex

        >>> _, x, y = ring("x, y", ZZ, lex)
        >>> f = x*y**7 + 2*x**2*y**3

        >>> f.terms()
        [((2, 3), 2), ((1, 7), 1)]
        >>> f.terms(grlex)
        [((1, 7), 1), ((2, 3), 2)]

        )r  r?   rH   r  r4   r4   r5   r   <  s    zPolyElement.termsc                 C   s   t |  S )z,Iterator over coefficients of a polynomial. )iterr@   r   r4   r4   r5   
itercoeffsV  s    zPolyElement.itercoeffsc                 C   s   t |  S )z)Iterator over monomials of a polynomial. )r  r  r   r4   r4   r5   rH  Z  s    zPolyElement.itermonomsc                 C   s   t |  S )z%Iterator over terms of a polynomial. )r  rH   r   r4   r4   r5   r   ^  s    zPolyElement.itertermsc                 C   s   t |  S )z+Unordered list of polynomial coefficients. r>   r   r4   r4   r5   
listcoeffsb  s    zPolyElement.listcoeffsc                 C   s   t |  S )z(Unordered list of polynomial monomials. )r?   r  r   r4   r4   r5   
listmonomsf  s    zPolyElement.listmonomsc                 C   s   t |  S )z$Unordered list of polynomial terms. r  r   r4   r4   r5   	listtermsj  s    zPolyElement.listtermsc                 C   sB   | | j jv r| | S |s$|   dS | D ]}| |  |9  < q(| S )a:  multiply inplace the polynomial p by an element in the
        coefficient ring, provided p is not one of the generators;
        else multiply not inplace

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y = ring('x, y', ZZ)
        >>> p = x + y**2
        >>> p1 = p.imul_num(3)
        >>> p1
        3*x + 3*y**2
        >>> p1 is p
        True
        >>> p = x
        >>> p1 = p.imul_num(3)
        >>> p1
        3*x
        >>> p1 is p
        False

        N)r6   r   clear)r   rE   r   r4   r4   r5   rh  n  s    zPolyElement.imul_numc                 C   s0   | j j}|j}|j}|  D ]}|||}q|S )z*Returns GCD of polynomial's coefficients. )r6   r1   r   r   r  )rk   r1   contr   r   r4   r4   r5   rB    s    zPolyElement.contentc                 C   s   |   }|| |fS )z,Returns content and a primitive polynomial. )rB  rq  )rk   r  r4   r4   r5   	primitive  s    zPolyElement.primitivec                 C   s   | s| S |  | jS dS )z5Divides all coefficients by the leading coefficient. N)rq  r9  rj   r4   r4   r5   monic  s    zPolyElement.monicc                    s,    s| j jS  fdd|  D }| |S )Nc                    s   g | ]\}}||  fqS r4   r4   r   r  r4   r5   r;     r<   z*PolyElement.mul_ground.<locals>.<listcomp>)r6   r   r   r   )rk   r  r   r4   r  r5   r!    s    zPolyElement.mul_groundc                    s*   | j j fdd|  D }| |S )Nc                    s   g | ]\}}| |fqS r4   r4   r9   f_monomZf_coeffr   r   r4   r5   r;     r<   z)PolyElement.mul_monom.<locals>.<listcomp>)r6   r   rH   r   )rk   r   r   r4   r  r5   	mul_monom  s    zPolyElement.mul_monomc                    sZ   |\ | r s| j jS | j jkr.|  S | j j fdd|  D }| |S )Nc                    s"   g | ]\}}||  fqS r4   r4   r  r   r   r   r4   r5   r;     r<   z(PolyElement.mul_term.<locals>.<listcomp>)r6   r   r   r!  r   rH   r   )rk   r  r   r4   r  r5   mul_term  s    
zPolyElement.mul_termc                    sl   | j j}std| r"|jkr&| S |jrL|j  fdd|  D }nfdd|  D }| |S )Nrn  c                    s   g | ]\}}| |fqS r4   r4   r   ry  r  r4   r5   r;     r<   z*PolyElement.quo_ground.<locals>.<listcomp>c                    s$   g | ]\}}|  s||  fqS r4   r4   r   r  r4   r5   r;     r<   )r6   r1   ro  r   r  ry  r   r   )rk   r  r1   r   r4   r  r5   rq    s    zPolyElement.quo_groundc                    sl    \}}|st dn"| s"| jjS || jjkr8| |S |   fdd|  D }| dd |D S )Nrn  c                    s   g | ]}| qS r4   r4   r9   tr  r  r4   r5   r;     r<   z(PolyElement.quo_term.<locals>.<listcomp>c                 S   s   g | ]}|d ur|qS rW   r4   r  r4   r4   r5   r;     r<   )ro  r6   r   r   rq  r  r   r   )rk   r  r   r   r   r4   r  r5   quo_term  s    

zPolyElement.quo_termc                    sx   | j jjrLg }|  D ]2\}}|  }| d kr:|  }|||f qn fdd|  D }| |}|  |S )NrJ  c                    s   g | ]\}}||  fqS r4   r4   r   r   r4   r5   r;     r<   z,PolyElement.trunc_ground.<locals>.<listcomp>)r6   r1   is_ZZr   r   r   r  )rk   r   r   r   r   r   r4   r  r5   trunc_ground  s    

zPolyElement.trunc_groundc                 C   sB   | }|  }|  }|jj||}||}||}|||fS rW   )rB  r6   r1   r   rq  )r   r  rk   fcgcr   r4   r4   r5   extract_ground  s    

zPolyElement.extract_groundc                    s6   | s| j jjS | j jj | fdd|  D S d S )Nc                    s   g | ]} |qS r4   r4   )r9   r   Z
ground_absr4   r5   r;     r<   z%PolyElement._norm.<locals>.<listcomp>)r6   r1   r   absr  )rk   Z	norm_funcr4   r  r5   _norm  s    

zPolyElement._normc                 C   s
   |  tS rW   )r  ri   rj   r4   r4   r5   max_norm  s    zPolyElement.max_normc                 C   s
   |  tS rW   )r  rJ   rj   r4   r4   r5   l1_norm	  s    zPolyElement.l1_normc                 G   s   | j }| gt| }dg|j }|D ]6}| D ](}t|D ]\}}t|| |||< q<q0q$t|D ]\}}	|	sdd||< qdt|}tdd |D r||fS g }
|D ]F}|j}|	 D ](\}}dd t
||D }||t|< q|
| q||
fS )Nr   r   c                 s   s   | ]}|d kV  qdS rD  r4   )r9   re   r4   r4   r5   r\     r<   z&PolyElement.deflate.<locals>.<genexpr>c                 S   s   g | ]\}}|| qS r4   r4   r9   r   rk  r4   r4   r5   r;   &  r<   z'PolyElement.deflate.<locals>.<listcomp>)r6   r?   r~   rH  r   r   rm   r^   r   r   rL   r   )rk   r  r6   rT   Jr   r   r   rD   re   HhIr   Nr4   r4   r5   deflate  s*    
zPolyElement.deflatec                 C   s>   | j j}|  D ](\}}dd t||D }||t|< q|S )Nc                 S   s   g | ]\}}|| qS r4   r4   r  r4   r4   r5   r;   1  r<   z'PolyElement.inflate.<locals>.<listcomp>)r6   r   r   rL   rm   )rk   r  r   r  r   r  r4   r4   r5   inflate-  s
    zPolyElement.inflatec                 C   sf   | }|j j}|js6| \}}| \}}|||}|| ||}|jsZ||S | S d S rW   )	r6   r1   r  r  r   ry  r   r!  r  )r   r  rk   r1   r  r  rE   r  r4   r4   r5   r   6  s    
zPolyElement.lcmc                 C   s   |  |d S r  )	cofactorsrk   r  r4   r4   r5   r   F  s    zPolyElement.gcdc                 C   s   | s|s| j j}|||fS | s8| |\}}}|||fS |sV|| \}}}|||fS t| dkr|| |\}}}|||fS t|dkr|| \}}}|||fS | |\}\} }| |\}}}||||||fS r   )r6   r   	_gcd_zerorn   
_gcd_monomr  _gcdr  )rk   r  r   r  cffcfgr  r4   r4   r5   r  I  s$    




zPolyElement.cofactorsc                 C   s4   | j j| j j }}|jr"|||fS | || fS d S rW   )r6   r   r   r=  )rk   r  r   r   r4   r4   r5   r  _  s    
zPolyElement._gcd_zeroc                    s   | j }|jj}|jj|j}|jt|  d \}}||  | D ]\}}||| | qH|  fg}	| || fg}
|  fdd| D }|	|
|fS )Nr   c                    s$   g | ]\}}|| fqS r4   r4   )r9   r  r  Z_cgcdZ_mgcdZ
ground_quor   r4   r5   r;   s  r<   z*PolyElement._gcd_monom.<locals>.<listcomp>)	r6   r1   r   ry  r   r   r?   r   r   )rk   r  r6   Z
ground_gcdr   mfcfr  r  r  r  r  r4   r  r5   r  f  s    

"zPolyElement._gcd_monomc                 C   s:   | j }|jjr| |S |jjr*| |S || |S d S rW   )r6   r1   is_QQ_gcd_QQr  _gcd_ZZdmp_inner_gcd)rk   r  r6   r4   r4   r5   r  v  s    

zPolyElement._gcdc                 C   s
   t | |S rW   r   r  r4   r4   r5   r    s    zPolyElement._gcd_ZZc                 C   s   | }|j }|j|j d}| \}}| \}}||}||}||\}}}	||}|j|  }
}||	|j
|
|}|	|	|j
|
|}	|||	fS )Nr   )r6   r   r1   r  r
  r   r  r9  r  r!  ry  )r   r  rk   r6   r   r  r  r  r  r  rE   r4   r4   r5   r    s    


zPolyElement._gcd_QQc                 C   s  | }|j }|s||jfS |j}|jr*|js<||\}}}n|j| d}| \}	}| \}
}|	|}|	|}||\}}}|j|
|	\}}
}	|	|}|	|}|
|
}|
|	}| }||jkr|| }}n2||j kr | |  }}n|
|}|
|}||fS )a  
        Cancel common factors in a rational function ``f/g``.

        Examples
        ========

        >>> from sympy.polys import ring, ZZ
        >>> R, x,y = ring("x,y", ZZ)

        >>> (2*x**2 - 2).cancel(x**2 - 2*x + 1)
        (2*x + 2, x - 1)

        r   )r6   r   r1   r  r  r  r   r  r
  r   r!  canonical_unit)r   r  rk   r6   r1   r  r   r  r   cqcpur4   r4   r5   cancel  s4    









zPolyElement.cancelc                 C   s   | j j}|| jS rW   )r6   r1   r  r9  )rk   r1   r4   r4   r5   r    s    zPolyElement.canonical_unitc           	      C   s`   | j }||}||}|j}|  D ]2\}}|| r(|||}||||  ||< q(|S )a!  Computes partial derivative in ``x``.

        Examples
        ========

        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> _, x, y = ring("x,y", ZZ)
        >>> p = x + x**2*y**3
        >>> p.diff(x)
        2*x*y**3 + 1

        )r6   r   r   r   r   r   r   )	rk   r  r6   r   rD   r  r   r   er4   r4   r5   diff  s    

zPolyElement.diffc                 G   sT   dt |  k r| jjkr8n n| tt| jj|S td| jjt |f d S )Nr   z1expected at least 1 and at most %s values, got %s)rn   r6   r~   evaluater?   rL   r/   r   )rk   r@   r4   r4   r5   r     s     zPolyElement.__call__c                    sH  | }t |tr`|d u r`|d |dd   \ }}| |}|sD|S  fdd|D }||S |j}||}|j|}|jdkr|jj}|	 D ]\\}}||||  7 }q|S |
|j}	|	 D ]t\}
}|
| |
d | |
|d d    }}
|||  }|
|	v r2||	|
  }|r*||	|
< n|	|
= q|r||	|
< q|	S d S )Nr   r   c                    s   g | ]\}}|  |fqS r4   )r   )r9   Yrd   Xr4   r5   r;     r<   z(PolyElement.evaluate.<locals>.<listcomp>)rX   r?   r  r6   r   r1   r   r~   r   r   r   )r   r  rd   rk   r6   r   resultrX  r   r   r   r4   r  r5   r    s8    


&


zPolyElement.evaluatec                 C   s   | }t |tr4|d u r4|D ]\}}|||}q|S |j}||}|j|}|jdkr|jj}|	 D ]\\}}	||	||  7 }qj|
|S |j}
|	 D ]x\}}	|| |d | d ||d d    }}|	||  }	||
v r
|	|
|  }	|	r|	|
|< n|
|= q|	r|	|
|< q|
S d S )Nr   rb   )rX   r?   subsr6   r   r1   r   r~   r   r   r   )r   r  rd   rk   r  r6   r   r  rX  r   r   r   r4   r4   r5   r  	  s2    


*


zPolyElement.subsc                    s  | j }|j}ttt|jtt|j |d ur>||fg}nDt|trRt|}n0t|trzt	t|
  fddd}ntdt|D ]"\}\}} | ||f||< q|  D ]`\}}	t|}|j}
|D ]*\}}|| d }||< |r|
|| 9 }
q|
t||	f}
||
7 }q|S )Nc                    s    | d  S r  r4   )r  Zgens_mapr4   r5   rf   Q	  r<   z%PolyElement.compose.<locals>.<lambda>rg   z9expected a generator, value pair a sequence of such pairsr   )r6   r   rK   r?   rL   r/   r   r~   rX   r  rH   r   r   r   r   r   r  rm   )rk   r  rd   r6   r   replacementsr  r  r   r   Zsubpolyr   rX  r4   r  r5   r   F	  s,    



zPolyElement.composec                 C   s   | j | |S rW   )r6   dmp_pdivr  r4   r4   r5   pdivi	  s    zPolyElement.pdivc                 C   s   | j | |S rW   )r6   dmp_premr  r4   r4   r5   preml	  s    zPolyElement.premc                 C   s   | j | |S rW   )r6   dmp_quor  r4   r4   r5   pquoo	  s    zPolyElement.pquoc                 C   s   | j | |S rW   )r6   	dmp_exquor  r4   r4   r5   pexquor	  s    zPolyElement.pexquoc                 C   s   | j | |S rW   )r6   dmp_half_gcdexr  r4   r4   r5   
half_gcdexu	  s    zPolyElement.half_gcdexc                 C   s   | j | |S rW   )r6   	dmp_gcdexr  r4   r4   r5   gcdexx	  s    zPolyElement.gcdexc                 C   s   | j | |S rW   )r6   dmp_subresultantsr  r4   r4   r5   subresultants{	  s    zPolyElement.subresultantsc                 C   s   | j | |S rW   )r6   dmp_resultantr  r4   r4   r5   	resultant~	  s    zPolyElement.resultantc                 C   s   | j | S rW   )r6   dmp_discriminantrj   r4   r4   r5   discriminant	  s    zPolyElement.discriminantc                 C   s    | j jr| j | S tdd S )Nzpolynomial decomposition)r6   r   dup_decomposer"   rj   r4   r4   r5   	decompose	  s    zPolyElement.decomposec                 C   s"   | j jr| j | |S tdd S )Nzpolynomial shift)r6   r   	dup_shiftr"   )rk   rd   r4   r4   r5   shift	  s    zPolyElement.shiftc                 C   s    | j jr| j | S tdd S )Nzsturm sequence)r6   r   	dup_sturmr"   rj   r4   r4   r5   sturm	  s    zPolyElement.sturmc                 C   s   | j | S rW   )r6   dmp_gff_listrj   r4   r4   r5   gff_list	  s    zPolyElement.gff_listc                 C   s   | j | S rW   )r6   dmp_sqf_normrj   r4   r4   r5   sqf_norm	  s    zPolyElement.sqf_normc                 C   s   | j | S rW   )r6   dmp_sqf_partrj   r4   r4   r5   sqf_part	  s    zPolyElement.sqf_partFc                 C   s   | j j| |dS )N)r^   )r6   dmp_sqf_list)rk   r^   r4   r4   r5   sqf_list	  s    zPolyElement.sqf_listc                 C   s   | j | S rW   )r6   dmp_factor_listrj   r4   r4   r5   factor_list	  s    zPolyElement.factor_list)N)N)N)N)N)N)N)N)N)F)rr   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$  rY   r   r8  r  r:  r;  r-  r<  r=  r>  r?  r@  rA  rC  rI  rK  rN  rP  rR  rS  rT  rW  rU  rZ  rY  r`  r[  rf  re  rd  rc  rt  rp  rx  rw  r{  rz  __floordiv____rfloordiv__r  r   rv  ry  r  r  r  r  r  r  r  r   r  r   r  r9  r  r  r  r  r  rR   r  r   r  rH  r   r  r  r  rh  rB  r  r  r!  r  r  rq  r  r  rr  r  r  r  r  r  r  r   r   r  r  r  r  r  r  r  r  r  r   r  r  r   r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r  r  r  r4   r4   r4   r5   ra   ,  sH  	0
















6620$!L-,%%


#!	
8,'#
ra   N)Or   typingr   r   tDictoperatorr   r   r   r   r   r	   	functoolsr
   typesr   sympy.core.exprr   sympy.core.numbersr   r   sympy.core.symbolr   r   r]   sympy.core.sympifyr   r   sympy.ntheory.multinomialr   Zsympy.polys.compatibilityr   sympy.polys.constructorr   sympy.polys.densebasicr   r   !sympy.polys.domains.domainelementr   "sympy.polys.domains.polynomialringr   Zsympy.polys.heuristicgcdr   sympy.polys.monomialsr   sympy.polys.orderingsr   sympy.polys.polyerrorsr   r    r!   r"   sympy.polys.polyoptionsr#   ro   r$   rq   r%   sympy.polys.polyutilsr&   r'   r(   Zsympy.printing.defaultsr)   sympy.utilitiesr*   sympy.utilities.iterablesr+   Zsympy.utilities.magicr,   r6   r7   r=   rU   r`   rs   r.   rK   ra   r4   r4   r4   r5   <module>   sL    
4  j