
    MZd,                     >   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 Z,d	 Z-d
 Z.d Z/d Z0d Z1d Z2d Z3d Z4ddZ5ddZ6ddZ7ddZ8ddZ9ddZ:d Z;d Z<y)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           
      L    | sy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Ks     9/usr/lib/python3/dist-packages/sympy/polys/sqfreetools.py	dup_sqf_pr-   !   s*      ga!Q):A>???    c                 f    t        | |      ry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+   s      r,   	dmp_sqf_pr2   7   s7      !Qga!Q1)=q!DaHHHr.   c                 V   |j                   st        d      dt        |j                  j                  dd|j
                        }}	 t        | d|d      \  }}t        ||d|j
                        }t        ||j
                        rnt        | |j                   |      |dz   }} _|| |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front)is_Algebraicr'   r   modrepdomr   r#   r-   r   unit)r*   r+   sgh_rs          r,   dup_sqf_normrA   M   s    6 >>;<<i		1a/qA
!Q.1!Q155)QQ+QUqA  a7Nr.   c                    |st        | |      S |j                  st        d      t        |j                  j
                  |dz   d|j                        }t        |j                  |j                   g|d|      }d}	 t        | ||d      \  }}t        |||dz   |j                        }t        |||j                        rnt        | |||      |dz   }} Y|| |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

    r4   r)   r   Tr5   )rA   r7   r'   r   r8   r9   r:   oner;   r   r#   r2   r   )	r*   r1   r+   r=   Fr<   r>   r?   r@   s	            r,   dmp_sqf_normrE   y   s    6 Aq!!>>;<<!%%))QUAquu-A155166'"Aq!,A	A
!Q.1!QAquu-Q155!q!Q*AEqA  a7Nr.   c                     |j                   st        d      t        |j                  j                  |dz   d|j
                        }t        | ||d      \  }}t        |||dz   |j
                        S )zE
    Norm of ``f`` in ``K[X1, ..., Xn]``, often not square-free.
    r4   r)   r   Tr5   )r7   r'   r   r8   r9   r:   r   r#   )r*   r1   r+   r=   r>   r?   s         r,   dmp_normrG      sd     >>;<<!%%))QUAquu-AaAT*DAqAq1uaee,,r.   c                     t        | ||j                        } t        | |j                  |j                        }t        ||j                  |      S )z3Compute square-free part of ``f`` in ``GF(p)[x]``. )r   r:   r%   r8   )r*   r+   r=   s      r,   dup_gf_sqf_partrI      s=    Aq!%% AAquuaee$Aq!%%##r.   c                     t        d      )z3Compute square-free part of ``f`` in ``GF(p)[X]``. +multivariate polynomials over finite fieldsNotImplementedErrorr0   s      r,   dmp_gf_sqf_partrN          
K
LLr.   c                     |j                   rt        | |      S | s| S |j                  t        | |            rt	        | |      } t        | t        | d|      |      }t        | ||      }|j                  rt        ||      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_FiniteFieldrI   is_negativer   r   r!   r   r   is_Fieldr   r   )r*   r+   gcdsqfs       r,   dup_sqf_partrV      s     	q!$$}}VAq\"AqM
!XaA&
*C
!S!
Czza  S!$Q''r.   c                    |st        | |      S |j                  rt        | ||      S t        | |      r| S |j	                  t        | ||            rt        | ||      } | }t        |dz         D ]  }t        |t        | d|||      ||      } t        | |||      }|j                  rt        |||      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)   )rV   rQ   rN   r   rR   r   r   ranger"   r   r	   rS   r   r   )r*   r1   r+   rT   irU   s         r,   dmp_sqf_partrZ      s     Aq!!q!Q''!Q}}]1a+,Aq!
C1Q3Z =c;q!Q15q!<=
!S!Q
CzzQ**#CA.q11r.   c                    t        | ||j                        } t        | |j                  |j                  |      \  }}t	        |      D ]$  \  }\  } }t        | |j                  |      |f||<   & |j                  ||j                        |fS )z<Compute square-free decomposition of ``f`` in ``GF(p)[x]``. all)r   r:   r$   r8   	enumerateconvert)r*   r+   r]   coefffactorsrY   ks          r,   dup_gf_sqf_listrc   	  s    Aq!%% A AEE155c:NE7w' 3	6Aq!!QUUA.2
3 99UAEE"G++r.   c                     t        d      )z<Compute square-free decomposition of ``f`` in ``GF(p)[X]``. rK   rL   )r*   r1   r+   r]   s       r,   dmp_gf_sqf_listre     rO   r.   c                 8   |j                   rt        | ||      S |j                  rt        | |      }t	        | |      } n9t        | |      \  }} |j                  t        | |            rt        | |      } | }t        |       dk  r|g fS g d}}t        | d|      }t        | ||      \  }}}		 t        |d|      }
t        |	|
|      }|s|j                  ||f       	 ||fS t        |||      \  }}}	|st        |      dkD  r|j                  ||f       |dz  }n)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)])

    r\   r   r)   )rQ   rc   rS   r   r   r   rR   r   r   r   r   r   append)r*   r+   r]   r`   resultrY   r>   r=   pqds              r,   dup_sqf_listrl     s1   $ 	q!--zzq!aO A&q==1&1AFE!}byAAFAqAAq!$GAq!
Q1Aq!MM1a&! &=  1a(1a*Q-!#MM1a&!	Q r.   c                     t        | ||      \  }}|r)|d   d   dk(  rt        |d   d   ||      }|dfg|dd z   S t        |g      }|dfg|z   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)]

    r\   r   r)   N)rl   r
   r   )r*   r+   r]   r`   ra   r=   s         r,   dup_sqf_list_includern   S  sr    $ "!QC0NE771:a=A%71:a=%3Ax'!"+%%ugAx'!!r.   c                    |st        | ||      S |j                  rt        | |||      S |j                  rt	        | ||      }t        | ||      } n<t        | ||      \  }} |j                  t	        | ||            rt        | ||      } | }t        | |      dk  r|g fS g d}}t        | d||      }t        | |||      \  }}	}
	 t        |	d||      }t        |
|||      }t        ||      r|j                  |	|f       	 ||fS t        |	|||      \  }}	}
|st        ||      dkD  r|j                  ||f       |dz  }|)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)])

    r\   r   r)   )rl   rQ   re   rS   r   r   r   rR   r   r   r   r    r   r   rg   )r*   r1   r+   r]   r`   rh   rY   r>   r=   ri   rj   rk   s               r,   dmp_sqf_listrp   o  sj   $ Aqc**q!QC00zzaA&Q1%'1a0q==q!Q/01a AFE!Q1byAAFAq!AAq!Q'GAq!
Q1a Aq!QaMM1a&! &=  1a+1a*Q"Q&MM1a&!	Q r.   c                     |st        | ||      S t        | |||      \  }}|r*|d   d   dk(  rt        |d   d   |||      }|dfg|dd z   S t        ||      }|dfg|z   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)]

    r\   r   r)   N)rn   rp   r   r   )r*   r1   r+   r]   r`   ra   r=   s          r,   dmp_sqf_list_includerr     s    $ #Aqc22!!Qs3NE771:a=A%71:a=%A6Ax'!"+%%ua Ax'!!r.   c           
      l   | st        d      t        | |      } t        |       sg S t        | t	        | |j
                  |      |      }t        ||      }t        |      D ]1  \  }\  }}t        |t	        | ||       |      |      }||dz   f||<   3 t        | ||      } t        |       s|S | dfg|z   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   rC   dup_gff_listr^   r   r   )r*   r+   r=   HrY   r>   rb   s          r,   ru   ru     s     _``!QAa=	AyAEE1-q1A"1 	IAv19Q1q115Aq1u:AaD	 Aq!!}HF8a<r.   c                 4    |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)

    )ru   r&   r0   s      r,   dmp_gff_listrx     s     Aq!!)!,,r.   N)F)=__doc__sympy.polys.densearithr   r   r   r   r   r   r	   r
   r   sympy.polys.densebasicr   r   r   r   r   r   r   r   r   r   sympy.polys.densetoolsr   r   r   r   r   r   r   r   r   sympy.polys.euclidtoolsr   r    r!   r"   r#   sympy.polys.galoistoolsr$   r%   sympy.polys.polyerrorsr&   r'   r-   r2   rA   rE   rG   rI   rN   rV   rZ   rc   re   rl   rn   rp   rr   ru   rx    r.   r,   <module>r      s    >$ $ $  ) ) )
 @,I,)X/d
-$M
(@"2J	,M
6r"89x">" J-r.   