
~9\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 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# 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/ m0 Z0 m1 Z1 m2 Z2 m3 Z3 m4 Z4 m5 Z5 m6 Z6 m7 Z7 m8 Z8 m9 Z9 m: Z: m; Z; m< Z< m= Z= m> Z> m? Z? m@ Z@ mA ZA mB ZB mC ZC d d lD mE ZE mF ZF mG ZG mH ZH mI ZI mJ ZJ mK ZK mL ZL mM ZM mN ZN mO ZO mP ZP mQ ZQ mR ZR mS ZS d d lT mU ZU mV ZV mW ZW d d lX mY ZY mZ ZZ m[ Z[ m\ Z\ m] Z] d d	 l^ m_ Z_ d d
 l` ma Za d d lb mc Zc md Zd me Ze mf Zf d d lg mh Zh mi Zi mj Zj d d lk ml Zl d d lm mn Zo mp Zq d d lr ms Zs d   Zt d   Zu d   Zv d   Zw d   Zx d   Zy d   Zz d   Z{ d   Z| e} d  Z~ d   Z d   Z d   Z d   Z d   Z d   Z d    Z d!   Z d"   Z d#   Z d$   Z d1 d1 d%  Z d&   Z d'   Z d(   Z d)   Z d*   Z d+   Z d,   Z d-   Z d.   Z d/   Z d0   Z d1 S(2   s:   Polynomial factorization routines in characteristic zero. i(   t   print_functiont   division(   t   gf_from_int_polyt   gf_to_int_polyt	   gf_lshiftt
   gf_add_mult   gf_mult   gf_divt   gf_remt   gf_gcdext   gf_sqf_pt   gf_factor_sqft	   gf_factor(   t   dup_LCt   dmp_LCt   dmp_ground_LCt   dup_TCt   dup_convertt   dmp_convertt
   dup_degreet
   dmp_degreet   dmp_degree_int   dmp_degree_listt   dmp_from_dictt
   dmp_zero_pt   dmp_onet   dmp_nestt	   dmp_raiset	   dup_stript
   dmp_groundt   dup_inflatet   dmp_excludet   dmp_includet
   dmp_injectt	   dmp_ejectt   dup_terms_gcdt   dmp_terms_gcd(   t   dup_negt   dmp_negt   dup_addt   dmp_addt   dup_subt   dmp_subt   dup_mult   dmp_mult   dup_sqrt   dmp_powt   dup_divt   dmp_divt   dup_quot   dmp_quot
   dmp_expandt   dmp_add_mult   dup_sub_mult   dmp_sub_mult
   dup_lshiftt   dup_max_normt   dmp_max_normt   dup_l1_normt   dup_mul_groundt   dmp_mul_groundt   dup_quo_groundt   dmp_quo_ground(   t   dup_clear_denomst   dmp_clear_denomst	   dup_trunct   dmp_ground_trunct   dup_contentt	   dup_monict   dmp_ground_monict   dup_primitivet   dmp_ground_primitivet   dmp_eval_tailt   dmp_eval_int   dmp_diff_eval_int   dmp_composet	   dup_shiftt
   dup_mirror(   t   dmp_primitivet   dup_inner_gcdt   dmp_inner_gcd(   t	   dup_sqf_pt   dup_sqf_normt   dmp_sqf_normt   dup_sqf_partt   dmp_sqf_part(   t   _sort_factors(   t   query(   t   ExtraneousFactorst   DomainErrort   CoercionFailedt   EvaluationFailed(   t	   nextprimet   isprimet	   factorint(   t   subsets(   t   ceilt   log(   t   rangec         C  sz   g  } xg | D]_ } d } x= t  rX t |  | |  \ } } | sT | | d }  } q Pq W| j | | f  q Wt |  S(   s:   Determine multiplicities of factors using trial division. i    i   (   t   TrueR/   t   appendRV   (   t   ft   factorst   Kt   resultt   factort   kt   qt   r(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_trial_divisionR   s    	c   	      C  s   g  } xs | D]k } d } xI t  rd t |  | | |  \ } } t | |  r` | | d }  } q Pq W| j | | f  q Wt |  S(   s:   Determine multiplicities of factors using trial division. i    i   (   Rc   R0   R   Rd   RV   (	   Re   Rf   t   uRg   Rh   Ri   Rj   Rk   Rl   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dmp_trial_divisionf   s    	c         C  sW   t  |  |  } t t |  |   } t |   } | j | | d   d | | | S(   s5   Mignotte bound for univariate polynomials in `K[x]`. i   i   (   R8   t   absR   R   t   sqrt(   Re   Rg   t   at   bt   n(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_mignotte_boundz   s    c         C  sf   t  |  | |  } t t |  | |   } t t |  |   } | j | | d   d | | | S(   s7   Mignotte bound for multivariate polynomials in `K[X]`. i   i   (   R9   Rp   R   t   sumR   Rq   (   Re   Rn   Rg   Rr   Rs   Rt   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dmp_zz_mignotte_bound   s    c         C  s  |  d } t  | | | |  } t | | |  } t t | | |  | |  \ }	 }
 t |	 | |  }	 t |
 | |  }
 t t | | |  t |	 | |  |  } t t | | |  | |  } t t | |
 |  | |  } t t | | |  t | | |  |  } t t | | j g |  | |  } t t | | |  | |  \ } } t | | |  } t | | |  } t t | | |  t | | |  |  } t t | | |  | |  } t t | | |  | |  } | | | | f S(   s  
    One step in Hensel lifting in `Z[x]`.

    Given positive integer `m` and `Z[x]` polynomials `f`, `g`, `h`, `s`
    and `t` such that::

        f == g*h (mod m)
        s*g + t*h == 1 (mod m)

        lc(f) is not a zero divisor (mod m)
        lc(h) == 1

        deg(f) == deg(g) + deg(h)
        deg(s) < deg(h)
        deg(t) < deg(g)

    returns polynomials `G`, `H`, `S` and `T`, such that::

        f == G*H (mod m**2)
        S*G + T**H == 1 (mod m**2)

    References
    ==========

    .. [1] [Gathen99]_

    i   (   R5   RA   R/   R+   R'   R)   t   one(   t   mRe   t   gt   ht   st   tRg   t   Mt   eRk   Rl   Rn   t   Gt   HRs   t   ct   dt   St   T(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_hensel_step   s$    
$**$$*c      	   C  s  t  |  } t | |  } | d k rd t | | j | |  |  d |  } t | |  | |  g S|  } | d }	 t t t | d    }
 t | g |   } x0 | |	  D]$ } t	 | t | |   |  |  } q Wt | |	 |   } x4 | |	 d D]$ } t	 | t | |   |  |  } q Wt
 | | |  |  \ } } } t | |   } t | |   } t | |   } t | |   } xP t d |
 d  D]; } t | | | | | | |  | d \ } } } } } qWt |  | | |	  | |  t |  | | |	 | |  S(   s  
    Multifactor Hensel lifting in `Z[x]`.

    Given a prime `p`, polynomial `f` over `Z[x]` such that `lc(f)`
    is a unit modulo `p`, monic pair-wise coprime polynomials `f_i`
    over `Z[x]` satisfying::

        f = lc(f) f_1 ... f_r (mod p)

    and a positive integer `l`, returns a list of monic polynomials
    `F_1`, `F_2`, ..., `F_r` satisfying::

       f = lc(f) F_1 ... F_r (mod p**l)

       F_i = f_i (mod p), i = 1..r

    References
    ==========

    .. [1] [Gathen99]_

    i   i    i   (   t   lenR   R;   t   gcdexRA   t   intt   _ceilt   _logR   R   R	   R   Rb   R   t   dup_zz_hensel_lift(   t   pRe   t   f_listt   lRg   Rl   t   lct   FRy   Rj   R   Rz   t   f_iR{   R|   R}   t   _(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR      s.    &
""9c         C  s5   | | d k r | | } n  | s' t  S|  | d k S(   Ni   i    (   Rc   (   t   fcRk   t   pl(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   _test_pl   s
    c   !      C  s  t  |   } | d k r |  g S|  d } t |  |  } t |  |  } t t | j | | d   d | | |   } t | d d | | d | d  } t t d t | d    } t d | t |   }	 g  }
 x t d |	 d  D] } t	 |  s | | d k r q n  | j
 |  } t |  |  } t | | |  sVq n  t | | |  d } |
 j | | f  t |  d k  st |
  d k r Pq q Wt |
 d d	   \ } } t t t d | d |    } g  | D] } t | |  ^ q} t | |  | | |  } t t |   } t |  } g  d } } | | } x[d | t |  k rx>t | |  D]#} | d k rd } x  | D] } | | | d } qW| | } t | | |  s\qq\n{ | g } x$ | D] } t | | | |  } qWt | | |  } t | |  d } | d } | r\| | d k r\qn  | g } t |  } | | } | d k r| g } x$ | D] } t | | | |  } qWt | | |  } n  x$ | D] } t | | | |  } qWt | | |  } t | |  } t | |  }  | |  | k r| } g  | D] } | | k r@| ^ q@} t | |  d } t | |  d }  | j |  t |  |  } PqqW| d 7} q[W| |  g S(
   s4   Factor primitive square-free polynomials in `Z[x]`. i   ii   i   i    i   i   t   keyc         S  s   t  |  d  S(   Ni   (   R   (   t   x(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   <lambda>%  t    (   R   R8   R   R   Rp   Rq   R   R   Rb   R]   t   convertR   R
   R   Rd   R   t   minR   R   t   setR_   R   R+   RA   RF   R:   (!   Re   Rg   Rt   R   t   ARs   t   Bt   Ct   gammat   boundRr   t   pxR   t   fsqfxR   t   fsqfR   t   fft   modularRz   t   sorted_TR   Rf   R|   R   R   Rk   t   iR   R   t   T_St   G_normt   H_norm(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_zassenhaus  s    
5($#"

		
	
	%c         C  s   t  |  |  } t |  |  } t |  d |  } | r t t |   } x3 | j   D]" } | | rV | | d rV t SqV Wn  d S(   s2   Test irreducibility using Eisenstein's criterion. i   i   N(   R   R   RC   R^   R   t   keysRc   (   Re   Rg   R   t   tct   e_fct   e_ffR   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_irreducible_pl  s    c         C  sw  | j  rJ y) | | j   } } t |  | |  }  WqW t k
 rF t SXn | j sW t St |  |  } t |  |  } | d k s | d k r | d k r t S| s t |  |  \ } } | | j	 k s | |  d f g k r t Sn  t
 |   } g  g  }	 }
 x. t | d d  D] } |	 j d |  |  qWx2 t | d d d  D] } |
 j d |  |  qDWt t |	  |  }	 t t |
  |  }
 t |	 t |
 d |  |  } | j t | |   rt | |  } n  | |  k rt St |  |  }	 | j t |	 |   rt |	 |  }	 n  | |	 k r<t |	 |  r<t St | |  } t | |  | k rst | |  rst St S(   sh  
    Efficiently test if ``f`` is a cyclotomic polnomial.

    Examples
    ========

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

    >>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1
    >>> R.dup_cyclotomic_p(f)
    False

    >>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1
    >>> R.dup_cyclotomic_p(g)
    True

    i   iii    (   t   is_QQt   get_ringR   RZ   t   Falset   is_ZZR   R   t   dup_factor_listRx   R   Rb   t   insertR-   R   R)   R7   t   is_negativeR%   Rc   RM   t   dup_cyclotomic_pRT   (   Re   Rg   t   irreduciblet   K0R   R   t   coeffRf   Rt   Rz   R{   R   R   R   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR   {  sL    		$$$c         C  sr   | j  | j  g } xX t |   j   D]D \ } } t t | | |  | |  } t | | | d |  } q& W| S(   s0   Efficiently generate n-th cyclotomic polnomial. i   (   Rx   R^   t   itemsR1   R   (   Rt   Rg   R{   R   Rj   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_cyclotomic_poly  s
    c   	      C  s   | j  | j  g g } x t |   j   D] \ } } g  | D]$ } t t | | |  | |  ^ q< } | j |  xI t d |  D]8 } g  | D] } t | | |  ^ q } | j |  q Wq) W| S(   Ni   (   Rx   R^   R   R1   R   t   extendRb   (	   Rt   Rg   R   R   Rj   R{   t   QR   Rk   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   _dup_cyclotomic_decompose  s    1%c         C  s   t  |  |  t |  |  } } t |   d k r5 d S| d k sM | d k rQ d St d   |  d d !D  rr d St |   } t | |  } | j |  s | Sg  } x7 t d | |  D]" } | | k r | j |  q q W| Sd S(   s  
    Efficiently factor polynomials `x**n - 1` and `x**n + 1` in `Z[x]`.

    Given a univariate polynomial `f` in `Z[x]` returns a list of factors
    of `f`, provided that `f` is in the form `x**n - 1` or `x**n + 1` for
    `n >= 1`. Otherwise returns None.

    Factorization is performed using using cyclotomic decomposition of `f`,
    which makes this method much faster that any other direct factorization
    approach (e.g. Zassenhaus's).

    References
    ==========

    .. [1] [Weisstein09]_

    i    i   ic         s  s   |  ] } t  |  Vq d  S(   N(   t   bool(   t   .0t   cf(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pys	   <genexpr>  s    i   N(   ii   (   R   R   R   t   Nonet   anyR   t   is_oneRd   (   Re   Rg   t   lc_ft   tc_fRt   R   R   R{   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_cyclotomic_factor  s     c         C  s  t  |  |  \ } } t |  } t | |  d k  rP | t | |  } } n  | d k rf | g  f S| d k r | | g f St d  r t | |  r | | g f Sn  d } t d  r t | |  } n  | d k r t | |  } n  | t	 | d t
 f S(   s9   Factor square-free (non-primitive) polyomials in `Z[x]`. i    i   t   USE_IRREDUCIBLE_IN_FACTORt   USE_CYCLOTOMIC_FACTORt   multipleN(   RF   R   R   R%   RW   R   R   R   R   RV   R   (   Re   Rg   t   contRz   Rt   Rf   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_factor_sqf  s"    
c         C  s#  t  |  |  \ } } t |  } t | |  d k  rP | t | |  } } n  | d k rf | g  f S| d k r | | d f g f St d  r t | |  r | | d f g f Sn  t | |  } d } t d  r t | |  } n  | d k rt	 | |  } n  t
 |  | |  } | | f S(   s  
    Factor (non square-free) polynomials in `Z[x]`.

    Given a univariate polynomial `f` in `Z[x]` computes its complete
    factorization `f_1, ..., f_n` into irreducibles over integers::

                f = content(f) f_1**k_1 ... f_n**k_n

    The factorization is computed by reducing the input polynomial
    into a primitive square-free polynomial and factoring it using
    Zassenhaus algorithm. Trial division is used to recover the
    multiplicities of factors.

    The result is returned as a tuple consisting of::

              (content(f), [(f_1, k_1), ..., (f_n, k_n))

    Examples
    ========

    Consider the polynomial `f = 2*x**4 - 2`::

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

        >>> R.dup_zz_factor(2*x**4 - 2)
        (2, [(x - 1, 1), (x + 1, 1), (x**2 + 1, 1)])

    In result we got the following factorization::

                 f = 2 (x - 1) (x + 1) (x**2 + 1)

    Note that this is a complete factorization over integers,
    however over Gaussian integers we can factor the last term.

    By default, polynomials `x**n - 1` and `x**n + 1` are factored
    using cyclotomic decomposition to speedup computations. To
    disable this behaviour set cyclotomic=False.

    References
    ==========

    .. [1] [Gathen99]_

    i    i   R   R   N(   RF   R   R   R%   RW   R   RT   R   R   R   Rm   (   Re   Rg   R   Rz   Rt   R   Rf   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_factor)  s&    .
c         C  s   | | g } x |  D]x } t  |  } xV t |  D]H } x, | d k rg | j | |  } | | } q< W| j |  r3 d Sq3 W| j |  q W| d S(   s,   Wang/EEZ: Compute a set of valid divisors.  i   N(   Rp   t   reversedt   gcdR   R   Rd   (   t   Et   cst   ctRg   Rh   Rk   Rl   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dmp_zz_wang_non_divisorst  s    c         C  s!  t  t |  |  | | d |  s1 t d   n  t  |  | | |  } t | |  sd t d   n  t | |  \ } } | j t | |   r | t | |  } } n  | d }	 g  | D]! \ }
 } t  |
 | |	 |  ^ q } t | | | |  } | d k	 r| | | f St d   d S(   s2   Wang/EEZ: Test evaluation points for suitability. i   s   no luckN(
   RH   R   R[   RQ   RF   R   R   R%   R   R   (   Re   R   R   R   Rn   Rg   Rz   R   R{   t   vR}   R   R   t   D(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dmp_zz_wang_test_points  s    "
.c      	   C  s  g  d g t  |  | d } }	 }
 x | D] } t |
 |  } t | |  | } x t t t  |    D] } d | | | | } } \ } } x# | | s | | | d } } q W| d k rm t | t | | |
 |  |
 |  d } |	 | <qm qm W| j |  q, Wt d   |	 D  r-t	  n  g  g  } } x t
 | |  D] \ } } t | | |
 |  } t | |  } | j |  r| | } nD | j | |  } | | | | } } t | | |  | | } } t | | |
 |  } | j |  | j |  qJW| j |  r)|  | | f Sg  g  } } xU t
 | |  D]D \ } } | j t | | |
 |   | j t | | d |   qFWt |  | t  |  d | |  }  |  | | f S(   s0   Wang/EEZ: Compute correct leading coefficients. i    i   c         s  s   |  ] } | Vq d  S(   N(    (   R   t   j(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pys	   <genexpr>  s    (   R   R   R   R   Rb   R,   R.   Rd   R   RX   t   zipRH   R   R   R;   R<   (   Re   R   R   R   R   R   Rn   Rg   R   t   JR   R{   R   R   R   Rj   R   R}   R   t   CCt   HHR   t   ccRz   t   CCCt   HHH(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dmp_zz_wang_lead_coeffs  sB    %"6	 #c      	   C  s;  t  |   d k r |  \ } } t | |  } t | |  } t | | | |  \ } }	 }
 t | | |  } t |	 | |  }	 t | | | |  \ } } t |	 | | | |  }	 t | |  } t |	 |  }	 | |	 g } nY|  d g }
 x; t |  d d ! D]& } |
 j d t	 | |
 d |   q Wg  d g g } } xe t
 |  |
  D]T \ } } t | | g | d g  d | d |  \ }	 } | j |	  | j |  qLWg  | | d g } } xx t
 | |   D]g \ } } t | |  } t | |  } t t | | |  | | |  } t | |  } | j |  qW| S(   s2   Wang/EEZ: Solve univariate Diophantine equations. i   ii   i    (   R   R   R	   R   R   R   R   R   R   R+   R   t   dmp_zz_diophantineRd   R   (   R   Ry   R   Rg   Rr   Rs   Re   Rz   R|   R}   R   Rk   Rh   R   R   Rl   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_zz_diophantine  s8    $.!c      	   C  s  | s g  |  D] } g  ^ q } t  |  }	 xt |  D] \ }
 } | sP q8 n  t |  |	 |
 | |  } x] t t | |   D]F \ } \ } } t | | |  } t t | | |  | |  | | <q Wq8 Wn't |  }	 t |  | |  } | d | d  } } g  g  } } xI |  D]A } | j	 t
 | | | |   | j	 t | | |	 | |   qWt | | |	 | |  } | d } t | | | | | | |  } g  | D] } t | d | |  ^ q} x5 t | |  D]$ \ } } t | | | | |  } qWt | | | |  } t | j | g |	 |  } t |	 |  } x| j t d |   D]v} t | |  rnPn  t | | | |  } t | | d | |	 | |  } t | |  sUt | | j | d  | |  } t | | | | | | |  } xB t |  D]4 \ }
 } t t | d | |  | | |  | |
 <qWxB t t | |   D]+ \ }
 \ } } t | | | |  | |
 <qLWx5 t | |  D]$ \ } } t | | | | |  } qWt | | | |  } qUqUWg  | D] } t | | | |  ^ q} | S(   s4   Wang/EEZ: Solve multivariate Diophantine equations. ii   i    (   R   t	   enumerateR   R   R;   RA   R'   R   R3   Rd   R2   RI   R   R   R6   RB   R   Rx   R   t   mapRb   R   R,   RJ   R>   t	   factorialR(   (   R   R   R   R   R   Rn   Rg   R   R   Rt   R   R   R   R   R|   R}   R   Rr   R   R   Re   R   R   Rs   Ry   R~   Rj   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR     sV    (-#
(",((c      
   C  s  |  g t  |  | d } } }	 t |  } xk t t | d   D]S \ }
 } t | d | | |
 | |
 |  } | j d t | | |	 |
 |   qD Wt t |  |  d  } xt	 t
 d | d  | |  D]\ } } } t |  | d } } | | d  | | d } } xy t t	 | |   D]b \ }
 \ } } t t | | |	 |  | | d |  } | g t | d d | d |  | |
 <q-Wt | j | g | |  } t | |  } t | t | | |  | |  } t | | |  } xk| j t
 d |   D]Q} t | |  r#Pn  t | | | |  } t | | d | | | |  } t | | d  s
t | | j | d  | d |  } t | | | | | | d |  } xm t t	 | |   D]V \ }
 \ } } t | t | d | d |  | | |  } t | | | |  | |
 <qWt | t | | |  | |  } t | | | |  } q
q
Wq Wt | | |  |  k rt  n | Sd S(   s-   Wang/EEZ: Parallel Hensel lifting algorithm. i   i    i   N(   R   t   listR   R   RI   R   RB   t   maxR   R   Rb   RH   R   R   Rx   R   R*   R3   R   R   R   R,   RJ   R>   R   R   R4   RX   (   Re   R   t   LCR   R   Rn   Rg   R   Rt   R   R   Rr   R|   R   R   R   t   wt   IR   R{   R   Ry   R~   R   t   djRj   R   R   R}   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dmp_zz_wang_hensel_liftingL  s@    !#$'/((,!&"(+! 	c       	   C  s{  d d l  m } | |  } t t |  |  | d |  \ } } t |  | |  }	 | t |	   }
 | d k r | d k r d } q d } n  t g   g  | j g | d f \ } } } } yt t	 |  | | | | |  \ } } } t
 | |  \ } } t |  } | d k r|  g S| | | | | f g } Wn t k
 rDn Xt d  } t d  } t d  } xyt |  | k  rx`t |  D]H} g  t |  D] } | | | |   ^ q} t |  | k r| j t |   n qy( t	 |  | | | | |  \ } } } Wn t k
 r)qn Xt
 | |  \ } } t |  } | d k	 r| | k r| | k  rg  | } } qqqn | } | d k r|  g S| j | | | | | f  t |  | k rPqqW| | 7} qlWd \ } } } xf | D]^ \ } } } } } t | |  } | d k	 rI| | k  rO| } | } qOn | } | d 7} qW| | \ } } } } } |  } yL t |  | | | | | | |  \ }  } } t |  | | | |
 | |  } Wn@ t k
 r
t d	  rt | | | | d  St d
   n Xg  } xc | D][ }  t |  | |  \ } }  | j t |  | |   rft |  | |  }  n  | j |   qW| S(   s^  
    Factor primitive square-free polynomials in `Z[X]`.

    Given a multivariate polynomial `f` in `Z[x_1,...,x_n]`, which is
    primitive and square-free in `x_1`, computes factorization of `f` into
    irreducibles over integers.

    The procedure is based on Wang's Enhanced Extended Zassenhaus
    algorithm. The algorithm works by viewing `f` as a univariate polynomial
    in `Z[x_2,...,x_n][x_1]`, for which an evaluation mapping is computed::

                      x_2 -> a_2, ..., x_n -> a_n

    where `a_i`, for `i = 2, ..., n`, are carefully chosen integers.  The
    mapping is used to transform `f` into a univariate polynomial in `Z[x_1]`,
    which can be factored efficiently using Zassenhaus algorithm. The last
    step is to lift univariate factors to obtain true multivariate
    factors. For this purpose a parallel Hensel lifting procedure is used.

    The parameter ``seed`` is passed to _randint and can be used to seed randint
    (when an integer) or (for testing purposes) can be a sequence of numbers.

    References
    ==========

    .. [1] [Wang78]_
    .. [2] [Geddes92]_

    i(   t   _randinti   i   t   EEZ_NUMBER_OF_CONFIGSt   EEZ_NUMBER_OF_TRIESt   EEZ_MODULUS_STEPi    t   EEZ_RESTART_IF_NEEDEDs3   we need to restart algorithm with better parametersN(   Ni    i    (   t   sympy.utilities.randtestR   t   dmp_zz_factorR   Rw   R\   R   R   t   zeroR   R   R   R[   RW   Rb   t   tuplet   addRd   R8   R   R   RX   t   dmp_zz_wangRG   R   R   R&   (    Re   Rn   Rg   t   modt   seedR   t   randintR   R   Rs   R   t   historyt   configsR   Rl   R   R|   R   R   R   t   eez_num_configst   eez_num_triest   eez_mod_stept   rrt   s_normt   s_argR   t   _s_normt   orig_fR   Rf   Rh   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR     s    %		.$/(	*"c   	      C  sh  | s t  |  |  St |  |  r/ | j g  f St |  | |  \ } } t | | |  d k  r| | t | | |  } } n  t d   t | |  D  r | g  f St | | |  \ } } g  } t	 | |  d k rt
 | | |  } t | | |  } t |  | | |  } n  xA t | | d |  d D]% \ } } | j d | g | f  q/W| t |  f S(   s  
    Factor (non square-free) polynomials in `Z[X]`.

    Given a multivariate polynomial `f` in `Z[x]` computes its complete
    factorization `f_1, ..., f_n` into irreducibles over integers::

                 f = content(f) f_1**k_1 ... f_n**k_n

    The factorization is computed by reducing the input polynomial
    into a primitive square-free polynomial and factoring it using
    Enhanced Extended Zassenhaus (EEZ) algorithm. Trial division
    is used to recover the multiplicities of factors.

    The result is returned as a tuple consisting of::

             (content(f), [(f_1, k_1), ..., (f_n, k_n))

    Consider polynomial `f = 2*(x**2 - y**2)`::

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

        >>> R.dmp_zz_factor(2*x**2 - 2*y**2)
        (2, [(x - y, 1), (x + y, 1)])

    In result we got the following factorization::

                    f = 2 (x - y) (x + y)

    References
    ==========

    .. [1] [Gathen99]_

    i    c         s  s   |  ] } | d  k Vq d S(   i    N(    (   R   R   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pys	   <genexpr>=  s    i   (   R   R   R   RG   R   R&   t   allR   RN   R   RU   R   Ro   R   R   RV   (	   Re   Rn   Rg   R   Rz   R   Rf   R   Rj   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR     s$    $
'c         C  sg  t  |   t |  |  } } t |  |  }  | d k rA | g  f S| d k r` | |  d f g f St |  |  |  }  } t |  |  \ } } } t | | j  } t |  d k r | |  | t  |   f g f S| | j }	 xl t	 |  D]^ \ }
 \ } } t
 | | j |  } t | | |  \ } } } t | |	 |  } | | |
 <q Wt | | |  } | | f S(   s<   Factor univariate polynomials over algebraic number fields. i    i   (   R   R   RD   RT   RR   t   dup_factor_list_includet   domR   t   unitR   R   RO   RL   Rm   (   Re   Rg   Rt   R   R   R|   Rz   Rl   Rf   R   R   Ri   R   R{   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_ext_factorO  s&    
c         C  s}  | s t  |  |  St |  | |  } t |  | |  }  t d   t |  |  D  r` | g  f St |  | |  |  }  } t |  | |  \ } } } t | | | j  } t	 |  d k r |  g } n t
 | j | | j g | d |  }	 xu t |  D]g \ }
 \ } } t | | | j |  } t | | | |  \ } } } t | |	 | |  } | | |
 <q W| t | | | |  f S(   s>   Factor multivariate polynomials over algebraic number fields. c         s  s   |  ] } | d  k Vq d S(   i    N(    (   R   R   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pys	   <genexpr>v  s    i   i    (   R  R   RE   R  R   RU   RS   t   dmp_factor_list_includeR
  R   R   Rx   R  R   R   RP   RK   Ro   (   Re   Rn   Rg   R   R   R|   Rz   Rl   Rf   R   R   Ri   R   R{   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dmp_ext_factorn  s$    
%c         C  s   t  |  | | j  }  t |  | j | j  \ } } x? t |  D]1 \ } \ }  } t  |  | j |  | f | | <q@ W| j | | j  | f S(   s2   Factor univariate polynomials over finite fields. (   R   R
  R   R   R   R   (   Re   Rg   R   Rf   R   Rj   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_gf_factor  s
    #c         C  s   t  d   d S(   s4   Factor multivariate polynomials over finite fields. s+   multivariate polynomials over finite fieldsN(   t   NotImplementedError(   Re   Rn   Rg   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dmp_gf_factor  s    c         C  s  t  |  |  \ } }  t |  |  \ } }  | j rK t |  |  \ } } nx| j rl t |  |  \ } } nW| j s | | j   } } t |  | |  }  n d } | j
 r | j   } t |  | |  \ } }  t |  | |  }  n | } | j rt |  |  \ } } n | j rt |  d |  \ }  }	 t |  |	 | j  \ } } x< t |  D]. \ }
 \ }  } t |  |	 |  | f | |
 <qUW| j | | j  } n t d |   | j
 rx< t |  D]. \ }
 \ }  } t |  | |  | f | |
 <qW| j | |  } | j | |  } | rx t |  D]s \ }
 \ }  } t |  |  } t |  | |  }  t |  | |  }  |  | f | |
 <| j | | j | |   } q.W| j | |  } | } qn  | r| j d | j | j g | f  n  | | t |  f S(   s0   Factor polynomials into irreducibles in `K[x]`. i    s#   factorization not supported over %sN(    R#   RF   t   is_FiniteFieldR  t   is_AlgebraicR  t   is_Exactt	   get_exactR   R   t   is_FieldR   R?   R   R   t   is_PolyR!   t   dmp_factor_listR
  R   R"   R   RY   t   quoR8   R=   t   mult   powR   Rx   R   RV   (   Re   R   R   R   R   Rf   t
   K0_inexactRg   t   denomRn   R   Rj   t   max_norm(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR     sR    						 	 "%c         C  sl   t  |  |  \ } } | s1 t | g  d f g St | d d | |  } | | d d f g | d Sd S(   s0   Factor polynomials into irreducibles in `K[x]`. i   i    N(   R   R   R;   (   Re   Rg   R   Rf   Rz   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR	    s
    c         C  s  | s t  |  |  St |  | |  \ } }  t |  | |  \ } }  | j rg t |  | |  \ } } n| j r t |  | |  \ } } n| j s | | j   } } t	 |  | | |  }  n d } | j r| j   } t |  | | |  \ }	 }  t	 |  | | |  }  n | } | j rt |  | |  \ }
 }  } t |  | |  \ } } x t |  D]1 \ } \ }  } t |  |
 | |  | f | | <q\Wn | j r't |  | |  \ }  } t |  | | j  \ } } x< t |  D]. \ } \ }  } t |  | |  | f | | <qW| j | | j  } n t d |   | j rWx? t |  D]1 \ } \ }  } t	 |  | | |  | f | | <qMW| j | |  } | j | |	  } | rWx t |  D]| \ } \ }  } t |  | |  } t |  | | |  }  t	 |  | | |  }  |  | f | | <| j | | j | |   } qW| j | |  } | } qWn  xr t t |   D]^ \ } } | sqjn  i | j  d | | d d | 6} | j! d t" | | |  | f  qjW| | t# |  f S(   s0   Factor polynomials into irreducibles in `K[X]`. s#   factorization not supported over %si    i   N(   i    (   i   (   i    ($   R   R$   RG   R  R  R  R  R  R  R   R   R  R   R@   R   R   R   R   R    R  R!   R  R
  R"   R   RY   R  R9   R>   R  R  R   Rx   R   R   RV   (   Re   Rn   R   R   R   R   Rf   R  Rg   R  t   levelsR   R   Rj   R  R   t   term(    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR    sb    					&	 	#"$&c         C  s   | s t  |  |  St |  | |  \ } } | sG t | |  d f g St | d d | | |  } | | d d f g | d Sd S(   s0   Factor polynomials into irreducibles in `K[X]`. i   i    N(   R	  R  R   R<   (   Re   Rn   Rg   R   Rf   Rz   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR  /  s    c         C  s   t  |  d |  S(   s:   Returns ``True`` if ``f`` has no factors over its domain. i    (   t   dmp_irreducible_p(   Re   Rg   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   dup_irreducible_p=  s    c         C  sV   t  |  | |  \ } } | s" t St |  d k r8 t S| d \ } } | d k Sd S(   s:   Returns ``True`` if ``f`` has no factors over its domain. i   i    N(   R  Rc   R   R   (   Re   Rn   Rg   R   Rf   Rj   (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyR!  B  s    N(   t   __doc__t
   __future__R    R   t   sympy.polys.galoistoolsR   R   R   R   R   R   R   R	   R
   R   R   t   sympy.polys.densebasicR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R    R!   R"   R#   R$   t   sympy.polys.densearithR%   R&   R'   R(   R)   R*   R+   R,   R-   R.   R/   R0   R1   R2   R3   R4   R5   R6   R7   R8   R9   R:   R;   R<   R=   R>   t   sympy.polys.densetoolsR?   R@   RA   RB   RC   RD   RE   RF   RG   RH   RI   RJ   RK   RL   RM   t   sympy.polys.euclidtoolsRN   RO   RP   t   sympy.polys.sqfreetoolsRQ   RR   RS   RT   RU   t   sympy.polys.polyutilsRV   t   sympy.polys.polyconfigRW   t   sympy.polys.polyerrorsRX   RY   RZ   R[   t   sympy.ntheoryR\   R]   R^   t   sympy.utilitiesR_   t   mathR`   R   Ra   R   t   sympy.core.compatibilityRb   Rm   Ro   Ru   Rw   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!  (    (    (    s6   lib/python2.7/site-packages/sympy/polys/factortools.pyt   <module>   s^   Ld("							9	9		g	L			,		K			6	0	D	4	A					>		I		