ó
¡¼™\c           @  sß  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 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! 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 „  Z0 d „  Z1 d „  Z2 d „  Z3 d „  Z4 d „  Z5 d „  Z6 e7 d „ Z8 e7 d „ Z9 e7 d „ Z: e7 d „ Z; e7 d „ Z< e7 d „ Z= d „  Z> d „  Z? d S(   s8   Square-free decomposition algorithms and related tools. iÿÿÿÿ(   t   print_functiont   division(	   t   dup_negt   dmp_negt   dup_subt   dmp_subt   dup_mult   dup_quot   dmp_quot   dup_mul_groundt   dmp_mul_ground(
   t	   dup_stript   dup_LCt   dmp_ground_LCt
   dmp_zero_pt
   dmp_groundt
   dup_degreet
   dmp_degreet	   dmp_raiset
   dmp_injectt   dup_convert(   t   dup_difft   dmp_difft	   dup_shiftt   dmp_composet	   dup_monict   dmp_ground_monict   dup_primitivet   dmp_ground_primitive(   t   dup_inner_gcdt   dmp_inner_gcdt   dup_gcdt   dmp_gcdt   dmp_resultant(   t   gf_sqf_listt   gf_sqf_part(   t   MultivariatePolynomialErrort   DomainErrorc         C  s1   |  s
 t  St t |  t |  d | ƒ | ƒ ƒ Sd S(   s  
    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

    i   N(   t   TrueR   R   R   (   t   ft   K(    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt	   dup_sqf_p"   s    c         C  sC   t  |  | ƒ r t St t |  t |  d | | ƒ | | ƒ | ƒ Sd S(   s  
    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

    i   N(   R   R&   R   R    R   (   R'   t   uR(   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt	   dmp_sqf_p8   s    c         C  sÄ   | j  s t d ƒ ‚ n  d t | j j d d | j ƒ } } xw t r¶ t |  d | d t ƒ\ } } t | | d | j ƒ } t	 | | j ƒ r’ Pq@ t
 |  | j | ƒ | d }  } q@ W| |  | f S(   sl  
    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

    s   ground domain must be algebraici    i   t   front(   t   is_AlgebraicR%   R   t   modt   rept   domR&   R   R!   R)   R   t   unit(   R'   R(   t   st   gt   ht   _t   r(    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dup_sqf_normN   s    	%	%c   	      C  s  | s t  |  | ƒ S| j s+ t d ƒ ‚ n  t | j j | d d | j ƒ } t | j | j g | d | ƒ } d } x} t	 rô t
 |  | | d t	 ƒ\ } } t | | | d | j ƒ } t | | | j ƒ rÑ Pqx t |  | | | ƒ | d }  } qx W| |  | f S(   s  
    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

    s   ground domain must be algebraici   i    R,   (   R7   R-   R%   R   R.   R/   R0   t   oneR1   R&   R   R!   R+   R   (	   R'   R*   R(   R3   t   FR2   R4   R5   R6   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dmp_sqf_normz   s    	""	$c         C  sr   | j  s t d ƒ ‚ n  t | j j | d d | j ƒ } t |  | | d t ƒ\ } } t | | | d | j ƒ S(   sE   
    Norm of ``f`` in ``K[X1, ..., Xn]``, often not square-free.
    s   ground domain must be algebraici   i    R,   (	   R-   R%   R   R.   R/   R0   R   R&   R!   (   R'   R*   R(   R3   R4   R5   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dmp_norm¬   s
    	"c         C  s@   t  |  | | j ƒ }  t |  | j | j ƒ } t  | | j | ƒ S(   s3   Compute square-free part of ``f`` in ``GF(p)[x]``. (   R   R0   R#   R.   (   R'   R(   R3   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dup_gf_sqf_part¹   s    c         C  s   t  d ƒ ‚ d S(   s3   Compute square-free part of ``f`` in ``GF(p)[X]``. s+   multivariate polynomials over finite fieldsN(   t   NotImplementedError(   R'   R*   R(   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dmp_gf_sqf_partÀ   s    c         C  s¥   | j  r t |  | ƒ S|  s  |  S| j t |  | ƒ ƒ rJ t |  | ƒ }  n  t |  t |  d | ƒ | ƒ } t |  | | ƒ } | j r t	 | | ƒ St
 | | ƒ d Sd S(   sÛ   
    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

    i   N(   t   is_FiniteFieldR<   t   is_negativeR   R   R   R   R   t   is_FieldR   R   (   R'   R(   t   gcdt   sqf(    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dup_sqf_partÅ   s    		c         C  sÙ   | s t  |  | ƒ S| j r, t |  | | ƒ St |  | ƒ r? |  S| j t |  | | ƒ ƒ ro t |  | | ƒ }  n  t |  t |  d | | ƒ | | ƒ } t	 |  | | | ƒ } | j
 rÁ t | | | ƒ St | | | ƒ d Sd S(   sç   
    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

    i   N(   RD   R?   R>   R   R@   R   R   R    R   R   RA   R   R   (   R'   R*   R(   RB   RC   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dmp_sqf_partå   s    	$	c         C  s”   t  |  | | j ƒ }  t |  | j | j d | ƒ\ } } x? t | ƒ D]1 \ } \ }  } t  |  | j | ƒ | f | | <qF W| j | | j ƒ | f S(   s<   Compute square-free decomposition of ``f`` in ``GF(p)[x]``. t   all(   R   R0   R"   R.   t	   enumeratet   convert(   R'   R(   RF   t   coefft   factorst   it   k(    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dup_gf_sqf_list  s
    $#c         C  s   t  d ƒ ‚ d S(   s<   Compute square-free decomposition of ``f`` in ``GF(p)[X]``. s+   multivariate polynomials over finite fieldsN(   R=   (   R'   R*   R(   RF   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dmp_gf_sqf_list  s    c         C  s  | j  r t |  | d | ƒS| j rF t |  | ƒ } t |  | ƒ }  nF t |  | ƒ \ } }  | j t |  | ƒ ƒ rŒ t |  | ƒ }  | } n  t |  ƒ d k r¨ | g  f Sg  d } } t	 |  d | ƒ } t
 |  | | ƒ \ } } }	 xž t r‚t	 | d | ƒ }
 t |	 |
 | ƒ } | s,| j | | f ƒ Pn  t
 | | | ƒ \ } } }	 | s_t | ƒ d k ru| j | | f ƒ n  | d 7} qå W| | f S(   ss  
    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)])

    RF   i    i   (   R?   RM   RA   R   R   R   R@   R   R   R   R   R&   R   t   append(   R'   R(   RF   RI   t   resultRK   R4   R3   t   pt   qt   d(    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dup_sqf_list  s2    		

	c         C  sˆ   t  |  | d | ƒ\ } } | rd | d d d k rd t | d d | | ƒ } | d f g | d St | g ƒ } | d f g | Sd S(   s  
    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)]

    RF   i    i   N(   RT   R	   R   (   R'   R(   RF   RI   RJ   R3   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dup_sqf_list_includeR  s    c         C  sÖ  | s t  |  | d | ƒS| j r8 t |  | | d | ƒS| j rh t |  | | ƒ } t |  | | ƒ }  nO t |  | | ƒ \ } }  | j t |  | | ƒ ƒ r· t |  | | ƒ }  | } n  t	 |  | ƒ d k rÖ | g  f Sg  d } } t
 |  d | | ƒ } t |  | | | ƒ \ } }	 }
 x³ t rËt
 |	 d | | ƒ } t |
 | | | ƒ } t | | ƒ ro| j |	 | f ƒ Pn  t |	 | | | ƒ \ } }	 }
 | s¨t	 | | ƒ d k r¾| j | | f ƒ n  | d 7} qW| | f S(   sZ  
    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)])

    RF   i    i   (   RT   R?   RN   RA   R   R   R   R@   R   R   R   R   R&   R   R   RO   (   R'   R*   R(   RF   RI   RP   RK   R4   R3   RQ   RR   RS   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dmp_sqf_listn  s6    		

	c         C  s§   | s t  |  | d | ƒSt |  | | d | ƒ\ } } | rƒ | d d d k rƒ t | d d | | | ƒ } | d f g | d St | | ƒ } | d f g | Sd S(   sh  
    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)]

    RF   i    i   N(   RU   RV   R
   R   (   R'   R*   R(   RF   RI   RJ   R3   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dmp_sqf_list_includeª  s    c         C  s÷   |  s t  d ƒ ‚ n  t |  | ƒ }  t |  ƒ s4 g  St |  t |  | j | ƒ | ƒ } t | | ƒ } xY t | ƒ D]K \ } \ } } t | t | | | ƒ | ƒ | ƒ } | | d f | | <qq Wt	 |  | | ƒ }  t |  ƒ sâ | S|  d f g | Sd S(   sû   
    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)]

    sD   greatest factorial factorization doesn't exist for a zero polynomiali   N(
   t
   ValueErrorR   R   R   R   R8   t   dup_gff_listRG   R   R   (   R'   R(   R3   t   HRK   R4   RL   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyRY   É  s    !%c         C  s#   | s t  |  | ƒ St |  ƒ ‚ d S(   s¯   
    Compute greatest factorial factorization of ``f`` in ``K[X]``.

    Examples
    ========

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

    N(   RY   R$   (   R'   R*   R(   (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   dmp_gff_listî  s    N(@   t   __doc__t
   __future__R    R   t   sympy.polys.densearithR   R   R   R   R   R   R   R	   R
   t   sympy.polys.densebasicR   R   R   R   R   R   R   R   R   R   t   sympy.polys.densetoolsR   R   R   R   R   R   R   R   t   sympy.polys.euclidtoolsR   R   R   R    R!   t   sympy.polys.galoistoolsR"   R#   t   sympy.polys.polyerrorsR$   R%   R)   R+   R7   R:   R;   R<   R>   RD   RE   t   FalseRM   RN   RT   RU   RV   RW   RY   R[   (    (    (    s6   lib/python2.7/site-packages/sympy/polys/sqfreetools.pyt   <module>   s0   @F:(			,	2				 	#9<	%