a
    RG5d9                     @   s  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d Zmdd Zndd Zodd Zpdd Zqdd Zrdd Zsdd Ztdd ZudZd!d"Zvd#d$ Zwd%d& Zxd'd( Zyd)d* Zzd+d, Z{d-d. Z|d/d0 Z}d1d2 Z~d3d4 Zd5d6 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.     )_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                 C   sP   g }|D ]>}d}t | ||\}}|s8||d  } }qq8q|||f qt|S )zc
    Determine multiplicities of factors for a univariate polynomial
    using trial division.
    r      )r0   appendrW   )ffactorsKresultfactorkqr rj   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/polys/factortools.pydup_trial_divisionO   s    rl   c           	      C   sX   g }|D ]F}d}t | |||\}}t||r@||d  } }qq@q|||f qt|S )ze
    Determine multiplicities of factors for a multivariate polynomial
    using trial division.
    r   r`   )r1   r   ra   rW   )	rb   rc   urd   re   rf   rg   rh   ri   rj   rj   rk   dmp_trial_divisionf   s    
rn   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 )rp   rj   .0cfrj   rj   rk   
<listcomp>       z)dup_zz_mignotte_bound.<locals>.<listcomp>r`   )	(sympy.functions.combinatorial.factorialsro   r   _ceilsqrtsumabsr   r9   )rb   rd   ro   ddeltaZdelta2Z	eucl_normt1t2lcboundrj   rj   rk   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]`. r`   rp   )r:   rz   r   ry   r   rx   )rb   rm   rd   abnrj   rj   rk   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]_

    rp   )r6   rB   r0   r,   r(   r*   one)mrb   ghstrd   Merh   ri   rm   GHr   cr{   STrj   rj   rk   dup_zz_hensel_step   s$    r   c              	   C   sx  t |}t||}|dkrHt|||| | d |}t|| | |gS | }|d }	ttt|d}
t|g| }|d|	 D ]}t	|t|| | |}q~t||	 | }||	d d D ]}t	|t|| | |}qt
||| |\}}}t|| }t|| }t|| }t|| }td|
d D ],}t||||||||d  \}}}}}qt| ||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,\ \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]_

    r`   r   rp   N)lenr   r<   gcdexrB   intrw   _logr   r   r
   r   ranger   dup_zz_hensel_lift)prb   Zf_listlrd   ri   r   Fr   rg   r{   r   Zf_ir   r   r   _rj   rj   rk   r      s0    




*r   c                 C   s(   ||d kr|| }|sdS | | dkS )Nrp   Tr   rj   )fcrh   plrj   rj   rk   _test_pl3  s
    r   c                    s~  t | }|dkr| gS ddlm} | 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 ]|}||r|| dkrq||}t| |}t|||sqt|||d }|||f t|dk s>t|dkr qFqt|d	d
 d\}ttt	d| d }fdd|D }t| |||}t
t|}t|}g d }}| }d| t|krtt||D ] |dkr$d} D ]}||| d  }q|| }t|||sqn\|g} D ]}t||| |}q.t|||}t||d }|d }|r|| dkrq|g}t  |  }|dkr|g} D ]}t||| |}qt|||}|D ]}t||| |}qt|||}t||}t||}|| |kr|} fdd|D }t||d }t||d } || t| |} qq|d7 }q|| g S )z4Factor primitive square-free polynomials in `Z[x]`. r`   r   )isprimerp            c                 S   s   t | d S )Nr`   )r   )xrj   rj   rk   <lambda>\  ru   z#dup_zz_zassenhaus.<locals>.<lambda>)keyc                    s   g | ]}t | qS rj   )r   )rr   ff)r   rj   rk   rt   `  ru   z%dup_zz_zassenhaus.<locals>.<listcomp>c                    s   g | ]}| vr|qS rj   rj   )rr   i)r   rj   rk   rt     ru   )r   sympy.ntheoryr   r9   r   r   rz   rx   rw   r   r   convertr   r   r   ra   r   minr   setr]   r   r,   rB   rG   r;   )rb   rd   r   r   r   Ar   BCgammar   r   Zpxr   ZfsqfxZfsqfr   Zmodularr   Zsorted_Tr   rc   r   r   rh   r   r   r   ZT_SZG_normZH_normrj   )r   r   rk   dup_zz_zassenhaus:  s    

*$









r   c                 C   sn   t | |}t| |}t| dd |}|rjddlm} |t|}| D ]}|| rJ||d  rJ dS qJdS )z2Test irreducibility using Eisenstein's criterion. r`   Nr   	factorintrp   T)r   r   rD   r   r   r   keys)rb   rd   r   tcZe_fcr   Ze_ffr   rj   rj   rk   dup_zz_irreducible_p  s    

r   Fc                 C   s  |j r<z||  }}t| ||} W qF ty8   Y dS 0 n
|jsFdS t| |}t| |}|dksr|dkrv|dkrvdS |st| |\}}||jks|| dfgkrdS t	| }g g  }	}
t
|ddD ]}|	d| |  qt
|d ddD ]}|
d| |  qtt|	|}	tt|
|}
t|	t|
d||}|t||rJt||}|| krXdS t| |}	|t|	|r~t|	|}	||	krt|	|rdS t||}t|||krt||rdS dS )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.

    Fr`   r   r   T)is_QQget_ringr   r[   is_ZZr   r   dup_factor_listr   r   r   insertr.   r   r*   r8   is_negativer&   rN   dup_cyclotomic_prU   )rb   rd   ZirreducibleK0r   r   coeffrc   r   r   r   r   r   r   rj   rj   rk   r     sL    








r   c                 C   s\   ddl m} |j|j g}||  D ]0\}}tt|||||}t|||d  |}q&|S )z1Efficiently generate n-th cyclotomic polynomial. r   r   r`   )r   r   r   itemsr2   r   )r   rd   r   r   r   rg   rj   rj   rk   dup_zz_cyclotomic_poly  s    r   c                    s   ddl m}  j j gg}||  D ]T\} fdd|D }|| td|D ]"} fdd|D }|| qXq(|S )Nr   r   c                    s    g | ]}t t| | qS rj   )r2   r   )rr   r   rd   r   rj   rk   rt     ru   z-_dup_cyclotomic_decompose.<locals>.<listcomp>r`   c                    s   g | ]}t | qS rj   )r   )rr   rh   r   rj   rk   rt     ru   )r   r   r   r   extendr   )r   rd   r   r   rg   Qr   rj   r   rk   _dup_cyclotomic_decompose  s    
r   c                 C   s   t | |t| | }}t| dkr&dS |dks6|dvr:dS tdd | dd D rXdS t| }t||}||sx|S g }td| |D ]}||vr|| q|S d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   Nr`   )r   r`   c                 s   s   | ]}t |V  qd S )N)boolrq   rj   rj   rk   	<genexpr><  ru   z+dup_zz_cyclotomic_factor.<locals>.<genexpr>r   rp   )r   r   r   anyr   is_onera   )rb   rd   Zlc_fZtc_fr   r   r   r   rj   rj   rk   dup_zz_cyclotomic_factor"  s     

r   c                 C   s   t | |\}}t|}t||dk r6| t|| }}|dkrF|g fS |dkrX||gfS tdrtt||rt||gfS d}tdrt||}|du rt||}|t|ddfS )z:Factor square-free (non-primitive) polynomials in `Z[x]`. r   r`   USE_IRREDUCIBLE_IN_FACTORNUSE_CYCLOTOMIC_FACTORF)multiple)	rG   r   r   r&   rX   r   r   r   rW   )rb   rd   contr   r   rc   rj   rj   rk   dup_zz_factor_sqfN  s"    




r   c                 C   s   t | |\}}t|}t||dk r6| t|| }}|dkrF|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rt||}|du rt||}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   r`   r   Nr   )
rG   r   r   r&   rX   r   rU   r   r   rl   )rb   rd   r   r   r   r   rc   rj   rj   rk   dup_zz_factork  s&    .



r   c                 C   sp   || g}| D ]T}t |}t|D ]4}|dkrD|||}|| }q&||r"  dS q"|| q|dd S )z,Wang/EEZ: Compute a set of valid divisors.  r`   N)rz   reversedgcdr   ra   )Ecsctrd   re   rh   ri   rj   rj   rk   dmp_zz_wang_non_divisors  s    



r   c                    s   t t|  |d s tdt |  |}t|s@tdt|\}}t|rp| t| }}|d  fdd|D }	t|	||}
|
dur|||	fS tddS )z2Wang/EEZ: Test evaluation points for suitability. r`   zno luckc                    s   g | ]\}}t | qS rj   )rI   )rr   r   r   r   rd   vrj   rk   rt     ru   z+dmp_zz_wang_test_points.<locals>.<listcomp>N)	rI   r   r\   rR   rG   r   r   r&   r   )rb   r   r   r   rm   rd   r   r   r   r   Drj   r   rk   dmp_zz_wang_test_points  s    

r   c              	   C   s  g dgt | |d   }}	}
|D ]}t|
|}t||| }ttt |D ]f}d|| ||   }}\}}|| s|| |d  }}qn|dkrNt|t|||
||
|d }|	|< qN|| q"t|	st	g g  }}t
||D ]\}}t|||
|}t||}||r|| }n4|||}|| ||  }}t|||||  }}t|||
|}|| || q||r| ||fS g g  }}t
||D ]2\}}|t|||
| |t||d| qt| |t |d  ||} | ||fS )z0Wang/EEZ: Compute correct leading coefficients. r   r`   )r   r   r   r   r   r-   r/   ra   allrY   ziprI   r   r   r<   r=   )rb   r   r   r   r   r   rm   rd   r   Jr   r   r   r{   r   rg   r   r   r   CCHHr   ccr   ZCCCZHHHrj   rj   rk   dmp_zz_wang_lead_coeffs  sB    
$





r   c              	   C   s  t | dkr| \}}t||}t||}t||||\}}	}
t|||}t|	||}	t||||\}}t|	||||}	t||}t|	|}	||	g}n| d g}
t| dd D ]}|
dt	||
d | qg dgg }}t
| |
D ]<\}}t||g|d g d|d|\}	}||	 || qg ||d g  }}t
|| D ]H\}}t||}t||}tt||||||}t||}|| q@|S )z2Wang/EEZ: Solve univariate Diophantine equations. rp   r   r`   r   )r   r   r
   r   r   r   r   r   r   r,   r   dmp_zz_diophantinera   r	   )r   r   r   rd   r   r   rb   r   r   r   r   rh   re   r   r   ri   rj   rj   rk   dup_zz_diophantine  s8    





 



r   c              	      s  |sdd | D }t |}t|D ]`\}	}
|
s0q"t| ||	  }tt||D ]0\}\}}t||
 }tt||  ||< qPq"n,t|}t|  }|d |dd  }}g g  }}| D ].}|	t
||  |	t|||  qt||| }d t|||| } fdd|D }t||D ]\}}t||| }q:t| }t j| g| }t| } td|D ]}t|r qt|| }t||d || }t|st| |d  }t|||| }t|D ]&\}	}tt|d | ||	< qtt||D ] \}	\}}t|| ||	< qDt||D ]\}}t||| }qpt| }q fdd|D }|S )	z4Wang/EEZ: Solve multivariate Diophantine equations. c                 S   s   g | ]}g qS rj   rj   rr   r   rj   rj   rk   rt   M  ru   z&dmp_zz_diophantine.<locals>.<listcomp>r   Nr`   c                    s   g | ]}t |d  qS r`   )r   rr   r   )rd   r   rj   rk   rt   i  ru   r   c                    s   g | ]}t | qS rj   )rC   r   )rd   r   rm   rj   rk   rt     ru   )r   	enumerater   r   r<   rB   r(   r   r4   ra   r3   rJ   r   r7   rC   r   r   r   mapr   r   r-   rK   r?   	factorialr   r)   )r   r   r   r{   r   rm   rd   r   r   r   r   r   jr   r   r   r   r   r   rb   r   r   r   r   rg   rj   )rd   r   rm   r   rk   r   J  sV     

 r   c              
   C   s  | gt ||d   }}}	t|}tt|dd D ]>\}
}t|d |||
 ||
 |}|dt|||	|
 | q6tt| |dd }t	t
d|d ||D ]\}}}t||d  }}|d|d  ||d d  }}tt	||D ]L\}
\}}tt|||	|||d |}|gt|dd d|d | ||
< qt|j| g||}t||}t|t|||||}t|||}|t
d|D ]}t||r qt||||}t||d ||||}t||d st|||d |d |}t||||||d |}tt	||D ]>\}
\}}t|t|d|d ||||}t||||||
< qt|t|||||}t||||}qqt|||| krtn|S dS )z-Wang/EEZ: Parallel Hensel lifting algorithm. r`   Nr   rp   )r   listr   r   rJ   r   rC   maxr   r   r   rI   r   r   r   r   r+   r4   r   r   r   r-   rK   r?   r   r   r5   rY   )rb   r   LCr   r   rm   rd   r   r   r   r   r   r   r{   r   r   wIr   r   r   r   r   r   djrg   r   r   r   rj   rj   rk   dmp_zz_wang_hensel_lifting  s@    ""&
r   Nc              	      sH  ddl m} t|tt|  |d  \}}t| | } ||}	du r`|dkr\dndt g  jg| df\}
}}}zRt| |||| \}}}t	| \}}t
|}|dkr| gW S |||||fg}W n ty   Y n0 td}td}td}t
||k rt|D  ]} fd	d
t|D }t||
vr|
t| nqzt| |||| \}}}W n ty   Y qY n0 t	| \}}t
|}|dur||kr||k rg | }}nqn|}|dkr| g  S ||||||f t
||kr qq|7 qd\}}}|D ]D\}}}}}t| }|durb||k rf|}|}n|}|d7 }q,|| \}}}}}| }z4t| |||||| \} }}t| ||||	| }W n< ty   tdrt|| d  Y S tdY n0 g }|D ]@} t| | \}}  t| | r6t| | } ||  q|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, \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   )	nextprimer`   Nrp   EEZ_NUMBER_OF_CONFIGSEEZ_NUMBER_OF_TRIESEEZ_MODULUS_STEPc                    s   g | ]}  qS rj   rj   r   rd   modrandintrj   rk   rt     ru   zdmp_zz_wang.<locals>.<listcomp>)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\   rX   r   tupleaddra   r9   r   r   rY   dmp_zz_wangrH   r   r   r'   )rb   rm   rd   r   seedr   r   r   r   r   historyconfigsr   ri   r   r   r   r   r   Zeez_num_configsZeez_num_triesZeez_mod_steprrZs_normZs_argr   Z_s_normorig_fr   rc   re   rj   r   rk   r    s    











r  c           	      C   s   |st | |S t| |r"|jg fS t| ||\}}t|||dk rV| t||| }}tdd t||D rv|g fS t|||\}}g }t	||dkrt
|||}t|||}t| |||}t||d |d D ]\}}|d|g|f q|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                 s   s   | ]}|d kV  qdS r   Nrj   rr   r{   rj   rj   rk   r     ru   z dmp_zz_factor.<locals>.<genexpr>r`   )r   r   r   rH   r   r'   r   r   rO   r   rV   r  rn   r   r   rW   )	rb   rm   rd   r   r   r   rc   r   rg   rj   rj   rk   r   P  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 rj   )r   rr   facr   r   K1rj   rk   rt     ru   z#dup_qq_i_factor.<locals>.<listcomp>)as_AlgebraicFieldr   r   r   )rb   r   r   rc   rj   r  rk   dup_qq_i_factor  s    r  c                 C   s   |  }t| ||} t| |\}}g }|D ]T\}}t||\}}	t|	||}
t|
d|\}}|||  ||  }|||f q*|}|||}||fS )z>Factor univariate polynomials into irreducibles in `ZZ_I[x]`. r   )	get_fieldr   r  r@   rH   ra   r   )rb   r   r  r   rc   new_factorsr
  r   	fac_denomfac_numfac_num_ZZ_Icontentfac_primrj   rj   rk   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 rj   )r   r	  r   r  rm   rj   rk   rt     ru   z#dmp_qq_i_factor.<locals>.<listcomp>)r  r   dmp_factor_listr   )rb   rm   r   r   rc   rj   r  rk   dmp_qq_i_factor  s    r  c                 C   s   |  }t| |||} t| ||\}}g }|D ]X\}}t|||\}	}
t|
|||}t|||\}}|||  |	|  }|||f q.|}|||}||fS )z@Factor multivariate polynomials into irreducibles in `ZZ_I[X]`. )r  r   r  rA   rH   ra   r   )rb   rm   r   r  r   rc   r  r
  r   r  r  r  r  r  rj   rj   rk   dmp_zz_i_factor  s    r  c                 C   s   t | t| | }}t| |} |dkr.|g fS |dkrD|| dfgfS t| ||  } }t| |\}}}t||j}t|dkr|| |t |  fgfS ||j }	t	|D ]@\}
\}}t
||j|}t|||\}}}t||	|}|||
< qt|||}||fS )z<Factor univariate polynomials over algebraic number fields. r   r`   )r   r   rE   rU   rS   dup_factor_list_includedomr   unitr   r   rP   rM   rl   )rb   rd   r   r   r   r   r   ri   rc   r   r   rf   r   r   rj   rj   rk   dup_ext_factor  s&    


r  c                 C   s  |st | |S t| ||}t| ||} tdd t| |D rF|g fS t| |||  } }t| ||\}}}t|||j}t	|dkr| g}njt
|j||j g|d|}	t|D ]F\}
\}}t|||j|}t||||\}}}t||	||}|||
< q|t||||fS )z>Factor multivariate polynomials over algebraic number fields. c                 s   s   | ]}|d kV  qdS r  rj   r  rj   rj   rk   r     ru   z!dmp_ext_factor.<locals>.<genexpr>r`   r   )r  r   rF   r   r   rV   rT   dmp_factor_list_includer  r   r   r   r  r   r   rQ   rL   rn   )rb   rm   rd   r   r   r   r   ri   rc   r   r   rf   r   r   rj   rj   rk   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   )rb   rd   r   rc   r   rg   rj   rj   rk   dup_gf_factor  s
    r!  c                 C   s   t ddS )z4Factor multivariate polynomials over finite fields. z+multivariate polynomials over finite fieldsN)NotImplementedError)rb   rm   rd   rj   rj   rk   dmp_gf_factor  s    r#  c                 C   sD  t | |\}} t| |\}} |jr4t| |\}}n|jrLt| |\}}n|jrdt| |\}}n|jr|t	| |\}}n|j
s||  }}t| ||} nd}|jr| }t| ||\}} t| ||} n|}|jrt| |\}}nr|jrNt| d|\} }	t| |	|j\}}t|D ]"\}
\} }t| |	||f||
< q|||j}ntd| |jrt|D ]"\}
\} }t| |||f||
< qj|||}|||}|rt|D ]P\}
\} }t| |}t| ||} t| ||} | |f||
< |||||}q|||}|}|r4|d|j |j!g|f || t"|fS );Factor univariate polynomials into irreducibles in `K[x]`. Nr   #factorization not supported over %s)#r$   rG   is_FiniteFieldr!  is_Algebraicr  is_GaussianRingr  is_GaussianFieldr  is_Exact	get_exactr   is_Fieldr   r@   r   r   is_Polyr"   r  r  r   r#   r   rZ   quor9   r>   mulpowr   r   r   rW   )rb   r   r   r   r   rc   
K0_inexactrd   denomrm   r   rg   max_normrj   rj   rk   r   !  sZ    
r   c                 C   sX   t | |\}}|s"t|gdfgS t|d d ||}||d d fg|dd  S dS )r$  r`   r   N)r   r   r<   )rb   rd   r   rc   r   rj   rj   rk   r  c  s
    r  c                 C   s  |st | |S t| ||\}} t| ||\}} |jrHt| ||\}}n:|jrbt| ||\}}n |jr|t| ||\}}n|j	rt
| ||\}}n|js||  }}t| |||} nd}|jr| }t| |||\}	} t| |||} n|}|jrLt| ||\}
} }t| ||\}}t|D ]$\}\} }t| |
|||f||< q$nr|jrt| ||\} }t| ||j\}}t|D ]"\}\} }t| |||f||< q~|||j}ntd| |jrt|D ]$\}\} }t| ||||f||< q|||}|||	}|rt|D ]V\}\} }t| ||}t| |||} t| |||} | |f||< | ||!||}q|||}|}tt"|D ]J\}}|sqd||  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%   rH   r&  r#  r'  r   r(  r  r)  r  r*  r+  r   r,  r   rA   r   r    r   r   r!   r-  r"   r  r  r#   r   rZ   r.  r:   r?   r/  r0  r   r   r   r   rW   )rb   rm   r   r   r   r   rc   r1  rd   r2  levelsr   r   rg   r3  r   termrj   rj   rk   r  n  sj    
r  c                 C   sj   |st | |S t| ||\}}|s2t||dfgS t|d d |||}||d d fg|dd  S dS )r4  r`   r   N)r  r  r   r=   )rb   rm   rd   r   rc   r   rj   rj   rk   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)rb   rd   rj   rj   rk   dup_irreducible_p  s    r8  c                 C   s@   t | ||\}}|sdS t|dkr(dS |d \}}|dkS dS )za
    Returns ``True`` if a multivariate polynomial ``f`` has no factors
    over its domain.
    Tr`   Fr   N)r  r   )rb   rm   rd   r   rc   rg   rj   rj   rk   r7    s    r7  )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.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   sympy.polys.densetoolsr@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   sympy.polys.euclidtoolsrO   rP   rQ   sympy.polys.sqfreetoolsrR   rS   rT   rU   rV   sympy.polys.polyutilsrW   sympy.polys.polyconfigrX   sympy.polys.polyerrorsrY   rZ   r[   r\   sympy.utilitiesr]   mathr^   rw   r_   r   rl   rn   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  r8  r7  rj   rj   rj   rk   <module>   sd   4hpD;	99i
S,K60D4
 ABM