
    wgF                      U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z6m7Z8m9Z9 ddl:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZAmBZB ddlCmDZD ddlEmFZF eAe.fd       ZGeAe.fd       ZHeAe.fd       ZIeAd        ZJd ZKi ZLd eMd!<    G d" d#e?e      ZN G d$ d%e&e?eeO      ZPy&)'zSparse polynomial rings.     )annotations)Any)addmulltlegtge)reduce)GeneratorType)Expr)igcd)Symbolsymbols)CantSympifysympify)multinomial_coefficients)IPolys)construct_domain)ninfdmp_to_dictdmp_from_dict)DomainElementPolynomialRingheugcd)MonomialOps)lex)CoercionFailedGeneratorsErrorExactQuotientFailedMultivariatePolynomialError)DomainOrderbuild_options)expr_from_dict_dict_reorder_parallel_dict_from_expr)DefaultPrinting)publicsubsets)is_sequence)pollutec                <    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   domainorder_rings       V/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/rings.pyringr8   #   s#    8 Wfe,E8ejj      c                8    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'>

    r0   r3   s       r7   xringr;   B   s!    8 Wfe,E5::r9   c                    t        | ||      }t        |j                  D cg c]  }|j                   c}|j                         |S c c}w )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'>

    )r1   r.   r   namer2   )r   r4   r5   r6   syms        r7   vringr?   a   s=    6 Wfe,E%--13chh15::>L 2s   Ac                   d}t        |       s| gd}} t        t        t        |             } t	        ||      }t        | |      \  }}|j                  t        |D cg c]  }t        |j                                c}g       }t        ||      \  |_        }t        t        ||            }	|D 
cg c])  }|j                         D 
ci c]  \  }
}|
|	|    c}}
+ }}
}}t        |j                  |j                  |j                        }t        t        |j                   |            }|r||d   fS ||fS c c}w c c}}
w c c}}
}w )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'>

    FT)optr   )r-   listmapr   r&   r)   r4   sumvaluesr   dictzipitemsr1   r2   r5   	from_dict)exprsr   optionssinglerA   repsrepcoeffs
coeffs_dom	coeff_mapmcr6   polyss                 r7   sringrU      s&   4 FuvWe$%E

)C )4ID#
zzT;ctCJJL);R@!1&c!B
JVZ01	EIJJcSYY[9TQIaL9JJSXXszz3995EU__d+,EuQx  u~ <
 :Js    D=4EEEEc                    t        | t              r| rt        | d      S dS t        | t              r| fS t	        |       r1t        d | D              rt        |       S t        d | D              r| S t        d      )NT)seq c              3  <   K   | ]  }t        |t                y wN)
isinstancestr.0ss     r7   	<genexpr>z!_parse_symbols.<locals>.<genexpr>   s     3az!S!3   c              3  <   K   | ]  }t        |t                y wrZ   )r[   r   r]   s     r7   r`   z!_parse_symbols.<locals>.<genexpr>   s     6At$6ra   zbexpected a string, Symbol or expression or a non-empty sequence of strings, Symbols or expressions)r[   r\   _symbolsr   r-   allr!   r   s    r7   _parse_symbolsrf      sr    '3.5xT*=2=	GT	"z	W	3733G$$6g66N
~
r9   zdict[Any, Any]_ring_cachec                     e Zd ZdZefdZd Zd Zd Zd Z	d Z
d Zd%d
Zd Zed        Zed        Zd&dZd Zd Zd ZeZd&dZd&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"d Z#d  Z$d! Z%d" Z&d# Z'd$ Z(y	)'r1   z*Multivariate distributed polynomial ring. c                   t        t        |            }t        |      }t        j                  |      }t        j                        | j                  |||f}t        j                  |      }|L|j                  r,t        |      t        |j                        z  rt        d      t        j                  |       }||_        t!        |      |_        t%        dt&        fd|i      |_        ||_        ||_        ||_        |_        d|z  |_        |j3                         |_        t        |j4                        |_        |j0                  |j8                  fg|_        |rt=        |      }|j?                         |_         |jC                         |_"        |jG                         |_$        |jK                         |_&        |jO                         |_(        |jS                         |_*        |jW                         |_,        n5d }||_         ||_"        d |_$        ||_&        ||_(        ||_*        ||_,        tZ        u rt\        |_/        n
fd|_/        ta        |j                  |j4                        D ]<  \  }	}
tc        |	td              s|	jf                  }ti        ||      r0tk        |||
       > |t        |<   |S )Nz7polynomial ring and it's ground domain share generatorsPolyElementr8   r   c                     yNrX   rX   )abs     r7   <lambda>z"PolyRing.__new__.<locals>.<lambda>       r9   c                     yrm   rX   )rn   ro   rS   s      r7   rp   z"PolyRing.__new__.<locals>.<lambda>   rq   r9   c                    t        |       S )Nkey)max)fr5   s    r7   rp   z"PolyRing.__new__.<locals>.<lambda>   s    S-> r9   )6tuplerf   len	DomainOpt
preprocessOrderOpt__name__rg   getis_Compositesetr   r!   object__new___hash_tuplehash_hashtyperj   dtypengensr4   r5   
zero_monom_gensr2   	_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   rv   leading_expvrG   r[   r   r=   hasattrsetattr)clsr   r4   r5   r   r   objcodegenmonunitsymbol	generatorr=   s      `        r7   r   zPolyRing.__new__   s?   w/0G%%f-##E*||WeVUCook*;""s7|c&..6I'I%&_``..%C)CO[)CI][NVSMJCI!CKCICJCI!%ZCNyy{CHMCM45CH%e,#*;;= #*;;= &-nn&6#$+LLN!#*;;= #*;;= #*;;= )#* #* &8#$+!#* #* #*  |#& #> %(chh%? 6!	ff-!;;D"3-T956 (+K$
r9   c                    | j                   j                  }g }t        | j                        D ]5  }| j	                  |      }| j
                  }|||<   |j                  |       7 t        |      S )z(Return a list of polynomial generators. )r4   r   ranger   monomial_basiszeroappendrx   )selfr   r   iexpvpolys         r7   r   zPolyRing._gens  sf    kkootzz" 	A&&q)D99DDJLL		
 U|r9   c                H    | j                   | j                  | j                  fS rZ   )r   r4   r5   r   s    r7   __getnewargs__zPolyRing.__getnewargs__  s    dkk4::66r9   c                x    | j                   j                         }|d= |D ]  }|j                  d      s||=  |S )Nr   	monomial_)__dict__copy
startswith)r   stateru   s      r7   __getstate__zPolyRing.__getstate__  sE    ""$.! 	C~~k*#J	 r9   c                    | j                   S rZ   )r   r   s    r7   __hash__zPolyRing.__hash__   s    zzr9   c                    t        |t              xr] | j                  | j                  | j                  | j
                  f|j                  |j                  |j                  |j
                  fk(  S rZ   )r[   r1   r   r4   r   r5   r   others     r7   __eq__zPolyRing.__eq__#  sV    %* D\\4;;

DJJ?]]ELL%++u{{CD	Dr9   c                    | |k(   S rZ   rX   r   s     r7   __ne__zPolyRing.__ne__(  s    5=  r9   Nc                |    | j                  |xs | j                  |xs | j                  |xs | j                        S rZ   )	__class__r   r4   r5   )r   r   r4   r5   s       r7   clonezPolyRing.clone+  s3    ~~g5v7LeNaW[WaWabbr9   c                B    dg| j                   z  }d||<   t        |      S )zReturn the ith-basis element. r      )r   rx   )r   r   basiss      r7   r   zPolyRing.monomial_basis.  s$    DJJaU|r9   c                "    | j                         S rZ   )r   r   s    r7   r   zPolyRing.zero4  s    zz|r9   c                8    | j                  | j                        S rZ   )r   r   r   s    r7   r   zPolyRing.one8  s    zz$))$$r9   c                :    | j                   j                  ||      S rZ   )r4   convertr   elementorig_domains      r7   
domain_newzPolyRing.domain_new<  s    {{""7K88r9   c                :    | j                  | j                  |      S rZ   )term_newr   )r   coeffs     r7   
ground_newzPolyRing.ground_new?  s    }}T__e44r9   c                N    | j                  |      }| j                  }|r|||<   |S rZ   )r   r   )r   monomr   r   s       r7   r   zPolyRing.term_newB  s*    &yyDKr9   c                V   t        |t              rj| |j                  k(  r|S t        | j                  t              r4| j                  j                  |j                  k(  r| j                  |      S t        d      t        |t              rt        d      t        |t              r| j                  |      S t        |t              r	 | j                  |      S t        |t              r| j                  |      S | j                  |      S # t        $ r | j                  |      cY S w xY w)N
conversionparsing)r[   rj   r8   r4   r   r   NotImplementedErrorr\   rF   rI   rB   
from_terms
ValueError	from_listr   	from_exprr   r   s     r7   ring_newzPolyRing.ring_newI  s    g{+w||#DKK8T[[=M=MQXQ]Q]=]w//),77%%i00&>>'**&/w// &>>'**??7++  /~~g../s   D D('D(c                    | j                   }| j                  }|j                         D ]  \  }} |||      }|s|||<    |S rZ   )r   r   rH   )r   r   r   r   r   r   r   s          r7   rI   zPolyRing.from_dicta  sL    __
yy#MMO 	$LE5uk2E#U	$
 r9   c                8    | j                  t        |      |      S rZ   )rI   rF   r   s      r7   r   zPolyRing.from_termsl  s    ~~d7m[99r9   c                h    | j                  t        || j                  dz
  | j                              S Nr   )rI   r   r   r4   r   s     r7   r   zPolyRing.from_listo  s&    ~~k'4::a<MNNr9   c                T      j                    fd t        |            S )Nc           	        j                  |       }||S | j                  r-t        t        t	        t        | j                                    S | j                  r-t        t        t	        t        | j                                    S | j                         \  }}|j                  r|dkD  r |      t        |      z  S j                  j                  |             S r   )r~   is_Addr   r   rB   rC   argsis_Mulr   as_base_exp
is_Integerintr   r   )exprr   baseexp_rebuildr4   mappingr   s       r7   r   z(PolyRing._rebuild_expr.<locals>._rebuildu  s    D)I$  c4Hdii(@#ABBc4Hdii(@#ABB !,,.	c>>cAg#D>3s833??6>>$+?@@r9   )r4   r   )r   r   r   r   r4   s   ` `@@r7   _rebuild_exprzPolyRing._rebuild_exprr  s#    	A$ &&r9   c                    t        t        t        | j                  | j                                    }	 | j                  ||      }| j                  |      S # t        $ r t        d| d|      w xY w)Nz6expected an expression convertible to a polynomial in z, got )	rF   rB   rG   r   r2   r   r   r    r   )r   r   r   r   s       r7   r   zPolyRing.from_expr  sm    tCdii89:	'%%dG4D ==&&  	pcgimnoo	ps   A A2c                "   || j                   rd}|S d}|S t        |t              rD|}d|k  r|| j                   k  r	 |S | j                    |k  r|dk  r| dz
  }|S t        d|z        t        || j                        r	 | j
                  j                  |      }|S t        |t              r	 | j                  j                  |      }|S t        d|z        # t        $ r t        d|z        w xY w# t        $ r t        d|z        w xY w)z+Compute index of ``gen`` in ``self.gens``. r   r   zinvalid generator index: %szinvalid generator: %szEexpected a polynomial generator, an integer, a string or None, got %s)	r   r[   r   r   r   r2   indexr\   r   )r   genr   s      r7   r   zPolyRing.index  sD   ;zz2 / . - S!AAv!djj.$ # **!a2gBF   !!>!DEETZZ(@IIOOC(  S!@LL&&s+  dgjjkk  @ !83!>??@
  @ !83!>??@s   C 0C6 C36Dc                    t        t        | j                  |            }t        | j                        D cg c]  \  }}||vs| }}}|s| j
                  S | j                  |      S c c}}w )z,Remove specified generators from this ring. re   )r   rC   r   	enumerater   r4   r   )r   r2   indicesr   r_   r   s         r7   dropzPolyRing.drop  sb    c$**d+,"+DLL"9O$!QQg=MAOO;;::g:.. Ps   A,A,c                `    | j                   |   }|s| j                  S | j                  |      S )Nre   )r   r4   r   )r   ru   r   s      r7   __getitem__zPolyRing.__getitem__  s.    ,,s#;;::g:..r9   c                    | j                   j                  st        | j                   d      r&| j                  | j                   j                         S t	        d| j                   z        )Nr4   r4   z%s is not a composite domain)r4   r   r   r   r   r   s    r7   	to_groundzPolyRing.to_ground  sL    ;;##wt{{H'E::T[[%7%7:88;dkkIJJr9   c                    t        |       S rZ   r   r   s    r7   	to_domainzPolyRing.to_domain  s    d##r9   c                ^    ddl m}  || j                  | j                  | j                        S )Nr   )	FracField)sympy.polys.fieldsr   r   r4   r5   )r   r   s     r7   to_fieldzPolyRing.to_field  s     0t{{DJJ??r9   c                2    t        | j                        dk(  S r   ry   r2   r   s    r7   is_univariatezPolyRing.is_univariate  s    499~""r9   c                2    t        | j                        dkD  S r   r  r   s    r7   is_multivariatezPolyRing.is_multivariate  s    499~!!r9   c                ~    | 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       r7   r   zPolyRing.add  sJ    " II 	C36XTXXs^#S		 r9   c                ~    | 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       r7   r   zPolyRing.mul  sJ    " HH 	C36XTXXs^#S		 r9   c                P   t        t        | j                  |            }t        | j                        D cg c]  \  }}||vs| }}}t        | j
                        D cg c]  \  }}||vs| }}}|s| S | j                  | | j                  |       S c c}}w c c}}w )zd
        Remove specified generators from the ring and inject them into
        its domain.
        r   r4   )r   rC   r   r   r   r2   r   r   )r   r2   r   r   r_   r   r   s          r7   drop_to_groundzPolyRing.drop_to_ground  s    
 c$**d+,!*4<<!8MAAW<L1MM"+DII"6K3!7:JKKK::gidii6F:GG NKs   BB$B"1B"c                    | |k7  rRt        | j                        j                  t        |j                              }| j                  t	        |            S | S )z+Add the generators of ``other`` to ``self``re   r   r   unionr   rB   )r   r   symss      r7   composezPolyRing.compose  sE    5=t||$**3u}}+=>D::d4j:11Kr9   c                    t        | j                        j                  t        |            }| j                  t	        |            S )z9Add the elements of ``symbols`` as generators to ``self``re   r  )r   r   r  s      r7   add_genszPolyRing.add_gens&  s4    4<< &&s7|4zz$t*z--r9   c                   |dk  s|| j                   kD  rt        d|d| j                        |s| j                  S | j                  }t        t        | j                         t        |            D ]R  t        fdt        | j                         D              }|| j                  || j                  j                        z  }T |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  8   K   | ]  }t        |v         y wrZ   )r   )r^   r   r_   s     r7   r`   z*PolyRing.symmetric_poly.<locals>.<genexpr>7  s     Eac!q&kEs   )r   r   r2   r   r   r,   r   r   rx   r   r4   )r   nr   r   r_   s       @r7   symmetric_polyzPolyRing.symmetric_poly+  s    
 q5A

NZ[]a]f]fghh88O99DU4::.A7 >E53DEEeT[[__==> Kr9   )NNNrZ   ))r}   
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   __call__rI   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r  r  r  r  rX   r9   r7   r1   r1      s    4,/ ?B	7D
!c   % %95,, H	:O'.'>//K$@ # # " "66H.
r9   r1   c                  `   e 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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+        Z.ed,        Z/ed-        Z0d. Z1d/ Z2d0 Z3d1 Z4d2 Z5d3 Z6d4 Z7d5 Z8d6 Z9d7 Z:d8 Z;d9 Z<d: Z=d; Z>d< Z?d= Z@d> ZAd? ZBeAZCeBZDd@ ZEdA ZFdB ZGdC ZHdD ZIdE ZJdF ZKddGZLdH ZMddIZNdJ ZOdK ZPdL ZQdM ZRdN ZSedO        ZTedP        ZUdQ ZVedR        ZWdS ZXdT ZYddUZZddVZ[ddWZ\dX Z]dY Z^dZ Z_d[ Z`d\ Zad] Zbd^ Zcd_ Zdd` Zeda Zfdb Zgdc Zhdd Zide Zjdf Zkdg ZlelZmdh Zndi Zodj Zpdk Zqdl Zrdm Zsdn Ztdo Zudp Zvdq Zwdr Zxds Zydt Zzdu Z{dv Z|dw Z}dx Z~dy ZddzZdd{Zd| Zdd}Zd~ ZddZddZddZddZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zy)rj   z5Element of multivariate distributed polynomial ring. c                $    | j                  |      S rZ   )r   )r   inits     r7   newzPolyElement.new?  s    ~~d##r9   c                6    | j                   j                         S rZ   )r8   r   r   s    r7   parentzPolyElement.parentB  s    yy""$$r9   c                L    | j                   t        | j                               fS rZ   )r8   rB   	itertermsr   s    r7   r   zPolyElement.__getnewargs__E  s    		4 0122r9   Nc                    | j                   }|5t        | j                  t        | j	                               f      x| _         }|S rZ   )r   r   r8   	frozensetrH   )r   r   s     r7   r   zPolyElement.__hash__J  s<     

=!%tyy)DJJL2I&J!KKDJr9   c                $    | j                  |       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    r7   r   zPolyElement.copyU  s    4 xx~r9   c           	     v   | j                   |k(  r| S | j                   j                  |j                  k7  r`t        t        t	        | | j                   j                  |j                               }|j                  || j                   j                        S |j                  | | j                   j                        S rZ   )r8   r   rB   rG   r(   r   r4   rI   )r   new_ringtermss      r7   set_ringzPolyElement.set_ringq  s    99 KYY("2"22mD$))2C2CXEUEUVWXE&&udii.>.>??%%dDII,<,<==r9   c                   |s| j                   j                  }nPt        |      | j                   j                  k7  r.t	        d| j                   j                  dt        |            t        | j                         g| S )Nz"Wrong number of symbols, expected z got )r8   r   ry   r   r   r'   as_expr_dict)r   r   s     r7   as_exprzPolyElement.as_exprz  sf    ii''G\TYY__,#g,0 
 d//1<G<<r9   c                    | j                   j                  j                  }| j                         D ci c]  \  }}| ||       c}}S c c}}w rZ   )r8   r4   to_sympyr)  )r   r5  r   r   s       r7   r2  zPolyElement.as_expr_dict  s?    99##,,;?>>;KL<5%x&LLLs   Ac           	        | j                   j                  }|j                  r|j                  s|j                  | fS |j                         }|j                  }|j                  }|j                  }| j                         D ]  } || ||            } | j                  | j                         D cg c]  \  }}|||z  f c}}      }	||	fS c c}}w rZ   )r8   r4   is_Fieldhas_assoc_Ringr   get_ringr   denomrE   r%  rH   )
r   r4   ground_ringcommonr   r:  r   kvr   s
             r7   clear_denomszPolyElement.clear_denoms  s    !!f&;&;::t##oo'oo[[] 	/Eu.F	/ xxDJJLBDAq1ah-BCt| Cs   3C
c                R    t        | j                               D ]  \  }}|r	| |=  y)z+Eliminate monomials with zero coefficient. NrB   rH   )r   r=  r>  s      r7   
strip_zerozPolyElement.strip_zero  s*    & 	DAqG	r9   c                    |s|  S t        |t              r/|j                  | j                  k(  rt        j	                  | |      S t        |       dkD  ry| j                  | j                  j                        |k(  S )aP  Equality test for polynomials.

        Examples
        ========

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

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

        r   F)r[   rj   r8   rF   r   ry   r~   r   p1p2s     r7   r   zPolyElement.__eq__  sd    " 6MK(RWW-?;;r2&&Wq[66"'',,-33r9   c                    | |k(   S rZ   rX   rD  s     r7   r   zPolyElement.__ne__  s    8|r9   c                   | j                   }t        ||j                        rrt        | j	                               t        |j	                               k7  ry|j
                  j                  }| j	                         D ]  } || |   ||   |      r y yt        |       dkD  ry	 |j
                  j                  |      }|j
                  j                  | j                         ||      S # t        $ r Y yw xY w)z+Approximate equality test for polynomials. FTr   )r8   r[   r   r   keysr4   almosteqry   r   constr    )rE  rF  	tolerancer8   rJ  r=  s         r7   rJ  zPolyElement.almosteq  s    wwb$**%2779~RWWY/{{++HWWY !1r!ui8 ! Wq[G[[((, {{++BHHJIFF " s   %C+ +	C76C7c                8    t        |       | j                         fS rZ   )ry   r/  r   s    r7   sort_keyzPolyElement.sort_key  s    D	4::<((r9   c                    t        || j                  j                        r% || j                         |j                               S t        S rZ   )r[   r8   r   rN  NotImplemented)rE  rF  ops      r7   _cmpzPolyElement._cmp  s3    b"''--(bkkmR[[]33!!r9   c                .    | j                  |t              S rZ   )rR  r   rD  s     r7   __lt__zPolyElement.__lt__      wwr2r9   c                .    | j                  |t              S rZ   )rR  r   rD  s     r7   __le__zPolyElement.__le__  rU  r9   c                .    | j                  |t              S rZ   )rR  r	   rD  s     r7   __gt__zPolyElement.__gt__  rU  r9   c                .    | j                  |t              S rZ   )rR  r
   rD  s     r7   __ge__zPolyElement.__ge__  rU  r9   c                    | j                   }|j                  |      }|j                  dk(  r||j                  fS t	        |j
                        }||= ||j                  |      fS )Nr   re   )r8   r   r   r4   rB   r   r   r   r   r8   r   r   s        r7   _dropzPolyElement._drop  s\    yyJJsO::?dkk>!4<<(G
djjj111r9   c                b   | j                  |      \  }}| j                  j                  dk(  r+| j                  r| j	                  d      S t        d|z        |j                  }| j                         D ]7  \  }}||   dk(  rt        |      }||= ||t        |      <   +t        d|z         |S )Nr   zCannot drop %sr   )
r^  r8   r   	is_groundr   r   r   rH   rB   rx   )r   r   r   r8   r   r=  r>  Ks           r7   r   zPolyElement.drop  s    **S/499??a~~zz!}$ !1C!78899D

 =1Q419QA!%&DqN$%5%;<<= Kr9   c                    | j                   }|j                  |      }t        |j                        }||= ||j	                  |||         fS )Nr  )r8   r   rB   r   r   r]  s        r7   _drop_to_groundzPolyElement._drop_to_ground  sI    yyJJsOt||$AJ$**WT!W*===r9   c                   | j                   j                  dk(  rt        d      | j                  |      \  }}|j                  }|j
                  j                  d   }| j                         D ]T  \  }}|d | ||dz   d  z   }||vr|||   z  j                  |      ||<   3||xx   |||   z  j                  |      z  cc<   V |S )Nr   z$Cannot drop only generator to groundr   )	r8   r   r   rc  r   r4   r2   r)  
mul_ground)r   r   r   r8   r   r   r   mons           r7   r  zPolyElement.drop_to_ground  s    99??aCDD&&s+4yykkq! NN, 	?LE5)eAaCDk)C$ %(]66u=S	S	c58m77>>		? r9   c                r    t        | | j                  j                  dz
  | j                  j                        S r   )r   r8   r   r4   r   s    r7   to_densezPolyElement.to_dense   s(    T499??1#4dii6F6FGGr9   c                    t        |       S rZ   )rF   r   s    r7   to_dictzPolyElement.to_dict#  s    Dzr9   c                6   | s/|j                  | j                  j                  j                        S |d   }|d   }| j                  }|j                  }|j
                  }	|j                  }
g }| j                         D ]Y  \  }}|j                  j                  |      }|rdnd}|j                  |       ||
k(  r*|j                  |      }|rV|j                  d      rE|dd  }n?|r| }|| j                  j                  j                  k7  r|j                  ||d      }nd	}g }t        |	      D ]{  }||   }|s|j                  ||   |d      }|dk7  rA|t        |      k7  s|d
k  r|j                  ||d      }n|}|j                  |||fz         h|j                  d|z         } |r|g|z   }|j                  |j                  |             \ |d
   dv r(|j!                  d
      }|dk(  r|j#                  d
d       d	j                  |      S )NMulAtom -  + -r   T)strict r   Fz%s)ro  rn  )_printr8   r4   r   r   r   r   r/  is_negativer   r   r   parenthesizer   r   joinpopinsert)r   printer
precedenceexp_pattern
mul_symbolprec_mul	prec_atomr8   r   r   zmsexpvsr   r   negativesignscoeffsexpvr   r   r   sexpheads                          r7   r\   zPolyElement.str&  s   >>$))"2"2"7"788e$v&	yy,,

__::< 	2KD%{{..u5H$5%DMM$rz . 1 1# 6#ABZF"FEDII,,000$11%$1OFFE5\ 01g --gaj)D-Q!8c#h#'&33C53Q"LL~!=>LL/0 5(MM*//%01?	2@ !9&::a=Du}a%wwvr9   c                2    | | j                   j                  v S rZ   )r8   r   r   s    r7   is_generatorzPolyElement.is_generatorV  s    tyy****r9   c                \    |  xs( t        |       dk(  xr | j                  j                  | v S r   )ry   r8   r   r   s    r7   r`  zPolyElement.is_groundZ  s+    xLCINKtyy/C/Ct/KLr9   c                J    |  xs t        |       dk(  xr | j                  dk(  S r   )ry   LCr   s    r7   is_monomialzPolyElement.is_monomial^  s$    x<CIN;tww!|<r9   c                    t        |       dk  S r   )ry   r   s    r7   is_termzPolyElement.is_termb  s    4yA~r9   c                `    | j                   j                  j                  | j                        S rZ   )r8   r4   rt  r  r   s    r7   rt  zPolyElement.is_negativef  !    yy++DGG44r9   c                `    | j                   j                  j                  | j                        S rZ   )r8   r4   is_positiver  r   s    r7   r  zPolyElement.is_positivej  r  r9   c                `    | j                   j                  j                  | j                        S rZ   )r8   r4   is_nonnegativer  r   s    r7   r  zPolyElement.is_nonnegativen  !    yy..tww77r9   c                `    | j                   j                  j                  | j                        S rZ   )r8   r4   is_nonpositiver  r   s    r7   r  zPolyElement.is_nonpositiver  r  r9   c                    |  S rZ   rX   rw   s    r7   is_zerozPolyElement.is_zerov  s	    ur9   c                4    | | j                   j                  k(  S rZ   )r8   r   r  s    r7   is_onezPolyElement.is_onez  s    AFFJJr9   c                `    | j                   j                  j                  | j                        S rZ   )r8   r4   r  r  r  s    r7   is_moniczPolyElement.is_monic~  s    vv}}##ADD))r9   c                h    | j                   j                  j                  | j                               S rZ   )r8   r4   r  contentr  s    r7   is_primitivezPolyElement.is_primitive  s!    vv}}##AIIK00r9   c                B    t        d | j                         D              S )Nc              3  8   K   | ]  }t        |      d k    ywr   NrD   r^   r   s     r7   r`   z(PolyElement.is_linear.<locals>.<genexpr>       ?u3u:??   rd   
itermonomsr  s    r7   	is_linearzPolyElement.is_linear      ????r9   c                B    t        d | j                         D              S )Nc              3  8   K   | ]  }t        |      d k    yw)   Nr  r  s     r7   r`   z+PolyElement.is_quadratic.<locals>.<genexpr>  r  r  r  r  s    r7   is_quadraticzPolyElement.is_quadratic  r  r9   c                f    | j                   j                  sy| j                   j                  |       S NT)r8   r   	dmp_sqf_pr  s    r7   is_squarefreezPolyElement.is_squarefree  s%    vv||vv""r9   c                f    | j                   j                  sy| j                   j                  |       S r  )r8   r   dmp_irreducible_pr  s    r7   is_irreduciblezPolyElement.is_irreducible  s%    vv||vv''**r9   c                z    | j                   j                  r| j                   j                  |       S t        d      )Nzcyclotomic polynomial)r8   r  dup_cyclotomic_pr#   r  s    r7   is_cyclotomiczPolyElement.is_cyclotomic  s0    6666**1---.EFFr9   c                x    | j                  | j                         D cg c]
  \  }}|| f c}}      S c c}}w rZ   )r%  r)  )r   r   r   s      r7   __neg__zPolyElement.__neg__  s0    xxdnn>NPleU55&/PQQPs   6
c                    | S rZ   rX   r   s    r7   __pos__zPolyElement.__pos__  s    r9   c                   |s| j                         S | j                  }t        ||j                        rc| j                         }|j                  }|j
                  j                  }|j                         D ]  \  }} |||      |z   }|r|||<   ||=  |S t        |t              rt        |j
                  t              r$|j
                  j                  |j                  k(  rn^t        |j                  j
                  t              r4|j                  j
                  j                  |k(  r|j                  |       S t        S 	 |j                  |      }| j                         }|s|S |j                  }	|	| j                         vr|||	<   |S |||	    k(  r||	= |S ||	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   r8   r[   r   r~   r4   r   rH   rj   r   __radd__rP  r   r   rI  r    )
rE  rF  r8   r  r~   r   r=  r>  cp2r  s
             r7   __add__zPolyElement.__add__  s    779wwb$**%	A%%C;;##D
 14L1$AaD! HK($++~64;;;K;Krww;VBGGNNN;@S@SW[@[{{2&%%	//"%C 	AB"" H	 !B%<" H bESLEH  	"!!	"s   F, ,F>=F>c                   | j                         }|s|S | j                  }	 |j                  |      }|j                  }|| j	                         vr|||<   |S |||    k(  r||= |S ||xx   |z  cc<   |S # t
        $ r	 t        cY S w xY wrZ   )r   r8   r   r   rI  r    rP  )rE  r  r  r8   r  s        r7   r  zPolyElement.__radd__  s    GGIHww	"A B"" H	 2;" H bEQJEH  	"!!	"s   A5 5BBc                z   |s| j                         S | j                  }t        ||j                        rc| j                         }|j                  }|j
                  j                  }|j                         D ]  \  }} |||      |z
  }|r|||<   ||=  |S t        |t              rt        |j
                  t              r$|j
                  j                  |j                  k(  rn^t        |j                  j
                  t              r4|j                  j
                  j                  |k(  r|j                  |       S t        S 	 |j                  |      }| j                         }|j                  }|| j                         vr| ||<   |S |||   k(  r||= |S ||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   r8   r[   r   r~   r4   r   rH   rj   r   __rsub__rP  r   r   rI  r    )	rE  rF  r8   r  r~   r   r=  r>  r  s	            r7   __sub__zPolyElement.__sub__  s     779wwb$**%	A%%C;;##D
 14L1$AaD! HK($++~64;;;K;Krww;VBGGNNN;@S@SW[@[{{2&%%	$B 	AB"" H	 2;" H bERKEH  	"!!	"s   F( (F:9F:c                    | j                   }	 |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

        )r8   r   r   r    rP  )rE  r  r8   r  r   s        r7   r  zPolyElement.__rsub__'  sn     ww		"A 		A $d8)$$FAH  	"!!	"s   A AAc                   | j                   }|j                  }| r|s|S t        ||j                        r|j                  }|j
                  j                  }|j                  }t        |j                               }| j                         D ]*  \  }}	|D ]   \  }
} |||
      } |||      |	|z  z   ||<   " , |j                          |S t        |t              rt        |j
                  t              r$|j
                  j                   |j                   k(  rn^t        |j                   j
                  t              r4|j                   j
                  j                   |k(  r|j                  |       S t        S 	 |j                  |      }| j                         D ]  \  }}	|	|z  }|s|||<    |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

        )r8   r   r[   r   r~   r4   r   rB   rH   rB  rj   r   __rmul__rP  r   r    )rE  rF  r8   r  r~   r   r   p2itexp1v1exp2v2r   r>  s                 r7   __mul__zPolyElement.__mul__B  s     wwIIHDJJ'%%C;;##D,,L
#DHHJ 4b $ 4HD"&tT2C d^be3AcF44 LLNHK($++~64;;;K;Krww;VBGGNNN;@S@SW[@[{{2&%%		$B HHJ  brEAdG  H  	"!!	"s   8F0 0GGc                    | j                   j                  }|s|S 	 |j                   j                  |      }| j                         D ]  \  }}||z  }|s|||<    |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

        )r8   r   r   rH   r    rP  )rE  rF  r  r  r  r>  s         r7   r  zPolyElement.__rmul__t  s     GGLLH		""2&B HHJ  brEAdG  H  	"!!	"s   A A0/A0c                p   | j                   }|s| r|j                  S t        d      t        |       dk(  rut	        | j                               d   \  }}|j                  }||j                  j                  k(  r|||j                  ||      <   |S ||z  ||j                  ||      <   |S t        |      }|dk  rt        d      |dk(  r| j                         S |dk(  r| j                         S |dk(  r| | j                         z  S t        |       dk  r| j                  |      S | j                  |      S )a(  raise polynomial to power `n`

        Examples
        ========

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

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

        z0**0r   r   zNegative exponentr        )r8   r   r   ry   rB   rH   r   r4   r   r   r   square_pow_multinomial_pow_generic)r   r  r8   r   r   r  s         r7   __pow__zPolyElement.__pow__  s-    yyxx ((Y!^

-a0LE5		A'16$##E1-. H 27$##E1-.H Fq5011!V99;!V;;= !V%%Y!^((++$$Q''r9   c                    | j                   j                  }| }	 |dz  r||z  }|dz  }|s	 |S |j                         }|dz  }*)Nr   r  )r8   r   r  )r   r  r  rS   s       r7   r  zPolyElement._pow_generic  sX    IIMM1uaCQ
  
AQA r9   c                   t        t        |       |      j                         }| j                  j                  }| j                  j
                  }| j                         }| j                  j                  j                  }| j                  j                  }|D ]g  \  }}	|}
|	}t        ||      D ]  \  }\  }}|s ||
||      }
|||z  z  } t        |
      }|}|j                  ||      |z   }|r|||<   `||v se||= i |S rZ   )r   ry   rH   r8   r   r   r4   r   rG   rx   r~   )r   r  multinomialsr   r   r/  r   r   multinomialmultinomial_coeffproduct_monomproduct_coeffr   r   r   s                  r7   r  zPolyElement._pow_multinomial  s
   /D	1=CCE))33YY))


yy$$yy~~.: 	 *K*&M-M'*;'> 0#^eU$3M5#$NM!UCZ/M0
 -(E!EHHUD)E1E#U$K#	 & r9   c                0   | j                   }|j                  }|j                  }t        | j	                               }|j
                  j                  }|j                  }t        t        |            D ]?  }||   }| |   }	t        |      D ]%  }
||
   } |||      } |||      |	| |   z  z   ||<   ' A |j                  d      }|j                  }| j                         D ]   \  }} |||      } |||      |dz  z   ||<   " |j                          |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  )r8   r   r~   rB   rI  r4   r   r   ry   imul_numrH   rB  )r   r8   r  r~   rI  r   r   r   k1pkjk2r   r=  r>  s                  r7   r  zPolyElement.square  s    yyIIeeDIIK {{((s4y! 	6AaBbB1X 6!W"2r*S$"T"X+5#6	6 JJqMeeJJL 	)DAqa#BDMAqD(AbE	) 	
r9   c                l   | j                   }|st        d      t        ||j                        r| j	                  |      S t        |t
              rt        |j                  t              r$|j                  j                   |j                   k(  rn^t        |j                   j                  t              r4|j                   j                  j                   |k(  r|j                  |       S t        S 	 |j                  |      }| j                  |      | j                  |      fS # t        $ r	 t        cY S w xY wNpolynomial division)r8   ZeroDivisionErrorr[   r   r   rj   r4   r   __rdivmod__rP  r   
quo_ground
rem_groundr    rE  rF  r8   s      r7   
__divmod__zPolyElement.__divmod__  s    ww#$9::DJJ'66":K($++~64;;;K;Krww;VBGGNNN;@S@SW[@[~~b))%%	:$B MM"%r}}R'899  	"!!	"s   .D! !D32D3c                    t         S rZ   rP  rD  s     r7   r  zPolyElement.__rdivmod__(      r9   c                J   | j                   }|st        d      t        ||j                        r| j	                  |      S t        |t
              rt        |j                  t              r$|j                  j                   |j                   k(  rn^t        |j                   j                  t              r4|j                   j                  j                   |k(  r|j                  |       S t        S 	 |j                  |      }| j                  |      S # t        $ r	 t        cY S w xY wr  )r8   r  r[   r   remrj   r4   r   __rmod__rP  r   r  r    r  s      r7   __mod__zPolyElement.__mod__+  s    ww#$9::DJJ'66":K($++~64;;;K;Krww;VBGGNNN;@S@SW[@[{{2&%%	%$B ==$$  	"!!	"s   .D D"!D"c                    t         S rZ   r  rD  s     r7   r  zPolyElement.__rmod__A  r  r9   c                r   | j                   }|st        d      t        ||j                        r%|j                  r| |dz  z  S | j                  |      S t        |t              rt        |j                  t              r$|j                  j                   |j                   k(  rn^t        |j                   j                  t              r4|j                   j                  j                   |k(  r|j                  |       S t        S 	 |j                  |      }| j                  |      S # t        $ r	 t        cY S w xY w)Nr  r   )r8   r  r[   r   r  quorj   r4   r   __rtruediv__rP  r   r  r    r  s      r7   __truediv__zPolyElement.__truediv__D  s    ww#$9::DJJ'~~28}$vvbz!K($++~64;;;K;Krww;VBGGNNN;@S@SW[@[r**%%	%$B ==$$  	"!!	"s   D$ $D65D6c                    t         S rZ   r  rD  s     r7   r  zPolyElement.__rtruediv__]  r  r9   c                    | j                   j                  | j                   j                  }|j                  | j                   j                  |j
                  r	fd}|S fd}|S )Nc                V    | \  }}|\  }}|	k(  r|}n	 ||      }|| ||      fS y rZ   rX   
	a_lm_a_lc	b_lm_b_lca_lma_lcb_lmb_lcr   
domain_quor   r  s
          r7   term_divz'PolyElement._term_div.<locals>.term_divl  sH    &
d&
d2: E(t4E$ *T4"888r9   c                `    | \  }}|\  }}|	k(  r|}n	 ||      }|||z  s| ||      fS y rZ   rX   r  s
          r7   r  z'PolyElement._term_div.<locals>.term_divx  sM    &
d&
d2: E(t4E *T4"888r9   )r8   r   r4   r  r   r7  )r   r4   r  r  r   r  s      @@@r7   	_term_divzPolyElement._term_dive  sX    YY!!!!ZZ
yy--??
 0 
  r9   c                   | j                   }d}t        |t              rd}|g}t        |      st	        d      | s(|r|j
                  |j
                  fS g |j
                  fS |D ]  }|j                   |k7  st        d       t        |      }t        |      D cg c]  }|j
                   }}| j                         }|j
                  }	| j                         }
|D cg c]  }|j                          }}|rd}d}||k  r||dk(  rw|j                         } |
|||   f||   ||   ||      f      }|9|\  }}||   j                  ||f      ||<   |j                  ||   || f      }d}n|dz  }||k  r|dk(  rw|s)|j                         }|	j                  |||   f      }	||= |r|j                  k(  r|	|z  }	|r|s|j
                  |	fS |d   |	fS ||	fS c c}w c c}w )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 ringr   r   )r8   r[   rj   rd   r  r   r   ry   r   r   r  r   _iadd_monom_iadd_poly_monomr   )r   fvr8   
ret_singlerw   r_   r   qvr  rr  fxexpvsdivoccurredr   termexpv1rS   s                     r7   r   zPolyElement.div  s   8 yy
b+&JB2w#$9::yy$))++499}$ 	GAvv~ !EFF	G G!&q*Adii**IIKII>>#-/0r"00AKa%K1,~~'qw%(BqE%(O1LM##HE1qE--uaj9BqE**2a551"+>A"#KFA a%K1, (MM44/2dG! " 4??"FAyy!|#!uaxq5L= + 1s   G!"G&c                   | }t        |t              r|g}t        |      st        d      |j                  }|j
                  }|j                  }|j                  }|j                  }|j                         }|j                  }	|j                         }|j                  }
|r|D ]r  } ||	|j                        }||\  }}|j                         D ](  \  }} |||      } |
||      ||z  z
  }|s||= $|||<   * |j                         }||||   f}	 n9 |	\  }}||v r||xx   |z  cc<   n|||<   ||= |j                         }||||   f}	|r|S r  )r[   rj   rd   r  r8   r4   r   r   r  LTr   r~   r)  r   )r   Grw   r8   r4   r   r   r  r  ltfr~   gtqrR   rS   mgcgm1c1ltmltcs                        r7   r  zPolyElement.rem  sz   a%A1v#$9::vv{{((II;;=ddFFHee &c144(>DAq"#++- 'B)"a0 T]QrT1! !"$&AbE' ..*C!1S6k&" S!8cFcMF AcFcFnn&?qv+C5 8 r9   c                *    | j                  |      d   S Nr   )r   )rw   r  s     r7   r  zPolyElement.quo  s    uuQx{r9   c                J    | j                  |      \  }}|s|S t        | |      rZ   )r   r"   )rw   r  qr  s       r7   exquozPolyElement.exquo  s(    uuQx1H%a++r9   c                    | | j                   j                  v r| j                         }n| }|\  }}|j                  |      }||||<   |S ||z  }|r|||<   |S ||= |S )a  add to self the monomial coeff*x0**i0*x1**i1*...
        unless self is a generator -- then just return the sum of the two.

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

        Examples
        ========

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

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

        )r8   r   r   r~   )r   mccpselfr   r   rS   s         r7   r	  zPolyElement._iadd_monom
  s~    8 499&&&YY[FFeJJt9 F4L  JA t  4Lr9   c                ^   | }||j                   j                  v r|j                         }|\  }}|j                  }|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

        )r8   r   r   r~   r4   r   r   rH   )r   rF  r&  rE  rR   rS   r~   r   r   r=  r>  kar   s                r7   r
  zPolyElement._iadd_poly_monom6  s    * """BAffww~~""ww++HHJ 	DAqa#BDMAaC'E2rF	 	r9   c                    | j                   j                  |      | st        S dk  ryt        fd| j	                         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   | ]	  }|     y wrZ   rX   r^   r   r   s     r7   r`   z%PolyElement.degree.<locals>.<genexpr>i       <EuQx<   )r8   r   r   rv   r  rw   xr   s     @r7   degreezPolyElement.degree[  ?     FFLLOKU<Q\\^<<<r9   c                    | st         f| j                  j                  z  S t        t	        t
        t        t        | j                                            S )z
        A tuple containing leading degrees in all variables.

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

        )	r   r8   r   rx   rC   rv   rB   rG   r  r  s    r7   degreeszPolyElement.degreesk  >     7166<<''S$sALLN';"<=>>r9   c                    | j                   j                  |      | st        S dk  ryt        fd| j	                         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   | ]	  }|     y wrZ   rX   r,  s     r7   r`   z*PolyElement.tail_degree.<locals>.<genexpr>  r-  r.  )r8   r   r   minr  r/  s     @r7   tail_degreezPolyElement.tail_degreew  r2  r9   c                    | st         f| j                  j                  z  S t        t	        t
        t        t        | j                                            S )z
        A tuple containing tail degrees in all variables.

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

        )	r   r8   r   rx   rC   r8  rB   rG   r  r  s    r7   tail_degreeszPolyElement.tail_degrees  r5  r9   c                >    | r| j                   j                  |       S y)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)r8   r   r   s    r7   r   zPolyElement.leading_expv  s     99))$//r9   c                b    | j                  || j                  j                  j                        S rZ   )r~   r8   r4   r   r   r   s     r7   
_get_coeffzPolyElement._get_coeff  s#    xxdii..3344r9   c                x   |dk(  r%| j                  | j                  j                        S t        || j                  j                        rct        |j                               }t        |      dk(  r<|d   \  }}|| j                  j                  j                  k(  r| j                  |      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)r?  r8   r   r[   r   rB   r)  ry   r4   r   r   )r   r   r/  r   r   s        r7   r   zPolyElement.coeff  s    4 a<??499#7#7881**,-E5zQ$QxuDII,,000??5116@AAr9   c                L    | j                  | j                  j                        S )z"Returns the constant coefficient. )r?  r8   r   r   s    r7   rK  zPolyElement.const  s    tyy3344r9   c                @    | j                  | j                               S rZ   )r?  r   r   s    r7   r  zPolyElement.LC  s    t00233r9   c                V    | j                         }|| j                  j                  S |S rZ   )r   r8   r   r>  s     r7   LMzPolyElement.LM  s*      "<99'''Kr9   c                    | j                   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

        )r8   r   r   r4   r   r   r  r   s      r7   leading_monomzPolyElement.leading_monom  s@     IINN  "ii&&**AdGr9   c                    | j                         }|6| j                  j                  | j                  j                  j                  fS || j                  |      fS rZ   )r   r8   r   r4   r   r?  r>  s     r7   r  zPolyElement.LT  sN      "<II(($))*:*:*?*?@@$//$/00r9   c                f    | j                   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

        )r8   r   r   rF  s      r7   leading_termzPolyElement.leading_term  s7     IINN  "4jAdGr9   c                    | j                   j                  nt        j                        t        u rt        |d d      S t        |fdd      S )Nc                    | d   S r!  rX   )r   s    r7   rp   z%PolyElement._sorted.<locals>.<lambda>  s
    q r9   T)ru   reversec                     | d         S r!  rX   )r   r5   s    r7   rp   z%PolyElement._sorted.<locals>.<lambda>  s    uQx r9   )r8   r5   r|   r{   r   sorted)r   rW   r5   s     `r7   _sortedzPolyElement._sorted  sL    =IIOOE''.EC<##94HH##@$OOr9   c                V    | j                  |      D cg c]  \  }}|	 c}}S c c}}w )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]

        r/  )r   r5   _r   s       r7   rO   zPolyElement.coeffs  s%    0 (,zz%'8:81e:::   %c                V    | j                  |      D cg c]  \  }}|	 c}}S c c}}w )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)]

        rR  )r   r5   r   rS  s       r7   monomszPolyElement.monoms5  s%    0 (,zz%'8:85!:::rT  c                T    | j                  t        | j                               |      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)]

        )rP  rB   rH   )r   r5   s     r7   r/  zPolyElement.termsO  s     0 ||D.66r9   c                4    t        | j                               S )z,Iterator over coefficients of a polynomial. )iterrE   r   s    r7   
itercoeffszPolyElement.itercoeffsi      DKKM""r9   c                4    t        | j                               S )z)Iterator over monomials of a polynomial. )rY  rI  r   s    r7   r  zPolyElement.itermonomsm      DIIK  r9   c                4    t        | j                               S )z%Iterator over terms of a polynomial. )rY  rH   r   s    r7   r)  zPolyElement.itertermsq      DJJL!!r9   c                4    t        | j                               S )z+Unordered list of polynomial coefficients. )rB   rE   r   s    r7   
listcoeffszPolyElement.listcoeffsu  r[  r9   c                4    t        | j                               S )z(Unordered list of polynomial monomials. )rB   rI  r   s    r7   
listmonomszPolyElement.listmonomsy  r]  r9   c                4    t        | j                               S )z$Unordered list of polynomial terms. rA  r   s    r7   	listtermszPolyElement.listterms}  r_  r9   c                    | | j                   j                  v r| |z  S |s| j                          y| 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)r8   r   clear)r  rS   r   s      r7   r  zPolyElement.imul_num  sO    4    Q3JGGI 	CcFaKF	r9   c                    | j                   j                  }|j                  }|j                  }| j	                         D ]  } |||      } |S )z*Returns GCD of polynomial's coefficients. )r8   r4   r   r   rZ  )rw   r4   contr   r   s        r7   r  zPolyElement.content  sI    {{jj\\^ 	$EtU#D	$ r9   c                H    | j                         }|| j                  |      fS )z,Returns content and a primitive polynomial. )r  r  )rw   ri  s     r7   	primitivezPolyElement.primitive  s!    yy{Q\\$'''r9   c                @    | s| S | j                  | j                        S )z5Divides all coefficients by the leading coefficient. )r  r  r  s    r7   moniczPolyElement.monic  s    H<<%%r9   c                    |s| j                   j                  S | j                         D cg c]  \  }}|||z  f }}}| j                  |      S c c}}w rZ   )r8   r   r)  r%  )rw   r0  r   r   r/  s        r7   re  zPolyElement.mul_ground  sM    66;;78{{}F|ue5%'"FFuuU| Gs   Ac                    | j                   j                  }| j                         D cg c]  \  }} |||      |f }}}| j                  |      S c c}}w rZ   )r8   r   rH   r%  )rw   r   r   f_monomf_coeffr/  s         r7   	mul_monomzPolyElement.mul_monom  sR    vv**RSRYRYR[]>Ngw</9]]uuU| ^s   Ac                J   |\  }}| r|s| j                   j                  S || j                   j                  k(  r| j                  |      S | j                   j                  }| j                         D cg c]  \  }} |||      ||z  f }}}| j                  |      S c c}}w rZ   )r8   r   r   re  r   rH   r%  )rw   r  r   r   r   rp  rq  r/  s           r7   mul_termzPolyElement.mul_term  s    u66;;aff'''<<&&vv**XYX_X_XacDTGW</?ccuuU| ds   3Bc           	        | j                   j                  }|st        d      | r||j                  k(  r| S |j                  r8|j
                  }| j                         D cg c]  \  }}| |||      f }}}n-| j                         D cg c]  \  }}||z  r|||z  f }}}| j                  |      S c c}}w c c}}w r  )r8   r4   r  r   r7  r  r)  r%  )rw   r0  r4   r  r   r   r/  s          r7   r  zPolyElement.quo_ground  s    #$9::AOH??**CABPuc%m,PEP>?kkm`leUTY\]T]ueqj)`E`uuU|	 Q`s   "B9B?	B?c                j   |\  }}|st        d      | s| j                  j                  S || j                  j                  k(  r| j	                  |      S | j                         }| j                         D cg c]  } |||       }}| j                  |D cg c]  }||	 c}      S c c}w c c}w r  )r  r8   r   r   r  r  r)  r%  )rw   r  r   r   r  tr/  s          r7   quo_termzPolyElement.quo_term  s    u#$9::66;;aff'''<<&&;;=-.[[]<(1d#<<uu%:Q1=q:;; =:s   7B+B0 B0c                b   | j                   j                  j                  r@g }| j                         D ]*  \  }}||z  }||dz  kD  r||z
  }|j	                  ||f       , n'| j                         D cg c]  \  }}|||z  f }}}| j                  |      }|j                          |S c c}}w )Nr  )r8   r4   is_ZZr)  r   r%  rB  )rw   r  r/  r   r   r   s         r7   trunc_groundzPolyElement.trunc_ground  s    66==E ! -u	16>!AIEeU^,- >?[[]L\UEueai(LELuuU|	 Ms   4B+c                    | }|j                         }|j                         }|j                  j                  j                  ||      }|j	                  |      }|j	                  |      }|||fS rZ   )r  r8   r4   r   r  )r   r  rw   fcgcr   s         r7   extract_groundzPolyElement.extract_ground  s_    YY[YY[ffmmB'LLLLAqyr9   c                    | s | j                   j                  j                  S | j                   j                  j                  } || j	                         D cg c]
  } ||       c}      S c c}w rZ   )r8   r4   r   absrZ  )rw   	norm_func
ground_absr   s       r7   _normzPolyElement._norm  sP    66==%%%**JallnNUz%0NOONs   A-c                ,    | j                  t              S rZ   )r  rv   r  s    r7   max_normzPolyElement.max_norm      wws|r9   c                ,    | j                  t              S rZ   )r  rD   r  s    r7   l1_normzPolyElement.l1_norm  r  r9   c                @   | j                   }| gt        |      z   }dg|j                  z  }|D ]<  }|j                         D ]'  }t	        |      D ]  \  }}t        ||   |      ||<    ) > t	        |      D ]  \  }}	|	r	d||<    t        |      }t        d |D              r||fS g }
|D ]f  }|j                  }|j                         D ]4  \  }}t        ||      D cg c]
  \  }}||z   }}}||t        |      <   6 |
j                  |       h ||
fS c c}}w )Nr   r   c              3  &   K   | ]	  }|d k(    ywr  rX   )r^   ro   s     r7   r`   z&PolyElement.deflate.<locals>.<genexpr>0  s     !!qAv!s   )r8   rB   r   r  r   r   rx   rd   r   r)  rG   r   )rw   r  r8   rT   Jr  r   r   rR   ro   HhIr   r  Ns                   r7   deflatezPolyElement.deflate  s@   vvd1gC

N 	)A )%e, )DAq!a=AaD))	)
 aL 	DAq!	 !H!q!!e8O 	A		AKKM $5),Q4Aa1f44#%($ HHQK	 !t 5s   !D
c                    | j                   j                  }| j                         D ]4  \  }}t        ||      D cg c]
  \  }}||z   }}}||t	        |      <   6 |S c c}}w rZ   )r8   r   r)  rG   rx   )rw   r  r   r  r   r   r  r  s           r7   inflatezPolyElement.inflate@  sb    vv{{ 	#HAu"%a)-$!Q!A#-A-"DqN	#  .s   A c                Z   | }|j                   j                  }|j                  s8|j                         \  }}|j                         \  }}|j	                  ||      }||z  j                  |j                  |            }|j                  s|j                        S |j                         S rZ   )	r8   r4   r7  rk  r   r  r   re  rm  )r   r  rw   r4   r}  r~  rS   r  s           r7   r   zPolyElement.lcmI  s    KKMEBKKMEB

2r"AqSIIaeeAh<<?"779r9   c                *    | j                  |      d   S r!  )	cofactorsrw   r  s     r7   r   zPolyElement.gcdY  s    {{1~a  r9   c                   | s|s| j                   j                  }|||fS | s| j                  |      \  }}}|||fS |s|j                  |       \  }}}|||fS t        |       dk(  r| j	                  |      \  }}}|||fS t        |      dk(  r|j	                  |       \  }}}|||fS | j                  |      \  }\  } }| j                  |      \  }}}|j                  |      |j                  |      |j                  |      fS r   )r8   r   	_gcd_zerory   
_gcd_monomr  _gcdr  )rw   r  r   r  cffcfgr  s          r7   r  zPolyElement.cofactors\  s   66;;Dt##++a.KAsCc3;++a.KAsCc3;Vq[,,q/KAsCc3;Vq[,,q/KAsCc3;IIaL	6AqffQi3		!ckk!nckk!n==r9   c                    | j                   j                  | j                   j                  }}|j                  r|||fS | || fS rZ   )r8   r   r   r  )rw   r  r   r   s       r7   r  zPolyElement._gcd_zeror  s@    FFJJTdC<2tcT>!r9   c                B   | j                   }|j                  j                  }|j                  j                  }|j                  }|j
                  }t        | j                               d   \  }}||}
}	|j                         D ]  \  }} ||	|      }	 ||
|      }
 | j                  |	|
fg      }| j                   |||	       |||
      fg      }| j                  |j                         D cg c]  \  }} |||	       |||
      f c}}      }|||fS c c}}w r!  )	r8   r4   r   r  r   r   rB   r)  r%  )rw   r  r8   
ground_gcd
ground_quor   r   mfcf_mgcd_cgcdr  r  r  r  r  s                   r7   r  zPolyElement._gcd_monomy  s   vv[[__
[[__
((**akkm$Q'B2ukkm 	*FB +Eub)E	* EEE5>"#eemB.
2u0EFGHeeUVU`U`Ubc62rmB.
2u0EFcd#s{ ds   2D
c                    | j                   }|j                  j                  r| j                  |      S |j                  j                  r| j                  |      S |j                  | |      S rZ   )r8   r4   is_QQ_gcd_QQrz  _gcd_ZZdmp_inner_gcd)rw   r  r8   s      r7   r  zPolyElement._gcd  sT    vv;;99Q<[[99Q<%%a++r9   c                    t        | |      S rZ   r   r  s     r7   r  zPolyElement._gcd_ZZ  s    a|r9   c                x   | }|j                   }|j                  |j                  j                               }|j	                         \  }}|j	                         \  }}|j                  |      }|j                  |      }|j                  |      \  }}}	|j                  |      }|j                  |j                         }}
|j                  |      j                  |j                  j                  |
|            }|	j                  |      j                  |j                  j                  |
|            }	|||	fS )Nr   )r8   r   r4   r9  r?  r0  r  r  rm  re  r  )r   r  rw   r8   r.  r  r  r  r  r  rS   s              r7   r  zPolyElement._gcd_QQ  s    vv::T[[%9%9%;:< A AJJx JJx iil3JJtttQWWY1ll4 ++DKKOOAr,BCll4 ++DKKOOAr,BC#s{r9   c                B   | }|j                   }|s||j                  fS |j                  }|j                  r|j                  s|j                  |      \  }}}n|j                  |j                               }|j                         \  }	}|j                         \  }
}|j                  |      }|j                  |      }|j                  |      \  }}}|j                  j                  |
|	      \  }}
}	|j                  |      }|j                  |      }|j                  |
      }|j                  |	      }|j                         }||j                  k(  r||}}||fS ||j                   k(  r
| | }}||fS |j                  |      }|j                  |      }||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   )r8   r   r4   r7  r8  r  r   r9  r?  r0  re  canonical_unit)r   r  rw   r8   r4   rS  r  r#  r.  cqcpus               r7   cancelzPolyElement.cancel  s    vvdhh;F$9$9kk!nGAq!zz):z;HNN$EBNN$EB

8$A

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

4 A

4 AR AR A
 

?aqA !t 6::+2rqA
 !t QAQA!tr9   c                d    | j                   j                  }|j                  | j                        S rZ   )r8   r4   r  r  )rw   r4   s     r7   r  zPolyElement.canonical_unit  s$    $$QTT**r9   c                   | j                   }|j                  |      }|j                  |      }|j                  }| j	                         D ]7  \  }}||   s|j                  ||      }|j                  |||   z        ||<   9 |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

        )r8   r   r   r   r)  r   r   )	rw   r0  r8   r   rR   r  r   r   es	            r7   diffzPolyElement.diff  s     vvJJqM"II;;= 	6KD%Aw&&tQ/uT!W}5!	6 r9   c                $   dt        |      cxk  r| j                  j                  k  r;n n8| j                  t	        t        | j                  j                  |                  S t        d| j                  j                  dt        |            )Nr   z expected at least 1 and at most z values, got )ry   r8   r   evaluaterB   rG   r2   r   )rw   rE   s     r7   r!  zPolyElement.__call__ 	  sb    s6{*affll*::d3qvv{{F#;<==TUTZTZT`T`beflbmnoor9   c                   | }t        |t              r\|Z|d   |dd  c\  }}}|j                  ||      }|s|S |D cg c]  \  }}|j                  |      |f }}}|j                  |      S |j                  }|j                  |      }|j                  j                  |      }|j                  dk(  r=|j                  j                  }|j                         D ]  \  \  }	}
||
||	z  z  z  } |S |j                  |      j                  }|j                         D ]@  \  }}
||   |d | ||dz   d  z   }}	|
||	z  z  }
||v r|
||   z   }
|
r|
||<   5||= 9|
s<|
||<   B |S c c}}w )Nr   r   )r[   rB   r  r   r8   r   r4   r   r   r   r)  )r   r0  rn   rw   XYr8   r   resultr  r   r   r   s                r7   r  zPolyElement.evaluate	  s   a19!aeIFQA

1a A346!Qqvvay!n66zz!}$vvJJqMKK"::?[[%%F {{} %e%1*$% M99Q<$$D ! ,u 8U2AYqst%<5ad
D=!DK/E&+U K&+U, KA 7s   E#c                T   | }t        |t              r ||D ]  \  }}|j                  ||      } |S |j                  }|j	                  |      }|j
                  j                  |      }|j                  dk(  rL|j
                  j                  }|j                         D ]  \  \  }}	||	||z  z  z  } |j                  |      S |j                  }
|j                         D ]C  \  }}	||   |d | dz   ||dz   d  z   }}|	||z  z  }	||
v r|	|
|   z   }	|	r|	|
|<   8|
|= <|	s?|	|
|<   E |
S )Nr   rk   )r[   rB   subsr8   r   r4   r   r   r   r)  r   )r   r0  rn   rw   r  r8   r   r  r  r   r   r   s               r7   r  zPolyElement.subs2	  sO   a19 !1FF1aL!HvvJJqMKK"::?[[%%F {{} %e%1*$% ??6**99D ! ,u 8U2AY%5acd%C5ad
D=!DK/E&+U K&+U, Kr9   c                |   | j                         }|j                  }|j                  }|s||j                  g fS t	        |      D cg c]  }|j                  |dz          c}i fd}t        t	        |dz
              }t        t	        |dd            }|j                  }|rd\  }	}
}t        |j                               D ]E  \  }\  }t        fd|D              st        d t        |      D              }||	kD  s@||}}
}	G |	dk7  r|
|c}nnwg }t        dd d	z         D ]  \  }}|j                  ||z
          ||j                  t        |      |      z  }|}t        |      D ]  \  }}| |||      z  } ||z  }|rt        t        |j                              }|||fS c c}w )
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

        r   c                8    | |fvr|    |z  | |f<   | |f   S rZ   rX   )r   r  poly_powersrT   s     r7   get_poly_powerz.PolyElement.symmetrize.<locals>.get_poly_power	  s4    1v[(&+AhkQF#1v&&r9   r   r   )r   NNc              3  :   K   | ]  }|   |d z      k\    ywr  rX   )r^   r   r   s     r7   r`   z)PolyElement.symmetrize.<locals>.<genexpr>	  s"     AAuQx5Q</As   c              3  ,   K   | ]  \  }}||z    y wrZ   rX   )r^   r  rR   s      r7   r`   z)PolyElement.symmetrize.<locals>.<genexpr>	  s      EA1 Es   Nrk   )r   r8   r   r   r   r  rB   r   r/  rd   rv   rG   r   r   rx   r2   )r   rw   r8   r  r   r  r   weights	symmetric_height_monom_coeffr   height	exponentsr  m2productr   r   r  rT   s                      @@@r7   
symmetrizezPolyElement.symmetrizeY	  s   v IIKvvJJdii##388<a$$QqS)<	'
 uQU|$uQ2'II	&4#GVV%.qwwy%9 G!>E5AAA  EWe1D EEF'28%G "}%vuIeU12Y%56 *B  b)* uY'7??IG!), 01>!Q//0LA1 4 s499e,-!W$$S =s   
F9c                   | j                   }|j                  }t        t        |j                  t        |j                                    |||fg}nVt        |t              rt        |      }n:t        |t              rt        |j                         fd      }nt        d      t        |      D ]!  \  }\  }}|   |j                  |      f||<   # | j                         D ]]  \  }}	t        |      }|j                  }
|D ]  \  }}||   dc}||<   |s|
||z  z  }
 |
j!                  t#        |      |	f      }
||
z  }_ |S )Nc                    | d      S r!  rX   )r=  gens_maps    r7   rp   z%PolyElement.compose.<locals>.<lambda>	  s    x!~ r9   rt   z9expected a generator, value pair a sequence of such pairsr   )r8   r   rF   rG   r2   r   r   r[   rB   rO  rH   r   r   r   r)  r   rt  rx   )rw   r0  rn   r8   r   replacementsr=  r  r   r   subpolyr   r  r  s                @r7   r  zPolyElement.compose	  sD   vvyyDIIuTZZ'89:=F8L!T"#AwAt$%aggi5MN !\]]"<0 	>IAv1'{DMM!,<=LO	> KKM 
	LE5KEhhG$ $1#Ah58q!tOG$
 &&ee'<=GGOD
	 r9   c                   | }|j                   j                  |      }|j                         D cg c]  \  }}||   |k(  s||f }}}|s|j                   j                  S t	        | \  }}	|D cg c]  }|d| dz   ||dz   d z    }}|j                   j                  t        t	        ||	                  S c c}}w c c}w )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

        Nrk   r   )r8   r   r)  r   rG   rI   rF   )
r   r0  degr  r   rR   rS   r/  rV  rO   s
             r7   	coeff_wrtzPolyElement.coeff_wrt	  s    T FFLLO$%KKMADAqQqTS[!QAA66;;e4:;q!BQ%$,1q56*;;vvS%8 9:: B <s   B:B:2C c                   | }|j                   j                  |      }|j                  |      }|j                  |      }|dk  rt        d      ||}}||k  r|S ||z
  dz   }|j	                  ||      }	|j                   j
                  |   }
	 |j	                  ||      }||z
  |dz
  }}||	z  }||z  |
|z  z  }||z
  }|j                  |      }||k  rnI|	|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 differnce between prem and rem
        x**2 + x*y
        >>> f.prem(g, y) # generator is given
        0
        >>> f.prem(g, 1) # generator index is given
        0

        See Also
        ========

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

        r   r  r   r8   r   r1  r  r  r2   )r   r  r0  rw   dfdgr  drr  lc_gxplc_rr  Rr  rS   s                   r7   premzPolyElement.prem
  s   l FFLLOXXa[XXa[6#$9::227HGaK{{1b!VV[[^;;q"%D7AEqADAD2q5 AAA!BBw  AI1ur9   c                   | }|j                   j                  |      }|j                  |      }|j                  |      }|dk  rt        d      |||}}}||k  r||fS ||z
  dz   }	|j	                  ||      }
|j                   j
                  |   }	 |j	                  ||      }||z
  |	dz
  }	}||
z  }||||z  z  z   }||
z  }||z  ||z  z  }||z
  }|j                  |      }||k  rnY|
|	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  r0  rw   r  r  r#  r  r  r  r  r  r  r  Qr  r  rS   s                     r7   pdivzPolyElement.pdivv
  s8   ` FFLLOXXa[XXa[6#$9::ab17a4KGaK{{1b!VV[[^;;q"%D7AEqADAT2q5L ADAD2q5 AAA!BBw% ( !GEE!tr9   c                0    | }|j                  ||      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  r0  rw   s       r7   pquozPolyElement.pquo
  s    6 vva|Ar9   c                d    | }|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 differnce between pexquo and exquo
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2*x + 2*y does not divide x**2 + x*y
        >>> f.pexquo(h)
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2*x + 2 does not divide x**2 + x*y

        See Also
        ========

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

        )r  r  r"   )r   r  r0  rw   r#  r  s         r7   pexquozPolyElement.pexquo  s5    : vva|199H%a++r9   c                   | }|j                   j                  |      }|j                  |      }|j                  |      }||k  r||}}||}}|dk(  rddgS |dk(  r|dgS ||g}||z
  }d|dz   z  }|j                  ||      }	|	|z  }	|j	                  ||      }
|
|z  }d|g}| }|	r|	j                  |      }|j                  |	       ||	|||z
  f\  }}}}|
 ||z  z  }|j                  ||      }	|	j                  |      }	|j	                  ||      }
|dkD  r |
 |z  }||dz
  z  }|j                  |      }n|
 }|j                  |        |	r|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   )r8   r   r1  r  r  r   r$  )r   r  r0  rw   r  rR   r  dro   r  lcrS   Sr=  r  r#  s                   r7   subresultantszPolyElement.subresultants7  s   D FFLLOHHQKHHQKq5aqAaqA6q6M6q6MFEQUO FF1aLE [[A!GFBAHHQKAq!a%JAq!Qa1fAq!A
AQ"B1uSQJ!a%LGGAJCHHaRL' * r9   c                :    | j                   j                  | |      S rZ   )r8   dmp_half_gcdexr  s     r7   
half_gcdexzPolyElement.half_gcdex  s    vv$$Q**r9   c                :    | j                   j                  | |      S rZ   )r8   	dmp_gcdexr  s     r7   gcdexzPolyElement.gcdex      vv1%%r9   c                :    | j                   j                  | |      S rZ   )r8   dmp_resultantr  s     r7   	resultantzPolyElement.resultant  s    vv##Aq))r9   c                8    | j                   j                  |       S rZ   )r8   dmp_discriminantr  s    r7   discriminantzPolyElement.discriminant  s    vv&&q))r9   c                z    | j                   j                  r| j                   j                  |       S t        d      )Nzpolynomial decomposition)r8   r  dup_decomposer#   r  s    r7   	decomposezPolyElement.decompose  s0    6666''**-.HIIr9   c                |    | j                   j                  r| j                   j                  | |      S t        d      )Nzshift: use shift_list instead)r8   r  	dup_shiftr#   rw   rn   s     r7   shiftzPolyElement.shift  s2    6666##Aq))-.MNNr9   c                :    | j                   j                  | |      S rZ   )r8   	dmp_shiftr  s     r7   
shift_listzPolyElement.shift_list  r  r9   c                z    | j                   j                  r| j                   j                  |       S t        d      )Nzsturm sequence)r8   r  	dup_sturmr#   r  s    r7   sturmzPolyElement.sturm  s0    6666##A&&-.>??r9   c                8    | j                   j                  |       S rZ   )r8   dmp_gff_listr  s    r7   gff_listzPolyElement.gff_list      vv""1%%r9   c                8    | j                   j                  |       S rZ   )r8   dmp_normr  s    r7   normzPolyElement.norm  s    vvq!!r9   c                8    | j                   j                  |       S rZ   )r8   dmp_sqf_normr  s    r7   sqf_normzPolyElement.sqf_norm  r
  r9   c                8    | j                   j                  |       S rZ   )r8   dmp_sqf_partr  s    r7   sqf_partzPolyElement.sqf_part  r
  r9   c                <    | j                   j                  | |      S )N)rd   )r8   dmp_sqf_list)rw   rd   s     r7   sqf_listzPolyElement.sqf_list  s    vv""1#"..r9   c                8    | j                   j                  |       S rZ   )r8   dmp_factor_listr  s    r7   factor_listzPolyElement.factor_list  s    vv%%a((r9   rZ   )F)r}   r  r  r  r%  r'  r   r   r   r   r0  r3  r2  r?  rB  r   r   rJ  rN  rR  rT  rW  rY  r[  r^  r   rc  r  rh  rj  r\   r   r  r`  r  r  rt  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  __floordiv____rfloordiv__r  r   r  r  r$  r	  r
  r1  r4  r9  r;  r   r?  r   rK  r  rD  rG  r  rJ  rP  rO   rV  r/  rZ  r  r)  ra  rc  re  r  r  rk  rm  re  rr  rt  r  rx  r{  r  r  r  r  r  r  r  r   r   r  r  r  r  r  r  r  r  r  r!  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r	  r  r  r  r  r  rX   r9   r7   rj   rj   <  sF   ?$%3 E	8>	=M"44G0)"	2*>"H.` + + M M = =   5 5 5 5 8 8 8 8     * * 1 1 @ @ @ @ # #
 + +
 G GR4l(4l60d:.(` :"H:,%,%2 L MBJX+Z,*X#J= 
?= 
?(5#BJ5 4 4  * 1 1(	P;4;474#!"#!"!F	(
&

 <$ J
PB !>," ,*6p+2p*X%Nk%Z@3;jYv||<#,JXz+&**JO&@&"&&/)r9   rj   N)Qr  
__future__r   typingr   operatorr   r   r   r   r	   r
   	functoolsr   typesr   sympy.core.exprr   sympy.core.intfuncr   sympy.core.symbolr   r   rc   sympy.core.sympifyr   r   sympy.ntheory.multinomialr   sympy.polys.compatibilityr   sympy.polys.constructorr   sympy.polys.densebasicr   r   r   !sympy.polys.domains.domainelementr   "sympy.polys.domains.polynomialringr   sympy.polys.heuristicgcdr   sympy.polys.monomialsr   sympy.polys.orderingsr   sympy.polys.polyerrorsr    r!   r"   r#   sympy.polys.polyoptionsr$   rz   r%   r|   r&   sympy.polys.polyutilsr'   r(   r)   sympy.printing.defaultsr*   sympy.utilitiesr+   r,   sympy.utilities.iterablesr-   sympy.utilities.magicr.   r8   r;   r?   rU   rf   rg   __annotations__r1   rF   rj   rX   r9   r7   <module>r6     s     "  - -     # 9 3 > , 4 C C ; = + - %6 6G G= = 3 + 1 ) # ! !< !$  < !$  < 2 2h@ !^  u upI&)-+t I&)r9   