
    Xh@P                   6   d 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 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/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6 ddl7m&Z8m9Z:m;Z; ddl<m=Z=m>Z>m?Z? ddl@mAZA ddlBmCZCmDZD ddlEmFZF ddlGmHZH eCe0fd(d            ZIeCe0fd            ZJeCe0fd             ZKeCd!             ZLd" ZM G d# d$eAe          ZN G d% d&e(eAeeO          ZPd'S ))zSparse polynomial rings.     )annotations)addmulltlegtge)reduce)GeneratorType)cacheit)Expr)igcd)Symbolsymbols)CantSympifysympify)multinomial_coefficients)IPolys)construct_domain)ninfdmp_to_dictdmp_from_dict)Domain)DomainElementPolynomialRingheugcd)MonomialOps)lexMonomialOrder)CoercionFailedGeneratorsErrorExactQuotientFailedMultivariatePolynomialError)r   Orderbuild_options)expr_from_dict_dict_reorder_parallel_dict_from_expr)DefaultPrinting)publicsubsets)is_sequence)polluteorderMonomialOrder | strc                :    t          | ||          }|f|j        z   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   domainr0   _rings       c/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/sympy/polys/rings.pyringr:   $   s$    8 Wfe,,E8ej      c                6    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'>

    r3   r6   s       r9   xringr=   C   s"    8 Wfe,,E5:r;   c                p    t          | ||          }t          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                    g | ]	}|j         
S  )name).0syms     r9   
<listcomp>zvring.<locals>.<listcomp>~   s    1113ch111r;   )r4   r/   r   r5   r6   s       r9   vringrE   b   s=    6 Wfe,,E11%-1115:>>>Lr;   c                (  
 d}t          |           s| gd}} t          t          t          |                     } t	          ||          }t          | |          \  }}|j        ^t          d |D             g           }t          ||          \  |_        }t          t          ||                    

f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                P    g | ]#}t          |                                          $S r@   listvalues)rB   reps     r9   rD   zsring.<locals>.<listcomp>   s(    ;;;ctCJJLL));;;r;   )optc                P    g | ]"}fd |                                 D             #S )c                (    i | ]\  }}||         S r@   r@   )rB   mc	coeff_maps      r9   
<dictcomp>z$sring.<locals>.<listcomp>.<dictcomp>   s#    999TQIaL999r;   )items)rB   rK   rQ   s     r9   rD   zsring.<locals>.<listcomp>   s6    JJJc9999SYY[[999JJJr;   r   )r.   rI   mapr   r'   r*   r7   sumr   dictzipr4   r5   r0   	from_dict)exprsr   optionssinglerL   repscoeffs
coeffs_domr8   polysrQ   s             @r9   sringr`      s    4 Fu &vWe$$%%E

)
)C )44ID#
z;;T;;;R@@!1&c!B!B!B
JVZ0011	JJJJTJJJSXsz3955EU_d++,,E uQx  u~r;   c                H   t          | t                    r| rt          | d          ndS t          | t                    r| fS t	          |           rCt          d | D                       rt          |           S t          d | D                       r| S t          d          )NT)seqr@   c              3  @   K   | ]}t          |t                    V  d S N)
isinstancestrrB   ss     r9   	<genexpr>z!_parse_symbols.<locals>.<genexpr>   s,      33az!S!!333333r;   c              3  @   K   | ]}t          |t                    V  d S rd   )re   r   rg   s     r9   ri   z!_parse_symbols.<locals>.<genexpr>   s,      66At$$666666r;   zbexpected a string, Symbol or expression or a non-empty sequence of strings, Symbols or expressions)re   rf   _symbolsr   r.   allr#   r   s    r9   _parse_symbolsrn      s    '3 .5=xT****2=	GT	"	" z	W		 33733333 	G$$$66g66666 	N
~

r;   c                  ~   e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   efdZd Zd Zd Z	d Z
d Zd Zd1dZed             Zd Zed             Zed             Zd Zd2dZd Zd Zd ZeZd2dZd2dZd  Zd! Zd" Zd# Zd$ Z d% Z!d& Z"d' Z#d( Z$ed)             Z%ed*             Z&d+ Z'd, Z(d- Z)d. Z*d/ Z+d0 Z,dS )3r4   z*Multivariate distributed polynomial ring. ztuple[PolyElement, ...]r5   ztuple[Expr, ...]r   intngensr   r7   r!   r0   c                   t          t          |                    }t          |          }t          j        |          }t          j                  | j        |||f}|j        r3t          |          t          |j	                  z  rt          d          t                              |           }||_        t          |          |_        ||_	        ||_        ||_        |_        t'          |d          j        |_        d|z  |_        |                                |_        t          |j                  |_        |j        |j        fg|_        |rt9          |          }|                                |_        |                                |_         |!                                |_"        |#                                |_$        |%                                |_&        |'                                |_(        |)                                |_*        n5d }||_        ||_         d |_"        ||_$        ||_&        ||_(        ||_*        tV          u rtX          |_-        n
fd|_-        t]          |j	        |j                  D ]B\  }	}
t_          |	t`                    r(|	j1        }te          ||          stg          |||
           C|S )Nz7polynomial ring and it's ground domain share generatorsr@   r   c                    dS Nr@   r@   )abs     r9   <lambda>z"PolyRing.__new__.<locals>.<lambda>   s    2 r;   c                    dS ru   r@   )rv   rw   rP   s      r9   rx   z"PolyRing.__new__.<locals>.<lambda>   s    " r;   c                &    t          |           S )Nkey)max)fr0   s    r9   rx   z"PolyRing.__new__.<locals>.<lambda>   s    QE):):): r;   )4tuplern   len	DomainOpt
preprocessOrderOpt__name__is_Compositesetr   r#   object__new___hash_tuplehash_hashrq   r7   r0   PolyElementnewdtype
zero_monom_gensr5   	_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    r}   leading_expvrW   re   r   rA   hasattrsetattr)clsr   r7   r0   rq   r   objcodegenmonunitsymbol	generatorrA   s      `        r9   r   zPolyRing.__new__   sx   w//00G%f--#E**|WeVUC 	]3w<<#fn2E2E#E 	]!"[\\\nnS!!%%%		
	R((,	e99;;CH^VZ01 	'!%((G&{{}}C&{{}}C").."2"2C 'C&{{}}C&{{}}C&{{}}C%oG&C&C"4"4C 'C&C&C&C C<<"C::::C!$S[#(!;!; 	2 	2FI&&)) 2{sD)) 2Cy111
r;   c                    | j         j        }g }t          | j                  D ]8}|                     |          }| j        }|||<   |                    |           9t          |          S )z(Return a list of polynomial generators. )r7   r   rangerq   monomial_basiszeroappendr   )selfr   r   iexpvpolys         r9   r   zPolyRing._gens  sm    kotz"" 	 	A&&q))D9DDJLLU||r;   c                *    | j         | j        | j        fS rd   )r   r7   r0   r   s    r9   __getnewargs__zPolyRing.__getnewargs__  s    dk4:66r;   c                x    | j                                         }|d= |D ]}|                    d          r||= |S )Nr   	monomial_)__dict__copy
startswith)r   stater|   s      r9   __getstate__zPolyRing.__getstate__  sM    ""$$.! 	 	C~~k** #Jr;   c                    | j         S rd   )r   r   s    r9   __hash__zPolyRing.__hash__#  s
    zr;   c                    t          |t                    o5| j        | j        | j        | j        f|j        |j        |j        |j        fk    S rd   )re   r4   r   r7   rq   r0   r   others     r9   __eq__zPolyRing.__eq__&  sI    %** D\4;
DJ?]EL%+u{CD	Dr;   c                    | |k     S rd   r@   r   s     r9   __ne__zPolyRing.__ne__+  s    5=  r;   Nc                |    |$t          |t                    rt          |          }|                     |||          S rd   )re   rI   r   _cloner   r   r7   r0   s       r9   clonezPolyRing.clone.  s8    :gt#<#<GnnG{{7FE222r;   c                Z    |                      |p| j        |p| j        |p| j                  S rd   )	__class__r   r7   r0   r   s       r9   r   zPolyRing._clone4  s/    ~~g5v7LeNaW[Wabbbr;   c                @    dg| j         z  }d||<   t          |          S )zReturn the ith-basis element. r      )rq   r   )r   r   basiss      r9   r   zPolyRing.monomial_basis8  s$    DJaU||r;   c                ,    |                      g           S rd   )r   r   s    r9   r   zPolyRing.zero>  s    zz"~~r;   c                6    |                      | j                  S rd   )r   r   r   s    r9   r   zPolyRing.oneB  s    zz$)$$$r;   c                B    t          |t                    o
|j        | k    S )zATrue if ``element`` is an element of this ring. False otherwise. )re   r   r:   r   elements     r9   
is_elementzPolyRing.is_elementF  s    ';//HGLD4HHr;   c                8    | j                             ||          S rd   )r7   convertr   r   orig_domains      r9   
domain_newzPolyRing.domain_newJ  s    {""7K888r;   c                8    |                      | j        |          S rd   )term_newr   )r   coeffs     r9   
ground_newzPolyRing.ground_newM  s    }}T_e444r;   c                L    |                      |          }| j        }|r|||<   |S rd   )r   r   )r   monomr   r   s       r9   r   zPolyRing.term_newP  s0    &&y 	 DKr;   c                   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                    r| 	                    |          S t          |t                    r;	 |                     |          S # t          $ r |                     |          cY S w xY wt          |t                    r|                     |          S |                     |          S )N
conversionparsing)re   r   r:   r7   r   r   NotImplementedErrorrf   rV   rX   rI   
from_terms
ValueError	from_listr   	from_exprr   s     r9   ring_newzPolyRing.ring_newW  sC   g{++ 	,w|##DK88 8T[=MQXQ]=]=]w///),777%% 	,%i000&& 
	,>>'***&& 	,/w/// / / /~~g...../&& 	,>>'***??7+++s   C/ /DDc                |    | j         }| j        }|                                D ]\  }} |||          }|r|||<   |S rd   )r   r   rS   )r   r   r   r   r   r   r   s          r9   rX   zPolyRing.from_dicto  sS    _
y#MMOO 	$ 	$LE5Juk22E $#Ur;   c                H    |                      t          |          |          S rd   )rX   rV   r   s      r9   r   zPolyRing.from_termsz  s    ~~d7mm[999r;   c                d    |                      t          || j        dz
  | j                            S Nr   )rX   r   rq   r7   r   s     r9   r   zPolyRing.from_list}  s(    ~~k'4:a<MMNNNr;   c                X      j          fd t          |                    S )Nc           	                             |           }||S | j        r5t          t          t	          t          | j                                      S | j        r5t          t          t	          t          | j                                      S | 	                                \  }}|j
        r!|dk    r |          t          |          z  S                                         |                     S r   )getis_Addr
   r   rI   rT   argsis_Mulr   as_base_exp
is_Integerrp   r   r   )exprr   baseexp_rebuildr7   mappingr   s       r9   r   z(PolyRing._rebuild_expr.<locals>._rebuild  s    D))I$   Ac4Hdi(@(@#A#ABBB 	Ac4Hdi(@(@#A#ABBB !,,..	c> AcAgg#8D>>3s8833??6>>$+?+?@@@r;   )r7   r   )r   r   r   r   r7   s   ` `@@r9   _rebuild_exprzPolyRing._rebuild_expr  sU    	A 	A 	A 	A 	A 	A 	A 	A$ x&&&r;   c                   t          t          t          | j        | j                                      }	 |                     ||          }|                     |          S # t          $ r t          d| d|          w xY w)Nz6expected an expression convertible to a polynomial in z, got )	rV   rI   rW   r   r5   r   r   r"   r   )r   r   r   r   s       r9   r   zPolyRing.from_expr  s    tCdi8899::	'%%dG44D ==&&&  	p 	p 	p*cgcgcgimimnooo	ps   A! ! Bc                8   || j         rd}nd}nt          |t                    r?|}d|k    r|| j         k     rn| j          |k    r|dk    r| dz
  }nt          d|z            |                     |          r<	 | j                            |          }n# t          $ r t          d|z            w xY wt          |t                    r<	 | j                            |          }n2# t          $ r t          d|z            w xY wt          d|z            |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)	rq   re   rp   r   r   r5   indexrf   r   )r   genr   s      r9   r   zPolyRing.index  sg   ;z S!! 	lAAvv!dj..*!!a2ggBF !>!DEEE__S!! 	l@IOOC(( @ @ @ !83!>???@S!! 	l@L&&s++ @ @ @ !83!>???@ dgjjkkks   <B B4C( (Dc                    t          t          | j        |                    fdt          | j                  D             }|s| j        S |                     |          S )z,Remove specified generators from this ring. c                "    g | ]\  }}|v	|S r@   r@   rB   r   rh   indicess      r9   rD   z!PolyRing.drop.<locals>.<listcomp>  s'    OOO$!QQg=M=MA=M=M=Mr;   rm   )r   rT   r   	enumerater   r7   r   r   r5   r   r  s      @r9   dropzPolyRing.drop  sc    c$*d++,,OOOO)DL"9"9OOO 	/;::g:...r;   c                Z    | j         |         }|s| j        S |                     |          S )Nrm   )r   r7   r   )r   r|   r   s      r9   __getitem__zPolyRing.__getitem__  s2    ,s# 	/;::g:...r;   c                    | j         j        st          | j         d          r |                     | j         j                   S t	          d| j         z            )Nr7   r7   z%s is not a composite domain)r7   r   r   r   r   r   s    r9   	to_groundzPolyRing.to_ground  sS    ;# 	Kwt{H'E'E 	K::T[%7:888;dkIJJJr;   c                     t          |           S rd   r   r   s    r9   	to_domainzPolyRing.to_domain  s    d###r;   c                F    ddl m}  || j        | j        | j                  S )Nr   )	FracField)sympy.polys.fieldsr  r   r7   r0   )r   r  s     r9   to_fieldzPolyRing.to_field  s.    000000yt{DJ???r;   c                2    t          | j                  dk    S r   r   r5   r   s    r9   is_univariatezPolyRing.is_univariate  s    49~~""r;   c                2    t          | j                  dk    S r   r  r   s    r9   is_multivariatezPolyRing.is_multivariate  s    49~~!!r;   c                p    | j         }|D ]+}t          |t                    r| | j        | z  }&||z  },|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   s       r9   r   zPolyRing.add  sS    " I 	 	C3666 XTXs^#Sr;   c                p    | j         }|D ]+}t          |t                    r| | j        | z  }&||z  },|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  s       r9   r   zPolyRing.mul  sS    " H 	 	C3666 XTXs^#Sr;   c                   t          t          | j        |                    fdt          | j                  D             }fdt          | j                  D             }|s| S |                     | | j        |           S )zd
        Remove specified generators from the ring and inject them into
        its domain.
        c                "    g | ]\  }}|v	|S r@   r@   r  s      r9   rD   z+PolyRing.drop_to_ground.<locals>.<listcomp>$  s'    MMMAAW<L<L1<L<L<Lr;   c                "    g | ]\  }}|v	|S r@   r@   )rB   r   r   r  s      r9   rD   z+PolyRing.drop_to_ground.<locals>.<listcomp>%  s'    KKK3!7:J:J:J:J:Jr;   r   r7   )r   rT   r   r  r   r5   r   r  r  s      @r9   drop_to_groundzPolyRing.drop_to_ground  s    
 c$*d++,,MMMM4<!8!8MMMKKKK)DI"6"6KKK 	HK::gidi6F:GGGr;   c                    | |k    r\t          | j                                      t          |j                            }|                     t	          |                    S | S )z+Add the generators of ``other`` to ``self``rm   r   r   unionr   rI   )r   r   symss      r9   composezPolyRing.compose,  sQ    5==t|$$**3u}+=+=>>D::d4jj:111Kr;   c                    t          | j                                      t          |                    }|                     t	          |                    S )z9Add the elements of ``symbols`` as generators to ``self``rm   r#  )r   r   r%  s      r9   add_genszPolyRing.add_gens4  s?    4<  &&s7||44zz$t**z---r;   c                   |dk     s|| j         k    rt          d|d| j                  |s| j        S | j        }t          t          | j                   t          |                    D ]Rt          fdt          | j                   D                       }|| 	                    || j
        j                  z  }S|S )zo
        Return the elementary symmetric polynomial of degree *n* over
        this ring's generators.
        r   z.Cannot generate symmetric polynomial of order z for c              3  :   K   | ]}t          |v           V  d S rd   )rp   )rB   r   rh   s     r9   ri   z*PolyRing.symmetric_poly.<locals>.<genexpr>E  s-      EEac!q&kkEEEEEEr;   )rq   r   r5   r   r   r-   r   rp   r   r   r7   )r   nr   r   rh   s       @r9   symmetric_polyzPolyRing.symmetric_poly9  s    
 q55A
NN*Z[Z[Z[]a]f]fghhh 	8O9DU4:..A77 > >EEEE53D3DEEEEEeT[_===Kr;   )NNNrd   )-r   
__module____qualname____doc____annotations__r    r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   __call__rX   r   r   r   r   r   r  r  r
  r  r  r  r  r   r   r!  r&  r(  r,  r@   r;   r9   r4   r4      s        44!!!!JJJNNN,/ < < < <|	 	 	7 7 7    D D D
! ! !3 3 3 3 c c Wc     X % % X%I I I9 9 9 95 5 5  , , ,, H	 	 	 	: : : :O O O' ' '.' ' '  >/ / // / /K K K$ $ $@ @ @ # # X# " " X"  6  6H H H  . . .
    r;   r4   c                      e Zd ZdZ fdZd Zd Zd Zd ZdZ	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z e!d             Z"e!d              Z#e!d!             Z$e!d"             Z%e!d#             Z&e!d$             Z'e!d%             Z(e!d&             Z)e!d'             Z*e!d(             Z+e!d)             Z,e!d*             Z-e!d+             Z.e!d,             Z/e!d-             Z0e!d.             Z1e!d/             Z2d0 Z3d1 Z4d2 Z5d3 Z6d4 Z7d5 Z8d6 Z9d7 Z:d8 Z;d9 Z<d: Z=d; Z>d< Z?d= Z@d> ZAd? ZBd@ ZCdA ZDdB ZEdC ZFdD ZGdE ZHdF ZIdG ZJdH ZKdI ZLdJ ZMddKZNdL ZOddMZPdN ZQdO ZRdP ZSdQ ZTdR ZUe!dS             ZVe!dT             ZWdU ZXe!dV             ZYdW ZZdX Z[ddYZ\ddZZ]dd[Z^d\ Z_d] Z`d^ Zad_ Zbd` Zcda Zddb Zedc Zfdd Zgde Zhdf Zidg Zjdh Zkdi Zldj Zmdk ZnenZodl Zpdm Zqdn Zrdo Zsdp Ztdq Zudr Zvds Zwdt Zxdu Zydv Zzdw Z{dx Z|dy Z}dz Z~d{ Zd| Zd} Zdd~ZddZd ZddZd ZddZddZddZddZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Z xZS )r   z5Element of multivariate distributed polynomial ring. c                X    t                                          |           || _        d S rd   )super__init__r:   )r   r:   initr   s      r9   r6  zPolyElement.__init__M  s&    			r;   c                   t          | t                    sJ t          | j        t                    sJ | j        j        }t          |t
                    sJ |                                 D ]V\  }}|                    |          sJ t          |          | j        j	        k    sJ t          d |D                       sJ Wd S )Nc              3  L   K   | ]}t          |t                    o|d k    V   dS )r   N)re   rp   )rB   r   s     r9   ri   z%PolyElement._check.<locals>.<genexpr>[  s5      JJSz#s++8qJJJJJJr;   )re   r   r:   r4   r7   r   rS   of_typer   rq   rl   )r   domr   r   s       r9   _checkzPolyElement._checkS  s    $,,,,,$)X.....i#v&&&&& JJLL 	K 	KLE5;;u%%%%%u::0000JJEJJJJJJJJJ	K 	Kr;   c                8    |                      | j        |          S rd   )r   r:   )r   r7  s     r9   r   zPolyElement.new]  s    ~~di...r;   c                4    | j                                         S rd   )r:   r  r   s    r9   parentzPolyElement.parent`  s    y""$$$r;   c                R    | j         t          |                                           fS rd   )r:   rI   	itertermsr   s    r9   r   zPolyElement.__getnewargs__c  s!    	4 0 01122r;   Nc                    | j         }|<t          | j        t          |                                           f          x| _         }|S rd   )r   r   r:   	frozensetrS   )r   r   s     r9   r   zPolyElement.__hash__h  s@     
=!%ty)DJJLL2I2I&J!K!KKDJr;   c                ,    |                      |           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   s    r9   r   zPolyElement.copys  s    4 xx~~r;   c           	     .   | j         |k    r| S | j         j        |j        k    rTt          t          t	          | | j         j        |j                             }|                    || j         j                  S |                    | | j         j                  S rd   )r:   r   rI   rW   r)   r   r7   rX   )r   new_ringtermss      r9   set_ringzPolyElement.set_ring  s    9  KY("222mD$)2CXEUVVWXXE&&udi.>???%%dDI,<===r;   c                    |s| j         j        }nIt          |          | j         j        k    r,t	          d| j         j        dt          |                    t          |                                 g|R  S )Nz"Wrong number of symbols, expected z got )r:   r   r   rq   r   r(   as_expr_dict)r   r   s     r9   as_exprzPolyElement.as_expr  s}     	i'GG\\TY_,,*#g,,,0  
 d//11<G<<<<r;   c                f    | j         j        j        fd|                                 D             S )Nc                .    i | ]\  }}| |          S r@   r@   )rB   r   r   to_sympys      r9   rR   z,PolyElement.as_expr_dict.<locals>.<dictcomp>  s'    LLL<5%xxLLLr;   )r:   r7   rN  rA  )r   rN  s    @r9   rJ  zPolyElement.as_expr_dict  s4    9#,LLLL4>>;K;KLLLLr;   c                b   | j         j        }|j        r|j        s	|j        | fS |                                }|j        |j        }|j        }|                                 D ]} | ||                    | 	                    fd| 
                                D                       }|fS )Nc                $    g | ]\  }}||z  fS r@   r@   )rB   kvcommons      r9   rD   z,PolyElement.clear_denoms.<locals>.<listcomp>  s%    BBBDAq1ah-BBBr;   )r:   r7   is_Fieldhas_assoc_Ringr   get_ringr   denomrJ   r   rS   )r   r7   ground_ringr   rW  r   r   rS  s          @r9   clear_denomszPolyElement.clear_denoms  s    ! 	$f&; 	$:t##oo''o[[]] 	/ 	/ESu..FFxxBBBBDJJLLBBBCCt|r;   c                ^    t          |                                           D ]
\  }}|s| |= dS )z+Eliminate monomials with zero coefficient. NrI   rS   )r   rQ  rR  s      r9   
strip_zerozPolyElement.strip_zero  s?    && 	 	DAq G	 	r;   c                    |s|  S | j                             |          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:   r   rV   r   r   r   r   p1p2s     r9   r   zPolyElement.__eq__  so    "  	46MW## 	4;;r2&&&WWq[[566"',--33r;   c                    | |k     S rd   r@   r^  s     r9   r   zPolyElement.__ne__  s    8|r;   c                &   | j         }|                    |          rt          |                                           t          |                                          k    rdS |j        j        }|                                 D ]} || |         ||         |          s dS dS t          |           dk    rdS 	 |j                            |          }|j                            |                                 ||          S # t          $ r Y dS w xY w)z+Approximate equality test for polynomials. FTr   )
r:   r   r   keysr7   almosteqr   r   constr"   )r_  r`  	tolerancer:   rd  rQ  s         r9   rd  zPolyElement.almosteq  s   w??2 	G27799~~RWWYY//u{+HWWYY ! !x1r!ui88 ! 55!4WWq[[5G[((,, {++BHHJJIFFF "   uus   :D 
DDc                H    t          |           |                                 fS rd   )r   rG  r   s    r9   sort_keyzPolyElement.sort_key  s    D		4::<<((r;   c                    | j                             |          r0 ||                                 |                                          S t          S rd   )r:   r   rh  NotImplemented)r_  r`  ops      r9   _cmpzPolyElement._cmp  sB    7b!! 	"2bkkmmR[[]]333!!r;   c                8    |                      |t                    S rd   )rl  r   r^  s     r9   __lt__zPolyElement.__lt__      wwr2r;   c                8    |                      |t                    S rd   )rl  r   r^  s     r9   __le__zPolyElement.__le__  ro  r;   c                8    |                      |t                    S rd   )rl  r   r^  s     r9   __gt__zPolyElement.__gt__   ro  r;   c                8    |                      |t                    S rd   )rl  r	   r^  s     r9   __ge__zPolyElement.__ge__  ro  r;   c                    | j         }|                    |          }|j        dk    r	||j        fS t	          |j                  }||= ||                    |          fS )Nr   rm   )r:   r   rq   r7   rI   r   r   r   r   r:   r   r   s        r9   _dropzPolyElement._drop  s^    yJJsOO:??dk>!4<((G
djjj1111r;   c                x   |                      |          \  }}| j        j        dk    r.| j        r|                     d          S t          d|z            |j        }|                                 D ]G\  }}||         dk    r%t          |          }||= ||t          |          <   6t          d|z            |S )Nr   zCannot drop %sr   )
rx  r:   rq   	is_groundr   r   r   rS   rI   r   )r   r   r   r:   r   rQ  rR  Ks           r9   r  zPolyElement.drop  s    **S//49?a~ 9zz!}}$ !1C!78889D

 = =1Q4199QA!%&DqNN$%5%;<<<Kr;   c                    | j         }|                    |          }t          |j                  }||= ||                    |||                   fS )Nr   )r:   r   rI   r   r   rw  s        r9   _drop_to_groundzPolyElement._drop_to_ground%  sM    yJJsOOt|$$AJ$**WT!W*====r;   c                   | j         j        dk    rt          d          |                     |          \  }}|j        }|j        j        d         }|                                 D ]o\  }}|d |         ||dz   d          z   }||vr"|||         z                      |          ||<   C||xx         |||         z                      |          z  cc<   p|S )Nr   z$Cannot drop only generator to groundr   )	r:   rq   r   r}  r   r7   r5   rA  
mul_ground)r   r   r   r:   r   r   r   mons           r9   r!  zPolyElement.drop_to_ground-  s    9?aCDDD&&s++4ykq! NN,, 	? 	?LE5)eAaCDDk)C$ %(]66u==S		S			c58m77>>>				r;   c                R    t          | | j        j        dz
  | j        j                  S r   )r   r:   rq   r7   r   s    r9   to_densezPolyElement.to_dense>  s"    T49?1#4di6FGGGr;   c                     t          |           S rd   )rV   r   s    r9   to_dictzPolyElement.to_dictA  s    Dzzr;   c                   | s$|                     | j        j        j                  S |d         }|d         }| j        }|j        }|j        }	|j        }
g }|                                 D ]\  }}|j                            |          }|rdnd}|	                    |           ||
k    r7|                     |          }|r|
                    d          r
|dd          }n5|r| }|| j        j        j        k    r|                    ||d          }nd	}g }t          |	          D ]}||         }|s|                    ||         |d          }|dk    rO|t          |          k    s|d
k     r|                    ||d          }n|}|	                    |||fz             |	                    d|z             |r|g|z   }|	                    |                    |                     |d
         dv r1|                    d
          }|dk    r|                    d
d           d	                    |          S )NMulAtom -  + -r   T)strict r   Fz%s)r  r  )_printr:   r7   r   r   rq   r   rG  is_negativer   r   r   parenthesizer   rp   joinpopinsert)r   printer
precedenceexp_pattern
mul_symbolprec_mul	prec_atomr:   r   rq   zmsexpvsr   r   negativesignscoeffsexpvr   r   r   sexpheads                          r9   rf   zPolyElement.strD  su    	9>>$)"2"7888e$v&	y,
_::<< 	2 	2KD%{..u55H$/55%DMM$rzz .. ( 1 1# 6 6 (#ABBZF #"FEDI,000$11%$1OOFFFE5\\ 0 01g  --gaj)D-QQ!88c#hh#''&33C53QQ"LL~!=>>>>LL//// )5(MM*//%001111!9&&::a==Du}}a%%%wwvr;   c                    | | j         j        v S rd   )r:   r   r   s    r9   is_generatorzPolyElement.is_generatort  s    ty***r;   c                J    |  p t          |           dk    o| j        j        | v S r   )r   r:   r   r   s    r9   rz  zPolyElement.is_groundx  s(    xLCIINKty/Ct/KLr;   c                D    |  pt          |           dk    o
| j        dk    S r   )r   LCr   s    r9   is_monomialzPolyElement.is_monomial|  s$    x<CIIN;tw!|<r;   c                (    t          |           dk    S r   )r   r   s    r9   is_termzPolyElement.is_term  s    4yyA~r;   c                J    | j         j                            | j                  S rd   )r:   r7   r  r  r   s    r9   r  zPolyElement.is_negative      y++DG444r;   c                J    | j         j                            | j                  S rd   )r:   r7   is_positiver  r   s    r9   r  zPolyElement.is_positive  r  r;   c                J    | j         j                            | j                  S rd   )r:   r7   is_nonnegativer  r   s    r9   r  zPolyElement.is_nonnegative      y..tw777r;   c                J    | j         j                            | j                  S rd   )r:   r7   is_nonpositiver  r   s    r9   r  zPolyElement.is_nonpositive  r  r;   c                    |  S rd   r@   r~   s    r9   is_zerozPolyElement.is_zero  s	    ur;   c                "    | | j         j        k    S rd   )r:   r   r  s    r9   is_onezPolyElement.is_one  s    AFJr;   c                J    | j         j                            | j                  S rd   )r:   r7   r  r  r  s    r9   is_moniczPolyElement.is_monic  s    v}##AD)))r;   c                d    | j         j                            |                                           S rd   )r:   r7   r  contentr  s    r9   is_primitivezPolyElement.is_primitive  s"    v}##AIIKK000r;   c                X    t          d |                                 D                       S )Nc              3  <   K   | ]}t          |          d k    V  dS r   NrU   rB   r   s     r9   ri   z(PolyElement.is_linear.<locals>.<genexpr>  ,      ??u3u::???????r;   rl   
itermonomsr  s    r9   	is_linearzPolyElement.is_linear  '    ????????r;   c                X    t          d |                                 D                       S )Nc              3  <   K   | ]}t          |          d k    V  dS )   Nr  r  s     r9   ri   z+PolyElement.is_quadratic.<locals>.<genexpr>  r  r;   r  r  s    r9   is_quadraticzPolyElement.is_quadratic  r  r;   c                R    | j         j        sdS | j                             |           S NT)r:   rq   	dmp_sqf_pr  s    r9   is_squarefreezPolyElement.is_squarefree  s)    v| 	4v"""r;   c                R    | j         j        sdS | j                             |           S r  )r:   rq   dmp_irreducible_pr  s    r9   is_irreduciblezPolyElement.is_irreducible  s)    v| 	4v''***r;   c                l    | j         j        r| j                             |           S t          d          )Nzcyclotomic polynomial)r:   r  dup_cyclotomic_pr%   r  s    r9   is_cyclotomiczPolyElement.is_cyclotomic  s5    6 	G6**1----.EFFFr;   c                d    |                      d |                                 D                       S )Nc                    g | ]
\  }}|| fS r@   r@   )rB   r   r   s      r9   rD   z'PolyElement.__neg__.<locals>.<listcomp>  s"    PPPleU55&/PPPr;   )r   rA  r   s    r9   __neg__zPolyElement.__neg__  s-    xxPPdnn>N>NPPPQQQr;   c                    | S rd   r@   r   s    r9   __pos__zPolyElement.__pos__  s    r;   c                `   |s|                                  S | j        }|                    |          r]|                                  }|j        }|j        j        }|                                D ]\  }} |||          |z   }|r|||<   ||=  |S t          |t                    rt          |j        t                    r|j        j        |j        k    rnPt          |j        j        t                    r*|j        j        j        |k    r|
                    |           S t          S 	 |                    |          }|                                  }|s|S |j        }	|	|                                 vr|||	<   n!|||	          k    r||	= n||	xx         |z  cc<   |S # t          $ r
 t          cY S w xY w)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   r:   r   r   r7   r   rS   re   r   r   __radd__rj  r   r   rc  r"   )
r_  r`  r:   r  r   r   rQ  rR  cp2r  s
             r9   __add__zPolyElement.__add__  s     	7799w??2 	&		A%C;#D

  1C4LL1$ AaDD!HK(( 	&$+~66 &4;;Krw;V;VBGNN;; &@SW[@[@[{{2&%%	//"%%C 		A B"""!B%<<"bEEESLEEEH  	" 	" 	"!!!!	"s   &F F-,F-c                &   |                                  }|s|S | j        }	 |                    |          }|j        }||                                 vr|||<   n!|||          k    r||= n||xx         |z  cc<   |S # t
          $ r
 t          cY S w xY wrd   )r   r:   r   r   rc  r"   rj  )r_  r+  r  r:   r  s        r9   r  zPolyElement.__radd__  s    GGII 	Hw	""A B"""2;;"bEEEQJEEEH  	" 	" 	"!!!!	"s   A< <BBc                X   |s|                                  S | j        }|                    |          r]|                                  }|j        }|j        j        }|                                D ]\  }} |||          |z
  }|r|||<   ||=  |S t          |t                    rt          |j        t                    r|j        j        |j        k    rnPt          |j        j        t                    r*|j        j        j        |k    r|
                    |           S t          S 	 |                    |          }|                                  }|j        }||                                 vr| ||<   n |||         k    r||= n||xx         |z  cc<   |S # t          $ r
 t          cY S w xY w)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   r:   r   r   r7   r   rS   re   r   r   __rsub__rj  r   r   rc  r"   )	r_  r`  r:   r  r   r   rQ  rR  r  s	            r9   __sub__zPolyElement.__sub__  s      	7799w??2 	&		A%C;#D

  1C4LL1$ AaDD!HK(( 	&$+~66 &4;;Krw;V;VBGNN;; &@SW[@[@[{{2&%%	$$B 		AB"""2;;"bEEERKEEEH  	" 	" 	"!!!!	"s   &F F)(F)c                    | j         }	 |                    |          }|j        }| D ]}| |          ||<   ||z  }|S # t          $ r
 t          cY S w xY w)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

        )r:   r   r   r"   rj  )r_  r+  r:   r  r   s        r9   r  zPolyElement.__rsub__E  s     w
	""A 	A $ $d8)$FAH  	" 	" 	"!!!!	"s   = AAc                P   | j         }|j        }| r|s|S |                    |          r|j        }|j        j        }|j        }t          |                                          }|                                 D ].\  }}	|D ]&\  }
} |||
          } |||          |	|z  z   ||<   '/|                                 |S t          |t                    rt          |j        t                    r|j        j         |j         k    rnPt          |j         j        t                    r*|j         j        j         |k    r|                    |           S t          S 	 |                    |          }|                                 D ]\  }}	|	|z  }|r|||<   |S # t          $ r
 t          cY S w xY w)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

        )r:   r   r   r   r7   r   rI   rS   r\  re   r   r   __rmul__rj  r   r"   )r_  r`  r:   r  r   r   r   p2itexp1v1exp2v2r   rR  s                 r9   __mul__zPolyElement.__mul__a  s     wI 	& 	&H__R   	&%C;#D,L

##DHHJJ 4 4b $ 4 4HD"&,tT22C Sd^^be3AcFF4 LLNNNHK(( 	&$+~66 &4;;Krw;V;VBGNN;; &@SW[@[@[{{2&%%
	$$B HHJJ    brE  AdGH  	" 	" 	"!!!!	"s   F F%$F%c                    | j         j        }|s|S 	 |j                             |          }|                                 D ]\  }}||z  }|r|||<   |S # t          $ r
 t
          cY S w xY w)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

        )r:   r   r   rS   r"   rj  )r_  r`  r  r  r  rR  s         r9   r  zPolyElement.__rmul__  s     GL 	H		""2&&B HHJJ    brE  AdGH  	" 	" 	"!!!!	"s   A A('A(c                >   t          |t                    st          d|z            |dk     rt          d|z            | j        }|s| r|j        S t          d          t          |           dk    ryt          |                                           d         \  }}|j	        }||j
        j        k    r|||                    ||          <   n||z  ||                    ||          <   |S t          |          }|dk     rt          d          |dk    r|                                 S |dk    r|                                 S |dk    r| |                                 z  S t          |           d	k    r|                     |          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#exponent must be an integer, got %sr   z/exponent must be a non-negative integer, got %sz0**0r   zNegative exponentr        )re   rp   	TypeErrorr   r:   r   r   rI   rS   r   r7   r   r   square_pow_multinomial_pow_generic)r   r+  r:   r   r   r  s         r9   __pow__zPolyElement.__pow__  s    !S!! 	TAAEFFFUUNQRRSSSy 	 )x (((YY!^^

--a0LE5	A''16$##E1--..16$##E1--.H FFq550111!VV99;;!VV;;== !VV%%YY!^^((+++$$Q'''r;   c                |    | j         j        }| }	 |dz  r||z  }|dz  }|sn|                                }|dz  },|S )NTr   r  )r:   r   r  )r   r+  r  rP   s       r9   r  zPolyElement._pow_generic  s`    IM	1u aCQ 

AQA	 r;   c                   t          t          |           |                                          }| j        j        }| j        j        }|                                 }| j        j        j        }| j        j        }|D ]r\  }}	|}
|	}t          ||          D ]\  }\  }}|r ||
||          }
|||z  z  } t          |
          }|}|
                    ||          |z   }|r|||<   k||v r||= s|S rd   )r   r   rS   r:   r   r   r7   r   rW   r   r   )r   r+  multinomialsr   r   rG  r   r   multinomialmultinomial_coeffproduct_monomproduct_coeffr   r   r   s                  r9   r  zPolyElement._pow_multinomial  s   /D		1==CCEE)3Y)


y$y~.: 	  	 *K*&M-M'*;'>'> 0 0#^eU 0$3OM5#$N$NM!UCZ/M-((E!EHHUD))E1E  #U$Kr;   c                N   | j         }|j        }|j        }t          |                                           }|j        j        }|j        }t          t          |                    D ]S}||         }| |         }	t          |          D ]1}
||
         } |||          } |||          |	| |         z  z   ||<   2T|	                    d          }|j        }| 
                                D ]&\  }} |||          } |||          |dz  z   ||<   '|                                 |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

        r  )r:   r   r   rI   rc  r7   r   r   r   imul_numrS   r\  )r   r:   r  r   rc  r   r   r   k1pkjk2r   rQ  rR  s                  r9   r  zPolyElement.square  s9    yIeDIIKK  {(s4yy!! 	6 	6AaBbB1XX 6 6!W"l2r**S$"T"X+5#6 JJqMMeJJLL 	) 	)DAqa##BCDMMAqD(AbEE	r;   c                ^   | j         }|st          d          |                    |          r|                     |          S t	          |t
                    rt	          |j        t                    r|j        j         |j         k    rnPt	          |j         j        t                    r*|j         j        j         |k    r|                    |           S t          S 	 |
                    |          }|                     |          |                     |          fS # t          $ r
 t          cY S w xY wNpolynomial division)r:   ZeroDivisionErrorr   r   re   r   r7   r   __rdivmod__rj  r   
quo_ground
rem_groundr"   r_  r`  r:   s      r9   
__divmod__zPolyElement.__divmod__:  s"   w 
	&#$9:::__R   	&66"::K(( 	&$+~66 &4;;Krw;V;VBGNN;; &@SW[@[@[~~b)))%%	:$$B MM"%%r}}R'8'899  	" 	" 	"!!!!	"s   D D,+D,c                    | j         }	 |                    |          }|                    |           S # t          $ r
 t          cY S w xY wrd   )r:   r   r   r"   rj  r  s      r9   r  zPolyElement.__rdivmod__P  Z    w	$$B 66"::  	" 	" 	"!!!!	"   3 AAc                4   | j         }|st          d          |                    |          r|                     |          S t	          |t
                    rt	          |j        t                    r|j        j         |j         k    rnPt	          |j         j        t                    r*|j         j        j         |k    r|                    |           S t          S 	 |
                    |          }|                     |          S # t          $ r
 t          cY S w xY wr  )r:   r  r   remre   r   r7   r   __rmod__rj  r   r  r"   r  s      r9   __mod__zPolyElement.__mod__Y  s   w 
	&#$9:::__R   	&66"::K(( 	&$+~66 &4;;Krw;V;VBGNN;; &@SW[@[@[{{2&%%	%$$B ==$$$  	" 	" 	"!!!!	"   D DDc                    | j         }	 |                    |          }|                    |           S # t          $ r
 t          cY S w xY wrd   )r:   r   r  r"   rj  r  s      r9   r  zPolyElement.__rmod__o  r
  r  c                4   | j         }|st          d          |                    |          r|                     |          S t	          |t
                    rt	          |j        t                    r|j        j         |j         k    rnPt	          |j         j        t                    r*|j         j        j         |k    r|                    |           S t          S 	 |
                    |          }|                     |          S # t          $ r
 t          cY S w xY wr  )r:   r  r   quore   r   r7   r   __rtruediv__rj  r   r  r"   r  s      r9   __floordiv__zPolyElement.__floordiv__x  s   w 
	&#$9:::__R   	&66"::K(( 	&$+~66 &4;;Krw;V;VBGNN;; &@SW[@[@[r***%%	%$$B ==$$$  	" 	" 	"!!!!	"r  c                    | j         }	 |                    |          }|                    |           S # t          $ r
 t          cY S w xY wrd   )r:   r   r  r"   rj  r  s      r9   __rfloordiv__zPolyElement.__rfloordiv__  r
  r  c                4   | j         }|st          d          |                    |          r|                     |          S t	          |t
                    rt	          |j        t                    r|j        j         |j         k    rnPt	          |j         j        t                    r*|j         j        j         |k    r|                    |           S t          S 	 |
                    |          }|                     |          S # t          $ r
 t          cY S w xY wr  )r:   r  r   exquore   r   r7   r   r  rj  r   r  r"   r  s      r9   __truediv__zPolyElement.__truediv__  s   w 
	&#$9:::__R   	&88B<<K(( 	&$+~66 &4;;Krw;V;VBGNN;; &@SW[@[@[r***%%	%$$B ==$$$  	" 	" 	"!!!!	"r  c                    | j         }	 |                    |          }|                    |           S # t          $ r
 t          cY S w xY wrd   )r:   r   r  r"   rj  r  s      r9   r  zPolyElement.__rtruediv__  sZ    w	 $$B 88B<<  	" 	" 	"!!!!	"r  c                    | j         j        | j         j        }|j        | j         j        |j        rfd}nfd}|S )Nc                f    | \  }}|\  }}|	k    r|}n ||          }|| ||          fS d S rd   r@   
	a_lm_a_lc	b_lm_b_lca_lma_lcb_lmb_lcr   
domain_quor   r  s
          r9   term_divz'PolyElement._term_div.<locals>.term_div  sX    &
d&
d2:: EE(Lt44E$ **T4"8"8884r;   c                p    | \  }}|\  }}|	k    r|}n ||          }|||z  s| ||          fS d S rd   r@   r  s
          r9   r&  z'PolyElement._term_div.<locals>.term_div  s_    &
d&
d2:: EE(Lt44E **T4"8"8884r;   )r:   r   r7   r  r   rT  )r   r7   r&  r%  r   r  s      @@@r9   	_term_divzPolyElement._term_div  s    Y!!Z
y-? 	 
  
  
  
  
  
  
  
 
  
  
  
  
  
  
  r;   c                   | j         d}t          |t                    rd}|g}t          |          st	          d          | s|rj        j        fS g j        fS |D ]}|j         k    rt          d          t          |          }fdt          |          D             }| 	                                }j        }| 
                                }d |D             }	|rd}
d}|
|k     r|dk    r|                                } ||||         f|	|
         ||
         |	|
                  f          }|G|\  }}||
                             ||f          ||
<   |                    ||
         || f          }d	}n|
d	z  }
|
|k     r|dk    |s4|                                }|                    |||         f          }||= ||j        k    r||z  }|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                    g | ]	}j         
S r@   )r   rB   r   r:   s     r9   rD   z#PolyElement.div.<locals>.<listcomp>  s    ***Adi***r;   c                6    g | ]}|                                 S r@   )r   )rB   fxs     r9   rD   z#PolyElement.div.<locals>.<listcomp>  s"    000r""000r;   r   Nr   )r:   re   r   rl   r  r   r   r   r   r   r(  r   _iadd_monom_iadd_poly_monomr   )r   fv
ret_singler~   rh   qvr  rr&  expvsr   divoccurredr   termexpv1rP   r:   s                   @r9   r   zPolyElement.div  sh   8 y
b+&& 	JB2ww 	;#$9::: 	% %y$)++49}$ 	G 	GAv~~ !EFFF GG****q***IIKKI>>##00R000 	AKa%%K1,,~~''xqw%(BqE%(O1LMM##HE1qE--uaj99BqE**2a551"+>>A"#KKFA a%%K1,,  ((MM44/22dG!  	" 4?""FA 	  y!|#!uaxq5Lr;   c                   | }t          |t                    r|g}t          |          st          d          |j        }|j        }|j        }|j        }|j        }|                                }|j	        }	|
                                }|j        }
|r|D ]} ||	|j	                  }|j|\  }}|                                D ].\  }} |||          } |
||          ||z  z
  }|s||= )|||<   /|                                }|
|||         f}	 nC|	\  }}||v r||xx         |z  cc<   n|||<   ||= |                                }|
|||         f}	||S r  )re   r   rl   r  r:   r7   r   r   r(  LTr   r   rA  r   )r   Gr~   r:   r7   r   r   r3  r&  ltfr   gtqrO   rP   mgcgm1c1ltmltcs                        r9   r  zPolyElement.rem#  s   a%% 	A1vv 	;#$9:::v{(I;;==dFFHHe 	& & &Xc14((>DAq"#++-- ' 'B)\"a00 ST]]QrT1! ' !"$&AbEE..**C!1S6kE " S!88cFFFcMFFFF AcFcFnn&&?qv+C5  	&8 r;   c                8    |                      |          d         S Nr   )r   )r~   r:  s     r9   r  zPolyElement.quoP  s    uuQxx{r;   c                Z    |                      |          \  }}|s|S t          | |          rd   )r   r$   )r~   r:  qr3  s       r9   r  zPolyElement.exquoS  s2    uuQxx1 	,H%a+++r;   c                    | | j         j        v r|                                 }n| }|\  }}|                    |          }||||<   n||z  }|r|||<   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

        )r:   r   r   r   )r   mccpselfr   r   rP   s         r9   r.  zPolyElement._iadd_monom[  sz    8 49&&&YY[[FFFeJJt9 F4LLJA ! t4Lr;   c                &   | }||j         j        v r|                                }|\  }}|j        }|j         j        j        }|j         j        }|                                D ].\  }	}
 ||	|          } |||          |
|z  z   }|r|||<   +||= /|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

        )r:   r   r   r   r7   r   r   rS   )r   r`  rI  r_  rO   rP   r   r   r   rQ  rR  kar   s                r9   r/  zPolyElement._iadd_poly_monom  s    * """BAfw~"w+HHJJ 	 	DAqa##BCDMMAaC'E 2rFF	r;   c                    | j                             |          | st          S dk     rdS t          f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  (   K   | ]}|         V  d S rd   r@   rB   r   r   s     r9   ri   z%PolyElement.degree.<locals>.<genexpr>  '      <<EuQx<<<<<<r;   )r:   r   r   r}   r  r~   xr   s     @r9   degreezPolyElement.degree  \     FLLOO 	=KUU1<<<<Q\\^^<<<<<<r;   c                    | st           f| j        j        z  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   r:   rq   r   rT   r}   rI   rW   r  r  s    r9   degreeszPolyElement.degrees  J      	?716<''S$sALLNN';"<"<==>>>r;   c                    | j                             |          | st          S dk     rdS t          fd|                                 D                       S )z
        The tail degree in ``x`` or the main variable.

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

        r   c              3  (   K   | ]}|         V  d S rd   r@   rO  s     r9   ri   z*PolyElement.tail_degree.<locals>.<genexpr>  rP  r;   )r:   r   r   minr  rQ  s     @r9   tail_degreezPolyElement.tail_degree  rT  r;   c                    | st           f| j        j        z  S t          t	          t
          t          t          |                                                                S )z
        A tuple containing tail degrees in all variables.

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

        )	r   r:   rq   r   rT   rZ  rI   rW   r  r  s    r9   tail_degreeszPolyElement.tail_degrees  rW  r;   c                >    | 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)r:   r   r   s    r9   r   zPolyElement.leading_expv  s'      	9))$///4r;   c                L    |                      || j        j        j                  S rd   )r   r:   r7   r   r   r   s     r9   
_get_coeffzPolyElement._get_coeff  s    xxdi.3444r;   c                v   |dk    r|                      | j        j                  S | j                            |          rit	          |                                          }t          |          dk    r5|d         \  }}|| j        j        j        k    r|                      |          S t          d|z            )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)
ra  r:   r   r   rI   rA  r   r7   r   r   )r   r   rG  r   r   s        r9   r   zPolyElement.coeff  s    4 a<<??49#7888Y!!'** 	2**,,--E5zzQ$QxuDI,000??51116@AAAr;   c                @    |                      | j        j                  S )z"Returns the constant coefficient. )ra  r:   r   r   s    r9   re  zPolyElement.const   s    ty3444r;   c                P    |                      |                                           S rd   )ra  r   r   s    r9   r  zPolyElement.LC$  s     t0022333r;   c                J    |                                  }|| j        j        S |S rd   )r   r:   r   r`  s     r9   LMzPolyElement.LM(  s(      ""<9''Kr;   c                r    | 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

        )r:   r   r   r7   r   r   r  r   s      r9   leading_monomzPolyElement.leading_monom0  s<     IN  "" 	+i&*AdGr;   c                    |                                  }|| j        j        | j        j        j        fS ||                     |          fS rd   )r   r:   r   r7   r   ra  r`  s     r9   r9  zPolyElement.LTE  sG      ""<I($)*:*?@@$//$//00r;   c                `    | j         j        }|                                 }|| |         ||<   |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

        )r:   r   r   rh  s      r9   leading_termzPolyElement.leading_termM  s6     IN  ""4jAdGr;   c                    | j         j        nt          j                  t          u rt          |d d          S t          |fdd          S )Nc                    | d         S rE  r@   )r   s    r9   rx   z%PolyElement._sorted.<locals>.<lambda>h  s
    q r;   T)r|   reversec                &     | d                   S rE  r@   )r   r0   s    r9   rx   z%PolyElement._sorted.<locals>.<lambda>j  s    uQx r;   )r:   r0   r   r   r    sorted)r   rb   r0   s     `r9   _sortedzPolyElement._sorteda  sd    =IOEE'..EC<<##9#94HHHH##@#@#@#@$OOOOr;   c                @    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                    g | ]\  }}|S r@   r@   )rB   _r   s      r9   rD   z&PolyElement.coeffs.<locals>.<listcomp>  s    :::81e:::r;   rG  r   r0   s     r9   r]   zPolyElement.coeffsl  $    0 ;:tzz%'8'8::::r;   c                @    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                    g | ]\  }}|S r@   r@   )rB   r   ru  s      r9   rD   z&PolyElement.monoms.<locals>.<listcomp>  s    :::85!:::r;   rv  rw  s     r9   monomszPolyElement.monoms  rx  r;   c                l    |                      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)]

        )rr  rI   rS   rw  s     r9   rG  zPolyElement.terms  s(    0 ||D..666r;   c                D    t          |                                           S )z,Iterator over coefficients of a polynomial. )iterrJ   r   s    r9   
itercoeffszPolyElement.itercoeffs      DKKMM"""r;   c                D    t          |                                           S )z)Iterator over monomials of a polynomial. )r~  rc  r   s    r9   r  zPolyElement.itermonoms      DIIKK   r;   c                D    t          |                                           S )z%Iterator over terms of a polynomial. )r~  rS   r   s    r9   rA  zPolyElement.iterterms      DJJLL!!!r;   c                D    t          |                                           S )z+Unordered list of polynomial coefficients. rH   r   s    r9   
listcoeffszPolyElement.listcoeffs  r  r;   c                D    t          |                                           S )z(Unordered list of polynomial monomials. )rI   rc  r   s    r9   
listmonomszPolyElement.listmonoms  r  r;   c                D    t          |                                           S )z$Unordered list of polynomial terms. r[  r   s    r9   	listtermszPolyElement.listterms  r  r;   c                    | | j         j        v r| |z  S |s|                                  dS | D ]}| |xx         |z  cc<   | 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)r:   r   clear)r  rP   r   s      r9   r  zPolyElement.imul_num  sb    4    Q3J 	GGIIIF 	 	CcFFFaKFFFFr;   c                    | j         j        }|j        }|j        }|                                 D ]} |||          }|S )z*Returns GCD of polynomial's coefficients. )r:   r7   r   r   r  )r~   r7   contr   r   s        r9   r  zPolyElement.content  sH    {j\\^^ 	$ 	$E3tU##DDr;   c                    |                                  }|| j        j        j        k    r|| fS ||                     |          fS )z,Returns content and a primitive polynomial. )r  r:   r7   r   r  )r~   r  s     r9   	primitivezPolyElement.primitive   sB    yy{{16=%%%!9Q\\$''''r;   c                >    | s| S |                      | j                  S )z5Divides all coefficients by the leading coefficient. )r  r  r  s    r9   moniczPolyElement.monic  s#     	&H<<%%%r;   c                    s| j         j        S fd|                                 D             }|                     |          S )Nc                $    g | ]\  }}||z  fS r@   r@   rB   r   r   rR  s      r9   rD   z*PolyElement.mul_ground.<locals>.<listcomp>  s&    FFF|ue5%'"FFFr;   )r:   r   rA  r   )r~   rR  rG  s    ` r9   r  zPolyElement.mul_ground  sF     	6;FFFFq{{}}FFFuuU||r;   c                    | j         j        fd|                                 D             }|                     |          S )Nc                2    g | ]\  }} |          |fS r@   r@   )rB   f_monomf_coeffr   r   s      r9   rD   z)PolyElement.mul_monom.<locals>.<listcomp>  s/    ]]]>Ngw<<//9]]]r;   )r:   r   rS   r   )r~   r   rG  r   s    ` @r9   	mul_monomzPolyElement.mul_monom  sG    v*]]]]]RSRYRYR[R[]]]uuU||r;   c                   |\  | rs| j         j        S | j         j        k    r|                               S | j         j        fd|                                 D             }|                     |          S )Nc                8    g | ]\  }} |          |z  fS r@   r@   )rB   r  r  r   r   r   s      r9   rD   z(PolyElement.mul_term.<locals>.<listcomp>#  s3    cccDTGW<<//?cccr;   )r:   r   r   r  r   rS   r   )r~   r6  rG  r   r   r   s      @@@r9   mul_termzPolyElement.mul_term  s    u 	' 	'6;af'''<<&&&v*ccccccXYX_X_XaXacccuuU||r;   c                (   | j         j        }st          d          | r|j        k    r| S |j        r)|j        fd|                                 D             }n fd|                                 D             }|                     |          S )Nr  c                2    g | ]\  }}| |          fS r@   r@   )rB   r   r   r  rR  s      r9   rD   z*PolyElement.quo_ground.<locals>.<listcomp>0  s,    PPPucc%mm,PPPr;   c                .    g | ]\  }}|z  
||z  fS r@   r@   r  s      r9   rD   z*PolyElement.quo_ground.<locals>.<listcomp>2  s2    ```leUTY\]T]`ueqj)```r;   )r:   r7   r  r   rT  r  rA  r   )r~   rR  r7   rG  r  s    `  @r9   r  zPolyElement.quo_ground&  s     	;#$9::: 	AOOH? 	a*CPPPPPPPPEE````akkmm```EuuU||r;   c                @   \  }}|st          d          | s| j        j        S || j        j        k    r|                     |          S |                                 fd|                                 D             }|                     d |D                       S )Nr  c                (    g | ]} |          S r@   r@   )rB   tr6  r&  s     r9   rD   z(PolyElement.quo_term.<locals>.<listcomp>B  s%    <<<((1d##<<<r;   c                    g | ]}||S rd   r@   )rB   r  s     r9   rD   z(PolyElement.quo_term.<locals>.<listcomp>C  s    :::Q1=q===r;   )r  r:   r   r   r  r(  rA  r   )r~   r6  r   r   rG  r&  s    `   @r9   quo_termzPolyElement.quo_term6  s    u 	'#$9::: 	'6;af'''<<&&&;;==<<<<<Q[[]]<<<uu::%:::;;;r;   c                J   | j         j        j        rGg }|                                 D ]/\  }}|z  }|dz  k    r|z
  }|                    ||f           0n fd|                                 D             }|                     |          }|                                 |S )Nr  c                $    g | ]\  }}||z  fS r@   r@   )rB   r   r   r  s      r9   rD   z,PolyElement.trunc_ground.<locals>.<listcomp>Q  s&    LLL\UEueai(LLLr;   )r:   r7   is_ZZrA  r   r   r\  )r~   r  rG  r   r   r   s    `    r9   trunc_groundzPolyElement.trunc_groundE  s    6= 	ME ! - -u	16>>!AIEeU^,,,,- MLLLQ[[]]LLLEuuU||r;   c                    | }|                                 }|                                 }|j        j                            ||          }|                    |          }|                    |          }|||fS rd   )r  r:   r7   r   r  )r   r<  r~   fcgcr   s         r9   extract_groundzPolyElement.extract_groundY  sh    YY[[YY[[fmB''LLLLAqyr;   c                    | s| j         j        j        S | j         j        j         |fd|                                 D                       S )Nc                &    g | ]} |          S r@   r@   )rB   r   
ground_abss     r9   rD   z%PolyElement._norm.<locals>.<listcomp>j  s#    NNNUzz%00NNNr;   )r:   r7   r   absr  )r~   	norm_funcr  s     @r9   _normzPolyElement._norme  sR     	P6=%%*J9NNNNallnnNNNOOOr;   c                6    |                      t                    S rd   )r  r}   r  s    r9   max_normzPolyElement.max_norml      wws||r;   c                6    |                      t                    S rd   )r  rU   r  s    r9   l1_normzPolyElement.l1_normo  r  r;   c                J   | j         }| gt          |          z   }dg|j        z  }|D ]G}|                                D ]0}t	          |          D ]\  }}t          ||         |          ||<   1Ht	          |          D ]\  }}	|	sd||<   t          |          }t          d |D                       r||fS g }
|D ]d}|j        }|	                                D ]1\  }}d t          ||          D             }||t          |          <   2|
                    |           e||
fS )Nr   r   c              3  "   K   | ]
}|d k    V  dS r  r@   )rB   rw   s     r9   ri   z&PolyElement.deflate.<locals>.<genexpr>  s&      !!!qAv!!!!!!r;   c                    g | ]
\  }}||z  S r@   r@   rB   r   r  s      r9   rD   z'PolyElement.deflate.<locals>.<listcomp>  s     444Aa1f444r;   )r:   rI   rq   r  r  r   r   rl   r   rA  rW   r   )r~   r:  r:   r_   Jr  r   r   rO   rw   HhIr   Ns                  r9   deflatezPolyElement.deflater  se   vd1ggC
N 	) 	)A ) )%e,, ) )DAq!a==AaDD)) aLL 	 	DAq !!HH!!q!!!!! 	e8O 	 	A	AKKMM $ $544Q444#%((HHQKKKK!tr;   c                    | j         j        }|                                 D ]1\  }}d t          ||          D             }||t	          |          <   2|S )Nc                    g | ]
\  }}||z  S r@   r@   r  s      r9   rD   z'PolyElement.inflate.<locals>.<listcomp>  s     ---$!Q!A#---r;   )r:   r   rA  rW   r   )r~   r  r   r  r   r  s         r9   inflatezPolyElement.inflate  sW    v{ 	# 	#HAu--#a))---A"DqNNr;   c                j   | }|j         j        }|j        sD|                                \  }}|                                \  }}|                    ||          }||z                      |                    |                    }|j        s|                    |          S |                                S rd   )	r:   r7   rT  r  r   r  r   r  r  )r   r<  r~   r7   r  r  rP   r  s           r9   r   zPolyElement.lcm  s     	#KKMMEBKKMMEB

2r""AqSIIaeeAhh 	<<??"7799r;   c                8    |                      |          d         S rE  )	cofactorsr~   r<  s     r9   r   zPolyElement.gcd  s    {{1~~a  r;   c                T   | s|s| j         j        }|||fS | s|                     |          \  }}}|||fS |s|                    |           \  }}}|||fS t          |           dk    r|                     |          \  }}}|||fS t          |          dk    r|                    |           \  }}}|||fS |                     |          \  }\  } }|                     |          \  }}}|                    |          |                    |          |                    |          fS r   )r:   r   	_gcd_zeror   
_gcd_monomr  _gcdr  )r~   r<  r   r  cffcfgr  s          r9   r  zPolyElement.cofactors  s0    	 	6;Dt## 	++a..KAsCc3; 	++a..KAsCc3;VVq[[,,q//KAsCc3;VVq[[,,q//KAsCc3;IIaLL	6AqffQii3		!ckk!nnckk!nn==r;   c                X    | j         j        | j         j        }}|j        r|||fS | || fS rd   )r:   r   r   r  )r~   r<  r   r   s       r9   r  zPolyElement._gcd_zero  s:    FJT 	"dC<2tcT>!r;   c                $   | j         }|j        j        }|j        j        |j        }|j        t          |                                           d         \  }}||c|                                D ]\  }} ||           ||          |                     fg          }	|                      |           |          fg          }
|                     fd|                                D                       }|	|
|fS )Nr   c                F    g | ]\  }} |           |          fS r@   r@   )rB   r>  r?  _cgcd_mgcd
ground_quor   s      r9   rD   z*PolyElement._gcd_monom.<locals>.<listcomp>  s:    ccc62rmmB..

2u0E0EFcccr;   )	r:   r7   r   r  r   r   rI   rA  r   )r~   r<  r:   
ground_gcdr   mfcfr>  r?  r  r  r  r  r  r  r   s               @@@@r9   r  zPolyElement._gcd_monom  s(   v[_
[_
(*akkmm$$Q'B2ukkmm 	* 	*FB L++EJub))EEEEE5>"##eemmB..

2u0E0EFGHHeecccccccUVU`U`UbUbcccdd#s{r;   c                    | j         }|j        j        r|                     |          S |j        j        r|                     |          S |                    | |          S rd   )r:   r7   is_QQ_gcd_QQr  _gcd_ZZdmp_inner_gcd)r~   r<  r:   s      r9   r  zPolyElement._gcd  sY    v; 	,99Q<<[ 	,99Q<<%%a+++r;   c                "    t          | |          S rd   r   r  s     r9   r  zPolyElement._gcd_ZZ  s    a||r;   c                   | }|j         }|                    |j                                                  }|                                \  }}|                                \  }}|                    |          }|                    |          }|                    |          \  }}}	|                    |          }|j        |                                }}
|                    |          	                    |j        
                    |
|                    }|	                    |          	                    |j        
                    |
|                    }	|||	fS )Nr	  )r:   r   r7   rV  rY  rH  r  r  r  r  r  )r   r<  r~   r:   rF  r  r?  r  r  r  rP   s              r9   r  zPolyElement._gcd_QQ  s   v::T[%9%9%;%;:<<  A  AJJx  JJx  iill3JJttQWWYY1ll4  ++DKOOAr,B,BCCll4  ++DKOOAr,B,BCC#s{r;   c                d   | }|j         }|s	||j        fS |j        }|j        r|j        s|                    |          \  }}}n|                    |                                          }|                                \  }	}|                                \  }
}|	                    |          }|	                    |          }|                    |          \  }}}|j                            |
|	          \  }}
}	|	                    |          }|	                    |          }|
                    |
          }|
                    |	          }|                                }||j        k    rn=||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	  )r:   r   r7   rT  rU  r  r   rV  rY  rH  r  canonical_unit)r   r<  r~   r:   r7   ru  r  rG  rF  cqcpus               r9   cancelzPolyElement.cancel  s    v 	dh; 	!F$9 	!kk!nnGAq!!zz):):z;;HNN$$EBNN$$EB

8$$A

8$$Akk!nnGAq! 11"b99IAr2

4  A

4  AR  AR  A
 
??6:+2rqAAQAQA!tr;   c                N    | j         j        }|                    | j                  S rd   )r:   r7   r  r  )r~   r7   s     r9   r  zPolyElement.canonical_unit6	  s!    $$QT***r;   c                (   | j         }|                    |          }|                    |          }|j        }|                                 D ]D\  }}||         r7|                    ||          }|                    |||         z            ||<   E|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

        )r:   r   r   r   rA  r   r   )	r~   rR  r:   r   rO   r<  r   r   es	            r9   diffzPolyElement.diff:	  s     vJJqMM""I;;== 	6 	6KD%Aw 6&&tQ//uT!W}55!r;   c                   dt          |          cxk     r| j        j        k    r=n n:|                     t	          t          | j        j        |                              S t          d| j        j        dt          |                    )Nr   z expected at least 1 and at most z values, got )r   r:   rq   evaluaterI   rW   r5   r   )r~   rJ   s     r9   r2  zPolyElement.__call__S	  s    s6{{****afl*****::d3qv{F#;#;<<===*TUTZT`T`T`beflbmbmbmnooor;   c                   | }t          |t                    rU|S|d         |dd          c\  }}|                    |          }|s|S fd|D             }|                    |          S |j        }|                    |          }|j                            |          }|j        dk    r5|j        j        }|	                                D ]\  \  }}||||z  z  z  }|S |
                    |          j        }	|	                                D ]O\  }
}|
|         |
d |         |
|dz   d          z   }
}|||z  z  }|
|	v r||	|
         z   }|r||	|
<   D|	|
= H|r||	|
<   P|	S )Nr   r   c                D    g | ]\  }}|                               |fS r@   )r  )rB   Yrv   Xs      r9   rD   z(PolyElement.evaluate.<locals>.<listcomp>c	  s+    666!Qqvvayy!n666r;   )re   rI   r  r:   r   r7   r   rq   r   rA  r  )r   rR  rv   r~   r:   r   resultr+  r   r   r   r  s              @r9   r  zPolyElement.evaluateY	  s   a 	%19!aeIFQA

1a  A %66661666zz!}}$vJJqMMK"":??[%F {{}} % %e%1*$M99Q<<$D ! , ,u 8U2A2Yqstt%<5ad
D==!DK/E (&+U KK ,&+UKr;   c                f   | }t          |t                    r"| |D ]\  }}|                    ||          }|S |j        }|                    |          }|j                            |          }|j        dk    rH|j        j        }|	                                D ]\  \  }}	||	||z  z  z  }|
                    |          S |j        }
|	                                D ]R\  }}	||         |d |         dz   ||dz   d          z   }}|	||z  z  }	||
v r|	|
|         z   }	|	r|	|
|<   G|
|= K|	r|	|
|<   S|
S )Nr   rs   )re   rI   subsr:   r   r7   r   rq   r   rA  r   )r   rR  rv   r~   r  r:   r   r  r+  r   r   r   s               r9   r  zPolyElement.subs	  so   a 	19 ! !1FF1aLLHvJJqMMK"":??[%F {{}} % %e%1*$??6***9D ! , ,u 8U2A2Y%5acdd%C5ad
D==!DK/E (&+U KK ,&+UKr;   c                   |                                  }|j        j        }|s
|j        g fS fdt	          |          D             i fd}t          t	          |dz
                      }t          t	          |dd                    }j        }|rd\  }}}	t          |                                          D ]V\  }
\  }t          fd|D                       r3t          d t          |          D                       }||k    r||}	}}W|dk    r||	c}nng }t          dd	         d
z             D ]\  }}|                    ||z
             |                    t          |          |          z  }|}t          |          D ]\  }
}| ||
|          z  }||z  }|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                @    g | ]}                     |d z             S )r   )r,  r+  s     r9   rD   z*PolyElement.symmetrize.<locals>.<listcomp>	  s+    <<<a$$QqS))<<<r;   c                D    | |fvr|          |z  | |f<   | |f         S rd   r@   )r   r+  poly_powersr_   s     r9   get_poly_powerz.PolyElement.symmetrize.<locals>.get_poly_power	  s7    1v[((&+AhkQF#1v&&r;   r   r   r   )r   NNc              3  B   K   | ]}|         |d z            k    V  dS r  r@   )rB   r   r   s     r9   ri   z)PolyElement.symmetrize.<locals>.<genexpr>	  s4      AAAuQx5Q</AAAAAAr;   c              3  &   K   | ]\  }}||z  V  d S rd   r@   )rB   r+  rO   s      r9   ri   z)PolyElement.symmetrize.<locals>.<genexpr> 
  s*       E EA1 E E E E E Er;   Nrs   )r   r:   rq   r   r   rI   r  rG  rl   r}   rW   r   r   r   r5   )r   r~   r+  r  r  weights	symmetric_height_monom_coeffr   r   height	exponentsr@  m2productr   r   r  r_   r:   s                     @@@@r9   
symmetrizezPolyElement.symmetrize	  sM   v IIKKvJ 	$di##<<<<588<<<	' 	' 	' 	' 	' 	'
 uQU||$$uQ2''I	 	&4#GVV%.qwwyy%9%9 G G!>E5AAAAAAAAA G  E EWe1D1D E E EEEF''28%"}}%vuuIeU122Y%566 * *B  b))))uY'7'7???IG!),, 0 01>>!Q///LA1  	4 s49e,,--!W$$r;   c                   | j         }|j        }t          t          |j        t          |j                                      |||fg}npt          |t                    rt          |          }nKt          |t                    r't          |
                                fd          }nt          d          t          |          D ](\  }\  }}|         |                    |          f||<   )|                                 D ]d\  }}	t          |          }|j        }
|D ]\  }}||         dc}||<   |r|
||z  z  }
|
                    t#          |          |	f          }
||
z  }e|S )Nc                     | d                  S rE  r@   )rQ  gens_maps    r9   rx   z%PolyElement.compose.<locals>.<lambda>$
  s    x!~ r;   r{   z9expected a generator, value pair a sequence of such pairsr   )r:   r   rV   rW   r5   r   rq   re   rI   rq  rS   r   r  r   rA  r   r  r   )r~   rR  rv   r:   r   replacementsrQ  r<  r   r   subpolyr   r+  r  s                @r9   r&  zPolyElement.compose
  s   vyDIuTZ'8'899::=F8LL!T"" ^#AwwAt$$ ^%aggii5M5M5M5MNNN !\]]]"<00 	> 	>IAv1'{DMM!,<,<=LOOKKMM 
	 
	LE5KKEhG$ $ $1#Ah58 $q!tOG&&ee'<==GGODDr;   c                :   | }|j                             |          fd|                                D             }|s|j         j        S t	          | \  }}f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                6    g | ]\  }}|         k    ||fS r@   r@   )rB   rO   rP   degr   s      r9   rD   z)PolyElement.coeff_wrt.<locals>.<listcomp>e
  s*    AAADAqQqTS[[!Q[[[r;   c                F    g | ]}|d          dz   |dz   d          z   S )Nrs   r   r@   )rB   rO   r   s     r9   rD   z)PolyElement.coeff_wrt.<locals>.<listcomp>k
  s6    ;;;q!BQB%$,1q566*;;;r;   )r:   r   rA  r   rW   rX   rV   )r   rR  r  r  rG  r{  r]   r   s     `    @r9   	coeff_wrtzPolyElement.coeff_wrt9
  s    T FLLOOAAAAAAKKMMAAA 	6;e;;;;F;;;vS%8%8 9 9:::r;   c                   | }|j                             |          }|                    |          }|                    |          }|dk     rt          d          ||}}||k     r|S ||z
  dz   }|                    ||          }	|j         j        |         }
	 |                    ||          }||z
  |dz
  }}||	z  }||z  |
|z  z  }||z
  }|                    |          }||k     rnR|	|z  }||z  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 difference 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   r:   r   rS  r  r  r5   )r   r<  rR  r~   dfdgr3  drr  lc_gxplc_rr  Rr:  rP   s                   r9   premzPolyElement.premn
  s   l FLLOOXXa[[XXa[[66#$9:::2277HGaK{{1b!!V[^	;;q"%%D7AEqADAD2q5 AAA!BBww	 AI1ur;   c                $   | }|j                             |          }|                    |          }|                    |          }|dk     rt          d          |||}}}||k     r||fS ||z
  dz   }	|                    ||          }
|j         j        |         }	 |                    ||          }||z
  |	dz
  }	}||
z  }||||z  z  z   }||
z  }||z  ||z  z  }||z
  }|                    |          }||k     rnb|
|	z  }||z  }||z  }||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<  rR  r~   r  r  rG  r3  r  r  r  r  r  r  Qr  r:  rP   s                     r9   pdivzPolyElement.pdiv
  sK   ` FLLOOXXa[[XXa[[66#$9:::ab177a4KGaK{{1b!!V[^	;;q"%%D7AEqADAT2q5L ADAD2q5 AAA!BBww%	( !GEE!tr;   c                >    | }|                     ||          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   )r  )r   r<  rR  r~   s       r9   pquozPolyElement.pquoG  s    6 vva||Ar;   c                j    | }|                     ||          \  }}|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 difference 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

        )r  r  r$   )r   r<  rR  r~   rG  r3  s         r9   pexquozPolyElement.pexquoe  s=    : vva||19 	,H%a+++r;   c                   | }|j                             |          }|                    |          }|                    |          }||k     r||}}||}}|dk    rddgS |dk    r|dgS ||g}||z
  }d|dz   z  }|                    ||          }	|	|z  }	|                    ||          }
|
|z  }d|g}| }|	r|	                    |          }|                    |	           ||	|||z
  f\  }}}}|
 ||z  z  }|                    ||          }	|	                    |          }	|                    ||          }
|dk    r$|
 |z  }||dz
  z  }|                    |          }n|
 }|                    |            |	|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   )r:   r   rS  r  r  r   r  )r   r<  rR  r~   r+  rO   r  drw   r  lcrP   SrQ  r  rG  s                   r9   subresultantszPolyElement.subresultants  s   D FLLOOHHQKKHHQKKq55aqAaqA66q6M66q6MFEQUO FF1aLLE [[A!GFB 	AHHQKKKAq!a%JAq!Qa1fAq!A

AQ""B1uuSQJ!a%LGGAJJCHHaRLLL'  	* r;   c                8    | j                             | |          S rd   )r:   dmp_half_gcdexr  s     r9   
half_gcdexzPolyElement.half_gcdex  s    v$$Q***r;   c                8    | j                             | |          S rd   )r:   	dmp_gcdexr  s     r9   gcdexzPolyElement.gcdex      v1%%%r;   c                8    | j                             | |          S rd   )r:   dmp_resultantr  s     r9   	resultantzPolyElement.resultant  s    v##Aq)))r;   c                6    | j                             |           S rd   )r:   dmp_discriminantr  s    r9   discriminantzPolyElement.discriminant  s    v&&q)))r;   c                l    | j         j        r| j                             |           S t          d          )Nzpolynomial decomposition)r:   r  dup_decomposer%   r  s    r9   	decomposezPolyElement.decompose  s5    6 	J6''***-.HIIIr;   c                n    | j         j        r| j                             | |          S t          d          )Nzshift: use shift_list instead)r:   r  	dup_shiftr%   r~   rv   s     r9   shiftzPolyElement.shift  s7    6 	O6##Aq)))-.MNNNr;   c                8    | j                             | |          S rd   )r:   	dmp_shiftr6  s     r9   
shift_listzPolyElement.shift_list  r*  r;   c                l    | j         j        r| j                             |           S t          d          )Nzsturm sequence)r:   r  	dup_sturmr%   r  s    r9   sturmzPolyElement.sturm  s5    6 	@6##A&&&-.>???r;   c                6    | j                             |           S rd   )r:   dmp_gff_listr  s    r9   gff_listzPolyElement.gff_list      v""1%%%r;   c                6    | j                             |           S rd   )r:   dmp_normr  s    r9   normzPolyElement.norm  s    vq!!!r;   c                6    | j                             |           S rd   )r:   dmp_sqf_normr  s    r9   sqf_normzPolyElement.sqf_norm  rA  r;   c                6    | j                             |           S rd   )r:   dmp_sqf_partr  s    r9   sqf_partzPolyElement.sqf_part  rA  r;   Fc                :    | j                             | |          S )N)rl   )r:   dmp_sqf_list)r~   rl   s     r9   sqf_listzPolyElement.sqf_list  s    v""1#"...r;   c                6    | j                             |           S rd   )r:   dmp_factor_listr  s    r9   factor_listzPolyElement.factor_list  s    v%%a(((r;   rd   )F)r   r-  r.  r/  r6  r<  r   r?  r   r   r   r   rH  rK  rJ  rY  r\  r   r   rd  rh  rl  rn  rq  rs  ru  rx  r  r}  r!  r  r  rf   r1  r  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r(  r   r  r  r  r.  r/  rS  rV  r[  r]  r   ra  r   re  r  rf  ri  r9  rl  rr  r]   r{  rG  r  r  rA  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  r2  r  r  r  r&  r  r  r  r  r  r#  r&  r)  r-  r0  r3  r7  r:  r=  r@  rD  rG  rJ  rM  rP  __classcell__)r   s   @r9   r   r   J  sp
       ??    K K K/ / /% % %3 3 3 E	 	 	  8> > >	= 	= 	=M M M  "  4 4 44  G G G G0) ) )" " "        	2 	2 	2  *> > >  "H H H  . . .` + + X+ M M XM = = X=   X 5 5 X5 5 5 X5 8 8 X8 8 8 X8   X   X * * X* 1 1 X1 @ @ X@ @ @ X@ # # X#
 + + X+
 G G XGR R R  4 4 4l  (4 4 4l  82 2 2h  :4( 4( 4(l     :# # #J: : :,  % % %,  % % %,  % % %,       BJ J JX+ + +Z  , , ,* * *X# # #J= = = = 
? 
? 
?= = = = 
? 
? 
?  (5 5 5#B #B #BJ5 5 5 4 4 X4   X  * 1 1 X1  (	P 	P 	P; ; ; ;4; ; ; ;47 7 7 74# # #! ! !" " "# # #! ! !" " "! ! !F	 	 	( ( (& & &    

 
 
   < < <  $ J
 
 
P P P      B     ! ! !> > >," " "   , , ,    *6 6 6p+ + +  2p p p* * * *X% % % %Nk% k% k%Z   @3; 3; 3;jY Y Y Yv| | | ||   <#, #, #, #,JX X X Xz+ + +& & &* * ** * *J J JO O O& & &@ @ @& & &" " "& & && & &/ / / /) ) ) ) ) ) )r;   r   N)r0   r1   )Qr/  
__future__r   operatorr   r   r   r   r   r	   	functoolsr
   typesr   sympy.core.cacher   sympy.core.exprr   sympy.core.intfuncr   sympy.core.symbolr   r   rk   sympy.core.sympifyr   r   sympy.ntheory.multinomialr   sympy.polys.compatibilityr   sympy.polys.constructorr   sympy.polys.densebasicr   r   r   sympy.polys.domains.domainr   !sympy.polys.domains.domainelementr   "sympy.polys.domains.polynomialringr   sympy.polys.heuristicgcdr   sympy.polys.monomialsr   sympy.polys.orderingsr    r!   sympy.polys.polyerrorsr"   r#   r$   r%   sympy.polys.polyoptionsr   r&   r   r'   sympy.polys.polyutilsr(   r)   r*   sympy.printing.defaultsr+   sympy.utilitiesr,   r-   sympy.utilities.iterablesr.   sympy.utilities.magicr/   r:   r=   rE   r`   rn   r4   rV   r   r@   r;   r9   <module>rl     s     " " " " " " - - - - - - - - - - - - - - - -             $ $ $ $ $ $             # # # # # # 9 9 9 9 9 9 9 9 3 3 3 3 3 3 3 3 > > > > > > , , , , , , 4 4 4 4 4 4 C C C C C C C C C C - - - - - - ; ; ; ; ; ; = = = = = = + + + + + + - - - - - - 4 4 4 4 4 4 4 46 6 6 6 6 6 6 6 6 6 6 6G G G G G G G G G G= = = = = = = = = = 3 3 3 3 3 3 + + + + + + + + 1 1 1 1 1 1 ) ) ) ) ) )58 ! ! ! ! !< !$    < !$    < 2 2 2h@ @ @C C C C C C C CLN') N') N') N') N')-+t N') N') N') N') N')r;   