o
    8Va,                     @   s`  d 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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% ddl&m'Z'm(Z( ddl)m*Z*m+Z+ dd	 Z,d
d Z-dd Z.dd Z/dd Z0dd Z1dd Z2dd Z3dd Z4d,ddZ5d,ddZ6d,dd Z7d,d!d"Z8d,d#d$Z9d,d%d&Z:d'd( Z;d)d* Z<d+S )-z8Square-free decomposition algorithms and related tools.     )	dup_negdmp_negdup_subdmp_subdup_muldup_quodmp_quodup_mul_grounddmp_mul_ground)
	dup_stripdup_LCdmp_ground_LC
dmp_zero_p
dmp_ground
dup_degree
dmp_degree	dmp_raise
dmp_injectdup_convert)	dup_diffdmp_diffdmp_diff_in	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 )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   )r   r    r   )fK r*   9/usr/lib/python3/dist-packages/sympy/polys/sqfreetools.py	dup_sqf_p!   s   r,   c                 C   s.   t | |rdS tt| t| 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'   )r   r   r!   r   r(   ur)   r*   r*   r+   	dmp_sqf_p7   s   
 r/   c                 C   s   |j stddt|jjdd|j}}	 t| d|dd\}}t||d|j}t||jr.nt	| |j
 ||d } }q|| |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

    ground domain must be algebraicr   r'   TZfront)is_Algebraicr&   r   modrepdomr   r"   r,   r   unit)r(   r)   sgh_rr*   r*   r+   dup_sqf_normM   s   
	r<   c           	      C   s   |st | |S |jstdt|jj|d d|j}t|j|j g|d|}d}	 t	| ||dd\}}t
|||d |j}t|||jrEnt| ||||d } }q)|| |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

    r0   r'   r   Tr1   )r<   r2   r&   r   r3   r4   r5   oner6   r   r"   r/   r   )	r(   r.   r)   r8   Fr7   r9   r:   r;   r*   r*   r+   dmp_sqf_normy   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.
    r0   r'   r   Tr1   )r2   r&   r   r3   r4   r5   r   r"   )r(   r.   r)   r8   r9   r:   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   r5   r$   r3   )r(   r)   r8   r*   r*   r+   dup_gf_sqf_part   s   rA   c                 C      t d)z3Compute square-free part of ``f`` in ``GF(p)[X]``. +multivariate polynomials over finite fieldsNotImplementedErrorr-   r*   r*   r+   dmp_gf_sqf_part      rF   c                 C   sp   |j rt| |S | s| S |t| |rt| |} t| t| d||}t| ||}|jr1t	||S t
||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'   )is_FiniteFieldrA   is_negativer   r   r    r   r   is_Fieldr   r   )r(   r)   gcdsqfr*   r*   r+   dup_sqf_part   s   


rM   c              	   C   s   |st | |S |jrt| ||S t| |r| S |t| ||r&t| ||} | }t|d D ]}t|t	| d|||||}q.t
| |||}|jrNt|||S t|||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'   )rM   rH   rF   r   rI   r   r   ranger!   r   r   rJ   r   r   )r(   r.   r)   rK   irL   r*   r*   r+   dmp_sqf_part   s   

rP   Fc                 C   sd   t | ||j} t| |j|j|d\}}t|D ]\}\} }t | |j||f||< q|||j|fS )z<Compute square-free decomposition of ``f`` in ``GF(p)[x]``. all)r   r5   r#   r3   	enumerateZconvert)r(   r)   rR   coefffactorsrO   kr*   r*   r+   dup_gf_sqf_list	  s
   rW   c                 C   rB   )z<Compute square-free decomposition of ``f`` in ``GF(p)[X]``. rC   rD   )r(   r.   r)   rR   r*   r*   r+   dmp_gf_sqf_list  rG   rX   c                 C   s
  |j r
t| ||dS |jrt| |}t| |} nt| |\}} |t| |r/t| |} | }t| dkr9|g fS g d}}t	| d|}t
| ||\}}}		 t	|d|}
t|	|
|}|sh|||f 	 ||fS t
|||\}}}	|syt|dkr|||f |d7 }qN)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)])

    rQ   r   r'   )rH   rW   rJ   r   r   r   rI   r   r   r   r   r   append)r(   r)   rR   rT   resultrO   r9   r8   pqdr*   r*   r+   dup_sqf_list  s4   


	r^   c                 C   sh   t | ||d\}}|r(|d d dkr(t|d d ||}|dfg|dd  S t|g}|dfg| 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)]

    rQ   r   r'   N)r^   r	   r   )r(   r)   rR   rT   rU   r8   r*   r*   r+   dup_sqf_list_includeS  s   
r_   c                 C   s<  |s	t | ||dS |jrt| |||dS |jr$t| ||}t| ||} nt| ||\}} |t| ||r>t| ||} | }t	| |dkrI|g fS g d}}t
| d||}t| |||\}}	}
	 t
|	d||}t|
|||}t||r||	|f 	 ||fS t|	|||\}}	}
|st	||dkr|||f |d7 }q`)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)])

    rQ   r   r'   )r^   rH   rX   rJ   r   r   r   rI   r   r   r   r   r   r   rY   )r(   r.   r)   rR   rT   rZ   rO   r9   r8   r[   r\   r]   r*   r*   r+   dmp_sqf_listo  s8   

	r`   c                 C   s~   |s	t | ||dS t| |||d\}}|r3|d d dkr3t|d d |||}|dfg|dd  S t||}|dfg| 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)]

    rQ   r   r'   N)r_   r`   r
   r   )r(   r.   r)   rR   rT   rU   r8   r*   r*   r+   dmp_sqf_list_include  s   
ra   c                 C   s   | st dt| |} t| sg S t| t| |j||}t||}t|D ]\}\}}t|t||| ||}||d f||< q%t	| ||} t| sM|S | dfg| 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'   )

ValueErrorr   r   r    r   r=   dup_gff_listrS   r   r   )r(   r)   r8   HrO   r9   rV   r*   r*   r+   rc     s   

rc   c                 C   s   |st | |S t| )z
    Compute greatest factorial factorization of ``f`` in ``K[X]``.

    Examples
    ========

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

    )rc   r%   r-   r*   r*   r+   dmp_gff_list  s   
re   N)F)=__doc__Zsympy.polys.densearithr   r   r   r   r   r   r   r	   r
   Zsympy.polys.densebasicr   r   r   r   r   r   r   r   r   r   Zsympy.polys.densetoolsr   r   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@   rA   rF   rM   rP   rW   rX   r^   r_   r`   ra   rc   re   r*   r*   r*   r+   <module>   s0    ,0,,2 
%


9

<%