
    Xh2t                         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 Z2d Z3d 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[d0 Z\d1 Z]d9d4Z^d5 Z_d9d6Z`d7 Zad8 Zbd2S ):zHAdvanced 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           
          |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           h/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/sympy/polys/densetools.pydup_integrater?   '   s      	AvvQv	

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   r/   )r?   r%   r(   r1   r2   r3   r4   r   )
r6   r7   ur8   r9   vr:   r;   r<   r=   s
             r>   dmp_integraterD   G   s       &Q1%%%AvvAq!!vQAq!!1q5qA(1++&& 3 31Eq! 	 	AQNAA	N1aaddAq112222Hr@   c                     k    rt          | |          S |dz
  dz   ct          fd| D             |          S )z.Recursive helper for :func:`dmp_integrate_in`.r/   c           
      8    g | ]}t          |          S  )_rec_integrate_in.0r;   r8   r:   r=   r7   ws     r>   
<listcomp>z%_rec_integrate_in.<locals>.<listcomp>q   s,    GGGq(Aq!Q::GGGr@   )rD   r   r9   r7   rC   r:   r=   r8   rK   s    ` ```@r>   rH   rH   j   sh    AvvQ1a(((q5!a%DAqGGGGGGGGAGGGKKKr@   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)
IndexErrorrH   r6   r7   r=   rB   r8   s        r>   dmp_integrate_inrQ   t   sE      	1uuACq!fLMMMQ1aA...r@   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   r/   N)r   appendr3   r   )r6   r7   r8   r<   derivcoeffkr:   s           r>   dup_diffrX      s     	Avv1A1uu	EAvvssV 	 	ELL1e$$$FAA	 ssV 	 	EA1q5!a%,,  QLL1e$$$FAAUr@   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   r/   NrS   )rX   r   r#   rT   r   r3   r   )
r6   r7   rB   r8   r<   rU   rC   rV   rW   r:   s
             r>   dmp_diffrZ      sH   $  !1a   Avv1aA1uu{{1q51EAvvssV 	 	ELLqqttQ::;;;FAA	 ssV 	 	EA1q5!a%,,  QLLqqttQ::;;;FAAUAr@   c                     k    rt          | |          S |dz
  dz   ct          fd| D             |          S )z)Recursive helper for :func:`dmp_diff_in`.r/   c           
      8    g | ]}t          |          S rG   )_rec_diff_inrI   s     r>   rL   z _rec_diff_in.<locals>.<listcomp>   +    BBB!|Aq!Q155BBBr@   )rZ   r   rM   s    ` ```@r>   r]   r]      h    Avv1a###q5!a%DAqBBBBBBBBqBBBAFFFr@   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 )rO   r]   rP   s        r>   dmp_diff_inrc      H    $ 	1uuAjAAAqqABBB1aAq)))r@   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!   r0   )r6   ar8   resultr;   s        r>   dup_evalri     sU      'yy1&&&VF  !!Mr@   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

    r/   N)ri   r"   r   r   r   )r6   rg   rB   r8   rh   rC   rV   s          r>   dmp_evalrk      s      !1a    a||q!a!eAF122 . .1a001--Mr@   c                     k    rt          |           S dz
  dz   ct          fd| D                       S )z)Recursive helper for :func:`dmp_eval_in`.r/   c           
      8    g | ]}t          |          S rG   )_rec_eval_in)rJ   r;   r8   rg   r:   r=   rC   s     r>   rL   z _rec_eval_in.<locals>.<listcomp>D  r^   r@   )rk   r   )r9   rg   rC   r:   r=   r8   s    `````r>   rn   rn   =  r_   r@   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   ra   rb   )rO   rn   )r6   rg   r=   rB   r8   s        r>   dmp_eval_inrp   G  rd   r@   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`.rS   c           	      <    g | ]}t          |d z             S r/   )_rec_eval_tail)rJ   r;   Ar8   r:   rB   s     r>   rL   z"_rec_eval_tail.<locals>.<listcomp>d  s-    <<<AnQAq!Q//<<<r@   r/   )ri   len)r9   r:   ru   rB   r8   hs    ```` r>   rt   rt   _  s    Avv1R5!$$$<<<<<<<<<<q3q66zA~HAq!a!}a000r@   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   r/   )r%   r#   rv   rt   r   )r6   ru   rB   r8   es        r>   dmp_eval_tailrz   l  s    $  !Q $CFF
###q!Q1%%ACFFQJAAJ'''r@   c                     k    r"t          t          |                     S dz
  dz   ct          fd| D                       S )z+Recursive helper for :func:`dmp_diff_eval`.r/   c                 :    g | ]}t          |          S rG   )_rec_diff_eval)rJ   r;   r8   rg   r:   r=   r7   rC   s     r>   rL   z"_rec_diff_eval.<locals>.<listcomp>  s-    GGGq~aAq!Q::GGGr@   )rk   rZ   r   )r9   r7   rg   rC   r:   r=   r8   s    ``````r>   r}   r}     sz    AvvAq!,,aA666q5!a%DAqGGGGGGGGGAGGGKKKr@   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 < rb   r   )rO   rk   rZ   r}   )r6   r7   rg   r=   rB   r8   s         r>   dmp_diff_eval_inr     sr    $ 	1uujQQQ11EFFF 7Aq!,,aA666!Q1aA...r@   c                 $   j         rDg }| D ]>}|z  }|dz  k    r|                    |z
             )|                    |           ?n4j        rt                    fd| D             }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                 F    g | ]} t          |          z            S rG   )int)rJ   r;   r8   pis     r>   rL   zdup_trunc.<locals>.<listcomp>  s+    )))aaAnn)))r@   c                     g | ]}|z  S rG   rG   )rJ   r;   ps     r>   rL   zdup_trunc.<locals>.<listcomp>  s       a!e   r@   )is_ZZrT   is_FiniteFieldr   r   )r6   r   r8   r9   r;   r   s    ``  @r>   	dup_truncr     s     	w ! 	 	AAA16zzQ	 
	 !VV)))))a)))    Q   Q<<r@   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 rs   )r   )rJ   r;   r8   r   rB   s     r>   rL   zdmp_trunc.<locals>.<listcomp>  s+    ;;;1wq!QUA..;;;r@   )r   )r6   r   rB   r8   s    ```r>   	dmp_truncr     s2    " ;;;;;;;;;Q???r@   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

    r/   c                 4    g | ]}t          |          S rG   )dmp_ground_trunc)rJ   r;   r8   r   rC   s     r>   rL   z$dmp_ground_trunc.<locals>.<listcomp>  s(    @@@'1a33@@@r@   )r   r   )r6   r   rB   r8   rC   s    ` `@r>   r   r     sU       "Aq!!!	AA@@@@@@Q@@@!DDDr@   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   )r6   r8   lcs      r>   	dup_monicr     sG    $  	1Bxx|| *2q)))r@   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   )r6   rB   r8   r   s       r>   dmp_ground_monicr     sm    ,  A!Q 	q!Q		Bxx|| -2q!,,,r@   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   r0   gcdr   )r6   r8   r   contr;   s        r>   dup_contentr   ?  s    , '&&&&& v6DBww 	" 	"A55q>>DD	"  	 	A55q>>Dxx~~  Kr@   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   r/   )r   r   r   r%   r0   r   dmp_ground_contentr   )r6   rB   r8   r   r   rC   r;   s          r>   r   r   i  s    , '&&&&& !1a   !Q vfa!e!DBww 	< 	<A551!Q::;;DD	<  	 	A551!Q::;;Dxx~~  Kr@   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)

    )r0   r   r   r   )r6   r8   r   s      r>   dup_primitiver     sY    ,  vqyq!Dxx~~ 0Qw^AtQ////r@   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%   r0   r   r   r   )r6   rB   r8   r   s       r>   dmp_ground_primitiver     s    ,  #Q"""!Q vqyaA&&Dxx~~ 3Qw^AtQ2222r@   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   )r6   r9   r8   fcgcr   s         r>   dup_extractr     ss     
Q		B	Q		B
%%B--C88C== &1c1%%1c1%%19r@   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   )r6   r9   rB   r8   r   r   r   s          r>   dmp_ground_extractr     s{     
Aq!	$	$B	Aq!	$	$B
%%B--C88C== )1c1a((1c1a((19r@   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 )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   r   N   )r   is_QQr+   r#   oner0   r$   r   r   r&   itemsr   r	   )
r6   r8   f1f2r9   rw   r;   HrW   r7   s
             r>   dup_real_imagr     sx   & 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!VVQ1%%BB!VVQ1%%BBQ1%%BBr6Mr@   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   rS   )listr3   rv   )r6   r8   r:   s      r>   
dup_mirrorr   C  sJ     	QA3q66A:r2&&  !u!Hr@   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

    r/   rS   r   rv   r3   )r6   rg   r8   r<   br:   s         r>   	dup_scaler   Y  sb     1ggs1vvz1!qA1q5"b!!  AaD&!A#!aaHr@   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

    r/   r   rS   r   )r6   rg   r8   r<   r:   r=   s         r>   	dup_shiftr   o  s}     77CFFQJqA1a__  q! 	 	Aa!eHHH!A$HHHH	 Hr@   c                   	 st          | |d                   S t          |           r| S |d         |dd         c}	t          	          r	fd| D             } nt          |           } |rxt	          |           dz
  }t          |dd          D ]T}t          d|          D ]A}t          | |         |dz
            }t          | |dz            |dz
            | |dz   <   BUt          |           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                 :    g | ]}t          |d z
            S rs   )	dmp_shift)rJ   r;   r8   a1rB   s     r>   rL   zdmp_shift.<locals>.<listcomp>  s+    3331i2qsA&&333r@   rS   )	r   r%   anyr   rv   r3   r   r   r   )
r6   rg   rB   r8   a0r<   r:   r=   afjr   s
     ``     @r>   r   r     s-   &  %AaD!$$$!Q qT1QRR5FB
2ww 333333333GG	 :FFQJq!R 	: 	:A1a[[ : :$QqT2qsA66"1QU8S!A#q99!a%: Q??r@   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

    r/   r   rS   N)rv   r   r3   rT   r
   zipr   r   )	r6   r   qr8   r<   rw   Qr:   r;   s	            r>   dup_transformr     s      	A
AaD6QUG9qA1a[[ ' '	21%%&&&&AabbE1QRR5!!  1Aq!1a##Aq!Hr@   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

    r/   r   N)rv   r   ri   r   r
   r   )r6   r9   r8   rw   r;   s        r>   dup_composer     s     1vv{{(1fQllA667888 		
1AqrrU % %Aq!Aq!$$Hr@   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   r/   N)r   r%   r   r   )r6   r9   rB   r8   rw   r;   s         r>   dmp_composer     s      $1a###!Q 	
1AqrrU ( (Aq!QAq!Q''Hr@   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`.r/   r   )rv   r   r&   r   r3   r0   quor'   )r6   sr8   r<   r   r9   rr:   rV   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"""r@   c                     i d}}| rEt          | ||          \  }}t          |          dk    rdS t          ||          ||<   ||dz   }} | Et          ||          S )r   r   Nr/   )r   r   r   r'   )r6   rw   r8   r9   r:   r   r   s          r>   _dup_left_decomposer   &  sx    qqA
 q!Q1a==14!Q<<AaDa!eqA   Q"""r@   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`.r/   r   r   N)rv   r3   r   r   )r6   r8   dfr   rw   r9   s         r>   _dup_decomposer   6  sw    	Q!B1b\\ 
 
6Q;; Aq))=#Aq!,,A}!t4r@   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   )r6   r8   Frh   rw   s        r>   dup_decomposer   I  sO    H 	A1%%DAqaAA 37Nr@   c                     |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+   )r6   rB   r8   s      r>   dmp_alg_injectr   {  sY    , 	 QA. Q'1a000	
 Q"1a+++OPPPr@   c                     t          | |          i }} |                                 D ]'\  }}|j        |j        }}|r||d|z   <   |r||d|z   <   (t	          ||dz   |j                  }||dz   |j        fS )+Helper function for :func:`dmp_alg_inject`.)r   rs   r/   )r   r   xyr   dom)	r6   rB   r8   rw   f_monomr9   r   r   r   s	            r>   r   r     s    q!bqAggii " "
sAC1 	" !AdWn 	" !AdWnaQ&&Aa!eQU?r@   c                    t          | |          i }} |                                 D ]9\  }}|                                                                D ]\  }}||||z   <   :t          ||dz   |j                  }||dz   |j        fS )r   r/   )r   r   to_dictr   r   )	r6   rB   r8   rw   r   r9   g_monomr;   r   s	            r>   r   r     s    q!bqAggii % %
))++++-- 	% 	%JGQ#$Ag  	% 	aQ&&Aa!eQU?r@   c           
          ddl m} t          | ||          \  }}}|j                                        }t          |t          t          d|dz                       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   r3   )	r6   rB   r8   r   r   rC   K2p_aP_As	            r>   dmp_liftr     sy    ( +*****aA&&HAq"
%--//C
c4aQ00!R
8
8C=CB'''r@   c                    fd}j         sj        sj        rj        }nj        rj        j        r|}n~j        sj        r^t          j
                  dk    rFj        j         sj        j        sj        r'j
        d         j        rj
        d         j        r|}nt          dz            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

    c                 X    | sdS t                              |           dk               S )NFr   )boolto_sympy)rg   r8   s    r>   is_negative_sympyz.dup_sign_variations.<locals>.is_negative_sympy  s/     		+5 

1)***r@   r/   r   z-sign variation counting not supported over %s)r   r   is_RRis_negativeis_AlgebraicFieldextis_comparableis_PolynomialRingis_FractionFieldrv   symbolsr   is_transcendentalr+   r0   )r6   r8   r   r   prevrW   rV   s    `     r>   dup_sign_variationsr    sB   
+ 
+ 
+ 
+ 
+  	w O!' OQW Om	
	 	O!4 	O' O!"4 O#ai..A:M:M5; ;N%+ ;N)*)< ;N
)A,
( ;N-.Yq\-G ;N
 (IAMNNNfa!D  ;uTz"" 	FA 	DHr@   NFc                 `   j         r                                nj        | D ]+}                                        |                    ,                              r|s| fS t          |           fS fd| D             } |s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           	          g | ]A}                     |                                                  |                    z  BS rG   )numerr   denom)rJ   r;   K0K1commons     r>   rL   z$dup_clear_denoms.<locals>.<listcomp>0  s@    <<<Q!RVVFBHHQKK00	0<<<r@   )has_assoc_Ringget_ringr   lcmr  r   r   )r6   r  r	  rf   r;   r
  s    ``  @r>   dup_clear_denomsr    s    $ 
z 	BBBVF - -,,	yy 2 	219;q"b1111 	=<<<<<!<<<A {1b"----qyr@   c           
          |j         }|s/| D ]+}|                    ||                    |                    },n0|dz
  }| D ](}|                    |t          ||||                    })|S )z.Recursive helper for :func:`dmp_clear_denoms`.r/   )r   r  r  _rec_clear_denoms)r9   rC   r  r	  r
  r;   rK   s          r>   r  r  8  s    VF E 	1 	1AVVFBHHQKK00FF	1 E 	E 	EAVVF$5aB$C$CDDFFMr@   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)

    )rf   )r  r  r  r  r   r   r   )r6   rB   r  r	  rf   r
  s         r>   dmp_clear_denomsr  H  s    $  <2r7;;;;	z 	BBBq!R,,F99V -1fa,, 1qy{1aR0000r@   c                    |                     t          | |                    g}|j        |j        |j        g}t	          t          t          |                              }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/   r   )revertr!   r   r0   r   _ceil_log2r3   r   r
   r   r   r   r   r   )	r6   r<   r8   r9   rw   Nr:   rg   r   s	            r>   
dup_revertr  n  s    ( 
&A,,		 A	
AE%((OOA1a!e__ , ,1aaddA&&Awq!}}a((GAq!$$a++q*Q--++Hr@   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*   )r6   r9   rB   r8   s       r>   
dmp_revertr    s.      0!Q""")!Q///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?   rD   rH   rQ   rX   rZ   r]   rc   ri   rk   rn   rp   rt   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  rG   r@   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  D@ @ @(E E E0* * *:!- !- !-H' ' 'T* * *Z0 0 0B!3 !3 !3H  4  41 1 1h  ,  ,  .( ( (V  >  :  :# # #8# # #   &/ / /dQ Q Q<   
 
 
( ( (<4 4 4n* * * *Z   #1 #1 #1 #1L  D0 0 0 0 0r@   