a
    RG5d                     @   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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.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=m>Z> ddl?m@Z@ ddlAmBZBmCZCmDZDmEZEmFZF dd	 ZGd
d ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRd d! ZSdxd#d$ZTd%d& ZUd'd( ZVd)d* ZWd+d, ZXd-d. ZYd/d0 ZZd1d2 Z[dyd3d4Z\d5d6 Z]d7d8 Z^d9d: Z_d;d< Z`d=d> Zad?d@ ZbdAdB ZcdCdD ZddEdF ZedGdH ZfdIdJ ZgdKZhdLdM ZidNdO ZjdPdQ ZkdRdS ZldTdU ZmdVdW ZndXdY ZodZd[ Zpd\d] Zqd^d_ Zrd`da Zsdbdc Ztddde Zudfdg Zvdhdi Zwdjdk Zxdldm Zydndo Zzdpdq Z{dzdsdtZ|d{dudvZ}dwS )|zEEuclidean algorithms, GCDs, LCMs and polynomial remainder sequences.     )dup_sub_muldup_negdmp_negdmp_adddmp_subdup_muldmp_muldmp_powdup_divdmp_divdup_remdup_quodmp_quodup_premdmp_premdup_mul_grounddmp_mul_grounddmp_mul_termdup_quo_grounddmp_quo_grounddup_max_normdmp_max_norm)	dup_strip	dmp_raisedmp_zerodmp_one
dmp_ground	dmp_one_p
dmp_zero_p	dmp_zeros
dup_degree
dmp_degreedmp_degree_indup_LCdmp_LCdmp_ground_LCdmp_multi_deflatedmp_inflatedup_convertdmp_convertdmp_apply_pairs)dup_clear_denomsdmp_clear_denomsdup_diffdmp_diffdup_evaldmp_evaldmp_eval_in	dup_truncdmp_ground_trunc	dup_monicdmp_ground_monicdup_primitivedmp_ground_primitivedup_extractdmp_ground_extractgf_intgf_crt)query)MultivariatePolynomialErrorHeuristicGCDFailedHomomorphismFailedNotInvertibleDomainErrorc                 C   sx   |j std| |jgg  }}|rTt| ||\}}|| } }|t|||| }}q t|t| ||}t| |} || fS )ar  
    Half extended Euclidean algorithm in `F[x]`.

    Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``.

    Examples
    ========

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

    >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
    >>> g = x**3 + x**2 - 4*x - 4

    >>> R.dup_half_gcdex(f, g)
    (-1/5*x + 3/5, x + 1)

    z(Cannot compute half extended GCD over %s)is_FieldrB   oner
   r   r   r#   r4   )fgKabqr rL   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/polys/euclidtools.pydup_half_gcdex2   s    

rN   c                 C   s   |st | ||S t| |dS )z
    Half extended Euclidean algorithm in `F[X]`.

    Examples
    ========

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

    N)rN   r>   rE   rF   urG   rL   rL   rM   dmp_half_gcdexU   s    rQ   c                 C   s4   t | ||\}}t||| |}t|||}|||fS )a  
    Extended Euclidean algorithm in `F[x]`.

    Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.

    Examples
    ========

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

    >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
    >>> g = x**3 + x**2 - 4*x - 4

    >>> R.dup_gcdex(f, g)
    (-1/5*x + 3/5, 1/5*x**2 - 6/5*x + 2, x + 1)

    )rN   r   r   )rE   rF   rG   shFtrL   rL   rM   	dup_gcdexf   s    rV   c                 C   s   |st | ||S t| |dS )z
    Extended Euclidean algorithm in `F[X]`.

    Examples
    ========

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

    N)rV   r>   rO   rL   rL   rM   	dmp_gcdex   s    rW   c                 C   s4   t | ||\}}||jgkr(t|||S tddS )at  
    Compute multiplicative inverse of `f` modulo `g` in `F[x]`.

    Examples
    ========

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

    >>> f = x**2 - 1
    >>> g = 2*x - 1
    >>> h = x - 1

    >>> R.dup_invert(f, g)
    -4/3

    >>> R.dup_invert(f, h)
    Traceback (most recent call last):
    ...
    NotInvertible: zero divisor

    zzero divisorN)rN   rD   r   rA   )rE   rF   rG   rR   rS   rL   rL   rM   
dup_invert   s    rX   c                 C   s   |st | ||S t| |dS )z
    Compute multiplicative inverse of `f` modulo `g` in `F[X]`.

    Examples
    ========

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

    N)rX   r>   rO   rL   rL   rM   
dmp_invert   s    rY   c                 C   s>   | |g}t | ||}|r:|| || } }t | ||}q|S )an  
    Euclidean polynomial remainder sequence (PRS) in `K[x]`.

    Examples
    ========

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

    >>> f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
    >>> g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21

    >>> prs = R.dup_euclidean_prs(f, g)

    >>> prs[0]
    x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
    >>> prs[1]
    3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
    >>> prs[2]
    -5/9*x**4 + 1/9*x**2 - 1/3
    >>> prs[3]
    -117/25*x**2 - 9*x + 441/25
    >>> prs[4]
    233150/19773*x - 102500/6591
    >>> prs[5]
    -1288744821/543589225

    )r   append)rE   rF   rG   prsrS   rL   rL   rM   dup_euclidean_prs   s    

r\   c                 C   s   |st | ||S t| |dS )z
    Euclidean polynomial remainder sequence (PRS) in `K[X]`.

    Examples
    ========

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

    N)r\   r>   rO   rL   rL   rM   dmp_euclidean_prs   s    r]   c                 C   sR   | |g}t t| |||\}}|rN|| || } }t t| |||\}}q|S )a;  
    Primitive polynomial remainder sequence (PRS) in `K[x]`.

    Examples
    ========

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

    >>> f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
    >>> g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21

    >>> prs = R.dup_primitive_prs(f, g)

    >>> prs[0]
    x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
    >>> prs[1]
    3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
    >>> prs[2]
    -5*x**4 + x**2 - 3
    >>> prs[3]
    13*x**2 + 25*x - 49
    >>> prs[4]
    4663*x - 6150
    >>> prs[5]
    1

    )r6   r   rZ   )rE   rF   rG   r[   _rS   rL   rL   rM   dup_primitive_prs   s    

r_   c                 C   s   |st | ||S t| |dS )z
    Primitive polynomial remainder sequence (PRS) in `K[X]`.

    Examples
    ========

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

    N)r_   r>   rO   rL   rL   rM   dmp_primitive_prs#  s    r`   c                 C   sJ  t | }t |}||k r,||  } }|| }}| s8g g fS |sJ| g|jgfS | |g}|| }|j |d  }t| ||}t|||}t||}	|	| }
|j|
g}|
 }
|rBt |}|| ||||| f\} }}}|	 |
|  }t| ||}t|||}t||}	|dkr.|
|d  }||	 | |}
n|	 }
||
  q||fS )a  
    Subresultant PRS algorithm in `K[x]`.

    Computes the subresultant polynomial remainder sequence (PRS)
    and the non-zero scalar subresultants of `f` and `g`.
    By [1] Thm. 3, these are the constants '-c' (- to optimize
    computation of sign).
    The first subdeterminant is set to 1 by convention to match
    the polynomial and the scalar subdeterminants.
    If 'deg(f) < deg(g)', the subresultants of '(g,f)' are computed.

    Examples
    ========

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

    >>> R.dup_inner_subresultants(x**2 + 1, x**2 - 1)
    ([x**2 + 1, x**2 - 1, -2], [1, 1, 4])

    References
    ==========

    .. [1] W.S. Brown, The Subresultant PRS Algorithm.
           ACM Transaction of Mathematical Software 4 (1978) 237-249

       )r    rD   r   r   r#   rZ   r   quo)rE   rF   rG   nmRdrI   rS   lccSkrJ   rL   rL   rM   dup_inner_subresultants4  s@    






rk   c                 C   s   t | ||d S )z
    Computes subresultant PRS of two polynomials in `K[x]`.

    Examples
    ========

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

    >>> R.dup_subresultants(x**2 + 1, x**2 - 1)
    [x**2 + 1, x**2 - 1, -2]

    r   )rk   rE   rF   rG   rL   rL   rM   dup_subresultants  s    rm   c                 C   sH   | r|s|j g fS t| ||\}}t|d dkr<|j |fS |d |fS )z
    Resultant algorithm in `K[x]` using subresultant PRS.

    Examples
    ========

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

    >>> R.dup_prs_resultant(x**2 + 1, x**2 - 1)
    (4, [x**2 + 1, x**2 - 1, -2])

    r   )zerork   r    )rE   rF   rG   re   ri   rL   rL   rM   dup_prs_resultant  s    

rp   Fc                 C   s    |rt | ||S t | ||d S )z
    Computes resultant of two polynomials in `K[x]`.

    Examples
    ========

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

    >>> R.dup_resultant(x**2 + 1, x**2 - 1)
    4

    r   )rp   )rE   rF   rG   
includePRSrL   rL   rM   dup_resultant  s    rr   c                    s  |st | | S t| |}t||}||k r@||  } }|| }}t| |rRg g fS |d t||rx| gt jgfS | |g}|| }tt j |d  t| || }t|d| }t| }	t|	| }
t j|
g}t	|
 }
t||st||}|
| ||||| f\} }}}tt	|	 t|
|  t| || } fdd|D }t| }	|dkrtt	|	 | }t|
|d  }t|| }
nt	|	 }
|
t	|
  q||fS )a  
    Subresultant PRS algorithm in `K[X]`.

    Examples
    ========

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

    >>> f = 3*x**2*y - y**3 - 4
    >>> g = x**2 + x*y**3 - 9

    >>> a = 3*x*y**4 + y**3 - 27*y + 4
    >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16

    >>> prs = [f, g, a, b]
    >>> sres = [[1], [1], [3, 0, 0, 0, 0], [-3, 0, 0, -12, 1, 0, -54, 8, 729, -216, 16]]

    >>> R.dmp_inner_subresultants(f, g) == (prs, sres)
    True

    ra   r   c                    s   g | ]}t | qS rL   r   ).0chrG   rI   vrL   rM   
<listcomp>      z+dmp_inner_subresultants.<locals>.<listcomp>)rk   r!   r   r   rD   r	   r   r   r$   r   rZ   r   r   )rE   rF   rP   rG   rc   rd   re   rf   rS   rg   rh   ri   rj   prJ   rL   rv   rM   dmp_inner_subresultants  sL    










r{   c                 C   s   t | |||d S )a  
    Computes subresultant PRS of two polynomials in `K[X]`.

    Examples
    ========

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

    >>> f = 3*x**2*y - y**3 - 4
    >>> g = x**2 + x*y**3 - 9

    >>> a = 3*x*y**4 + y**3 - 27*y + 4
    >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16

    >>> R.dmp_subresultants(f, g) == [f, g, a, b]
    True

    r   )r{   rO   rL   rL   rM   dmp_subresultants  s    r|   c                 C   st   |st | ||S t| |s$t||r4t|d g fS t| |||\}}t|d |dkrht|d |fS |d |fS )a  
    Resultant algorithm in `K[X]` using subresultant PRS.

    Examples
    ========

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

    >>> f = 3*x**2*y - y**3 - 4
    >>> g = x**2 + x*y**3 - 9

    >>> a = 3*x*y**4 + y**3 - 27*y + 4
    >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16

    >>> res, prs = R.dmp_prs_resultant(f, g)

    >>> res == b             # resultant has n-1 variables
    False
    >>> res == b.drop(x)
    True
    >>> prs == [f, g, a, b]
    True

    ra   rn   r   )rp   r   r   r{   r!   )rE   rF   rP   rG   re   ri   rL   rL   rM   dmp_prs_resultant(  s    r}   c                 C   s  |st t| ||d | |S |d }t| |}t||}t| d|}t|d|}	||	 ||  }
|jg|j  }}t|}t||
kr||j7 }||krtdt| t ||d||}t|||krt|t ||d||}t|||krqqt	|||||}t
||||}|s*t|g}t|g}n|g}|g}|t||||}t|||}t||d|}t|t||||||}t||||}t||||}t||j| g|}t|||}q||S )a  
    Compute resultant of `f` and `g` modulo a prime `p`.

    Examples
    ========

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

    >>> f = x + y + 2
    >>> g = 2*x*y + x + 3

    >>> R.dmp_zz_modular_resultant(f, g, 5)
    -2*y**2 + 1

    r   ra   no luck)r;   rp   r!   r"   rD   r   r    r@   r1   dmp_zz_modular_resultantr0   r   invertr/   r   r   r   r   r   r3   r   r2   )rE   rF   rz   rP   rG   rw   rc   rd   NMBDrH   rK   rT   Gre   erf   rh   rL   rL   rM   r   P  sF    



r   c                 C   s   t t| |g||g||| S )z2Wrapper of CRT for Collins's resultant algorithm. r:   )rK   re   Prz   rG   rL   rL   rM   _collins_crt  s    r   c                 C   sl  t | |}t ||}|dk s$|dk r0t|d S t| ||}t|||}t| ||}t|||}	|d }
|d||||  ||  ||  }t|
|j|j  }}}ddlm} ||krh|||}|| r|	| s|||}qt| |||}t||||}zt	|||||}W n t
y2   Y qY n0 ||rF|}nt||t|||f|
|}||9 }q|S )a  
    Collins's modular resultant algorithm in `Z[X]`.

    Examples
    ========

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

    >>> f = x + y + 2
    >>> g = 2*x*y + x + 3

    >>> R.dmp_zz_collins_resultant(f, g)
    -2*y**2 - 5*y + 1

    r   ra      )	nextprime)r!   r   r   r%   	factorialrD   Zsympy.ntheoryr   r3   r   r@   is_oner*   r   )rE   rF   rP   rG   rc   rd   Ar   rH   rI   rw   rK   rz   r   r   rT   r   re   rL   rL   rM   dmp_zz_collins_resultant  s6    

*


r   c                 C   s   t | |}t ||}|dk s$|dk r0t|d S | }t| |||\}} t||||\}}t| |||} t||||}t| |||}	t|	|d ||}	||| ||  |}
t|	|
|d |S )a$  
    Collins's modular resultant algorithm in `Q[X]`.

    Examples
    ========

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

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

    >>> R.dmp_qq_collins_resultant(f, g)
    -2*y**2 - 7/3*y + 5/6

    r   ra   )r!   r   get_ringr,   r)   r   convertr   )rE   rF   rP   K0rc   rd   K1cfcgrK   rh   rL   rL   rM   dmp_qq_collins_resultant  s    

r   c                 C   sx   |st | |||dS |r&t| |||S |jrJ|jrftdrft| |||S n|jrftdrft| |||S t| |||d S )aH  
    Computes resultant of two polynomials in `K[X]`.

    Examples
    ========

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

    >>> f = 3*x**2*y - y**3 - 4
    >>> g = x**2 + x*y**3 - 9

    >>> R.dmp_resultant(f, g)
    -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16

    )rq   ZUSE_COLLINS_RESULTANTr   )rr   r}   rC   is_QQr=   r   is_ZZr   )rE   rF   rP   rG   rq   rL   rL   rM   dmp_resultant  s    r   c                 C   s`   t | }|dkr|jS d||d  d  }t| |}t| t| d||}||||| S dS )z
    Computes discriminant of a polynomial in `K[x]`.

    Examples
    ========

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

    >>> R.dup_discriminant(x**2 + 2*x + 3)
    -8

    r   rn   ra   r   N)r    ro   r#   rr   r-   rb   )rE   rG   rf   rR   rh   rK   rL   rL   rM   dup_discriminant#  s    
r   c                 C   s   |st | |S t| ||d  }}|dkr2t|S d||d  d  }t| |}t| t| d||||}t|||||}t||||S dS )z
    Computes discriminant of a polynomial in `K[X]`.

    Examples
    ========

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

    >>> R.dmp_discriminant(x**2*y + x*z + t)
    -4*y*t + z**2

    ra   r   rn   r   N)r   r!   r   r$   r   r.   r   r   )rE   rP   rG   rf   rw   rR   rh   rK   rL   rL   rM   dmp_discriminant>  s    

r   c                 C   s   | s|sg g g fS | sL| t||r4|g |jgfS t||g |j gfS n8|s| t| |rn| |jgg fS t| ||j gg fS dS )3Handle trivial cases in GCD algorithm over a ring. N)is_nonnegativer#   rD   r   rl   rL   rL   rM   _dup_rr_trivial_gcd]  s    
r   c                 C   sR   | s|sg g g fS | s.t ||g t||gfS |sJt | |t| |gg fS dS dS )4Handle trivial cases in GCD algorithm over a field. N)r4   r#   rl   rL   rL   rM   _dup_ff_trivial_gcdo  s    
r   c                 C   s  t | |}t ||}t| ||p*t|||}|rD|rDttd||S |r|t|||rn|t|t||fS t|||t|t	|j
 |fS n||r|t| ||r| t||t|fS t| ||t	|j
 |t|fS n0|rt||| |fS tdrt| |||S dS dS )r      USE_SIMPLIFY_GCDN)r   r   tupler   r   r%   r   r   r   r   rD   r=   _dmp_simplify_gcd)rE   rF   rP   rG   zero_fzero_gZif_contain_onerL   rL   rM   _dmp_rr_trivial_gcd{  s$    

""
r   c                 C   s   t | |}t ||}|r,|r,ttd||S |rTt|||t|tt||||fS |r|t| ||tt| |||t|fS tdrt| |||S dS dS )r   r   r   N)	r   r   r   r5   r   r   r%   r=   r   )rE   rF   rP   rG   r   r   rL   rL   rM   _dmp_ff_trivial_gcd  s"    



r   c           
         s   t | |}t ||}|dkr(|dkr(dS |sF|sFt|  }t| }n2|sbt|  }t|| }nt| | }t| }|d t||  fdd| D } fdd|D }	g||	fS )z7Try to eliminate `x_0` from GCD computation in `K[X]`. r   Nra   c                    s   g | ]}t | qS rL   rs   )rt   r   rG   rS   rw   rL   rM   rx     ry   z%_dmp_simplify_gcd.<locals>.<listcomp>c                    s   g | ]}t | qS rL   rs   )rt   r   r   rL   rM   rx     ry   )r!   r$   dmp_contentdmp_gcd)
rE   rF   rP   rG   dfdgrT   r   cffcfgrL   r   rM   r     s"    




r   c                 C   s   t | ||}|dur|S t| |\}}t||\}}|||}t|||d }	t|	|\}
}	||t|	|9 }t|	||}	t| |	|}t||	|}|	||fS )aa  
    Computes polynomial GCD using subresultants over a ring.

    Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
    and ``cfg = quo(g, h)``.

    Examples
    ========

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

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

    Nrn   )r   r6   gcdrm   canonical_unitr#   r   r   )rE   rF   rG   resultfcrT   gcr   rh   rS   r^   r   r   rL   rL   rM   dup_rr_prs_gcd  s    r   c                 C   sT   t | ||}|dur|S t| ||d }t||}t| ||}t|||}|||fS )ab  
    Computes polynomial GCD using subresultants over a field.

    Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
    and ``cfg = quo(g, h)``.

    Examples
    ========

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

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

    Nrn   )r   rm   r4   r   )rE   rF   rG   r   rS   r   r   rL   rL   rM   dup_ff_prs_gcd  s    
r   c                 C   s   |st | ||S t| |||}|dur*|S t| ||\}}t|||\}}t||||d }	t|||d |\}
}}|t|	||rt|	||}	t|	||\}}	t|	|
d||}	t	| |	||}t	||	||}|	||fS )a  
    Computes polynomial GCD using subresultants over a ring.

    Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
    and ``cfg = quo(g, h)``.

    Examples
    ========

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

    >>> f = x**2 + 2*x*y + y**2
    >>> g = x**2 + x*y

    >>> R.dmp_rr_prs_gcd(f, g)
    (x + y, x + y, x)

    Nrn   ra   r   )
r   r   dmp_primitiver|   dmp_rr_prs_gcdis_negativer%   r   r   r   rE   rF   rP   rG   r   r   rT   r   r   rS   rh   r^   r   r   rL   rL   rM   r     s     r   c                 C   s   |st | ||S t| |||}|dur*|S t| ||\}}t|||\}}t||||d }	t|||d |\}
}}t|	||\}}	t|	|
d||}	t|	||}	t| |	||}t||	||}|	||fS )a  
    Computes polynomial GCD using subresultants over a field.

    Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
    and ``cfg = quo(g, h)``.

    Examples
    ========

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

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

    >>> R.dmp_ff_prs_gcd(f, g)
    (x + y, 1/2*x + 1/2*y, x)

    Nrn   ra   r   )r   r   r   r|   dmp_ff_prs_gcdr   r5   r   r   rL   rL   rM   r   ;  s    r      c                 C   sB   g }| r>| | }||d kr$||8 }| d| | | | } q|S )-Interpolate polynomial GCD from integer GCD. r   r   )insert)rS   xrG   rE   rF   rL   rL   rM   _dup_zz_gcd_interpolatei  s    r   c              
   C   sB  t | ||}|dur|S t| }t|}t| ||\}} }|dksJ|dkrV|g| |fS t| |}t||}|dt|| d }	tt|	d||	 dt|tt| | |tt||  d }
t	dt
D ]d}t| |
|}t||
|}|r|r|||}|| }|| }t||
|}t||d }t| ||\}}|stt|||\}}|stt|||}|||f  S t||
|}t| ||\}}|st|||\}}|st|||}|||f  S t||
|}t|||\}}|st| ||\}}|st|||}|||f  S d|
 |||
 d }
qtd	dS )
a
  
    Heuristic polynomial GCD in `Z[x]`.

    Given univariate polynomials `f` and `g` in `Z[x]`, returns
    their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg``
    such that::

          h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h)

    The algorithm is purely heuristic which means it may fail to compute
    the GCD. This will be signaled by raising an exception. In this case
    you will need to switch to another GCD method.

    The algorithm computes the polynomial GCD by evaluating polynomials
    f and g at certain points and computing (fast) integer GCD of those
    evaluations. The polynomial GCD is recovered from the integer image
    by interpolation.  The final step is to verify if the result is the
    correct GCD. This gives cofactors as a side effect.

    Examples
    ========

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

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

    References
    ==========

    .. [1] [Liao95]_

    Nr   r      c   ra   B  i  r~   )r   r    r8   r   minmaxsqrtabsr#   rangeHEU_GCD_MAXr/   r   r   r6   r
   r   r?   )rE   rF   rG   r   r   r   r   f_normg_normr   r   iffggrS   r   r   cff_rK   cfg_rL   rL   rM   dup_zz_heu_gcdy  sb    #

r   c                 C   st   g }t | |sFt| |||}|d| t| |||} t| |||} q|t||d |rlt||d |S |S dS )r   r   ra   N)r   r3   r   r   r   r   r%   r   )rS   r   rw   rG   rE   rF   rL   rL   rM   _dmp_zz_gcd_interpolate  s    
r   c                 C   s  |st | ||S t| |||}|dur*|S t| |||\}} }t| ||}t|||}|dt|| d }tt|d|| dt|tt| || |tt|||  d }	t	dt
D ]}
t| |	||}t||	||}|d }t||sVt||sVt||||\}}}t||	||}t|||d }t| |||\}}t||rt||||\}}t||rt||||}|||f  S t||	||}t| |||\}}t||rt||||\}}t||rt||||}|||f  S t||	||}t||||\}}t||rVt| |||\}}t||rVt||||}|||f  S d|	 |||	 d }	qtd	dS )
a  
    Heuristic polynomial GCD in `Z[X]`.

    Given univariate polynomials `f` and `g` in `Z[X]`, returns
    their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg``
    such that::

          h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h)

    The algorithm is purely heuristic which means it may fail to compute
    the GCD. This will be signaled by raising an exception. In this case
    you will need to switch to another GCD method.

    The algorithm computes the polynomial GCD by evaluating polynomials
    f and g at certain points and computing (fast) integer GCD of those
    evaluations. The polynomial GCD is recovered from the integer image
    by interpolation. The evaluation process reduces f and g variable by
    variable into a large integer.  The final step is to verify if the
    interpolated polynomial is the correct GCD. This gives cofactors of
    the input polynomials as a side effect.

    Examples
    ========

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

    >>> f = x**2 + 2*x*y + y**2
    >>> g = x**2 + x*y

    >>> R.dmp_zz_heu_gcd(f, g)
    (x + y, x + y, x)

    References
    ==========

    .. [1] [Liao95]_

    Nr   r   r   r   ra   r   r   r~   )r   r   r9   r   r   r   r   r   r%   r   r   r0   r   dmp_zz_heu_gcdr   r7   r   r   r?   )rE   rF   rP   rG   r   r   r   r   r   r   r   r   r   rw   rS   r   r   r   rK   r   rL   rL   rM   r     s\    (r   c                 C   s   t | ||}|dur|S | }t| ||\}} t|||\}}t| ||} t|||}t| ||\}}}	t|||}t||}
t||}t|||}t|	||}	t|||
||}t|	||
||}	|||	fS )a  
    Heuristic polynomial GCD in `Q[x]`.

    Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
    ``cff = quo(f, h)``, and ``cfg = quo(g, h)``.

    Examples
    ========

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

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

    >>> R.dup_qq_heu_gcd(f, g)
    (x + 2, 1/2*x + 3/4, 1/2*x)

    N)	r   r   r+   r(   r   r#   r4   r   rb   )rE   rF   r   r   r   r   r   rS   r   r   rh   rL   rL   rM   dup_qq_heu_gcd_  s"    

r   c                 C   s   t | |||}|dur|S | }t| |||\}} t||||\}}t| |||} t||||}t| |||\}}	}
t||||}t|||}t|||}t|	|||}	t|
|||}
t|	|||||}	t|
|||||}
||	|
fS )a  
    Heuristic polynomial GCD in `Q[X]`.

    Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
    ``cff = quo(f, h)``, and ``cfg = quo(g, h)``.

    Examples
    ========

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

    >>> f = QQ(1,4)*x**2 + x*y + y**2
    >>> g = QQ(1,2)*x**2 + x*y

    >>> R.dmp_qq_heu_gcd(f, g)
    (x + 2*y, 1/4*x + 1/2*y, 1/2*x)

    N)	r   r   r,   r)   r   r%   r5   r   rb   )rE   rF   rP   r   r   r   r   r   rS   r   r   rh   rL   rL   rM   dmp_qq_heu_gcd  s"    r   c                 C   s  |j sz| }W n  ty2   |jg| |f Y S 0 t| ||} t|||}t| ||\}}}t|||}t|||}t|||}|||fS |jr|jrtdrzt	| ||W S  t
y   Y n0 t| ||S |jrtdrzt| ||W S  t
y   Y n0 t| ||S dS )ag  
    Computes polynomial GCD and cofactors of `f` and `g` in `K[x]`.

    Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
    ``cff = quo(f, h)``, and ``cfg = quo(g, h)``.

    Examples
    ========

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

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

    USE_HEU_GCDN)is_Exact	get_exactrB   rD   r(   dup_inner_gcdrC   r   r=   r   r?   r   r   r   r   )rE   rF   rG   exactrS   r   r   rL   rL   rM   r     s2    
r   c                 C   s*  |j sz| }W n" ty4   t||| |f Y S 0 t| |||} t||||}t| |||\}}}t||||}t||||}t||||}|||fS |jr|jrtdrzt	| |||W S  t
y   Y n0 t| |||S |jrtdrzt| |||W S  t
y   Y n0 t| |||S dS )z'Helper function for `dmp_inner_gcd()`. r   N)r   r   rB   r   r)   _dmp_inner_gcdrC   r   r=   r   r?   r   r   r   r   )rE   rF   rP   rG   r   rS   r   r   rL   rL   rM   r     s2    
r   c                 C   sd   |st | ||S t| |f||\}\} }t| |||\}}}t||||t||||t||||fS )a  
    Computes polynomial GCD and cofactors of `f` and `g` in `K[X]`.

    Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
    ``cff = quo(f, h)``, and ``cfg = quo(g, h)``.

    Examples
    ========

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

    >>> f = x**2 + 2*x*y + y**2
    >>> g = x**2 + x*y

    >>> R.dmp_inner_gcd(f, g)
    (x + y, x + y, x)

    )r   r&   r   r'   )rE   rF   rP   rG   JrS   r   r   rL   rL   rM   dmp_inner_gcd  s    r   c                 C   s   t | ||d S )z
    Computes polynomial GCD of `f` and `g` in `K[x]`.

    Examples
    ========

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

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

    r   )r   rl   rL   rL   rM   dup_gcd7  s    r   c                 C   s   t | |||d S )z
    Computes polynomial GCD of `f` and `g` in `K[X]`.

    Examples
    ========

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

    >>> f = x**2 + 2*x*y + y**2
    >>> g = x**2 + x*y

    >>> R.dmp_gcd(f, g)
    x + y

    r   )r   rO   rL   rL   rM   r   H  s    r   c                 C   sP   t | |\}} t ||\}}|||}tt| ||t| |||}t|||S )z
    Computes polynomial LCM over a ring in `K[x]`.

    Examples
    ========

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

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

    )r6   lcmr   r   r   r   )rE   rF   rG   r   r   rh   rS   rL   rL   rM   
dup_rr_lcm\  s    r   c                 C   s&   t t| ||t| |||}t||S )a  
    Computes polynomial LCM over a field in `K[x]`.

    Examples
    ========

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

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

    >>> R.dup_ff_lcm(f, g)
    x**3 + 7/2*x**2 + 3*x

    )r   r   r   r4   )rE   rF   rG   rS   rL   rL   rM   
dup_ff_lcmu  s    r   c                 C   s"   |j rt| ||S t| ||S dS )z
    Computes polynomial LCM of `f` and `g` in `K[x]`.

    Examples
    ========

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

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

    N)rC   r   r   rl   rL   rL   rM   dup_lcm  s    r   c                 C   s\   t | ||\}} t |||\}}|||}tt| |||t| |||||}t||||S )a  
    Computes polynomial LCM over a ring in `K[X]`.

    Examples
    ========

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

    >>> f = x**2 + 2*x*y + y**2
    >>> g = x**2 + x*y

    >>> R.dmp_rr_lcm(f, g)
    x**3 + 2*x**2*y + x*y**2

    )r7   r   r   r   r   r   )rE   rF   rP   rG   r   r   rh   rS   rL   rL   rM   
dmp_rr_lcm  s    r   c                 C   s.   t t| |||t| |||||}t|||S )a"  
    Computes polynomial LCM over a field in `K[X]`.

    Examples
    ========

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

    >>> f = QQ(1,4)*x**2 + x*y + y**2
    >>> g = QQ(1,2)*x**2 + x*y

    >>> R.dmp_ff_lcm(f, g)
    x**3 + 4*x**2*y + 4*x*y**2

    )r   r   r   r5   )rE   rF   rP   rG   rS   rL   rL   rM   
dmp_ff_lcm  s    r   c                 C   s6   |st | ||S |jr$t| |||S t| |||S dS )a  
    Computes polynomial LCM of `f` and `g` in `K[X]`.

    Examples
    ========

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

    >>> f = x**2 + 2*x*y + y**2
    >>> g = x**2 + x*y

    >>> R.dmp_lcm(f, g)
    x**3 + 2*x**2*y + x*y**2

    N)r   rC   r   r   rO   rL   rL   rM   dmp_lcm  s
    r   c                 C   sx   t | ||d  }}t| |r"|S | dd D ]"}t||||}t|||r. qRq.|t|||rpt|||S |S dS )z
    Returns GCD of multivariate coefficients.

    Examples
    ========

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

    >>> R.dmp_content(2*x*y + 6*x + 4*y + 12)
    2*y + 6

    ra   N)r$   r   r   r   r   r%   r   )rE   rP   rG   contrw   rh   rL   rL   rM   r     s    
r   c                    sR   t | | |d  t| |s,t r4| fS  fdd| D fS dS )z
    Returns multivariate content and a primitive polynomial.

    Examples
    ========

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

    >>> R.dmp_primitive(2*x*y + 6*x + 4*y + 12)
    (2*y + 6, x + 2)

    ra   c                    s   g | ]}t | qS rL   rs   )rt   rh   rG   r   rw   rL   rM   rx     ry   z!dmp_primitive.<locals>.<listcomp>N)r   r   r   )rE   rP   rG   rL   r   rM   r     s    r   Tc                 C   s   t | |d||dS )z
    Cancel common factors in a rational function `f/g`.

    Examples
    ========

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

    >>> R.dup_cancel(2*x**2 - 2, x**2 - 2*x + 1)
    (2*x + 2, x - 1)

    r   )include)
dmp_cancel)rE   rF   rG   r   rL   rL   rM   
dup_cancel"  s    r   c                 C   s\  d}|j rL|jrL||  }}t| |||dd\}} t||||dd\}}n|j|j }}t| |||\}}	}
|dur|||\}}}t|	|||}	t|
|||}
|}|t	|	||}|t	|
||}|r|rt
|	||t
|
|| }	}
n6|r| t
|	|| }}	n|r&| t
|
|| }}
|s8|||	|
fS t|	|||}	t|
|||}
|	|
fS )z
    Cancel common factors in a rational function `f/g`.

    Examples
    ========

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

    >>> R.dmp_cancel(2*x**2 - 2, x**2 - 2*x + 1)
    (2*x + 2, x - 1)

    NT)r   )rC   has_assoc_Ringr   r,   rD   r   	cofactorsr)   r   r%   r   r   )rE   rF   rP   rG   r   r   cqcpr^   rz   rJ   Zp_negZq_negrL   rL   rM   r   3  s2    r   N)F)F)T)T)~__doc__sympy.polys.densearithr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   sympy.polys.densebasicr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   sympy.polys.densetoolsr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   Zsympy.polys.galoistoolsr;   r<   Zsympy.polys.polyconfigr=   sympy.polys.polyerrorsr>   r?   r@   rA   rB   rN   rQ   rV   rW   rX   rY   r\   r]   r_   r`   rk   rm   rp   rr   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   r   r   r   r   r   r   rL   rL   rL   rM   <module>   sz   `T
D
#((P
P(I='
!(.+jk113$
