
~9\c        	   @  s  d  Z  d d l m Z m Z d d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z 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 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 l0 m1 Z1 d d l2 m3 Z3 d d l4 m5 Z5 d d l6 m7 Z7 m8 Z8 m9 Z9 m: Z: m; Z; m< Z< m= Z= m> Z> m? Z? m@ Z@ mA ZA d d lB mC ZC mD ZD mE ZE mF ZF mG ZG mH ZH d d lI mJ ZJ d d lK mL ZL d d lM mN ZN mO ZO mP ZP mQ ZQ d d l" ZR d d lS ZS d d lT mU ZU eP d e f d     Y ZV eP d eV f d     Y ZW eP d     ZX d!   ZY eP d"    ZZ d#   Z[ d$   Z\ eP d% d&   Z] eP d'    Z^ eP d(    Z_ eP d)    Z` eP d*    Za eP d+    Zb eP d,    Zc eP d-    Zd eP d.    Ze eP d/    Zf eP d0    Zg eP d1    Zh eP d2    Zi eP d3    Zj eP d4    Zk eP d5    Zl eP d6    Zm eP d7    Zn eP d8    Zo eP d9    Zp eP d:    Zq eP d;    Zr eP es d<   Zt eP d=    Zu eP es d>   Zv eP d?    Zw eP d@    Zx eP dA    Zy eP dB    Zz eP dC    Z{ eP dD    Z| eP dE    Z} eP dF    Z~ eP dG    Z eP dH    Z eP dI    Z eP dJ    Z dK   Z dL   Z dM   Z dN   Z dO   Z dP   Z dQ   Z dR   Z eP dS    Z eP dT    Z eP dU    Z eP dV    Z eP e es es es e e e dW   Z eP es es e e dX   Z eP es es dY   Z eP e dZ   Z eP d[ d\ e d]   Z eP d^    Z eP d_    Z eP d`    Z eP da    Z eP db    Z. eP dc    Z eP dd e f de     Y Z eP df    Z d S(g   s8   User-friendly public interface to polynomial functions. i(   t   print_functiont   division(	   t   St   Basict   Exprt   It   Integert   Addt   Mult   Dummyt   Tuple(   t   preorder_traversal(   t   iterablet   ranget   ordered(   t
   _sympifyit(   t
   Derivative(   t   _keep_coeff(   t
   Relational(   t   Symbol(   t   sympify(   t   BooleanAtom(   t   polyoptions(   t   construct_domain(   t   FFt   QQt   ZZ(   t   matrix_fglm(   t   groebner(   t   Monomial(   t   monomial_key(   t   DMP(   t   OperationNotSupportedt   DomainErrort   CoercionFailedt   UnificationFailedt   GeneratorsNeededt   PolynomialErrort   MultivariatePolynomialErrort   ExactQuotientFailedt   PolificationFailedt   ComputationFailedt   GeneratorsError(   t   basic_from_dictt
   _sort_genst   _unify_genst   _dict_reordert   _dict_from_exprt   _parallel_dict_from_expr(   t   together(   t   dup_isolate_real_roots_list(   t   groupt   siftt   publict
   filldedentN(   t   NoConvergencet   Polyc           B  s  e  Z d  Z d d g Z e Z e 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 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 d   Z d   Z d d d  Z! d   Z" d   Z# d   Z$ d   Z% d   Z& d    Z' d d!  Z( d"   Z) d#   Z* d$   Z+ d%   Z, d&   Z- d'   Z. d d(  Z/ d d)  Z0 d d*  Z1 d d+  Z2 d d,  Z3 d-   Z4 d.   Z5 d/   Z6 d0   Z7 d1   Z8 e9 e9 d2  Z: e9 d3  Z; d4   Z< d5   Z= d6   Z> e9 d7  Z? d8   Z@ d9   ZA d:   ZB d;   ZC d<   ZD d=   ZE d>   ZF d?   ZG d@   ZH dA   ZI dB   ZJ dC   ZK dD   ZL dE   ZM dF   ZN dG   ZO dH   ZP dI   ZQ e dJ  ZR e dK  ZS e dL  ZT e dM  ZU dN   ZV dO dP  ZW dQ   ZX dR   ZY dS   ZZ dT   Z[ d dU  Z\ dV   Z] d dW  Z^ dX   Z_ dY   Z` dZ e9 d[  Za d d\  Zb d d]  Zc d d^  Zd d d_  Ze d`   Zf da   Zg e9 db  Zh dc   Zi dd   Zj de   Zk ek Zl d e df  Zm dg   Zn e dh  Zo e di  Zp e dj  Zq dk   Zr dl   Zs e9 dm  Zt dn   Zu d do  Zv d dp  Zw dq   Zx dr   Zy ds   Zz dt   Z{ e du  Z| dv   Z} dw   Z~ dx   Z dy   Z dz   Z d{   Z e d|  Z d}   Z d~   Z d   Z d   Z e9 d  Z e9 d  Z d   Z d   Z e9 d d d e9 e9 d  Z d d e9 e9 d  Z d d d  Z e d  Z e e d  Z e e d  Z d d e d  Z d   Z d   Z e9 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 d   Z d   Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e d e  d    Z e Z e Z e d e  d    Z e d e  d    Z d   Z e Z e9 d  Z e9 d  Z d   Z RS(   s  
    Generic class for representing and operating on polynomial expressions.
    Subclasses Expr class.

    Examples
    ========

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

    Create a univariate polynomial:

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

    Create a univariate polynomial with specific domain:

    >>> from sympy import sqrt
    >>> Poly(x**2 + 2*x + sqrt(3), domain='R')
    Poly(1.0*x**2 + 2.0*x + 1.73205080756888, x, domain='RR')

    Create a multivariate polynomial:

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

    Create a univariate polynomial, where y is a constant:

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

    You can evaluate the above polynomial as a function of y:

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

    See Also
    ========

    sympy.core.expr.Expr

    t   rept   gensgn $@c         O  s   t  j | |  } d | k r- t d   n  t | d t rw t | t  r^ |  j | |  S|  j t	 |  |  Sn5 t
 |  } | j r |  j | |  S|  j | |  Sd S(   s:   Create a new polynomial instance out of something useful. t   orders&   'order' keyword is not implemented yett   excludeN(   t   optionst   build_optionst   NotImplementedErrorR   t   strt
   isinstancet   dictt
   _from_dictt
   _from_listt   listR   t   is_Polyt
   _from_polyt
   _from_expr(   t   clsR9   R:   t   argst   opt(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __new__k   s    	c         G  sy   t  | t  s" t d |   n2 | j t |  d k rT t d | | f   n  t j |   } | | _ | | _ | S(   s:   Construct :class:`Poly` instance from raw representation. s%   invalid polynomial representation: %si   s   invalid arguments: %s, %s(	   RA   R   R%   t   levt   lenR   RL   R9   R:   (   RI   R9   R:   t   obj(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   new   s    		c         O  s"   t  j | |  } |  j | |  S(   s(   Construct a polynomial from a ``dict``. (   R=   R>   RC   (   RI   R9   R:   RJ   RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt	   from_dict   s    c         O  s"   t  j | |  } |  j | |  S(   s(   Construct a polynomial from a ``list``. (   R=   R>   RD   (   RI   R9   R:   RJ   RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt	   from_list   s    c         O  s"   t  j | |  } |  j | |  S(   s*   Construct a polynomial from a polynomial. (   R=   R>   RG   (   RI   R9   R:   RJ   RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt	   from_poly   s    c         O  s"   t  j | |  } |  j | |  S(   s+   Construct a polynomial from an expression. (   R=   R>   RH   (   RI   R9   R:   RJ   RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt	   from_expr   s    c         C  s   | j  } | s t d   n  t |  d } | j } | d k r^ t | d | \ } } n0 x- | j   D] \ } } | j |  | | <qk W|  j t	 j
 | | |  |  S(   s(   Construct a polynomial from a ``dict``. s/   can't initialize from 'dict' without generatorsi   RK   N(   R:   R$   RN   t   domaint   NoneR   t   itemst   convertRP   R   RQ   (   RI   R9   RK   R:   t   levelRU   t   monomt   coeff(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRC      s    		c         C  s   | j  } | s t d   n! t |  d k r? t d   n  t |  d } | j } | d k r t | d | \ } } n t t | j	 |   } |  j
 t j | | |  |  S(   s(   Construct a polynomial from a ``list``. s/   can't initialize from 'list' without generatorsi   s#   'list' representation not supportedRK   N(   R:   R$   RN   R&   RU   RV   R   RE   t   mapRX   RP   R   RR   (   RI   R9   RK   R:   RY   RU   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRD      s    		c         C  s   |  | j  k r* |  j | j | j  } n  | j } | j } | j } | r | j | k r t | j  t |  k r |  j | j   |  S| j	 |   } n  d | k r | r | j
 |  } n | t k r | j   } n  | S(   s*   Construct a polynomial from a polynomial. RU   (   t	   __class__RP   R9   R:   t   fieldRU   t   setRH   t   as_exprt   reordert
   set_domaint   Truet   to_field(   RI   R9   RK   R:   R^   RU   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRG      s    			c         C  s%   t  | |  \ } } |  j | |  S(   s+   Construct a polynomial from an expression. (   R/   RC   (   RI   R9   RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRH      s    c         C  s   |  j  |  j f S(   s$   Allow SymPy to hash Poly instances. (   R9   R:   (   t   self(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _hashable_content   s    c         C  s   t  t |   j   S(   N(   t   superR8   t   __hash__(   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRh      s    c         C  sp   t    } |  j } xP t t |   D]< } x3 |  j   D]% } | | r8 | | | j O} Pq8 q8 Wq% W| |  j BS(   s  
        Free symbols of a polynomial expression.

        Examples
        ========

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

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

        (   R_   R:   R   RN   t   monomst   free_symbolst   free_symbols_in_domain(   Re   t   symbolsR:   t   iRZ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRj      s    		
c         C  sw   |  j  j t   } } | j rC xQ | j D] } | | j O} q) Wn0 | j rs x$ |  j   D] } | | j O} qY Wn  | S(   sj  
        Free symbols of the domain of ``self``.

        Examples
        ========

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

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

        (   R9   t   domR_   t   is_CompositeRl   Rj   t   is_EXt   coeffs(   Re   RU   Rl   t   genR[   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRk     s    		c         C  s   |  j    f S(   s   
        Don't mess up with the core.

        Examples
        ========

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

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

        (   R`   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRJ   2  s    c         C  s   |  j  d S(   s   
        Return the principal generator.

        Examples
        ========

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

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

        i    (   R:   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRr   C  s    c         C  s
   |  j    S(   s#   Get the ground domain of ``self``. (   t
   get_domain(   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRU   T  s    c         C  s.   |  j  |  j j |  j j |  j j  |  j  S(   s3   Return zero polynomial with ``self``'s properties. (   RP   R9   t   zeroRM   Rn   R:   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRt   Y  s    c         C  s.   |  j  |  j j |  j j |  j j  |  j  S(   s2   Return one polynomial with ``self``'s properties. (   RP   R9   t   oneRM   Rn   R:   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRu   ^  s    c         C  s.   |  j  |  j j |  j j |  j j  |  j  S(   s3   Return unit polynomial with ``self``'s properties. (   RP   R9   t   unitRM   Rn   R:   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRv   c  s    c         C  s1   |  j  |  \ } } } } | |  | |  f S(   s  
        Make ``f`` and ``g`` belong to the same domain.

        Examples
        ========

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

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

        >>> f
        Poly(1/2*x + 1, x, domain='QQ')
        >>> g
        Poly(2*x + 1, x, domain='ZZ')

        >>> F, G = f.unify(g)

        >>> F
        Poly(1/2*x + 1, x, domain='QQ')
        >>> G
        Poly(2*x + 1, x, domain='QQ')

        (   t   _unify(   t   ft   gt   _t   pert   Ft   G(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   unifyh  s    c           s  t  |  } | j s} y; |  j j |  j |  j |  j j |  j j j |   f SWq} t k
 ry t d |  | f   q} Xn  t |  j t	  rFt | j t	  rFt
 |  j | j  } |  j j j | j j |  t |  d } } |  j | k rt |  j j   |  j |  \ } } |  j j | k rXg  | D] } | j | |  j j  ^ q1} n  t	 t t t | |    | |  } n |  j j |  } | j | k r1t | j j   | j |  \ }	 }
 | j j | k rg  |
 D] } | j | | j j  ^ q}
 n  t	 t t t |	 |
    | |  } q\| j j |  } n t d |  | f   |  j   | | d    f d  } | | | | f S(   Ns   can't unify %s with %si   c           sH   | d  k	 r8 | |  | | d } | s8 | j |   Sn    j |  |  S(   Ni   (   RV   t   to_sympyRP   (   R9   Rn   R:   t   remove(   RI   (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR{     s
    (   R   RF   R9   Rn   R{   t
   from_sympyR"   R#   RA   R   R-   R:   R~   RN   R.   t   to_dictRX   RB   RE   t   zipR]   RV   (   Rx   Ry   R:   Rn   RM   t   f_monomst   f_coeffst   cR|   t   g_monomst   g_coeffsR}   R{   (    (   RI   s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRw     s6    	;$/!.*!.*		c         C  si   | d k r |  j } n  | d k	 rV | |  | | d } | sV |  j j j |  Sn  |  j j | |  S(   sb  
        Create a Poly out of the given representation.

        Examples
        ========

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

        >>> from sympy.polys.polyclasses import DMP

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

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

        i   N(   RV   R:   R9   Rn   R   R]   RP   (   Rx   R9   R:   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR{     s    c         C  s8   t  j |  j i | d 6 } |  j |  j j | j   S(   s    Set the ground domain of ``f``. RU   (   R=   R>   R:   R{   R9   RX   RU   (   Rx   RU   RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRb     s    c         C  s
   |  j  j S(   s    Get the ground domain of ``f``. (   R9   Rn   (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRs     s    c         C  s%   t  j j |  } |  j t |   S(   s   
        Set the modulus of ``f``.

        Examples
        ========

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

        >>> Poly(5*x**2 + 2*x - 1, x).set_modulus(2)
        Poly(x**2 + 1, x, modulus=2)

        (   R=   t   Modulust
   preprocessRb   R   (   Rx   t   modulus(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   set_modulus  s    c         C  s5   |  j    } | j r% t | j    St d   d S(   s   
        Get the modulus of ``f``.

        Examples
        ========

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

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

        s$   not a polynomial over a Galois fieldN(   Rs   t   is_FiniteFieldR   t   characteristicR%   (   Rx   RU   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   get_modulus  s    	c         C  si   | |  j  k rS | j r( |  j | |  Sy |  j | |  SWqS t k
 rO qS Xn  |  j   j | |  S(   s)   Internal implementation of :func:`subs`. (   R:   t	   is_numbert   evalt   replaceR%   R`   t   subs(   Rx   t   oldRP   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt
   _eval_subs  s    	c         C  sq   |  j  j   \ } } g  } x@ t t |  j   D]) } | | k r1 | j |  j |  q1 q1 W|  j | d | S(   s  
        Remove unnecessary generators from ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import a, b, c, d, x

        >>> Poly(a + x, a, b, c, d, x).exclude()
        Poly(a + x, a, x, domain='ZZ')

        R:   (   R9   R<   R   RN   R:   t   appendR{   (   Rx   t   JRP   R:   t   j(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR<     s    c         G  s   | d k r7 |  j r( |  j | } } q7 t d   n  | | k sR | |  j k rV |  S| |  j k r | |  j k r |  j   } | j s | | j k r t |  j  } | | | j	 |  <|  j
 |  j d | Sn  t d | | |  f   d S(   s  
        Replace ``x`` with ``y`` in generators list.

        Examples
        ========

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

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

        s(   syntax supported only in univariate caseR:   s   can't replace %s with %s in %sN(   RV   t   is_univariateRr   R%   R:   Rs   Ro   Rl   RE   t   indexR{   R9   (   Rx   t   xt   yt   _ignoreRn   R:   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   '  s    	c         O  s   t  j d |  } | s0 t |  j d | } n* t |  j  t |  k rZ t d   n  t t t t	 |  j
 j   |  j |      } |  j t | |  j
 j t |  d  d | S(   s  
        Efficiently apply new order of generators.

        Examples
        ========

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

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

        RK   s7   generators list can differ only up to order of elementsi   R:   (    (   R=   t   OptionsR,   R:   R_   R%   RB   RE   R   R.   R9   R   R{   R   Rn   RN   (   Rx   R:   RJ   RK   R9   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRa   J  s    0c         C  s   |  j  d t  } |  j |  } i  } xU | j   D]G \ } } t d   | |  D  rm t d |    n  | | | | <q4 W|  j | } |  j t j	 | t
 |  d |  j j  |  S(   s(  
        Remove dummy generators from ``f`` that are to the left of
        specified ``gen`` in the generators as ordered. When ``gen``
        is an integer, it refers to the generator located at that
        position within the tuple of generators of ``f``.

        Examples
        ========

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

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

        t   nativec         s  s   |  ] } | Vq d  S(   N(    (   t   .0Rm   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pys	   <genexpr>~  s    s   can't left trim %si   (   t   as_dictRc   t   _gen_to_levelRW   t   anyR%   R:   RP   R   RQ   RN   R9   Rn   (   Rx   Rr   R9   R   t   termsRZ   R[   R:   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   ltrimd  s    c         G  s   t    } x[ | D]S } y |  j j |  } Wn' t k
 rU t d |  | f   q X| j |  q WxG |  j   D]9 } x0 t |  D]" \ } } | | k r | r t Sq Wqt Wt	 S(   sJ  
        Return ``True`` if ``Poly(f, *gens)`` retains ground domain.

        Examples
        ========

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

        >>> Poly(x*y + 1, x, y, z).has_only_gens(x, y)
        True
        >>> Poly(x*y + z, x, y, z).has_only_gens(x, y)
        False

        s   %s doesn't have %s as generator(
   R_   R:   R   t
   ValueErrorR*   t   addRi   t	   enumeratet   FalseRc   (   Rx   R:   t   indicesRr   R   RZ   Rm   t   elt(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   has_only_gens  s    	c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s   
        Make the ground domain a ring.

        Examples
        ========

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

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

        t   to_ring(   t   hasattrR9   R   R    R{   (   Rx   t   result(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s   
        Make the ground domain a field.

        Examples
        ========

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

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

        Rd   (   R   R9   Rd   R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRd     s    c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s   
        Make the ground domain exact.

        Examples
        ========

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

        >>> Poly(x**2 + 1.0, x, domain=RR).to_exact()
        Poly(x**2 + 1, x, domain='QQ')

        t   to_exact(   R   R9   R   R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  sO   t  |  j d t  d | d |  j j p* d \ } } |  j | |  j d | S(   s  
        Recalculate the ground domain of a polynomial.

        Examples
        ========

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

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

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

        Rt   R^   t	   compositeRU   N(   R   R   Rc   RU   Ro   RV   RQ   R:   (   Rx   R^   Rn   R9   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   retract  s    !c         C  s   | d k r# d | | } } } n |  j |  } t |  t |  } } t |  j d  rx |  j j | | |  } n t |  d   |  j |  S(   s1   Take a continuous subsequence of terms of ``f``. i    t   sliceN(   RV   R   t   intR   R9   R   R    R{   (   Rx   R   t   mt   nR   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s5   g  |  j  j d |  D] } |  j  j j |  ^ q S(   sQ  
        Returns all non-zero coefficients from ``f`` in lex order.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x + 3, x).coeffs()
        [1, 2, 3]

        See Also
        ========
        all_coeffs
        coeff_monomial
        nth

        R;   (   R9   Rq   Rn   R   (   Rx   R;   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRq     s    c         C  s   |  j  j d |  S(   sU  
        Returns all non-zero monomials from ``f`` in lex order.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).monoms()
        [(2, 0), (1, 2), (1, 1), (0, 1)]

        See Also
        ========
        all_monoms

        R;   (   R9   Ri   (   Rx   R;   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRi   '  s    c         C  sA   g  |  j  j d |  D]' \ } } | |  j  j j |  f ^ q S(   sc  
        Returns all non-zero terms from ``f`` in lex order.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).terms()
        [((2, 0), 1), ((1, 2), 2), ((1, 1), 1), ((0, 1), 3)]

        See Also
        ========
        all_terms

        R;   (   R9   R   Rn   R   (   Rx   R;   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   ;  s    c         C  s/   g  |  j  j   D] } |  j  j j |  ^ q S(   s  
        Returns all coefficients from a univariate polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x - 1, x).all_coeffs()
        [1, 0, 2, -1]

        (   R9   t
   all_coeffsRn   R   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   O  s    c         C  s   |  j  j   S(   s?  
        Returns all monomials from a univariate polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x - 1, x).all_monoms()
        [(3,), (2,), (1,), (0,)]

        See Also
        ========
        all_terms

        (   R9   t
   all_monoms(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   _  s    c         C  s;   g  |  j  j   D]' \ } } | |  j  j j |  f ^ q S(   s  
        Returns all terms from a univariate polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x - 1, x).all_terms()
        [((3,), 1), ((2,), 0), ((1,), 2), ((0,), -1)]

        (   R9   t	   all_termsRn   R   (   Rx   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   s  s    c         O  s   i  } x |  j    D]q \ } } | | |  } t | t  rL | \ } } n | } | r | | k rq | | | <q t d |   q q W|  j | | p |  j |  S(   sh  
        Apply a function to all terms of ``f``.

        Examples
        ========

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

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

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

        s   %s monomial was generated twice(   R   RA   t   tupleR%   RQ   R:   (   Rx   t   funcR:   RJ   R   RZ   R[   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   termwise  s    c         C  s   t  |  j    S(   s   
        Returns the number of non-zero terms in ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x - 1).length()
        3

        (   RN   R   (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   length  s    c         C  s0   | r |  j  j d |  S|  j  j d |  Sd S(   s  
        Switch to a ``dict`` representation.

        Examples
        ========

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

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

        Rt   N(   R9   R   t   to_sympy_dict(   Rx   R   Rt   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s$   | r |  j  j   S|  j  j   Sd S(   s%   Switch to a ``list`` representation. N(   R9   t   to_listt   to_sympy_list(   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   as_list  s    c         G  s   | s |  j  } n t |  d k r t | d t  r | d } t |  j   } xd | j   D]S \ } } y | j |  } Wn' t k
 r t d |  | f   q] X| | | <q] Wn  t	 |  j
 j   |  S(   sr  
        Convert a Poly instance to an Expr instance.

        Examples
        ========

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

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

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

        i   i    s   %s doesn't have %s as generator(   R:   RN   RA   RB   RE   RW   R   R   R*   R+   R9   R   (   Rx   R:   t   mappingRr   t   valueR   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR`     s    %
c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s  
        Convert algebraic coefficients to rationals.

        Examples
        ========

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

        >>> Poly(x**2 + I*x + 1, x, extension=I).lift()
        Poly(x**4 + 3*x**2 + 1, x, domain='QQ')

        t   lift(   R   R9   R   R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  sL   t  |  j d  r* |  j j   \ } } n t |  d   | |  j |  f S(   s+  
        Reduce degree of ``f`` by mapping ``x_i**m`` to ``y_i``.

        Examples
        ========

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

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

        t   deflate(   R   R9   R   R    R{   (   Rx   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s   |  j  j } | j r |  S| j s5 t d |   n  t |  j  d  r_ |  j  j d |  } n t |  d   | r | j |  j	 } n |  j	 | j } |  j
 | |  S(   s  
        Inject ground domain generators into ``f``.

        Examples
        ========

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

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

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

        s   can't inject generators over %st   injectt   front(   R9   Rn   t   is_NumericalRF   R!   R   R   R    Rl   R:   RP   (   Rx   R   Rn   R   R:   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   !  s    		c         G  s   |  j  j } | j s( t d |   n  t |  } |  j |  | k r^ |  j | t } } n8 |  j | | k r |  j |  t } } n t d   | j	 |   } t
 |  j  d  r |  j  j | d | } n t |  d   |  j | |  S(   s  
        Eject selected generators into the ground domain.

        Examples
        ========

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

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

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

        s   can't eject generators over %ss'   can only eject front or back generatorst   ejectR   (   R9   Rn   R   R!   RN   R:   Rc   R   R?   R   R   R   R    RP   (   Rx   R:   Rn   t   kt   _gensR   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   F  s    		c         C  sL   t  |  j d  r* |  j j   \ } } n t |  d   | |  j |  f S(   s  
        Remove GCD of terms from the polynomial ``f``.

        Examples
        ========

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

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

        t	   terms_gcd(   R   R9   R   R    R{   (   Rx   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   p  s    c         C  sC   t  |  j d  r' |  j j |  } n t |  d   |  j |  S(   s   
        Add an element of the ground domain to ``f``.

        Examples
        ========

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

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

        t
   add_ground(   R   R9   R   R    R{   (   Rx   R[   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  sC   t  |  j d  r' |  j j |  } n t |  d   |  j |  S(   s   
        Subtract an element of the ground domain from ``f``.

        Examples
        ========

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

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

        t
   sub_ground(   R   R9   R   R    R{   (   Rx   R[   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  sC   t  |  j d  r' |  j j |  } n t |  d   |  j |  S(   s   
        Multiply ``f`` by a an element of the ground domain.

        Examples
        ========

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

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

        t
   mul_ground(   R   R9   R   R    R{   (   Rx   R[   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  sC   t  |  j d  r' |  j j |  } n t |  d   |  j |  S(   sO  
        Quotient of ``f`` by a an element of the ground domain.

        Examples
        ========

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

        >>> Poly(2*x + 4).quo_ground(2)
        Poly(x + 2, x, domain='ZZ')

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

        t
   quo_ground(   R   R9   R   R    R{   (   Rx   R[   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  sC   t  |  j d  r' |  j j |  } n t |  d   |  j |  S(   s  
        Exact quotient of ``f`` by a an element of the ground domain.

        Examples
        ========

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

        >>> Poly(2*x + 4).exquo_ground(2)
        Poly(x + 2, x, domain='ZZ')

        >>> Poly(2*x + 3).exquo_ground(2)
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2 does not divide 3 in ZZ

        t   exquo_ground(   R   R9   R   R    R{   (   Rx   R[   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s   
        Make all coefficients in ``f`` positive.

        Examples
        ========

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

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

        t   abs(   R   R9   R   R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s4  
        Negate all coefficients in ``f``.

        Examples
        ========

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

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

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

        t   neg(   R   R9   R   R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  sz   t  |  } | j s" |  j |  S|  j |  \ } } } } t |  j d  ra | j |  } n t |  d   | |  S(   s[  
        Add two polynomials ``f`` and ``g``.

        Examples
        ========

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

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

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

        R   (   R   RF   R   Rw   R   R9   R   R    (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   #  s    	c         C  sz   t  |  } | j s" |  j |  S|  j |  \ } } } } t |  j d  ra | j |  } n t |  d   | |  S(   s`  
        Subtract two polynomials ``f`` and ``g``.

        Examples
        ========

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

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

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

        t   sub(   R   RF   R   Rw   R   R9   R   R    (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   B  s    	c         C  sz   t  |  } | j s" |  j |  S|  j |  \ } } } } t |  j d  ra | j |  } n t |  d   | |  S(   sp  
        Multiply two polynomials ``f`` and ``g``.

        Examples
        ========

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

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

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

        t   mul(   R   RF   R   Rw   R   R9   R   R    (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   a  s    	c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s3  
        Square a polynomial ``f``.

        Examples
        ========

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

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

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

        t   sqr(   R   R9   R   R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  sO   t  |  } t |  j d  r3 |  j j |  } n t |  d   |  j |  S(   sX  
        Raise ``f`` to a non-negative power ``n``.

        Examples
        ========

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

        >>> Poly(x - 2, x).pow(3)
        Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')

        >>> Poly(x - 2, x)**3
        Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')

        t   pow(   R   R   R9   R   R    R{   (   Rx   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s
    c         C  sj   |  j  |  \ } } } } t |  j d  rE | j |  \ } } n t |  d   | |  | |  f S(   s#  
        Polynomial pseudo-division of ``f`` by ``g``.

        Examples
        ========

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

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

        t   pdiv(   Rw   R   R9   R   R    (   Rx   Ry   Rz   R{   R|   R}   t   qt   r(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s
    c         C  sX   |  j  |  \ } } } } t |  j d  r? | j |  } n t |  d   | |  S(   sN  
        Polynomial pseudo-remainder of ``f`` by ``g``.

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

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

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


        Examples
        ========

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

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

        t   prem(   Rw   R   R9   R   R    (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s
    c         C  sX   |  j  |  \ } } } } t |  j d  r? | j |  } n t |  d   | |  S(   s  
        Polynomial pseudo-quotient of ``f`` by ``g``.

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

        Examples
        ========

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

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

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

        t   pquo(   Rw   R   R9   R   R    (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s
    c         C  s   |  j  |  \ } } } } t |  j d  rw y | j |  } Wq t k
 rs } | j |  j   | j     q Xn t |  d   | |  S(   s  
        Polynomial exact pseudo-quotient of ``f`` by ``g``.

        Examples
        ========

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

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

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

        t   pexquo(   Rw   R   R9   R   R'   RP   R`   R    (   Rx   Ry   Rz   R{   R|   R}   R   t   exc(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    %c         C  s   |  j  |  \ } } } } t } | r\ | j r\ | j r\ | j   | j   } } t } n  t |  j d  r | j |  \ } }	 n t	 |  d   | r y | j
   |	 j
   }
 } Wn t k
 r q X|
 | } }	 n  | |  | |	  f S(   s  
        Polynomial division with remainder of ``f`` by ``g``.

        Examples
        ========

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

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

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

        t   div(   Rw   R   t   is_Ringt   is_FieldRd   Rc   R   R9   R   R    R   R"   (   Rx   Ry   t   autoRn   R{   R|   R}   R   R   R   t   Qt   R(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   +  s    	c   	      C  s   |  j  |  \ } } } } t } | r\ | j r\ | j r\ | j   | j   } } t } n  t |  j d  r | j |  } n t	 |  d   | r y | j
   } Wq t k
 r q Xn  | |  S(   so  
        Computes the polynomial remainder of ``f`` by ``g``.

        Examples
        ========

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

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

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

        t   rem(   Rw   R   R   R   Rd   Rc   R   R9   R   R    R   R"   (	   Rx   Ry   R   Rn   R{   R|   R}   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   R  s    	c   	      C  s   |  j  |  \ } } } } t } | r\ | j r\ | j r\ | j   | j   } } t } n  t |  j d  r | j |  } n t	 |  d   | r y | j
   } Wq t k
 r q Xn  | |  S(   sa  
        Computes polynomial quotient of ``f`` by ``g``.

        Examples
        ========

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

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

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

        t   quo(   Rw   R   R   R   Rd   Rc   R   R9   R   R    R   R"   (	   Rx   Ry   R   Rn   R{   R|   R}   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   w  s    	c   
      C  s   |  j  |  \ } } } } t } | r\ | j r\ | j r\ | j   | j   } } t } n  t |  j d  r y | j |  } Wq t	 k
 r }	 |	 j
 |  j   | j     q Xn t |  d   | r y | j   } Wq t k
 r q Xn  | |  S(   s  
        Computes polynomial exact quotient of ``f`` by ``g``.

        Examples
        ========

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

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

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

        t   exquo(   Rw   R   R   R   Rd   Rc   R   R9   R   R'   RP   R`   R    R   R"   (
   Rx   Ry   R   Rn   R{   R|   R}   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s"    	%c         C  s   t  | t  rr t |  j  } | | k o6 | k  n rV | d k  rO | | S| Sq t d | | | f   n> y |  j j t |   SWn! t k
 r t d |   n Xd S(   s3   Returns level associated with the given generator. i    s    -%s <= gen < %s expected, got %ss"   a valid generator expected, got %sN(   RA   R   RN   R:   R%   R   R   R   (   Rx   Rr   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    i    c         C  sD   |  j  |  } t |  j d  r1 |  j j |  St |  d   d S(   su  
        Returns degree of ``f`` in ``x_j``.

        The degree of 0 is negative infinity.

        Examples
        ========

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

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

        t   degreeN(   R   R   R9   R   R    (   Rx   Rr   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s2   t  |  j d  r |  j j   St |  d   d S(   s   
        Returns a list of degrees of ``f``.

        Examples
        ========

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

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

        t   degree_listN(   R   R9   R   R    (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s2   t  |  j d  r |  j j   St |  d   d S(   s  
        Returns the total degree of ``f``.

        Examples
        ========

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

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

        t   total_degreeN(   R   R9   R   R    (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s   t  | t  s( t d t |    n  | |  j k rU |  j j |  } |  j } n t |  j  } |  j | f } t |  j d  r |  j	 |  j j
 |  d | St |  d   d S(   s  
        Returns the homogeneous polynomial of ``f``.

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

        Examples
        ========

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

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

        s   ``Symbol`` expected, got %st
   homogenizeR:   t   homogeneous_orderN(   RA   R   t	   TypeErrort   typeR:   R   RN   R   R9   R{   R   R    (   Rx   t   sRm   R:   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s2   t  |  j d  r |  j j   St |  d   d S(   s-  
        Returns the homogeneous order of ``f``.

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

        Examples
        ========

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

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

        R   N(   R   R9   R   R    (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   ?  s    c         C  sc   | d k	 r |  j |  d St |  j d  rA |  j j   } n t |  d   |  j j j |  S(   s   
        Returns the leading coefficient of ``f``.

        Examples
        ========

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

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

        i    t   LCN(   RV   Rq   R   R9   R   R    Rn   R   (   Rx   R;   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   X  s    c         C  sF   t  |  j d  r$ |  j j   } n t |  d   |  j j j |  S(   s   
        Returns the trailing coefficient of ``f``.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x**2 + 3*x, x).TC()
        0

        t   TC(   R   R9   R   R    Rn   R   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   p  s    c         C  s6   t  |  j d  r# |  j |  d St |  d   d S(   s   
        Returns the last non-zero coefficient of ``f``.

        Examples
        ========

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

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

        Rq   it   ECN(   R   R9   Rq   R    (   Rx   R;   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         C  s   |  j  t | |  j  j   S(   sE  
        Returns the coefficient of ``monom`` in ``f`` if there, else None.

        Examples
        ========

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

        >>> p = Poly(24*x*y*exp(8) + 23*x, x, y)

        >>> p.coeff_monomial(x)
        23
        >>> p.coeff_monomial(y)
        0
        >>> p.coeff_monomial(x*y)
        24*exp(8)

        Note that ``Expr.coeff()`` behaves differently, collecting terms
        if possible; the Poly must be converted to an Expr to use that
        method, however:

        >>> p.as_expr().coeff(x)
        24*y*exp(8) + 23
        >>> p.as_expr().coeff(y)
        24*x*exp(8)
        >>> p.as_expr().coeff(x*y)
        24*exp(8)

        See Also
        ========
        nth: more efficient query using exponents of the monomial's generators

        (   t   nthR   R:   t	   exponents(   Rx   RZ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   coeff_monomial  s    #c         G  s   t  |  j d  r` t |  t |  j  k r< t d   n  |  j j t t t |     } n t	 |  d   |  j j
 j |  S(   s.  
        Returns the ``n``-th coefficient of ``f`` where ``N`` are the
        exponents of the generators in the term of interest.

        Examples
        ========

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

        >>> Poly(x**3 + 2*x**2 + 3*x, x).nth(2)
        2
        >>> Poly(x**3 + 2*x*y**2 + y**2, x, y).nth(1, 2)
        2
        >>> Poly(4*sqrt(x)*y)
        Poly(4*y*(sqrt(x)), y, sqrt(x), domain='ZZ')
        >>> _.nth(1, 1)
        4

        See Also
        ========
        coeff_monomial

        R   s,   exponent of each generator must be specified(   R   R9   RN   R:   R   R   RE   R\   R   R    Rn   R   (   Rx   t   NR   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    $i   c         C  s   t  d   d  S(   Nsy   Either convert to Expr with `as_expr` method to use Expr's coeff method or else use the `coeff_monomial` method of Polys.(   R?   (   Rx   R   R   t   right(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR[     s    c         C  s   t  |  j |  d |  j  S(   s  
        Returns the leading monomial of ``f``.

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

        Examples
        ========

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

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

        i    (   R   Ri   R:   (   Rx   R;   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   LM  s    c         C  s   t  |  j |  d |  j  S(   s   
        Returns the last non-zero monomial of ``f``.

        Examples
        ========

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

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

        i(   R   Ri   R:   (   Rx   R;   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   EM  s    c         C  s/   |  j  |  d \ } } t | |  j  | f S(   s  
        Returns the leading term of ``f``.

        The Leading term signifies the term having
        the highest power of the principal generator in the
        expression f along with its coefficient.

        Examples
        ========

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

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

        i    (   R   R   R:   (   Rx   R;   RZ   R[   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   LT  s    c         C  s/   |  j  |  d \ } } t | |  j  | f S(   s   
        Returns the last non-zero term of ``f``.

        Examples
        ========

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

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

        i(   R   R   R:   (   Rx   R;   RZ   R[   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   ET$  s    c         C  sF   t  |  j d  r$ |  j j   } n t |  d   |  j j j |  S(   s   
        Returns maximum norm of ``f``.

        Examples
        ========

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

        >>> Poly(-x**2 + 2*x - 3, x).max_norm()
        3

        t   max_norm(   R   R9   R   R    Rn   R   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   5  s    c         C  sF   t  |  j d  r$ |  j j   } n t |  d   |  j j j |  S(   s   
        Returns l1 norm of ``f``.

        Examples
        ========

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

        >>> Poly(-x**2 + 2*x - 3, x).l1_norm()
        6

        t   l1_norm(   R   R9   R   R    Rn   R   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   J  s    c         C  s   |  } | j  j j s" t j | f S| j   } | j rL | j  j j   } n  t | j  d  rv | j  j	   \ } } n t
 | d   | j |  | j |  } } | s | j r | | f S| | j   f Sd S(   s  
        Clear denominators, but keep the ground domain.

        Examples
        ========

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

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

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

        t   clear_denomsN(   R9   Rn   R   R   t   OneRs   t   has_assoc_Ringt   get_ringR   R   R    R   R{   R   (   Re   RX   Rx   Rn   R[   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   _  s    	
c         C  s   |  } | j  |  \ } } } } | |  } | |  } | j oH | j sU | | f S| j d t  \ } } | j d t  \ } } | j |  } | j |  } | | f S(   s  
        Clear denominators in a rational function ``f/g``.

        Examples
        ========

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

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

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

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

        RX   (   Rw   R   R   R   Rc   R   (   Re   Ry   Rx   Rn   R{   t   at   b(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   rat_clear_denoms  s    
c         O  s   |  } | j  d t  r6 | j j j r6 | j   } n  t | j d  r | sj | j | j j d d   S| j } x] | D]U } t	 |  t
 k r | \ } } n | d } } | j t |  | j |   } qz W| j |  St | d   d S(   s  
        Computes indefinite integral of ``f``.

        Examples
        ========

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

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

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

        R   t	   integrateR   i   N(   t   getRc   R9   Rn   R   Rd   R   R{   R  R   R   R   R   R    (   Re   t   specsRJ   Rx   R9   t   specRr   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    !	%c         O  s   | j  d t  s" t |  | |  St |  j d  r | sV |  j |  j j d d   S|  j } x] | D]U } t |  t k r | \ } } n | d } } | j t	 |  |  j
 |   } qf W|  j |  St |  d   d S(   sX  
        Computes partial derivative of ``f``.

        Examples
        ========

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

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

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

        t   evaluatet   diffR   i   N(   R  Rc   R   R   R9   R{   R
  R   R   R   R   R    (   Rx   R  t   kwargsR9   R  Rr   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR
    s    	%c         C  s  |  } | d k r t | t  rZ | } x, | j   D] \ } } | j | |  } q4 W| St | t t f  r | } t |  t | j  k r t	 d   n  x2 t
 | j |  D] \ } } | j | |  } q W| Sd | }	 } n | j |  }	 t | j d  st | d   n  y | j j | |	  }
 Wn t k
 r| sit d | | j j f   qt | g  \ } \ } | j   j | | j  } | j |  } | j | |  } | j j | |	  }
 n X| j |
 d |	 S(   s  
        Evaluate ``f`` at ``a`` in the given variable.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x + 3, x).eval(2)
        11

        >>> Poly(2*x*y + 3*x + y + 2, x, y).eval(x, 2)
        Poly(5*y + 8, y, domain='ZZ')

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

        >>> f.eval({x: 2})
        Poly(5*y + 2*z + 6, y, z, domain='ZZ')
        >>> f.eval({x: 2, y: 5})
        Poly(2*z + 31, z, domain='ZZ')
        >>> f.eval({x: 2, y: 5, z: 7})
        45

        >>> f.eval((2, 5))
        Poly(2*z + 31, z, domain='ZZ')
        >>> f(2, 5)
        Poly(2*z + 31, z, domain='ZZ')

        s   too many values providedi    R   s   can't evaluate at %s in %sR   N(   RV   RA   RB   RW   R   R   RE   RN   R:   R   R   R   R   R9   R    R"   R!   Rn   R   Rs   t   unify_with_symbolsRb   RX   R{   (   Re   R   R  R   Rx   R   Rr   R   t   valuesR   R   t   a_domaint
   new_domain(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s:    c         G  s   |  j  |  S(   sz  
        Evaluate ``f`` at the give values.

        Examples
        ========

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

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

        >>> f(2)
        Poly(5*y + 2*z + 6, y, z, domain='ZZ')
        >>> f(2, 5)
        Poly(2*z + 31, z, domain='ZZ')
        >>> f(2, 5, 7)
        45

        (   R   (   Rx   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __call__H	  s    c   	      C  s   |  j  |  \ } } } } | rF | j rF | j   | j   } } n  t |  j d  rp | j |  \ } } n t |  d   | |  | |  f S(   s  
        Half extended Euclidean algorithm of ``f`` and ``g``.

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

        Examples
        ========

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

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

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

        t
   half_gcdex(   Rw   R   Rd   R   R9   R  R    (	   Rx   Ry   R   Rn   R{   R|   R}   R   t   h(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  ^	  s    c   
      C  s   |  j  |  \ } } } } | rF | j rF | j   | j   } } n  t |  j d  rs | j |  \ } } }	 n t |  d   | |  | |  | |	  f S(   s  
        Extended Euclidean algorithm of ``f`` and ``g``.

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

        Examples
        ========

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

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

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

        t   gcdex(   Rw   R   Rd   R   R9   R  R    (
   Rx   Ry   R   Rn   R{   R|   R}   R   t   tR  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  }	  s    c         C  s   |  j  |  \ } } } } | rF | j rF | j   | j   } } n  t |  j d  rj | j |  } n t |  d   | |  S(   s  
        Invert ``f`` modulo ``g`` when possible.

        Examples
        ========

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

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

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

        t   invert(   Rw   R   Rd   R   R9   R  R    (   Rx   Ry   R   Rn   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  	  s    c         C  sI   t  |  j d  r- |  j j t |   } n t |  d   |  j |  S(   sc  
        Compute ``f**(-1)`` mod ``x**n``.

        Examples
        ========

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

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

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

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

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

        t   revert(   R   R9   R  R   R    R{   (   Rx   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  	  s    c         C  sa   |  j  |  \ } } } } t |  j d  r? | j |  } n t |  d   t t | |   S(   sd  
        Computes the subresultant PRS of ``f`` and ``g``.

        Examples
        ========

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

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

        t   subresultants(   Rw   R   R9   R  R    RE   R\   (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  	  s
    c   	      C  s   |  j  |  \ } } } } t |  j d  rc | rQ | j | d | \ } } qr | j |  } n t |  d   | r | | d d t t | |   f S| | d d S(   s  
        Computes the resultant of ``f`` and ``g`` via PRS.

        If includePRS=True, it includes the subresultant PRS in the result.
        Because the PRS is used to calculate the resultant, this is more
        efficient than calling :func:`subresultants` separately.

        Examples
        ========

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

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

        >>> f.resultant(Poly(x**2 - 1, x))
        4
        >>> f.resultant(Poly(x**2 - 1, x), includePRS=True)
        (4, [Poly(x**2 + 1, x, domain='ZZ'), Poly(x**2 - 1, x, domain='ZZ'),
             Poly(-2, x, domain='ZZ')])

        t	   resultantt
   includePRSR   i    (   Rw   R   R9   R  R    RE   R\   (	   Rx   Ry   R  Rz   R{   R|   R}   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  	  s    %c         C  sF   t  |  j d  r$ |  j j   } n t |  d   |  j | d d S(   s   
        Computes the discriminant of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x + 3, x).discriminant()
        -8

        t   discriminantR   i    (   R   R9   R  R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  
  s    c         C  s   d d l  m } | |  |  S(   s  Compute the *dispersion set* of two polynomials.

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

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

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

        Examples
        ========

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

        Dispersion set and dispersion of a simple polynomial:

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

        Note that the definition of the dispersion is not symmetric:

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

        Computing the dispersion also works over field extensions:

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

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

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

        See Also
        ========

        dispersion

        References
        ==========

        1. [ManWright94]_
        2. [Koepf98]_
        3. [Abramov71]_
        4. [Man93]_
        i(   t   dispersionset(   t   sympy.polys.dispersionR  (   Rx   Ry   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  2
  s    Hc         C  s   d d l  m } | |  |  S(   s  Compute the *dispersion* of polynomials.

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

        .. math::
            \operatorname{dis}(f, g)
            & := \max\{ J(f,g) \cup \{0\} \} \\
            &  = \max\{ \{a \in \mathbb{N} | \gcd(f(x), g(x+a)) \neq 1\} \cup \{0\} \}

        and for a single polynomial `\operatorname{dis}(f) := \operatorname{dis}(f, f)`.

        Examples
        ========

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

        Dispersion set and dispersion of a simple polynomial:

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

        Note that the definition of the dispersion is not symmetric:

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

        Computing the dispersion also works over field extensions:

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

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

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

        See Also
        ========

        dispersionset

        References
        ==========

        1. [ManWright94]_
        2. [Koepf98]_
        3. [Abramov71]_
        4. [Man93]_
        i(   t
   dispersion(   R  R  (   Rx   Ry   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  }
  s    Hc   	      C  sv   |  j  |  \ } } } } t |  j d  rH | j |  \ } } } n t |  d   | |  | |  | |  f S(   s#  
        Returns the GCD of ``f`` and ``g`` and their cofactors.

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

        Examples
        ========

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

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

        t	   cofactors(   Rw   R   R9   R  R    (	   Rx   Ry   Rz   R{   R|   R}   R  t   cfft   cfg(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  
  s
    c         C  sX   |  j  |  \ } } } } t |  j d  r? | j |  } n t |  d   | |  S(   s  
        Returns the polynomial GCD of ``f`` and ``g``.

        Examples
        ========

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

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

        t   gcd(   Rw   R   R9   R!  R    (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR!  
  s
    c         C  sX   |  j  |  \ } } } } t |  j d  r? | j |  } n t |  d   | |  S(   s  
        Returns polynomial LCM of ``f`` and ``g``.

        Examples
        ========

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

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

        t   lcm(   Rw   R   R9   R"  R    (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR"  
  s
    c         C  sX   |  j  j j |  } t |  j  d  r< |  j  j |  } n t |  d   |  j |  S(   s  
        Reduce ``f`` modulo a constant ``p``.

        Examples
        ========

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

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

        t   trunc(   R9   Rn   RX   R   R#  R    R{   (   Rx   t   pR   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR#    s
    c         C  sj   |  } | r* | j  j j r* | j   } n  t | j  d  rN | j  j   } n t | d   | j |  S(   sz  
        Divides all coefficients by ``LC(f)``.

        Examples
        ========

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

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

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

        t   monic(   R9   Rn   R   Rd   R   R%  R    R{   (   Re   R   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR%  *  s    c         C  sF   t  |  j d  r$ |  j j   } n t |  d   |  j j j |  S(   s   
        Returns the GCD of polynomial coefficients.

        Examples
        ========

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

        >>> Poly(6*x**2 + 8*x + 12, x).content()
        2

        t   content(   R   R9   R&  R    Rn   R   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR&  G  s    c         C  s[   t  |  j d  r* |  j j   \ } } n t |  d   |  j j j |  |  j |  f S(   s  
        Returns the content and a primitive form of ``f``.

        Examples
        ========

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

        >>> Poly(2*x**2 + 8*x + 12, x).primitive()
        (2, Poly(x**2 + 4*x + 6, x, domain='ZZ'))

        t	   primitive(   R   R9   R'  R    Rn   R   R{   (   Rx   t   contR   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR'  \  s    c         C  sX   |  j  |  \ } } } } t |  j d  r? | j |  } n t |  d   | |  S(   s  
        Computes the functional composition of ``f`` and ``g``.

        Examples
        ========

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

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

        t   compose(   Rw   R   R9   R)  R    (   Rx   Ry   Rz   R{   R|   R}   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR)  q  s
    c         C  sI   t  |  j d  r$ |  j j   } n t |  d   t t |  j |   S(   s=  
        Computes a functional decomposition of ``f``.

        Examples
        ========

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

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

        t	   decompose(   R   R9   R*  R    RE   R\   R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR*    s    c         C  sC   t  |  j d  r' |  j j |  } n t |  d   |  j |  S(   s  
        Efficiently compute Taylor shift ``f(x + a)``.

        Examples
        ========

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

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

        t   shift(   R   R9   R+  R    R{   (   Rx   R  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR+    s    c         C  s   | j  |  \ } } |  j  |  \ } } | j  |  \ } } t | j d  ro | j j | j | j  } n t | d   | j |  S(   s3  
        Efficiently evaluate the functional transformation ``q**n * f(p/q)``.


        Examples
        ========

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

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

        t	   transform(   R~   R   R9   R,  R    R{   (   Rx   R$  R   t   PR   R|   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR,    s    c         C  ss   |  } | r* | j  j j r* | j   } n  t | j  d  rN | j  j   } n t | d   t t | j	 |   S(   s  
        Computes the Sturm sequence of ``f``.

        Examples
        ========

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

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

        t   sturm(
   R9   Rn   R   Rd   R   R.  R    RE   R\   R{   (   Re   R   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR.    s    c         C  s_   t  |  j d  r$ |  j j   } n t |  d   g  | D]! \ } } |  j |  | f ^ q: S(   sI  
        Computes greatest factorial factorization of ``f``.

        Examples
        ========

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

        >>> f = x**5 + 2*x**4 - x**3 - 2*x**2

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

        t   gff_list(   R   R9   R/  R    R{   (   Rx   R   Ry   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR/    s    c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s  
        Computes the product, ``Norm(f)``, of the conjugates of
        a polynomial ``f`` defined over a number field ``K``.

        Examples
        ========

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

        >>> a, b = sqrt(2), sqrt(3)

        A polynomial over a quadratic extension.
        Two conjugates x - a and x + a.

        >>> f = Poly(x - a, x, extension=a)
        >>> f.norm()
        Poly(x**2 - 2, x, domain='QQ')

        A polynomial over a quartic extension.
        Four conjugates x - a, x - a, x + a and x + a.

        >>> f = Poly(x - a, x, extension=(a, b))
        >>> f.norm()
        Poly(x**4 - 4*x**2 + 4, x, domain='QQ')

        t   norm(   R   R9   R0  R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR0     s    c         C  s[   t  |  j d  r- |  j j   \ } } } n t |  d   | |  j |  |  j |  f S(   sf  
        Computes square-free norm of ``f``.

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

        Examples
        ========

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

        >>> s, f, r = Poly(x**2 + 1, x, extension=[sqrt(3)]).sqf_norm()

        >>> s
        1
        >>> f
        Poly(x**2 - 2*sqrt(3)*x + 4, x, domain='QQ<sqrt(3)>')
        >>> r
        Poly(x**4 - 4*x**2 + 16, x, domain='QQ')

        t   sqf_norm(   R   R9   R1  R    R{   (   Rx   R   Ry   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR1  #  s    c         C  s@   t  |  j d  r$ |  j j   } n t |  d   |  j |  S(   s   
        Computes square-free part of ``f``.

        Examples
        ========

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

        >>> Poly(x**3 - 3*x - 2, x).sqf_part()
        Poly(x**2 - x - 2, x, domain='ZZ')

        t   sqf_part(   R   R9   R2  R    R{   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR2  B  s    c         C  s}   t  |  j d  r- |  j j |  \ } } n t |  d   |  j j j |  g  | D]! \ } } |  j |  | f ^ qU f S(   s   
        Returns a list of square-free factors of ``f``.

        Examples
        ========

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

        >>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16

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

        >>> Poly(f).sqf_list(all=True)
        (2, [(Poly(1, x, domain='ZZ'), 1),
             (Poly(x + 1, x, domain='ZZ'), 2),
             (Poly(x + 2, x, domain='ZZ'), 3)])

        t   sqf_list(   R   R9   R3  R    Rn   R   R{   (   Rx   t   allR[   t   factorsRy   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR3  W  s    c         C  sb   t  |  j d  r' |  j j |  } n t |  d   g  | D]! \ } } |  j |  | f ^ q= S(   s  
        Returns a list of square-free factors of ``f``.

        Examples
        ========

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

        >>> f = expand(2*(x + 1)**3*x**4)
        >>> f
        2*x**7 + 6*x**6 + 6*x**5 + 2*x**4

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

        >>> Poly(f).sqf_list_include(all=True)
        [(Poly(2, x, domain='ZZ'), 1),
         (Poly(1, x, domain='ZZ'), 2),
         (Poly(x + 1, x, domain='ZZ'), 3),
         (Poly(x, x, domain='ZZ'), 4)]

        t   sqf_list_include(   R   R9   R6  R    R{   (   Rx   R4  R5  Ry   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR6  t  s    c         C  s   t  |  j d  rU y |  j j   \ } } Wqd t k
 rQ t j |  d f g f SXn t |  d   |  j j j |  g  | D]! \ } } |  j	 |  | f ^ q} f S(   s~  
        Returns a list of irreducible factors of ``f``.

        Examples
        ========

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

        >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y

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

        t   factor_listi   (
   R   R9   R7  R!   R   R   R    Rn   R   R{   (   Rx   R[   R5  Ry   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR7    s    c         C  s   t  |  j d  rF y |  j j   } WqU t k
 rB |  d f g SXn t |  d   g  | D]! \ } } |  j |  | f ^ q\ S(   s  
        Returns a list of irreducible factors of ``f``.

        Examples
        ========

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

        >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y

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

        t   factor_list_includei   (   R   R9   R8  R!   R    R{   (   Rx   R5  Ry   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR8    s    c         C  s  | d k	 r9 t j |  } | d k r9 t d   q9 n  | d k	 rW t j |  } n  | d k	 ru t j |  } n  t |  j d  r |  j j d | d | d | d | d | d	 |  } n t |  d   | r1d
   } | s t t	 | |   Sd   }	 | \ }
 } t t	 | |
   t t	 |	 |   f Sd   } | sSt t	 | |   Sd   }	 | \ }
 } t t	 | |
   t t	 |	 |   f Sd S(   s  
        Compute isolating intervals for roots of ``f``.

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

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

        Examples
        ========

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

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

        i    s!   'eps' must be a positive rationalt	   intervalsR4  t   epst   inft   supt   fastt   sqfc         S  s(   |  \ } } t  j |  t  j |  f S(   N(   R   R   (   t   intervalR   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _real  s    c         S  sV   |  \ \ } } \ } } t  j |  t t  j |  t  j |  t t  j |  f S(   N(   R   R   R   (   t	   rectanglet   ut   vR   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _complex  s    c         S  s4   |  \ \ } } } t  j |  t  j |  f | f S(   N(   R   R   (   R?  R   R  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR@    s    c         S  sb   |  \ \ \ } } \ } } } t  j |  t t  j |  t  j |  t t  j |  f | f S(   N(   R   R   R   (   RA  RB  RC  R   R  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRD    s    N(
   RV   R   RX   R   R   R9   R9  R    RE   R\   (   Rx   R4  R:  R;  R<  R=  R>  R   R@  RD  t	   real_partt   complex_part(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR9    s2    *		(		c   	   	   C  s  | r |  j  r t d   n  t j |  t j |  } } | d	 k	 rw t j |  } | d k rw t d   qw n  | d	 k	 r t |  } n | d	 k r d } n  t |  j d  r |  j j	 | | d | d | d | \ } } n t
 |  d   t j |  t j |  f S(
   s  
        Refine an isolating interval of a root to the given precision.

        Examples
        ========

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

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

        s&   only square-free polynomials supportedi    s!   'eps' must be a positive rationali   t   refine_rootR:  t   stepsR=  N(   t   is_sqfR%   R   RX   RV   R   R   R   R9   RG  R    R   (	   Rx   R   R  R:  RH  R=  t	   check_sqfR   t   T(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRG    s    	0c         C  s  t  t  } } | d k	 r t |  } | t j k r= d } q | j   \ } } | sg t j |  } q t t	 t j | | f   t
 } } n  | d k	 rt |  } | t j k r d } q| j   \ } } | s t j |  } qt t	 t j | | f   t
 } } n  | r_| r_t |  j d  rM|  j j d | d |  } qt |  d   n | r| d k	 r| t j f } n  | r| d k	 r| t j f } n  t |  j d  r|  j j d | d |  } n t |  d   t |  S(   s<  
        Return the number of roots of ``f`` in ``[inf, sup]`` interval.

        Examples
        ========

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

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

        t   count_real_rootsR;  R<  t   count_complex_rootsN(   Rc   RV   R   R   t   NegativeInfinityt   as_real_imagR   RX   RE   R\   R   t   InfinityR   R9   RL  R    Rt   RM  R   (   Rx   R;  R<  t   inf_realt   sup_realt   ret   imt   count(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   count_roots<  s:    	(	(c         C  s   t  j j j |  | d | S(   s  
        Get an indexed root of a polynomial.

        Examples
        ========

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

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

        >>> f.root(0)
        -1/2
        >>> f.root(1)
        2
        >>> f.root(2)
        2
        >>> f.root(3)
        Traceback (most recent call last):
        ...
        IndexError: root index out of [-3, 2] range, got 3

        >>> Poly(x**5 + x + 1).root(0)
        CRootOf(x**3 - x**2 + 1, 0)

        t   radicals(   t   sympyt   polyst   rootoftoolst   rootof(   Rx   R   RW  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   root{  s    c         C  s<   t  j j j j |  d | } | r( | St | d t Sd S(   sL  
        Return a list of real roots with multiplicities.

        Examples
        ========

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

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

        RW  t   multipleN(   RX  RY  RZ  t   CRootOft
   real_rootsR3   R   (   Rx   R]  RW  t   reals(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR_    s    c         C  s<   t  j j j j |  d | } | r( | St | d t Sd S(   s  
        Return a list of real and complex roots with multiplicities.

        Examples
        ========

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

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

        RW  R]  N(   RX  RY  RZ  R^  t	   all_rootsR3   R   (   Rx   R]  RW  t   roots(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRa    s    i   i2   c           s2  d d l  m   |  j r, t d |    n  |  j   d k rB g  S|  j j t k r| g  |  j   D] } t	 |  ^ qa } n |  j j t
 k r g  |  j   D] } | j ^ q } d d l m } | |   } g  |  j   D] } t	 | |  ^ q } n g  |  j   D] } | j d |  j   ^ q} y& g  | D] } t j |   ^ q3} Wn' t k
 rxt d |  j j   n Xt j j }	 | t j _ z y_ t j | d | d	 | d
 t d |  j   d }
 t t t t |
 d   f d     }
 Wn' t k
 rt d | | f   n XWd |	 t j _ X|
 S(   s  
        Compute numerical approximations of roots of ``f``.

        Parameters
        ==========

        n ... the number of digits to calculate
        maxsteps ... the maximum number of iterations to do

        If the accuracy `n` cannot be reached in `maxsteps`, it will raise an
        exception. You need to rerun with higher maxsteps.

        Examples
        ========

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

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

        i(   t   signs#   can't compute numerical roots of %si    (   t   ilcmR   s!   Numerical domain expected, got %st   maxstepst   cleanupt   errort	   extrapreci
   t   keyc           s4   |  j  r d n d |  j t |  j     |  j   f S(   Ni   i    (   t   imagt   realR   (   R   (   Rc  (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   <lambda>  t    s7   convergence to root failed; try n < %s or maxsteps > %sN(   t$   sympy.functions.elementary.complexesRc  t   is_multivariateR&   R   R9   Rn   R   R   R   R   R   t   sympy.core.numbersRd  t   evalfRO  t   mpmatht   mpcR   R!   t   mpt   dpst	   polyrootsR   RE   R\   R   t   sortedR7   (   Rx   R   Re  Rf  R[   Rq   t   denomsRd  t   facRu  Rb  (    (   Rc  s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   nroots  sD    	(",.& 	%c         C  st   |  j  r t d |    n  i  } xK |  j   d D]9 \ } } | j r3 | j   \ } } | | | | <q3 q3 W| S(   s  
        Compute roots of ``f`` by factorization in the ground domain.

        Examples
        ========

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

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

        s    can't compute ground roots of %si   (   Ro  R&   R7  t	   is_linearR   (   Rx   Rb  t   factorR   R  R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   ground_roots  s    		c         C  s   |  j  r t d   n  t |  } | j rH | d k rH t |  } n t d |   |  j } t d  } |  j |  j	 j
 | | | | |   } | j | |  S(   sf  
        Construct a polynomial with n-th powers of roots of ``f``.

        Examples
        ========

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

        >>> f = Poly(x**4 - x**2 + 1)

        >>> f.nth_power_roots_poly(2)
        Poly(x**4 - 2*x**3 + 3*x**2 - 2*x + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(3)
        Poly(x**4 + 2*x**2 + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(4)
        Poly(x**4 + 2*x**3 + 3*x**2 + 2*x + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(12)
        Poly(x**4 - 4*x**3 + 6*x**2 - 4*x + 1, x, domain='ZZ')

        s   must be a univariate polynomiali   s&   'n' must an integer and n >= 1, got %sR  (   Ro  R&   R   t
   is_IntegerR   R   Rr   R	   R  R]   RT   R   (   Rx   R   R   R   R  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   nth_power_roots_poly-  s    		)c         C  s   |  j  |  \ } } } } t | d  rB | j | d | } n t |  d   | s | j ro | j   } n  | \ } }	 }
 } | j |  } | j |	  }	 | |	 | |
  | |  f St t | |   Sd S(   s  
        Cancel common factors in a rational function ``f/g``.

        Examples
        ========

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

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

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

        t   cancelt   includeN(	   Rw   R   R  R    R   R  R   R   R\   (   Rx   Ry   R  Rn   R{   R|   R}   R   t   cpt   cqR$  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  U  s    	c         C  s
   |  j  j S(   s  
        Returns ``True`` if ``f`` is a zero polynomial.

        Examples
        ========

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

        >>> Poly(0, x).is_zero
        True
        >>> Poly(1, x).is_zero
        False

        (   R9   t   is_zero(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  z  s    c         C  s
   |  j  j S(   s  
        Returns ``True`` if ``f`` is a unit polynomial.

        Examples
        ========

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

        >>> Poly(0, x).is_one
        False
        >>> Poly(1, x).is_one
        True

        (   R9   t   is_one(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s
   |  j  j S(   s   
        Returns ``True`` if ``f`` is a square-free polynomial.

        Examples
        ========

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

        >>> Poly(x**2 - 2*x + 1, x).is_sqf
        False
        >>> Poly(x**2 - 1, x).is_sqf
        True

        (   R9   RI  (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRI    s    c         C  s
   |  j  j S(   s   
        Returns ``True`` if the leading coefficient of ``f`` is one.

        Examples
        ========

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

        >>> Poly(x + 2, x).is_monic
        True
        >>> Poly(2*x + 2, x).is_monic
        False

        (   R9   t   is_monic(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s
   |  j  j S(   s;  
        Returns ``True`` if GCD of the coefficients of ``f`` is one.

        Examples
        ========

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

        >>> Poly(2*x**2 + 6*x + 12, x).is_primitive
        False
        >>> Poly(x**2 + 3*x + 6, x).is_primitive
        True

        (   R9   t   is_primitive(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s
   |  j  j S(   sJ  
        Returns ``True`` if ``f`` is an element of the ground domain.

        Examples
        ========

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

        >>> Poly(x, x).is_ground
        False
        >>> Poly(2, x).is_ground
        True
        >>> Poly(y, x).is_ground
        True

        (   R9   t	   is_ground(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s
   |  j  j S(   s,  
        Returns ``True`` if ``f`` is linear in all its variables.

        Examples
        ========

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

        >>> Poly(x + y + 2, x, y).is_linear
        True
        >>> Poly(x*y + 2, x, y).is_linear
        False

        (   R9   R{  (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR{    s    c         C  s
   |  j  j S(   s6  
        Returns ``True`` if ``f`` is quadratic in all its variables.

        Examples
        ========

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

        >>> Poly(x*y + 2, x, y).is_quadratic
        True
        >>> Poly(x*y**2 + 2, x, y).is_quadratic
        False

        (   R9   t   is_quadratic(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s
   |  j  j S(   s%  
        Returns ``True`` if ``f`` is zero or has only one term.

        Examples
        ========

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

        >>> Poly(3*x**2, x).is_monomial
        True
        >>> Poly(3*x**2 + 1, x).is_monomial
        False

        (   R9   t   is_monomial(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s
   |  j  j S(   sZ  
        Returns ``True`` if ``f`` is a homogeneous polynomial.

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

        Examples
        ========

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

        >>> Poly(x**2 + x*y, x, y).is_homogeneous
        True
        >>> Poly(x**3 + x*y, x, y).is_homogeneous
        False

        (   R9   t   is_homogeneous(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  '  s    c         C  s
   |  j  j S(   sG  
        Returns ``True`` if ``f`` has no factors over its domain.

        Examples
        ========

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

        >>> Poly(x**2 + x + 1, x, modulus=2).is_irreducible
        True
        >>> Poly(x**2 + 1, x, modulus=2).is_irreducible
        False

        (   R9   t   is_irreducible(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  ?  s    c         C  s   t  |  j  d k S(   s  
        Returns ``True`` if ``f`` is a univariate polynomial.

        Examples
        ========

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

        >>> Poly(x**2 + x + 1, x).is_univariate
        True
        >>> Poly(x*y**2 + x*y + 1, x, y).is_univariate
        False
        >>> Poly(x*y**2 + x*y + 1, x).is_univariate
        True
        >>> Poly(x**2 + x + 1, x, y).is_univariate
        False

        i   (   RN   R:   (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   R  s    c         C  s   t  |  j  d k S(   s  
        Returns ``True`` if ``f`` is a multivariate polynomial.

        Examples
        ========

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

        >>> Poly(x**2 + x + 1, x).is_multivariate
        False
        >>> Poly(x*y**2 + x*y + 1, x, y).is_multivariate
        True
        >>> Poly(x*y**2 + x*y + 1, x).is_multivariate
        False
        >>> Poly(x**2 + x + 1, x, y).is_multivariate
        True

        i   (   RN   R:   (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRo  i  s    c         C  s
   |  j  j S(   s  
        Returns ``True`` if ``f`` is a cyclotomic polnomial.

        Examples
        ========

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

        >>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1

        >>> Poly(f).is_cyclotomic
        False

        >>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1

        >>> Poly(g).is_cyclotomic
        True

        (   R9   t   is_cyclotomic(   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s
   |  j    S(   N(   R   (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __abs__  s    c         C  s
   |  j    S(   N(   R   (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __neg__  s    Ry   c         C  sQ   | j  sD y |  j | |  j  } WqD t k
 r@ |  j   | SXn  |  j |  S(   N(   RF   R]   R:   R%   R`   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __add__  s    	c         C  sQ   | j  sD y |  j | |  j  } WqD t k
 r@ | |  j   SXn  | j |   S(   N(   RF   R]   R:   R%   R`   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __radd__  s    	c         C  sQ   | j  sD y |  j | |  j  } WqD t k
 r@ |  j   | SXn  |  j |  S(   N(   RF   R]   R:   R%   R`   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __sub__  s    	c         C  sQ   | j  sD y |  j | |  j  } WqD t k
 r@ | |  j   SXn  | j |   S(   N(   RF   R]   R:   R%   R`   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __rsub__  s    	c         C  sQ   | j  sD y |  j | |  j  } WqD t k
 r@ |  j   | SXn  |  j |  S(   N(   RF   R]   R:   R%   R`   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __mul__  s    	c         C  sQ   | j  sD y |  j | |  j  } WqD t k
 r@ | |  j   SXn  | j |   S(   N(   RF   R]   R:   R%   R`   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __rmul__  s    	R   c         C  s4   | j  r" | d k r" |  j |  S|  j   | Sd  S(   Ni    (   R~  R   R`   (   Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __pow__  s    c         C  s.   | j  s! |  j | |  j  } n  |  j |  S(   N(   RF   R]   R:   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt
   __divmod__  s    	c         C  s.   | j  s! |  j | |  j  } n  | j |   S(   N(   RF   R]   R:   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __rdivmod__  s    	c         C  s.   | j  s! |  j | |  j  } n  |  j |  S(   N(   RF   R]   R:   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __mod__  s    	c         C  s.   | j  s! |  j | |  j  } n  | j |   S(   N(   RF   R]   R:   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __rmod__  s    	c         C  s.   | j  s! |  j | |  j  } n  |  j |  S(   N(   RF   R]   R:   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __floordiv__  s    	c         C  s.   | j  s! |  j | |  j  } n  | j |   S(   N(   RF   R]   R:   R   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __rfloordiv__  s    	c         C  s   |  j    | j    S(   N(   R`   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __div__  s    c         C  s   | j    |  j    S(   N(   R`   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __rdiv__  s    t   otherc         C  s   |  | } } | j  s\ y% | j | | j d | j   } Wq\ t t t f k
 rX t SXn  | j | j k rr t S| j j	 | j j	 k r y% | j j	 j
 | j j	 | j  } Wn t k
 r t SX| j |  } | j |  } n  | j | j k S(   NRU   (   RF   R]   R:   Rs   R%   R!   R"   R   R9   Rn   R~   R#   Rb   (   Re   R  Rx   Ry   Rn   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __eq__  s     	%%c         C  s   |  | k S(   N(    (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __ne__.  s    c         C  s   |  j  S(   N(   R  (   Rx   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __nonzero__2  s    c         C  s'   | s |  | k S|  j  t |   Sd  S(   N(   t
   _strict_eqR   (   Rx   Ry   t   strict(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   eq7  s    
c         C  s   |  j  | d | S(   NR  (   R  (   Rx   Ry   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   ne=  s    c         C  s=   t  | |  j  o< |  j | j k o< |  j j | j d t S(   NR  (   RA   R]   R:   R9   R  Rc   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  @  s    N(   t   __name__t
   __module__t   __doc__t	   __slots__Rc   t   is_commutativeRF   t   _op_priorityRL   t   classmethodRP   RQ   RR   RS   RT   RC   RD   RG   RH   Rf   Rh   t   propertyRj   Rk   RJ   Rr   RU   Rt   Ru   Rv   R~   Rw   RV   R{   Rb   Rs   R   R   R   R<   R   Ra   R   R   R   Rd   R   R   R   Rq   Ri   R   R   R   R   R   R   R   R   R   R`   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R[   R   R   R   R   R   R   R   R  R  R
  t   _eval_derivativeR   R  R  R  R  R  R  R  R  R  R  R  R!  R"  R#  R%  R&  R'  R)  R*  R+  R,  R.  R/  R0  R1  R2  R3  R6  R7  R8  R9  RG  RV  R\  R_  Ra  Rz  R}  R  R  R  R  RI  R  R  R  R{  R  R  R  R  R   Ro  R  R  R  R   t   NotImplementedR  R  R  R  R  R  R  R  R  R  R  R  R  R  R  t   __truediv__t   __rtruediv__R  R  R  t   __bool__R  R  R  (    (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR8   8   sn  +					3						#		$	"							%		%		%	*															'		'%%*				"			%	"		'	'	(	&K	!	"	%	KK												#		!		L%?J		(%		





	t   PurePolyc           B  sY   e  Z d  Z d   Z d   Z e d    Z e d e  d    Z	 d   Z
 d   Z RS(   s)   Class for representing pure polynomials. c         C  s
   |  j  f S(   s$   Allow SymPy to hash Poly instances. (   R9   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRf   H  s    c         C  s   t  t |   j   S(   N(   Rg   R  Rh   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRh   L  s    c         C  s   |  j  S(   sR  
        Free symbols of a polynomial.

        Examples
        ========

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

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

        (   Rk   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRj   O  s    R  c         C  s  |  | } } | j  s\ y% | j | | j d | j   } Wq\ t t t f k
 rX t SXn  t | j  t | j  k r~ t S| j	 j
 | j	 j
 k r y% | j	 j
 j | j	 j
 | j  } Wn t k
 r t SX| j |  } | j |  } n  | j	 | j	 k S(   NRU   (   RF   R]   R:   Rs   R%   R!   R"   R   RN   R9   Rn   R~   R#   Rb   (   Re   R  Rx   Ry   Rn   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  d  s     	%%c         C  s+   t  | |  j  o* |  j j | j d t S(   NR  (   RA   R]   R9   R  Rc   (   Rx   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  |  s    c           sm  t  |  } | j s} y; |  j j |  j |  j |  j j |  j j j |   f SWq} t k
 ry t d |  | f   q} Xn  t |  j	  t | j	  k r t d |  | f   n  t
 |  j t  o t
 | j t  s t d |  | f   n  |  j   |  j	 } |  j j j | j j |  } |  j j |  } | j j |  } | | d    f d  } | | | | f S(   Ns   can't unify %s with %sc           sH   | d  k	 r8 | |  | | d } | s8 | j |   Sn    j |  |  S(   Ni   (   RV   R   RP   (   R9   Rn   R:   R   (   RI   (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR{     s
    (   R   RF   R9   Rn   R{   R   R"   R#   RN   R:   RA   R   R]   R~   RX   RV   (   Rx   Ry   R:   Rn   R|   R}   R{   (    (   RI   s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRw     s"    	;$			(   R  R  R  Rf   Rh   R  Rj   R   R  R  R  Rw   (    (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  D  s   			c         O  s   t  j | |  } t |  |  S(   s+   Construct a polynomial from an expression. (   R=   R>   t   _poly_from_expr(   t   exprR:   RJ   RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   poly_from_expr  s    c         C  s  |  t  |   } }  t |  t  s7 t | | |    ns |  j r |  j j |  |  } | j | _ | j | _ | j	 d k r t | _	 n  | | f S| j r |  j   }  n  t |  |  \ } } | j s t | | |    n  t t t | j       \ } } | j } | d k r4t | d | \ | _ } n t t | j |   } t t t | |    } t j | |  } | j	 d k rt | _	 n  | | f S(   s+   Construct a polynomial from an expression. RK   N(   R   RA   R   R(   RF   R]   RG   R:   RU   RY  RV   Rc   t   expandR/   RE   R   RW   R   R\   R   RB   R8   RC   R   (   R  RK   t   origt   polyR9   Ri   Rq   RU   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s2    	
		$	c         O  s   t  j | |  } t |  |  S(   s(   Construct polynomials from expressions. (   R=   R>   t   _parallel_poly_from_expr(   t   exprsR:   RJ   RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   parallel_poly_from_expr  s    c         C  s  d d l  m } t |   d k r |  \ } } t | t  r t | t  r | j j | |  } | j j | |  } | j |  \ } } | j | _ | j	 | _	 | j
 d k r t | _
 n  | | g | f Sn  t |   g  } }  g  g  } } t } x t |  D]{ \ }	 }
 t |
  }
 t |
 t  rl|
 j rD| j |	  qr| j |	  | j rr|
 j   }
 qrn t } |  j |
  qW| rt | | |  t   n  | rx% | D] }	 |  |	 j   |  |	 <qWn  t |  |  \ } } | j st | | |  t   n  x/ | j D]$ } t | |  rt d   qqWg  g  } } g  } g  } x_ | D]W } t t t | j       \ } } | j |  | j |  | j t |   qWW| j	 } | d k rt | d | \ | _	 } n t t | j |   } x) | D]! } | j | |   | | } qWg  } xW t | |  D]F \ } } t t t | |    } t j  | |  } | j |  q?W| j
 d k rt! |  | _
 n  | | f S(   s(   Construct polynomials from expressions. i(   t	   Piecewisei   s&   Piecewise generators do not make senseRK   N("   t$   sympy.functions.elementary.piecewiseR  RN   RA   R8   R]   RG   R~   R:   RU   RY  RV   Rc   RE   R   R   R   R   RF   R   R  R(   R`   R0   R%   R   RW   t   extendR   R\   R   RB   RC   t   bool(   R  RK   R  Rx   Ry   t   origst   _exprst   _polyst   failedRm   R  t   repsR   t   coeffs_listt   lengthsR   R   R9   Ri   Rq   RU   RY  R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    sv    			$	c         C  s)   t  |   }  | |  k r% | |  | <n  |  S(   s7   Add a new ``(key, value)`` pair to arguments ``dict``. (   RB   (   RJ   Ri  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _update_args7  s    i    c         C  sY  t  |  d t }  t  | d t j } |  j rH |  } | j   j } nB |  j } | s | rr t |   \ } } q t |  |  \ } } n  | r |  r t j St j S| s |  j r | | j	 k r t |  j    \ } } n  | | j	 k rFt j SnP |  j rFt
 |  j  d k rFt t d |  t t |  j   | f    n  t | j |   S(   sx  
    Return the degree of ``f`` in the given variable.

    The degree of 0 is negative infinity.

    Examples
    ========

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

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

    See also
    ========
    total_degree
    degree_list
    R  i   s   
         A symbolic generator of interest is required for a multivariate
         expression like func = %s, e.g. degree(func, gen = %s) instead of
         degree(func, gen = %s).
        (   R   Rc   t	   is_NumberRF   R`   R  R   t   ZeroRN  R:   RN   Rj   R   R6   t   nextR   R   R   (   Rx   Rr   t
   gen_is_NumR$  t   isNumRz   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   A  s*    		
+c         G  sp   t  |   } | j r$ | j   } n  | j r6 d } n0 |  j rQ | pK |  j } n  t | |  j   } t |  S(   s  
    Return the total_degree of ``f`` in the given variables.

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

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

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

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

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

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

    This is done automatically if any variables are given:

    >>> total_degree(p, x)
    1

    See also
    ========
    degree
    i    (   R   RF   R`   R  R:   R8   R   R   (   Rx   R:   R$  t   rv(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   {  s    (				c         O  sv   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j   } t t t |   S(   s   
    Return a list of degrees of ``f`` in all variables.

    Examples
    ========

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

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

    RY  R   i   (	   R=   t   allowed_flagsR  R(   R)   R   R   R\   R   (   Rx   R:   RJ   R|   RK   R   t   degrees(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         O  sj   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j d | j  S(   s   
    Return the leading coefficient of ``f``.

    Examples
    ========

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

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

    RY  R   i   R;   (   R=   R  R  R(   R)   R   R;   (   Rx   R:   RJ   R|   RK   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         O  sv   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j d | j  } | j   S(   s   
    Return the leading monomial of ``f``.

    Examples
    ========

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

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

    RY  R   i   R;   (   R=   R  R  R(   R)   R   R;   R`   (   Rx   R:   RJ   R|   RK   R   RZ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c         O  s   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j d | j  \ } } | | j   S(   s   
    Return the leading term of ``f``.

    Examples
    ========

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

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

    RY  R   i   R;   (   R=   R  R  R(   R)   R   R;   R`   (   Rx   R:   RJ   R|   RK   R   RZ   R[   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    c   
      O  s   t  j | d g  y( t |  | f | |  \ \ } } } Wn% t k
 rb } t d d |   n X| j |  \ } }	 | j s | j   |	 j   f S| |	 f Sd S(   s   
    Compute polynomial pseudo-division of ``f`` and ``g``.

    Examples
    ========

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

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

    RY  R   i   N(   R=   R  R  R(   R)   R   RY  R`   (
   Rx   Ry   R:   RJ   R|   R}   RK   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    (	c   	      O  s   t  j | d g  y( t |  | f | |  \ \ } } } Wn% t k
 rb } t d d |   n X| j |  } | j s | j   S| Sd S(   s   
    Compute polynomial pseudo-remainder of ``f`` and ``g``.

    Examples
    ========

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

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

    RY  R   i   N(   R=   R  R  R(   R)   R   RY  R`   (	   Rx   Ry   R:   RJ   R|   R}   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   6  s    (	
c   	      O  s   t  j | d g  y( t |  | f | |  \ \ } } } Wn% t k
 rb } t d d |   n Xy | j |  } Wn  t k
 r t |  |   n X| j s | j   S| Sd S(   s   
    Compute polynomial pseudo-quotient of ``f`` and ``g``.

    Examples
    ========

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

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

    RY  R   i   N(	   R=   R  R  R(   R)   R   R'   RY  R`   (	   Rx   Ry   R:   RJ   R|   R}   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   T  s    (	
c   	      O  s   t  j | d g  y( t |  | f | |  \ \ } } } Wn% t k
 rb } t d d |   n X| j |  } | j s | j   S| Sd S(   s_  
    Compute polynomial exact pseudo-quotient of ``f`` and ``g``.

    Examples
    ========

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

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

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

    RY  R   i   N(   R=   R  R  R(   R)   R   RY  R`   (	   Rx   Ry   R:   RJ   R|   R}   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   w  s    (	
c   
      O  s   t  j | d d g  y( t |  | f | |  \ \ } } } Wn% t k
 re } t d d |   n X| j | d | j \ } }	 | j s | j   |	 j   f S| |	 f Sd S(   s  
    Compute polynomial division of ``f`` and ``g``.

    Examples
    ========

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

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

    R   RY  R   i   N(	   R=   R  R  R(   R)   R   R   RY  R`   (
   Rx   Ry   R:   RJ   R|   R}   RK   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    (	c   	      O  s   t  j | d d g  y( t |  | f | |  \ \ } } } Wn% t k
 re } t d d |   n X| j | d | j } | j s | j   S| Sd S(   s  
    Compute polynomial remainder of ``f`` and ``g``.

    Examples
    ========

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

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

    R   RY  R   i   N(	   R=   R  R  R(   R)   R   R   RY  R`   (	   Rx   Ry   R:   RJ   R|   R}   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    (	
c   	      O  s   t  j | d d g  y( t |  | f | |  \ \ } } } Wn% t k
 re } t d d |   n X| j | d | j } | j s | j   S| Sd S(   s   
    Compute polynomial quotient of ``f`` and ``g``.

    Examples
    ========

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

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

    R   RY  R   i   N(	   R=   R  R  R(   R)   R   R   RY  R`   (	   Rx   Ry   R:   RJ   R|   R}   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    (	
c   	      O  s   t  j | d d g  y( t |  | f | |  \ \ } } } Wn% t k
 re } t d d |   n X| j | d | j } | j s | j   S| Sd S(   sQ  
    Compute polynomial exact quotient of ``f`` and ``g``.

    Examples
    ========

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

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

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

    R   RY  R   i   N(	   R=   R  R  R(   R)   R   R   RY  R`   (	   Rx   Ry   R:   RJ   R|   R}   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR     s    (	
c         O  s  t  j | d d g  y( t |  | f | |  \ \ } } } Wn t k
 r } t | j  \ } \ }	 }
 y | j |	 |
  \ } } Wn# t k
 r t d d |   q X| j	 |  | j	 |  f Sn X| j | d | j
 \ } } | j s
| j   | j   f S| | f Sd S(   sT  
    Half extended Euclidean algorithm of ``f`` and ``g``.

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

    Examples
    ========

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

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

    R   RY  R  i   N(   R=   R  R  R(   R   R  R  R?   R)   R   R   RY  R`   (   Rx   Ry   R:   RJ   R|   R}   RK   R   RU   R  R  R   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    ( 	c         O  s6  t  j | d d g  y( t |  | f | |  \ \ } } } Wn t k
 r } t | j  \ } \ }	 }
 y | j |	 |
  \ } } } Wn# t k
 r t d d |   q X| j	 |  | j	 |  | j	 |  f Sn X| j | d | j
 \ } } } | j s%| j   | j   | j   f S| | | f Sd S(   sZ  
    Extended Euclidean algorithm of ``f`` and ``g``.

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

    Examples
    ========

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

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

    R   RY  R  i   N(   R=   R  R  R(   R   R  R  R?   R)   R   R   RY  R`   (   Rx   Ry   R:   RJ   R|   R}   RK   R   RU   R  R  R   R  R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  D  s    (,!	c         O  s   t  j | d d g  y( t |  | f | |  \ \ } } } Wnq t k
 r } t | j  \ } \ }	 }
 y | j | j |	 |
   SWq t k
 r t	 d d |   q Xn X| j | d | j
 } | j s | j   S| Sd S(   s>  
    Invert ``f`` modulo ``g`` when possible.

    Examples
    ========

    >>> from sympy import invert, S
    >>> from sympy.core.numbers import mod_inverse
    >>> from sympy.abc import x

    >>> invert(x**2 - 1, 2*x - 1)
    -4/3

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

    For more efficient inversion of Rationals,
    use the ``mod_inverse`` function:

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

    See Also
    ========
    sympy.core.numbers.mod_inverse
    R   RY  R  i   N(   R=   R  R  R(   R   R  R   R  R?   R)   R   RY  R`   (   Rx   Ry   R:   RJ   R|   R}   RK   R   RU   R  R  R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  k  s     (	
c   
      O  s   t  j | d g  y( t |  | f | |  \ \ } } } Wn% t k
 rb } t d d |   n X| j |  } | j s g  | D] }	 |	 j   ^ q S| Sd S(   s   
    Compute subresultant PRS of ``f`` and ``g``.

    Examples
    ========

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

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

    RY  R  i   N(   R=   R  R  R(   R)   R  RY  R`   (
   Rx   Ry   R:   RJ   R|   R}   RK   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    (	c         O  s  | j  d t  } t j | d g  y( t |  | f | |  \ \ } } } Wn% t k
 rt } t d d |   n X| r | j | d | \ }	 }
 n | j |  }	 | j s | r |	 j	   g  |
 D] } | j	   ^ q f S|	 j	   S| r |	 |
 f S|	 Sd S(   s   
    Compute resultant of ``f`` and ``g``.

    Examples
    ========

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

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

    R  RY  R  i   N(
   t   popR   R=   R  R  R(   R)   R  RY  R`   (   Rx   Ry   R:   RJ   R  R|   R}   RK   R   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s     (	)

c         O  s~   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j   } | j sv | j   S| Sd S(   s   
    Compute discriminant of ``f``.

    Examples
    ========

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

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

    RY  R  i   N(   R=   R  R  R(   R)   R  RY  R`   (   Rx   R:   RJ   R|   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    	
c         O  s*  t  j | d g  y( t |  | f | |  \ \ } } } Wn t k
 r } t | j  \ } \ }	 }
 y | j |	 |
  \ } } } Wn# t k
 r t d d |   q X| j	 |  | j	 |  | j	 |  f Sn X| j |  \ } } } | j
 s| j   | j   | j   f S| | | f Sd S(   s  
    Compute GCD and cofactors of ``f`` and ``g``.

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

    Examples
    ========

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

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

    RY  R  i   N(   R=   R  R  R(   R   R  R  R?   R)   R   RY  R`   (   Rx   Ry   R:   RJ   R|   R}   RK   R   RU   R  R  R  R  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    (,	c           s  t  |   }     f d   } | |   } | d
 k	 r: | St j   d g  y t |      \ } } t |   d k rt d   |  D  r|  d } g  |  d  D] } | | j   ^ q }	 t d   |	 D  rd }
 x' |	 D] } t |
 | j	   d  }
 q W| |
 Sn  WnJ t
 k
 r_} | | j  } | d
 k	 rD| St d t |   |   n X| s| j svt j St d d	 | Sn  | d | d } } x* | D]" } | j |  } | j rPqqW| j s| j   S| Sd
 S(   s   
    Compute GCD of a list of polynomials.

    Examples
    ========

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

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

    c           s    r   r t  |   \ } } | s- | j S| j r | d | d } } x3 | D]+ } | j | |  } | j |  rR PqR qR W| j |  Sn  d  S(   Ni    i   (   R   Rt   R   R!  R  R   RV   (   t   seqRU   t   numbersR   t   number(   RJ   R:   (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   try_non_polynomial_gcd;  s    	RY  i   c         s  s!   |  ] } | j  o | j Vq d  S(   N(   t   is_algebraict   is_irrational(   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pys	   <genexpr>Y  s    ic         s  s   |  ] } | j  Vq d  S(   N(   t   is_rational(   R   t   frc(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pys	   <genexpr>\  s    i    t   gcd_listRK   N(   R   RV   R=   R  R  RN   R4  t   ratsimpR"  t   as_numer_denomR(   R  R)   RY  R   R  R8   R!  R  R`   (   R  R:   RJ   R  R   RY  RK   R  R   t   lstt   lcR  R   R  (    (   RJ   R:   s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  *  sB    (
'			
c         O  s  t  |  d  r; | d k	 r+ | f | } n  t |  | |  S| d k rV t d   n  t j | d g  y t |  | f | |  \ \ } } } t t |  | f  \ } } | j	 r | j
 r | j	 r | j
 r | | j   }	 |	 j r | |	 j   d Sn  Wnq t k
 rq}
 t |
 j  \ } \ } } y | j | j | |   SWqrt k
 rmt d d |
   qrXn X| j |  } | j s| j   S| Sd S(   s   
    Compute GCD of ``f`` and ``g``.

    Examples
    ========

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

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

    t   __iter__s2   gcd() takes 2 arguments or a sequence of argumentsRY  i    R!  i   N(   R   RV   R  R   R=   R  R  R\   R   R  R  R  R  R  R(   R   R  R   R!  R?   R)   RY  R`   (   Rx   Ry   R:   RJ   R|   R}   RK   R  R  R  R   RU   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR!  ~  s0    $$		
c           s  t  |   }     f d   } | |   } | d
 k	 r: | St j   d g  y t |      \ } } t |   d k rt d   |  D  r|  d } g  |  d  D] } | | j   ^ q }	 t d   |	 D  rd }
 x' |	 D] } t |
 | j	   d  }
 q W| |
 Sn  WnJ t
 k
 r_} | | j  } | d
 k	 rD| St d t |   |   n X| s| j svt j St d d | Sn  | d	 | d } } x | D] } | j |  } qW| j s| j   S| Sd
 S(   s   
    Compute LCM of a list of polynomials.

    Examples
    ========

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

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

    c           s    r~   r~ t  |   \ } } | s- | j S| j r~ | d | d } } x  | D] } | j | |  } qR W| j |  Sn  d  S(   Ni    i   (   R   Ru   R   R"  R   RV   (   R  RU   R  R   R  (   RJ   R:   (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   try_non_polynomial_lcm  s    	RY  i   c         s  s!   |  ] } | j  o | j Vq d  S(   N(   R  R  (   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pys	   <genexpr>  s    ic         s  s   |  ] } | j  Vq d  S(   N(   R  (   R   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pys	   <genexpr>  s    t   lcm_listRK   i    N(   R   RV   R=   R  R  RN   R4  R  R"  R  R(   R  R)   RY  R   R   R8   R`   (   R  R:   RJ   R  R   RY  RK   R  R   R  R  R  R   R  (    (   RJ   R:   s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s>    (
'		
c         O  s  t  |  d  r; | d k	 r+ | f | } n  t |  | |  S| d k rV t d   n  t j | d g  y t |  | f | |  \ \ } } } t t |  | f  \ } } | j	 r | j
 r | j	 r | j
 r | | j   }	 |	 j r | |	 j   d Sn  Wnq t k
 rq}
 t |
 j  \ } \ } } y | j | j | |   SWqrt k
 rmt d d |
   qrXn X| j |  } | j s| j   S| Sd S(   s   
    Compute LCM of ``f`` and ``g``.

    Examples
    ========

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

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

    R  s2   lcm() takes 2 arguments or a sequence of argumentsRY  i   R"  i   N(   R   RV   R  R   R=   R  R  R\   R   R  R  R  R  R  R(   R   R  R   R"  R?   R)   RY  R`   (   Rx   Ry   R:   RJ   R|   R}   RK   R  R  R  R   RU   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR"    s0    $$		
c         O  sE  d d l  m } t |   } t |  t  s5 |  j r9 | S| j d t  r |  j g  |  j	 D] } t
 | | |  ^ q[   } | j d  t | d <t
 | | |  St |  |  r |  S| j d t  } t j | d g  y t |  | |  \ } }	 Wn t k
 r}
 |
 j SX| j
   \ } }  |	 j j r|	 j j rV|  j d t  \ } }  n  |  j   \ } }  |	 j j r| | } qn	 t j } t g  t |  j |  D] \ } } | | ^ q  } | d k rt j } | d k r| Sn  | rt | | |  j    St | |  j   d t j   \ } }  t | | |  d t S(	   sz  
    Remove GCD of terms from ``f``.

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

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

    Examples
    ========

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

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

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

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

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

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

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

    Rationals are factored out by default:

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

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

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

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

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

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

    i(   t   Equalityt   deepR  t   clearRY  RX   i   (   t   sympy.core.relationalR  R   RA   R   t   is_AtomR  R   R   RJ   R   R  Rc   R=   R  R  R(   R  RU   R   R   R   R'  R   R   R   R   R:   R   R`   t   as_coeff_Mul(   Rx   R:   RJ   R  R  R  RP   R  R|   RK   R   R   t   denomR[   R   R   t   term(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   2  sD    B1
	5	'c         O  s   t  j | d d g  y t |  | |  \ } } Wn% t k
 rY } t d d |   n X| j t |   } | j s | j   S| Sd S(   s   
    Reduce ``f`` modulo a constant ``p``.

    Examples
    ========

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

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

    R   RY  R#  i   N(	   R=   R  R  R(   R)   R#  R   RY  R`   (   Rx   R$  R:   RJ   R|   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR#    s    	
c         O  s   t  j | d d g  y t |  | |  \ } } Wn% t k
 rY } t d d |   n X| j d | j  } | j s | j   S| Sd S(   s   
    Divide all coefficients of ``f`` by ``LC(f)``.

    Examples
    ========

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

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

    R   RY  R%  i   N(	   R=   R  R  R(   R)   R%  R   RY  R`   (   Rx   R:   RJ   R|   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR%    s    	
c         O  sa   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j   S(   s   
    Compute GCD of coefficients of ``f``.

    Examples
    ========

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

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

    RY  R&  i   (   R=   R  R  R(   R)   R&  (   Rx   R:   RJ   R|   RK   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR&    s    c         O  s   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j   \ } } | j s | | j   f S| | f Sd S(   s  
    Compute content and the primitive form of ``f``.

    Examples
    ========

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

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

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

    Expansion is performed by default:

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

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

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

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

    RY  R'  i   N(   R=   R  R  R(   R)   R'  RY  R`   (   Rx   R:   RJ   R|   RK   R   R(  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR'    s     	c   	      O  s   t  j | d g  y( t |  | f | |  \ \ } } } Wn% t k
 rb } t d d |   n X| j |  } | j s | j   S| Sd S(   s   
    Compute functional composition ``f(g)``.

    Examples
    ========

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

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

    RY  R)  i   N(   R=   R  R  R(   R)   R)  RY  R`   (	   Rx   Ry   R:   RJ   R|   R}   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR)  )  s    (	
c         O  s   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j   } | j s g  | D] } | j   ^ qs S| Sd S(   s   
    Compute functional decomposition of ``f``.

    Examples
    ========

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

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

    RY  R*  i   N(   R=   R  R  R(   R)   R*  RY  R`   (   Rx   R:   RJ   R|   RK   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR*  G  s    	c         O  s   t  j | d d g  y t |  | |  \ } } Wn% t k
 rY } t d d |   n X| j d | j  } | j s g  | D] } | j   ^ q S| Sd S(   s   
    Compute Sturm sequence of ``f``.

    Examples
    ========

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

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

    R   RY  R.  i   N(	   R=   R  R  R(   R)   R.  R   RY  R`   (   Rx   R:   RJ   R|   RK   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR.  e  s    	c   	      O  s   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j   } | j s g  | D] \ } } | j   | f ^ qs S| Sd S(   s/  
    Compute a list of greatest factorial factors of ``f``.

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

    Examples
    ========

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

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

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

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

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

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

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

    RY  R/  i   N(   R=   R  R  R(   R)   R/  RY  R`   (	   Rx   R:   RJ   R|   RK   R   R5  Ry   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR/    s     	)c         O  s   t  d   d S(   s3   Compute greatest factorial factorization of ``f``. s   symbolic falling factorialN(   R?   (   Rx   R:   RJ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   gff  s    c   	      O  s   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j   \ } } } | j s t |  | j   | j   f St |  | | f Sd S(   s  
    Compute square-free norm of ``f``.

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

    Examples
    ========

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

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

    RY  R1  i   N(	   R=   R  R  R(   R)   R1  RY  R   R`   (	   Rx   R:   RJ   R|   RK   R   R   Ry   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR1    s    	c         O  s~   t  j | d g  y t |  | |  \ } } Wn% t k
 rV } t d d |   n X| j   } | j sv | j   S| Sd S(   s   
    Compute square-free part of ``f``.

    Examples
    ========

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

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

    RY  R2  i   N(   R=   R  R  R(   R)   R2  RY  R`   (   Rx   R:   RJ   R|   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR2    s    	
c         C  s1   | d k r d   } n	 d   } t  |  d | S(   s&   Sort a list of ``(expr, exp)`` pairs. R>  c         S  s7   |  \ } } | j  j  } | t |  t | j  | f S(   N(   R9   RN   R:   (   RO   R  t   expR9   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRi    s    c         S  s7   |  \ } } | j  j  } t |  t | j  | | f S(   N(   R9   RN   R:   (   RO   R  R  R9   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRi     s    Ri  (   Rw  (   R5  t   methodRi  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _sorted_factors  s    	c         C  s-   t  g  |  D] \ } } | j   | ^ q
   S(   s*   Multiply a list of ``(expr, exp)`` pairs. (   R   R`   (   R5  Rx   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _factors_product  s    c         C  s  t  j g  } } g  t j |   D]' } t | d  rA | j   n | ^ q  } xJ| D]B} | j rs | | 9} qT n  | j r | j | j	  qT n  | j
 r | j	 \ } }	 | j r |	 j r | | 9} qT n  | j r| j | |	 f  qT qn | t  j } }	 y t | |  \ }
 } Wn) t k
 rE} | j | j |	 f  qT Xt |
 | d  } |   \ } } | t  j k	 r|	 j r| | |	 9} q| j r| j | |	 f  q| j | t  j f  n  |	 t  j k r| j |  qT |	 j r$| j g  | D] \ } } | | |	 f ^ q qT g  } xP | D]H \ } } | j   j rf| j | | |	 f  q1| j | | f  q1W| j t |  |	 f  qT W| | f S(   s.   Helper function for :func:`_symbolic_factor`. t   _eval_factort   _list(   R   R   R   t	   make_argsR   R  R  t   is_MulR  RJ   t   is_PowR   R  R(   R  t   getattrR~  t   is_positivet
   is_integerR`   R  (   R  RK   R  R[   R5  Rm   RJ   t   argt   baseR  R  Rz   R   R   t   _coefft   _factorsRx   R   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _symbolic_factor_list  sT    :	
		
					3c         C  s   t  |  t  rm |  j rm t |  d  r2 |  j   St t |  d | d | |  \ } } t | t |   St |  d  r |  j	 g  |  j
 D] } t | | |  ^ q   St |  d  r |  j g  |  D] } t | | |  ^ q  S|  Sd S(   s%   Helper function for :func:`_factor`. R  t   fractionRJ   R  N(   RA   R   t   is_RelationalR   R  R  R1   R   R  R   RJ   t   _symbolic_factorR]   (   R  RK   R  R[   R5  R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR  G  s    
(/,c         C  s  t  j | d d g  t  j | |  } t |   }  t |  t  r|  j rt |   j   \ } } t	 | | |  \ } } t	 | | |  \ }	 }
 |
 r | j
 r t d |    n  | j t d t   } xh | |
 f D]Z } xQ t |  D]C \ } \ } } | j s t | |  \ } } | | f | | <q q Wq Wt | |  } t |
 |  }
 | j sg  | D] \ } } | j   | f ^ qi} g  |
 D] \ } } | j   | f ^ q}
 n  | |	 } | j
 s| | f S| | |
 f Sn t d |    d S(   s>   Helper function for :func:`sqf_list` and :func:`factor_list`. t   fracRY  s   a polynomial expected, got %sR  N(   R=   R  R>   R   RA   R   R  R1   R  R  R   R%   t   cloneRB   Rc   R   RF   R  R  RY  R`   (   R  R:   RJ   R  RK   t   numerR  R  t   fpR  t   fqt   _optR5  Rm   Rx   R   Rz   R[   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _generic_factor_listV  s2    		+.
	
c         C  sT   | j  d t  } t j | g   t j | |  } | | d <t t |   | |  S(   s4   Helper function for :func:`sqf` and :func:`factor`. R  (   R  Rc   R=   R  R>   R  R   (   R  R:   RJ   R  R  RK   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _generic_factor  s
    
c           s   d d l  m   d	   f d  } d	   f d  } d   } |  j   j r | |   r |  j   } | |  |  } | r | d | d d	 | d f S| |  |  } | r d	 d	 | d | d f Sn  d	 S(
   s!  
    try to transform a polynomial to have rational coefficients

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

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

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

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

    Notes
    =====

    Currently it transforms only polynomials without roots larger than 2.

    Examples
    ========

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

    i(   t   simplifyc           s  d d l  m } t |  j  d k s7 |  j d j rA d |  f S|  j   } |  j   } | ph | j   } | j	   d } g  | D] }   |  ^ q } | d r  | d | d  } g  } x t
 t |   D]< }	   | |	 | |	 d  } | j sPn  | j |  q W  d |  }
 |  j d } | | g } x< t
 d | d  D]' }	 | j | |	 d | | |	  qSW| |   }  t |   }  | |
 |  f Sn  d S(   s$  
        try rescaling ``x -> alpha*x`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the rescaling is successful,
        ``alpha`` is the rescaling factor, and ``f`` is the rescaled
        polynomial; else ``alpha`` is ``None``.
        i(   R   i   i    iN(   t   sympy.core.addR   RN   R:   R  RV   R   R   R%  R   R   R  R   R8   (   Rx   t   f1R   R   R  Rq   t   coeffxt
   rescale1_xt   coeffs1Rm   t	   rescale_xR   RC  (   R  (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _try_rescale  s2    '

	%c           s  d d l  m } t |  j  d k s7 |  j d j rA d |  f S|  j   } | p\ | j   } | j   d }   | d  } | r| j	 r| } | j
 r | j } | j } n	 | g } t | d   d t \ } }	 | |	   | }
 | j |
  } |
 | f Sd S(   s+  
        try translating ``x -> x + alpha`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the translating is successful,
        ``alpha`` is the translating factor, and ``f`` is the shifted
        polynomial; else ``alpha`` is ``None``.
        i(   R   i   i    c         S  s   |  j  S(   N(   R  (   t   z(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRl    Rm  t   binaryN(   R	  R   RN   R:   R  RV   R   R%  R   R  t   is_AddRJ   R   R4   Rc   R+  (   Rx   R
  R   R   Rq   R   R   RJ   t   c1t   c2t   alphat   f2(   R  (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _try_translate  s$    '
			
c   
      S  s   d d l  m } |  j   } t } x | D] } x t j |  D] } | |  j } g  | j   D]6 \ } } | j ra | j	 ra | j
 d k ra | j
 ^ qa }	 |	 s q? n  t |	  d k r t } n  t |	  d k r? t Sq? Wq) W| S(   sS   
        Return True if ``f`` is a sum with square roots but no other root
        i(   t   Factorsi   (   t   sympy.core.exprtoolsR  Rq   R   R   R  R5  RW   R   t   is_RationalR   t   minRc   t   max(
   R$  R  Rq   t   has_sqR   R   Rx   R  t   wxR   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _has_square_roots  s    0	i    i   i   N(   t   sympy.simplify.simplifyR  RV   Rs   Rp   R%  (   Rx   R  R  R  R
  R   (    (   R  s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   to_rational_coeffs  s    &#	c         C  sV  d d l  m } t |  | d d } | j   } t |  } | sG d S| \ } } } }	 t |	 j    }
 | r | |
 d | | |  } | d |  } g  } x |
 d d D]; } | j | | d j	 i | | | 6  | d f  q WnX |
 d } g  } xE |
 d d D]5 } | j | d j	 i | | | 6 | d f  qW| | f S(   s  
    helper function to factor polynomial using to_rational_coeffs

    Examples
    ========

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

    i(   R  RU   t   EXi    i   N(
   R   R  R8   R   R!  RV   R7  R`   R   R   (   R$  R   R  t   p1R   t   resR  R   R  Ry   R5  R   t   r1R  R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _torational_factor_list  s&    <
3c         O  s   t  |  | | d d S(   s   
    Compute a list of square-free factors of ``f``.

    Examples
    ========

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

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

    R  R>  (   R  (   Rx   R:   RJ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR3  ;  s    c         O  s   t  |  | | d d S(   s   
    Compute square-free factorization of ``f``.

    Examples
    ========

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

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

    R  R>  (   R  (   Rx   R:   RJ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR>  M  s    c         O  s   t  |  | | d d S(   s  
    Compute a list of irreducible factors of ``f``.

    Examples
    ========

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

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

    R  R|  (   R  (   Rx   R:   RJ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR7  _  s    c           s%  t  |   }    j d t  r d d l m }    f d   } | |  |  }  i  } |  j t t  } xK | D]C } t |     } | j	 s | j
 rn | | k rn | | | <qn qn W|  j |  Sy t |     d d SWnB t k
 r }	 |  j sd d l m }
 |
 |   St |	   n Xd S(	   s  
    Compute the factorization of expression, ``f``, into irreducibles. (To
    factor an integer into primes, use ``factorint``.)

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

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

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

    Examples
    ========

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

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

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

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

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

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

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

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

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

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

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

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

    R  i(   t	   bottom_upc           s,   t  |      } | j s$ | j r( | S|  S(   sS   
            Factor, but avoid changing the expression when unable to.
            (   R|  R  R  (   R  Ry  (   RJ   R:   (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   _try_factor  s    R  R|  (   t	   factor_ncN(   R   R  R   R   R'  t   atomsR   R   R|  R  R  t   xreplaceR  R%   R  R  R)  (   Rx   R:   RJ   R'  R(  t   partialst   muladdR$  Ry  t   msgR)  (    (   RJ   R:   s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR|  q  s&    D		
c         C  s  t  |  d  sb y t |   }  Wn t k
 r3 g  SX|  j d | d | d | d | d | d |  St |  d d	 \ } }	 t |	 j  d
 k r t  n  x* t |  D] \ }
 } | j	 j	 | |
 <q W| d k	 r|	 j j |  } | d k rt d   qn  | d k	 r"|	 j j |  } n  | d k	 rC|	 j j |  } n  t | |	 j d | d | d | d | d | } g  } xX | D]P \ \ } } } |	 j j |  |	 j j |  } } | j | | f | f  qW| Sd S(   s/  
    Compute isolating intervals for roots of ``f``.

    Examples
    ========

    >>> from sympy import intervals
    >>> from sympy.abc import x

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

    R  R4  R:  R;  R<  R=  R>  RU   R   i   i    s!   'eps' must be a positive rationalR  N(   R   R8   R$   R9  R  RN   R:   R&   R   R9   RV   RU   RX   R   R2   R   R   (   R|   R4  R:  R;  R<  R  R=  R>  RY  RK   Rm   R  R9  R   R   R  R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR9    s4    .	!%c         C  s\   y t  |   } Wn! t k
 r3 t d |    n X| j | | d | d | d | d | S(   s   
    Refine an isolating interval of a root to the given precision.

    Examples
    ========

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

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

    s+   can't refine a root of %s, not a polynomialR:  RH  R=  RJ  (   R8   R$   R%   RG  (   Rx   R   R  R:  RH  R=  RJ  R|   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRG    s    c         C  sP   y t  |  d t } Wn! t k
 r9 t d |    n X| j d | d |  S(   s  
    Return the number of roots of ``f`` in ``[inf, sup]`` interval.

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

    Examples
    ========

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

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

    t   greedys)   can't count roots of %s, not a polynomialR;  R<  (   R8   R   R$   R%   RV  (   Rx   R;  R<  R|   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRV  &  s
    c         C  sJ   y t  |  d t } Wn! t k
 r9 t d |    n X| j d |  S(   s   
    Return a list of real roots with multiplicities of ``f``.

    Examples
    ========

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

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

    R/  s0   can't compute real roots of %s, not a polynomialR]  (   R8   R   R$   R%   R_  (   Rx   R]  R|   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR_  B  s    i   i2   c         C  sV   y t  |  d t } Wn! t k
 r9 t d |    n X| j d | d | d |  S(   sL  
    Compute numerical approximations of roots of ``f``.

    Examples
    ========

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

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

    R/  s5   can't compute numerical roots of %s, not a polynomialR   Re  Rf  (   R8   R   R$   R%   Rz  (   Rx   R   Re  Rf  R|   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRz  Z  s    c         O  s^   t  j | g   y t |  | |  \ } } Wn% t k
 rS } t d d |   n X| j   S(   s   
    Compute roots of ``f`` by factorization in the ground domain.

    Examples
    ========

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

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

    R}  i   (   R=   R  R  R(   R)   R}  (   Rx   R:   RJ   R|   RK   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR}  t  s    c         O  s~   t  j | g   y t |  | |  \ } } Wn% t k
 rS } t d d |   n X| j |  } | j sv | j   S| Sd S(   s  
    Construct a polynomial with n-th powers of roots of ``f``.

    Examples
    ========

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

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

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

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

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

    R  i   N(   R=   R  R  R(   R)   R  RY  R`   (   Rx   R   R:   RJ   R|   RK   R   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    	
c           s  d d l  m } d d l m   t j | d g  t |   }  t |  t t	 f  s |  j
 s| t |  t  s| t |  t  r |  S| |  d t }  |  j   \ } } nJ t |   d k r |  \ } } n) t |  t	  r | |   St d |    y( t | | f | |  \ \ } } } Wnt k
 rUt |  t t	 f  sB|  St j | | f SnKt k
 r}	 |  j r|  j    rt |	   n  |  j s|  j rt |  j   f d   d	 t \ }
 } g  | D] } t |  ^ q} |  j t |  j |
    |  Sg  } t |   } t |  xg | D]_ } t | t t	 t  f  rJq&n  y' | j! | t |  f  | j"   Wq&t# k
 rq&Xq&W|  j$ t% |   Sn X| j |  \ }
 } } t |  t t	 f  s|
 | j&   | j&   S| j' s|
 | j&   | j&   f S|
 | | f Sd
 S(   sd  
    Cancel common factors in a rational function ``f``.

    Examples
    ========

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

    >>> cancel((2*x**2 - 2)/(x**2 - 2*x + 1))
    (2*x + 2)/(x - 1)
    >>> cancel((sqrt(3) + sqrt(15)*A)/(sqrt(2) + sqrt(10)*A))
    sqrt(6)/2
    i(   t   factor_terms(   R  RY  t   radicali   s   unexpected argument: %sc           s   |  j  t k o |  j    S(   N(   R  Rc   t   has(   R   (   R  (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRl    s    R  N((   R  R0  R  R  R=   R  R   RA   R   R
   R  R   R   Rc   R  RN   R   R  R(   R   R   R%   R  R2  R  R  R4   RJ   R  R   R   R  R   R   t   skipR?   R+  RB   R`   RY  (   Rx   R:   RJ   R0  R$  R   R|   R}   RK   R.  R   t   ncRm   R  t   pott   eR-  R   (    (   R  s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s\    (
(
	c         O  s/  t  j | d d g  y) t |  g t |  | |  \ } } Wn% t k
 rf } t d d |   n X| j } t } | j r | j	 r | j
 r | j t d | j     } t } n  d d l m }	 |	 | j | j | j  \ }
 } xH t |  D]: \ } } | j | j  j j   } |
 j |  | | <q W| d j | d  \ } } g  | D] } t j t |  |  ^ qY} t j t |  |  } | ry0 g  | D] } | j   ^ q| j   } } Wn t k
 rqX| | } } n  | j s!g  | D] } | j   ^ q| j   f S| | f Sd	 S(
   s<  
    Reduces a polynomial ``f`` modulo a set of polynomials ``G``.

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

    Examples
    ========

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

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

    RY  R   t   reducedi    RU   i(   t   xringi   N(   R=   R  R  RE   R(   R)   RU   R   R   R   R   R  RB   t	   get_fieldRc   t   sympy.polys.ringsR8  R:   R;   R   Rb   R9   R   RQ   R   R8   RC   R   R"   RY  R`   (   Rx   R}   R:   RJ   RY  RK   R   RU   R   R8  t   _ringRz   Rm   R  R   R   R   t   _Qt   _r(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR7    s6    )		!+0	)c         O  s   t  |  | |  S(   s  
    Computes the reduced Groebner basis for a set of polynomials.

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

    For more information on Groebner bases, see the references and the docstring
    of `solve_poly_system()`.

    Examples
    ========

    Example taken from [1].

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

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

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

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

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

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

    References
    ==========

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

    (   t   GroebnerBasis(   R|   R:   RJ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR   ?  s    3c         O  s   t  |  | |  j S(   s[  
    Checks if the ideal generated by a Groebner basis is zero-dimensional.

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

    References
    ==========

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

    (   R>  t   is_zero_dimensional(   R|   R:   RJ   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR?  u  s    R>  c           B  s   e  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 d	   Z d
   Z d   Z d   Z d   Z d   Z e d    Z d   Z e d  Z d   Z RS(   s%   Represents a reduced Groebner basis. c         O  s	  t  j | d d g  y t | | |  \ } } Wn+ t k
 r_ } t d t |  |   n Xd d l m } | | j | j	 | j
  } g  | D]$ }	 |	 r | j |	 j j    ^ q } t | | d | j }
 g  |
 D] } t j | |  ^ q }
 |  j |
 |  S(   s>   Compute a reduced Groebner basis for a system of polynomials. RY  R  R   i(   t   PolyRing(   R=   R  R  R(   R)   RN   R:  R@  R:   RU   R;   RQ   R9   R   t	   _groebnerR  R8   RC   t   _new(   RI   R|   R:   RJ   RY  RK   R   R@  t   ringR  R}   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRL     s    1%c         C  s+   t  j |   } t |  | _ | | _ | S(   N(   R   RL   R   t   _basist   _options(   RI   t   basisR=   RO   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRB    s    	c         C  s   t  |  j   t  |  j j   f S(   N(   R
   RD  RE  R:   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRJ     s    c         C  s    g  |  j  D] } | j   ^ q
 S(   N(   RD  R`   (   Re   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s   t  |  j  S(   N(   RE   RD  (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRY    s    c         C  s
   |  j  j S(   N(   RE  R:   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR:     s    c         C  s
   |  j  j S(   N(   RE  RU   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRU     s    c         C  s
   |  j  j S(   N(   RE  R;   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR;     s    c         C  s   t  |  j  S(   N(   RN   RD  (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __len__  s    c         C  s*   |  j  j r t |  j  St |  j  Sd  S(   N(   RE  RY  t   iterR  (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  s)   |  j  j r |  j } n	 |  j } | | S(   N(   RE  RY  R  (   Re   t   itemRF  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   __getitem__  s    	c         C  s"   t  |  j t |  j j    f  S(   N(   t   hashRD  R   RE  RW   (   Re   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRh     s    c         C  sp   t  | |  j  r4 |  j | j k o3 |  j | j k St |  rh |  j t |  k pg |  j t |  k St Sd  S(   N(	   RA   R]   RD  RE  R   RY  RE   R  R   (   Re   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s
    "(c         C  s   |  | k S(   N(    (   Re   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    c         C  sz   d   } t  d g t |  j   } |  j j } x< |  j D]1 } | j d |  } | |  r; | | 9} q; q; Wt |  S(   s{  
        Checks if the ideal generated by a Groebner basis is zero-dimensional.

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

        References
        ==========

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

        c         S  s   t  t t |    d k S(   Ni   (   t   sumR\   R  (   t   monomial(    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt
   single_var  s    i    R;   (   R   RN   R:   RE  R;   RY  R   R4  (   Re   RN  R   R;   R  RM  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR?    s    	c         C  s  |  j  } | j } t |  } | | k r. |  S|  j sF t d   n  t |  j  } | j } | j t	 d | j
   d |   } d d l m } | | j | j |  \ } }	 xH t |  D]: \ }
 } | j | j  j j   } | j |  | |
 <q Wt | | |  } g  | D] } t j t	 |  |  ^ q} | j svg  | D] } | j d t  d ^ qH} | | _ n  |  j | |  S(   s  
        Convert a Groebner basis from one ordering to another.

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

        Examples
        ========

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

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

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

        References
        ==========

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

        s>   can't convert Groebner bases of ideals with positive dimensionRU   R;   i(   R8  RX   i   (   RE  R;   R   R?  R?   RE   RD  RU   R  RB   R9  R:  R8  R:   R   Rb   R9   R   RQ   R   R8   RC   R   R   Rc   RB  (   Re   R;   RK   t	   src_ordert	   dst_orderRY  RU   R8  R;  Rz   Rm   R  R}   Ry   (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   fglm  s.    				+	)c         C  s  t  j | |  j  } | g t |  j  } |  j } | j } t } | r | j r | j r | j	 t
 d | j     } t } n  d d l m } | | j | j | j  \ }	 }
 xH t |  D]: \ } } | j | j  j j   } |	 j |  | | <q W| d j | d  \ } } g  | D] } t  j t
 |  |  ^ q#} t  j t
 |  |  } | ry0 g  | D] } | j   ^ qo| j   } } Wn t k
 rqX| | } } n  | j sg  | D] } | j   ^ q| j   f S| | f Sd S(   s#  
        Reduces a polynomial modulo a Groebner basis.

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

        Examples
        ========

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

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

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

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

        RU   i(   R8  i    i   N(   R8   RH   RE  RE   RD  RU   R   R   R   R  RB   R9  Rc   R:  R8  R:   R;   R   Rb   R9   R   RQ   R   RC   R   R"   RY  R`   (   Re   R  R   R  RY  RK   RU   R   R8  R;  Rz   Rm   R   R   R   R<  R=  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   reduce@  s2    			!+0	)c         C  s   |  j  |  d d k S(   sm  
        Check if ``poly`` belongs the ideal generated by ``self``.

        Examples
        ========

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

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

        >>> G.contains(f)
        True
        >>> G.contains(f + 1)
        False

        i   i    (   RR  (   Re   R  (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   contains  s    (   R  R  R  RL   R  RB  R  RJ   R  RY  R:   RU   R;   RG  R  RJ  Rh   R  R  R?  RQ  Rc   RR  RS  (    (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR>    s&   								 	BAc           s   t  j  g      f d     t |   }  |  j rG t |  |   Sd  k r` t  d <n  t  j |   }   |  |  S(   s   
    Efficiently transform an expression into a polynomial.

    Examples
    ========

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

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

    c   
        s!  g  g  } } xat  j |   D]P} g  g  } } x t j |  D] } | j rh | j   | |   q@ | j r | j j r | j j r | j d k r | j   | j |  j	 | j   q@ | j |  q@ W| s | j |  q | d } x! | d D] } | j
 |  } q W| r`t |   } | j rB| j
 |  } q`| j
 t j | |   } n  | j |  q W| st j |  |  }	 ny | d }	 x! | d D] } |	 j |  }	 qW| rt  |   } | j r|	 j |  }	 q|	 j t j | |   }	 n  |	 j | j d d     S(   Ni    i   R:   (    (   R   R  R   R  R   R  R  R  R~  R   R   R  R8   RH   R   Ra   R  (
   R  RK   R   t
   poly_termsR  R5  t   poly_factorsR|  t   productR   (   t   _polyRJ   (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyRW    sB    	"
	
	R  (   R=   R  R   RF   R8   R   R>   (   R  R:   RJ   RK   (    (   RW  RJ   s4   lib/python2.7/site-packages/sympy/polys/polytools.pyR    s    4	(   R  t
   __future__R    R   t
   sympy.coreR   R   R   R   R   R   R   R	   R
   t   sympy.core.basicR   t   sympy.core.compatibilityR   R   R   t   sympy.core.decoratorsR   t   sympy.core.functionR   t   sympy.core.mulR   R  R   t   sympy.core.symbolR   t   sympy.core.sympifyR   t   sympy.logic.boolalgR   t   sympy.polysR   R=   t   sympy.polys.constructorR   t   sympy.polys.domainsR   R   R   t   sympy.polys.fglmtoolsR   t   sympy.polys.groebnertoolsR   RA  t   sympy.polys.monomialsR   t   sympy.polys.orderingsR   t   sympy.polys.polyclassesR   t   sympy.polys.polyerrorsR    R!   R"   R#   R$   R%   R&   R'   R(   R)   R*   t   sympy.polys.polyutilsR+   R,   R-   R.   R/   R0   t   sympy.polys.rationaltoolsR1   t   sympy.polys.rootisolationR2   t   sympy.utilitiesR3   R4   R5   R6   RX  Rr  t   mpmath.libmp.libhyperR7   R8   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  RV   R!  R  R"  R   R#  R%  R&  R'  R)  R*  R.  R/  R  R1  R2  R  R  R  R  R  R  R!  R&  R3  R>  R7  R|  R   R9  RG  RV  Rc   R_  Rz  R}  R  R  R7  R?  R>  R  (    (    (    s4   lib/python2.7/site-packages/sympy/polys/polytools.pyt   <module>   s   @L
."                ]	(	_	
95##   #''4&)T2N2t./"			:		)				,e!7'O<6 