B
    [
e                 @   sh  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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+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z4m5Z6 ddl7m8Z8 d	d
 Z9dd Z:dd Z;dd Z<dd Z=dd Z>dd Z?dd Z@dd ZAdd ZBdd ZCdd  ZDd!d" ZEd#d$ ZFd%d& ZGd'd( ZHd)d* ZId+d, ZJd-d. ZKd/d0 ZLd1d2 ZMd3d4 ZNd5d6 ZOd7d8 ZPd9d: ZQd;d< ZRd=d> ZSd?d@ ZTdAdB ZUdCdD ZVdEdF ZWdGdH ZXdIdJ ZYdKdL ZZdMdN Z[dOdP Z\dQdR Z]dSdT Z^dUdV Z_dWdX Z`ded[d\Zad]d^ Zbdfd_d`Zcdadb Zddcdd ZedYS )gzHAdvanced tools for dense recursive polynomials in ``K[x]`` or ``K[X]``.     )print_functiondivision)	dup_strip	dmp_stripdup_convertdmp_convert
dup_degree
dmp_degreedmp_to_dictdmp_from_dictdup_LCdmp_LCdmp_ground_LCdup_TCdmp_TCdmp_zero
dmp_ground
dmp_zero_pdup_to_raw_dictdup_from_raw_dict	dmp_zeros)dup_add_termdmp_add_term
dup_lshiftdup_adddmp_adddup_subdmp_subdup_muldmp_muldup_sqrdup_divdup_remdmp_rem
dmp_expanddup_mul_grounddmp_mul_grounddup_quo_grounddmp_quo_grounddup_exquo_grounddmp_exquo_ground)MultivariatePolynomialErrorDomainError)
variations)ceillog)rangec          	   C   s~   |dks| s| S |j g| }x\tt| D ]L\}}|d }x"td|D ]}||| d 9 }qFW |d|||| q*W |S )a  
    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

    r      )zero	enumeratereversedr0   insertZexquo)fmKgicnj r>   5lib/python3.7/site-packages/sympy/polys/densetools.pydup_integrate,   s    r@   c       
   	   C   s   |st | ||S |dks"t| |r&| S t||d ||d  }}x^tt| D ]N\}}|d }x"td|D ]}	|||	 d 9 }qjW |dt||||| qNW |S )a&  
    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

    r   r1   )r@   r   r   r3   r4   r0   r5   r(   )
r6   r7   ur8   r9   vr:   r;   r<   r=   r>   r>   r?   dmp_integrateL   s    rC   c                sH   krt | | S |d d  t fdd| D |S )z.Recursive helper for :func:`dmp_integrate_in`.r1   c          	      s   g | ]}t | qS r>   )_rec_integrate_in).0r;   )r8   r:   r=   r7   wr>   r?   
<listcomp>v   s    z%_rec_integrate_in.<locals>.<listcomp>)rC   r   )r9   r7   rB   r:   r=   r8   r>   )r8   r:   r=   r7   rF   r?   rD   o   s    rD   c             C   s2   |dk s||kr t d||f t| ||d||S )a+  
    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

    r   z(0 <= j <= u expected, got u = %d, j = %d)
IndexErrorrD   )r6   r7   r=   rA   r8   r>   r>   r?   dmp_integrate_iny   s    rI   c             C   s   |dkr| S t | }||k r g S g }|dkr`x| d|  D ]}||||  |d8 }q<W n\xZ| d|  D ]H}|}x$t|d || dD ]}||9 }qW ||||  |d8 }qpW t|S )a#  
    ``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

    r   r1   N)r   appendr0   r   )r6   r7   r8   r<   derivcoeffkr:   r>   r>   r?   dup_diff   s"    rO   c       
      C   s   |st | ||S |dkr| S t| |}||k r6t|S g |d  }}|dkrx| d|  D ]$}|t||||| |d8 }q\W nbx`| d|  D ]N}|}x$t|d || dD ]}	||	9 }qW |t||||| |d8 }qW t||S )a3  
    ``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

    r   r1   NrJ   )rO   r	   r   rK   r&   r0   r   )
r6   r7   rA   r8   r<   rL   rB   rM   rN   r:   r>   r>   r?   dmp_diff   s&    
rP   c                sH   krt | | S |d d  t fdd| D |S )z)Recursive helper for :func:`dmp_diff_in`.r1   c          	      s   g | ]}t | qS r>   )_rec_diff_in)rE   r;   )r8   r:   r=   r7   rF   r>   r?   rG      s    z _rec_diff_in.<locals>.<listcomp>)rP   r   )r9   r7   rB   r:   r=   r8   r>   )r8   r:   r=   r7   rF   r?   rQ      s    rQ   c             C   s2   |dk s||kr t d||f t| ||d||S )aS  
    ``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

    r   z0 <= j <= %s expected, got %s)rH   rQ   )r6   r7   r=   rA   r8   r>   r>   r?   dmp_diff_in   s    rR   c             C   s6   |st | |S |j}x| D ]}||9 }||7 }qW |S )z
    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   r2   )r6   ar8   resultr;   r>   r>   r?   dup_eval  s    

rU   c             C   sh   |st | ||S |st| |S t| ||d  }}x0| dd D ] }t||||}t||||}q@W |S )z
    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

    r1   N)rU   r   r   r&   r   )r6   rS   rA   r8   rT   rB   rM   r>   r>   r?   dmp_eval%  s    
rV   c                sH   krt |  S d d  t fdd| D S )z)Recursive helper for :func:`dmp_eval_in`.r1   c          	      s   g | ]}t | qS r>   )_rec_eval_in)rE   r;   )r8   rS   r:   r=   rB   r>   r?   rG   I  s    z _rec_eval_in.<locals>.<listcomp>)rV   r   )r9   rS   rB   r:   r=   r8   r>   )r8   rS   r:   r=   rB   r?   rW   B  s    rW   c             C   s2   |dk s||kr t d||f t| ||d||S )a2  
    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

    r   z0 <= j <= %s expected, got %s)rH   rW   )r6   rS   r=   rA   r8   r>   r>   r?   dmp_eval_inL  s    rX   c                sf   krt |  d S  fdd| D }t  d k rH|S t |   d  S dS )z+Recursive helper for :func:`dmp_eval_tail`.rJ   c                s    g | ]}t |d   qS )r1   )_rec_eval_tail)rE   r;   )Ar8   r:   rA   r>   r?   rG   i  s    z"_rec_eval_tail.<locals>.<listcomp>r1   N)rU   len)r9   r:   rZ   rA   r8   hr>   )rZ   r8   r:   rA   r?   rY   d  s    rY   c             C   s\   |s| S t | |r"t|t| S t| d|||}|t|d krF|S t||t| S dS )a!  
    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

    r   r1   N)r   r   r[   rY   r   )r6   rZ   rA   r8   er>   r>   r?   dmp_eval_tailq  s    
r^   c                sT   kr t t|   S d d  t fdd| D S )z+Recursive helper for :func:`dmp_diff_eval`.r1   c          
      s    g | ]}t | qS r>   )_rec_diff_eval)rE   r;   )r8   rS   r:   r=   r7   rB   r>   r?   rG     s    z"_rec_diff_eval.<locals>.<listcomp>)rV   rP   r   )r9   r7   rS   rB   r:   r=   r8   r>   )r8   rS   r:   r=   r7   rB   r?   r_     s    r_   c             C   sJ   ||krt d|||f |s6tt| ||||||S t| |||d||S )a]  
    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

    z-%s <= j < %s expected, got %sr   )rH   rV   rP   r_   )r6   r7   rS   r=   rA   r8   r>   r>   r?   dmp_diff_eval_in  s
    r`   c                sb   |j rHg }xN| D ]2}|  }| d kr8||   q|| qW n fdd| D }t|S )z
    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

       c                s   g | ]}|  qS r>   r>   )rE   r;   )pr>   r?   rG     s    zdup_trunc.<locals>.<listcomp>)is_ZZrK   r   )r6   rb   r8   r9   r;   r>   )rb   r?   	dup_trunc  s    
rd   c                s   t  fdd| D S )a9  
    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

    c                s   g | ]}t |d   qS )r1   )r#   )rE   r;   )r8   rb   rA   r>   r?   rG     s    zdmp_trunc.<locals>.<listcomp>)r   )r6   rb   rA   r8   r>   )r8   rb   rA   r?   	dmp_trunc  s    re   c                s4   |st |  S |d t fdd| D |S )a   
    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

    r1   c                s   g | ]}t | qS r>   )dmp_ground_trunc)rE   r;   )r8   rb   rB   r>   r?   rG     s    z$dmp_ground_trunc.<locals>.<listcomp>)rd   r   )r6   rb   rA   r8   r>   )r8   rb   rB   r?   rf     s    rf   c             C   s0   | s| S t | |}||r | S t| ||S dS )a7  
    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   is_oner)   )r6   r8   lcr>   r>   r?   	dup_monic  s    

ri   c             C   sH   |st | |S t| |r| S t| ||}||r6| S t| |||S dS )a  
    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)ri   r   r   rg   r*   )r6   rA   r8   rh   r>   r>   r?   dmp_ground_monic  s    


rj   c             C   sj   ddl m} | s|jS |j}||kr@x@| D ]}|||}q*W n&x$| D ]}|||}||rFP qFW |S )aA  
    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

    r   )QQ)sympy.polys.domainsrk   r2   gcdrg   )r6   r8   rk   contr;   r>   r>   r?   dup_content@  s    


ro   c             C   s   ddl m} |st| |S t| |r*|jS |j|d  }}||krfxP| D ]}||t|||}qHW n.x,| D ]$}||t|||}||rlP qlW |S )aa  
    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

    r   )rk   r1   )rl   rk   ro   r   r2   rm   dmp_ground_contentrg   )r6   rA   r8   rk   rn   rB   r;   r>   r>   r?   rp   j  s    




rp   c             C   s>   | s|j | fS t| |}||r*|| fS |t| ||fS dS )at  
    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)r2   ro   rg   r'   )r6   r8   rn   r>   r>   r?   dup_primitive  s    


rq   c             C   sV   |st | |S t| |r"|j| fS t| ||}||r@|| fS |t| |||fS dS )a  
    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)rq   r   r2   rp   rg   r(   )r6   rA   r8   rn   r>   r>   r?   dmp_ground_primitive  s    



rr   c             C   sL   t | |}t ||}|||}||sBt| ||} t|||}|| |fS )a  
    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)

    )ro   rm   rg   r'   )r6   r9   r8   fcgcrm   r>   r>   r?   dup_extract  s    


ru   c             C   sT   t | ||}t |||}|||}||sJt| |||} t||||}|| |fS )a  
    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)

    )rp   rm   rg   r(   )r6   r9   rA   r8   rs   rt   rm   r>   r>   r?   dmp_ground_extract  s    
rv   c       
      C   s  |j s|jstd| td}td}| s4||fS |j|jgg|jgg gg}t| d d}x8| dd D ](}t||d|}t|t|ddd|}qjW t	|}xn|
 D ]b\}}|d }	|	st||d|}q|	dkrt||d|}q|	dkrt||d|}qt||d|}qW ||fS )a4  
    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)

    z;computing real and imaginary parts is not supported over %sr1   r   ra   N   )rc   Zis_QQr,   r   oner2   r   r   r   r   itemsr   r   )
r6   r8   f1f2r9   r\   r;   HrN   r7   r>   r>   r?   dup_real_imag  s,    r}   c             C   s8   t | } x*tt| d ddD ]}| |  | |< qW | S )z
    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

    ra   rJ   )listr0   r[   )r6   r8   r:   r>   r>   r?   
dup_mirror?  s    r   c             C   sT   t | t| d |  } }}x2t|d ddD ]}|| |  ||  | |< }q.W | S )z
    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

    r1   rJ   )r   r[   r0   )r6   rS   r8   r<   br:   r>   r>   r?   	dup_scaleU  s    r   c             C   s`   t | t| d  } }xDt|ddD ]4}x.td|D ] }| |d   || |  7  < q4W q$W | S )z
    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

    r1   r   rJ   )r   r[   r0   )r6   rS   r8   r<   r:   r=   r>   r>   r?   	dup_shiftk  s
    $r   c       	      C   s   | sg S t | d }| d g|jgg }}x(td|D ]}|t|d || q6W xJt| dd |dd D ],\}}t|||}t|||}t|||}qpW |S )a  
    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

    r1   r   rJ   N)r[   rx   r0   rK   r   zipr%   r   )	r6   rb   qr8   r<   r\   Qr:   r;   r>   r>   r?   dup_transform  s    $r   c             C   sj   t |dkr$tt| t|||gS | s,g S | d g}x.| dd D ]}t|||}t||d|}qDW |S )z
    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

    r1   r   N)r[   r   rU   r   r   r   )r6   r9   r8   r\   r;   r>   r>   r?   dup_compose  s    
r   c             C   s`   |st | ||S t| |r| S | d g}x2| dd D ]"}t||||}t||d||}q6W |S )z
    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

    r   r1   N)r   r   r   r   )r6   r9   rA   r8   r\   r;   r>   r>   r?   dmp_compose  s    

r   c             C   s   t | d }t| |}t| } ||ji}|| }xtd|D ]}|j}xhtd|D ]Z}	||	 | | krhqR||	 |krvqR| ||	 |  |||	   }
}||||	  |
 | 7 }qRW |||| | ||| < q<W t||S )z+Helper function for :func:`_dup_decompose`.r1   r   )r[   r   r   rx   r0   r2   Zquor   )r6   sr8   r<   rh   r9   rr:   rM   r=   rs   rt   r>   r>   r?   _dup_right_decompose  s     

 r   c             C   sZ   i d }}xD| rNt | ||\}}t|dkr0dS t||||< ||d  } }qW t||S )z+Helper function for :func:`_dup_decompose`.r   Nr1   )r!   r   r   r   )r6   r\   r8   r9   r:   r   r   r>   r>   r?   _dup_left_decompose  s    
r   c             C   sb   t | d }xPtd|D ]B}|| dkr*qt| ||}|dk	rt| ||}|dk	r||fS qW dS )z*Helper function for :func:`dup_decompose`.r1   ra   r   N)r[   r0   r   r   )r6   r8   Zdfr   r\   r9   r>   r>   r?   _dup_decompose  s    r   c             C   s<   g }x,t | |}|dk	r,|\} }|g| }qP qW | g| S )aa  
    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)r   )r6   r8   FrT   r\   r>   r>   r?   dup_decompose  s    $
r   c             C   s   |j stdt| |g g   }}}x$| D ]\}}|js.|| q.W tddgt|dd}xT|D ]L}	t|}
x,t	|	|D ]\}}|dkr~|
|  |
|< q~W |t
|
|| qfW tt||||||jS )a^  
    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

    z3computation can be done only in an algebraic domainrJ   r1   T)Z
repetition)Zis_Algebraicr,   r
   ry   Z	is_groundrK   r-   r[   dictr   r   r   r$   Zdom)r6   rA   r8   r   ZmonomsZpolysZmonomrM   ZpermsZpermGZsignr>   r>   r?   dmp_liftL  s    
r   c             C   s<   |j d }}x*| D ]"}||| r,|d7 }|r|}qW |S )z
    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

    r   r1   )r2   Zis_negative)r6   r8   prevrN   rM   r>   r>   r?   dup_sign_variationsw  s    
r   NFc             C   sx   |dkr|j r| }n|}|j}x| D ]}||||}q(W ||sXt| ||} |sd|| fS |t| ||fS dS )a@  
    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)has_assoc_Ringget_ringrx   lcmdenomrg   r%   r   )r6   K0K1convertcommonr;   r>   r>   r?   dup_clear_denoms  s    


r   c          	   C   s\   |j }|s,xL| D ]}||||}qW n,|d }x"| D ]}||t||||}q:W |S )z.Recursive helper for :func:`dmp_clear_denoms`.r1   )rx   r   r   _rec_clear_denoms)r9   rB   r   r   r   r;   rF   r>   r>   r?   r     s    

r   c             C   sx   |st | |||dS |dkr0|jr,| }n|}t| |||}||sVt| |||} |sb|| fS |t| |||fS dS )aV  
    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   rg   r&   r   )r6   rA   r   r   r   r   r>   r>   r?   dmp_clear_denoms  s    

r   c       	      C   s   | t| |g}|j|j|jg}ttt|d}x\td|d D ]J}t||d|}t	| t
|||}tt|||||}t|t||}qDW |S )a  
    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

    ra   r1   )Zrevertr   rx   r2   int_ceil_logr0   r%   r   r    r"   r   r   r   )	r6   r<   r8   r9   r\   Nr:   rS   r   r>   r>   r?   
dup_revert  s    r   c             C   s   |st | ||S t| |dS )z
    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+   )r6   r9   rA   r8   r>   r>   r?   
dmp_revert  s    r   )NF)NF)f__doc__Z
__future__r   r   Zsympy.polys.densebasicr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Zsympy.polys.densearithr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   Zsympy.polys.polyerrorsr+   r,   Zsympy.utilitiesr-   Zmathr.   r   r/   r   Zsympy.core.compatibilityr0   r@   rC   rD   rI   rO   rP   rQ   rR   rU   rV   rW   rX   rY   r^   r_   r`   rd   re   rf   ri   rj   ro   rp   rq   rr   ru   rv   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   r>   r>   r?   <module>   sh   TX #
+/

 
$*-!$/2+
&
&"