B
    [,                 @   sl  d Z ddl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 ddl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! ddl"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z- d	d
 Z.dd Z/dd Z0dd Z1dd Z2dd Z3dd Z4dd Z5dd Z6d-ddZ7d.ddZ8d/d d!Z9d0d"d#Z:d1d$d%Z;d2d&d'Z<d(d) Z=d*d+ Z>d,S )3z8Square-free decomposition algorithms and related tools.     )print_functiondivision)
	dup_stripdup_LCdmp_ground_LC
dmp_zero_p
dmp_ground
dup_degree
dmp_degree	dmp_raise
dmp_injectdup_convert)	dup_negdmp_negdup_subdmp_subdup_muldup_quodmp_quodup_mul_grounddmp_mul_ground)dup_diffdmp_diff	dup_shiftdmp_compose	dup_monicdmp_ground_monicdup_primitivedmp_ground_primitive)dup_inner_gcddmp_inner_gcddup_gcddmp_gcddmp_resultant)gf_sqf_listgf_sqf_part)MultivariatePolynomialErrorDomainErrorc             C   s&   | sdS t t| t| d|| S dS )a  
    Return ``True`` if ``f`` is a square-free polynomial in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_sqf_p(x**2 - 2*x + 1)
    False
    >>> R.dup_sqf_p(x**2 - 1)
    True

    T   N)r	   r!   r   )fK r+   6lib/python3.7/site-packages/sympy/polys/sqfreetools.py	dup_sqf_p'   s    r-   c             C   s2   t | |rdS tt| t| d||||| S dS )a  
    Return ``True`` if ``f`` is a square-free polynomial in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_sqf_p(x**2 + 2*x*y + y**2)
    False
    >>> R.dmp_sqf_p(x**2 + y**2)
    True

    Tr(   N)r   r
   r"   r   )r)   ur*   r+   r+   r,   	dmp_sqf_p=   s    
r/   c             C   s   |j stddt|jjdd|j }}xRt| d|dd\}}t||d|j}t||jr^P q*t	| |j
 ||d  } }q*W || |fS )al  
    Square-free norm of ``f`` in ``K[x]``, useful over algebraic domains.

    Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and ``r(x) = Norm(g(x))``
    is a square-free polynomial over K, where ``a`` is the algebraic extension of ``K``.

    Examples
    ========

    >>> from sympy.polys import ring, QQ
    >>> from sympy import sqrt

    >>> K = QQ.algebraic_field(sqrt(3))
    >>> R, x = ring("x", K)
    >>> _, X = ring("x", QQ)

    >>> s, f, r = R.dup_sqf_norm(x**2 - 2)

    >>> s == 1
    True
    >>> f == x**2 + K([QQ(-2), QQ(0)])*x + 1
    True
    >>> r == X**4 - 10*X**2 + 1
    True

    zground domain must be algebraicr   r(   T)front)is_Algebraicr'   r   modrepdomr   r#   r-   r   unit)r)   r*   sgh_rr+   r+   r,   dup_sqf_normS   s    r;   c       	      C   s   |st | |S |jstdt|jj|d d|j}t|j|j g|d|}d}xVt	| ||dd\}}t
|||d |j}t|||jrP qRt| ||||d  } }qRW || |fS )a  
    Square-free norm of ``f`` in ``K[X]``, useful over algebraic domains.

    Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and ``r(x) = Norm(g(x))``
    is a square-free polynomial over K, where ``a`` is the algebraic extension of ``K``.

    Examples
    ========

    >>> from sympy.polys import ring, QQ
    >>> from sympy import I

    >>> K = QQ.algebraic_field(I)
    >>> R, x, y = ring("x,y", K)
    >>> _, X, Y = ring("x,y", QQ)

    >>> s, f, r = R.dmp_sqf_norm(x*y + y**2)

    >>> s == 1
    True
    >>> f == x*y + y**2 + K([QQ(-1), QQ(0)])*y
    True
    >>> r == X**2*Y**2 + 2*X*Y**3 + Y**4 + Y**2
    True

    zground domain must be algebraicr(   r   T)r0   )r;   r1   r'   r   r2   r3   r4   oner5   r   r#   r/   r   )	r)   r.   r*   r7   Fr6   r8   r9   r:   r+   r+   r,   dmp_sqf_norm   s    
r>   c             C   sN   |j stdt|jj|d d|j}t| ||dd\}}t|||d |jS )zE
    Norm of ``f`` in ``K[X1, ..., Xn]``, often not square-free.
    zground domain must be algebraicr(   r   T)r0   )r1   r'   r   r2   r3   r4   r   r#   )r)   r.   r*   r7   r8   r9   r+   r+   r,   dmp_norm   s
    r?   c             C   s,   t | ||j} t| |j|j}t ||j|S )z3Compute square-free part of ``f`` in ``GF(p)[x]``. )r   r4   r%   r2   )r)   r*   r7   r+   r+   r,   dup_gf_sqf_part   s    r@   c             C   s   t ddS )z3Compute square-free part of ``f`` in ``GF(p)[X]``. z+multivariate polynomials over finite fieldsN)NotImplementedError)r)   r*   r+   r+   r,   dmp_gf_sqf_part   s    rB   c             C   st   |j rt| |S | s| S |t| |r2t| |} t| t| d||}t| ||}|jrbt	||S t
||d S dS )z
    Returns square-free part of a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_sqf_part(x**3 - 3*x - 2)
    x**2 - x - 2

    r(   N)is_FiniteFieldr@   is_negativer   r   r!   r   r   is_Fieldr   r   )r)   r*   gcdsqfr+   r+   r,   dup_sqf_part   s    


rH   c             C   s   |st | |S |jr t| ||S t| |r.| S |t| ||rLt| ||} t| t| d||||}t	| |||}|j
rt|||S t|||d S dS )z
    Returns square-free part of a polynomial in ``K[X]``.

    Examples
    ========

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

    >>> R.dmp_sqf_part(x**3 + 2*x**2*y + x*y**2)
    x**2 + x*y

    r(   N)rH   rC   rB   r   rD   r   r   r"   r   r   rE   r   r   )r)   r.   r*   rF   rG   r+   r+   r,   dmp_sqf_part   s    

rI   Fc             C   sh   t | ||j} t| |j|j|d\}}x.t|D ]"\}\} }t | |j||f||< q0W |||j|fS )z<Compute square-free decomposition of ``f`` in ``GF(p)[x]``. )all)r   r4   r$   r2   	enumerateZconvert)r)   r*   rJ   coefffactorsikr+   r+   r,   dup_gf_sqf_list  s
    rP   c             C   s   t ddS )z<Compute square-free decomposition of ``f`` in ``GF(p)[X]``. z+multivariate polynomials over finite fieldsN)rA   )r)   r.   r*   rJ   r+   r+   r,   dmp_gf_sqf_list  s    rQ   c             C   s  |j rt| ||dS |jr0t| |}t| |} n.t| |\}} |t| |r^t| |} | }t| dkrr|g fS g d }}t	| d|}t
| ||\}}}	xht	|d|}
t|	|
|}|s|||f P t
|||\}}}	|st|dkr|||f |d7 }qW ||fS )as  
    Return square-free decomposition of a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16

    >>> R.dup_sqf_list(f)
    (2, [(x + 1, 2), (x + 2, 3)])
    >>> R.dup_sqf_list(f, all=True)
    (2, [(1, 1), (x + 1, 2), (x + 2, 3)])

    )rJ   r   r(   )rC   rP   rE   r   r   r   rD   r   r	   r   r   r   append)r)   r*   rJ   rL   resultrN   r8   r7   pqdr+   r+   r,   dup_sqf_list  s2    


rW   c             C   sl   t | ||d\}}|rP|d d dkrPt|d d ||}|dfg|dd  S t|g}|dfg| S dS )a  
    Return square-free decomposition of a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16

    >>> R.dup_sqf_list_include(f)
    [(2, 1), (x + 1, 2), (x + 2, 3)]
    >>> R.dup_sqf_list_include(f, all=True)
    [(2, 1), (x + 1, 2), (x + 2, 3)]

    )rJ   r   r(   N)rW   r   r   )r)   r*   rJ   rL   rM   r7   r+   r+   r,   dup_sqf_list_includeW  s    
rX   c             C   sB  |st | ||dS |jr(t| |||dS |jrHt| ||}t| ||} n4t| ||\}} |t| ||r|t| ||} | }t	| |dkr|g fS g d }}t
| d||}t| |||\}}	}
xzt
|	d||}t|
|||}t||r||	|f P t|	|||\}}	}
|s t	||dkr.|||f |d7 }qW ||fS )aZ  
    Return square-free decomposition of a polynomial in ``K[X]``.

    Examples
    ========

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

    >>> f = x**5 + 2*x**4*y + x**3*y**2

    >>> R.dmp_sqf_list(f)
    (1, [(x + y, 2), (x, 3)])
    >>> R.dmp_sqf_list(f, all=True)
    (1, [(1, 1), (x + y, 2), (x, 3)])

    )rJ   r   r(   )rW   rC   rQ   rE   r   r   r   rD   r   r
   r   r    r   r   rR   )r)   r.   r*   rJ   rL   rS   rN   r8   r7   rT   rU   rV   r+   r+   r,   dmp_sqf_lists  s6    

rY   c             C   s   |st | ||dS t| |||d\}}|rf|d d dkrft|d d |||}|dfg|dd  S t||}|dfg| S dS )ah  
    Return square-free decomposition of a polynomial in ``K[x]``.

    Examples
    ========

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

    >>> f = x**5 + 2*x**4*y + x**3*y**2

    >>> R.dmp_sqf_list_include(f)
    [(1, 1), (x + y, 2), (x, 3)]
    >>> R.dmp_sqf_list_include(f, all=True)
    [(1, 1), (x + y, 2), (x, 3)]

    )rJ   r   r(   N)rX   rY   r   r   )r)   r.   r*   rJ   rL   rM   r7   r+   r+   r,   dmp_sqf_list_include  s    
rZ   c             C   s   | st dt| |} t| s"g S t| t| |j||}t||}xBt|D ]6\}\}}t|t||| ||}||d f||< qLW t	| ||} t| s|S | dfg| S dS )z
    Compute greatest factorial factorization of ``f`` in ``K[x]``.

    Examples
    ========

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

    >>> R.dup_gff_list(x**5 + 2*x**4 - x**3 - 2*x**2)
    [(x, 1), (x + 2, 4)]

    zDgreatest factorial factorization doesn't exist for a zero polynomialr(   N)

ValueErrorr   r	   r!   r   r<   dup_gff_listrK   r   r   )r)   r*   r7   HrN   r8   rO   r+   r+   r,   r\     s    

r\   c             C   s   |st | |S t| dS )z
    Compute greatest factorial factorization of ``f`` in ``K[X]``.

    Examples
    ========

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

    N)r\   r&   )r)   r.   r*   r+   r+   r,   dmp_gff_list  s    
r^   N)F)F)F)F)F)F)?__doc__Z
__future__r   r   Zsympy.polys.densebasicr   r   r   r   r   r	   r
   r   r   r   Zsympy.polys.densearithr   r   r   r   r   r   r   r   r   Zsympy.polys.densetoolsr   r   r   r   r   r   r   r   Zsympy.polys.euclidtoolsr   r    r!   r"   r#   Zsympy.polys.galoistoolsr$   r%   Zsympy.polys.polyerrorsr&   r'   r-   r/   r;   r>   r?   r@   rB   rH   rI   rP   rQ   rW   rX   rY   rZ   r\   r^   r+   r+   r+   r,   <module>   s0   0	,(,2 #


9

<
%