o
    ohF                    @  s  U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	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#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@mAZAmBZB ddlCmDZD ddlEmFZF eAe.fddZGeAe.fddZHeAe.fdd ZIeAd!d" ZJd#d$ ZKi ZLd%eMd&< G d'd( d(e?eZNG d)d* d*e&e?eeOZPd+S ),zSparse polynomial rings.     )annotations)Any)addmulltlegtge)reduce)GeneratorType)Expr)igcd)Symbolsymbols)CantSympifysympify)multinomial_coefficients)IPolys)construct_domain)ninf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subsets)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 r5   e/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/polys/rings.pyring#   s   r7   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.   r1   r5   r5   r6   xringB   s   
r8   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 r5   )name).0symr5   r5   r6   
<listcomp>}       zvring.<locals>.<listcomp>)r/   r-   r   r0   r1   r5   r5   r6   vringa   s   r>   c           
        s   d}t | s| gd} }ttt| } t||}t| |\}}|jdu rGt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 )	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 r5   listvaluesr:   repr5   r5   r6   r<          zsring.<locals>.<listcomp>)optc                   s"   g | ]} fd d|  D qS )c                   s   i | ]	\}}| | qS r5   r5   r:   mc	coeff_mapr5   r6   
<dictcomp>       z$sring.<locals>.<listcomp>.<dictcomp>)itemsrB   rI   r5   r6   r<         " r   )r,   r@   mapr   r%   r(   r2   sumr   dictzipr/   r0   r3   	from_dict)
exprsr   optionssinglerE   repscoeffs
coeffs_domr4   polysr5   rI   r6   sring   s    

r[   c                 C  sn   t | tr| rt| ddS dS t | tr| fS t| r3tdd | D r(t| S tdd | D r3| S td)NT)seqr5   c                 s      | ]}t |tV  qd S N)
isinstancestrr:   sr5   r5   r6   	<genexpr>       z!_parse_symbols.<locals>.<genexpr>c                 s  r]   r^   )r_   r   ra   r5   r5   r6   rc      rd   zbexpected a string, Symbol or expression or a non-empty sequence of strings, Symbols or expressions)r_   r`   _symbolsr   r,   allr    r   r5   r5   r6   _parse_symbols   s   

rh   zdict[Any, Any]_ring_cachec                   @  s@  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dGddZdd Zedd Zedd ZdHddZdd Zdd Zdd  ZeZdHd!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'dEdF Z(dS )Ir/   z*Multivariate distributed polynomial ring. c                   s  t t|}t|}t|}t  | j||| f}t|}|d u r|j	r5t
|t
|j@ r5tdt| }||_t||_tdtfd|i|_||_||_||_ |_d| |_| |_t
|j|_|j|jfg|_|rt|}| |_ |! |_"|# |_$|% |_&|' |_(|) |_*|+ |_,ndd }||_ ||_"dd |_$||_&||_(||_*||_, t-u rt.|_/n fdd|_/t0|j|jD ]\}	}
t1|	t2r|	j3}t4||st5|||
 q|t|< |S )	Nz7polynomial ring and it's ground domain share generatorsPolyElementr7   r   c                 S     dS Nr5   r5   )abr5   r5   r6   <lambda>       z"PolyRing.__new__.<locals>.<lambda>c                 S  rl   rm   r5   )rn   ro   rH   r5   r5   r6   rp      rq   c                   s   t |  dS )Nkey)maxfr3   r5   r6   rp          )6tuplerh   len	DomainOpt
preprocessOrderOpt__name__ri   getis_Compositesetr   r    object__new___hash_tuplehash_hashtyperj   dtypengensr2   r3   
zero_monom_gensr0   	_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   rt   leading_expvrR   r_   r   r9   hasattrsetattr)clsr   r2   r3   r   r   objcodegenmonunitsymbol	generatorr9   r5   rw   r6   r      sb   















zPolyRing.__new__c                 C  sF   | j j}g }t| jD ]}| |}| j}|||< || qt|S )z(Return a list of polynomial generators. )r2   r   ranger   monomial_basiszeroappendry   )selfr   r   iexpvpolyr5   r5   r6   r     s   
zPolyRing._gensc                 C  s   | j | j| jfS r^   )r   r2   r3   r   r5   r5   r6   __getnewargs__     zPolyRing.__getnewargs__c                 C  s.   | j  }|d= |D ]
}|dr||= q
|S )Nr   	monomial_)__dict__copy
startswith)r   staters   r5   r5   r6   __getstate__  s   

zPolyRing.__getstate__c                 C  s   | j S r^   )r   r   r5   r5   r6   __hash__   s   zPolyRing.__hash__c                 C  s2   t |to| j| j| j| jf|j|j|j|jfkS r^   )r_   r/   r   r2   r   r3   r   otherr5   r5   r6   __eq__#  s
   
zPolyRing.__eq__c                 C  
   | |k S r^   r5   r   r5   r5   r6   __ne__(     
zPolyRing.__ne__Nc                 C  s    |  |p| j|p
| j|p| jS r^   )	__class__r   r2   r3   )r   r   r2   r3   r5   r5   r6   clone+  s    zPolyRing.clonec                 C  s   dg| j  }d||< t|S )zReturn the ith-basis element. r      )r   ry   )r   r   basisr5   r5   r6   r   .  s   zPolyRing.monomial_basisc                 C  s   |   S r^   )r   r   r5   r5   r6   r   4  s   zPolyRing.zeroc                 C  s   |  | jS r^   )r   r   r   r5   r5   r6   r   8     zPolyRing.onec                 C  s   | j ||S r^   )r2   convertr   elementorig_domainr5   r5   r6   
domain_new<     zPolyRing.domain_newc                 C  s   |  | j|S r^   )term_newr   )r   coeffr5   r5   r6   
ground_new?  r   zPolyRing.ground_newc                 C  s    |  |}| j}|r|||< |S r^   )r   r   )r   monomr   r   r5   r5   r6   r   B  s
   
zPolyRing.term_newc                 C  s   t |tr"| |jkr|S t | jtr| jj|jkr| |S tdt |tr+tdt |tr5| 	|S t |t
rOz| |W S  tyN   | | Y S w t |trY| |S | |S )N
conversionparsing)r_   rj   r7   r2   r   r   NotImplementedErrorr`   rQ   rS   r@   
from_terms
ValueError	from_listr   	from_exprr   r   r5   r5   r6   ring_newI  s&   









zPolyRing.ring_newc                 C  s8   | j }| j}| D ]\}}|||}|r|||< q
|S r^   )r   r   rM   )r   r   r   r   r   r   r   r5   r5   r6   rS   a  s   
zPolyRing.from_dictc                 C  s   |  t||S r^   )rS   rQ   r   r5   r5   r6   r   l  r   zPolyRing.from_termsc                 C  s   |  t|| jd | jS Nr   )rS   r   r   r2   r   r5   r5   r6   r   o  s   zPolyRing.from_listc                   s$   j  fdd  t|S )Nc                   s    | }|d ur|S | jrtttt | jS | jr'tttt | jS | 	 \}}|j
r<|dkr< |t| S | S r   )r   is_Addr
   r   r@   rO   argsis_Mulr   as_base_exp
is_Integerintr   r   )exprr   baseexp_rebuildr2   mappingr   r5   r6   r   u  s   
z(PolyRing._rebuild_expr.<locals>._rebuild)r2   r   )r   r   r   r5   r   r6   _rebuild_exprr  s   zPolyRing._rebuild_exprc                 C  sP   t tt| j| j}z| ||}W n ty"   td| |f w | |S )Nz@expected an expression convertible to a polynomial in %s, got %s)	rQ   r@   rR   r   r0   r   r   r   r   )r   r   r   r   r5   r5   r6   r     s   
zPolyRing.from_exprc                 C  s   |du r| j rd}|S d}|S t|tr9|}d|kr"|| j k r"	 |S | j  |kr3|dkr3| d }|S td| t|| jrVz	| j|}W |S  tyU   td| w t|trrz	| j|}W |S  tyq   td| w td| )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   r_   r   r   r   r0   indexr`   r   )r   genr   r5   r5   r6   r     s<   


zPolyRing.indexc                   s>   t t| j|  fddt| jD }|s| jS | j|dS )z,Remove specified generators from this ring. c                      g | ]
\}}| vr|qS r5   r5   r:   r   rb   indicesr5   r6   r<         z!PolyRing.drop.<locals>.<listcomp>rg   )r   rO   r   	enumerater   r2   r   r   r0   r   r5   r   r6   drop  s
   zPolyRing.dropc                 C  s    | j | }|s
| jS | j|dS )Nrg   )r   r2   r   )r   rs   r   r5   r5   r6   __getitem__  s   
zPolyRing.__getitem__c                 C  s2   | j js
t| j dr| j| j j dS td| j  )Nr2   r2   z%s is not a composite domain)r2   r   r   r   r   r   r5   r5   r6   	to_ground  s   zPolyRing.to_groundc                 C     t | S r^   r   r   r5   r5   r6   	to_domain     zPolyRing.to_domainc                 C  s   ddl m} || j| j| jS )Nr   )	FracField)sympy.polys.fieldsr   r   r2   r3   )r   r   r5   r5   r6   to_field  s   zPolyRing.to_fieldc                 C  s   t | jdkS r   rz   r0   r   r5   r5   r6   is_univariate     zPolyRing.is_univariatec                 C  s   t | jdkS r   r   r   r5   r5   r6   is_multivariate  r   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   r5   r5   r6   r        
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   r5   r5   r6   r     r   zPolyRing.mulc                   s\   t t| j|  fddt| jD } fddt| jD }|s$| S | j|| j| dS )zd
        Remove specified generators from the ring and inject them into
        its domain.
        c                   r   r5   r5   r   r   r5   r6   r<     r   z+PolyRing.drop_to_ground.<locals>.<listcomp>c                   r   r5   r5   )r:   r   r   r   r5   r6   r<     r   r   r2   )r   rO   r   r   r   r0   r   r   r   r5   r   r6   drop_to_ground  s   zPolyRing.drop_to_groundc                 C  s2   | |krt | jt |j}| jt|dS | S )z+Add the generators of ``other`` to ``self``rg   r   r   unionr   r@   )r   r   symsr5   r5   r6   compose  s   zPolyRing.composec                 C  s$   t | jt |}| jt|dS )z9Add the elements of ``symbols`` as generators to ``self``rg   r  )r   r   r  r5   r5   r6   add_gens&  s   zPolyRing.add_gensc                   s   |dk s	|| j krtd|| jf |s| jS | j}tt| j t|D ] t fddt| j D }|| 	|| j
j7 }q$|S )zo
        Return the elementary symmetric polynomial of degree *n* over
        this ring's generators.
        r   z7Cannot generate symmetric polynomial of order %s for %sc                 3  s    | ]	}t | v V  qd S r^   )r   r:   r   rb   r5   r6   rc   7      z*PolyRing.symmetric_poly.<locals>.<genexpr>)r   r   r0   r   r   r+   r   r   ry   r   r2   )r   nr   r   r5   r	  r6   symmetric_poly+  s   zPolyRing.symmetric_poly)NNNr^   ))r~   
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   __call__rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r5   r5   r5   r6   r/      sR    A










r/   c                   @  sf  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Zdd Zd"ddZdd Zd"ddZd"ddZd"dd Zd"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d Zdd Zdd Zd#ddZd d! ZdS ($  rj   z5Element of multivariate distributed polynomial ring. c                 C  s
   |  |S r^   )r   )r   initr5   r5   r6   new?  r   zPolyElement.newc                 C  s
   | j  S r^   )r7   r   r   r5   r5   r6   parentB  r   zPolyElement.parentc                 C  s   | j t|  fS r^   )r7   r@   	itertermsr   r5   r5   r6   r   E     zPolyElement.__getnewargs__Nc                 C  s.   | j }|d u rt| jt|  f | _ }|S r^   )r   r   r7   	frozensetrM   )r   r   r5   r5   r6   r   J  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   r5   r5   r6   r   U  s   
zPolyElement.copyc                 C  sV   | j |kr| S | j j|jkr#ttt| | j j|j }||| j jS || | j jS r^   )r7   r   r@   rR   r'   r   r2   rS   )r   new_ringtermsr5   r5   r6   set_ringq  s   
zPolyElement.set_ringc                 G  sJ   |s| j j}nt|| j jkrtd| j jt|f t|  g|R  S )Nz+Wrong number of symbols, expected %s got %s)r7   r   rz   r   r   r&   as_expr_dict)r   r   r5   r5   r6   as_exprz  s   
zPolyElement.as_exprc                   s    | j jj  fdd|  D S )Nc                   s   i | ]	\}}| |qS r5   r5   r:   r   r   to_sympyr5   r6   rK     rL   z,PolyElement.as_expr_dict.<locals>.<dictcomp>)r7   r2   r  r  r   r5   r  r6   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                      g | ]
\}}||  fqS r5   r5   )r:   kvcommonr5   r6   r<     r   z,PolyElement.clear_denoms.<locals>.<listcomp>)r7   r2   is_Fieldhas_assoc_Ringr   get_ringr   denomrA   r  rM   )r   r2   ground_ringr   r(  r   r   r5   r#  r6   clear_denoms  s   
zPolyElement.clear_denomsc                 C  s$   t |  D ]	\}}|s| |= qdS )z+Eliminate monomials with zero coefficient. Nr@   rM   )r   r!  r"  r5   r5   r6   
strip_zero  s
   zPolyElement.strip_zeroc                 C  sN   |s|  S t |tr|j| jkrt| |S t| dkrdS | | jj|k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   F)r_   rj   r7   rQ   r   rz   r   r   p1p2r5   r5   r6   r     s   zPolyElement.__eq__c                 C  r   r^   r5   r-  r5   r5   r6   r     r   zPolyElement.__ne__c                 C  s   | j }t||jr1t|  t| krdS |jj}|  D ]}|| | || |s. dS qdS t| dkr9dS z|j|}W n
 t	yK   Y dS w |j| 
 ||S )z+Approximate equality test for polynomials. FTr   )r7   r_   r   r   keysr2   almosteqrz   r   r   const)r.  r/  	tolerancer7   r1  r!  r5   r5   r6   r1    s$   zPolyElement.almosteqc                 C  s   t | |  fS r^   )rz   r  r   r5   r5   r6   sort_key  r   zPolyElement.sort_keyc                 C  s$   t || jjr||  | S tS r^   )r_   r7   r   r4  NotImplemented)r.  r/  opr5   r5   r6   _cmp  s   zPolyElement._cmpc                 C     |  |tS r^   )r7  r   r-  r5   r5   r6   __lt__     zPolyElement.__lt__c                 C  r8  r^   )r7  r   r-  r5   r5   r6   __le__  r:  zPolyElement.__le__c                 C  r8  r^   )r7  r   r-  r5   r5   r6   __gt__  r:  zPolyElement.__gt__c                 C  r8  r^   )r7  r	   r-  r5   r5   r6   __ge__  r:  zPolyElement.__ge__c                 C  sD   | j }||}|jdkr||jfS t|j}||= ||j|dfS )Nr   rg   )r7   r   r   r2   r@   r   r   r   r   r7   r   r   r5   r5   r6   _drop  s   



zPolyElement._dropc                 C  s   |  |\}}| jjdkr| jr| dS td| |j}|  D ]\}}|| dkr:t|}||= ||t	|< q"td| |S )Nr   zCannot drop %sr   )
r?  r7   r   	is_groundr   r   r   rM   r@   ry   )r   r   r   r7   r   r!  r"  Kr5   r5   r6   r     s   
zPolyElement.dropc                 C  s6   | j }||}t|j}||= ||j||| dfS )Nr  )r7   r   r@   r   r   r>  r5   r5   r6   _drop_to_ground  s
   

zPolyElement._drop_to_groundc                 C  s   | j jdkr
td| |\}}|j}|jjd }|  D ]1\}}|d | ||d d   }||vr@|||  |||< q||  |||  |7  < q|S )Nr   z$Cannot drop only generator to groundr   )	r7   r   r   rB  r   r2   r0   r  
mul_ground)r   r   r   r7   r   r   r   monr5   r5   r6   r    s    zPolyElement.drop_to_groundc                 C  s   t | | jjd | jjS r   )r   r7   r   r2   r   r5   r5   r6   to_dense      zPolyElement.to_densec                 C  r   r^   )rQ   r   r5   r5   r6   to_dict#  r   zPolyElement.to_dictc                 C  s  | s
| | jjjS |d }|d }| j}|j}|j}	|j}
g }|  D ]\}}|j|}|r2dnd}|	| ||
krP| |}|rO|
drO|dd  }n|rU| }|| jjjkre|j||dd}nd	}g }t|	D ]=}|| }|svqm|j|| |dd}|dkr|t|ks|d
k r|j||dd}n|}|	|||f  qm|	d|  qm|r|g| }|	|| q$|d
 dv r|d
}|dkr|d
d d	|S )NMulAtom -  + -r   T)strict r   Fz%s)rK  rJ  )_printr7   r2   r   r   r   r   r  is_negativer   r   r   parenthesizer   r   joinpopinsert)r   printer
precedenceexp_pattern
mul_symbolprec_mul	prec_atomr7   r   r   zmsexpvsr   r   negativesignscoeffsexpvr   r   r   sexpheadr5   r5   r6   r`   &  sV   




zPolyElement.strc                 C  s   | | j jv S r^   )r7   r   r   r5   r5   r6   is_generatorV  r   zPolyElement.is_generatorc                 C  s   |  pt | dko| jj| v S r   )rz   r7   r   r   r5   r5   r6   r@  Z  s   zPolyElement.is_groundc                 C  s   |  pt | dko| jdkS r   )rz   LCr   r5   r5   r6   is_monomial^  s   zPolyElement.is_monomialc                 C  s   t | dkS r   )rz   r   r5   r5   r6   is_termb  r   zPolyElement.is_termc                 C     | j j| jS r^   )r7   r2   rP  rd  r   r5   r5   r6   rP  f     zPolyElement.is_negativec                 C  rg  r^   )r7   r2   is_positiverd  r   r5   r5   r6   ri  j  rh  zPolyElement.is_positivec                 C  rg  r^   )r7   r2   is_nonnegativerd  r   r5   r5   r6   rj  n  rh  zPolyElement.is_nonnegativec                 C  rg  r^   )r7   r2   is_nonpositiverd  r   r5   r5   r6   rk  r  rh  zPolyElement.is_nonpositivec                 C  s   |  S r^   r5   ru   r5   r5   r6   is_zerov  s   zPolyElement.is_zeroc                 C  s   | | j jkS r^   )r7   r   ru   r5   r5   r6   is_onez  r   zPolyElement.is_onec                 C  rg  r^   )r7   r2   rm  rd  ru   r5   r5   r6   is_monic~  rh  zPolyElement.is_monicc                 C  s   | j j|  S r^   )r7   r2   rm  contentru   r5   r5   r6   is_primitive  s   zPolyElement.is_primitivec                 C     t dd |  D S )Nc                 s      | ]	}t |d kV  qdS r   NrP   r:   r   r5   r5   r6   rc     r
  z(PolyElement.is_linear.<locals>.<genexpr>rf   
itermonomsru   r5   r5   r6   	is_linear     zPolyElement.is_linearc                 C  rq  )Nc                 s  rr  )   Nrt  ru  r5   r5   r6   rc     r
  z+PolyElement.is_quadratic.<locals>.<genexpr>rv  ru   r5   r5   r6   is_quadratic  ry  zPolyElement.is_quadraticc                 C     | j jsdS | j | S NT)r7   r   	dmp_sqf_pru   r5   r5   r6   is_squarefree     zPolyElement.is_squarefreec                 C  r|  r}  )r7   r   dmp_irreducible_pru   r5   r5   r6   is_irreducible  r  zPolyElement.is_irreduciblec                 C     | j jr
| j | S td)Nzcyclotomic polynomial)r7   r   dup_cyclotomic_pr"   ru   r5   r5   r6   is_cyclotomic  s   zPolyElement.is_cyclotomicc                 C  s   |  dd |  D S )Nc                 S  s   g | ]	\}}|| fqS r5   r5   r  r5   r5   r6   r<     rL   z'PolyElement.__neg__.<locals>.<listcomp>)r  r  r   r5   r5   r6   __neg__  rF  zPolyElement.__neg__c                 C  s   | S r^   r5   r   r5   r5   r6   __pos__     zPolyElement.__pos__c           
      C  s<  |s|   S | j}t||jr6|   }|j}|jj}| D ]\}}|||| }|r0|||< q||= q|S t|tr^t|jt	rI|jj|jkrInt|jjt	r\|jjj|kr\|
| S tS z||}W n typ   t Y S w |   }|sy|S |j}	|	|  vr|||	< |S |||	  kr||	= |S ||	  |7  < |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

        )r   r7   r_   r   r   r2   r   rM   rj   r   __radd__r5  r   r   r   r0  )
r.  r/  r7   r   r   r   r!  r"  cp2r[  r5   r5   r6   __add__  sH   


zPolyElement.__add__c                 C  s   |   }|s|S | j}z||}W n ty   t Y S w |j}||  vr-|||< |S |||  kr9||= |S ||  |7  < |S r^   )r   r7   r   r   r5  r   r0  )r.  r  r   r7   r[  r5   r5   r6   r    s$   zPolyElement.__radd__c           	      C  s4  |s|   S | j}t||jr6|   }|j}|jj}| D ]\}}|||| }|r0|||< q||= q|S t|tr^t|jt	rI|jj|jkrInt|jjt	r\|jjj|kr\|
| S tS z||}W n typ   t Y S w |   }|j}||  vr| ||< |S ||| kr||= |S ||  |8  < |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

        )r   r7   r_   r   r   r2   r   rM   rj   r   __rsub__r5  r   r   r   r0  )	r.  r/  r7   r   r   r   r!  r"  r[  r5   r5   r6   __sub__  sD   



zPolyElement.__sub__c                 C  sV   | j }z||}W n ty   t Y S w |j}| D ]	}| |  ||< q||7 }|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

        )r7   r   r   r5  r   )r.  r  r7   r   r   r5   r5   r6   r  '  s   zPolyElement.__rsub__c                 C  s0  | j }|j}| r
|s|S t||jrH|j}|jj}|j}t| }|  D ]\}}	|D ]\}
}|||
}||||	|  ||< q,q&|	  |S t|t
rpt|jtr[|jj |j kr[nt|j jtrn|j jj |krn|| S tS z||}W n ty   t Y S w |  D ]\}}	|	| }|r|||< q|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

        )r7   r   r_   r   r   r2   r   r@   rM   r,  rj   r   __rmul__r5  r   r   )r.  r/  r7   r   r   r   r   p2itexp1v1exp2v2r   r"  r5   r5   r6   __mul__B  sB   


zPolyElement.__mul__c                 C  sb   | j j}|s|S z|j |}W n ty   t Y S w |  D ]\}}|| }|r.|||< q |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

        )r7   r   r   r   r5  rM   )r.  r/  r   r  r  r"  r5   r5   r6   r  t  s   zPolyElement.__rmul__c                 C  s   | j }|s| r
|jS tdt| dkr=t|  d \}}|j}||jjkr1|||||< |S || ||||< |S t	|}|dk rItd|dkrQ| 
 S |dkrY|  S |dkrc| |   S t| dkrn| |S | |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 exponentrz        )r7   r   r   rz   r@   rM   r   r2   r   r   r   square_pow_multinomial_pow_generic)r   r  r7   r   r   r   r5   r5   r6   __pow__  s2   

zPolyElement.__pow__c                 C  sB   | j j}| }	 |d@ r|| }|d8 }|s	 |S | }|d }q)NTr   rz  )r7   r   r  )r   r  r   rH   r5   r5   r6   r    s   zPolyElement._pow_genericc                 C  s   t t| | }| jj}| jj}|  }| jjj}| jj}|D ]>\}}	|}
|	}t||D ]\}\}}|rA||
||}
||| 9 }q-t	|
}|}|
||| }|rW|||< q ||v r^||= q |S r^   )r   rz   rM   r7   r   r   r2   r   rR   ry   r   )r   r  multinomialsr   r   r  r   r   multinomialmultinomial_coeffproduct_monomproduct_coeffr   r   r   r5   r5   r6   r    s.   

zPolyElement._pow_multinomialc                 C  s   | j }|j}|j}t|  }|jj}|j}tt|D ]'}|| }| | }	t|D ]}
||
 }|||}||||	| |   ||< q*q|	d}|j}| 
 D ]\}}|||}||||d  ||< qP|  |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

        rz  )r7   r   r   r@   r0  r2   r   r   rz   imul_numrM   r,  )r   r7   r   r   r0  r   r   r   k1pkjk2r   r!  r"  r5   r5   r6   r    s*   


zPolyElement.squarec                 C  s   | j }|s	tdt||jr| |S t|tr<t|jtr'|jj |j kr'nt|j jtr:|j jj |kr:|| S t	S z|
|}W n tyN   t	 Y S w | || |fS Npolynomial division)r7   ZeroDivisionErrorr_   r   r   rj   r2   r   __rdivmod__r5  r   r   
quo_ground
rem_groundr.  r/  r7   r5   r5   r6   
__divmod__  s"   


zPolyElement.__divmod__c                 C     t S r^   r5  r-  r5   r5   r6   r  (  r  zPolyElement.__rdivmod__c                 C  s   | j }|s	tdt||jr| |S t|tr<t|jtr'|jj |j kr'nt|j jtr:|j jj |kr:|| S t	S z|
|}W n tyN   t	 Y S w | |S r  )r7   r  r_   r   remrj   r2   r   __rmod__r5  r   r   r  r  r5   r5   r6   __mod__+  s"   



zPolyElement.__mod__c                 C  r  r^   r  r-  r5   r5   r6   r  A  r  zPolyElement.__rmod__c                 C  s   | j }|s	tdt||jr|jr| |d  S | |S t|trEt|jtr0|jj |j kr0nt|j jtrC|j jj |krC|	| S t
S z||}W n tyW   t
 Y S w | |S )Nr  r   )r7   r  r_   r   re  quorj   r2   r   __rtruediv__r5  r   r   r  r  r5   r5   r6   __truediv__D  s&   



zPolyElement.__truediv__c                 C  r  r^   r  r-  r5   r5   r6   r  ]  r  zPolyElement.__rtruediv__c                   sL   | j j| j j}|j | j j|jr fdd}|S  fdd}|S )Nc                   sB   | \}}|\}}|kr|}n||}|d ur| ||fS d S r^   r5   	a_lm_a_lc	b_lm_b_lca_lma_lcb_lmb_lcr   
domain_quor   r[  r5   r6   term_divl  s   
z'PolyElement._term_div.<locals>.term_divc                   sJ   | \}}|\}}|kr|}n||}|d u s#|| s#| ||fS d S r^   r5   r  r  r5   r6   r  x  s   
)r7   r   r2   r  r   r%  )r   r2   r  r5   r  r6   	_term_dive  s   zPolyElement._term_divc                   s  | j  d}t|trd}|g}t|std| s&|r! j jfS g  jfS |D ]}|j  kr3tdq(t|} fddt|D }| 	 } j}| 
 }dd |D }	|rd}
d}|
|k r|dkr| }|||| f|	|
 ||
 |	|
  f}|d	ur|\}}||
 ||f||
< |||
 || f}d
}n|
d
7 }
|
|k r|dksc|s| }|||| f}||= |sW| jkr||7 }|r|s͈ j|fS |d |fS ||f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

        FTr  z"self and f must have the same ringc                   s   g | ]} j qS r5   )r   r  r7   r5   r6   r<     r=   z#PolyElement.div.<locals>.<listcomp>c                 S  s   g | ]}|  qS r5   )r   )r:   fxr5   r5   r6   r<         r   Nr   )r7   r_   rj   rf   r  r   r   rz   r   r   r  r   _iadd_monom_iadd_poly_monomr   )r   fv
ret_singlerv   rb   qvr   rr  expvsr   divoccurredr   termexpv1rH   r5   r  r6   r     s\   


&


zPolyElement.divc                 C  s>  | }t |tr
|g}t|std|j}|j}|j}|j}|j}| }|j	}	|
 }|j}
|r|D ]A}||	|j	}|d urt|\}}| D ]\}}|||}|
||||  }|s_||= qG|||< qG| }|d urr||| f}	 n'q3|	\}}||v r||  |7  < n|||< ||= | }|d ur||| f}	|s1|S r  )r_   rj   rf   r  r7   r2   r   r   r  LTr   r   r  r   )r   Grv   r7   r2   r   r   r  r  ltfr   gtqrG   rH   mgcgm1c1ltmltcr5   r5   r6   r    sP   


zPolyElement.remc                 C     |  |d S Nr   )r   )rv   r  r5   r5   r6   r    r   zPolyElement.quoc                 C  s    |  |\}}|s|S t| |r^   )r   r!   )rv   r  qr  r5   r5   r6   exquo  s   
zPolyElement.exquoc                 C  sb   | | j jv r|  }n| }|\}}||}|du r |||< |S ||7 }|r,|||< |S ||= |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)r7   r   r   r   )r   mccpselfr   r   rH   r5   r5   r6   r  
  s   

zPolyElement._iadd_monomc                 C  s~   | }||j jv r| }|\}}|j}|j jj}|j j}| D ]\}	}
||	|}||||
|  }|r9|||< 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

        )r7   r   r   r   r2   r   r   rM   )r   r/  r  r.  rG   rH   r   r   r   r!  r"  kar   r5   r5   r6   r  6  s   


zPolyElement._iadd_poly_monomc                   :   | j | | s
tS  dk rdS t fdd|  D S )z
        The leading degree in ``x`` or the main variable.

        Note that the degree of 0 is negative infinity (``float('-inf')``)

        r   c                 3      | ]}|  V  qd S r^   r5   ru  r   r5   r6   rc   i      z%PolyElement.degree.<locals>.<genexpr>)r7   r   r   rt   rw  rv   xr5   r  r6   degree[     zPolyElement.degreec                 C  ,   | s	t f| jj S ttttt|   S )z
        A tuple containing leading degrees in all variables.

        Note that the degree of 0 is negative infinity (``float('-inf')``)

        )	r   r7   r   ry   rO   rt   r@   rR   rw  ru   r5   r5   r6   degreesk     zPolyElement.degreesc                   r  )z
        The tail degree in ``x`` or the main variable.

        Note that the degree of 0 is negative infinity (``float('-inf')``)

        r   c                 3  r  r^   r5   ru  r  r5   r6   rc     r  z*PolyElement.tail_degree.<locals>.<genexpr>)r7   r   r   minrw  r  r5   r  r6   tail_degreew  r  zPolyElement.tail_degreec                 C  r  )z
        A tuple containing tail degrees in all variables.

        Note that the degree of 0 is negative infinity (``float('-inf')``)

        )	r   r7   r   ry   rO   r  r@   rR   rw  ru   r5   r5   r6   tail_degrees  r  zPolyElement.tail_degreesc                 C  s   | r| j | 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)r7   r   r   r5   r5   r6   r     s   zPolyElement.leading_expvc                 C  s   |  || jjjS r^   )r   r7   r2   r   r   r   r5   r5   r6   
_get_coeff  r  zPolyElement._get_coeffc                 C  sl   |dkr|  | jjS t|| jjr0t| }t|dkr0|d \}}|| jjj	kr0|  |S t
d| )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 %s)r  r7   r   r_   r   r@   r  rz   r2   r   r   )r   r   r  r   r   r5   r5   r6   r     s   
zPolyElement.coeffc                 C  s   |  | jjS )z"Returns the constant coefficient. )r  r7   r   r   r5   r5   r6   r2    r   zPolyElement.constc                 C  s   |  |  S r^   )r  r   r   r5   r5   r6   rd    r   zPolyElement.LCc                 C  s   |   }|d u r| jjS |S r^   )r   r7   r   r  r5   r5   r6   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

        )r7   r   r   r2   r   r   r   r   r5   r5   r6   leading_monom  s
   zPolyElement.leading_monomc                 C  s0   |   }|d u r| jj| jjjfS || |fS r^   )r   r7   r   r2   r   r  r  r5   r5   r6   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)r7   r   r   r  r5   r5   r6   leading_term  s
   zPolyElement.leading_termc                   sL    d u r	| j j nt   tu rt|dd ddS t| fddddS )Nc                 S  s   | d S r  r5   r   r5   r5   r6   rp     s    z%PolyElement._sorted.<locals>.<lambda>T)rs   reversec                   s    | d S r  r5   r  rw   r5   r6   rp     rx   )r7   r3   r}   r|   r   sorted)r   r\   r3   r5   rw   r6   _sorted  s   

zPolyElement._sortedc                 C     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 r5   r5   )r:   _r   r5   r5   r6   r<   3  r  z&PolyElement.coeffs.<locals>.<listcomp>r  r   r3   r5   r5   r6   rX        zPolyElement.coeffsc                 C  r  )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 r5   r5   )r:   r   r  r5   r5   r6   r<   M  r  z&PolyElement.monoms.<locals>.<listcomp>r  r  r5   r5   r6   monoms5  r  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@   rM   r  r5   r5   r6   r  O  r  zPolyElement.termsc                 C     t |  S )z,Iterator over coefficients of a polynomial. )iterrA   r   r5   r5   r6   
itercoeffsi  r   zPolyElement.itercoeffsc                 C  r  )z)Iterator over monomials of a polynomial. )r  r0  r   r5   r5   r6   rw  m  r   zPolyElement.itermonomsc                 C  r  )z%Iterator over terms of a polynomial. )r  rM   r   r5   r5   r6   r  q  r   zPolyElement.itertermsc                 C  r  )z+Unordered list of polynomial coefficients. r?   r   r5   r5   r6   
listcoeffsu  r   zPolyElement.listcoeffsc                 C  r  )z(Unordered list of polynomial monomials. )r@   r0  r   r5   r5   r6   
listmonomsy  r   zPolyElement.listmonomsc                 C  r  )z$Unordered list of polynomial terms. r+  r   r5   r5   r6   	listterms}  r   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)r7   r   clear)r   rH   r   r5   r5   r6   r    s   zPolyElement.imul_numc                 C  s0   | j j}|j}|j}|  D ]}|||}q|S )z*Returns GCD of polynomial's coefficients. )r7   r2   r   r   r  )rv   r2   contr   r   r5   r5   r6   ro    s   zPolyElement.contentc                 C  s   |   }|| |fS )z,Returns content and a primitive polynomial. )ro  r  )rv   r  r5   r5   r6   	primitive  s   zPolyElement.primitivec                 C  s   | s| S |  | jS )z5Divides all coefficients by the leading coefficient. )r  rd  ru   r5   r5   r6   monic  s   zPolyElement.monicc                   s,    s| j jS  fdd|  D }| |S )Nc                   r   r5   r5   r  r  r5   r6   r<     r   z*PolyElement.mul_ground.<locals>.<listcomp>)r7   r   r  r  )rv   r  r  r5   r  r6   rC    s   
zPolyElement.mul_groundc                   s*   | j j fdd|  D }| |S )Nc                   s   g | ]\}}| |fqS r5   r5   r:   f_monomf_coeffr   r   r5   r6   r<         z)PolyElement.mul_monom.<locals>.<listcomp>)r7   r   rM   r  )rv   r   r  r5   r  r6   	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 r5   r5   r  r   r   r   r5   r6   r<     rN   z(PolyElement.mul_term.<locals>.<listcomp>)r7   r   r   rC  r   rM   r  )rv   r  r  r5   r  r6   mul_term  s   

zPolyElement.mul_termc                   sl   | j j}s
td| r|jkr| S |jr&|j  fdd|  D }nfdd|  D }| |S )Nr  c                   s   g | ]\}}| |fqS r5   r5   r  r  r  r5   r6   r<     r  z*PolyElement.quo_ground.<locals>.<listcomp>c                   s$   g | ]\}}|  s||  fqS r5   r5   r  r  r5   r6   r<        $ )r7   r2   r  r   r%  r  r  r  )rv   r  r2   r  r5   r  r6   r    s   
zPolyElement.quo_groundc                   sj    \}}|s
t d| s| jjS || jjkr| |S |   fdd|  D }| dd |D S )Nr  c                   s   g | ]}| qS r5   r5   r:   tr  r  r5   r6   r<     s    z(PolyElement.quo_term.<locals>.<listcomp>c                 S  s   g | ]}|d ur|qS r^   r5   r  r5   r5   r6   r<     rD   )r  r7   r   r   r  r  r  r  )rv   r  r   r   r  r5   r  r6   quo_term  s   
zPolyElement.quo_termc                   sx   | j jjr&g }|  D ]\}}|  }| d kr|  }|||f qn fdd|  D }| |}|  |S )Nrz  c                   s   g | ]
\}}||  fqS r5   r5   r  r   r5   r6   r<     r   z,PolyElement.trunc_ground.<locals>.<listcomp>)r7   r2   is_ZZr  r   r  r,  )rv   r   r  r   r   r   r5   r  r6   trunc_ground  s   

zPolyElement.trunc_groundc                 C  sB   | }|  }|  }|jj||}||}||}|||fS r^   )ro  r7   r2   r   r  )r   r  rv   fcgcr   r5   r5   r6   extract_ground  s   


zPolyElement.extract_groundc                   s2   | s| j jjS | j jj | fdd|  D S )Nc                   s   g | ]} |qS r5   r5   )r:   r   
ground_absr5   r6   r<     r  z%PolyElement._norm.<locals>.<listcomp>)r7   r2   r   absr  )rv   	norm_funcr5   r  r6   _norm  s   

zPolyElement._normc                 C  
   |  tS r^   )r   rt   ru   r5   r5   r6   max_norm  r   zPolyElement.max_normc                 C  r!  r^   )r   rP   ru   r5   r5   r6   l1_norm  r   zPolyElement.l1_normc                 G  s   | j }| gt| }dg|j }|D ]}| D ]}t|D ]\}}t|| |||< qqqt|D ]
\}}	|	s<d||< q2t|}tdd |D rN||fS g }
|D ]#}|j}|	 D ]\}}dd t
||D }||t|< q[|
| qR||
fS )Nr   r   c                 s  s    | ]}|d kV  qdS rs  r5   )r:   ro   r5   r5   r6   rc   0  r  z&PolyElement.deflate.<locals>.<genexpr>c                 S  s   g | ]\}}|| qS r5   r5   r:   r   r  r5   r5   r6   r<   9  rD   z'PolyElement.deflate.<locals>.<listcomp>)r7   r@   r   rw  r   r   ry   rf   r   r  rR   r   )rv   r  r7   rZ   Jr   r   r   rG   ro   HhIr   Nr5   r5   r6   deflate  s0   zPolyElement.deflatec                 C  s>   | j j}|  D ]\}}dd t||D }||t|< q|S )Nc                 S  s   g | ]\}}|| qS r5   r5   r$  r5   r5   r6   r<   D  rD   z'PolyElement.inflate.<locals>.<listcomp>)r7   r   r  rR   ry   )rv   r%  r   r(  r   r)  r5   r5   r6   inflate@  s
   zPolyElement.inflatec                 C  sb   | }|j j}|js| \}}| \}}|||}|| ||}|js-||S | S r^   )	r7   r2   r%  r  r   r  r   rC  r  )r   r  rv   r2   r  r  rH   r'  r5   r5   r6   r   I  s   
zPolyElement.lcmc                 C  r  r  )	cofactorsrv   r  r5   r5   r6   r   Y  r   zPolyElement.gcdc                 C  s   | s|s| j j}|||fS | s| |\}}}|||fS |s+|| \}}}|||fS t| dkr>| |\}}}|||fS t|dkrQ|| \}}}|||fS | |\}\} }| |\}}}||||||fS r   )r7   r   	_gcd_zerorz   
_gcd_monomr*  _gcdr+  )rv   r  r   r'  cffcfgr%  r5   r5   r6   r,  \  s$   




zPolyElement.cofactorsc                 C  s0   | j j| j j}}|jr|||fS | || fS r^   )r7   r   r   rj  )rv   r  r   r   r5   r5   r6   r.  r  s   
zPolyElement._gcd_zeroc                   s   | j }|jj}|jj|j}|jt|  d \}}|| | D ]\}}||| | q$|  fg}	| || fg}
|  fdd| D }|	|
|fS )Nr   c                   s$   g | ]\}}|| fqS r5   r5   )r:   r  r  _cgcd_mgcd
ground_quor   r5   r6   r<     r  z*PolyElement._gcd_monom.<locals>.<listcomp>)	r7   r2   r   r  r   r   r@   r  r  )rv   r  r7   
ground_gcdr   mfcfr  r  r'  r1  r2  r5   r3  r6   r/  y  s   

"
zPolyElement._gcd_monomc                 C  s6   | j }|jjr| |S |jjr| |S || |S r^   )r7   r2   is_QQ_gcd_QQr  _gcd_ZZdmp_inner_gcd)rv   r  r7   r5   r5   r6   r0    s   

zPolyElement._gcdc                 C  s
   t | |S r^   r   r-  r5   r5   r6   r<    r   zPolyElement._gcd_ZZc                 C  s   | }|j }|j|j d}| \}}| \}}||}||}||\}}}	||}|j| }
}||	|j
|
|}|	|	|j
|
|}	|||	fS )Nr   )r7   r   r2   r'  r*  r  r<  rd  r  rC  r  )r   r  rv   r7   r  r9  r  r'  r1  r2  rH   r5   r5   r6   r;    s   



zPolyElement._gcd_QQc                 C  s&  | }|j }|s||jfS |j}|jr|js||\}}}nD|j| d}| \}	}| \}
}|	|}|	|}||\}}}|j|
|	\}}
}	|	|}|	|}|
|
}|
|	}| }||jkrt||}}||fS ||j kr| | }}||fS |
|}|
|}||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   )r7   r   r2   r%  r&  r,  r   r'  r*  r  rC  canonical_unit)r   r  rv   r7   r2   r  r   r  r  cqcpur5   r5   r6   cancel  s8   










zPolyElement.cancelc                 C  s   | j j}|| jS r^   )r7   r2   r>  rd  )rv   r2   r5   r5   r6   r>    s   zPolyElement.canonical_unitc           	      C  s`   | j }||}||}|j}|  D ]\}}|| 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

        )r7   r   r   r   r  r   r   )	rv   r  r7   r   rG   r  r   r   er5   r5   r6   diff  s   

zPolyElement.diffc                 G  sP   dt |  k r| jjkrn n| tt| jj|S td| jjt |f )Nr   z1expected at least 1 and at most %s values, got %s)rz   r7   r   evaluater@   rR   r0   r   )rv   rA   r5   r5   r6   r   	  s    zPolyElement.__call__c                   s@  | }t |tr0|d u r0|d |dd  \ }}| |}|s"|S  fdd|D }||S |j}||}|j|}|jdkr[|jj}|	 D ]\\}}||||  7 }qK|S |
|j}	|	 D ]8\}
}|
| |
d | |
|d d   }}
|||  }|
|	v r||	|
  }|r||	|
< qe|	|
= qe|r||	|
< qe|	S )Nr   r   c                   s   g | ]\}}|  |fqS r5   )r   )r:   Yrn   Xr5   r6   r<   	  r  z(PolyElement.evaluate.<locals>.<listcomp>)r_   r@   rE  r7   r   r2   r   r   r   r  r   )r   r  rn   rv   r7   r   resultr  r   r   r   r5   rG  r6   rE  	  s:   


&
zPolyElement.evaluatec                 C  s  | }t |tr|d u r|D ]
\}}|||}q|S |j}||}|j|}|jdkrH|jj}|	 D ]\\}}	||	||  7 }q5|
|S |j}
|	 D ]:\}}	|| |d | d ||d d   }}|	||  }	||
v r|	|
|  }	|	r|	|
|< qO|
|= qO|	r|	|
|< qO|
S )Nr   rk   )r_   r@   subsr7   r   r2   r   r   r   r  r   )r   r  rn   rv   rH  r7   r   rI  r  r   r   r   r5   r5   r6   rJ  2	  s4   


*
zPolyElement.subsc                   s  |   }|jj}|s|jg fS fddt|D i fdd}tt|d }tt|dd}j}|rd\}}}	t| D ])\}
\ }t fd	d
|D rot	dd
 t
| D }||kro| |}}}	qF|dkrz||	 }nn;g }t
  dd d D ]\}}|||  q|t||7 }|}t|D ]\}
}|||
|9 }q||8 }|s;tt
j}|||fS )aX  
        Rewrite *self* in terms of elementary symmetric polynomials.

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

        If this :py:class:`~.PolyElement` belongs to a ring of $n$ variables,
        we can try to write it as a function of the elementary symmetric
        polynomials on $n$ variables. We compute a symmetric part, and a
        remainder for any part we were not able to symmetrize.

        Examples
        ========

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

        >>> f = x**2 + y**2
        >>> f.symmetrize()
        (x**2 - 2*y, 0, [(x, x + y), (y, x*y)])

        >>> f = x**2 - y**2
        >>> f.symmetrize()
        (x**2 - 2*y, -2*y**2, [(x, x + y), (y, x*y)])

        Returns
        =======

        Triple ``(p, r, m)``
            ``p`` is a :py:class:`~.PolyElement` that represents our attempt
            to express *self* as a function of elementary symmetric
            polynomials. Each variable in ``p`` stands for one of the
            elementary symmetric polynomials. The correspondence is given
            by ``m``.

            ``r`` is the remainder.

            ``m`` is a list of pairs, giving the mapping from variables in
            ``p`` to elementary symmetric polynomials.

            The triple satisfies the equation ``p.compose(m) + r == self``.
            If the remainder ``r`` is zero, *self* is symmetric. If it is
            nonzero, we were not able to represent *self* as symmetric.

        See Also
        ========

        sympy.polys.polyfuncs.symmetrize

        References
        ==========

        .. [1] Lauer, E. Algorithms for symmetrical polynomials, Proc. 1976
            ACM Symp. on Symbolic and Algebraic Computing, NY 242-247.
            https://dl.acm.org/doi/pdf/10.1145/800205.806342

        c                   s   g | ]	}  |d  qS )r   )r  r  r  r5   r6   r<   	  rL   z*PolyElement.symmetrize.<locals>.<listcomp>c                   s,   | |f vr|  |  | |f<  | |f S r^   r5   )r   r  )poly_powersrZ   r5   r6   get_poly_power	  s   z.PolyElement.symmetrize.<locals>.get_poly_powerr   r   r   )r   NNc                 3  s$    | ]} |  |d   kV  qdS rs  r5   r  r  r5   r6   rc   	  s   " z)PolyElement.symmetrize.<locals>.<genexpr>c                 s  s    | ]	\}}|| V  qd S r^   r5   )r:   r  rG   r5   r5   r6   rc   	  r
  Nrk   )r   r7   r   r   r   r@   r   r  rf   rt   rR   r   r   ry   r0   )r   rv   r  rL  r   weights	symmetric_height_monom_coeffr   r   height	exponentsr  m2productr   r5   )r   rK  rZ   r7   r6   
symmetrizeY	  sB   ;

zPolyElement.symmetrizec                   s  | j }|j}tt|jt|j |d ur||fg}n t|tr%t|}nt|tr7t	|
  fddd}ntdt|D ]\}\}} | ||f||< q?|  D ]0\}}	t|}|j}
|D ]\}}|| d}||< |rw|
|| 9 }
qb|
t||	f}
||
7 }qU|S )Nc                   s    | d  S r  r5   )r!  gens_mapr5   r6   rp   	  rx   z%PolyElement.compose.<locals>.<lambda>rr   z9expected a generator, value pair a sequence of such pairsr   )r7   r   rQ   rR   r0   r   r   r_   r@   r  rM   r   r   r   r  r   r  ry   )rv   r  rn   r7   r   replacementsr!  r  r   r   subpolyr   r  r5   rW  r6   r  	  s.   



zPolyElement.composec                   sh   | }|j | fdd| D }|s|j jS t| \}}fdd|D }|j tt||S )aU  
        Coefficient of ``self`` with respect to ``x**deg``.

        Treating ``self`` as a univariate polynomial in ``x`` this finds the
        coefficient of ``x**deg`` as a polynomial in the other generators.

        Parameters
        ==========

        x : generator or generator index
            The generator or generator index to compute the expression for.
        deg : int
            The degree of the monomial to compute the expression for.

        Returns
        =======

        :py:class:`~.PolyElement`
            The coefficient of ``x**deg`` as a polynomial in the same ring.

        Examples
        ========

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

        >>> p = 2*x**4 + 3*y**4 + 10*z**2 + 10*x*z**2
        >>> deg = 2
        >>> p.coeff_wrt(2, deg) # Using the generator index
        10*x + 10
        >>> p.coeff_wrt(z, deg) # Using the generator
        10*x + 10
        >>> p.coeff(z**2) # shows the difference between coeff and coeff_wrt
        10

        See Also
        ========

        coeff, coeffs

        c                   s$   g | ]\}}|  kr||fqS r5   r5   rF   degr   r5   r6   r<   
  r  z)PolyElement.coeff_wrt.<locals>.<listcomp>c                   s,   g | ]}|d   d | d d   qS )Nrk   r   r5   )r:   rG   r  r5   r6   r<   
  s   , )r7   r   r  r   rR   rS   rQ   )r   r  r\  r   r  r  rX   r5   r[  r6   	coeff_wrt	  s   *zPolyElement.coeff_wrtc                 C  s   | }|j |}||}||}|dk rtd||}}||k r%|S || d }|||}	|j j| }
	 |||}|| |d }}||	 }|| |
|  }|| }||}||k ranq8|	| }|| S )a  
        Pseudo-remainder of the polynomial ``self`` with respect to ``g``.

        The pseudo-quotient ``q`` and pseudo-remainder ``r`` with respect to
        ``z`` when dividing ``f`` by ``g`` satisfy ``m*f = g*q + r``,
        where ``deg(r,z) < deg(g,z)`` and
        ``m = LC(g,z)**(deg(f,z) - deg(g,z)+1)``.

        See :meth:`pdiv` for explanation of pseudo-division.


        Parameters
        ==========

        g : :py:class:`~.PolyElement`
            The polynomial to divide ``self`` by.
        x : generator or generator index, optional
            The main variable of the polynomials and default is first generator.

        Returns
        =======

        :py:class:`~.PolyElement`
            The pseudo-remainder polynomial.

        Raises
        ======

        ZeroDivisionError : If ``g`` is the zero polynomial.

        Examples
        ========

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

        >>> f = x**2 + x*y
        >>> g = 2*x + 2
        >>> f.prem(g) # first generator is chosen by default if it is not given
        -4*y + 4
        >>> f.rem(g) # shows the differnce between prem and rem
        x**2 + x*y
        >>> f.prem(g, y) # generator is given
        0
        >>> f.prem(g, 1) # generator index is given
        0

        See Also
        ========

        pdiv, pquo, pexquo, sympy.polys.domains.ring.Ring.rem

        r   r  r   r7   r   r  r  r]  r0   )r   r  r  rv   dfdgr  drr)  lc_gxplc_rr  Rr  rH   r5   r5   r6   prem
  s0   6



zPolyElement.premc                 C  s  | }|j |}||}||}|dk rtd|||}}}||k r*||fS || d }	|||}
|j j| }	 |||}|| |	d }}	||
 }||||   }||
 }|| ||  }|| }||}||k rrnq=|
|	 }|| }|| }||fS )a|  
        Computes the pseudo-division of the polynomial ``self`` with respect to ``g``.

        The pseudo-division algorithm is used to find the pseudo-quotient ``q``
        and pseudo-remainder ``r`` such that ``m*f = g*q + r``, where ``m``
        represents the multiplier and ``f`` is the dividend polynomial.

        The pseudo-quotient ``q`` and pseudo-remainder ``r`` are polynomials in
        the variable ``x``, with the degree of ``r`` with respect to ``x``
        being strictly less than the degree of ``g`` with respect to ``x``.

        The multiplier ``m`` is defined as
        ``LC(g, x) ^ (deg(f, x) - deg(g, x) + 1)``,
        where ``LC(g, x)`` represents the leading coefficient of ``g``.

        It is important to note that in the context of the ``prem`` method,
        multivariate polynomials in a ring, such as ``R[x,y,z]``, are treated
        as univariate polynomials with coefficients that are polynomials,
        such as ``R[x,y][z]``. When dividing ``f`` by ``g`` with respect to the
        variable ``z``, the pseudo-quotient ``q`` and pseudo-remainder ``r``
        satisfy ``m*f = g*q + r``, where ``deg(r, z) < deg(g, z)``
        and ``m = LC(g, z)^(deg(f, z) - deg(g, z) + 1)``.

        In this function, the pseudo-remainder ``r`` can be obtained using the
        ``prem`` method, the pseudo-quotient ``q`` can
        be obtained using the ``pquo`` method, and
        the function ``pdiv`` itself returns a tuple ``(q, r)``.


        Parameters
        ==========

        g : :py:class:`~.PolyElement`
            The polynomial to divide ``self`` by.
        x : generator or generator index, optional
            The main variable of the polynomials and default is first generator.

        Returns
        =======

        :py:class:`~.PolyElement`
            The pseudo-division polynomial (tuple of ``q`` and ``r``).

        Raises
        ======

        ZeroDivisionError : If ``g`` is the zero polynomial.

        Examples
        ========

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

        >>> f = x**2 + x*y
        >>> g = 2*x + 2
        >>> f.pdiv(g) # first generator is chosen by default if it is not given
        (2*x + 2*y - 2, -4*y + 4)
        >>> f.div(g) # shows the difference between pdiv and div
        (0, x**2 + x*y)
        >>> f.pdiv(g, y) # generator is given
        (2*x**3 + 2*x**2*y + 6*x**2 + 2*x*y + 8*x + 4, 0)
        >>> f.pdiv(g, 1) # generator index is given
        (2*x**3 + 2*x**2*y + 6*x**2 + 2*x*y + 8*x + 4, 0)

        See Also
        ========

        prem
            Computes only the pseudo-remainder more efficiently than
            `f.pdiv(g)[1]`.
        pquo
            Returns only the pseudo-quotient.
        pexquo
            Returns only an exact pseudo-quotient having no remainder.
        div
            Returns quotient and remainder of f and g polynomials.

        r   r  r   r^  )r   r  r  rv   r_  r`  r  r  ra  r)  rb  rc  rd  r  Qre  r  rH   r5   r5   r6   pdivv
  s8   P


zPolyElement.pdivc                 C  s   | }| ||d S )aW  
        Polynomial pseudo-quotient in multivariate polynomial ring.

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

        >>> f = x**2 + x*y
        >>> g = 2*x + 2*y
        >>> h = 2*x + 2
        >>> f.pquo(g)
        2*x
        >>> f.quo(g) # shows the difference between pquo and quo
        0
        >>> f.pquo(h)
        2*x + 2*y - 2
        >>> f.quo(h) # shows the difference between pquo and quo
        0

        See Also
        ========

        prem, pdiv, pexquo, sympy.polys.domains.ring.Ring.quo

        r   )rh  )r   r  r  rv   r5   r5   r6   pquo
  s   zPolyElement.pquoc                 C  s(   | }| ||\}}|jr|S t||)a  
        Polynomial exact pseudo-quotient in multivariate polynomial ring.

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

        >>> f = x**2 + x*y
        >>> g = 2*x + 2*y
        >>> h = 2*x + 2
        >>> f.pexquo(g)
        2*x
        >>> f.exquo(g) # shows the differnce between pexquo and exquo
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2*x + 2*y does not divide x**2 + x*y
        >>> f.pexquo(h)
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2*x + 2 does not divide x**2 + x*y

        See Also
        ========

        prem, pdiv, pquo, sympy.polys.domains.ring.Ring.exquo

        )rh  rl  r!   )r   r  r  rv   r  r  r5   r5   r6   pexquo  s
   
zPolyElement.pexquoc                 C  sV  | }|j |}||}||}||k r ||}}||}}|dkr(ddgS |dkr0|dgS ||g}|| }d|d  }|||}	|	| }	|||}
|
| }d|g}| }|	r|	|}||	 ||	||| f\}}}}|
 ||  }|||}	|	|}	|||}
|dkr|
 | }||d  }||}n|
 }||  |	s[|S )a  
        Computes the subresultant PRS of two polynomials ``self`` and ``g``.

        Parameters
        ==========

        g : :py:class:`~.PolyElement`
            The second polynomial.
        x : generator or generator index
            The variable with respect to which the subresultant sequence is computed.

        Returns
        =======

        R : list
            Returns a list polynomials representing the subresultant PRS.

        Examples
        ========

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

        >>> f = x**2*y + x*y
        >>> g = x + y
        >>> f.subresultants(g) # first generator is chosen by default if not given
        [x**2*y + x*y, x + y, y**3 - y**2]
        >>> f.subresultants(g, 0) # generator index is given
        [x**2*y + x*y, x + y, y**3 - y**2]
        >>> f.subresultants(g, y) # generator is given
        [x**2*y + x*y, x + y, x**3 + x**2]

        r   r   r   )r7   r   r  rf  r]  r   r  )r   r  r  rv   r  rG   re  dro   r'  lcrH   Sr!  r   r  r5   r5   r6   subresultants7  sH   "







zPolyElement.subresultantsc                 C     | j | |S r^   )r7   dmp_half_gcdexr-  r5   r5   r6   
half_gcdex  r   zPolyElement.half_gcdexc                 C  ro  r^   )r7   	dmp_gcdexr-  r5   r5   r6   gcdex  r   zPolyElement.gcdexc                 C  ro  r^   )r7   dmp_resultantr-  r5   r5   r6   	resultant  r   zPolyElement.resultantc                 C     | j | S r^   )r7   dmp_discriminantru   r5   r5   r6   discriminant  r:  zPolyElement.discriminantc                 C  r  )Nzpolynomial decomposition)r7   r   dup_decomposer"   ru   r5   r5   r6   	decompose     zPolyElement.decomposec                 C  s   | j jr| j | |S td)Nzshift: use shift_list instead)r7   r   	dup_shiftr"   rv   rn   r5   r5   r6   shift  s   zPolyElement.shiftc                 C  ro  r^   )r7   	dmp_shiftr}  r5   r5   r6   
shift_list  r   zPolyElement.shift_listc                 C  r  )Nzsturm sequence)r7   r   	dup_sturmr"   ru   r5   r5   r6   sturm  r{  zPolyElement.sturmc                 C  rv  r^   )r7   dmp_gff_listru   r5   r5   r6   gff_list  r:  zPolyElement.gff_listc                 C  rv  r^   )r7   dmp_normru   r5   r5   r6   norm  r:  zPolyElement.normc                 C  rv  r^   )r7   dmp_sqf_normru   r5   r5   r6   sqf_norm  r:  zPolyElement.sqf_normc                 C  rv  r^   )r7   dmp_sqf_partru   r5   r5   r6   sqf_part  r:  zPolyElement.sqf_partFc                 C  s   | j j| |dS )N)rf   )r7   dmp_sqf_list)rv   rf   r5   r5   r6   sqf_list  r   zPolyElement.sqf_listc                 C  rv  r^   )r7   dmp_factor_listru   r5   r5   r6   factor_list  r:  zPolyElement.factor_listr^   )F)r~   r  r  r  r  r  r   r   r   r   r  r  r  r*  r,  r   r   r1  r4  r7  r9  r;  r<  r=  r?  r   rB  r  rE  rG  r`   r  rc  r@  re  rf  rP  ri  rj  rk  rl  rm  rn  rp  rx  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __floordiv____rfloordiv__r  r   r  r  r  r  r  r  r  r  r  r   r  r   r2  rd  r  r  r  r  r  rX   r  r  r  rw  r  r   r  r  r  ro  r  r  rC  r  r  r  r  r  r  r  r   r"  r#  r*  r+  r   r   r,  r.  r/  r0  r<  r;  rB  r>  rD  r  rE  rJ  rV  r  r]  rf  rh  ri  rj  rn  rq  rs  ru  rx  rz  r~  r  r  r  r  r  r  r  r  r5   r5   r5   r6   rj   <  sR   	0
















6620$!L-,%%


#!	
8,'m 5[~%]rj   N)Qr  
__future__r   typingr   operatorr   r   r   r   r   r	   	functoolsr
   typesr   sympy.core.exprr   sympy.core.intfuncr   sympy.core.symbolr   r   re   sympy.core.sympifyr   r   sympy.ntheory.multinomialr   sympy.polys.compatibilityr   sympy.polys.constructorr   sympy.polys.densebasicr   r   r   !sympy.polys.domains.domainelementr   "sympy.polys.domains.polynomialringr   sympy.polys.heuristicgcdr   sympy.polys.monomialsr   sympy.polys.orderingsr   sympy.polys.polyerrorsr   r    r!   r"   sympy.polys.polyoptionsr#   r{   r$   r}   r%   sympy.polys.polyutilsr&   r'   r(   sympy.printing.defaultsr)   sympy.utilitiesr*   r+   sympy.utilities.iterablesr,   sympy.utilities.magicr-   r7   r8   r>   r[   rh   ri   __annotations__r/   rQ   rj   r5   r5   r5   r6   <module>   sP     
4  z