o
    §ƒðj2t  ã                   @   s`  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 ddlmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+m,Z, ddl-m.Z/m0Z1 dd„ Z2dd	„ Z3d
d„ Z4dd„ Z5dd„ Z6dd„ Z7dd„ Z8dd„ Z9dd„ Z:dd„ Z;dd„ Z<dd„ Z=dd„ Z>d d!„ Z?d"d#„ Z@d$d%„ ZAd&d'„ ZBd(d)„ ZCd*d+„ ZDd,d-„ ZEd.d/„ ZFd0d1„ ZGd2d3„ ZHd4d5„ ZId6d7„ ZJd8d9„ ZKd:d;„ ZLd<d=„ ZMd>d?„ ZNd@dA„ ZOdBdC„ ZPdDdE„ ZQdFdG„ ZRdHdI„ ZSdJdK„ ZTdLdM„ ZUdNdO„ ZVdPdQ„ ZWdRdS„ ZXdTdU„ ZYdVdW„ ZZdXdY„ Z[dZd[„ Z\d\d]„ Z]djd`da„Z^dbdc„ Z_djddde„Z`dfdg„ Zadhdi„ Zbd^S )kzHAdvanced 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Ú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Údmp_include)ÚMultivariatePolynomialErrorÚDomainError)ÚceilÚlog2c              	   C   sv   |dks| s| S |j g| }tt| ƒƒD ]$\}}|d }td|ƒD ]
}||| d 9 }q!| d| |||ƒ¡¡ q|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Új© r<   ú˜/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/polys/densetools.pyÚdup_integrate'   s   r>   c           
   	   C   sœ   |st | ||ƒS |dkst| |ƒr| S t||d |ƒ|d }}tt| ƒƒD ]%\}}|d }td|ƒD ]
}	|||	 d 9 }q3| dt|||ƒ||ƒ¡ q&|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   r-   )r>   r$   r'   r/   r0   r1   r2   r   )
r4   r5   Úur6   r7   Úvr8   r9   r:   r;   r<   r<   r=   Údmp_integrateG   s   rA   c                    óH   ˆˆkrt | ˆ|ˆ ƒS |d ˆd ‰‰t‡ ‡‡‡‡fdd„| D ƒ|ƒS )z.Recursive helper for :func:`dmp_integrate_in`.r-   c              	      ó   g | ]}t |ˆˆˆˆˆ ƒ‘qS r<   )Ú_rec_integrate_in©Ú.0r9   ©r6   r8   r;   r5   Úwr<   r=   Ú
<listcomp>q   ó    z%_rec_integrate_in.<locals>.<listcomp>)rA   r   ©r7   r5   r@   r8   r;   r6   r<   rG   r=   rD   j   ó    rD   c                 C   ó2   |dk s||krt d||f ƒ‚t| ||d||ƒS )a+  
    Computes the indefinite integral of ``f`` in ``x_j`` in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_integrate_in(x + 2*y, 1, 0)
    1/2*x**2 + 2*x*y
    >>> R.dmp_integrate_in(x + 2*y, 1, 1)
    x*y + y**2

    r   z(0 <= j <= u expected, got u = %d, j = %d)Ú
IndexErrorrD   ©r4   r5   r;   r?   r6   r<   r<   r=   Údmp_integrate_int   s   rP   c                 C   s¾   |dkr| S t | ƒ}||k rg S g }|dkr1| d| … D ]}| ||ƒ| ¡ |d8 }qt|ƒS | d| … D ]"}|}t|d || dƒD ]}||9 }qF| ||ƒ| ¡ |d8 }q8t|ƒ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   r-   Néÿÿÿÿ)r   Úappendr1   r   )r4   r5   r6   r:   ÚderivÚcoeffÚkr8   r<   r<   r=   Údup_diffŠ   s$   
÷

rV   c           
      C   sæ   |st | ||ƒS |dkr| S t| |ƒ}||k rt|ƒS g |d }}|dkrA| d| … D ]}| t|||ƒ||ƒ¡ |d8 }q-n-| d| … D ]%}|}t|d || dƒD ]}	||	9 }qV| t|||ƒ||ƒ¡ |d8 }qHt||ƒ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   r-   NrQ   )rV   r   r"   rR   r   r1   r   )
r4   r5   r?   r6   r:   rS   r@   rT   rU   r8   r<   r<   r=   Údmp_diffµ   s(   

þ


rW   c                    rB   )z)Recursive helper for :func:`dmp_diff_in`.r-   c              	      rC   r<   )Ú_rec_diff_inrE   rG   r<   r=   rI   ë   rJ   z _rec_diff_in.<locals>.<listcomp>)rW   r   rK   r<   rG   r=   rX   ä   rL   rX   c                 C   rM   )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 <= %s expected, got %s)rN   rX   rO   r<   r<   r=   Údmp_diff_inî   ó   rZ   c                 C   s8   |s
|  t| |ƒ¡S |j}| D ]
}||9 }||7 }q|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    r.   )r4   Úar6   Úresultr9   r<   r<   r=   Údup_eval  s   
r_   c                 C   sd   |st | ||ƒS |st| |ƒS t| |ƒ|d }}| dd… D ]}t||||ƒ}t||||ƒ}q|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

    r-   N)r_   r!   r   r   r   )r4   r]   r?   r6   r^   r@   rT   r<   r<   r=   Údmp_eval   s   
r`   c                    sH   ˆˆkrt | ˆˆˆ ƒS ˆd ˆd ‰‰t‡ ‡‡‡‡fdd„| D ƒˆƒS )z)Recursive helper for :func:`dmp_eval_in`.r-   c              	      s   g | ]}t |ˆˆˆˆˆ ƒ‘qS r<   )Ú_rec_eval_inrE   ©r6   r]   r8   r;   r@   r<   r=   rI   D  rJ   z _rec_eval_in.<locals>.<listcomp>)r`   r   )r7   r]   r@   r8   r;   r6   r<   rb   r=   ra   =  rL   ra   c                 C   rM   )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   rY   )rN   ra   )r4   r]   r;   r?   r6   r<   r<   r=   Údmp_eval_inG  r[   rc   c                    sb   ˆˆkrt | ˆ d ˆƒS ‡ ‡‡‡fdd„| D ƒ}ˆˆtˆ ƒ d k r$|S t |ˆ ˆ ˆ d  ˆƒS )z+Recursive helper for :func:`dmp_eval_tail`.rQ   c                    s    g | ]}t |ˆd  ˆ ˆˆƒ‘qS ©r-   )Ú_rec_eval_tailrE   ©ÚAr6   r8   r?   r<   r=   rI   d  ó     z"_rec_eval_tail.<locals>.<listcomp>r-   )r_   Úlen)r7   r8   rg   r?   r6   Úhr<   rf   r=   re   _  s   re   c                 C   sX   |s| S t | |ƒrt|t|ƒ ƒS t| d|||ƒ}|t|ƒd kr#|S t||t|ƒ ƒ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   r-   )r$   r"   ri   re   r   )r4   rg   r?   r6   Úer<   r<   r=   Údmp_eval_taill  s   
rl   c                    sT   ˆˆkrt t| ˆˆˆ ƒˆˆˆ ƒS ˆd ˆd ‰‰t‡ ‡‡‡‡‡fdd„| D ƒˆƒS )z+Recursive helper for :func:`dmp_diff_eval`.r-   c              
      s    g | ]}t |ˆˆˆˆˆˆ ƒ‘qS r<   )Ú_rec_diff_evalrE   ©r6   r]   r8   r;   r5   r@   r<   r=   rI   “  rh   z"_rec_diff_eval.<locals>.<listcomp>)r`   rW   r   )r7   r5   r]   r@   r8   r;   r6   r<   rn   r=   rm   Œ  s   "rm   c                 C   sJ   ||krt d|||f ƒ‚|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-%s <= j < %s expected, got %sr   )rN   r`   rW   rm   )r4   r5   r]   r;   r?   r6   r<   r<   r=   Údmp_diff_eval_in–  s
   ro   c                    sŽ   ˆ j r%g }| D ]}|ˆ }|ˆd kr| |ˆ ¡ q| |¡ qt|ƒS ˆ jr:tˆƒ‰‡ ‡fdd„| D ƒ}t|ƒS ‡fdd„| D ƒ}t|ƒS )zô
    Reduce a ``K[x]`` polynomial modulo a constant ``p`` in ``K``.

    Examples
    ========

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

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

    é   c                    s   g | ]
}ˆ t |ƒˆ ƒ‘qS r<   )ÚintrE   )r6   Úpir<   r=   rI   Ë  s    zdup_trunc.<locals>.<listcomp>c                    s   g | ]}|ˆ  ‘qS r<   r<   rE   )Úpr<   r=   rI   Í  s    )Úis_ZZrR   Úis_FiniteFieldrq   r   )r4   rs   r6   r7   r9   r<   )r6   rs   rr   r=   Ú	dup_trunc°  s   ùþrv   c                    s   t ‡ ‡‡fdd„| D ƒˆƒS )a9  
    Reduce a ``K[X]`` polynomial modulo a polynomial ``p`` in ``K[Y]``.

    Examples
    ========

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

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

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

    c                    ó   g | ]}t |ˆˆd  ˆ ƒ‘qS rd   )r   rE   ©r6   rs   r?   r<   r=   rI   ã  rJ   zdmp_trunc.<locals>.<listcomp>)r   ©r4   rs   r?   r6   r<   rx   r=   Ú	dmp_truncÒ  s   rz   c                    s4   |st | ˆˆ ƒS |d ‰t‡ ‡‡fdd„| D ƒ|ƒS )a   
    Reduce a ``K[X]`` polynomial modulo a constant ``p`` in ``K``.

    Examples
    ========

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

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

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

    r-   c                    s   g | ]	}t |ˆˆˆ ƒ‘qS r<   )Údmp_ground_truncrE   ©r6   rs   r@   r<   r=   rI   û  s    z$dmp_ground_trunc.<locals>.<listcomp>)rv   r   ry   r<   r|   r=   r{   æ  s   r{   c                 C   s,   | 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   )r4   r6   Úlcr<   r<   r=   Ú	dup_monicþ  s   

r   c                 C   sD   |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   )r4   r?   r6   r~   r<   r<   r=   Údmp_ground_monic  s   


r€   c                 C   sh   ddl m} | s|jS |j}||kr| D ]}| ||¡}q|S | D ]}| ||¡}| |¡r1 |S q!|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‚   r.   Úgcdr}   )r4   r6   r‚   Úcontr9   r<   r<   r=   Údup_content?  s   ú
ýr†   c                 C   s–   ddl m} |st| |ƒS t| |ƒr|jS |j|d }}||kr2| D ]}| |t|||ƒ¡}q#|S | D ]}| |t|||ƒ¡}| |¡rH |S q4|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�   r-   )rƒ   r‚   r†   r$   r.   r„   Údmp_ground_contentr}   )r4   r?   r6   r‚   r…   r@   r9   r<   r<   r=   r‡   i  s"   

ú
ýr‡   c                 C   s:   | 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)

    )r.   r†   r}   r   )r4   r6   r…   r<   r<   r=   Údup_primitive–  s   


rˆ   c                 C   sR   |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$   r.   r‡   r}   r   )r4   r?   r6   r…   r<   r<   r=   Údmp_ground_primitive·  s   



r‰   c                 C   sL   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   )r4   r7   r6   ÚfcÚgcr„   r<   r<   r=   Údup_extractÛ  s   



rŒ   c                 C   sT   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   )r4   r7   r?   r6   rŠ   r‹   r„   r<   r<   r=   Údmp_ground_extractõ  s   

r�   c           
      C   s  |j s|jstd| ƒ‚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|ƒ}q4t	|ƒ}| 
¡ D ]1\}}|d }	|	sct||d|ƒ}qQ|	dkrot||d|ƒ}qQ|	dkr{t||d|ƒ}qQt||d|ƒ}qQ||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 %sr-   r   rp   Né   )rt   Úis_QQr*   r"   Úoner.   r#   r
   r   r%   Úitemsr   r   )
r4   r6   Úf1Úf2r7   rj   r9   ÚHrU   r5   r<   r<   r=   Údup_real_imag  s,   r•   c                 C   s4   t | ƒ} tt| ƒd ddƒD ]	}| |  | |< q| 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

    rp   rQ   éþÿÿÿ)Úlistr1   ri   )r4   r6   r8   r<   r<   r=   Ú
dup_mirrorC  s   r˜   c                 C   sP   t | ƒt| ƒd |} }}t|d ddƒD ]}|| |  || | |< }q| 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

    r-   rQ   ©r—   ri   r1   )r4   r]   r6   r:   Úbr8   r<   r<   r=   Ú	dup_scaleY  s   r›   c                 C   sX   t | ƒt| ƒd } }t|ddƒD ]}td|ƒD ]}| |d   || |  7  < qq| 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

    r-   r   rQ   r™   )r4   r]   r6   r:   r8   r;   r<   r<   r=   Ú	dup_shifto  s   ÿrœ   c           	         sÔ   ˆs
t | |d ˆ ƒS t| ˆƒr| S |d |dd… }‰tˆƒr,‡ ‡‡fdd„| D ƒ} nt| ƒ} |ret| ƒd }t|ddƒD ]&}td|ƒD ]}t| | |ˆd ˆ ƒ}t| |d  |ˆd ˆ ƒ| |d < qEq>t| ˆƒS )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   r-   Nc                    rw   rd   )Ú	dmp_shiftrE   ©r6   Úa1r?   r<   r=   rI   ¢  rJ   zdmp_shift.<locals>.<listcomp>rQ   )	rœ   r$   Úanyr—   ri   r1   r   r   r   )	r4   r]   r?   r6   Úa0r:   r8   r;   Úafjr<   rž   r=   r�   †  s    
$þ
r�   c           	      C   sœ   | sg S t | ƒd }| d g|jgg}}td|ƒD ]}| t|d ||ƒ¡ qt| dd… |dd… ƒD ]\}}t|||ƒ}t|||ƒ}t|||ƒ}q5|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

    r-   r   rQ   N)ri   r�   r1   rR   r	   Úzipr   r   )	r4   rs   Úqr6   r:   rj   ÚQr8   r9   r<   r<   r=   Údup_transform±  s   "r¦   c                 C   sf   t |ƒdkrtt| t||ƒ|ƒgƒS | sg S | d g}| dd… D ]}t|||ƒ}t||d|ƒ}q!|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

    r-   r   N)ri   r   r_   r   r	   r   )r4   r7   r6   rj   r9   r<   r<   r=   Údup_composeÐ  s   
r§   c                 C   s\   |st | ||ƒS t| |ƒr| S | d g}| dd… D ]}t||||ƒ}t||d||ƒ}q|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   r-   N)r§   r$   r
   r   )r4   r7   r?   r6   rj   r9   r<   r<   r=   Údmp_composeí  s   

r¨   c                 C   sÒ   t | ƒd }t| |ƒ}t| ƒ} ||ji}|| }td|ƒD ]F}|j}td|ƒD ]-}	||	 | | vr2q'||	 |vr9q'| ||	 |  |||	  }
}||||	  |
 | 7 }q'| ||| | ¡||| < qt||ƒS )ú+Helper function for :func:`_dup_decompose`.r-   r   )ri   r   r%   r�   r1   r.   Úquor&   )r4   Úsr6   r:   r~   r7   Úrr8   rT   r;   rŠ   r‹   r<   r<   r=   Ú_dup_right_decompose
  s    


r­   c                 C   sX   i d}}| r't | ||ƒ\}}t|ƒdkrdS t||ƒ||< ||d } }| st||ƒS )r©   r   Nr-   )r   r   r   r&   )r4   rj   r6   r7   r8   r¤   r¬   r<   r<   r=   Ú_dup_left_decompose&  s   
ù
	r®   c                 C   sb   t | ƒd }td|ƒD ]#}|| dkrqt| ||ƒ}|dur.t| ||ƒ}|dur.||f  S qdS )z*Helper function for :func:`dup_decompose`.r-   rp   r   N)ri   r1   r­   r®   )r4   r6   Údfr«   rj   r7   r<   r<   r=   Ú_dup_decompose6  s   €r°   c                 C   s:   g }	 t | |ƒ}|dur|\} }|g| }nnq| g| 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]_

    TN)r°   )r4   r6   ÚFr^   rj   r<   r<   r=   Údup_decomposeI  s   $
ù
	r²   c                 C   s2   |j s|jrt| ||ƒS |jrt| ||ƒS tdƒ‚)a…  
    Convert polynomial from ``K(a)[X]`` to ``K[a,X]``.

    Examples
    ========

    >>> from sympy.polys.densetools import dmp_alg_inject
    >>> from sympy import QQ, sqrt

    >>> K = QQ.algebraic_field(sqrt(2))

    >>> p = [K.from_sympy(sqrt(2)), K.zero, K.one]
    >>> P, lev, dom = dmp_alg_inject(p, 0, K)
    >>> P
    [[1, 0, 0], [1]]
    >>> lev
    1
    >>> dom
    QQ

    z3computation can be done only in an algebraic domain)Úis_GaussianRingÚis_GaussianFieldÚ_dmp_alg_inject_gaussianÚis_AlgebraicÚ_dmp_alg_inject_algr*   )r4   r?   r6   r<   r<   r=   Údmp_alg_inject{  s
   r¸   c           	      C   sr   t | |ƒi } }|  ¡ D ]\}}|j|j}}|r||d| < |r'||d| < qt||d |jƒ}||d |jfS )ú+Helper function for :func:`dmp_alg_inject`.)r   rd   r-   )r   r‘   ÚxÚyr   Údom)	r4   r?   r6   rj   Úf_monomr7   rº   r»   r±   r<   r<   r=   rµ   ™  s   €rµ   c           	      C   sf   t | |ƒi } }|  ¡ D ]\}}| ¡  ¡ D ]
\}}|||| < qqt||d |jƒ}||d |jfS )r¹   r-   )r   r‘   Úto_dictr   r¼   )	r4   r?   r6   rj   r½   r7   Úg_monomr9   r±   r<   r<   r=   r·   ©  s   ÿr·   c           	      C   sR   ddl m} t| ||ƒ\}}}|j ¡ }t|ttd|d ƒƒd|ƒ}|||||ƒS )aO  
    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**4 + x**2 + 4*x + 4

    r-   )Údmp_resultantr   )ÚeuclidtoolsrÀ   r¸   ÚmodÚto_listr(   r—   r1   )	r4   r?   r6   rÀ   r±   r@   ÚK2Úp_aÚP_Ar<   r<   r=   Údmp_lift¶  s
   
rÇ   c                    sÊ   ‡ fdd„}ˆ j sˆ jsˆ jrˆ j}n7ˆ jrˆ jjr|}n-ˆ js#ˆ jrDt	ˆ j
ƒdkrDˆ jj s5ˆ jjs5ˆ jrDˆ j
d jrDˆ j
d jrD|}ntdˆ  ƒ‚ˆ jd}}| D ]}||| ƒr^|d7 }|rb|}qR|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

    c                    s   | sdS t ˆ  | ¡dk ƒS )NFr   )ÚboolÚto_sympy)r]   ©r6   r<   r=   Úis_negative_sympyâ  s   z.dup_sign_variations.<locals>.is_negative_sympyr-   r   z-sign variation counting not supported over %s)rt   r�   Úis_RRÚis_negativeÚis_AlgebraicFieldÚextÚis_comparableÚis_PolynomialRingÚis_FractionFieldri   Úsymbolsr¼   Úis_transcendentalr*   r.   )r4   r6   rË   rÍ   ÚprevrU   rT   r<   rÊ   r=   Údup_sign_variationsÔ  s4   ÿÿÿ
þ
þ€rÖ   NFc                    s–   ˆdu rˆ j rˆ  ¡ ‰nˆ ‰ˆj‰| D ]}ˆ ˆˆ  |¡¡‰qˆ ˆ¡r2|s*ˆ| fS ˆt| ˆ ˆƒfS ‡ ‡‡fdd„| D ƒ} |sGˆt| ˆˆ ƒfS ˆ| 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)

    Nc              	      s(   g | ]}ˆ   |¡ˆ ˆˆ  |¡¡ ‘qS r<   )Únumerrª   ÚdenomrE   ©ÚK0ÚK1Úcommonr<   r=   rI   0  s   ( z$dup_clear_denoms.<locals>.<listcomp>)Úhas_assoc_RingÚget_ringr�   ÚlcmrØ   r}   r   )r4   rÚ   rÛ   r\   r9   r<   rÙ   r=   Údup_clear_denoms  s   

rà   c              	   C   sV   |j }|s| D ]}| || |¡¡}q|S |d }| D ]}| |t||||ƒ¡}q|S )z.Recursive helper for :func:`dmp_clear_denoms`.r-   )r�   rß   rØ   Ú_rec_clear_denoms)r7   r@   rÚ   rÛ   rÜ   r9   rH   r<   r<   r=   rá   8  s   ûrá   c                 C   st   |s
t | |||d�S |du r|jr| ¡ }n|}t| |||ƒ}| |¡s+t| |||ƒ} |s1|| 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)

    )r\   N)rà   rÝ   rÞ   rá   r}   r   r   )r4   r?   rÚ   rÛ   r\   rÜ   r<   r<   r=   Údmp_clear_denomsH  s   

râ   c           	      C   s�   |  t| |ƒ¡g}|j|j|jg}ttt|ƒƒƒ}td|d ƒD ]%}t||dƒ|ƒ}t	| t
||ƒ|ƒ}tt|||ƒ||ƒ}t|t|ƒ|ƒ}q |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-   rp   )Úrevertr    r�   r.   rq   Ú_ceilÚ_log2r1   r   r	   r   r   r   r   r   )	r4   r:   r6   r7   rj   ÚNr8   r]   rš   r<   r<   r=   Ú
dup_revertn  s   rç   c                 C   s   |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)   )r4   r7   r?   r6   r<   r<   r=   Ú
dmp_revert�  s   
rè   )NF)cÚ__doc__Úsympy.polys.densearithr   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'   r(   Úsympy.polys.polyerrorsr)   r*   Úmathr+   rä   r,   rå   r>   rA   rD   rP   rV   rW   rX   rZ   r_   r`   ra   rc   re   rl   rm   ro   rv   rz   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>   sl    TX #
+/

 
"$*-!$4+2
7-
&"