
    wgj                        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mZmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+m,Z, ddl-m.Z. ddl/m0Z1m2Z3 d Z4d Z5d	 Z6d
 Z7d Z8d Z9d Z:d Z;d Z<d Z=d Z>d Z?d Z@d ZAd ZBd ZCd ZDd ZEd ZFd ZGd ZHd ZId ZJd ZKd ZLd  ZMd! ZNd" ZOd# ZPd$ ZQd% ZRd& ZSd' ZTd( ZUd) ZVd* ZWd+ ZXd, ZYd- ZZd. Z[d/ Z\d6d1Z]d2 Z^d6d3Z_d4 Z`d5 Zay0)7zHAdvanced tools for dense recursive polynomials in ``K[x]`` or ``K[X]``.     )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)	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)MultivariatePolynomialErrorDomainError)
variations)ceillog2c           
         |dk  s| s| S |j                   g|z  }t        t        |             D ]N  \  }}|dz   }t        d|      D ]  }|||z   dz   z  } |j	                  d|j                  | ||                   P |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reversedrangeinsertexquo)fmKgicnjs           [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/densetools.pydup_integrater@   '   s      	AvQ	

A(1+& &1Eq! 	AQNA	 	
AGGAqt$%& H    c           
      6   |st        | ||      S |dk  st        | |      r| S t        ||dz
  |      |dz
  }}t        t	        |             D ]J  \  }}|dz   }t        d|      D ]  }	|||	z   dz   z  } |j                  dt        | ||      ||             L |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   r0   )r@   r&   r)   r2   r3   r4   r5   r   )
r7   r8   ur9   r:   vr;   r<   r=   r>   s
             r?   dmp_integraterE   G   s      Q1%%AvAq!QAq!1q5qA(1+& 31Eq! 	AQNA	 	
N1adAq123 HrA   c                     ||k(  rt        | |||      S |dz
  |dz   }}t        | D cg c]  }t        ||||||       c}|      S c c}w )z.Recursive helper for :func:`dmp_integrate_in`.r0   )rE   r   _rec_integrate_inr:   r8   rD   r;   r>   r9   wr<   s           r?   rG   rG   j   sV    AvQ1a((q5!a%qAAGq(Aq!Q:GKKG   Ac                 V    |dk  s||kD  rt        d||fz        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)
IndexErrorrG   r7   r8   r>   rC   r9   s        r?   dmp_integrate_inrN   t   s;      	1uACq!fLMMQ1aA..rA   c                 T   |dk  r| S t        |       }||k  rg S g }|dk(  r5| d|  D ]!  }|j                   ||      |z         |dz  }# t        |      S | d|  D ]@  }|}t        |dz
  ||z
  d      D ]  }||z  }	 |j                   ||      |z         |dz  }B 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   r0   N)r   appendr4   r   )r7   r8   r9   r=   derivcoeffkr;   s           r?   dup_diffrU      s      	Av1A1u	EAvsV 	ELL1e$FA	 U sV 	EA1q5!a%, Q LL1e$FA	 UrA   c           	         |st        | ||      S |dk  r| S t        | |      }||k  rt        |      S g |dz
  }}|dk(  r4| d|  D ]*  }|j                  t	        | ||      ||             |dz  }, nR| d|  D ]I  }|}t        |dz
  ||z
  d      D ]  }	||	z  }	 |j                  t	        | ||      ||             |dz  }K 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   r0   NrP   )rU   r   r$   rQ   r   r4   r   )
r7   r8   rC   r9   r=   rR   rD   rS   rT   r;   s
             r?   dmp_diffrW      s
   $ 1a  Av1aA1u{1q51EAvsV 	ELLqtQ:;FA	 sV 	EA1q5!a%, Q LLqtQ:;FA	 UArA   c                     ||k(  rt        | |||      S |dz
  |dz   }}t        | D cg c]  }t        ||||||       c}|      S c c}w )z)Recursive helper for :func:`dmp_diff_in`.r0   )rW   r   _rec_diff_inrH   s           r?   rY   rY      U    Av1a##q5!a%qAqB!|Aq!Q15BAFFBrJ   c                 X    |dk  s||kD  rt        d|d|      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   
0 <= j <=  expected, got )rL   rY   rM   s        r?   dmp_diff_inr^      6    $ 	1uAAqABB1aAq))rA   c                 z    |s|j                  t        | |            S |j                  }| D ]  }||z  }||z  } |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

    )convertr"   r1   )r7   ar9   resultr<   s        r?   dup_evalrd     sM     yy1&&VVF !! MrA   c                     |st        | ||      S |st        | |      S t        | |      |dz
  }}| dd D ]  }t        ||||      }t	        ||||      }  |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

    r0   N)rd   r#   r    r   r   )r7   rb   rC   r9   rc   rD   rS   s          r?   dmp_evalrf      st     1a  a|q!a!eAF12 .1a01-. MrA   c                     ||k(  rt        | |||      S |dz
  |dz   }}t        | D cg c]  }t        ||||||       c}|      S c c}w )z)Recursive helper for :func:`dmp_eval_in`.r0   )rf   r   _rec_eval_in)r:   rb   rD   r;   r>   r9   r<   s          r?   rh   rh   =  rZ   rJ   c                 X    |dk  s||kD  rt        d|d|      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   r\   r]   )rL   rh   )r7   rb   r>   rC   r9   s        r?   dmp_eval_inrj   G  r_   rA   c           
          ||k(  rt        | |d   |      S | D cg c]  }t        ||dz   |||       }}||t        |      z
  dz   k  r|S t        ||| |z   dz
     |      S c c}w )z+Recursive helper for :func:`dmp_eval_tail`.rP   r0   )rd   _rec_eval_taillen)r:   r;   ArC   r9   r<   hs          r?   rl   rl   _  s{    Av1R5!$$9:<AnQAq!Q/<<q3q6zA~HAq!a!}a00 =s   A"c                     |s| S t        | |      rt        |t        |      z
        S t        | d|||      }|t        |      dz
  k(  r|S t	        ||t        |      z
        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   r0   )r&   r$   rm   rl   r   )r7   rn   rC   r9   es        r?   dmp_eval_tailrr   l  sd    $ !QCF
##q!Q1%ACFQJAAJ''rA   c                     ||k(  rt        t        | |||      |||      S |dz
  |dz   }}t        | D cg c]  }t        |||||||       c}|      S c c}w )z+Recursive helper for :func:`dmp_diff_eval`.r0   )rf   rW   r   _rec_diff_eval)r:   r8   rb   rD   r;   r>   r9   r<   s           r?   rt   rt     sb    AvAq!,aA66q5!a%qAAGq~aAq!Q:GKKGs   Ac           	          ||kD  rt        d|d|d|      |st        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 <= j < r]   r   )rL   rf   rW   rt   )r7   r8   rb   r>   rC   r9   s         r?   dmp_diff_eval_inrw     sS    $ 	1uQ1EFFAq!,aA66!Q1aA..rA   c                 |   |j                   rGg }| D ]5  }||z  }||dz  kD  r|j                  ||z
         %|j                  |       7 t	        |      S |j                  r7t        |      }| D cg c]  } |t        |      |z         }}t	        |      S | D cg c]  }||z  	 }}t	        |      S c c}w c c}w )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

       )is_ZZrQ   is_FiniteFieldintr   )r7   pr9   r:   r<   pis         r?   	dup_truncr     s     	ww 	AAA16zQ	 Q< 
		V&')aAn)) Q<  a!e  Q<	 * s   /B4B9c                 ^    t        | D cg c]  }t        |||dz
  |       c}|      S c c}w )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

    r0   )r   r   )r7   r}   rC   r9   r<   s        r?   	dmp_truncr     s.    " ;1wq!QUA.;Q??;s   *c                     |st        | ||      S |dz
  }t        | D cg c]  }t        ||||       c}|      S c c}w )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

    r0   )r   r   dmp_ground_trunc)r7   r}   rC   r9   rD   r<   s         r?   r   r     sF      Aq!!	AAQ@'1a3@!DD@s   ;c                 b    | s| S t        | |      }|j                  |      r| S t        | ||      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

    )r   is_oner   )r7   r9   lcs      r?   	dup_monicr     s6    $ 	1Bxx|2q))rA   c                     |st        | |      S t        | |      r| S t        | ||      }|j                  |      r| S t	        | |||      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

    )r   r&   r!   r   r   )r7   rC   r9   r   s       r?   dmp_ground_monicr     sQ    , A!Q	q!Q	Bxx|2q!,,rA   c                     ddl m} | s|j                  S |j                  }||k(  r| D ]  }|j                  ||      } |S | D ](  }|j                  ||      }|j	                  |      s' |S  |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.domainsr   r1   gcdr   )r7   r9   r   contr<   s        r?   dup_contentr   ?  s    , 'vv66DBw 	"A55q>D	" K  	A55q>Dxx~K	 KrA   c           	      F   ddl m} |st        | |      S t        | |      r|j                  S |j                  |dz
  }}||k(  r&| D ]  }|j                  |t        |||            }! |S | D ]3  }|j                  |t        |||            }|j                  |      s2 |S  |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   r   r0   )r   r   r   r&   r1   r   dmp_ground_contentr   )r7   rC   r9   r   r   rD   r<   s          r?   r   r   i  s    , '1a  !Qvvffa!e!DBw 	<A551!Q:;D	< K  	A551!Q:;Dxx~K	 KrA   c                     | s|j                   | fS t        | |      }|j                  |      r|| fS |t        | ||      f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)

    )r1   r   r   r   )r7   r9   r   s      r?   dup_primitiver     sI    , vvqyq!Dxx~Qw^AtQ///rA   c                     |st        | |      S t        | |      r|j                  | fS t        | ||      }|j	                  |      r|| fS |t        | |||      f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)

    )r   r&   r1   r   r   r   )r7   rC   r9   r   s       r?   dmp_ground_primitiver     sd    , Q""!QvvqyaA&Dxx~Qw^AtQ222rA   c                     t        | |      }t        ||      }|j                  ||      }|j                  |      st        | ||      } 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)

    )r   r   r   r   )r7   r:   r9   fcgcr   s         r?   dup_extractr     s^     
Q	B	Q	B
%%B-C88C=1c1%1c1%19rA   c                     t        | ||      }t        |||      }|j                  ||      }|j                  |      st        | |||      } 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)

    )r   r   r   r   )r7   r:   rC   r9   r   r   r   s          r?   dmp_ground_extractr     sf     
Aq!	$B	Aq!	$B
%%B-C88C=1c1a(1c1a(19rA   c                 <   |j                   s|j                  st        d|z        t        d      }t        d      }| s||fS |j                  |j
                  gg|j                  gg gg}t        | d   d      }| dd D ])  }t        ||d|      }t        |t        |d      dd|      }+ t        |      }|j                         D ]Q  \  }}|dz  }	|	st        ||d|      }|	dk(  rt        ||d|      }0|	dk(  rt        ||d|      }Dt        ||d|      }S ||fS )a  
    Find ``f1`` and ``f2``, such that ``f(x+I*y) = f1(x,y) + f2(x,y)*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)

    >>> from sympy.abc import x, y, z
    >>> from sympy import I
    >>> (z**3 + z**2 + z + 1).subs(z, x+I*y).expand().collect(I)
    x**3 + x**2 - 3*x*y**2 + x - y**2 + I*(3*x**2*y + 2*x*y - y**3 + y) + 1

    z;computing real and imaginary parts is not supported over %sr0   r   ry   N   )rz   is_QQr+   r$   oner1   r%   r   r   r'   itemsr   r	   )
r7   r9   f1f2r:   ro   r<   HrT   r8   s
             r?   dup_real_imagr     s<   & 77177WZ[[\\	!B	!B2v55!&&/	aeeWbM*A1Q4AqrU 7Aq!QJq!,aA67 	A	 
&1EQ1%B!VQ1%B!VQ1%BQ1%B
& r6MrA   c                 j    t        |       } t        t        |       dz
  dd      D ]  }| |    | |<    | 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

    ry   rP   )listr4   rm   )r7   r9   r;   s      r?   
dup_mirrorr   C  sC     	QA3q6A:r2& !u! HrA   c                     t        |       t        |       dz
  |}}} t        |dz
  dd      D ]  }|| |   z  ||z  c| |<   } | 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

    r0   rP   r   rm   r4   )r7   rb   r9   r=   br;   s         r?   	dup_scaler   Y  s[     1gs1vz1!qA1q5"b! AaD&!A#!a HrA   c                     t        |       t        |       dz
  }} t        |dd      D ])  }t        d|      D ]  }| |dz   xx   || |   z  z  cc<    + | 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

    r0   r   rP   r   )r7   rb   r9   r=   r;   r>   s         r?   	dup_shiftr   o  sf     7CFQJqA1a_ q! 	Aa!eH!A$H	 HrA   c           	      d   |st        | |d   |      S t        | |      r| S |d   |dd }}| D cg c]  }t        |||dz
  |       } }t        |       dz
  }t	        |dd      D ]D  }t	        d|      D ]3  }	t        | |	   ||dz
  |      }
t        | |	dz      |
|dz
  |      | |	dz   <   5 F | S c c}w )a  
    Evaluate efficiently Taylor shift ``f(X + A)`` in ``K[X]``.

    Examples
    ========

    >>> from sympy import symbols, ring, ZZ
    >>> x, y = symbols('x y')
    >>> R, _, _ = ring([x, y], ZZ)

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

    >>> R.dmp_shift(R(p), [ZZ(1), ZZ(2)])
    x**2*y + 2*x**2 + 4*x*y + 11*x + 7*y + 22

    >>> p.subs({x: x + 1, y: y + 2}).expand()
    x**2*y + 2*x**2 + 4*x*y + 11*x + 7*y + 22
    r   r0   NrP   )r   r&   	dmp_shiftrm   r4   r   r   )r7   rb   rC   r9   a0a1r<   r=   r;   r>   afjs              r?   r   r     s    & AaD!$$!QqT1QR5B,-/q)Ar1Q3
"/A/A
A1a_ 6q! 	6A 1r1Q32CqQxac15Aa!eH	66
 H 	0s   B-c                 4   | sg S t        |       dz
  }| d   g|j                  gg}}t        d|      D ]!  }|j                  t	        |d   ||             # t        | dd |dd       D ],  \  }}t	        |||      }t        |||      }t        |||      }. |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

    r0   r   rP   N)rm   r   r4   rQ   r
   zipr   r   )	r7   r}   qr9   r=   ro   Qr;   r<   s	            r?   dup_transformr     s     	A
AaD6QUUG9qA1a[ '	21%&' AabE1QR5! 1Aq!1a#Aq!
 HrA   c           	          t        |      dk  r!t        t        | t        ||      |      g      S | sg S | d   g}| dd D ]  }t	        |||      }t        ||d|      } |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

    r0   r   N)rm   r   rd   r   r
   r   )r7   r:   r9   ro   r<   s        r?   dup_composer     sy     1v{(1fQlA6788		
1AqrU %Aq!Aq!$% HrA   c                     |st        | ||      S t        | |      r| S | d   g}| dd D ]  }t        ||||      }t        ||d||      }! |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   r0   N)r   r&   r   r   )r7   r:   rC   r9   ro   r<   s         r?   dmp_composer     sn     1a##!Q	
1AqrU (Aq!QAq!Q'( HrA   c                    t        |       dz
  }t        | |      }t        |       } ||j                  i}||z  }t	        d|      D ]t  }|j
                  }t	        d|      D ]9  }	||	z   |z
  | vr||	z
  |vr| ||	z   |z
     |||	z
     }}
||||	z  z
  |
z  |z  z  }; |j                  |||z  |z        |||z
  <   v t        ||      S )+Helper function for :func:`_dup_decompose`.r0   r   )rm   r   r'   r   r4   r1   quor(   )r7   sr9   r=   r   r:   rr;   rS   r>   r   r   s               r?   _dup_right_decomposer     s    A
A	1BA
QUUA	QA1a[ (q! 	%Aq519>q5A:q1uqy\1QU8Ba!A#gr\"_$E	% 55!B'!a%( Q""rA   c                     i d}}| r8t        | ||      \  }}t        |      dkD  ryt        ||      ||<   ||dz   }} | r8t        ||      S )r   r   Nr0   )r   r   r   r(   )r7   ro   r9   r:   r;   r   r   s          r?   _dup_left_decomposer   !  s_    qqA
q!Q1a=1!Q<AaDa!eqA  Q""rA   c                     t        |       dz
  }t        d|      D ]0  }||z  dk7  rt        | ||      }|t        | ||      }|,||fc S  y)z*Helper function for :func:`dup_decompose`.r0   ry   r   N)rm   r4   r   r   )r7   r9   dfr   ro   r:   s         r?   _dup_decomposer   1  sf    	Q!B1b\ 
6Q; Aq)=#Aq!,A}!t
 rA   c                 L    g }	 t        | |      }||\  } }|g|z   }nn| g|z   S )ae  
    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]_

    )r   )r7   r9   Frc   ro   s        r?   dup_decomposer   D  sH    H 	A
1%DAqaA  37NrA   c                 .   |j                   r |j                         }t        | |||      } |}|j                  st	        d      t        | |      g g }}}|j                         D ]#  \  }}|j                  r|j                  |       % t        ddgt        |      d      }	|	D ]L  }
t        |      }t        |
|      D ]  \  }}|dk(  s||    ||<    |j                  t        |||             N 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 domainrP   r0   T)
repetition)is_GaussianFieldas_AlgebraicFieldr   is_Algebraicr+   r   r   	is_groundrQ   r,   rm   dictr   r   r   dom)r7   rC   r9   K1r   monomspolysmonomrS   permspermGsigns                r?   dmp_liftr   v  s"   & 	  "1a$>>AC 	C #1a("buvA	 !uMM% ! AwF=E -GtV, 	%KD%rzeH9%	% 	]1a+,- z%A.1aee<<rA   c                 l    |j                   d}}| D ]   }|j                  ||z        r|dz  }|s|}" |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   r0   )r1   is_negative)r7   r9   prevrT   rS   s        r?   dup_sign_variationsr     sH     ffa!D ==t$FAD HrA   Nc           
         ||j                   r|j                         }n|}|j                  }| D ]#  }|j                  ||j	                  |            }% |j                  |      r|s|| fS |t        | ||      fS | D cg c]5  }|j                  |      |j                  ||j	                  |            z  7 } }|s|t        | ||      fS || fS c c}w )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)

    )	has_assoc_Ringget_ringr   lcmdenomr   r   numerr   )r7   K0r   ra   commonr<   s         r?   dup_clear_denomsr     s    $ 
zBBVVF -,- 
yy19;q"b111 ;<<Q!RVVFBHHQK0	0<A<{1b"---qy 	=s    :Cc           
          |j                   }|s*| D ]#  }|j                  ||j                  |            }% |S |dz
  }| D ]   }|j                  |t        ||||            }" |S )z.Recursive helper for :func:`dmp_clear_denoms`.r0   )r   r   r   _rec_clear_denoms)r:   rD   r   r   r   r<   rI   s          r?   r   r     s{    VVF 	1AVVFBHHQK0F	1 M E 	EAVVF$5aB$CDF	E MrA   c                     |st        | |||      S ||j                  r|j                         }n|}t        | |||      }|j	                  |      st        | |||      } |s|| fS |t        | |||      f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)

    )ra   )r   r   r   r   r   r   r   )r7   rC   r   r   ra   r   s         r?   dmp_clear_denomsr     s    $ 2r7;;	zBBq!R,F99V1fa,qy{1aR000rA   c                    |j                  t        | |            g}|j                  |j                  |j                  g}t	        t        t        |                  }t        d|dz         D ]Z  }t        | |d      |      }t        | t        ||      |      }t        t        |||      ||      }t        |t        |      |      }\ |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

    r0   ry   )revertr"   r   r1   r|   _ceil_log2r4   r   r
   r   r   r   r   r   )	r7   r=   r9   r:   ro   Nr;   rb   r   s	            r?   
dup_revertr   #  s    ( 
&A,	 A	
AE%(OA1a!e_ ,1adA&Awq!}a(GAq!$a+q*Q-+	, HrA   c                 8    |st        | ||      S t        | |      )z
    Compute ``f**(-1)`` mod ``x**n`` using Newton iteration.

    Examples
    ========

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

    )r   r*   )r7   r:   rC   r9   s       r?   
dmp_revertr   E  s#     !Q"")!Q//rA   )NF)b__doc__sympy.polys.densearithr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   sympy.polys.densebasicr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   sympy.polys.polyerrorsr*   r+   sympy.utilitiesr,   mathr-   r   r.   r   r@   rE   rG   rN   rU   rW   rY   r^   rd   rf   rh   rj   rl   rr   rt   rw   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    rA   r?   <module>r      sO   N           ' -@ FL/,(V,^G*04:G*0
1(@L/4D@(E0*:!-H'T*Z0B!3H441h,,.#L>::#8# &/d-=`4*Z #1LD0rA   