ó
¡¼™\c           @  s  d  Z  d d l m Z m Z d d l m Z d d l m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z 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$ m% Z% m& Z& m' Z' m( Z( m) Z) m* Z* m+ Z+ m, Z, m- Z- m. Z. d d l/ m0 Z0 m1 Z1 d d l2 m3 Z3 d d l4 m5 Z6 m7 Z8 d	 „  Z9 d
 „  Z: d „  Z; d „  Z< d „  Z= d „  Z> d „  Z? d „  Z@ d „  ZA d „  ZB d „  ZC d „  ZD d „  ZE d „  ZF d „  ZG d „  ZH d „  ZI d „  ZJ d „  ZK d „  ZL d „  ZM d „  ZN d „  ZO d  „  ZP d! „  ZQ d" „  ZR d# „  ZS d$ „  ZT d% „  ZU d& „  ZV d' „  ZW d( „  ZX d) „  ZY d* „  ZZ d+ „  Z[ d, „  Z\ d- „  Z] d. „  Z^ d/ „  Z_ d0 „  Z` d6 eb d1 „ Zc d2 „  Zd d6 eb d3 „ Ze d4 „  Zf d5 „  Zg d6 S(7   sH   Advanced tools for dense recursive polynomials in ``K[x]`` or ``K[X]``. iÿÿÿÿ(   t   print_functiont   division(   t   range(   t   dup_add_termt   dmp_add_termt
   dup_lshiftt   dup_addt   dmp_addt   dup_subt   dmp_subt   dup_mult   dmp_mult   dup_sqrt   dup_divt   dup_remt   dmp_remt
   dmp_expandt   dup_mul_groundt   dmp_mul_groundt   dup_quo_groundt   dmp_quo_groundt   dup_exquo_groundt   dmp_exquo_ground(   t	   dup_stript	   dmp_stript   dup_convertt   dmp_convertt
   dup_degreet
   dmp_degreet   dmp_to_dictt   dmp_from_dictt   dup_LCt   dmp_LCt   dmp_ground_LCt   dup_TCt   dmp_TCt   dmp_zerot
   dmp_groundt
   dmp_zero_pt   dup_to_raw_dictt   dup_from_raw_dictt	   dmp_zeros(   t   MultivariatePolynomialErrort   DomainError(   t
   variations(   t   ceilt   logc         C  s¦   | d k s |  r |  S| j  g | } xx t t |  ƒ ƒ D]d \ } } | d } x) t d | ƒ D] } | | | d 9} q` W| j d | j | | | ƒ ƒ ƒ q: W| S(   s  
    Computes the indefinite integral of ``f`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_integrate(x**2 + 2*x, 1)
    1/3*x**3 + x**2
    >>> R.dup_integrate(x**2 + 2*x, 2)
    1/12*x**4 + 1/3*x**3

    i    i   (   t   zerot	   enumeratet   reversedR   t   insertt   exquo(   t   ft   mt   Kt   gt   it   ct   nt   j(    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_integrate)   s    
&c   
      C  sØ   | s t  |  | | ƒ S| d k s1 t |  | ƒ r5 |  St | | d | ƒ | d } } x{ t t |  ƒ ƒ D]g \ } } | d } x) t d | ƒ D] }	 | | |	 d 9} q W| j d t | | | ƒ | | ƒ ƒ qi W| S(   s&  
    Computes the indefinite integral of ``f`` in ``x_0`` in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_integrate(x + 2*y, 1)
    1/2*x**2 + 2*x*y
    >>> R.dmp_integrate(x + 2*y, 2)
    1/6*x**3 + x**2*y

    i    i   (   R<   R&   R)   R0   R1   R   R2   R   (
   R4   R5   t   uR6   R7   t   vR8   R9   R:   R;   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_integrateI   s    !
)c      
   C  si   | | k r t  |  | | | ƒ S| d | d } } t g  |  D]! } t | | | | | | ƒ ^ q> | ƒ S(   s.   Recursive helper for :func:`dmp_integrate_in`.i   (   R?   R   t   _rec_integrate_in(   R7   R5   R>   R8   R;   R6   t   wR9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyR@   l   s    c         C  sJ   | d k  s | | k r1 t  d | | f ƒ ‚ n  t |  | | d | | ƒ S(   s+  
    Computes the indefinite integral of ``f`` in ``x_j`` in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_integrate_in(x + 2*y, 1, 0)
    1/2*x**2 + 2*x*y
    >>> R.dmp_integrate_in(x + 2*y, 1, 1)
    x*y + y**2

    i    s(   0 <= j <= u expected, got u = %d, j = %d(   t
   IndexErrorR@   (   R4   R5   R;   R=   R6   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_integrate_inv   s    c         C  sî   | d k r |  St  |  ƒ } | | k  r, g  Sg  } | d k rx x£ |  |  D]' } | j | | ƒ | ƒ | d 8} qJ Wnl xi |  |  D]\ } | } x, t | d | | d ƒ D] } | | 9} q« W| j | | ƒ | ƒ | d 8} q„ Wt | ƒ S(   s#  
    ``m``-th order derivative of a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_diff(x**3 + 2*x**2 + 3*x + 4, 1)
    3*x**2 + 4*x + 3
    >>> R.dup_diff(x**3 + 2*x**2 + 3*x + 4, 2)
    6*x + 4

    i    i   iÿÿÿÿ(   R   t   appendR   R   (   R4   R5   R6   R:   t   derivt   coefft   kR8   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_diffŒ   s"    !c   
      C  s1  | s t  |  | | ƒ S| d k r& |  St |  | ƒ } | | k  rK t | ƒ Sg  | d } } | d k r­ x¹ |  |  D]2 } | j t | | | ƒ | | ƒ ƒ | d 8} qt Wnw xt |  |  D]g } | } x, t | d | | d ƒ D] }	 | |	 9} qà W| j t | | | ƒ | | ƒ ƒ | d 8} q¹ Wt | | ƒ S(   s3  
    ``m``-th order derivative in ``x_0`` of a polynomial in ``K[X]``.

    Examples
    ========

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

    >>> f = x*y**2 + 2*x*y + 3*x + 2*y**2 + 3*y + 1

    >>> R.dmp_diff(f, 1)
    y**2 + 2*y + 3
    >>> R.dmp_diff(f, 2)
    0

    i    i   iÿÿÿÿ(   RH   R   R$   RD   R   R   R   (
   R4   R5   R=   R6   R:   RE   R>   RF   RG   R8   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_diff·   s&    
"!"c      
   C  si   | | k r t  |  | | | ƒ S| d | d } } t g  |  D]! } t | | | | | | ƒ ^ q> | ƒ S(   s)   Recursive helper for :func:`dmp_diff_in`.i   (   RI   R   t   _rec_diff_in(   R7   R5   R>   R8   R;   R6   RA   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyRJ   æ   s    c         C  sJ   | d k  s | | k r1 t  d | | f ƒ ‚ n  t |  | | d | | ƒ S(   sS  
    ``m``-th order derivative in ``x_j`` of a polynomial in ``K[X]``.

    Examples
    ========

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

    >>> f = x*y**2 + 2*x*y + 3*x + 2*y**2 + 3*y + 1

    >>> R.dmp_diff_in(f, 1, 0)
    y**2 + 2*y + 3
    >>> R.dmp_diff_in(f, 1, 1)
    2*x*y + 2*x + 4*y + 3

    i    s   0 <= j <= %s expected, got %s(   RB   RJ   (   R4   R5   R;   R=   R6   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_diff_inð   s    c         C  sE   | s t  |  | ƒ S| j } x" |  D] } | | 9} | | 7} q# W| S(   sÞ   
    Evaluate a polynomial at ``x = a`` in ``K[x]`` using Horner scheme.

    Examples
    ========

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

    >>> R.dup_eval(x**2 + 2*x + 3, 2)
    11

    (   R"   R/   (   R4   t   aR6   t   resultR9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_eval  s    	
c         C  s†   | s t  |  | | ƒ S| s) t |  | ƒ St |  | ƒ | d } } x< |  d D]0 } t | | | | ƒ } t | | | | ƒ } qN W| S(   sò   
    Evaluate a polynomial at ``x_0 = a`` in ``K[X]`` using the Horner scheme.

    Examples
    ========

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

    >>> R.dmp_eval(2*x*y + 3*x + y + 2, 2)
    5*y + 8

    i   (   RN   R#   R    R   R   (   R4   RL   R=   R6   RM   R>   RF   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_eval"  s    c      
   C  si   | | k r t  |  | | | ƒ S| d | d } } t g  |  D]! } t | | | | | | ƒ ^ q> | ƒ S(   s)   Recursive helper for :func:`dmp_eval_in`.i   (   RO   R   t   _rec_eval_in(   R7   RL   R>   R8   R;   R6   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyRP   ?  s    c         C  sJ   | d k  s | | k r1 t  d | | f ƒ ‚ n  t |  | | d | | ƒ S(   s2  
    Evaluate a polynomial at ``x_j = a`` in ``K[X]`` using the Horner scheme.

    Examples
    ========

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

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

    >>> R.dmp_eval_in(f, 2, 0)
    5*y + 8
    >>> R.dmp_eval_in(f, 2, 1)
    7*x + 4

    i    s   0 <= j <= %s expected, got %s(   RB   RP   (   R4   RL   R;   R=   R6   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_eval_inI  s    c         C  sŽ   | | k r  t  |  | d | ƒ Sg  |  D]" } t | | d | | | ƒ ^ q' } | | t | ƒ d k  rm | St  | | | | d | ƒ Sd S(   s+   Recursive helper for :func:`dmp_eval_tail`.iÿÿÿÿi   N(   RN   t   _rec_eval_tailt   len(   R7   R8   t   AR=   R6   R9   t   h(    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyRR   a  s    /c         C  sz   | s
 |  St  |  | ƒ r- t | t | ƒ ƒ St |  d | | | ƒ } | t | ƒ d k r_ | St | | t | ƒ ƒ Sd S(   s!  
    Evaluate a polynomial at ``x_j = a_j, ...`` in ``K[X]``.

    Examples
    ========

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

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

    >>> R.dmp_eval_tail(f, [2])
    7*x + 4
    >>> R.dmp_eval_tail(f, [2, 2])
    18

    i    i   N(   R&   R$   RS   RR   R   (   R4   RT   R=   R6   t   e(    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_eval_tailn  s    c         C  s{   | | k r. t  t |  | | | ƒ | | | ƒ S| d | d } } t g  |  D]$ } t | | | | | | | ƒ ^ qM | ƒ S(   s+   Recursive helper for :func:`dmp_diff_eval`.i   (   RO   RI   R   t   _rec_diff_eval(   R7   R5   RL   R>   R8   R;   R6   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyRX   Ž  s    "c         C  sl   | | k r( t  d | | | f ƒ ‚ n  | sP t t |  | | | ƒ | | | ƒ St |  | | | d | | ƒ S(   s]  
    Differentiate and evaluate a polynomial in ``x_j`` at ``a`` in ``K[X]``.

    Examples
    ========

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

    >>> f = x*y**2 + 2*x*y + 3*x + 2*y**2 + 3*y + 1

    >>> R.dmp_diff_eval_in(f, 1, 2, 0)
    y**2 + 2*y + 3
    >>> R.dmp_diff_eval_in(f, 1, 2, 1)
    6*x + 11

    s   -%s <= j < %s expected, got %si    (   RB   RO   RI   RX   (   R4   R5   RL   R;   R=   R6   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_diff_eval_in˜  s
    "c         C  s…   | j  r^ g  } xi |  D]A } | | } | | d k rJ | j | | ƒ q | j | ƒ q Wn g  |  D] } | | ^ qe } t | ƒ S(   sô   
    Reduce a ``K[x]`` polynomial modulo a constant ``p`` in ``K``.

    Examples
    ========

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

    >>> R.dup_trunc(2*x**3 + 3*x**2 + 5*x + 7, ZZ(3))
    -x**3 - x + 1

    i   (   t   is_ZZRD   R   (   R4   t   pR6   R7   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt	   dup_trunc²  s    	
c         C  s3   t  g  |  D] } t | | | d | ƒ ^ q
 | ƒ S(   s9  
    Reduce a ``K[X]`` polynomial modulo a polynomial ``p`` in ``K[Y]``.

    Examples
    ========

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

    >>> f = 3*x**2*y + 8*x**2 + 5*x*y + 6*x + 2*y + 3
    >>> g = (y - 1).drop(x)

    >>> R.dmp_trunc(f, g)
    11*x**2 + 11*x + 5

    i   (   R   R   (   R4   R[   R=   R6   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt	   dmp_truncÐ  s    c         C  sO   | s t  |  | | ƒ S| d } t g  |  D] } t | | | | ƒ ^ q* | ƒ S(   s   
    Reduce a ``K[X]`` polynomial modulo a constant ``p`` in ``K``.

    Examples
    ========

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

    >>> f = 3*x**2*y + 8*x**2 + 5*x*y + 6*x + 2*y + 3

    >>> R.dmp_ground_trunc(f, ZZ(3))
    -x**2 - x*y - y

    i   (   R\   R   t   dmp_ground_trunc(   R4   R[   R=   R6   R>   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyR^   ä  s    
c         C  s@   |  s
 |  St  |  | ƒ } | j | ƒ r, |  St |  | | ƒ Sd S(   s7  
    Divide all coefficients by ``LC(f)`` in ``K[x]``.

    Examples
    ========

    >>> from sympy.polys import ring, ZZ, QQ

    >>> R, x = ring("x", ZZ)
    >>> R.dup_monic(3*x**2 + 6*x + 9)
    x**2 + 2*x + 3

    >>> R, x = ring("x", QQ)
    >>> R.dup_monic(3*x**2 + 4*x + 2)
    x**2 + 4/3*x + 2/3

    N(   R   t   is_oneR   (   R4   R6   t   lc(    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt	   dup_monicü  s    c         C  sb   | s t  |  | ƒ St |  | ƒ r& |  St |  | | ƒ } | j | ƒ rK |  St |  | | | ƒ Sd S(   sÃ  
    Divide all coefficients by ``LC(f)`` in ``K[X]``.

    Examples
    ========

    >>> from sympy.polys import ring, ZZ, QQ

    >>> R, x,y = ring("x,y", ZZ)
    >>> f = 3*x**2*y + 6*x**2 + 3*x*y + 9*y + 3

    >>> R.dmp_ground_monic(f)
    x**2*y + 2*x**2 + x*y + 3*y + 1

    >>> R, x,y = ring("x,y", QQ)
    >>> f = 3*x**2*y + 8*x**2 + 5*x*y + 6*x + 2*y + 3

    >>> R.dmp_ground_monic(f)
    x**2*y + 8/3*x**2 + 5/3*x*y + 2*x + 2/3*y + 1

    N(   Ra   R&   R!   R_   R   (   R4   R=   R6   R`   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_ground_monic  s    c         C  s’   d d l  m } |  s | j S| j } | | k rX xY |  D] } | j | | ƒ } q9 Wn6 x3 |  D]+ } | j | | ƒ } | j | ƒ r_ Pq_ q_ W| S(   sA  
    Compute the GCD of coefficients of ``f`` in ``K[x]``.

    Examples
    ========

    >>> from sympy.polys import ring, ZZ, QQ

    >>> R, x = ring("x", ZZ)
    >>> f = 6*x**2 + 8*x + 12

    >>> R.dup_content(f)
    2

    >>> R, x = ring("x", QQ)
    >>> f = 6*x**2 + 8*x + 12

    >>> R.dup_content(f)
    2

    iÿÿÿÿ(   t   QQ(   t   sympy.polys.domainsRc   R/   t   gcdR_   (   R4   R6   Rc   t   contR9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_content=  s    	c         C  sÑ   d d l  m } | s# t |  | ƒ St |  | ƒ r9 | j S| j | d } } | | k r‹ xq |  D]$ } | j | t | | | ƒ ƒ } q` WnB x? |  D]7 } | j | t | | | ƒ ƒ } | j | ƒ r’ Pq’ q’ W| S(   sa  
    Compute the GCD of coefficients of ``f`` in ``K[X]``.

    Examples
    ========

    >>> from sympy.polys import ring, ZZ, QQ

    >>> R, x,y = ring("x,y", ZZ)
    >>> f = 2*x*y + 6*x + 4*y + 12

    >>> R.dmp_ground_content(f)
    2

    >>> R, x,y = ring("x,y", QQ)
    >>> f = 2*x*y + 6*x + 4*y + 12

    >>> R.dmp_ground_content(f)
    2

    iÿÿÿÿ(   Rc   i   (   Rd   Rc   Rg   R&   R/   Re   t   dmp_ground_contentR_   (   R4   R=   R6   Rc   Rf   R>   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyRh   g  s    %c         C  sU   |  s | j  |  f St |  | ƒ } | j | ƒ r; | |  f S| t |  | | ƒ f Sd S(   st  
    Compute content and the primitive form of ``f`` in ``K[x]``.

    Examples
    ========

    >>> from sympy.polys import ring, ZZ, QQ

    >>> R, x = ring("x", ZZ)
    >>> f = 6*x**2 + 8*x + 12

    >>> R.dup_primitive(f)
    (2, 3*x**2 + 4*x + 6)

    >>> R, x = ring("x", QQ)
    >>> f = 6*x**2 + 8*x + 12

    >>> R.dup_primitive(f)
    (2, 3*x**2 + 4*x + 6)

    N(   R/   Rg   R_   R   (   R4   R6   Rf   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_primitive”  s    
c         C  sw   | s t  |  | ƒ St |  | ƒ r/ | j |  f St |  | | ƒ } | j | ƒ rZ | |  f S| t |  | | | ƒ f Sd S(   sš  
    Compute content and the primitive form of ``f`` in ``K[X]``.

    Examples
    ========

    >>> from sympy.polys import ring, ZZ, QQ

    >>> R, x,y = ring("x,y", ZZ)
    >>> f = 2*x*y + 6*x + 4*y + 12

    >>> R.dmp_ground_primitive(f)
    (2, x*y + 3*x + 2*y + 6)

    >>> R, x,y = ring("x,y", QQ)
    >>> f = 2*x*y + 6*x + 4*y + 12

    >>> R.dmp_ground_primitive(f)
    (2, x*y + 3*x + 2*y + 6)

    N(   Ri   R&   R/   Rh   R_   R   (   R4   R=   R6   Rf   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_ground_primitiveµ  s    
c         C  ss   t  |  | ƒ } t  | | ƒ } | j | | ƒ } | j | ƒ sf t |  | | ƒ }  t | | | ƒ } n  | |  | f S(   s  
    Extract common content from a pair of polynomials in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_extract(6*x**2 + 12*x + 18, 4*x**2 + 8*x + 12)
    (2, 3*x**2 + 6*x + 9, 2*x**2 + 4*x + 6)

    (   Rg   Re   R_   R   (   R4   R7   R6   t   fct   gcRe   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_extractÙ  s    c         C  s   t  |  | | ƒ } t  | | | ƒ } | j | | ƒ } | j | ƒ sr t |  | | | ƒ }  t | | | | ƒ } n  | |  | f S(   s  
    Extract common content from a pair of polynomials in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_ground_extract(6*x*y + 12*x + 18, 4*x*y + 8*x + 12)
    (2, 3*x*y + 6*x + 9, 2*x*y + 4*x + 6)

    (   Rh   Re   R_   R   (   R4   R7   R=   R6   Rk   Rl   Re   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_ground_extractó  s    c   
      C  sŒ  | j  r' | j r' t d | ƒ ‚ n  t d ƒ } t d ƒ } |  sO | | f S| j | j g g | j g g  g g } t |  d d ƒ } xH |  d D]< } t | | d | ƒ } t | t | d ƒ d d | ƒ } q” Wt	 | ƒ } xŸ | j
 ƒ  D]‘ \ } } | d }	 |	 s!t | | d | ƒ } qí |	 d k rEt | | d | ƒ } qí |	 d k rit | | d | ƒ } qí t | | d | ƒ } qí W| | f S(   s4  
    Return bivariate polynomials ``f1`` and ``f2``, such that ``f = f1 + f2*I``.

    Examples
    ========

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

    >>> R.dup_real_imag(x**3 + x**2 + x + 1)
    (x**3 + x**2 - 3*x*y**2 + x - y**2 + 1, 3*x**2*y + 2*x*y - y**3 + y)

    s;   computing real and imaginary parts is not supported over %si   i    i   i   (   RZ   t   is_QQR+   R$   t   oneR/   R%   R   R   R'   t   itemsR   R	   (
   R4   R6   t   f1t   f2R7   RU   R9   t   HRG   R5   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_real_imag  s,    
'%
c         C  sF   t  |  ƒ }  x3 t t |  ƒ d d d ƒ D] } |  | |  | <q) W|  S(   sô   
    Evaluate efficiently the composition ``f(-x)`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_mirror(x**3 + 2*x**2 - 4*x + 2)
    -x**3 + 2*x**2 + 4*x + 2

    i   iÿÿÿÿiþÿÿÿ(   t   listR   RS   (   R4   R6   R8   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt
   dup_mirror<  s    #c         C  sf   t  |  ƒ t |  ƒ d | }  } } x; t | d d d ƒ D]# } | |  | | | |  | <} q; W|  S(   sæ   
    Evaluate efficiently composition ``f(a*x)`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_scale(x**2 - 2*x + 1, ZZ(2))
    4*x**2 - 4*x + 1

    i   iÿÿÿÿ(   Rv   RS   R   (   R4   RL   R6   R:   t   bR8   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt	   dup_scaleR  s    $!c         C  st   t  |  ƒ t |  ƒ d }  } xP t | d d ƒ D]< } x3 t d | ƒ D]" } |  | d c | |  | 7<qF Wq0 W|  S(   sç   
    Evaluate efficiently Taylor shift ``f(x + a)`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_shift(x**2 - 2*x + 1, ZZ(2))
    x**2 + 2*x + 1

    i   i    iÿÿÿÿ(   Rv   RS   R   (   R4   RL   R6   R:   R8   R;   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt	   dup_shifth  s
    $c   	      C  sÐ   |  s
 g  St  |  ƒ d } |  d g | j g g } } x4 t d | ƒ D]# } | j t | d | | ƒ ƒ qG Wx[ t |  d | d ƒ D]B \ } } t | | | ƒ } t | | | ƒ } t | | | ƒ } q† W| S(   s  
    Evaluate functional transformation ``q**n * f(p/q)`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_transform(x**2 - 2*x + 1, x**2 + 1, x - 1)
    x**4 - 2*x**3 + 5*x**2 - 4*x + 4

    i   i    iÿÿÿÿ(   RS   Rp   R   RD   R
   t   zipR   R   (	   R4   R[   t   qR6   R:   RU   t   QR8   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_transform  s    !$c         C  s‹   t  | ƒ d k r4 t t |  t | | ƒ | ƒ g ƒ S|  s> g  S|  d g } x9 |  d D]- } t | | | ƒ } t | | d | ƒ } qV W| S(   s×   
    Evaluate functional composition ``f(g)`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_compose(x**2 + x, x - 1)
    x**2 - x

    i   i    (   RS   R   RN   R   R
   R   (   R4   R7   R6   RU   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_composež  s    "c         C  s|   | s t  |  | | ƒ St |  | ƒ r) |  S|  d g } x? |  d D]3 } t | | | | ƒ } t | | d | | ƒ } qA W| S(   sÞ   
    Evaluate functional composition ``f(g)`` in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_compose(x*y + 2*x + y, y)
    y**2 + 3*y

    i    i   (   R   R&   R   R   (   R4   R7   R=   R6   RU   R9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_compose»  s    c         C  s  t  |  ƒ d } t |  | ƒ } t |  ƒ }  i | j | 6} | | } xÇ t d | ƒ D]¶ } | j } x‚ t d | ƒ D]q }	 | |	 | |  k r” qt n  | |	 | k rª qt n  |  | |	 | | | |	 }
 } | | | |	 |
 | 7} qt W| j | | | | ƒ | | | <qU Wt | | ƒ S(   s+   Helper function for :func:`_dup_decompose`.i   i    (   RS   R   R'   Rp   R   R/   t   quoR(   (   R4   t   sR6   R:   R`   R7   t   rR8   RF   R;   Rk   Rl   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   _dup_right_decomposeØ  s     
	!&c         C  sy   i  d } } x\ |  rk t  |  | | ƒ \ } } t | ƒ d k rD d St | | ƒ | | <| | d }  } q Wt | | ƒ S(   s+   Helper function for :func:`_dup_decompose`.i    i   N(   R   R   t   NoneR   R(   (   R4   RU   R6   R7   R8   R|   Rƒ   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   _dup_left_decomposeô  s    	c         C  s   t  |  ƒ d } xv t d | ƒ D]e } | | d k r< q  n  t |  | | ƒ } | d k	 r  t |  | | ƒ } | d k	 r… | | f Sq  q  Wd S(   s*   Helper function for :func:`dup_decompose`.i   i   i    N(   RS   R   R„   R…   R†   (   R4   R6   t   dfR‚   RU   R7   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   _dup_decompose  s    c         C  sV   g  } xB t  rJ t |  | ƒ } | d k	 rF | \ }  } | g | } q	 Pq	 W|  g | S(   se  
    Computes functional decomposition of ``f`` in ``K[x]``.

    Given a univariate polynomial ``f`` with coefficients in a field of
    characteristic zero, returns list ``[f_1, f_2, ..., f_n]``, where::

              f = f_1 o f_2 o ... f_n = f_1(f_2(... f_n))

    and ``f_2, ..., f_n`` are monic and homogeneous polynomials of at
    least second degree.

    Unlike factorization, complete functional decompositions of
    polynomials are not unique, consider examples:

    1. ``f o g = f(x + b) o (g - b)``
    2. ``x**n o x**m = x**m o x**n``
    3. ``T_n o T_m = T_m o T_n``

    where ``T_n`` and ``T_m`` are Chebyshev polynomials.

    Examples
    ========

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

    >>> R.dup_decompose(x**4 - 2*x**3 + x**2)
    [x**2, x**2 - x]

    References
    ==========

    .. [1] [Kozen89]_

    N(   t   TrueRˆ   R…   (   R4   R6   t   FRM   RU   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_decompose  s    $	c         C  s"  | j  s t d ƒ ‚ n  t |  | ƒ g  g  } } } x3 | j ƒ  D]% \ } } | j sB | j | ƒ qB qB Wt d d g t | ƒ d t ƒ} xq | D]i }	 t	 | ƒ }
 x; t
 |	 | ƒ D]* \ } } | d k rµ |
 | |
 | <qµ qµ W| j t |
 | | ƒ ƒ q“ Wt t | | | ƒ | | | j ƒ S(   s^  
    Convert algebraic coefficients to integers in ``K[X]``.

    Examples
    ========

    >>> from sympy.polys import ring, QQ
    >>> from sympy import I

    >>> K = QQ.algebraic_field(I)
    >>> R, x = ring("x", K)

    >>> f = x**2 + K([QQ(1), QQ(0)])*x + K([QQ(2), QQ(0)])

    >>> R.dmp_lift(f)
    x**8 + 2*x**6 + 9*x**4 - 8*x**2 + 16

    s3   computation can be done only in an algebraic domainiÿÿÿÿi   t
   repetition(   t   is_AlgebraicR+   R   Rq   t	   is_groundRD   R,   RS   R‰   t   dictR{   R   R   R   t   dom(   R4   R=   R6   RŠ   t   monomst   polyst   monomRF   t   permst   permt   Gt   sign(    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_liftI  s    		!c         C  sT   | j  d } } x= |  D]5 } | j | | ƒ r= | d 7} n  | r | } q q W| S(   sâ   
    Compute the number of sign variations of ``f`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_sign_variations(x**4 - x**2 - x + 1)
    2

    i    i   (   R/   t   is_negative(   R4   R6   t   prevRG   RF   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_sign_variationst  s    c         C  s°   | d k r- | j r$ | j ƒ  } q- | } n  | j } x) |  D]! } | j | | j | ƒ ƒ } q= W| j | ƒ s† t |  | | ƒ }  n  | s– | |  f S| t |  | | ƒ f Sd S(   s@  
    Clear denominators, i.e. transform ``K_0`` to ``K_1``.

    Examples
    ========

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

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

    >>> R.dup_clear_denoms(f, convert=False)
    (6, 3*x + 2)
    >>> R.dup_clear_denoms(f, convert=True)
    (6, 3*x + 2)

    N(	   R…   t   has_assoc_Ringt   get_ringRp   t   lcmt   denomR_   R   R   (   R4   t   K0t   K1t   convertt   commonR9   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dup_clear_denomsŽ  s    			
c         C  s~   | j  } | s> xh |  D]! } | j | | j | ƒ ƒ } q Wn< | d } x/ |  D]' } | j | t | | | | ƒ ƒ } qO W| S(   s.   Recursive helper for :func:`dmp_clear_denoms`.i   (   Rp   Rž   RŸ   t   _rec_clear_denoms(   R7   R>   R    R¡   R£   R9   RA   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyR¥   ´  s    	"
%c         C  s²   | s t  |  | | d | ƒS| d k rI | j r@ | j ƒ  } qI | } n  t |  | | | ƒ } | j | ƒ s… t |  | | | ƒ }  n  | s• | |  f S| t |  | | | ƒ f Sd S(   sV  
    Clear denominators, i.e. transform ``K_0`` to ``K_1``.

    Examples
    ========

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

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

    >>> R.dmp_clear_denoms(f, convert=False)
    (6, 3*x + 2*y + 6)
    >>> R.dmp_clear_denoms(f, convert=True)
    (6, 3*x + 2*y + 6)

    R¢   N(   R¤   R…   Rœ   R   R¥   R_   R   R   (   R4   R=   R    R¡   R¢   R£   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   dmp_clear_denomsÄ  s    		
c   	      C  sÙ   | j  t |  | ƒ ƒ g } | j | j | j g } t t t | d ƒ ƒ ƒ } x„ t d | d ƒ D]o } t | | d ƒ | ƒ } t	 |  t
 | | ƒ | ƒ } t t | | | ƒ | | ƒ } t | t | ƒ | ƒ } qb W| S(   s÷  
    Compute ``f**(-1)`` mod ``x**n`` using Newton iteration.

    This function computes first ``2**n`` terms of a polynomial that
    is a result of inversion of a polynomial modulo ``x**n``. This is
    useful to efficiently compute series expansion of ``1/f``.

    Examples
    ========

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

    >>> f = -QQ(1,720)*x**6 + QQ(1,24)*x**4 - QQ(1,2)*x**2 + 1

    >>> R.dup_revert(f, 8)
    61/720*x**6 + 5/24*x**4 + 1/2*x**2 + 1

    i   i   (   t   revertR"   Rp   R/   t   intt   _ceilt   _logR   R   R
   R   R   R   R   R   (	   R4   R:   R6   R7   RU   t   NR8   RL   Rx   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt
   dup_revertê  s    c         C  s)   | s t  |  | | ƒ St |  | ƒ ‚ d S(   s©   
    Compute ``f**(-1)`` mod ``x**n`` using Newton iteration.

    Examples
    ========

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

    N(   R¬   R*   (   R4   R7   R=   R6   (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt
   dmp_revert  s    N(h   t   __doc__t
   __future__R    R   t   sympy.core.compatibilityR   t   sympy.polys.densearithR   R   R   R   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   R   R   t   sympy.polys.densebasicR   R   R   R   R   R   R   R   R   R    R!   R"   R#   R$   R%   R&   R'   R(   R)   t   sympy.polys.polyerrorsR*   R+   t   sympy.utilitiesR,   t   mathR-   R©   R.   Rª   R<   R?   R@   RC   RH   RI   RJ   RK   RN   RO   RP   RQ   RR   RW   RX   RY   R\   R]   R^   Ra   Rb   Rg   Rh   Ri   Rj   Rm   Rn   Ru   Rw   Ry   Rz   R~   R   R€   R„   R†   Rˆ   R‹   R˜   R›   R…   t   FalseR¤   R¥   R¦   R¬   R­   (    (    (    s5   lib/python2.7/site-packages/sympy/polys/densetools.pyt   <module>   sh   ‚|	 	#	
		+	/	
				
			 	
						$	*	-	!	$			/										2	+	&	&	"