a
    <b                     @   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   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      )r/   appendrV   )ffactorsKresultfactorkqr rl   g/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/polys/factortools.pydup_trial_divisionO   s    rn   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   rb   )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   .0cfrl   rl   rm   
<listcomp>       z)dup_zz_mignotte_bound.<locals>.<listcomp>rb   )	Z(sympy.functions.combinatorial.factorialsrq   r   _ceilsqrtsumabsr   r8   )rd   rf   rq   d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   r{   r   rz   r   ry   )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   cr|   STrl   rl   rm   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]_

    rb   r   rr   N)lenr   r;   ZgcdexrA   intrx   _logr   r   r	   r   ranger   dup_zz_hensel_lift)prd   Zf_listlrf   rk   r   Fr   ri   r|   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                    sp  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 ]z}t	|r|| dkrq|
|}t| |}t|||sqt|||d }|
||f t|dk s0t|
dkr q8qt|
dd	 d
\}tttd| d }fdd|D }t| |||}tt|}t|}g d }}| }d| t|krft||D ] |dkrd} D ]}||| d  }q|| }t|||srqn\|g} D ]}t||| |}q t|||}t||d }|d }|rr|| dkrr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]`. rb   rr      r         c                 S   s   t | d S )Nrb   )r   )xrl   rl   rm   <lambda>[  rw   z#dup_zz_zassenhaus.<locals>.<lambda>)keyc                    s   g | ]}t | qS rl   )r   )rt   ff)r   rl   rm   rv   _  rw   z%dup_zz_zassenhaus.<locals>.<listcomp>c                    s   g | ]}| vr|qS rl   rl   )rt   i)r   rl   rm   rv     rw   )r   r8   r   r   r{   ry   rx   r   r   r]   convertr   r
   r   rc   r   minr   setr_   r   r+   rA   rF   r:   )rd   rf   r   r   Ar   BC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    

*$









r   c                 C   sb   t | |}t| |}t| dd |}|r^tt|}| D ]}|| r>||d  r> dS q>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 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 )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     sL    








r   c                 C   sP   |j |j  g}t|  D ]0\}}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 ]T\} fdd|D }|| td|D ]"} fdd|D }|| qLq|S )Nc                    s    g | ]}t t| | qS rl   )r1   r   )rt   r   rf   r   rl   rm   rv     rw   z-_dup_cyclotomic_decompose.<locals>.<listcomp>rb   c                    s   g | ]}t | qS rl   )r   )rt   rj   r   rl   rm   rv     rw   )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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   Nrb   )r   rb   c                 s   s   | ]}t |V  qd S )N)boolrs   rl   rl   rm   	<genexpr>0  rw   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 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   rb   USE_IRREDUCIBLE_IN_FACTORNUSE_CYCLOTOMIC_FACTORF)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 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   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   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.  rb   N)r{   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|rp| t| }}|d  fdd|D }	t|	||}
|
dur|||	fS tddS )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   rv     rw   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 ]}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   rb   )r   r   r   r   r   r,   r.   rc   allrX   ziprH   r   r   r;   r<   )rd   r   r   r   r   r   ro   rf   r   Jr   r   r   r|   r   ri   r   r   r   CCZHHr   ccr   ZCCCZHHHrl   rl   rm   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. 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  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 rl   rl   rt   r   rl   rl   rm   rv   A  rw   z&dmp_zz_diophantine.<locals>.<listcomp>r   Nrb   c                    s   g | ]}t |d  qS rb   )r   rt   r   )rf   r   rl   rm   rv   ]  rw   r   c                    s   g | ]}t | qS rl   )rB   r   )rf   r   ro   rl   rm   rv   }  rw   )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   r|   r   ro   rf   r   r   r   r   r   jr   r   r   r   r   r   rd   r   r   r   r   ri   rl   )rf   r   ro   r   rm   r   >  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. 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   r|   r   r   wIr   r   r   r   r   r   Zdjri   r   r   r   rl   rl   rm   dmp_zz_wang_hensel_lifting  s@    ""&
r   Nc              	      sH  ddl m} ||tt|  |d  \}}t| | } 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   )_randintrb   Nrr   ZEEZ_NUMBER_OF_CONFIGSZEEZ_NUMBER_OF_TRIESZEEZ_MODULUS_STEPc                    s   g | ]}  qS rl   rl   r   rf   modrandintrl   rm   rv     rw   zdmp_zz_wang.<locals>.<listcomp>)Nr   r   ZEEZ_RESTART_IF_NEEDEDz3we need to restart algorithm with better parameters)Zsympy.core.randomr   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   seedr   r   r   r   r   historyZconfigsr   rk   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   re   rg   rl   r   rm   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   Nrl   rt   r|   rl   rl   rm   r   s  rw   z dmp_zz_factor.<locals>.<genexpr>rb   )r   r   r   rG   r   r&   r   r   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   rv     rw   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|	||}
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   rv     rw   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 ]X\}}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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   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 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   rl   r   rl   rl   rm   r     rw   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dS )z4Factor multivariate polynomials over finite fields. z+multivariate polynomials over finite fieldsN)NotImplementedError)rd   ro   rf   rl   rl   rm   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#   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   sX   t | |\}}|s"t|gdfgS t|d d ||}||d d fg|dd  S 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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$   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  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 )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 dS )za
    Returns ``True`` if a multivariate polynomial ``f`` has no factors
    over its domain.
    Trb   Fr   N)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_   mathr`   rx   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>   sd   4hpD<	99g
L,K60D4
 ABM