
    Ed<                        d 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 Zd Zd Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d  Z.d! Z/d" Z0d# Z1d$ Z2d% Z3d& Z4d' Z5d( Z6d) Z7d* Z8d+ Z9d, Z:d- Z;d. Z<d/ Z=d0 Z>d1 Z?d2 Z@d3 ZAd4 ZBd5 ZCd6 ZDd7 ZEd8 ZFd9 ZGd: ZHd; ZId< ZJd= ZKd> ZLd? ZMd@ ZNdA ZOdB ZPdC ZQdDS )EzEArithmetics for dense recursive polynomials in ``K[x]`` or ``K[X]``.     )	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PolynomialDivisionFailedc                    |s| S t          |           }||z
  dz
  }||dz
  k    r$t          | d         |z   g| dd         z             S ||k    r|g|j        g||z
  z  z   | z   S | d|         | |         |z   gz   | |dz   d         z   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ms         6lib/python3.11/site-packages/sympy/polys/densearith.pydup_add_termr      s      AA	A	AAEz 2!A$(ae+,,,6 	23!&1q5))A--RaR5AaD1H:%!a%&&	11    c                    |st          | |||          S |dz
  }t          ||          r| S t          |           }||z
  dz
  }||dz
  k    r2t          t	          | d         |||          g| dd         z   |          S ||k    r|gt          ||z
  ||          z   | z   S | d|         t	          | |         |||          gz   | |dz   d         z   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   s           r   dmp_add_termr&   +   s     (Aq!Q'''	AA!Q AA	A	AAEz @'!A$1a001AabbE91===6 	@31q5!Q///!33RaR5GAaD!Q2233aAi??r    c                    |s| S t          |           }||z
  dz
  }||dz
  k    r$t          | d         |z
  g| dd         z             S ||k    r| g|j        g||z
  z  z   | z   S | d|         | |         |z
  gz   | |dz   d         z   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   Nr   r   s         r   dup_sub_termr(   M   s      AA	A	AAEz 2!A$(ae+,,,6 	2B416(AE**Q..RaR5AaD1H:%!a%&&	11r    c                    |st          | | ||          S |dz
  }t          ||          r| S t          |           }||z
  dz
  }||dz
  k    r2t          t	          | d         |||          g| dd         z   |          S ||k    r*t          |||          gt          ||z
  ||          z   | z   S | d|         t	          | |         |||          gz   | |dz   d         z   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#   s           r   dmp_sub_termr,   j   s     )Ar1a(((	AA!Q AA	A	AAEz @'!A$1a001AabbE91===6 	@Aq!$$%	!a%A(>(>>BBRaR5GAaD!Q2233aAi??r    c                 D    r| sg S fd| D             |j         g|z  z   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                     g | ]}|z  S  r/   .0cfr   s     r   
<listcomp>z dup_mul_term.<locals>.<listcomp>       %%%Ba%%%r    r   )r   r   r   r   s    `  r   dup_mul_termr6      s@      3A 3	%%%%!%%%
22r    c                     |st          | |          S |dz
  t          | |          r| S t                    rt          |          S fd| D             t          |          z   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                 4    g | ]}t          |          S r/   )dmp_mulr1   r2   r   r   r%   s     r   r3   z dmp_mul_term.<locals>.<listcomp>   s'    333"Q1%%333r    )r6   r
   r   r   )r   r   r   r$   r   r%   s    `  `@r   dmp_mul_termr;      s      (Aq!Q'''	AA!Q !Q I{{333333333i1a6H6HHHr    c                 &    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   s      r   dup_add_groundr>           1a###r    c                 J    t          | t          ||dz
            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   s       r   dmp_add_groundrB      (     :aQ//Aq999r    c                 &    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=   s      r   dup_sub_groundrE      r?   r    c                 J    t          | t          ||dz
            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   rA   s       r   dmp_sub_groundrG      rC   r    c                 ,    r| sg S fd| 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                     g | ]}|z  S r/   r/   r0   s     r   r3   z"dup_mul_ground.<locals>.<listcomp>  r4   r    r/   r=   s    ` r   dup_mul_groundrJ      s3      &A &	%%%%!%%%%r    c                 X    |st          |           S |dz
  f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                 4    g | ]}t          |          S r/   )dmp_mul_groundr:   s     r   r3   z"dmp_mul_ground.<locals>.<listcomp>&  '    666R^B1a((666r    )rJ   r   r   r$   r   r%   s    ` `@r   rM   rM     sJ      'aA&&&	AA66666616666r    c                 x    st          d          | s| S j        rfd| D             S fd| 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

    polynomial divisionc                 <    g | ]}                     |          S r/   )quor1   r2   r   r   s     r   r3   z"dup_quo_ground.<locals>.<listcomp>A  s%    +++"r1+++r    c                     g | ]}|z  S r/   r/   r0   s     r   r3   z"dup_quo_ground.<locals>.<listcomp>C  s    &&&Rq&&&r    )ZeroDivisionErroris_Fieldr=   s    ``r   dup_quo_groundrX   )  sk    $  7 5666 z '+++++++++&&&&1&&&&r    c                 X    |st          |           S |dz
  f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                 4    g | ]}t          |          S r/   )dmp_quo_groundr:   s     r   r3   z"dmp_quo_ground.<locals>.<listcomp>]  rN   r    )rX   rO   s    ` `@r   r[   r[   F  sJ    $  'aA&&&	AA66666616666r    c                 N    st          d          | s| S f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

    rQ   c                 <    g | ]}                     |          S r/   )exquorT   s     r   r3   z$dup_exquo_ground.<locals>.<listcomp>s  s%    )))QWWR^^)))r    )rV   r=   s    ``r   dup_exquo_groundr_   `  sG      7 5666 )))))a))))r    c                 X    |st          |           S |dz
  f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                 4    g | ]}t          |          S r/   )dmp_exquo_groundr:   s     r   r3   z$dmp_exquo_ground.<locals>.<listcomp>  s(    888rb!Q**888r    )r_   rO   s    ` `@r   rb   rb   v  sJ      )1a(((	AA888888Q8888r    c                 &    | s| S | |j         g|z  z   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

    r5   r   r   r   s      r   
dup_lshiftre     s#      AF8A:~r    c                     | 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/   rd   s      r   
dup_rshiftrg     s      SqbS6Mr    c                      f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                 :    g | ]}                     |          S r/   )abs)r1   coeffr   s     r   r3   zdup_abs.<locals>.<listcomp>  s#    ***eQUU5\\***r    r/   r   r   s    `r   dup_absrm     s     +***q****r    c                 R    |st          |           S |dz
  f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                 2    g | ]}t          |          S r/   )dmp_absr1   r2   r   r%   s     r   r3   zdmp_abs.<locals>.<listcomp>  %    ,,,2WRA,,,r    )rm   r   r$   r   r%   s     `@r   rp   rp     B      q!}}	AA,,,,,,,,,r    c                     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                     g | ]}| S r/   r/   r1   rk   s     r   r3   zdup_neg.<locals>.<listcomp>  s    $$$eV$$$r    r/   rl   s     r   dup_negrx     s     %$$$$$r    c                 R    |st          |           S |dz
  f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                 2    g | ]}t          |          S r/   )r+   rq   s     r   r3   zdmp_neg.<locals>.<listcomp>  rr   r    )rx   rs   s     `@r   r+   r+     rt   r    c                 d   | s|S |s| S t          |           }t          |          }||k    r't          d t          | |          D                       S t          ||z
            }||k    r| d|         | |d         } }n|d|         ||d         }}|d t          | |          D             z   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                     g | ]
\  }}||z   S r/   r/   r1   abs      r   r3   zdup_add.<locals>.<listcomp>       888TQ1q5888r    Nc                     g | ]
\  }}||z   S r/   r/   r}   s      r   r3   zdup_add.<locals>.<listcomp>!       222tq!QU222r    )r   r   ziprj   r   gr   dfdgkhs          r   dup_addr     s       	AB	AB	Rx 
388SAYY888999RLL7 	 RaR5!ABB%qAARaR5!ABB%qA22s1ayy22222r    c                    |st          | |          S t          | |          }|dk     r|S t          ||          }|dk     r| S |dz
  ||k    r+t          fdt          | |          D             |          S t	          ||z
            }||k    r| d|         | |d         } }n|d|         ||d         }}|fdt          | |          D             z   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                 :    g | ]\  }}t          ||          S r/   r"   r1   r~   r   r   r%   s      r   r3   zdmp_add.<locals>.<listcomp>B  +    FFF41a71aA..FFFr    Nc                 :    g | ]\  }}t          ||          S r/   r   r   s      r   r3   zdmp_add.<locals>.<listcomp>K  +    @@@TQWQ1a((@@@r    )r   r   r	   r   rj   	r   r   r$   r   r   r   r   r   r%   s	      `    @r   r"   r"   $  s       q!Q	Aq		B	Av 	Aq		B	Av 	AA	Rx 
AFFFFF3q!99FFFJJJRLL7 	 RaR5!ABB%qAARaR5!ABB%qA@@@@@SAYY@@@@@r    c                    | st          ||          S |s| S t          |           }t          |          }||k    r't          d t          | |          D                       S t	          ||z
            }||k    r| d|         | |d         } }n"t          |d|         |          ||d         }}|d t          | |          D             z   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                     g | ]
\  }}||z
  S r/   r/   r}   s      r   r3   zdup_sub.<locals>.<listcomp>e  r   r    Nc                     g | ]
\  }}||z
  S r/   r/   r}   s      r   r3   zdup_sub.<locals>.<listcomp>n  r   r    )rx   r   r   r   rj   r   s          r   dup_subr   N  s      q!}} 	AB	AB	Rx 
388SAYY888999RLL7 	,RaR5!ABB%qAA1RaR5!$$aeqA22s1ayy22222r    c                    |st          | |          S t          | |          }|dk     rt          ||          S t          ||          }|dk     r| S |dz
  ||k    r+t          fdt	          | |          D             |          S t          ||z
            }||k    r| d|         | |d         } }n#t          |d|         |          ||d         }}|fdt	          | |          D             z   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                 :    g | ]\  }}t          ||          S r/   r*   r   s      r   r3   zdmp_sub.<locals>.<listcomp>  r   r    Nc                 :    g | ]\  }}t          ||          S r/   r   r   s      r   r3   zdmp_sub.<locals>.<listcomp>  r   r    )r   r   r+   r	   r   rj   r   s	      `    @r   r*   r*   q  s9      q!Q	Aq		B	Av  q!Q	Aq		B	Av 	AA	Rx 
AFFFFF3q!99FFFJJJRLL7 	/RaR5!ABB%qAA1RaR5!Q''122qA@@@@@SAYY@@@@@r    c                 B    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

    )r   dup_mulr   r   r   r   s       r   dup_add_mulr     "     1gaA&&***r    c           	      F    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"   r9   r   r   r   r$   r   s        r   dmp_add_mulr     &     1gaAq))1a000r    c                 B    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

    )r   r   r   s       r   dup_sub_mulr     r   r    c           	      F    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*   r9   r   s        r   dmp_sub_mulr     r   r    c           
         | |k    rt          | |          S | r|sg S t          |           }t          |          }t          ||          dz   }|dk     rg }t          d||z   dz             D ]j}|j        }t          t          d||z
            t          ||          dz             D ]}	|| |	         |||	z
           z  z  }|                    |           kt          |          S |dz  }
t          | d|
|          t          |d|
|          }}t          t          | |
||          |
|          }t          t          ||
||          |
|          }t          |||          t          |||          }}t          t          |||          t          |||          |          }t          |t          |||          |          }t          t          |t          ||
|          |          t          |d|
z  |          |          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      )dup_sqrr   maxranger   minappendr   r   rg   r   r   r   re   )r   r   r   r   r   r   r   r   rk   jn2flglfhghlohimids                     r   r   r     s    	Av q!}} ! 		AB	ABBaA3w 3q"r'A+&& 	 	AFE3q!b&>>3r1::>:: ' '1aAh&HHUOOOO||
 T1aQ''1aQ)?)?B	!RA..A66	!RA..A66R##WRQ%7%7Bgb"a(('"b!*<*<a@@c72r1--q11wr:c2q#9#91==!"adA..3 3 	3r    c                 *   |st          | ||          S | |k    rt          | ||          S t          | |          }|dk     r| S t          ||          }|dk     r|S g |dz
  }}t          d||z   dz             D ]}t	          |          }	t          t          d||z
            t          ||          dz             D ]3}
t          |	t          | |
         |||
z
           ||          ||          }	4|	                    |	           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   )r   dmp_sqrr   r   r   r   r   r"   r9   r   r	   )r   r   r$   r   r   r   r   r%   r   rk   r   s              r   r9   r9     s6      q!QAv  q!Q	Aq		B	Av 	Aq		B	Av q1uqA1b2gk""  s1a"f~~s2qzzA~66 	H 	HAE71Q41q51a#@#@!QGGEE	Q??r    c                    t          |           dz
  g }}t          dd|z  dz             D ]}|j        }t          d||z
            }t	          ||          }||z
  dz   }||dz  z   dz
  }t          ||dz             D ]}	|| |	         | ||	z
           z  z  }||z  }|dz  r| |dz            }
||
dz  z  }|                    |           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   r   )r   r   r   r   r   r   r   )r   r   r   r   r   r   jminjmaxr   r   elems              r   r   r   C  s    FFQJB1adQh  F1a"f~~1bzz4K!Oa1f}q tTAX&& 	 	A1aAhAA	Qq5 	TAX;DqLA	Q<<r    c                    |st          | |          S t          | |          }|dk     r| S g |dz
  }}t          dd|z  dz             D ]}t          |          }t	          d||z
            }t          ||          }	|	|z
  dz   }
||
dz  z   dz
  }	t          ||	dz             D ]3}t          |t          | |         | ||z
           ||          ||          }4t          | |d          ||          }|
dz  r,t          | |	dz            ||          }t          ||||          }|
                    |           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   r   )r   r   r   r   r   r   r"   r9   rM   r   r   r	   )r   r$   r   r   r   r%   r   r   r   r   r   r   r   s                r   r   r   k  sp     q!}}	Aq		B	Av q1uqA1adQh  QKK1a"f~~1bzz4K!Oa1f}q tTAX&& 	@ 	@A71Q41q51a88!Q??AA1aaddAq))q5 	'1TAX;1--D4A&&A	Q??r    c                     |s|j         gS |dk     rt          d          |dk    s| r| |j         gk    r| S |j         g}	 |dz  |}}|dz  rt          || |          }|snt          | |          } 1|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   +Cannot raise polynomial to a negative powerr   Tr   )one
ValueErrorr   r   )r   r   r   r   r   s        r   dup_powr     s      w1u HFGGGAv Q !w, 	
A	!tQ1q5 	1a  A AqMM	 Hr    c                 T   |st          | ||          S |st          ||          S |dk     rt          d          |dk    s!t          | |          st	          | ||          r| S t          ||          }	 |dz  |}}|dz  rt          || ||          }|snt          | ||          } 3|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   r   r   Tr   )r   r   r   r
   r   r9   r   )r   r   r$   r   r   r   s         r   dmp_powr     s       q!Q q!}}1u HFGGGAv Aq!! Yq!Q%7%7 1A	!tQ1q5 	1a##A Aq!	 Hr    c                 J   t          |           }t          |          }g | |}}}|st          d          ||k     r||fS ||z
  dz   }t          ||          }		 t          ||          }
||z
  |dz
  }}t          ||	|          }t	          ||
||          }t          ||	|          }t          ||
||          }t          |||          }|t          |          }}||k     rn||k     st          | ||          |	|z  }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)

    rQ   r   )r   rV   r   rJ   r   r6   r   r   )r   r   r   r   r   qrdrNlc_glc_rr   QRG_drr   s                    r   dup_pdivr     s`    
AB	AB1b"qA  5666	b !t
R!A!Q<<D4a||BwA11dA&&D!Q''1dA&&D!Q''Aq!jmmR7 	4s( 	4*1a333!4$ 	aAq!QAq!QAa4Kr    c                    t          |           }t          |          }| |}}|st          d          ||k     r|S ||z
  dz   }t          ||          }	 t          ||          }	||z
  |dz
  }}
t          |||          }t	          ||	|
|          }t          |||          }|t          |          }}||k     rn||k     st          | ||          ~t          |||z  |          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

    rQ   r   )r   rV   r   rJ   r6   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                 r   dup_premr     s    
AB	ABrrA  5666	b 
R!A!Q<<D4a||BwA11dA&&D!Q''Aq!jmmR7 	4s( 	4*1a3334 !T1Wa(((r    c                 0    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   )r   r   r   r   s      r   dup_pquor   J  s    " Aq!Qr    c                 R    t          | ||          \  }}|s|S t          | |          )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]

    )r   r   r   r   r   r   r   s        r   
dup_pexquor   ^  s6    & Aq!DAq (!!Q'''r    c                    |st          | ||          S t          | |          }t          ||          }|dk     rt          d          t          |          | |}}}||k     r||fS ||z
  dz   }	t	          ||          }
	 t	          ||          }||z
  |	dz
  }	}t          ||
d||          }t          |||||          }t          ||
d||          }t          |||||          }t          ||||          }|t          ||          }}||k     rn||k     st          | ||          t          |
|	|dz
  |          }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   rQ   r   )
r   r   rV   r   r   r;   r&   r*   r   r   )r   r   r$   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                     r   dmp_pdivr   y  s     !1a   	Aq		B	Aq		B	Av 7 5666{{Ar"qA	Bw !t
R!A!Q<<D4a||BwA1D!Q**D!Q**D!Q**D!Q**Aq!QjA&&R7 	4s( 	4*1a333!4$ 	aQ""AQ1a##AQ1a##Aa4Kr    c                 2   |st          | ||          S t          | |          }t          ||          }|dk     rt          d          | |}}||k     r|S ||z
  dz   }t          ||          }		 t          ||          }
||z
  |dz
  }}t	          ||	d||          }t	          ||
|||          }t          ||||          }|t          ||          }}||k     rn||k     st          | ||          t          |	||dz
  |          }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   rQ   r   )r   r   rV   r   r;   r*   r   r   )r   r   r$   r   r   r   r   r   r   r   r   r   r   r   r   r   s                   r   dmp_premr     s_     !1a   	Aq		B	Aq		B	Av 7 5666rrA	Bw 
R!A!Q<<D4a||BwA1D!Q**D!Q**Aq!QjA&&R7 	4s( 	4*1a3334 	aQ""A1aA&&&r    c                 2    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   )r   r   r   r$   r   s       r   dmp_pquor     s    * Aq!Q""r    c                 p    t          | |||          \  }}t          ||          r|S t          | |          )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], []]

    )r   r
   r   r   r   r$   r   r   r   s         r   
dmp_pexquor     sB    . Aq!QDAq!Q (!!Q'''r    c                    t          |           }t          |          }g | |}}}|st          d          ||k     r||fS t          ||          }	 t          ||          }	|	|z  rn|                    |	|          }
||z
  }t	          ||
||          }t          ||
||          }t          |||          }|t          |          }}||k     rn||k     st          | ||          ||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)

    rQ   )r   rV   r   r^   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                 r   
dup_rr_divr     s     
AB	AB1b"qA  5666	b !t!Q<<D4a||$; 	GGD$GAq!$$Aq!$$Aq!jmmR7 	4s( 	4*1a333%4( a4Kr    c                 H   |st          | ||          S t          | |          }t          ||          }|dk     rt          d          t          |          | |}}}||k     r||fS t	          ||          |dz
  }
}		 t	          ||          }t          ||	|
|          \  }}t          ||
          snn||z
  }t          |||||          }t          |||||          }t          ||||          }|t          ||          }}||k     rn||k     st          | ||          ||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   rQ   r   )r   r   rV   r   r   
dmp_rr_divr
   r&   r;   r*   r   r   r   r$   r   r   r   r   r   r   r   r%   r   r   r   r   r   r   s                    r   r   r   M  h     #!Q"""	Aq		B	Aq		B	Av 7 5666{{Ar"qA	Bw !tQllAE!D4a||$a++1!Q 	GAq!Q''Aq!Q''Aq!QjA&&R7 	4s( 	4*1a333%4( a4Kr    c                 @   t          |           }t          |          }g | |}}}|st          d          ||k     r||fS t          ||          }	 t          ||          }	|                    |	|          }
||z
  }t	          ||
||          }t          ||
||          }t          |||          }|t          |          }}||k     rnS||k    r5|j        s.t          |dd                   }t          |          }||k     rnn||k     st          | ||          ||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)

    rQ   Tr   N)
r   rV   r   r^   r   r6   r   is_Exactr   r   r   s                 r   
dup_ff_divr     sZ    
AB	AB1b"qA  5666	b !t!Q<<D4a||GGD$GAq!$$Aq!$$Aq!jmmR7 		43Y 	4qz 	4!ABB%  AABBw s( 	4*1a333+4. a4Kr    c                 H   |st          | ||          S t          | |          }t          ||          }|dk     rt          d          t          |          | |}}}||k     r||fS t	          ||          |dz
  }
}		 t	          ||          }t          ||	|
|          \  }}t          ||
          snn||z
  }t          |||||          }t          |||||          }t          ||||          }|t          ||          }}||k     rn||k     st          | ||          ||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   rQ   r   )r   r   rV   r   r   
dmp_ff_divr
   r&   r;   r*   r   r   s                    r   r   r     r   r    c                 T    |j         rt          | ||          S t          | ||          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)

    )rW   r   r   r   s      r   dup_divr     s2    $ 	z #!Q"""!Q"""r    c                 0    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   r   r   s      r   dup_remr         $ 1aAr    c                 0    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   r   r   s      r   dup_quor     r   r    c                 R    t          | ||          \  }}|s|S t          | |          )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]

    )r   r   r   s        r   	dup_exquor   -  s6    & 1aDAq (!!Q'''r    c                 X    |j         rt          | |||          S t          | |||          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)

    )rW   r   r   r   s       r   dmp_divr   H  s6    $ 	z &!Q1%%%!Q1%%%r    c                 2    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   s       r   dmp_remr   `      $ 1aAq!!r    c                 2    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   s       r   dmp_quor  u  r   r    c                 p    t          | |||          \  }}t          ||          r|S t          | |          )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], []]

    )r   r
   r   r   s         r   	dmp_exquor    sB    . 1aADAq!Q (!!Q'''r    c                 N    | s|j         S t          t          | |                    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

    )r   r   rm   rl   s     r   dup_max_normr    )      "v71a==!!!r    c                 l    |st          |           S |dz
  t          f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                 2    g | ]}t          |          S r/   )dmp_max_normr1   r   r   r%   s     r   r3   z dmp_max_norm.<locals>.<listcomp>  s%    3331aA&&333r    )r  r   rs   s     `@r   r
  r
    sM      "Aq!!!	AA33333333444r    c                 N    | s|j         S t          t          | |                    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

    )r   sumrm   rl   s     r   dup_l1_normr    r  r    c                 l    |st          |           S |dz
  t          f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                 2    g | ]}t          |          S r/   )dmp_l1_normr  s     r   r3   zdmp_l1_norm.<locals>.<listcomp>  s%    222!Q1%%222r    )r  r  rs   s     `@r   r  r    sM      !1a   	AA22222q222333r    c                 @    t          d | D             |j                  S )z
    Returns squared l2 norm of a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_l2_norm_squared(2*x**3 - 3*x**2 + 1)
    14

    c                     g | ]}|d z  S )r   r/   rw   s     r   r3   z'dup_l2_norm_squared.<locals>.<listcomp>  s    (((Uq(((r    )r  r   rl   s     r   dup_l2_norm_squaredr    s%     ((a(((!&111r    c                 l    |st          |           S |dz
  t          fd| D                       S )z
    Returns squared l2 norm of a polynomial in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_l2_norm_squared(2*x*y - x - 3)
    14

    r   c                 2    g | ]}t          |          S r/   )dmp_l2_norm_squaredr  s     r   r3   z'dmp_l2_norm_squared.<locals>.<listcomp>!  s&    :::!$Q1--:::r    )r  r  rs   s     `@r   r  r    sM      )"1a(((	AA:::::q:::;;;r    c                 f    | s|j         gS | d         }| dd         D ]}t          |||          }|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   r   )polysr   r   r   s       r   
dup_expandr  $  sM      waA122Y  Aq!Hr    c                 x    | st          ||          S | d         }| dd         D ]}t          ||||          }|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   r9   )r  r$   r   r   r   s        r   
dmp_expandr  =  sT      q!}}aA122Y    Aq!QHr    N)R__doc__sympy.polys.densebasicr   r   r   r   r   r   r	   r
   r   r   r   r   r   sympy.polys.polyerrorsr   r   r   r&   r(   r,   r6   r;   r>   rB   rE   rG   rJ   rM   rX   r[   r_   rb   re   rg   rm   rp   rx   r+   r   r"   r   r*   r   r   r   r   r   r9   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r  r  r  r  r  r  r/   r    r   <module>r      s   K K                              S R R R R R R R2 2 2:@ @ @D2 2 2:@ @ @D3 3 3(I I I6$ $ $": : :"$ $ $": : :"& & &(7 7 7,' ' ':7 7 74* * *,9 9 9,  (  &+ + +"- - -,% % %"- - -, 3  3  3F'A 'A 'AT 3  3  3F'A 'A 'AT+ + +"1 1 1"+ + +"1 1 1"63 63 63r( ( (V% % %P- - -`" " "J% % %P2 2 2j*) *) *)Z     (( ( (66 6 6r0' 0' 0'f# # #0( ( (>. . .b2 2 2j1 1 1h2 2 2j# # #0  *  *( ( (6& & &0" " "*" " "*( ( (>" " "(5 5 5," " "(4 4 4,2 2 2"< < <,  2    r    