B
    [                 @   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mZmZmZmZ ddlmZmZ ddlmZ dd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z dd Z!dd Z"dd Z#d d! Z$d"d# Z%d$d% Z&d&d' Z'd(d) Z(d*d+ Z)d,d- Z*d.d/ Z+d0d1 Z,d2d3 Z-d4d5 Z.d6d7 Z/d8d9 Z0d:d; Z1d<d= Z2d>d? Z3d@dA Z4dBdC Z5dDdE Z6dFdG Z7dHdI Z8dJdK Z9dLdM Z:dNdO Z;dPdQ Z<dRdS Z=dTdU Z>dVdW Z?dXdY Z@dZd[ ZAd\d] ZBd^d_ ZCd`da ZDdbdc ZEddde ZFdfdg ZGdhdi ZHdjdk ZIdldm ZJdndo ZKdpdq ZLdrds ZMdtdu ZNdvdw ZOdxdy ZPdzd{ ZQd|d} ZRd~d ZSdd ZTdS )zEArithmetics for dense recursive polynomials in ``K[x]`` or ``K[X]``.     )print_functiondivision)	dup_slicedup_LCdmp_LC
dup_degree
dmp_degree	dup_strip	dmp_strip
dmp_zero_pdmp_zero	dmp_one_pdmp_one
dmp_ground	dmp_zeros)ExactQuotientFailedPolynomialDivisionFailed)rangec             C   s   |s| S t | }|| d }||d krFt| d | g| dd  S ||krh|g|jg||   |  S | d| | | | g | |d d  S dS )z
    Add ``c*x**i`` to ``f`` in ``K[x]``.

    Examples
    ========

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

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

       r   N)lenr	   zero)fciKnm r   5lib/python3.7/site-packages/sympy/polys/densearith.pydup_add_term   s    r   c             C   s   |st | |||S |d }t||r(| S t| }|| d }||d krntt| d |||g| dd  |S ||kr|gt|| || |  S | d| t| | |||g | |d d  S dS )z
    Add ``c(x_2..x_u)*x_0**i`` to ``f`` in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_add_term(x*y + 1, 2, 2)
    2*x**2 + x*y + 1

    r   r   N)r   r   r   r
   dmp_addr   )r   r   r   ur   vr   r   r   r   r   dmp_add_term.   s    
&r#   c             C   s   |s| S t | }|| d }||d krFt| d | g| dd  S ||krj| g|jg||   |  S | d| | | | g | |d d  S dS )z
    Subtract ``c*x**i`` from ``f`` in ``K[x]``.

    Examples
    ========

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

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

    r   r   N)r   r	   r   )r   r   r   r   r   r   r   r   r   dup_sub_termP   s    r$   c             C   s   |st | | ||S |d }t||r*| S t| }|| d }||d krptt| d |||g| dd  |S ||krt|||gt|| || |  S | d| t| | |||g | |d d  S dS )z
    Subtract ``c(x_2..x_u)*x_0**i`` from ``f`` in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_sub_term(2*x**2 + x*y + 1, 2, 2)
    x*y + 1

    r   r   N)r   r   r   r
   dmp_subdmp_negr   )r   r   r   r!   r   r"   r   r   r   r   r   dmp_sub_termm   s    
&"r'   c                s.    r| sg S  fdd| D |j g|  S dS )z
    Multiply ``f`` by ``c*x**i`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_mul_term(x**2 - 1, ZZ(3), 2)
    3*x**4 - 3*x**2

    c                s   g | ]}|  qS r   r   ).0cf)r   r   r   
<listcomp>   s    z dup_mul_term.<locals>.<listcomp>N)r   )r   r   r   r   r   )r   r   dup_mul_term   s    r+   c                s`   |st | | S |d t| |r(| S tr:t|S  fdd| D t|  S dS )z
    Multiply ``f`` by ``c(x_2..x_u)*x_0**i`` in ``K[X]``.

    Examples
    ========

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

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

    r   c                s   g | ]}t | qS r   )dmp_mul)r(   r)   )r   r   r"   r   r   r*      s    z dmp_mul_term.<locals>.<listcomp>N)r+   r   r   r   )r   r   r   r!   r   r   )r   r   r"   r   dmp_mul_term   s    

r-   c             C   s   t | |d|S )z
    Add an element of the ground domain to ``f``.

    Examples
    ========

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

    >>> R.dup_add_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
    x**3 + 2*x**2 + 3*x + 8

    r   )r   )r   r   r   r   r   r   dup_add_ground   s    r.   c             C   s   t | t||d d||S )z
    Add an element of the ground domain to ``f``.

    Examples
    ========

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

    >>> R.dmp_add_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
    x**3 + 2*x**2 + 3*x + 8

    r   r   )r#   r   )r   r   r!   r   r   r   r   dmp_add_ground   s    r/   c             C   s   t | |d|S )z
    Subtract an element of the ground domain from ``f``.

    Examples
    ========

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

    >>> R.dup_sub_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
    x**3 + 2*x**2 + 3*x

    r   )r$   )r   r   r   r   r   r   dup_sub_ground   s    r0   c             C   s   t | t||d d||S )z
    Subtract an element of the ground domain from ``f``.

    Examples
    ========

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

    >>> R.dmp_sub_ground(x**3 + 2*x**2 + 3*x + 4, ZZ(4))
    x**3 + 2*x**2 + 3*x

    r   r   )r'   r   )r   r   r!   r   r   r   r   dmp_sub_ground   s    r1   c                s"    r| sg S  fdd| D S dS )z
    Multiply ``f`` by a constant value in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_mul_ground(x**2 + 2*x - 1, ZZ(3))
    3*x**2 + 6*x - 3

    c                s   g | ]}|  qS r   r   )r(   r)   )r   r   r   r*     s    z"dup_mul_ground.<locals>.<listcomp>Nr   )r   r   r   r   )r   r   dup_mul_ground  s    r2   c                s.   |st |  S |d  fdd| D S )z
    Multiply ``f`` by a constant value in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_mul_ground(2*x + 2*y, ZZ(3))
    6*x + 6*y

    r   c                s   g | ]}t | qS r   )dmp_mul_ground)r(   r)   )r   r   r"   r   r   r*   )  s    z"dmp_mul_ground.<locals>.<listcomp>)r2   )r   r   r!   r   r   )r   r   r"   r   r3     s    r3   c                sD   st d| s| S  jr. fdd| D S fdd| D S dS )a)  
    Quotient by a constant in ``K[x]``.

    Examples
    ========

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

    >>> R, x = ring("x", ZZ)
    >>> R.dup_quo_ground(3*x**2 + 2, ZZ(2))
    x**2 + 1

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

    zpolynomial divisionc                s   g | ]}  |qS r   )Zquo)r(   r)   )r   r   r   r   r*   D  s    z"dup_quo_ground.<locals>.<listcomp>c                s   g | ]}|  qS r   r   )r(   r)   )r   r   r   r*   F  s    N)ZeroDivisionErroris_Field)r   r   r   r   )r   r   r   dup_quo_ground,  s    r6   c                s.   |st |  S |d  fdd| D S )a=  
    Quotient by a constant in ``K[X]``.

    Examples
    ========

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

    >>> R, x,y = ring("x,y", ZZ)
    >>> R.dmp_quo_ground(2*x**2*y + 3*x, ZZ(2))
    x**2*y + x

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

    r   c                s   g | ]}t | qS r   )dmp_quo_ground)r(   r)   )r   r   r"   r   r   r*   `  s    z"dmp_quo_ground.<locals>.<listcomp>)r6   )r   r   r!   r   r   )r   r   r"   r   r7   I  s    r7   c                s(   st d| s| S  fdd| D S )z
    Exact quotient by a constant in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_exquo_ground(x**2 + 2, QQ(2))
    1/2*x**2 + 1

    zpolynomial divisionc                s   g | ]}  |qS r   )exquo)r(   r)   )r   r   r   r   r*   v  s    z$dup_exquo_ground.<locals>.<listcomp>)r4   )r   r   r   r   )r   r   r   dup_exquo_groundc  s
    r9   c                s.   |st |  S |d  fdd| D S )z
    Exact quotient by a constant in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_exquo_ground(x**2*y + 2*x, QQ(2))
    1/2*x**2*y + x

    r   c                s   g | ]}t | qS r   )dmp_exquo_ground)r(   r)   )r   r   r"   r   r   r*     s    z$dmp_exquo_ground.<locals>.<listcomp>)r9   )r   r   r!   r   r   )r   r   r"   r   r:   y  s    r:   c             C   s   | s| S | |j g|  S dS )z
    Efficiently multiply ``f`` by ``x**n`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_lshift(x**2 + 1, 2)
    x**4 + x**2

    N)r   )r   r   r   r   r   r   
dup_lshift  s    r;   c             C   s   | d|  S )a  
    Efficiently divide ``f`` by ``x**n`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_rshift(x**4 + x**2, 2)
    x**2 + 1
    >>> R.dup_rshift(x**4 + x**2 + 2, 2)
    x**2 + 1

    Nr   )r   r   r   r   r   r   
dup_rshift  s    r<   c                s    fdd| D S )z
    Make all coefficients positive in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_abs(x**2 - 1)
    x**2 + 1

    c                s   g | ]}  |qS r   )abs)r(   coeff)r   r   r   r*     s    zdup_abs.<locals>.<listcomp>r   )r   r   r   )r   r   dup_abs  s    r?   c                s*   |st |  S |d  fdd| D S )z
    Make all coefficients positive in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_abs(x**2*y - x)
    x**2*y + x

    r   c                s   g | ]}t | qS r   )dmp_abs)r(   r)   )r   r"   r   r   r*     s    zdmp_abs.<locals>.<listcomp>)r?   )r   r!   r   r   )r   r"   r   r@     s    
r@   c             C   s   dd | D S )z
    Negate a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_neg(x**2 - 1)
    -x**2 + 1

    c             S   s   g | ]
}| qS r   r   )r(   r>   r   r   r   r*     s    zdup_neg.<locals>.<listcomp>r   )r   r   r   r   r   dup_neg  s    rA   c                s*   |st |  S |d  fdd| D S )z
    Negate a polynomial in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_neg(x**2*y - x)
    -x**2*y + x

    r   c                s   g | ]}t | qS r   )r&   )r(   r)   )r   r"   r   r   r*     s    zdmp_neg.<locals>.<listcomp>)rA   )r   r!   r   r   )r   r"   r   r&     s    
r&   c             C   s   | s|S |s| S t | }t |}||kr@tdd t| |D S t|| }||krp| d| | |d  }} n|d| ||d  }}|dd t| |D  S dS )z
    Add dense polynomials in ``K[x]``.

    Examples
    ========

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

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

    c             S   s   g | ]\}}|| qS r   r   )r(   abr   r   r   r*     s    zdup_add.<locals>.<listcomp>Nc             S   s   g | ]\}}|| qS r   r   )r(   rB   rC   r   r   r   r*   $  s    )r   r	   zipr=   )r   gr   dfdgkhr   r   r   dup_add  s    rJ   c                s   |st | | S t| |}|dk r&|S t||}|dk r<| S |d ||krlt fddt| |D |S t|| }||kr| d| | |d  }} n|d| ||d  }}| fddt| |D  S dS )z
    Add dense polynomials in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_add(x**2 + y, x**2*y + x)
    x**2*y + x**2 + x + y

    r   r   c                s   g | ]\}}t || qS r   )r    )r(   rB   rC   )r   r"   r   r   r*   E  s    zdmp_add.<locals>.<listcomp>Nc                s   g | ]\}}t || qS r   )r    )r(   rB   rC   )r   r"   r   r   r*   N  s    )rJ   r   r
   rD   r=   )r   rE   r!   r   rF   rG   rH   rI   r   )r   r"   r   r    '  s     

 r    c             C   s   | st ||S |s| S t| }t|}||krFtdd t| |D S t|| }||krv| d| | |d  }} n t |d| |||d  }}|dd t| |D  S dS )z
    Subtract dense polynomials in ``K[x]``.

    Examples
    ========

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

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

    c             S   s   g | ]\}}|| qS r   r   )r(   rB   rC   r   r   r   r*   h  s    zdup_sub.<locals>.<listcomp>Nc             S   s   g | ]\}}|| qS r   r   )r(   rB   rC   r   r   r   r*   q  s    )rA   r   r	   rD   r=   )r   rE   r   rF   rG   rH   rI   r   r   r   dup_subQ  s    
 rK   c                s   |st | | S t| |}|dk r.t|| S t||}|dk rD| S |d ||krtt fddt| |D |S t|| }||kr| d| | |d  }} n"t|d| | ||d  }}| fddt| |D  S dS )z
    Subtract dense polynomials in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_sub(x**2 + y, x**2*y + x)
    -x**2*y + x**2 - x + y

    r   r   c                s   g | ]\}}t || qS r   )r%   )r(   rB   rC   )r   r"   r   r   r*     s    zdmp_sub.<locals>.<listcomp>Nc                s   g | ]\}}t || qS r   )r%   )r(   rB   rC   )r   r"   r   r   r*     s    )rK   r   r&   r
   rD   r=   )r   rE   r!   r   rF   rG   rH   rI   r   )r   r"   r   r%   t  s     

 "r%   c             C   s   t | t||||S )z
    Returns ``f + g*h`` where ``f, g, h`` are in ``K[x]``.

    Examples
    ========

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

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

    )rJ   dup_mul)r   rE   rI   r   r   r   r   dup_add_mul  s    rM   c             C   s   t | t||||||S )z
    Returns ``f + g*h`` where ``f, g, h`` are in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_add_mul(x**2 + y, x, x + 2)
    2*x**2 + 2*x + y

    )r    r,   )r   rE   rI   r!   r   r   r   r   dmp_add_mul  s    rN   c             C   s   t | t||||S )z
    Returns ``f - g*h`` where ``f, g, h`` are in ``K[x]``.

    Examples
    ========

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

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

    )rK   rL   )r   rE   rI   r   r   r   r   dup_sub_mul  s    rO   c             C   s   t | t||||||S )z
    Returns ``f - g*h`` where ``f, g, h`` are in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_sub_mul(x**2 + y, x, x + 2)
    -2*x + y

    )r%   r,   )r   rE   rI   r!   r   r   r   r   dmp_sub_mul  s    rP   c             C   s  | |krt | |S | r|sg S t| }t|}t||d }|dk rg }xjtd|| d D ]T}|j}x>ttd|| t||d D ]}	|| |	 |||	   7 }qW || q\W t|S |d }
t| d|
|t|d|
| }}t	t| |
|||
|}t	t||
|||
|}t
|||t
||| }}t
t|||t||||}t|t||||}tt|t||
||t|d|
 ||S dS )z
    Multiply dense polynomials in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_mul(x - 2, x + 2)
    x**2 - 4

    r   d   r      N)dup_sqrr   maxr   r   minappendr	   r   r<   rL   rJ   rK   r;   )r   rE   r   rF   rG   r   rI   r   r>   jZn2ZflZglZfhZghlohiZmidr   r   r   rL     s0    
$rL   c          	   C   s   |st | ||S | |kr$t| ||S t| |}|dk r:| S t||}|dk rP|S g |d  }}xxtd|| d D ]b}t|}	xJttd|| t||d D ](}
t|	t| |
 |||
  ||||}	qW |	|	 qrW t
||S )z
    Multiply dense polynomials in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_mul(x*y + 1, x)
    x**2*y + x

    r   r   )rL   dmp_sqrr   r   r   rT   rU   r    r,   rV   r
   )r   rE   r!   r   rF   rG   rI   r"   r   r>   rW   r   r   r   r,     s"    

$(r,   c             C   s   t | d g  }}xtdd| d D ]}|j}td|| }t||}|| d }||d  d }x.t||d D ]}	|| |	 | ||	   7 }qtW ||7 }|d@ r| |d  }
||
d 7 }|| q&W t|S )z
    Square dense polynomials in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_sqr(x**2 + 1)
    x**4 + 2*x**2 + 1

    r   r   rR   )r   r   r   rT   rU   rV   r	   )r   r   rF   rI   r   r   jminjmaxr   rW   elemr   r   r   rS   F  s    
rS   c          	   C   s  |st | |S t| |}|dk r$| S g |d  }}xtdd| d D ]}t|}td|| }t||}	|	| d }
||
d  d }	x:t||	d D ](}t|t| | | ||  ||||}qW t||d||}|
d@ r t	| |	d  ||}t||||}|
| qFW t||S )z
    Square dense polynomials in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_sqr(x**2 + x*y + y**2)
    x**4 + 2*x**3*y + 3*x**2*y**2 + 2*x*y**3 + y**4

    r   r   rR   )rS   r   r   r   rT   rU   r    r,   r3   rZ   rV   r
   )r   r!   r   rF   rI   r"   r   r   r[   r\   r   rW   r]   r   r   r   rZ   n  s(    


(
rZ   c             C   s|   |s|j gS |dk rtd|dks4| r4| |j gkr8| S |j g}x6|d | }}|d rjt|| |}|sjP t| |} qBW |S )z
    Raise ``f`` to the ``n``-th power in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_pow(x - 2, 3)
    x**3 - 6*x**2 + 12*x - 8

    r   z*can't raise polynomial to a negative powerr   rR   )one
ValueErrorrL   rS   )r   r   r   rE   r   r   r   r   dup_pow  s    r`   c             C   s   |st | ||S |st||S |dk r.td|dksLt| |sLt| ||rP| S t||}x:|d | }}|d@ rt|| ||}|sP t| ||} q\W |S )z
    Raise ``f`` to the ``n``-th power in ``K[X]``.

    Examples
    ========

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

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

    r   z*can't raise polynomial to a negative powerr   rR   )r`   r   r_   r   r   r,   rZ   )r   r   r!   r   rE   r   r   r   r   dmp_pow  s"    

ra   c             C   s  t | }t |}g | |  }}}|s.tdn||k r>||fS || d }t||}	xt||}
|| |d  }}t||	|}t||
||}t||	|}t||
||}t|||}|t | }}||k rP qV||k sVt| ||qVW |	| }t|||}t|||}||fS )z
    Polynomial pseudo-division in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_pdiv(x**2 + 1, 2*x - 4)
    (2*x + 4, 20)

    zpolynomial divisionr   )r   r4   r   r2   r   r+   rK   r   )r   rE   r   rF   rG   qrdrNlc_glc_rrW   QRG_drr   r   r   r   dup_pdiv  s4    


rl   c             C   s   t | }t |}| | }}|s(tdn||k r4|S || d }t||}xtt||}	|| |d  }
}t|||}t||	|
|}t|||}|t | }}||k rP qL||k sLt| ||qLW t||| |S )z
    Polynomial pseudo-remainder in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_prem(x**2 + 1, 2*x - 4)
    20

    zpolynomial divisionr   )r   r4   r   r2   r+   rK   r   )r   rE   r   rF   rG   rc   rd   re   rf   rg   rW   ri   rj   rk   r   r   r   dup_prem   s*    



rm   c             C   s   t | ||d S )a   
    Polynomial exact pseudo-quotient in ``K[X]``.

    Examples
    ========

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

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

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

    r   )rl   )r   rE   r   r   r   r   dup_pquoM  s    rn   c             C   s&   t | ||\}}|s|S t| |dS )a\  
    Polynomial pseudo-quotient in ``K[x]``.

    Examples
    ========

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

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

    >>> R.dup_pexquo(x**2 + 1, 2*x - 4)
    Traceback (most recent call last):
    ...
    ExactQuotientFailed: [2, -4] does not divide [1, 0, 1]

    N)rl   r   )r   rE   r   rb   rc   r   r   r   
dup_pexquoa  s    ro   c             C   sH  |st | ||S t| |}t||}|dk r4tdt|| |  }}}||k rX||fS || d }	t||}
xt||}|| |	d  }}	t||
d||}t|||||}t||
d||}t|||||}t||||}|t|| }}||k rP qp||k spt| ||qpW t	|
|	|d |}t||d||}t||d||}||fS )z
    Polynomial pseudo-division in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_pdiv(x**2 + x*y, 2*x + 2)
    (2*x + 2*y - 2, -4*y + 4)

    r   zpolynomial divisionr   )
rl   r   r4   r   r   r-   r#   r%   r   ra   )r   rE   r!   r   rF   rG   rb   rc   rd   re   rf   rg   rW   rh   ri   rj   rk   r   r   r   r   dmp_pdiv|  s8    



rp   c             C   s  |st | ||S t| |}t||}|dk r4td| | }}||k rJ|S || d }t||}	x~t||}
|| |d  }}t||	d||}t||
|||}t||||}|t|| }}||k rP qb||k sbt| ||qbW t|	||d |}t||d||S )z
    Polynomial pseudo-remainder in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_prem(x**2 + x*y, 2*x + 2)
    -4*y + 4

    r   zpolynomial divisionr   )rm   r   r4   r   r-   r%   r   ra   )r   rE   r!   r   rF   rG   rc   rd   re   rf   rg   rW   ri   rj   rk   r   r   r   r   dmp_prem  s0    




rq   c             C   s   t | |||d S )a.  
    Polynomial exact pseudo-quotient in ``K[X]``.

    Examples
    ========

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

    >>> f = x**2 + x*y
    >>> g = 2*x + 2*y
    >>> h = 2*x + 2

    >>> R.dmp_pquo(f, g)
    2*x

    >>> R.dmp_pquo(f, h)
    2*x + 2*y - 2

    r   )rp   )r   rE   r!   r   r   r   r   dmp_pquo  s    rr   c             C   s.   t | |||\}}t||r |S t| |dS )a  
    Polynomial pseudo-quotient in ``K[X]``.

    Examples
    ========

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

    >>> f = x**2 + x*y
    >>> g = 2*x + 2*y
    >>> h = 2*x + 2

    >>> R.dmp_pexquo(f, g)
    2*x

    >>> R.dmp_pexquo(f, h)
    Traceback (most recent call last):
    ...
    ExactQuotientFailed: [[2], [2]] does not divide [[1], [1, 0], []]

    N)rp   r   r   )r   rE   r!   r   rb   rc   r   r   r   
dmp_pexquo   s    
rs   c             C   s   t | }t |}g | |  }}}|s.tdn||k r>||fS t||}xt||}	|	| r^P ||	|}
|| }t||
||}t||
||}t|||}|t | }}||k rP qJ||k sJt| ||qJW ||fS )z
    Univariate division with remainder over a ring.

    Examples
    ========

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

    >>> R.dup_rr_div(x**2 + 1, 2*x - 4)
    (0, x**2 + 1)

    zpolynomial division)r   r4   r   r8   r   r+   rK   r   )r   rE   r   rF   rG   rb   rc   rd   rf   rg   r   rW   rI   rk   r   r   r   
dup_rr_div  s.    


rt   c             C   s  |st | ||S t| |}t||}|dk r4tdt|| |  }}}||k rX||fS t|||d  }	}
xt||}t||	|
|\}}t||
sP || }t|||||}t|||||}t	||||}|t|| }}||k rP qn||k snt
| ||qnW ||fS )z
    Multivariate division with remainder over a ring.

    Examples
    ========

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

    >>> R.dmp_rr_div(x**2 + x*y, 2*x + 2)
    (0, x**2 + x*y)

    r   zpolynomial divisionr   )rt   r   r4   r   r   
dmp_rr_divr   r#   r-   r%   r   )r   rE   r!   r   rF   rG   rb   rc   rd   rf   r"   rg   r   ri   rW   rI   rk   r   r   r   ru   P  s2    



ru   c             C   s   t | }t |}g | |  }}}|s.tdn||k r>||fS t||}xxt||}	||	|}
|| }t||
||}t||
||}t|||}|t | }}||k rP qJ||k sJt| ||qJW ||fS )z
    Polynomial division with remainder over a field.

    Examples
    ========

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

    >>> R.dup_ff_div(x**2 + 1, 2*x - 4)
    (1/2*x + 1, 5)

    zpolynomial division)r   r4   r   r8   r   r+   rK   r   )r   rE   r   rF   rG   rb   rc   rd   rf   rg   r   rW   rI   rk   r   r   r   
dup_ff_div  s*    


rv   c             C   s  |st | ||S t| |}t||}|dk r4tdt|| |  }}}||k rX||fS t|||d  }	}
xt||}t||	|
|\}}t||
sP || }t|||||}t|||||}t	||||}|t|| }}||k rP qn||k snt
| ||qnW ||fS )z
    Polynomial division with remainder over a field.

    Examples
    ========

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

    >>> R.dmp_ff_div(x**2 + x*y, 2*x + 2)
    (1/2*x + 1/2*y - 1/2, -y + 1)

    r   zpolynomial divisionr   )rv   r   r4   r   r   
dmp_ff_divr   r#   r-   r%   r   )r   rE   r!   r   rF   rG   rb   rc   rd   rf   r"   rg   r   ri   rW   rI   rk   r   r   r   rw     s2    



rw   c             C   s"   |j rt| ||S t| ||S dS )a.  
    Polynomial division with remainder in ``K[x]``.

    Examples
    ========

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

    >>> R, x = ring("x", ZZ)
    >>> R.dup_div(x**2 + 1, 2*x - 4)
    (0, x**2 + 1)

    >>> R, x = ring("x", QQ)
    >>> R.dup_div(x**2 + 1, 2*x - 4)
    (1/2*x + 1, 5)

    N)r5   rv   rt   )r   rE   r   r   r   r   dup_div  s    rx   c             C   s   t | ||d S )a  
    Returns polynomial remainder in ``K[x]``.

    Examples
    ========

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

    >>> R, x = ring("x", ZZ)
    >>> R.dup_rem(x**2 + 1, 2*x - 4)
    x**2 + 1

    >>> R, x = ring("x", QQ)
    >>> R.dup_rem(x**2 + 1, 2*x - 4)
    5

    r   )rx   )r   rE   r   r   r   r   dup_rem   s    ry   c             C   s   t | ||d S )a  
    Returns exact polynomial quotient in ``K[x]``.

    Examples
    ========

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

    >>> R, x = ring("x", ZZ)
    >>> R.dup_quo(x**2 + 1, 2*x - 4)
    0

    >>> R, x = ring("x", QQ)
    >>> R.dup_quo(x**2 + 1, 2*x - 4)
    1/2*x + 1

    r   )rx   )r   rE   r   r   r   r   dup_quo  s    rz   c             C   s&   t | ||\}}|s|S t| |dS )aW  
    Returns polynomial quotient in ``K[x]``.

    Examples
    ========

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

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

    >>> R.dup_exquo(x**2 + 1, 2*x - 4)
    Traceback (most recent call last):
    ...
    ExactQuotientFailed: [2, -4] does not divide [1, 0, 1]

    N)rx   r   )r   rE   r   rb   rc   r   r   r   	dup_exquo*  s    r{   c             C   s&   |j rt| |||S t| |||S dS )aK  
    Polynomial division with remainder in ``K[X]``.

    Examples
    ========

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

    >>> R, x,y = ring("x,y", ZZ)
    >>> R.dmp_div(x**2 + x*y, 2*x + 2)
    (0, x**2 + x*y)

    >>> R, x,y = ring("x,y", QQ)
    >>> R.dmp_div(x**2 + x*y, 2*x + 2)
    (1/2*x + 1/2*y - 1/2, -y + 1)

    N)r5   rw   ru   )r   rE   r!   r   r   r   r   dmp_divE  s    r|   c             C   s   t | |||d S )a)  
    Returns polynomial remainder in ``K[X]``.

    Examples
    ========

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

    >>> R, x,y = ring("x,y", ZZ)
    >>> R.dmp_rem(x**2 + x*y, 2*x + 2)
    x**2 + x*y

    >>> R, x,y = ring("x,y", QQ)
    >>> R.dmp_rem(x**2 + x*y, 2*x + 2)
    -y + 1

    r   )r|   )r   rE   r!   r   r   r   r   dmp_rem]  s    r}   c             C   s   t | |||d S )a2  
    Returns exact polynomial quotient in ``K[X]``.

    Examples
    ========

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

    >>> R, x,y = ring("x,y", ZZ)
    >>> R.dmp_quo(x**2 + x*y, 2*x + 2)
    0

    >>> R, x,y = ring("x,y", QQ)
    >>> R.dmp_quo(x**2 + x*y, 2*x + 2)
    1/2*x + 1/2*y - 1/2

    r   )r|   )r   rE   r!   r   r   r   r   dmp_quor  s    r~   c             C   s.   t | |||\}}t||r |S t| |dS )a  
    Returns polynomial quotient in ``K[X]``.

    Examples
    ========

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

    >>> f = x**2 + x*y
    >>> g = x + y
    >>> h = 2*x + 2

    >>> R.dmp_exquo(f, g)
    x

    >>> R.dmp_exquo(f, h)
    Traceback (most recent call last):
    ...
    ExactQuotientFailed: [[2], [2]] does not divide [[1], [1, 0], []]

    N)r|   r   r   )r   rE   r!   r   rb   rc   r   r   r   	dmp_exquo  s    
r   c             C   s   | s
|j S tt| |S dS )z
    Returns maximum norm of a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_max_norm(-x**2 + 2*x - 3)
    3

    N)r   rT   r?   )r   r   r   r   r   dup_max_norm  s    r   c                s.   |st |  S |d t fdd| D S )z
    Returns maximum norm of a polynomial in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_max_norm(2*x*y - x - 3)
    3

    r   c                s   g | ]}t | qS r   )dmp_max_norm)r(   r   )r   r"   r   r   r*     s    z dmp_max_norm.<locals>.<listcomp>)r   rT   )r   r!   r   r   )r   r"   r   r     s    
r   c             C   s   | s
|j S tt| |S dS )z
    Returns l1 norm of a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_l1_norm(2*x**3 - 3*x**2 + 1)
    6

    N)r   sumr?   )r   r   r   r   r   dup_l1_norm  s    r   c                s.   |st |  S |d t fdd| D S )z
    Returns l1 norm of a polynomial in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_l1_norm(2*x*y - x - 3)
    6

    r   c                s   g | ]}t | qS r   )dmp_l1_norm)r(   r   )r   r"   r   r   r*     s    zdmp_l1_norm.<locals>.<listcomp>)r   r   )r   r!   r   r   )r   r"   r   r     s    
r   c             C   s:   | s|j gS | d }x | dd D ]}t|||}q"W |S )z
    Multiply together several polynomials in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_expand([x**2 - 1, x, 2])
    2*x**3 - 2*x

    r   r   N)r^   rL   )polysr   r   rE   r   r   r   
dup_expand  s    r   c             C   s>   | st ||S | d }x"| dd D ]}t||||}q$W |S )z
    Multiply together several polynomials in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_expand([x**2 + y**2, x + 1])
    x**3 + x**2 + x*y**2 + y**2

    r   r   N)r   r,   )r   r!   r   r   rE   r   r   r   
dmp_expand  s    
r   N)U__doc__Z
__future__r   r   Zsympy.polys.densebasicr   r   r   r   r   r	   r
   r   r   r   r   r   r   Zsympy.polys.polyerrorsr   r   Zsympy.core.compatibilityr   r   r#   r$   r'   r+   r-   r.   r/   r0   r1   r2   r3   r6   r7   r9   r:   r;   r<   r?   r@   rA   r&   rJ   r    rK   r%   rM   rN   rO   rP   rL   r,   rS   rZ   r`   ra   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   <module>   s   <	""#*#*9+(0%(5-9315.5