U
    ZÊ¹j°j  ã                   @   sT  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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,„ ZFd-d.„ ZGd/d0„ ZHd1d2„ ZId3d4„ ZJd5d6„ ZKd7d8„ ZLd9d:„ ZMd;d<„ ZNd=d>„ ZOd?d@„ ZPdAdB„ ZQdCdD„ ZRdEdF„ ZSdGdH„ ZTdIdJ„ ZUdKdL„ ZVdMdN„ ZWdOdP„ ZXdQdR„ ZYdSdT„ ZZdUdV„ Z[dWdX„ Z\ded[d\„Z]d]d^„ Z^dfd_d`„Z_dadb„ Z`dcdd„ ZadYS )gzHAdvanced 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              	   C   sv   |dks| s| S |j g| }tt| ƒƒD ]H\}}|d }td|ƒD ]}||| d 9 }qB| 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ÚinsertZexquo)ÚfÚmÚKÚgÚiÚcÚnÚj© r<   úU/var/www/html/bwave/python/venv/lib/python3.8/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 ]J\}}|d }td|ƒD ]}	|||	 d 9 }qf| dt|||ƒ||ƒ¡ qL|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(   r0   r1   r2   r3   r   )
r4   r5   Úur6   r7   Úvr8   r9   r:   r;   r<   r<   r=   Údmp_integrateG   s    rA   c                    sH   ˆˆkrt | ˆ|ˆ ƒS |d ˆd  ‰‰t‡ ‡‡‡‡fdd„| D ƒ|ƒS )z.Recursive helper for :func:`dmp_integrate_in`.r.   c              	      s   g | ]}t |ˆˆˆˆˆ ƒ‘qS r<   )Ú_rec_integrate_in©Ú.0r9   ©r6   r8   r;   r5   Úwr<   r=   Ú
<listcomp>q   s     z%_rec_integrate_in.<locals>.<listcomp>)rA   r   ©r7   r5   r@   r8   r;   r6   r<   rE   r=   rB   j   s    rB   c                 C   s2   |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)Ú
IndexErrorrB   ©r4   r5   r;   r?   r6   r<   r<   r=   Údmp_integrate_int   s    rK   c                 C   s¸   |dkr| S t | ƒ}||k r g S g }|dkr\| d| … D ]}| ||ƒ| ¡ |d8 }q:nT| d| … D ]D}|}t|d || dƒD ]}||9 }q†| ||ƒ| ¡ |d8 }qjt|ƒ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   Úappendr2   r   )r4   r5   r6   r:   ÚderivÚcoeffÚkr8   r<   r<   r=   Údup_diffŠ   s"    

rQ   c           
      C   sæ   |st | ||ƒS |dkr| S t| |ƒ}||k r6t|ƒS g |d  }}|dkr‚| d| … D ]$}| t|||ƒ||ƒ¡ |d8 }qZnZ| d| … D ]J}|}t|d || dƒD ]}	||	9 }q¬| t|||ƒ||ƒ¡ |d8 }q�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   r.   NrL   )rQ   r   r#   rM   r   r2   r   )
r4   r5   r?   r6   r:   rN   r@   rO   rP   r8   r<   r<   r=   Údmp_diffµ   s&    


rR   c                    sH   ˆˆkrt | ˆ|ˆ ƒS |d ˆd  ‰‰t‡ ‡‡‡‡fdd„| D ƒ|ƒS )z)Recursive helper for :func:`dmp_diff_in`.r.   c              	      s   g | ]}t |ˆˆˆˆˆ ƒ‘qS r<   )Ú_rec_diff_inrC   rE   r<   r=   rG   ë   s     z _rec_diff_in.<locals>.<listcomp>)rR   r   rH   r<   rE   r=   rS   ä   s    rS   c                 C   s2   |dk s||kr t d||f ƒ‚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 <= %s expected, got %s)rI   rS   rJ   r<   r<   r=   Údmp_diff_inî   s    rU   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    
rY   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)rY   r"   r   r   r   )r4   rW   r?   r6   rX   r@   rO   r<   r<   r=   Údmp_eval   s    
rZ   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_inrC   ©r6   rW   r8   r;   r@   r<   r=   rG   D  s     z _rec_eval_in.<locals>.<listcomp>)rZ   r   )r7   rW   r@   r8   r;   r6   r<   r\   r=   r[   =  s    r[   c                 C   s2   |dk s||kr t d||f ƒ‚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   rT   )rI   r[   )r4   rW   r;   r?   r6   r<   r<   r=   Údmp_eval_inG  s    r]   c                    sf   ˆˆkrt | ˆ d ˆƒS ‡ ‡‡‡fdd„| D ƒ}ˆˆtˆ ƒ d k rH|S t |ˆ ˆ ˆ d  ˆƒS dS )z+Recursive helper for :func:`dmp_eval_tail`.rL   c                    s    g | ]}t |ˆd  ˆ ˆˆƒ‘qS ©r.   )Ú_rec_eval_tailrC   ©ÚAr6   r8   r?   r<   r=   rG   d  s     z"_rec_eval_tail.<locals>.<listcomp>r.   N)rY   Úlen)r7   r8   ra   r?   r6   Úhr<   r`   r=   r_   _  s    r_   c                 C   s\   |s| S t | |ƒr"t|t|ƒ ƒS t| d|||ƒ}|t|ƒd krF|S t||t|ƒ ƒS d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.   N)r%   r#   rb   r_   r   )r4   ra   r?   r6   Úer<   r<   r=   Údmp_eval_taill  s    
re   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_evalrC   ©r6   rW   r8   r;   r5   r@   r<   r=   rG   “  s     z"_rec_diff_eval.<locals>.<listcomp>)rZ   rR   r   )r7   r5   rW   r@   r8   r;   r6   r<   rg   r=   rf   Œ  s    rf   c                 C   sJ   ||krt d|||f ƒ‚|s6t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   )rI   rZ   rR   rf   )r4   r5   rW   r;   r?   r6   r<   r<   r=   Údmp_diff_eval_in–  s
    rh   c                    s‚   ˆ j rDg }| D ]2}|ˆ }|ˆd kr6| |ˆ ¡ q| |¡ qn6ˆ jrhtˆƒ‰‡ ‡fdd„| D ƒ}n‡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<   )ÚintrC   )r6   Úpir<   r=   rG   Ë  s     zdup_trunc.<locals>.<listcomp>c                    s   g | ]}|ˆ  ‘qS r<   r<   rC   )Úpr<   r=   rG   Í  s     )Úis_ZZrM   Zis_FiniteFieldrj   r   )r4   rl   r6   r7   r9   r<   )r6   rl   rk   r=   Ú	dup_trunc°  s    rn   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                    s   g | ]}t |ˆˆd  ˆ ƒ‘qS r^   )r   rC   ©r6   rl   r?   r<   r=   rG   ã  s     zdmp_trunc.<locals>.<listcomp>)r   ©r4   rl   r?   r6   r<   ro   r=   Ú	dmp_truncÒ  s    rq   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_truncrC   ©r6   rl   r@   r<   r=   rG   û  s     z$dmp_ground_trunc.<locals>.<listcomp>)rn   r   rp   r<   rs   r=   rr   æ  s    rr   c                 C   s0   | s| S t | |ƒ}| |¡r | S t| ||ƒS d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

    N)r   Úis_oner   )r4   r6   Úlcr<   r<   r=   Ú	dup_monicþ  s    

rv   c                 C   sH   |st | |ƒS t| |ƒr| S t| ||ƒ}| |¡r6| S t| |||ƒS d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

    N)rv   r%   r    rt   r   )r4   r?   r6   ru   r<   r<   r=   Údmp_ground_monic  s    


rw   c                 C   sd   ddl m} | s|jS |j}||kr<| D ]}| ||¡}q(n$| D ]}| ||¡}| |¡r@ q`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.domainsry   r/   Úgcdrt   )r4   r6   ry   Ú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rb| D ]}| |t|||ƒ¡}qFn,| D ]&}| |t|||ƒ¡}| |¡rf qŽqf|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   rx   r.   )rz   ry   r}   r%   r/   r{   Údmp_ground_contentrt   )r4   r?   r6   ry   r|   r@   r9   r<   r<   r=   r~   i  s    


r~   c                 C   s>   | s|j | fS t| |ƒ}| |¡r*|| fS |t| ||ƒfS d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)

    N)r/   r}   rt   r   )r4   r6   r|   r<   r<   r=   Údup_primitive–  s    


r   c                 C   sV   |st | |ƒS t| |ƒr"|j| fS t| ||ƒ}| |¡r@|| fS |t| |||ƒfS d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)

    N)r   r%   r/   r~   rt   r   )r4   r?   r6   r|   r<   r<   r=   Údmp_ground_primitive·  s    



r€   c                 C   sL   t | |ƒ}t ||ƒ}| ||¡}| |¡sBt| ||ƒ} 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{   rt   r   )r4   r7   r6   ÚfcÚgcr{   r<   r<   r=   Údup_extractÛ  s    


rƒ   c                 C   sT   t | ||ƒ}t |||ƒ}| ||¡}| |¡sJt| |||ƒ} 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{   rt   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ƒ}| s4||fS |j|jgg|jgg gg}t| d dƒ}| dd… D ](}t||d|ƒ}t|t|dƒdd|ƒ}qht	|ƒ}| 
¡ D ]b\}}|d }	|	sÆt||d|ƒ}q¢|	dkrÞt||d|ƒ}q¢|	dkröt||d|ƒ}q¢t||d|ƒ}q¢||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   ri   Né   )rm   Zis_QQr*   r#   Úoner/   r$   r
   r   r&   Úitemsr   r   )
r4   r6   Úf1Úf2r7   rc   r9   ÚHrP   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

    ri   rL   éþÿÿÿ)Úlistr2   rb   )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.   rL   ©r�   rb   r2   )r4   rW   r6   r:   Úbr8   r<   r<   r=   Ú	dup_scaleY  s    r‘   c                 C   sX   t | ƒt| ƒd  } }t|ddƒD ]0}td|ƒD ] }| |d   || |  7  < q0q"| 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   rL   r�   )r4   rW   r6   r:   r8   r;   r<   r<   r=   Ú	dup_shifto  s
     r’   c           	         s¸   ˆst | |d ˆ ƒS t| ˆƒr"| S |d |dd…  }‰‡ ‡‡fdd„| D ƒ} t| ƒd }t|ddƒD ]L}td|ƒD ]<}t| | |ˆd ˆ ƒ}t| |d  |ˆd ˆ ƒ| |d < qtqf| 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                    s   g | ]}t |ˆˆd  ˆ ƒ‘qS r^   )Ú	dmp_shiftrC   ©r6   Za1r?   r<   r=   rG   ¡  s     zdmp_shift.<locals>.<listcomp>rL   )r’   r%   rb   r2   r   r   )	r4   rW   r?   r6   Za0r:   r8   r;   Zafjr<   r”   r=   r“   †  s    
&r“   c           	      C   sœ   | sg S t | ƒd }| d g|jgg }}td|ƒD ]}| t|d ||ƒ¡ q4t| dd… |dd… ƒD ],\}}t|||ƒ}t|||ƒ}t|||ƒ}qj|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   rL   N)rb   r†   r2   rM   r	   Úzipr   r   )	r4   rl   Úqr6   r:   rc   Ú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|ƒ}qB|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)rb   r   rY   r   r	   r   )r4   r7   r6   rc   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||ƒ}q4|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   rc   r9   r<   r<   r=   Údmp_composeè  s    

rš   c                 C   sÒ   t | ƒd }t| |ƒ}t| ƒ} ||ji}|| }td|ƒD ]Œ}|j}td|ƒD ]Z}	||	 | | krdqN||	 |krrqN| ||	 |  |||	   }
}||||	  |
 | 7 }qN| ||| | ¡||| < q:t||ƒS )ú+Helper function for :func:`_dup_decompose`.r.   r   )rb   r   r&   r†   r2   r/   Úquor'   )r4   Úsr6   r:   ru   r7   Úrr8   rO   r;   r�   r‚   r<   r<   r=   Ú_dup_right_decompose  s     

rŸ   c                 C   sV   i d }}| rLt | ||ƒ\}}t|ƒdkr.dS t||ƒ||< ||d  } }q
t||ƒS )r›   r   Nr.   )r   r   r   r'   )r4   rc   r6   r7   r8   r–   rž   r<   r<   r=   Ú_dup_left_decompose!  s    
r    c                 C   sb   t | ƒd }td|ƒD ]F}|| dkr(qt| ||ƒ}|dk	rt| ||ƒ}|dk	r||f  S qdS )z*Helper function for :func:`dup_decompose`.r.   ri   r   N)rb   r2   rŸ   r    )r4   r6   Zdfr�   rc   r7   r<   r<   r=   Ú_dup_decompose1  s    r¡   c                 C   s8   g }t | |ƒ}|dk	r.|\} }|g| }qq.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]_

    N)r¡   )r4   r6   ÚFrX   rc   r<   r<   r=   Údup_decomposeD  s    $
r£   c                 C   sâ   |j r | ¡ }t| |||ƒ} |}|js.tdƒ‚t| |ƒg g   }}}| ¡ D ]\}}|jsL| |¡ qLt	ddgt
|ƒdd�}	|	D ]H}
t|ƒ}t|
|ƒD ]\}}|dkr–||  ||< q–| t|||ƒ¡ q€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 domainrL   r.   T)Z
repetition)Zis_GaussianFieldZas_AlgebraicFieldr   Zis_Algebraicr*   r   r‡   Z	is_groundrM   r+   rb   Údictr•   r   r   Údom)r4   r?   r6   ÚK1r¢   ZmonomsZpolysZmonomrO   ZpermsÚpermÚGÚsignr<   r<   r=   Údmp_liftv  s(    ÿrª   c                 C   s8   |j d }}| D ]"}| || ¡r*|d7 }|r|}q|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   r.   )r/   Zis_negative)r4   r6   ÚprevrP   rO   r<   r<   r=   Údup_sign_variations¦  s    r¬   NFc                    sš   ˆdkrˆ j rˆ  ¡ ‰nˆ ‰ˆj‰| D ]}ˆ ˆˆ  |¡¡‰q&ˆ ˆ¡rd|sTˆ| fS ˆt| ˆ ˆƒfS ‡ ‡‡fdd„| D ƒ} |sŽˆt| ˆˆ ƒfS ˆ| fS d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<   )Znumerrœ   ÚdenomrC   ©ÚK0r¦   Úcommonr<   r=   rG   å  s     z$dup_clear_denoms.<locals>.<listcomp>)Úhas_assoc_RingÚget_ringr†   Úlcmr­   rt   r   )r4   r¯   r¦   rV   r9   r<   r®   r=   Údup_clear_denomsÀ  s    

r´   c              	   C   sT   |j }|s(| D ]}| || |¡¡}qn(|d }| D ]}| |t||||ƒ¡}q4|S )z.Recursive helper for :func:`dmp_clear_denoms`.r.   )r†   r³   r­   Ú_rec_clear_denoms)r7   r@   r¯   r¦   r°   r9   rF   r<   r<   r=   rµ   í  s    rµ   c                 C   sx   |st | |||d�S |dkr0|jr,| ¡ }n|}t| |||ƒ}| |¡sVt| |||ƒ} |sb|| fS |t| |||ƒfS d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)

    )rV   N)r´   r±   r²   rµ   rt   r   r   )r4   r?   r¯   r¦   rV   r°   r<   r<   r=   Údmp_clear_denomsý  s    

r¶   c           	      C   s�   |  t| |ƒ¡g}|j|j|jg}ttt|ƒƒƒ}td|d ƒD ]J}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.   ri   )Úrevertr!   r†   r/   rj   Ú_ceilÚ_log2r2   r   r	   r   r   r   r   r   )	r4   r:   r6   r7   rc   ÚNr8   rW   r�   r<   r<   r=   Ú
dup_revert#  s    r»   c                 C   s   |st | ||ƒS t| |ƒ‚dS )z©
    Compute ``f**(-1)`` mod ``x**n`` using Newton iteration.

    Examples
    ========

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

    N)r»   r)   )r4   r7   r?   r6   r<   r<   r=   Ú
dmp_revertE  s    r¼   )NF)NF)bÚ__doc__Zsympy.polys.densearithr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Zsympy.polys.densebasicr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   Zsympy.polys.polyerrorsr)   r*   Zsympy.utilitiesr+   Úmathr,   r¸   r-   r¹   r>   rA   rB   rK   rQ   rR   rS   rU   rY   rZ   r[   r]   r_   re   rf   rh   rn   rq   rr   rv   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=   Ú<module>   sf   XT #
+/

 
"$*-!$4&20
-
&"