o
    8Va                     @   s  d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
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-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZP ddlQmRZRmSZSmTZT ddlUmVZVmWZWmXZXmYZYmZZZ ddl[m\Z\ dd	l]m^Z^ dd
l_m`Z`maZambZbmcZc ddldmeZemfZfmgZg ddlhmiZi ddljmkZlmmZn dd Zodd Zpdd Zqdd Zrdd Zsdd Ztdd Zudd Zvdd ZwdZd!d"Zxd#d$ Zyd%d& Zzd'd( Z{d)d* Z|d+d, Z}d-d. Z~d/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd[d:d;Zd<d= Zd>d? Zd@dA ZdBdC ZdDdE ZdFdG ZdHdI ZdJdK ZdLdM ZdNdO ZdPdQ ZdRdS ZdTdU ZdVdW ZdXdY Zd9S )\z:Polynomial factorization routines in characteristic zero.     )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)	nextprimeisprime	factorint)subsets)ceillogc                 C   sR   g }|D ] }d}	 t | ||\}}|s||d } }nnq	|||f qt|S )zc
    Determine multiplicities of factors for a univariate polynomial
    using trial division.
    r   T   )r/   appendrV   )ffactorsKresultfactorkqr rl   9/usr/lib/python3/dist-packages/sympy/polys/factortools.pydup_trial_divisionO   s   rn   c           	      C   sZ   g }|D ]$}d}	 t | |||\}}t||r||d } }nnq	|||f qt|S )ze
    Determine multiplicities of factors for a multivariate polynomial
    using trial division.
    r   Trb   )r0   r   rc   rV   )	rd   re   urf   rg   rh   ri   rj   rk   rl   rl   rm   dmp_trial_divisionf   s   
rp   c                 C   s   ddl m} t| }t|d }t|d }|tdd | D }||d |}||d |d }|t| |}	|| ||	  }
|
t| |7 }
t|
d d }
|
S )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   c                 S   s   g | ]}|d  qS )rr   rl   .0Zcfrl   rl   rm   
<listcomp>   s    z)dup_zz_mignotte_bound.<locals>.<listcomp>rb   )	Zsympyrq   r   _ceilsqrtsumabsr   r8   )rd   rf   rq   dZdeltaZdelta2Z	eucl_normt1t2lcboundrl   rl   rm   dup_zz_mignotte_bound}   s   (r   c                 C   sL   t | ||}tt| ||}tt| |}|||d d|  | | S )z7Mignotte bound for multivariate polynomials in `K[X]`. rb   rr   )r9   ry   r   rx   r   rw   )rd   ro   rf   abnrl   rl   rm   dmp_zz_mignotte_bound   s   "r   c                 C   sJ  | 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 )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]_

    rr   )r5   rA   r/   r+   r'   r)   one)mrd   ghstrf   Merj   rk   ro   GHr   crz   STrl   rl   rm   dup_zz_hensel_step   s$   r   c              	   C   sv  t |}t||}|dkr$t|||| | d |}t|| | |gS | }|d }	ttt|d}
t|g| }|d|	 D ]}t	|t|| | |}q?t||	 | }||	d d D ]}t	|t|| | |}q[t
||| |\}}}t|| }t|| }t|| }t|| }td|
d D ]}t||||||||d \}}}}}qt| ||d|	 ||t| |||	d || 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`, ..., `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   rr   N)lenr   r;   ZgcdexrA   intrv   _logr   r   r	   r   ranger   dup_zz_hensel_lift)prd   Zf_listlrf   rk   r}   Fr   ri   rz   r   Zf_ir   r   r   _rl   rl   rm   r      s0   




(r   c                 C   s(   ||d kr
|| }|sdS | | dkS )Nrr   Tr   rl   )fcrj   plrl   rl   rm   _test_pl4  s
   r   c                    sl  t | }|dkr| gS | d }t| |}t| |}tt|||d d|  | | }t|d d|  |d| d   }ttdt|d }td| t| }	g }
td|	d D ];}t	|rj|| dkrkq^|
|}t| |}t|||s|q^t|||d }|
||f t|dk st|
dkr nq^t|
dd	 d
\}tttd| d }fdd|D }t| |||}tt|}t|}g d}}| }d| t|krt||D ] |dkrd} D ]
}||| d  }q|| }t|||sqn-|g} D ]}t||| |}qt|||}t||d }|d }|r3|| dkr3q|g}t  |  }|dkrZ|g} D ]}t||| |}qHt|||}|D ]}t||| |}q\t|||}t||}t||}|| |kr|} fdd|D }t||d }t||d } || t| |} nq|d7 }d| t|ks|| g S )z4Factor primitive square-free polynomials in `Z[x]`. rb   rr      r         c                 S   s   t | d S )Nrb   )r   )xrl   rl   rm   <lambda>[  s    z#dup_zz_zassenhaus.<locals>.<lambda>)keyc                    s   g | ]}t | qS rl   )r   )rt   Zff)r   rl   rm   ru   _  s    z%dup_zz_zassenhaus.<locals>.<listcomp>c                    s   g | ]}| vr|qS rl   rl   )rt   i)r   rl   rm   ru         )r   r8   r   r   ry   rw   rv   r   r   r]   convertr   r
   r   rc   r   minr   setr_   r   r+   rA   rF   r:   )rd   rf   r   r   Ar   BCZgammar~   r   Zpxr   ZfsqfxZfsqfr   Zmodularr   Zsorted_Tr   re   r   r   rj   r   r   r   ZT_SZG_normZH_normrl   )r   r   rm   dup_zz_zassenhaus;  s   

*$









7r   c                 C   sf   t | |}t| |}t| dd |}|r/tt|}| D ]}|| r.||d  r. dS qdS dS )z2Test irreducibility using Eisenstein's criterion. rb   Nrr   T)r   r   rC   r^   r   keys)rd   rf   r}   tcZe_fcZe_ffr   rl   rl   rm   dup_zz_irreducible_p  s   

r   Fc                 C   s  |j rz|| }}t| ||} W n ty   Y dS w |js"dS t| |}t| |}|dks8|dkr:|dkr:dS |sQt| |\}}||jksO|| dfgkrQdS t	| }g g }	}
t
|ddD ]
}|	d| |  q`t
|d ddD ]
}|
d| |  qstt|	|}	tt|
|}
t|	t|
d||}|t||rt||}|| krdS t| |}	|t|	|rt|	|}	||	krt|	|rdS t||}t|||krt||rdS dS )ai  
    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

    Frb   r   r   T)Zis_QQget_ringr   rZ   is_ZZr   r   dup_factor_listr   r   r   insertr-   r   r)   r7   is_negativer%   rM   dup_cyclotomic_prT   )rd   rf   ZirreducibleK0r}   r   coeffre   r   r   r   r   r   r   rl   rl   rm   r     sN   






r   c                 C   sP   |j |j  g}t|  D ]\}}tt|||||}t|||d  |}q|S )z1Efficiently generate n-th cyclotomic polynomial. rb   )r   r^   itemsr1   r   )r   rf   r   r   ri   rl   rl   rm   dup_zz_cyclotomic_poly  s
   r   c                    sv    j  j  gg}t|  D ]*\} fdd|D }|| td|D ]} fdd|D }|| q&q|S )Nc                    s    g | ]}t t| | qS rl   )r1   r   )rt   r   rf   r   rl   rm   ru          z-_dup_cyclotomic_decompose.<locals>.<listcomp>rb   c                    s   g | ]}t | qS rl   )r   )rt   rj   r   rl   rm   ru     r   )r   r^   r   extendr   )r   rf   r   ri   Qr   rl   r   rm   _dup_cyclotomic_decompose  s   
r   c                 C   s   t | |t| |}}t| dkrdS |dks|dvrdS tdd | dd D r,dS t| }t||}||s<|S g }td| |D ]}||vrP|| qE|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                 s   s    | ]}t |V  qd S N)boolrs   rl   rl   rm   	<genexpr>0      z+dup_zz_cyclotomic_factor.<locals>.<genexpr>r   rr   )r   r   r   anyr   is_onerc   )rd   rf   Zlc_fZtc_fr   r   r   r   rl   rl   rm   dup_zz_cyclotomic_factor  s"   


r   c                 C   s   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rEt||}|du rNt||}|t|ddfS )z:Factor square-free (non-primitive) polynomials in `Z[x]`. r   rb   USE_IRREDUCIBLE_IN_FACTORNUSE_CYCLOTOMIC_FACTORF)Zmultiple)	rF   r   r   r%   rW   r   r   r   rV   )rd   rf   contr   r   re   rl   rl   rm   dup_zz_factor_sqfB  s"   




r   c                 C   s   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rNt||}|du rWt||}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   )
rF   r   r   r%   rW   r   rT   r   r   rn   )rd   rf   r   r   r   r   re   rl   rl   rm   dup_zz_factor_  s&   .



r   c                 C   sv   || g}| D ]-}t |}t|D ]}|dkr%|||}|| }|dks||r.  dS q|| q|dd S )z,Wang/EEZ: Compute a set of valid divisors.  rb   N)ry   reversedgcdr   rc   )Ecsctrf   rg   rj   rk   rl   rl   rm   dmp_zz_wang_non_divisors  s   

r   c                    s   t t|  |d stdt |  |}t|s tdt|\}}t|r8| t|}}|d  fdd|D }	t|	||}
|
durW|||	fS td)z2Wang/EEZ: Test evaluation points for suitability. rb   zno luckc                    s   g | ]\}}t | qS rl   )rH   )rt   r   r   r   rf   vrl   rm   ru     s    z+dmp_zz_wang_test_points.<locals>.<listcomp>N)	rH   r   r[   rQ   rF   r   r   r%   r   )rd   r   r   r   ro   rf   r   r   r   r   Drl   r   rm   dmp_zz_wang_test_points  s   

r   c              	   C   s  g dgt | |d }}	}
|D ]R}t|
|}t||| }ttt |D ]6}d|| || }}\}}|| sH|| |d }}|| r;|dkr]t|t|||
||
|d}|	|< q'|| qtdd |	D rot	g g }}t
||D ]E\}}t|||
|}t||}||r|| }n|||}|| || }}t||||| }}t|||
|}|| || qy||r| ||fS g g }}t
||D ]\}}|t|||
| |t||d| qt| |t |d  ||} | ||fS )z0Wang/EEZ: Compute correct leading coefficients. r   rb   c                 s   s    | ]}| V  qd S r   rl   )rt   jrl   rl   rm   r     s    z*dmp_zz_wang_lead_coeffs.<locals>.<genexpr>)r   r   r   r   r   r,   r.   rc   r   rX   ziprH   r   r   r;   r<   )rd   r   r   r   r   r   ro   rf   r   Jr   r   r   rz   r   ri   r   r   r   ZCCZHHr}   Zccr   ZCCCZHHHrl   rl   rm   dmp_zz_wang_lead_coeffs  sF   
"








r   c              	   C   s  t | dkrK| \}}t||}t||}t||||\}}	}
t|||}t|	||}	t||||\}}t|	||||}	t||}t|	|}	||	g}|S | d g}
t| dd D ]}|
dt	||
d | qXg dgg}}t
| |
D ]\}}t||g|d g d|d|\}	}||	 || qsg ||d g }}t
|| D ]#\}}t||}t||}tt||||||}t||}|| q|S )z2Wang/EEZ: Solve univariate Diophantine equations. rr   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   rl   rl   rm   dup_zz_diophantine  s:   




 



r   c              	      s  |sDdd | D }t |}t|D ]0\}	}
|
sqt| ||	  }tt||D ]\}\}}t||
 }tt||  ||< q(q|S t|}t|  }|d |dd }}g g }}| D ]}|	t
||  |	t|||  q`t||| }d t|||| } fdd|D }t||D ]\}}t||| }qt| }t j| g| }t| } td|D ]}t|r nyt|| }t||d || }t|sIt| |d  }t|||| }t|D ]\}	}tt|d | ||	< qtt||D ]\}	\}}t|| ||	< qt||D ]\}}t||| }q4t| }qȇ fdd|D }|S )	z4Wang/EEZ: Solve multivariate Diophantine equations. c                 S   s   g | ]}g qS rl   rl   rt   r   rl   rl   rm   ru   A  s    z&dmp_zz_diophantine.<locals>.<listcomp>r   Nrb   c                    s   g | ]	}t |d  qS rb   )r   rt   r   )rf   r   rl   rm   ru   ]      r   c                    s   g | ]	}t | qS rl   )rB   r   )rf   r   ro   rl   rm   ru   }  r   )r   	enumerater   r   r;   rA   r'   r   r3   rc   r2   rI   r   r6   rB   r   r   r   mapr   r   r,   rJ   r>   	factorialr   r(   )r   r   r   rz   r   ro   rf   r   r   r   r   r   r   r   r   r   r   r   r   rd   r   r   r   r   ri   rl   )rf   r   ro   r   rm   r   >  s\   5


 r   c              
   C   s  | gt ||d }}}	t|}tt|dd D ]\}
}t|d |||
 ||
 |}|dt|||	|
 | qtt| |dd }t	t
d|d ||D ]\}}}t||d }}|d|d  ||d d }}tt	||D ]&\}
\}}tt|||	|||d |}|gt|dd d|d | ||
< qwt|j| g||}t||}t|t|||||}t|||}|t
d|D ]v}t||r nnt||||}t||d ||||}t||d s<t|||d |d |}t||||||d |}tt	||D ]\}
\}}t|t|d|d ||||}t||||||
< q
t|t|||||}t||||}qqQt|||| krIt|S )z-Wang/EEZ: Parallel Hensel lifting algorithm. rb   Nr   rr   )r   listr   r   rI   r   rB   maxr   r   r   rH   r   r   r   r   r*   r3   r   r   r   r,   rJ   r>   r   r   r4   rX   )rd   r   LCr   r   ro   rf   r   r   r   r   r   r   rz   r   r   wIr   r   r}   r   r   r   Zdjri   r   r   r   rl   rl   rm   dmp_zz_wang_hensel_lifting  sB    "&

r   Nc              	      s2  ddl m} ||tt|  |d  \}}t| | } t|}	du r0|dkr.dndt g  jg| df\}
}}}z)t| |||| \}}}t	| \}}t
|}|dkr_| gW S |||||fg}W n	 tyq   Y nw td}td}td}t
||k rt|D ]r} fd	d
t|D }t||
vr|
t| nqzt| |||| \}}}W n	 ty   Y qw t	| \}}t
|}|dur||kr||k rg |}}nqn|}|dkr| g  S ||||||f t
||kr nq|7 t
||k sd\}}}|D ]"\}}}}}t| }|dur(||k r'|}|}n|}|d7 }q|| \}}}}}| }zt| |||||| \} }}t| ||||	| }W n tyq   tdrmt|| d  Y S tdw g }|D ] } t| | \}}  t| | rt| | } ||  qv|S )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, ..., 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   )_randintrb   Nrr   ZEEZ_NUMBER_OF_CONFIGSZEEZ_NUMBER_OF_TRIESZEEZ_MODULUS_STEPc                    s   g | ]
}  qS rl   rl   r   rf   modZrandintrl   rm   ru     s    zdmp_zz_wang.<locals>.<listcomp>)Nr   r   ZEEZ_RESTART_IF_NEEDEDz3we need to restart algorithm with better parameters)Zsympy.testing.randtestr   dmp_zz_factorr   r   r\   r   zeror   r   r   r[   rW   r   tupleaddrc   r8   r   r   rX   dmp_zz_wangrG   r   r   r&   )rd   ro   rf   r   Zseedr   r   r   r   r   historyZconfigsr   rk   r   r   r   r   r   Zeez_num_configsZeez_num_triesZeez_mod_stepZrrZs_normZs_argr   Z_s_normZorig_fr   re   rg   rl   r   rm   r     s   

%



r   c           	      C   s   |st | |S t| |r|jg fS t| ||\}}t|||dk r+| t|||}}tdd t||D r;|g fS t|||\}}g }t	||dkr_t
|||}t|||}t| |||}t||d |d D ]\}}|d|g|f qi|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, ..., 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                 s       | ]}|d kV  qdS r   Nrl   rt   rz   rl   rl   rm   r   s  r   z dmp_zz_factor.<locals>.<genexpr>rb   )r   r   r   rG   r   r&   allr   rN   r   rU   r   rp   r   r   rV   )	rd   ro   rf   r   r   r   re   r   ri   rl   rl   rm   r   D  s$   $


r   c                    sJ      t|  } t| \}} fdd|D } |}||fS )z>Factor univariate polynomials into irreducibles in `QQ_I[x]`. c                    s    g | ]\}}t | |fqS rl   )r   rt   facr   r   K1rl   rm   ru     r   z#dup_qq_i_factor.<locals>.<listcomp>)as_AlgebraicFieldr   r   r   )rd   r   r   re   rl   r   rm   dup_qq_i_factor  s   r   c                 C   s   |  }t| ||} t| |\}}g }|D ]*\}}t||\}}	t|	||}
t|
d|\}}|||  ||  }|||f q|}|||}||fS )z>Factor univariate polynomials into irreducibles in `ZZ_I[x]`. r   )	get_fieldr   r   r?   rG   rc   r   )rd   r   r   r   re   new_factorsr   r   	fac_denomfac_numfac_num_ZZ_Icontentfac_primrl   rl   rm   dup_zz_i_factor  s   r  c                    sP      t|  } t| \}} fdd|D } |}||fS )z@Factor multivariate polynomials into irreducibles in `QQ_I[X]`. c                    s"   g | ]\}}t | |fqS rl   )r   r   r   r   ro   rl   rm   ru     s   " z#dmp_qq_i_factor.<locals>.<listcomp>)r   r   dmp_factor_listr   )rd   ro   r   r   re   rl   r  rm   dmp_qq_i_factor  s   r  c                 C   s   |  }t| |||} t| ||\}}g }|D ],\}}t|||\}	}
t|
|||}t|||\}}|||  |	|  }|||f q|}|||}||fS )z@Factor multivariate polynomials into irreducibles in `ZZ_I[X]`. )r   r   r  r@   rG   rc   r   )rd   ro   r   r   r   re   r   r   r   r   r   r  r  r  rl   rl   rm   dmp_zz_i_factor  s   r  c                 C   s   t | t| |}}t| |} |dkr|g fS |dkr"|| dfgfS t| || } }t| |\}}}t||j}t|dkrI|| |t |  fgfS ||j }	t	|D ] \}
\}}t
||j|}t|||\}}}t||	|}|||
< qRt|||}||fS )z<Factor univariate polynomials over algebraic number fields. r   rb   )r   r   rD   rT   rR   dup_factor_list_includedomr   unitr   r   rO   rL   rn   )rd   rf   r   r}   r   r   r   rk   re   r   r   rh   r   r   rl   rl   rm   dup_ext_factor  s&   


r  c                 C   s  |st | |S t| ||}t| ||} tdd t| |D r#|g fS t| ||| } }t| ||\}}}t|||j}t	|dkrF| g}n5t
|j||j g|d|}	t|D ]#\}
\}}t|||j|}t||||\}}}t||	||}|||
< qW|t||||fS )z>Factor multivariate polynomials over algebraic number fields. c                 s   r   r   rl   r   rl   rl   rm   r     r   z!dmp_ext_factor.<locals>.<genexpr>rb   r   )r  r   rE   r   r   rU   rS   dmp_factor_list_includer
  r   r   r   r  r   r   rP   rK   rp   )rd   ro   rf   r}   r   r   r   rk   re   r   r   rh   r   r   rl   rl   rm   dmp_ext_factor  s$   

r  c                 C   s`   t | ||j} t| |j|j\}}t|D ]\}\} }t | |j||f||< q|||j|fS )z2Factor univariate polynomials over finite fields. )r   r
  r   r   r   r   )rd   rf   r   re   r   ri   rl   rl   rm   dup_gf_factor  s
   r  c                 C   s   t d)z4Factor multivariate polynomials over finite fields. z+multivariate polynomials over finite fields)NotImplementedError)rd   ro   rf   rl   rl   rm   dmp_gf_factor  s   r  c                 C   s4  t | |\}} t| |\}} |jrt| |\}}n|jr$t| |\}}n|jr/t| |\}}n|jr:t	| |\}}n|j
sK|| }}t| ||} nd}|jrc| }t| ||\}} t| ||} n|}|jrpt| |\}}n7|jrt| d|\} }	t| |	|j\}}t|D ]\}
\} }t| |	||f||
< q|||j}ntd| |jrt|D ]\}
\} }t| |||f||
< q|||}|||}|rt|D ]'\}
\} }t| |}t| ||} t| ||} | |f||
< |||||}q|||}|}|r|d|j |j!g|f || t"|fS );Factor univariate polynomials into irreducibles in `K[x]`. Nr   #factorization not supported over %s)#r#   rF   is_FiniteFieldr  is_Algebraicr  is_GaussianRingr  is_GaussianFieldr   is_Exact	get_exactr   is_Fieldr   r?   r   r   is_Polyr!   r  r
  r   r"   r   rY   quor8   r=   mulpowr   r   r   rV   )rd   r   r   r   r   re   
K0_inexactrf   denomro   r   ri   max_normrl   rl   rm   r     sZ   
r   c                 C   sT   t | |\}}|st|gdfgS t|d d ||}||d d fg|dd  S )r  rb   r   N)r   r   r;   )rd   rf   r   re   r   rl   rl   rm   r	  W  s
   r	  c                 C   s  |st | |S t| ||\}} t| ||\}} |jr$t| ||\}}n|jr1t| ||\}}n	|jr=t| ||\}}n|j	rIt
| ||\}}n|js[|| }}t| |||} nd}|jru| }t| |||\}	} t| |||} n|}|jrt| ||\}
} }t| ||\}}t|D ]\}\} }t| |
|||f||< qn7|jrt| ||\} }t| ||j\}}t|D ]\}\} }t| |||f||< q|||j}ntd| |jr:t|D ]\}\} }t| ||||f||< q|||}|||	}|r:t|D ]+\}\} }t| ||}t| |||} t| |||} | |f||< | ||!||}q|||}|}tt"|D ]%\}}|sIq@d||  d d|  |j#i}|$dt%||||f q@|| t&|fS )=Factor multivariate polynomials into irreducibles in `K[X]`. Nr  )r   r   r   )'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  r
  r"   r   rY   r  r9   r>   r  r  r   r   r   r   rV   )rd   ro   r   r   r   r   re   r  rf   r   Zlevelsr   r   ri   r!  r   Ztermrl   rl   rm   r  b  sl   
r  c                 C   sf   |st | |S t| ||\}}|st||dfgS t|d d |||}||d d fg|dd  S )r"  rb   r   N)r	  r  r   r<   )rd   ro   rf   r   re   r   rl   rl   rm   r    s   
r  c                 C   s   t | d|S )z_
    Returns ``True`` if a univariate polynomial ``f`` has no factors
    over its domain.
    r   )dmp_irreducible_p)rd   rf   rl   rl   rm   dup_irreducible_p  s   r$  c                 C   s<   t | ||\}}|sdS t|dkrdS |d \}}|dkS )za
    Returns ``True`` if a multivariate polynomial ``f`` has no factors
    over its domain.
    Trb   Fr   )r  r   )rd   ro   rf   r   re   ri   rl   rl   rm   r#    s   r#  )F)NN)__doc__Zsympy.polys.galoistoolsr   r   r   r   r   r   r   r	   r
   r   r   Z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$   Z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>   Zsympy.polys.densetoolsr?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   Zsympy.polys.euclidtoolsrN   rO   rP   Zsympy.polys.sqfreetoolsrQ   rR   rS   rT   rU   Zsympy.polys.polyutilsrV   Zsympy.polys.polyconfigrW   Zsympy.polys.polyerrorsrX   rY   rZ   r[   Zsympy.ntheoryr\   r]   r^   Zsympy.utilitiesr_   Zmathr`   rv   ra   r   rn   rp   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	  r  r  r$  r#  rl   rl   rl   rm   <module>   sf    4hpD<	99g
L,K60D
4 ABM