o
    GZh2t                     @   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insertZexquo)fmKgicnj r;   E/var/www/auris/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   )
r3   r4   ur5   r6   vr7   r8   r9   r:   r;   r;   r<   dmp_integrateG   s   r@   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.0r8   r5   r7   r:   r4   wr;   r<   
<listcomp>q       z%_rec_integrate_in.<locals>.<listcomp>)r@   r   r6   r4   r?   r7   r:   r5   r;   rF   r<   rC   j       rC   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)
IndexErrorrC   r3   r4   r:   r>   r5   r;   r;   r<   dmp_integrate_int   s   rO   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   )r3   r4   r5   r9   derivcoeffkr7   r;   r;   r<   dup_diff   s$   


rU   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-   NrP   )rU   r   r"   rQ   r   r1   r   )
r3   r4   r>   r5   r9   rR   r?   rS   rT   r7   r;   r;   r<   dmp_diff   s(   




rV   c                    rA   )z)Recursive helper for :func:`dmp_diff_in`.r-   c              	      rB   r;   )_rec_diff_inrD   rF   r;   r<   rH      rI   z _rec_diff_in.<locals>.<listcomp>)rV   r   rJ   r;   rF   r<   rW      rK   rW   c                 C   rL   )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)rM   rW   rN   r;   r;   r<   dmp_diff_in      rY   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.   )r3   ar5   resultr8   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   )r3   r\   r>   r5   r]   r?   rS   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_inrD   r5   r\   r7   r:   r?   r;   r<   rH   D  rI   z _rec_eval_in.<locals>.<listcomp>)r_   r   )r6   r\   r?   r7   r:   r5   r;   ra   r<   r`   =  rK   r`   c                 C   rL   )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   rX   )rM   r`   )r3   r\   r:   r>   r5   r;   r;   r<   dmp_eval_inG  rZ   rb   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`.rP   c                    s    g | ]}t |d   qS r-   )_rec_eval_tailrD   Ar5   r7   r>   r;   r<   rH   d       z"_rec_eval_tail.<locals>.<listcomp>r-   )r^   len)r6   r7   rf   r>   r5   hr;   re   r<   rd   _  s   rd   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"   rh   rd   r   )r3   rf   r>   r5   er;   r;   r<   dmp_eval_taill  s   
rk   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_evalrD   r5   r\   r7   r:   r4   r?   r;   r<   rH     rg   z"_rec_diff_eval.<locals>.<listcomp>)r_   rV   r   )r6   r4   r\   r?   r7   r:   r5   r;   rm   r<   rl     s   "rl   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   )rM   r_   rV   rl   )r3   r4   r\   r:   r>   r5   r;   r;   r<   dmp_diff_eval_in  s
   rn   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;   )intrD   )r5   pir;   r<   rH     s    zdup_trunc.<locals>.<listcomp>c                    s   g | ]}|  qS r;   r;   rD   )pr;   r<   rH     s    )is_ZZrQ   Zis_FiniteFieldrp   r   )r3   rr   r5   r6   r8   r;   )r5   rr   rq   r<   	dup_trunc  s   rt   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 rc   )r   rD   r5   rr   r>   r;   r<   rH     rI   zdmp_trunc.<locals>.<listcomp>)r   r3   rr   r>   r5   r;   rv   r<   	dmp_trunc  s   rx   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_truncrD   r5   rr   r?   r;   r<   rH     s    z$dmp_ground_trunc.<locals>.<listcomp>)rt   r   rw   r;   rz   r<   ry     s   ry   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   )r3   r5   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   )r3   r>   r5   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{   )r3   r5   r   contr8   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{   )r3   r>   r5   r   r   r?   r8   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   )r3   r5   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   )r3   r>   r5   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   )r3   r6   r5   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   )r3   r6   r>   r5   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   ro   N   )rs   is_QQr*   r"   oner.   r#   r
   r   r%   itemsr   r   )
r3   r5   f1f2r6   ri   r8   HrT   r4   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

    ro   rP   )listr1   rh   )r3   r5   r7   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-   rP   r   rh   r1   )r3   r\   r5   r9   br7   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   rP   r   )r3   r\   r5   r9   r7   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                    ru   rc   )	dmp_shiftrD   r5   Za1r>   r;   r<   rH     rI   zdmp_shift.<locals>.<listcomp>rP   )	r   r$   anyr   rh   r1   r   r   r   )	r3   r\   r>   r5   Za0r9   r7   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 || 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   rP   N)rh   r   r1   rQ   r	   zipr   r   )	r3   rr   qr5   r9   ri   Qr7   r8   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)rh   r   r^   r   r	   r   )r3   r6   r5   ri   r8   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   )r3   r6   r>   r5   ri   r8   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   )rh   r   r%   r   r1   r.   quor&   )r3   sr5   r9   r|   r6   rr7   rS   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&   )r3   ri   r5   r6   r7   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-   ro   r   N)rh   r1   r   r   )r3   r5   Zdfr   ri   r6   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   )r3   r5   Fr]   ri   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)Zis_GaussianRingZis_GaussianField_dmp_alg_inject_gaussianZis_Algebraic_dmp_alg_inject_algr*   )r3   r>   r5   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   rc   r-   )r   r   xyr   dom)	r3   r>   r5   ri   f_monomr6   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   )	r3   r>   r5   ri   r   r6   Zg_monomr8   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   )Zeuclidtoolsr   r   modZto_listr(   r   r1   )	r3   r>   r5   r   r   r?   ZK2Zp_aZ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   )boolZto_sympy)r\   r5   r;   r<   is_negative_sympy  s   z.dup_sign_variations.<locals>.is_negative_sympyr-   r   z-sign variation counting not supported over %s)rs   r   Zis_RRis_negativeZis_AlgebraicFieldextZis_comparableZis_PolynomialRingZis_FractionFieldrh   symbolsr   Zis_transcendentalr*   r.   )r3   r5   r   r   prevrT   rS   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;   )Znumerr   denomrD   K0K1commonr;   r<   rH   0  s   ( z$dup_clear_denoms.<locals>.<listcomp>)has_assoc_Ringget_ringr   lcmr   r{   r   )r3   r   r   r[   r8   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)r6   r?   r   r   r   r8   rG   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   )r3   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-   ro   )revertr    r   r.   rp   _ceil_log2r1   r   r	   r   r   r   r   r   )	r3   r9   r5   r6   ri   Nr7   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)   )r3   r6   r>   r5   r;   r;   r<   
dmp_revert  s   
r   )NF)c__doc__Zsympy.polys.densearithr   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'   r(   Zsympy.polys.polyerrorsr)   r*   mathr+   r   r,   r   r=   r@   rC   rO   rU   rV   rW   rY   r^   r_   r`   rb   rd   rk   rl   rn   rt   rx   ry   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-
&"