U
    ZÊ¹jP£  ã                   @   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   úV/var/www/html/bwave/python/venv/lib/python3.8/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||ƒ�sÞt||ƒ}| 
|¡ ||||| f\} }}}tt	|	ˆˆ ƒt|
|ˆˆ ƒˆˆ ƒ‰t| ||ˆ ƒ}‡ ‡‡fdd„|D ƒ}t|ˆ ƒ}	|dk�r¾t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>  s     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Á  sP    








  ÿ

rz   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   )rz   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   rz   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r¤tdƒ‚t| t ||ƒd||ƒ}t||ƒ|krŠt|t ||ƒd||ƒ}t||ƒ|krŠqðqŠt	|||||ƒ}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   ry   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   ÚPry   rG   rL   rL   rM   Ú_collins_crt™  s    r‡   c                 C   sn  t | |ƒ}t ||ƒ}|dk s$|dk r0t|d ƒS t| ||ƒ}t|||ƒ}t| ||ƒ}t|||ƒ}	|d }
|dƒ| ||| ƒ¡ ||  ||  }t|
ƒ|j|j  }}}ddlm} ||k�rj|||ƒƒ}|| rÜ|	| sê|||ƒƒ}qÌt| |||ƒ}t||||ƒ}zt	|||||ƒ}W n t
k
�r4   Y q¶Y nX | |¡�rH|}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@   Zis_oner*   r‡   )rE   rF   rP   rG   rc   rd   ÚAr‚   rH   rI   rw   rK   ry   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|rðt||ƒ| |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ƒr’t| |||ƒ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   À  s     z%_dmp_simplify_gcd.<locals>.<listcomp>c                    s   g | ]}t |ˆˆˆ ƒ‘qS rL   rs   )rt   r’   r¦   rL   rM   rx   Á  s     )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k	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k	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k	r*|S t| ||ƒ\}}t|||ƒ\}}t||||ƒd }	t|||d |ƒ\}
}}t|	||ƒ\}}	t|	|
d||ƒ}	| t|	||ƒ¡}||jkr¾t	|	|||ƒ}	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_gcdr   r®   r%   rD   r   r   )rE   rF   rP   rG   r¯   r°   rT   r±   r„   rS   rh   r^   Úunitr«   r¬   rL   rL   rM   rµ     s"    
rµ   c                 C   sÆ   |st | ||ƒS t| |||ƒ}|dk	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   )rE   rF   rP   rG   r¯   r°   rT   r±   r„   rS   rh   r^   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_interpolatek  s    r¼   c              
   C   sB  t | ||ƒ}|dk	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| ||ƒ\}}|�sÆt|||ƒ\}}|�sÆt|||ƒ}|||f  S t||
|ƒ}t|||ƒ\}}|�st| ||ƒ\}}|�st|||ƒ}|||f  S d|
 | | |
¡¡ d	 }
qÎt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_gcd{  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   Úis_negativer%   r   )rS   r»   rw   rG   rE   rF   rL   rL   rM   Ú_dmp_zz_gcd_interpolateå  s    
rÑ   c                 C   s€  |st | ||ƒS t| |||ƒ}|dk	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||ƒ�rŠt||||ƒ\}}t||ƒ�rŠt||||ƒ}|||f  S t||	||ƒ}t| |||ƒ\}}t||ƒ�rðt||||ƒ\}}t||ƒ�rðt||||ƒ}|||f  S t||	||ƒ}t||||ƒ\}}t||ƒ�rVt| |||ƒ\}}t||ƒ�rVt||||ƒ}|||f  S d|	 | | |	¡¡ d	 }	q¾t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¿   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k	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_gcda  s"    

rÓ   c                 C   sî   t | |||ƒ}|dk	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 s|jr”z| ¡ }W n" tk
r:   |jg| |f Y S X t| ||ƒ} t|||ƒ}t| ||ƒ\}}}t|||ƒ}t|||ƒ}t|||ƒ}|||fS |jrØ|jrÌt	dƒrÌzt
| ||ƒW S  tk
rÊ   Y nX t| ||ƒS |j�rt	dƒ�rzt| ||ƒW S  tk
�r   Y nX 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)Zis_RRZis_CCÚ	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   s0  |j sœz| ¡ }W n$ tk
r6   t||ƒ| |f Y S X t| |||ƒ} t||||ƒ}t| |||ƒ\}}}t||||ƒ}t||||ƒ}t||||ƒ}|||fS |jrä|jrÖtdƒrÖzt	| |||ƒW S  t
k
rÔ   Y nX t| |||ƒS |j�rtdƒ�rzt| |||ƒW S  t
k
�r   Y nX t| |||ƒS dS )z'Helper function for `dmp_inner_gcd()`. rÕ   N)Zis_ExactrÖ   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_gcdE  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¨   V  s    r¨   c                 C   st   | r|st dƒS t| |ƒ\}} 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

    r   )	r   r6   Úlcmr   r   rÜ   r®   r#   r   )rE   rF   rG   r°   r±   rh   rS   rP   rL   rL   rM   Ú
dup_rr_lcmj  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_lcmˆ  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   2  s     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_cancel5  s    rè   c                 C   s\  d}|j rL|jrL|| ¡  }}t| |||dd�\}} t||||dd�\}}n|j|j }}t| |||ƒ\}}	}
|dk	r¨| ||¡\}}}t|	|||ƒ}	t|
|||ƒ}
|}| t	|	||ƒ¡}| t	|
||ƒ¡}|rð|rðt
|	||ƒ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   Zhas_assoc_Ringr�   r,   rD   rÛ   Z	cofactorsr)   rÐ   r%   r   r   )rE   rF   rP   rG   ræ   r�   ZcqÚcpr^   ry   rJ   Zp_negZq_negrL   rL   rM   rç   F  s2    rç   N)F)F)T)T)~Ú__doc__Zsympy.polys.densearithr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   Zsympy.polys.densebasicr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   Zsympy.polys.densetoolsr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   Zsympy.polys.galoistoolsr;   r<   Zsympy.polys.polyconfigr=   Zsympy.polys.polyerrorsr>   r?   r@   rA   rB   rN   rQ   rV   rW   rX   rY   r\   r]   r_   r`   rk   rm   rp   rr   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â   rã   r§   r´   rè   rç   rL   rL   rL   rM   Ú<module>   sz   `T
D
#((P
P(I='
!(0+jk11?$
