
    Ed.e                        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[d6d1Z\d2 Z]d6d3Z^d4 Z_d5 Z`d/S )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logc           
          |dk    s| s| S |j         g|z  }t          t          |                     D ][\  }}|dz   }t          d|          D ]}|||z   dz   z  }|                    d|                    | ||                               \|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           6lib/python3.11/site-packages/sympy/polys/densetools.pydup_integrater@   '   s      	Av Q 	

A(1++&& & &1Eq! 	 	AQNAA	AGGAqqtt$$%%%%H    c           
      v   |st          | ||          S |dk    st          | |          r| S t          ||dz
  |          |dz
  }}t          t	          |                     D ]W\  }}|dz   }t          d|          D ]}	|||	z   dz   z  }|                    dt          | ||          ||                     X|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 31Eq! 	 	AQNAA	N1aaddAq112222HrA   c                     k    rt          | |          S |dz
  dz   ct          fd| D             |          S )z.Recursive helper for :func:`dmp_integrate_in`.r0   c           
      8    g | ]}t          |          S  )_rec_integrate_in.0r<   r9   r;   r>   r8   ws     r?   
<listcomp>z%_rec_integrate_in.<locals>.<listcomp>q   s,    GGGq(Aq!Q::GGGrA   )rE   r   r:   r8   rD   r;   r>   r9   rL   s    ` ```@r?   rI   rI   j   sk    Av )Q1a(((q5!a%DAqGGGGGGGGAGGGKKKrA   c                 j    |dk     s||k    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)
IndexErrorrI   r7   r8   r>   rC   r9   s        r?   dmp_integrate_inrR   t   sM      	1u NA NCq!fLMMMQ1aA...rA   c                    |dk    r| S t          |           }||k     rg S g }|dk    r5| d|          D ](}|                     ||          |z             |dz  })nU| d|          D ]I}|}t          |dz
  ||z
  d          D ]}||z  }|                     ||          |z             |dz  }Jt          |          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_diffrY      s     	Av 1A1u 	EAv ssV 	 	ELL1e$$$FAA	 ssV 	 	EA1q5!a%,,  QLL1e$$$FAAUrA   c           	         |st          | ||          S |dk    r| S t          | |          }||k     rt          |          S g |dz
  }}|dk    rB| d|          D ]5}|                    t	          | ||          ||                     |dz  }6nb| d|          D ]V}|}t          |dz
  ||z
  d          D ]}	||	z  }|                    t	          | ||          ||                     |dz  }Wt          ||          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   NrT   )rY   r   r$   rU   r   r4   r   )
r7   r8   rC   r9   r=   rV   rD   rW   rX   r;   s
             r?   dmp_diffr[      sQ   $  !1a   Av 1aA1u {{1q51EAv ssV 	 	ELLqqttQ::;;;FAA	 ssV 	 	EA1q5!a%,,  QLLqqttQ::;;;FAAUArA   c                     k    rt          | |          S |dz
  dz   ct          fd| D             |          S )z)Recursive helper for :func:`dmp_diff_in`.r0   c           
      8    g | ]}t          |          S rH   )_rec_diff_inrJ   s     r?   rM   z _rec_diff_in.<locals>.<listcomp>   +    BBB!|Aq!Q155BBBrA   )r[   r   rN   s    ` ```@r?   r^   r^      k    Av $1a###q5!a%DAqBBBBBBBBqBBBAFFFrA   c                 l    |dk     s||k    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 )rP   r^   rQ   s        r?   dmp_diff_inrd      P    $ 	1u CA CjAAAqqABBB1aAq)))rA   c                 |    |s#|                     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_evalrj     sU      'yy1&&&VF  !!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)rj   r#   r    r   r   )r7   rh   rC   r9   ri   rD   rW   s          r?   dmp_evalrl      s      !1a    a||q!a!eAF122 . .1a001--MrA   c                     k    rt          |           S dz
  dz   ct          fd| D                       S )z)Recursive helper for :func:`dmp_eval_in`.r0   c           
      8    g | ]}t          |          S rH   )_rec_eval_in)rK   r<   r9   rh   r;   r>   rD   s     r?   rM   z _rec_eval_in.<locals>.<listcomp>D  r_   rA   )rl   r   )r:   rh   rD   r;   r>   r9   s    `````r?   ro   ro   =  r`   rA   c                 l    |dk     s||k    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   rb   rc   )rP   ro   )r7   rh   r>   rC   r9   s        r?   dmp_eval_inrq   G  re   rA   c                     k    rt          | d                   S fd| D             }t                    z
  dz   k     r|S t          | z   dz
                     S )z+Recursive helper for :func:`dmp_eval_tail`.rT   c           	      <    g | ]}t          |d z             S r0   )_rec_eval_tail)rK   r<   Ar9   r;   rC   s     r?   rM   z"_rec_eval_tail.<locals>.<listcomp>d  s-    <<<AnQAq!Q//<<<rA   r0   )rj   len)r:   r;   rv   rC   r9   hs    ```` r?   ru   ru   _  s    Av 11R5!$$$<<<<<<<<<<q3q66zA~ 	1HAq!a!}a000rA   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$   rw   ru   r   )r7   rv   rC   r9   es        r?   dmp_eval_tailr{   l  s    $  !Q $CFF
###q!Q1%%ACFFQJ (AAJ'''rA   c                     k    r"t          t          |                     S dz
  dz   ct          fd| D                       S )z+Recursive helper for :func:`dmp_diff_eval`.r0   c                 :    g | ]}t          |          S rH   )_rec_diff_eval)rK   r<   r9   rh   r;   r>   r8   rD   s     r?   rM   z"_rec_diff_eval.<locals>.<listcomp>  s-    GGGq~aAq!Q::GGGrA   )rl   r[   r   )r:   r8   rh   rD   r;   r>   r9   s    ``````r?   r~   r~     s}    Av 7Aq!,,aA666q5!a%DAqGGGGGGGGGAGGGKKKrA   c           	          ||k    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 < rc   r   )rP   rl   r[   r~   )r7   r8   rh   r>   rC   r9   s         r?   dmp_diff_eval_inr     sv    $ 	1u GjQQQ11EFFF 7Aq!,,aA666!Q1aA...rA   c                     |j         rDg }| D ]>}|z  }|dz  k    r|                    |z
             )|                    |           ?nfd| D             }t          |          S )z
    Reduce a ``K[x]`` polynomial modulo a constant ``p`` in ``K``.

    Examples
    ========

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

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

       c                     g | ]}|z  S rH   rH   )rK   r<   ps     r?   rM   zdup_trunc.<locals>.<listcomp>  s       a!e   rA   )is_ZZrU   r   )r7   r   r9   r:   r<   s    `   r?   	dup_truncr     s     	w ! 	 	AAA16z Q	 !   Q   Q<<rA   c                 D    t          fd| D                       S )a9  
    Reduce a ``K[X]`` polynomial modulo a polynomial ``p`` in ``K[Y]``.

    Examples
    ========

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

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

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

    c                 :    g | ]}t          |d z
            S rt   )r   )rK   r<   r9   r   rC   s     r?   rM   zdmp_trunc.<locals>.<listcomp>  s+    ;;;1wq!QUA..;;;rA   )r   )r7   r   rC   r9   s    ```r?   	dmp_truncr     s2    " ;;;;;;;;;Q???rA   c                 t    |st          |           S |dz
  t          fd| D             |          S )a   
    Reduce a ``K[X]`` polynomial modulo a constant ``p`` in ``K``.

    Examples
    ========

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

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

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

    r0   c                 4    g | ]}t          |          S rH   )dmp_ground_trunc)rK   r<   r9   r   rD   s     r?   rM   z$dmp_ground_trunc.<locals>.<listcomp>  s(    @@@'1a33@@@rA   )r   r   )r7   r   rC   r9   rD   s    ` `@r?   r   r     sU       "Aq!!!	AA@@@@@@Q@@@!DDDrA   c                 z    | s| S t          | |          }|                    |          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     sG    $  	1Bxx|| *2q)))rA   c                     |st          | |          S t          | |          r| S t          | ||          }|                    |          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     sm    ,  A!Q 	q!Q		Bxx|| -2q!,,,rA   c                     ddl m} | s|j        S |j        }||k    r| D ]}|                    ||          }n2| D ]/}|                    ||          }|                    |          r n0|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    , '&&&&& v6DBw  	" 	"A55q>>DD	"  	 	A55q>>Dxx~~  KrA   c           	      `   ddl m} |st          | |          S t          | |          r|j        S |j        |dz
  }}||k    r+| D ]'}|                    |t          |||                    }(nA| D ]>}|                    |t          |||                    }|                    |          r n?|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   e  s    , '&&&&& !1a   !Q vfa!e!DBw  	< 	<A551!Q::;;DD	<  	 	A551!Q::;;Dxx~~  KrA   c                     | s	|j         | fS t          | |          }|                    |          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     sY    ,  vqyq!Dxx~~ 0Qw^AtQ////rA   c                     |st          | |          S t          | |          r	|j        | fS t          | ||          }|                    |          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     s    ,  #Q"""!Q vqyaA&&Dxx~~ 3Qw^AtQ2222rA   c                     t          | |          }t          ||          }|                    ||          }|                    |          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     ss     
Q		B	Q		B
%%B--C88C== &1c1%%1c1%%19rA   c                     t          | ||          }t          |||          }|                    ||          }|                    |          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     s{     
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 ]5}t          ||d|          }t          |t          |d          dd|          }6t          |          }|
                                D ]c\  }}|dz  }	|	st          ||d|          }|	dk    rt          ||d|          }8|	dk    rt          ||d|          }Qt          ||d|          }d||fS )a4  
    Return bivariate polynomials ``f1`` and ``f2``, such that ``f = f1 + f2*I``.

    Examples
    ========

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

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

    z;computing real and imaginary parts is not supported over %sr0   r   r   N   )r   is_QQr+   r$   oner1   r%   r   r   r'   itemsr   r	   )
r7   r9   f1f2r:   rx   r<   HrX   r8   s
             r?   dup_real_imagr     s~    7 ]17 ]WZ[[\\\	!B	!B 2v5!&/	aeWbM*A1Q4AqrrU 7 7Aq!QJq!,,aA66A		 
& 
&1E 	&Q1%%BB!V 	&Q1%%BB!V 	&Q1%%BBQ1%%BBr6MrA   c                     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

    r   rT   )listr4   rw   )r7   r9   r;   s      r?   
dup_mirrorr   :  sJ     	QA3q66A: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   rT   r   rw   r4   )r7   rh   r9   r=   br;   s         r?   	dup_scaler   P  sb     1ggs1vvz1!qA1q5"b!!  AaD&!A#!aaHrA   c                     t          |           t          |           dz
  }} t          |dd          D ]1}t          d|          D ]}| |dz   xx         || |         z  z  cc<   2| 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   rT   r   )r7   rh   r9   r=   r;   r>   s         r?   	dup_shiftr   f  s}     77CFFQJqA1a__  q! 	 	Aa!eHHH!A$HHHH	 HrA   c                    | sg S t          |           dz
  }| d         g|j        gg}}t          d|          D ],}|                    t	          |d         ||                     -t          | dd         |dd                   D ]8\  }}t	          |||          }t          |||          }t          |||          }9|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   rT   N)rw   r   r4   rU   r
   zipr   r   )	r7   r   qr9   r=   rx   Qr;   r<   s	            r?   dup_transformr   }  s      	A
AaD6QUG9qA1a[[ ' '	21%%&&&&AabbE1QRR5!!  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)rw   r   rj   r   r
   r   )r7   r:   r9   rx   r<   s        r?   dup_composer     s     1vv{ 9(1fQllA667888 		
1AqrrU % %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   rx   r<   s         r?   dmp_composer     s      $1a###!Q 	
1AqrrU ( (Aq!QAq!Q''HrA   c                    t          |           dz
  }t          | |          }t          |           } ||j        i}||z  }t	          d|          D ]{}|j        }t	          d|          D ]?}	||	z   |z
  | vr||	z
  |vr| ||	z   |z
           |||	z
           }}
||||	z  z
  |
z  |z  z  }@|                    |||z  |z            |||z
  <   |t          ||          S )+Helper function for :func:`_dup_decompose`.r0   r   )rw   r   r'   r   r4   r1   quor(   )r7   sr9   r=   r   r:   rr;   rW   r>   r   r   s               r?   _dup_right_decomposer     s   A
A	1BA
QUA	QA1a[[ ( (q! 	% 	%Aq519> q5A: q1uqy\1QU8Ba!A#gr\"_$EE55!B''!a%Q"""rA   c                     i d}}| rEt          | ||          \  }}t          |          dk    rdS t          ||          ||<   ||dz   }} | Et          ||          S )r   r   Nr0   )r   r   r   r(   )r7   rx   r9   r:   r;   r   r   s          r?   _dup_left_decomposer     sz    qqA
 q!Q1a==1 	4!Q<<AaDa!eqA   Q"""rA   c                     t          |           dz
  }t          d|          D ]8}||z  dk    rt          | ||          }|t          | ||          }|||fc S 9dS )z*Helper function for :func:`dup_decompose`.r0   r   r   N)rw   r4   r   r   )r7   r9   dfr   rx   r:   s         r?   _dup_decomposer     s    	Q!B1b\\ 
 
6Q; 	 Aq)) 	#Aq!,,A !t4rA   c                 T    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   Fri   rx   s        r?   dup_decomposer     sQ    H 	A1%% 	DAqaAA 37NrA   c                 n   |j         r(|                                }t          | |||          } |}|j        st	          d          t          | |          g g }}}|                                D ]!\  }}|j        s|                    |           "t          ddgt          |          d          }	|	D ]]}
t          |          }t          |
|          D ]\  }}|dk    r||          ||<   |                    t          |||                     ^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 domainrT   r0   T)
repetition)is_GaussianFieldas_AlgebraicFieldr   is_Algebraicr+   r   r   	is_groundrU   r,   rw   dictr   r   r   dom)r7   rC   r9   K1r   monomspolysmonomrW   permspermGsigns                r?   dmp_liftr   G  s`   & 	   ""1a$$> CAC C 	C #1a(("buvA		 ! !u 	!MM%   AwF===E - -GGtV,, 	% 	%KD%rz %eH9%]1a++,,,,z%A..1ae<<<rA   c                 d    |j         d}}| D ]#}|                    ||z            r|dz  }|r|}$|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   prevrX   rW   s        r?   dup_sign_variationsr   w  sR     fa!D  ==t$$ 	FA 	DHrA   NFc                 *   ||j         r|                                }n|}|j        }| D ]+}|                    ||                    |                    },|                    |          st          | ||          } |s|| fS |t          | ||          fS )a@  
    Clear denominators, i.e. transform ``K_0`` to ``K_1``.

    Examples
    ========

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

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

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

    )has_assoc_Ringget_ringr   lcmdenomr   r   r   )r7   K0r   rg   commonr<   s         r?   dup_clear_denomsr     s    $ 
  	BBBVF - -,,99V *1fb)) .qy{1b"----rA   c           
          |j         }|s/| D ]+}|                    ||                    |                    },n0|dz
  }| D ](}|                    |t          ||||                    })|S )z.Recursive helper for :func:`dmp_clear_denoms`.r0   )r   r   r   _rec_clear_denoms)r:   rD   r   r   r   r<   rL   s          r?   r   r     s    VF E 	1 	1AVVFBHHQKK00FF	1 E 	E 	EAVVF$5aB$C$CDDFFMrA   c                    |st          | |||          S ||j        r|                                }n|}t          | |||          }|                    |          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)

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

    r   r0   )revertr"   r   r1   int_ceil_logr4   r   r
   r   r   r   r   r   )	r7   r=   r9   r:   rx   Nr;   rh   r   s	            r?   
dup_revertr     s    ( 
&A,,		 A	
AE$q!**A1a!e__ , ,1aaddA&&Awq!}}a((GAq!$$a++q*Q--++HrA   c                 H    |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     s.      0!Q""")!Q///rA   )NF)a__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   rI   rR   rY   r[   r^   rd   rj   rl   ro   rq   ru   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   rH   rA   r?   <module>r     s   N N                                                                                            ' & & & & & + + + + + + + +  @     FL L L/ / /,( ( (V, , ,^G G G* * *0  4  :G G G* * *0
1 
1 
1( ( (@L L L/ / /4  <@ @ @(E E E0* * *:!- !- !-H' ' 'T* * *Z0 0 0B!3 !3 !3H  4  4, , ,^  ,  ,  .  >  :  :# # #8# # #   &/ / /d-= -= -=`  4#. #. #. #.L   #1 #1 #1 #1L  D0 0 0 0 0rA   