
    MZd9                        d Z ddl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.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZB ddlCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZR ddlSmTZTmUZUmVZV ddlWmXZXmYZYmZZZm[Z[m\Z\ dd	l]m^Z^ dd
l_m`Z` ddlambZbmcZcmdZdmeZe ddlfmgZg ddlhmiZjmkZl d Zmd Znd Zod Zpd Zqd Zrd Zsd Ztd Zud4dZvd Zwd Zxd Zyd Zzd Z{d Z|d Z}d Z~d  Zd! Zd" Zd5d$Zd% Zd& Zd' Zd( Zd) Zd* Zd+ Zd, Zd- Zd. Zd/ Zd0 Zd1 Zd2 Zd3 Zy#)6z:Polynomial factorization routines in characteristic zero.     )_randint)gf_from_int_polygf_to_int_poly	gf_lshift
gf_add_mulgf_mulgf_divgf_remgf_gcdexgf_sqf_pgf_factor_sqf	gf_factor)dup_LCdmp_LCdmp_ground_LCdup_TCdup_convertdmp_convert
dup_degree
dmp_degreedmp_degree_indmp_degree_listdmp_from_dict
dmp_zero_pdmp_onedmp_nest	dmp_raise	dup_strip
dmp_grounddup_inflatedmp_excludedmp_include
dmp_inject	dmp_ejectdup_terms_gcddmp_terms_gcd)dup_negdmp_negdup_adddmp_adddup_subdmp_subdup_muldmp_muldup_sqrdmp_powdup_divdmp_divdup_quodmp_quo
dmp_expanddmp_add_muldup_sub_muldmp_sub_mul
dup_lshiftdup_max_normdmp_max_normdup_l1_normdup_mul_grounddmp_mul_grounddup_quo_grounddmp_quo_ground)dup_clear_denomsdmp_clear_denoms	dup_truncdmp_ground_truncdup_content	dup_monicdmp_ground_monicdup_primitivedmp_ground_primitivedmp_eval_taildmp_eval_indmp_diff_eval_indmp_compose	dup_shift
dup_mirror)dmp_primitivedup_inner_gcddmp_inner_gcd)	dup_sqf_pdup_sqf_normdmp_sqf_normdup_sqf_partdmp_sqf_part)_sort_factors)query)ExtraneousFactorsDomainErrorCoercionFailedEvaluationFailed)subsets)ceillogc                     g }|D ]4  }d}	 t        | ||      \  }}|s||dz   }} nn|j                  ||f       6 t        |      S )zc
    Determine multiplicities of factors for a univariate polynomial
    using trial division.
    r      )r1   appendrX   )ffactorsKresultfactorkqrs           9/usr/lib/python3/dist-packages/sympy/polys/factortools.pydup_trial_divisionrm   O   si    
 F #1fa(DAq!a%1  	vqk"#       c                     g }|D ]?  }d}	 t        | |||      \  }}t        ||      r||dz   }} nn'|j                  ||f       A t        |      S )ze
    Determine multiplicities of factors for a multivariate polynomial
    using trial division.
    r   rb   )r2   r   rc   rX   )	rd   re   urf   rg   rh   ri   rj   rk   s	            rl   dmp_trial_divisionrq   f   sr    
 F #1fa+DAq!Q!a%1  	vqk"#   rn   c           	         ddl m} t        |       }t        |dz        }t        |dz        }|j	                  t        | D cg c]  }|dz  	 c}            } ||dz
  |      } ||dz
  |dz
        }	|j                  t        | |            }
||z  |	|
z  z   }|t        | |      z  }t        |dz        dz  }|S c c}w )a  
    The Knuth-Cohen variant of Mignotte bound for
    univariate polynomials in `K[x]`.

    Examples
    ========

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

    >>> f = x**3 + 14*x**2 + 56*x + 64
    >>> R.dup_zz_mignotte_bound(f)
    152

    By checking `factor(f)` we can see that max coeff is 8

    Also consider a case that `f` is irreducible for example `f = 2*x**2 + 3*x + 4`
    To avoid a bug for these cases, we return the bound plus the max coefficient of `f`

    >>> f = 2*x**2 + 3*x + 4
    >>> R.dup_zz_mignotte_bound(f)
    6

    Lastly,To see the difference between the new and the old Mignotte bound
    consider the irreducible polynomial::

    >>> f = 87*x**7 + 4*x**6 + 80*x**5 + 17*x**4 + 9*x**3 + 12*x**2 + 49*x + 26
    >>> R.dup_zz_mignotte_bound(f)
    744

    The new Mignotte bound is 744 whereas the old one (SymPy 1.5.1) is 1937664.


    References
    ==========

    ..[1] [Abbott2013]_

    r   )binomial   rb   )	(sympy.functions.combinatorial.factorialsrs   r   _ceilsqrtsumabsr   r:   )rd   rf   rs   ddeltadelta2cf	eucl_normt1t2lcbounds               rl   dup_zz_mignotte_boundr   }   s    P B1A!a%LE519F a0b!e024I 
%!)V	$B	%!)VaZ	(B	
va|	BNR"W$E	\!QE%!)q EL 1s   B<c                     t        | ||      }t        t        | ||            }t        t	        | |            }|j                   ||dz               d|z  z  |z  |z  S )z7Mignotte bound for multivariate polynomials in `K[X]`. rb   rt   )r;   ry   r   rx   r   rw   )rd   rp   rf   abns         rl   dmp_zz_mignotte_boundr      s^    Q1AM!Q"#AOAq!"A66!AE(AqD "1$$rn   c                    | dz  }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 )a
  
    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]_

    rt   )r7   rC   r1   r-   r)   r+   one)mrd   ghstrf   Merj   rk   rp   GHr   crz   STs                      rl   dup_zz_hensel_stepr      s   8 	
1AAq!QA!QA71a#Q*DAq!QA!QA1a '!Q"2A6A'!Q"Aq)A'!Q"Aq)A1a '!Q"2A6A'!aeeWa(!Q/A71a#Q*DAq!QA!QA1a '!Q"2A6A'!Q"Aq)A'!Q"Aq)AaA:rn   c           
         t        |      }t        ||      }|dk(  r4t        ||j                  || |z        d   |      }t	        || |z  |      gS | }|dz  }	t        t        t        |d                  }
t        |g|       }|d|	 D ]  }t        |t        ||       | |      } t        ||	   |       }||	dz   d D ]  }t        |t        ||       | |      } t        ||| |      \  }}}t        ||       }t        ||       }t        ||       }t        ||       }t        d|
dz         D ]  }t        |||||||      |dz  c\  }}}}}  t        | ||d|	 ||      t        | |||	d ||      z   S )a  
    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,\ \dots,\ 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]_

    rb   r   rt   N)lenr   r=   gcdexrC   intrv   _logr   r   r   r   ranger   dup_zz_hensel_lift)prd   f_listlrf   rk   r   Fr   ri   rz   r   f_ir   r   r   _s                    rl   r   r      s   . 	FA	1BAv1aggb!Q$/2A61adA&((	A	QAE$q!*A"q!Abqz 61&sA.156 	A&Aa!ef~ 61&sA.156 q!Q"GAq!q!Aq!Aq!Aq!A1a!e_ H,Q1aAqA1a4Aq!aH aF2AJ15
Q6!":q!
45 5rn   c                 2    ||dz  kD  r||z
  }|sy| |z  dk(  S )Nrt   Tr    )fcrj   pls      rl   _test_plr   3  s*    27{F6Q;rn   c           
         t        |       }|dk(  r| gS ddlm} | d   }t        | |      }t	        | |      }t        t        |j                   ||dz               d|z  z  |z  |z              }t        |dz   d|z  z  |d|z  dz
  z  z        }t        t        dt        |d      z              }	t        d|	z  t        |	      z        }
g }t        d|
dz         D ]  } ||      r||z  dk(  r|j                  |      }t        | |      }t        |||      s?t        |||      d   }|j                  ||f       t!        |      dk  st!        |      dkD  s n t#        |d	 
      \  }}t        t        t        d|z  dz   |                  }|D cg c]  }t%        ||       }}t'        || |||      }t        t!        |            }t)        |      }g d}}||z  }d|z  t!        |      k  rrt+        ||      D ]J  }|dk(  r'd}|D ]  }|||   d   z  } ||z  }t-        |||      sG0|g}|D ]  }t/        |||   |      } t1        |||      }t3        ||      d   }|d   }|r	||z  dk7  rv|g}t)        |      }||z
  }|dk(  r'|g}|D ]  }t/        |||   |      } t1        |||      }|D ]  }t/        |||   |      } t1        |||      }t5        |      } t5        ||      }!| |!z  |k  s|}|D cg c]	  }||vs| }}t3        ||      d   }t3        ||      d   } |j                  |       t	        | |      } n |dz  }d|z  t!        |      k  rr|| gz   S c c}w c c}w )z4Factor primitive square-free polynomials in `Z[x]`. rb   r   )isprimert            c                     t        | d         S )Nrb   )r   )xs    rl   <lambda>z#dup_zz_zassenhaus.<locals>.<lambda>\  s    3qt9 rn   )key)r   sympy.ntheoryr   r:   r   r   ry   rw   rv   r   r   convertr   r   r   rc   r   minr   r   setr^   r   r-   rC   rH   r<   )"rd   rf   r   r   r   Ar   BCgammar   r   pxr   fsqfxr   fsqfr   ffmodularr   sorted_Tr   re   r   r   r   rj   ir   r   T_SG_normH_norms"                                     rl   dup_zz_zassenhausr   :  s   1AAvs
%	
2BQAq!ACqQx A%a')*+AQUacN1qsQw<'(AaQ
l#$E%U#$E
A Auqy! r{a"fkYYr]Q#2q!aQ'*	"eu:?c!fqj !,-GAtE$qsQw"#$A/34~b!$4G41a!Q/ASV}HHAQQG	
AB
A#Q-1% 4	A
 Av #A!A$r(
A#FAr*C ,A1Q4+A,aQ'!!Q'*bEa1AAAa%CAvC ,A1Q4+A,aQ' (AqtQ'( !R#A A&F A&Ff}!'/>!1A:A>>!!Q'*!!Q'*q!1aLe4	h FAk A#Q-n aS=A 5h ?s   M, 	M1
M1c                     t        | |      }t        | |      }t        | dd |      }|r=ddlm}  |t        |            }|j                         D ]  }||z  s	||dz  z  s y yy)z2Test irreducibility using Eisenstein's criterion. rb   Nr   	factorintrt   T)r   r   rE   r   r   r   keys)rd   rf   r   tce_fcr   e_ffr   s           rl   dup_zz_irreducible_pr     sl    	1B	1Bqua D+T# 	AQR!Q$Y		 rn   c                    |j                   r!	 ||j                         }}t        | ||      } n|j                  syt        | |      }t        | |      }|dk7  s
|dk7  r|dk7  ry|s't        | |      \  }}||j                  k7  s|| dfgk7  ryt        |       }g g }
}	t        |dd      D ]  }|	j                  d| |           t        |dz
  dd      D ]  }|
j                  d| |           t        t        |	      |      }	t        t        |
      |      }
t        |	t        |
d|      |      }|j!                  t        ||            rt#        ||      }|| k(  ryt%        | |      }	|j!                  t        |	|            rt#        |	|      }	||	k(  rt'        |	|      ryt)        ||      }t        ||      |k(  rt'        ||      ryy# t        $ r Y yw xY w)ad  
    Efficiently test if ``f`` is a cyclotomic polynomial.

    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

    References
    ==========

    Bradford, Russell J., and James H. Davenport. "Effective tests for
    cyclotomic polynomials." In International Symposium on Symbolic and
    Algebraic Computation, pp. 244-251. Springer, Berlin, Heidelberg, 1988.

    Frb   r   r   T)is_QQget_ringr   r\   is_ZZr   r   dup_factor_listr   r   r   insertr/   r   r+   r9   is_negativer'   rO   dup_cyclotomic_prV   )rd   rf   irreducibleK0r   r   coeffre   r   r   r   r   r   r   s                 rl   r   r     s   4 	ww	qzz|BAr1%A WW	1B	1B	Qw28a(A.wAEE>W!Q01ArqA1b" 	AaD 1q5"b! 	AaD 		!a A	!a A:aA&*A}}VAq\"AqMAv1aA}}VAq\"AqMAv"1a(QAq!}.q!4e  		s   G	 		GGc                     ddl m} |j                  |j                   g} ||       j                         D ]0  \  }}t	        t        |||      ||      }t        |||dz
  z  |      }2 |S )z1Efficiently generate n-th cyclotomic polynomial. r   r   rb   )r   r   r   itemsr3   r    )r   rf   r   r   r   ri   s         rl   dup_zz_cyclotomic_polyr     sl    '	
A!""$ *1K1a(!Q/1q1u:q)* Hrn   c                 z   ddl m} |j                  |j                   gg} ||       j                         D ]w  \  }}|D cg c]  }t	        t        |||      ||       }}|j                  |       t        d|      D ]-  }|D 	cg c]  }	t        |	||       }}	|j                  |       / y |S c c}w c c}	w )Nr   r   rb   )r   r   r   r   r3   r    extendr   )
r   rf   r   r   r   ri   r   Qr   rj   s
             rl   _dup_cyclotomic_decomposer     s    '
%%!%%A!""$ 1;<>agk!Q*Aq1>>	q! 	A0131+aA&3A3HHQK		 H ? 4s   B3B8c                 @   t        | |      t        | |      }}t        |       dk  ry|dk7  s|dvryt        d | dd D              ryt        |       }t	        ||      }|j                  |      s|S g }t	        d|z  |      D ]  }||vs|j                  |        |S )a  
    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 cyclotomic decomposition of `f`,
    which makes this method much faster that any other direct factorization
    approach (e.g. Zassenhaus's).

    References
    ==========

    .. [1] [Weisstein09]_

    r   Nrb   )r   rb   c              3   2   K   | ]  }t        |        y w)N)bool).0r}   s     rl   	<genexpr>z+dup_zz_cyclotomic_factor.<locals>.<genexpr><  s     
&48
&s   r   rt   )r   r   r   anyr   is_onerc   )rd   rf   lc_ftc_fr   r   r   r   s           rl   dup_zz_cyclotomic_factorr   "  s    $ 1va|$D!}qyD'

&a"g
&&1A!!Q'A88D>*1Q32 	Az	 rn   c                 <   t        | |      \  }}t        |      }t        ||      dk  r| t        ||      }}|dk  r|g fS |dk(  r||gfS t	        d      rt        ||      r||gfS d}t	        d      rt        ||      }|t        ||      }|t        |d      fS )z:Factor square-free (non-primitive) polynomials in `Z[x]`. r   rb   USE_IRREDUCIBLE_IN_FACTORNUSE_CYCLOTOMIC_FACTORF)multiple)	rH   r   r   r'   rY   r   r   r   rX   )rd   rf   contr   r   re   s         rl   dup_zz_factor_sqfr   N  s    Aq!GD!1Aa|a%AaAvRx	
aaSy()1%!9G$%*1a0#Aq)w777rn   c                 `   t        | |      \  }}t        |      }t        ||      dk  r| t        ||      }}|dk  r|g fS |dk(  r||dfgfS t	        d      rt        ||      r||dfgfS t        ||      }d}t	        d      rt        ||      }|t        ||      }t        | ||      }||fS )a  
    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]_

    r   rb   r   Nr   )
rH   r   r   r'   rY   r   rV   r   r   rm   )rd   rf   r   r   r   r   re   s          rl   dup_zz_factorr   k  s    \ Aq!GD!1Aa|a%AaAvRx	
aq!fX~()1%1a&>!QAA$%$Q*ya# Aq)G=rn   c                     ||z  g}| D ]d  }t        |      }t        |      D ]8  }|dk7  r|j                  ||      }||z  }|dk7  r|j                  |      s7  y |j	                  |       f |dd S )z,Wang/EEZ: Compute a set of valid divisors.  rb   N)ry   reversedgcdr   rc   )Ecsctrf   rg   rj   rk   s          rl   dmp_zz_wang_non_divisorsr     s    "uYF F&! 	Aq&EE!QKF q& xx{	 	a !":rn   c           
         t        t        | |      ||dz
  |      st        d      t        | |||      }t        ||      st        d      t	        ||      \  }}|j                  t        ||            r| t        ||      }}|dz
  }	|D 
cg c]  \  }
}t        |
||	|       }}
}t        ||||      }||||fS t        d      c c}}
w )z2Wang/EEZ: Test evaluation points for suitability. rb   zno luck)	rJ   r   r]   rS   rH   r   r   r'   r   )rd   r   r   r   rp   rf   r   r   r   vr   r   r   Ds                 rl   dmp_zz_wang_test_pointsr     s    1q!a%3y))aAq!AQ?y))ADAq}}VAq\"r71a=1	AA0131-1a
#3A3 Ar1-A}!Qwy)) 	4s   C
c                    g dgt        |      z  |dz
  }
}	}|D ]  }t        |
|      }t        ||      |z  }t        t	        t        |                  D ]M  }d||   ||   c}}\  }}||z  s||z  |dz   }}||z  s|dk7  s.t        |t        |||
|      |
|      dc}|	|<   O |j                  |        t        |	      st        g g }}t        ||      D ]  \  }}t        |||
|      }t        ||      }|j                  |      r||z  }n.|j                  ||      }||z  ||z  }}t        |||      ||z  }}t        |||
|      }|j                  |       |j                  |        |j                  |      r| ||fS g g }}t        ||      D ]?  \  }}|j                  t        |||
|             |j                  t        ||d|             A t        | |t        |      dz
  z  ||      } | ||fS )z0Wang/EEZ: Compute correct leading coefficients. r   rb   )r   r   r   r   r   r.   r0   rc   allrZ   ziprJ   r   r   r=   r>   )rd   r   r   r   r   r   rp   rf   r   Jr   r   r   rz   r   ri   r   r   r   CCHHr   ccr   CCCHHHs                             rl   dmp_zz_wang_lead_coeffsr    s+   1#c!f*a!e!qA AqM1aLO%A-( 	CAadAaDLAq&1a1u!tQU1 1u Av!!WQ1a%8!Q?1Q4	C 	
 q6BAq	 1!Q1%Aq\88B<QBb!AqD"a%rA"1a+RUrA1b!Q'
		!
		!  	xx|"by2CB 01

>!RA./

>!RA./0 	q"s1vz*Aq1Ac3;rn   c           
      &   t        |       dk(  r| \  }}t        ||      }t        ||      }t        ||||      \  }}	}
t        |||      }t        |	||      }	t	        ||||      \  }}t        |	||||      }	t        ||      }t        |	|      }	||	g}|S | d   g}
t        | dd       D ]"  }|
j                  dt        ||
d   |             $ g dgg}}t        | |
      D ]@  \  }}t        ||g|d   g d|d|      \  }	}|j                  |	       |j                  |       B g ||d   gz   }}t        ||       D ]S  \  }}t        ||      }t        ||      }t        t        |||      |||      }t        ||      }|j                  |       U |S )z2Wang/EEZ: Solve univariate Diophantine equations. rt   r   rb   r   )r   r   r   r   r	   r   r   r   r   r-   r  dmp_zz_diophantinerc   r
   )r   r   r   rf   r   r   rd   r   r   r   r   rj   rg   r   r   rk   s                   rl   dup_zz_diophantiner    s   
1v{1Q"Q"1aA&1aaAaAaAq!1q!Q1%1a 1a Q2 M/ rUG!Ab'" 	-AHHQ1Q4+,	- QC511I 	DAq%q!faeRAq!DDAqHHQKHHQK	
 QrUG1I 	DAq A&A A&AyAq)1a3Aq!$AMM!	 Mrn   c           
         |s| D cg c]  }g  }}t        |      }	t        |      D ]a  \  }
}|s	t        | |	|
z
  ||      }t        t        ||            D ]0  \  }\  }}t	        |||      }t        t        |||      ||      ||<   2 c |S t        |      }	t        | ||      }|d   |dd }}g g }}| D ]=  }|j                  t        ||||             |j                  t        |||	||             ? t        |||	||      }|dz
  }t        |||||||      }|D cg c]  }t        |d||       }}t        ||      D ]  \  }}t        |||||      } t        ||||      }t!        |j"                  | g|	|      }t%        |	|      }|j'                  t)        d|            D ]  }t+        ||      r nt-        ||||      }t/        ||dz   ||	||      }t+        ||      r@t1        ||j3                  |dz         ||      }t        |||||||      }t        |      D ]"  \  }
}t-        t        |d||      |||      ||
<   $ t        t        ||            D ]  \  }
\  }}t5        ||||      ||
<    t        ||      D ]  \  }}t        |||||      } t        ||||      } |D cg c]  }t        ||||       }}|S c c}w c c}w c c}w )z4Wang/EEZ: Solve multivariate Diophantine equations. r   Nrb   r   )r   	enumerater  r  r=   rC   r)   r   r5   rc   r4   rK   r
  r   r8   rD   r   r   r   mapr   r   r.   rL   r@   	factorialr*   )r   r   r   rz   r   rp   rf   r   r   r   r   r   r   jr   r   r   r   r   r   rd   r   r   r   r   r   ri   s                              rl   r
  r
  J  sA   QbqM!! 	9HAu"1a!eQ2A&s1ay1 9	6Aq"1eQ/ Aq!11a8!9	9v Hc Fq!Quaf121 	1AHHWQ1a()HH[Aq!Q/0	1 1aA&Eq!Q1a3-.0i1a#001I 	+DAqAq!Q*A	+ Q1a(aeeaR[!Q'AqMuQ{# 	1A!Q1a#A AE1aA6Aa#"1akk!a%&8!Q?&q!Q1a;%aL CDAq"9Q1a#8!QBAaDC "+3q!9!5 /IAv1"1aA.AaD/  1I 3DAq#Aq!Q2A3 %Q1a0)	1, 567qq!Q*77H} 8 1@ 8s   	KK2Kc                 8   | gt        |      |dz
  }	}}t        |      }t        t        |dd             D ]>  \  }
}t	        |d   |||
z
  ||
z
  |      }|j                  dt        |||	|
z
  |             @ t        t        | |      dd       }t        t        d|dz         ||      D ]  \  }}}t        |      |dz
  }}|d|dz
   ||dz
  d }}t        t        ||            D ]@  \  }
\  }}t        t        |||	|      ||dz
  |      }|gt        |dd d|dz
  |      z   ||
<   B t        |j                  | g||      }t        ||      }t!        |t#        |||      ||      }t%        |||      }|j'                  t        d|            D ]  }t)        ||      r t+        ||||      }t-        ||dz   ||||      }t)        ||dz
        rBt/        ||j1                  |dz         |dz
  |      }t3        ||||||dz
  |      }t        t        ||            D ]7  \  }
\  }}t5        |t        |d|dz
  |      |||      }t        ||||      ||
<   9 t!        |t#        |||      ||      }t        ||||      }  t#        |||      | k7  rt6        |S )z-Wang/EEZ: Parallel Hensel lifting algorithm. rb   Nr   rt   )r   listr  r   rK   r   rD   maxr   r  r   rJ   r   r   r   r   r,   r5   r   r  r   r.   rL   r@   r  r
  r6   rZ   )rd   r   LCr   r   rp   rf   r   r   r   r   r   r   rz   r  r   wIr  r   r   r   r   r   djri   r   r   r   s                                rl   dmp_zz_wang_hensel_liftingr    s   c3q61q5!qAQA(1QR5/* 61!aQAq1	$Q1q5!456 	OAq!!"%&AuQA1-  11aAwA1!a%y!AEF)1#C2J/ 	8JAw2!-Aq!"<aQJB4)AabE1a!eQ77AaD	8 aeeaR[!Q'AqMAz!Q*Aq11a#uQ|$ 	1A!Q1a#A AE1aA6AaQ'"1akk!a%&8!a%C&q!Q1a!eQ?!*3q!9!5 8IAv1#AyAq1ua'@!QJA+Aq!Q7AaD8 Az!Q2Aq9$Q1a0!	1! 1D !Qarn   Nc           
         ddl m} t        |      }t        t	        | |      |dz
  |      \  }}t        | ||      }	 | ||	            }
|
|dk(  rd}nd}t               g |j                  g|z  df\  }}}}	 t        | |||||      \  }}}t        ||      \  }}t        |      }|dk(  r| gS |||||fg}t        d      }t        d      }t        d      }t        |      |k  rt        |      D ]  }t        |      D cg c]  } | || |             }}t        |      |vr|j                  t        |             nR	 t        | |||||      \  }}}t        ||      \  }}t        |      }|||k7  r||k  rg |}}n|}|dk(  r| gc S |j!                  |||||f       t        |      |k(  s n ||z  }t        |      |k  rd	\  }}}|D ]'  \  }}}}}t#        ||      }|
||k  r|}|}n|}|dz  }) ||   \  }}}}}| }	 t%        | |||||||      \  } }}t'        | ||||
||      }g }|D ]L  } t-        | ||      \  }} |j/                  t1        | ||            rt3        | ||      } |j!                  |        N |S # t        $ r Y w xY wc c}w # t        $ r Y w xY w# t(        $ r* t        d
      rt+        ||||dz         cY S t)        d      w xY w)a`  
    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, \dots, 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]_

    r   )	nextprimerb   Nrt   EEZ_NUMBER_OF_CONFIGSEEZ_NUMBER_OF_TRIESEEZ_MODULUS_STEP)Nr   r   EEZ_RESTART_IF_NEEDEDz3we need to restart algorithm with better parameters)r   r  r   dmp_zz_factorr   r   r   zeror   r   r   r]   rY   r   tupleaddrc   r:   r  r  rZ   dmp_zz_wangrI   r   r   r(   ) rd   rp   rf   modseedr  randintr   r   r   r   historyconfigsr   rk   r   r   r   r   r   eez_num_configseez_num_trieseez_mod_steprrs_norms_argr   _s_normorig_fr  re   rg   s                                    rl   r#  r#    s   < (tnG&A,Aq1EBaA&A	)A,A
{6CC UB
D8GWa*1aQ1=Aq A&1F63Jr1a#$ 34O/0M+,L
g,
(}% "	 A16q;A!GSD#&';A;Qxw&E!H%21aQ1EAq %Q*DAqQB}7Av%' Avs
NNAr1a+,7|.A"	 D <CG g,
(J "FE1  
1aAq!$ F	Q
 U^NAr1aFG*1aQ1aC1b,Q2q!QB F #Aq!,1==q!Q/01a Aa Mc   < $ \  G()vq!S1W55#EG G	GsB   85J .J  JJ'J, 	JJ	J)(J),%KKc                    |st        | |      S t        | |      r|j                  g fS t        | ||      \  }}t	        |||      dk  r| t        |||      }}t        d t        ||      D              r|g fS t        |||      \  }}g }t        ||      dkD  r(t        |||      }t        |||      }t        | |||      }t        ||dz
  |      d   D ]  \  }}|j                  d|g|f        |t        |      fS )a  
    Factor (non square-free) polynomials in `Z[X]`.

    Given a multivariate polynomial `f` in `Z[x]` computes its complete
    factorization `f_1, \dots, 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]_

    r   c              3   &   K   | ]	  }|d k    ywr   Nr   r   rz   s     rl   r   z dmp_zz_factor.<locals>.<genexpr>       
1a16
1   rb   )r   r   r   rI   r   r(   r   r   rP   r   rW   r#  rq   r  r   rX   )	rd   rp   rf   r   r   r   re   r   ri   s	            rl   r  r  P  s   H Q""!Qvvrz"1a+GD!Q1!%Aq)a

1?1a0
11RxAq!DAqG!Q!Aq!1a $Q1a0aQ*1- $1qA3(#$ w'''rn   c           	          |j                         }t        | ||      } t        | |      \  }}|D cg c]  \  }}t        |||      |f }}}|j                  ||      }||fS c c}}w )z>Factor univariate polynomials into irreducibles in `QQ_I[x]`. )as_AlgebraicFieldr   r   r   )rd   r   K1r   re   facr   s          rl   dup_qq_i_factorr;    su     
			BAr2A$Q+NE7;BCaCR(!,CGCJJub!E'> Ds   A$c                 <   |j                         }t        | ||      } t        | |      \  }}g }|D ]R  \  }}t        ||      \  }}	t        |	||      }
t	        |
d|      \  }}|||z  z  ||z  z  }|j                  ||f       T |}|j                  ||      }||fS )z>Factor univariate polynomials into irreducibles in `ZZ_I[x]`. r   )	get_fieldr   r;  rA   rI   rc   r   )rd   r   r9  r   re   new_factorsr:  r   	fac_denomfac_numfac_num_ZZ_Icontentfac_prims                rl   dup_zz_i_factorrD    s     
BAr2A$Q+NE7K *Q-c26	7"7B30q"EA%)q.8Ha=)* GJJub!E'>rn   c           
          |j                         }t        | |||      } t        | ||      \  }}|D cg c]  \  }}t        ||||      |f }}}|j                  ||      }||fS c c}}w )z@Factor multivariate polynomials into irreducibles in `QQ_I[X]`. )r8  r   dmp_factor_listr   )rd   rp   r   r9  r   re   r:  r   s           rl   dmp_qq_i_factorrG    s{     
			BAq"b!A$Q2.NE7>EFFCCB+Q/FGFJJub!E'> Gs   A'c                 D   |j                         }t        | |||      } t        | ||      \  }}g }|D ]T  \  }}t        |||      \  }	}
t        |
|||      }t	        |||      \  }}|||z  z  |	|z  z  }|j                  ||f       V |}|j                  ||      }||fS )z@Factor multivariate polynomials into irreducibles in `ZZ_I[X]`. )r=  r   rG  rB   rI   rc   r   )rd   rp   r   r9  r   re   r>  r:  r   r?  r@  rA  rB  rC  s                 rl   dmp_zz_i_factorrI    s     
BAq"b!A$Q2.NE7K *Q-c1b9	7"7Ar260q"EA%)q.8Ha=)* GJJub!E'>rn   c                    t        |       t        | |      }}t        | |      } |dk  r|g fS |dk(  r|| dfgfS t        | |      | }} t	        | |      \  }}}t        ||j                        }t        |      dk(  r|| |t        |       z  fgfS ||j                  z  }	t        |      D ]B  \  }
\  }}t        ||j                  |      }t        |||      \  }}}t        ||	|      }|||
<   D t        |||      }||fS )z<Factor univariate polynomials over algebraic number fields. r   rb   )r   r   rF   rV   rT   dup_factor_list_includedomr   unitr  r   rQ   rN   rm   )rd   rf   r   r   r   r   r   rk   re   r   r   rh   r   r   s                 rl   dup_ext_factorrN    s!   qM6!Q<rA!QAAv2vAvQF8|1qqA1a GAq!%a/G
7|qQ:a=()***	!&&A#G, ;FAq)1a(1aaA
	 !GQ/Gw;rn   c                 8   |st        | |      S t        | ||      }t        | ||      } t        d t	        | |      D              r|g fS t        | ||      | }} t        | ||      \  }}}t        |||j                        }t        |      dk(  r| g}nzt        |j                  ||j                  z  g|d|      }	t        |      D ]E  \  }
\  }}t        |||j                  |      }t        ||||      \  }}}t!        ||	||      }|||
<   G |t#        ||||      fS )z>Factor multivariate polynomials over algebraic number fields. c              3   &   K   | ]	  }|d k    ywr3  r   r4  s     rl   r   z!dmp_ext_factor.<locals>.<genexpr>  r5  r6  rb   r   )rN  r   rG   r   r   rW   rU   dmp_factor_list_includerL  r   r   r   rM  r  r   rR   rM   rq   )rd   rp   rf   r   r   r   r   rk   re   r   r   rh   r   r   s                 rl   dmp_ext_factorrR    s0   a##	q!Q	BAq!A

1?1a0
112v1a !qA1a#GAq!%aAEE2G
7|q#quuah'Aq1'0 	NA{FAquua0A#Aq!Q/GAq!Aq!Q'AGAJ		 !!Wa333rn   c                    t        | ||j                        } t        | |j                  |j                        \  }}t	        |      D ]$  \  }\  } }t        | |j                  |      |f||<   & |j                  ||j                        |fS )z2Factor univariate polynomials over finite fields. )r   rL  r   r$  r  r   )rd   rf   r   re   r   ri   s         rl   dup_gf_factorrT    s    Aq!%% Aq!%%/NE7w' 3	6Aq!!QUUA.2
3 99UAEE"G++rn   c                     t        d      )z4Factor multivariate polynomials over finite fields. z+multivariate polynomials over finite fields)NotImplementedError)rd   rp   rf   s      rl   dmp_gf_factorrW    s    
K
LLrn   c                 b   t        | |      \  }} t        | |      \  }} |j                  rt        | |      \  }}n9|j                  rt        | |      \  }}n|j                  rt        | |      \  }}n|j                  rt        | |      \  }}n|j                  s ||j                         }}t        | ||      } nd}|j                  r.|j                         }t        | ||      \  }} t        | ||      } n|}|j                   rt#        | |      \  }}n|j$                  rot'        | d|      \  } }	t)        | |	|j*                        \  }}t-        |      D ]  \  }
\  } }t/        | |	|      |f||
<    |j1                  ||j*                        }nt3        d|z        |j                  rt-        |      D ]  \  }
\  } }t        | ||      |f||
<    |j1                  ||      }|j5                  |      }|ryt-        |      D ]W  \  }
\  } }t7        | |      }t9        | ||      } t        | ||      } | |f||
<   |j;                  ||j=                  ||            }Y |j1                  ||      }|}|r*|j?                  d|j@                  |jB                  g|f       ||z  tE        |      fS );Factor univariate polynomials into irreducibles in `K[x]`. Nr   #factorization not supported over %s)#r%   rH   is_FiniteFieldrT  is_AlgebraicrN  is_GaussianRingrD  is_GaussianFieldr;  is_Exact	get_exactr   is_Fieldr   rA   r   r   is_Polyr#   rF  rL  r  r$   r   r[   quor:   r?   mulpowr   r   r   rX   )rd   r   r  r   r   re   
K0_inexactrf   denomrp   r   ri   max_norms                rl   r   r   !  s   BDAqAr"GD!	&q"-w	'2.w			(B/w			(B/w{{JAz2.AJ;;A'2q1HE1Ar1%AA77*1a0NE7YYaA&DAq,Q1559NE7&w/ 5	6Aq'1a0!4
5 IIeQUU+ECbHII;;&w/ 8	6Aq)!Q3Q7
8 JJua(EFF5%(E!*7!3 ?IAv1+Ar2H&q(B7A#Ar:6A"#QGAJFF5"&&1*=>E? #**5"5qBFFBGG,a01:}W---rn   c                     t        | |      \  }}|st        |g      dfgS t        |d   d   ||      }||d   d   fg|dd z   S )rY  rb   r   N)r   r   r=   )rd   rf   r   re   r   s        rl   rK  rK  c  sa    $Q*NE7E7#Q'((71:a=%3GAJqM"#gabk11rn   c           	      l   |st        | |      S t        | ||      \  }} t        | ||      \  }} |j                  rt	        | ||      \  }}n~|j
                  rt        | ||      \  }}n`|j                  rt        | ||      \  }}nB|j                  rt        | ||      \  }}n$|j                  s!||j                         }}t        | |||      } nd}|j                  r0|j                         }t!        | |||      \  }	} t        | |||      } n|}|j"                  rKt%        | ||      \  }
} }t'        | ||      \  }}t)        |      D ]  \  }\  } }t+        | |
||      |f||<    n|j,                  rot/        | ||      \  } }t1        | ||j2                        \  }}t)        |      D ]  \  }\  } }t5        | ||      |f||<    |j7                  ||j2                        }nt9        d|z        |j                  rt)        |      D ]  \  }\  } }t        | |||      |f||<    |j7                  ||      }|j;                  |	      }|r|t)        |      D ]Z  \  }\  } }t=        | ||      }t?        | |||      } t        | |||      } | |f||<   |jA                  ||jC                  ||            }\ |j7                  ||      }|}t)        tE        |            D ]D  \  }}|s	d||z
  z  dz   d|z  z   |jF                  i}|jI                  dtK        |||      |f       F ||z  tM        |      fS )=Factor multivariate polynomials into irreducibles in `K[X]`. NrZ  )r   )rb   r   )'r   r&   rI   r[  rW  r\  rR  r]  rI  r^  rG  r_  r`  r   ra  r   rB   r   r!   r  r  r"   rb  r#   rF  rL  r$   r   r[   rc  r;   r@   rd  re  r   r   r   r   rX   )rd   rp   r   r  r   r   re   rf  rf   rg  levelsr   r   ri   rh  r  terms                    rl   rF  rF  n  sU   q"%%Ar"DAq"1a,GD!	&q!R0w	'1b1w			(Ar2w			(Ar2w{{JAq*b1AJ;;A'1b!4HE1Aq"a(AA77&q!Q/LFAq*1a3NE7&w/ ?	6Aq)!VQ:A>
?YYaA&DAq,Q1559NE7&w/ 5	6Aq'1a0!4
5 IIeQUU+ECbHII;;&w/ ;	6Aq)!Q26:
; JJua(EFF5%(E!*7!3 ?IAv1+Aq"5H&q(Ar:A#Aq"j9A"#QGAJFF5"&&1*=>E? #**5"5(1+& ;1a!et#d1f,bff5q=q"5q9:; :}W---rn   c                     |st        | |      S t        | ||      \  }}|st        ||      dfgS t        |d   d   |||      }||d   d   fg|dd z   S )rk  rb   r   N)rK  rF  r   r>   )rd   rp   rf   r   re   r   s         rl   rQ  rQ    su    &q!,,$Q1-NE7E1%q)**71:a=%A6GAJqM"#gabk11rn   c                     t        | d|      S )z_
    Returns ``True`` if a univariate polynomial ``f`` has no factors
    over its domain.
    r   )dmp_irreducible_p)rd   rf   s     rl   dup_irreducible_prq    s    
 Q1%%rn   c                 `    t        | ||      \  }}|syt        |      dkD  ry|d   \  }}|dk(  S )za
    Returns ``True`` if a multivariate polynomial ``f`` has no factors
    over its domain.
    Trb   Fr   )rF  r   )rd   rp   rf   r   re   ri   s         rl   rp  rp    sA    
 !Aq)JAw	W	qz1Avrn   )F)NN)__doc__sympy.core.randomr   sympy.polys.galoistoolsr   r   r   r   r   r	   r
   r   r   r   r   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&   sympy.polys.densearithr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   sympy.polys.densetoolsrA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   sympy.polys.euclidtoolsrP   rQ   rR   sympy.polys.sqfreetoolsrS   rT   rU   rV   rW   sympy.polys.polyutilsrX   sympy.polys.polyconfigrY   sympy.polys.polyerrorsrZ   r[   r\   r]   sympy.utilitiesr^   mathr_   rv   r`   r   rm   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r
  r  r#  r  r;  rD  rG  rI  rN  rR  rT  rW  r   rK  rF  rQ  rq  rp  r   rn   rl   <module>r     s}   @ &   " " " " " " ""$ $ $ $ $ $ $$	 	 	 	 	" "   
 0 (F F $ +!.!.9v%6r75rfR Pf	 )X8:HV(*43l-`AH1hK\>(B,,>4<	,M
?.D2J.Z2&rn   