a
    RG5d                     @   s  d Z ddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	d
Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd d!Zd"d# Zdd$d%Zd&d' Zd(d) Z d*d+ Z!d,d- Z"d.d/ Z#d0d1 Z$d2d3 Z%d4d5 Z&d6d7 Z'd8d9 Z(d:d; Z)d<d= Z*d>d? Z+d@dA Z,dBdC Z-dDdE Z.dFdG Z/dHdI Z0dJdK Z1dLdM Z2dNdO Z3dPdQ Z4dRdS Z5dTdU Z6dVdW Z7dXdY Z8dZd[ Z9d\d] Z:d^d_ Z;d`da Z<dbdc Z=ddde Z>dfdg Z?dhdi Z@djdk ZAdldm ZBdndo ZCdpdq ZDdrds ZEeDeEdtZFdudv ZGdwdx ZHdydz ZIdd|d}ZJd~d ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSeMeReSdZTdddZUdd ZVdd ZWdd ZXdd ZYdddZZdd Z[dS )zADense univariate polynomials with coefficients in Galois fields.     )ceilsqrtprod)uniform)
SYMPY_INTS)query)ExactQuotientFailed)_sort_factorsNc           
      C   s^   t ||jd}|j}t| |D ]6\}}|| }|||\}}	}	|||| |  7 }q|| S )aH  
    Chinese Remainder Theorem.

    Given a set of integer residues ``u_0,...,u_n`` and a set of
    co-prime integer moduli ``m_0,...,m_n``, returns an integer
    ``u``, such that ``u = u_i mod m_i`` for ``i = ``0,...,n``.

    Examples
    ========

    Consider a set of residues ``U = [49, 76, 65]``
    and a set of moduli ``M = [99, 97, 95]``. Then we have::

       >>> from sympy.polys.domains import ZZ
       >>> from sympy.polys.galoistools import gf_crt

       >>> gf_crt([49, 76, 65], [99, 97, 95], ZZ)
       639985

    This is the correct result because::

       >>> [639985 % m for m in [99, 97, 95]]
       [49, 76, 65]

    Note: this is a low-level routine with no error checking.

    See Also
    ========

    sympy.ntheory.modular.crt : a higher level crt routine
    sympy.ntheory.modular.solve_congruence

    start)r   onezerozipgcdex)
UMKpvumes_ r   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/polys/galoistools.pygf_crt   s    "r   c                 C   sX   g g  }}t | |jd}| D ]0}|||  |||d |d |  q|||fS )a  
    First part of the Chinese Remainder Theorem.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_crt1

    >>> gf_crt1([99, 97, 95], ZZ)
    (912285, [9215, 9405, 9603], [62, 24, 12])

    r
   r   )r   r   appendr   )r   r   ESr   r   r   r   r   gf_crt19   s    
 r!   c                 C   s>   |j }t| |||D ] \}}}	}
||	||
 |  7 }q|| S )a`  
    Second part of the Chinese Remainder Theorem.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_crt2

    >>> U = [49, 76, 65]
    >>> M = [99, 97, 95]
    >>> p = 912285
    >>> E = [9215, 9405, 9603]
    >>> S = [62, 24, 12]

    >>> gf_crt2(U, M, p, E, S, ZZ)
    639985

    )r   r   )r   r   r   r   r    r   r   r   r   r   r   r   r   r   gf_crt2Q   s    r"   c                 C   s   | |d kr| S | | S dS )z
    Coerce ``a mod p`` to an integer in the range ``[-p/2, p/2]``.

    Examples
    ========

    >>> from sympy.polys.galoistools import gf_int

    >>> gf_int(2, 7)
    2
    >>> gf_int(5, 7)
    -2

       Nr   ar   r   r   r   gf_intm   s    r&   c                 C   s   t | d S )z
    Return the leading degree of ``f``.

    Examples
    ========

    >>> from sympy.polys.galoistools import gf_degree

    >>> gf_degree([1, 1, 2, 0])
    3
    >>> gf_degree([])
    -1

       lenfr   r   r   	gf_degree   s    r,   c                 C   s   | s
|j S | d S dS )z
    Return the leading coefficient of ``f``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_LC

    >>> gf_LC([3, 0, 1], ZZ)
    3

    r   Nr   r+   r   r   r   r   gf_LC   s    r/   c                 C   s   | s
|j S | d S dS )z
    Return the trailing coefficient of ``f``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_TC

    >>> gf_TC([3, 0, 1], ZZ)
    1

    r   Nr-   r.   r   r   r   gf_TC   s    r0   c                 C   s<   | r| d r| S d}| D ]}|r& q0q|d7 }q| |d S )z
    Remove leading zeros from ``f``.


    Examples
    ========

    >>> from sympy.polys.galoistools import gf_strip

    >>> gf_strip([0, 0, 0, 3, 0, 1])
    [3, 0, 1]

    r   r'   Nr   )r+   kcoeffr   r   r   gf_strip   s    
r3   c                    s   t  fdd| D S )z
    Reduce all coefficients modulo ``p``.

    Examples
    ========

    >>> from sympy.polys.galoistools import gf_trunc

    >>> gf_trunc([7, -2, 3], 5)
    [2, 3, 3]

    c                    s   g | ]}|  qS r   r   .0r%   r   r   r   
<listcomp>       zgf_trunc.<locals>.<listcomp>)r3   r+   r   r   r6   r   gf_trunc   s    r:   c                 C   s   t tt|| |S )z
    Normalize all coefficients in ``K``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_normal

    >>> gf_normal([5, 10, 21, -3], 5, ZZ)
    [1, 2]

    )r:   listmapr+   r   r   r   r   r   	gf_normal   s    r>   c                 C   s   t |  g  }}t|trHt|ddD ]}|| ||j|  q(n2|\}t|ddD ]}|| |f|j|  qZt||S )a  
    Create a ``GF(p)[x]`` polynomial from a dict.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_from_dict

    >>> gf_from_dict({10: ZZ(4), 4: ZZ(33), 0: ZZ(-1)}, 5, ZZ)
    [4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4]

    r   )	maxkeys
isinstancer   ranger   getr   r:   )r+   r   r   nhr1   r   r   r   gf_from_dict   s    
rF   Tc                 C   sV   t | i  }}td|d D ]4}|r8t| ||  |}n| ||  }|r|||< q|S )aA  
    Convert a ``GF(p)[x]`` polynomial to a dict.

    Examples
    ========

    >>> from sympy.polys.galoistools import gf_to_dict

    >>> gf_to_dict([4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4], 5)
    {0: -1, 4: -2, 10: -1}
    >>> gf_to_dict([4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4], 5, symmetric=False)
    {0: 4, 4: 3, 10: 4}

    r   r'   )r,   rB   r&   )r+   r   	symmetricrD   resultr1   r%   r   r   r   
gf_to_dict  s    
rI   c                 C   s
   t | |S )z
    Create a ``GF(p)[x]`` polynomial from ``Z[x]``.

    Examples
    ========

    >>> from sympy.polys.galoistools import gf_from_int_poly

    >>> gf_from_int_poly([7, -2, 3], 5)
    [2, 3, 3]

    )r:   r9   r   r   r   gf_from_int_poly2  s    rJ   c                    s   |r fdd| D S | S dS )a  
    Convert a ``GF(p)[x]`` polynomial to ``Z[x]``.


    Examples
    ========

    >>> from sympy.polys.galoistools import gf_to_int_poly

    >>> gf_to_int_poly([2, 3, 3], 5)
    [2, -2, -2]
    >>> gf_to_int_poly([2, 3, 3], 5, symmetric=False)
    [2, 3, 3]

    c                    s   g | ]}t | qS r   )r&   )r5   cr6   r   r   r7   S  r8   z"gf_to_int_poly.<locals>.<listcomp>Nr   )r+   r   rG   r   r6   r   gf_to_int_polyB  s    rL   c                    s    fdd| D S )z
    Negate a polynomial in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_neg

    >>> gf_neg([3, 2, 1, 0], 5, ZZ)
    [2, 3, 4, 0]

    c                    s   g | ]}|   qS r   r   )r5   r2   r6   r   r   r7   f  r8   zgf_neg.<locals>.<listcomp>r   r=   r   r6   r   gf_negX  s    rM   c                 C   sN   | s|| }n.| d | | }t | dkr<| dd |g S |sDg S |gS dS )a  
    Compute ``f + a`` where ``f`` in ``GF(p)[x]`` and ``a`` in ``GF(p)``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_add_ground

    >>> gf_add_ground([3, 2, 4], 2, 5, ZZ)
    [3, 2, 1]

    r   r'   Nr(   r+   r%   r   r   r   r   r   gf_add_groundi  s    
rO   c                 C   sP   | s| | }n.| d | | }t | dkr>| dd |g S |sFg S |gS dS )a  
    Compute ``f - a`` where ``f`` in ``GF(p)[x]`` and ``a`` in ``GF(p)``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_sub_ground

    >>> gf_sub_ground([3, 2, 4], 2, 5, ZZ)
    [3, 2, 2]

    r   r'   Nr(   rN   r   r   r   gf_sub_ground  s    rP   c                    s     sg S  fdd| D S dS )a  
    Compute ``f * a`` where ``f`` in ``GF(p)[x]`` and ``a`` in ``GF(p)``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_mul_ground

    >>> gf_mul_ground([3, 2, 4], 2, 5, ZZ)
    [1, 4, 3]

    c                    s   g | ]} |  qS r   r   )r5   br$   r   r   r7     r8   z!gf_mul_ground.<locals>.<listcomp>Nr   rN   r   r$   r   gf_mul_ground  s    rR   c                 C   s   t | |||||S )a  
    Compute ``f/a`` where ``f`` in ``GF(p)[x]`` and ``a`` in ``GF(p)``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_quo_ground

    >>> gf_quo_ground(ZZ.map([3, 2, 4]), ZZ(2), 5, ZZ)
    [4, 1, 2]

    )rR   invertrN   r   r   r   gf_quo_ground  s    rT   c                    s   | s|S |s| S t | }t |}||krDt fddt| |D S t|| }||krt| d| | |d  }} n|d| ||d  }}| fddt| |D  S dS )z
    Add polynomials in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_add

    >>> gf_add([3, 2, 4], [2, 2, 2], 5, ZZ)
    [4, 1]

    c                    s   g | ]\}}||   qS r   r   r5   r%   rQ   r6   r   r   r7     r8   zgf_add.<locals>.<listcomp>Nc                    s   g | ]\}}||   qS r   r   rU   r6   r   r   r7     r8   )r,   r3   r   absr+   gr   r   dfdgr1   rE   r   r6   r   gf_add  s    r[   c                    s   |s| S | st | |S t| }t|}||krLt fddt| |D S t|| }||kr|| d| | |d  }} n"t |d|  |||d  }}| fddt| |D  S dS )z
    Subtract polynomials in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_sub

    >>> gf_sub([3, 2, 4], [2, 2, 2], 5, ZZ)
    [1, 0, 2]

    c                    s   g | ]\}}||   qS r   r   rU   r6   r   r   r7      r8   zgf_sub.<locals>.<listcomp>Nc                    s   g | ]\}}||   qS r   r   rU   r6   r   r   r7   	  r8   )rM   r,   r3   r   rV   rW   r   r6   r   gf_sub  s    "r\   c                 C   s   t | }t |}|| }dg|d  }td|d D ]R}|j}	ttd|| t||d D ]}
|	| |
 |||
   7 }	q\|	| ||< q4t|S )z
    Multiply polynomials in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_mul

    >>> gf_mul([3, 2, 4], [2, 2, 2], 5, ZZ)
    [1, 0, 3, 2, 3]

    r   r'   r,   rB   r   r?   minr3   )r+   rX   r   r   rY   rZ   dhrE   ir2   jr   r   r   gf_mul  s    "rb   c                 C   s   t | }d| }dg|d  }td|d D ]}|j}td|| }t||}	|	| d }
||
d  d }	t||	d D ]}|| | | ||   7 }qx||7 }|
d@ r| |	d  }||d 7 }|| ||< q,t|S )z
    Square polynomials in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_sqr

    >>> gf_sqr([3, 2, 4], 5, ZZ)
    [4, 2, 3, 1, 1]

    r#   r   r'   r]   )r+   r   r   rY   r_   rE   r`   r2   jminjmaxrD   ra   elemr   r   r   gf_sqr+  s"    
rf   c                 C   s   t | t||||||S )a  
    Returns ``f + g*h`` where ``f``, ``g``, ``h`` in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_add_mul
    >>> gf_add_mul([3, 2, 4], [2, 2, 2], [1, 4], 5, ZZ)
    [2, 3, 2, 2]
    )r[   rb   r+   rX   rE   r   r   r   r   r   
gf_add_mulV  s    rh   c                 C   s   t | t||||||S )a  
    Compute ``f - g*h`` where ``f``, ``g``, ``h`` in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_sub_mul

    >>> gf_sub_mul([3, 2, 4], [2, 2, 2], [1, 4], 5, ZZ)
    [3, 3, 2, 1]

    )r\   rb   rg   r   r   r   
gf_sub_mule  s    ri   c                 C   sN   t | tr| \}} n|j}|g}| D ]$\}}t||||}t||||}q$|S )a  
    Expand results of :func:`~.factor` in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_expand

    >>> gf_expand([([3, 2, 4], 1), ([2, 2], 2), ([3, 1], 3)], 5, ZZ)
    [4, 3, 0, 3, 0, 1, 4, 1]

    )rA   tupler   gf_powrb   )Fr   r   lcrX   r+   r1   r   r   r   	gf_expandv  s    

rn   c                 C   s   t | }t |}|stdn||k r.g | fS ||d |}t| || |d   }}}	td|d D ]p}
||
 }ttd||
 t||
 |	d D ]$}|||
| |  |||   8 }q|
|kr||9 }|| ||
< qh|d|d  t||d d fS )a	  
    Division with remainder in ``GF(p)[x]``.

    Given univariate polynomials ``f`` and ``g`` with coefficients in a
    finite field with ``p`` elements, returns polynomials ``q`` and ``r``
    (quotient and remainder) such that ``f = q*g + r``.

    Consider polynomials ``x**3 + x + 1`` and ``x**2 + x`` in GF(2)::

       >>> from sympy.polys.domains import ZZ
       >>> from sympy.polys.galoistools import gf_div, gf_add_mul

       >>> gf_div(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
       ([1, 1], [1])

    As result we obtained quotient ``x + 1`` and remainder ``1``, thus::

       >>> gf_add_mul(ZZ.map([1]), ZZ.map([1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
       [1, 0, 1, 1]

    References
    ==========

    .. [1] [Monagan93]_
    .. [2] [Gathen99]_

    polynomial divisionr   r'   N)r,   ZeroDivisionErrorrS   r;   rB   r?   r^   r3   r+   rX   r   r   rY   rZ   invrE   Zdqdrr`   r2   ra   r   r   r   gf_div  s     
&"rt   c                 C   s   t | |||d S )z
    Compute polynomial remainder in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_rem

    >>> gf_rem(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
    [1]

    r'   )rt   r+   rX   r   r   r   r   r   gf_rem  s    rv   c                 C   s   t | }t |}|stdn||k r*g S ||d |}| dd || |d   }}}	td|d D ]d}
||
 }ttd||
 t||
 |	d D ]$}|||
| |  |||   8 }q|| | ||
< qh|d|d  S )aG  
    Compute exact quotient in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_quo

    >>> gf_quo(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
    [1, 1]
    >>> gf_quo(ZZ.map([1, 0, 3, 2, 3]), ZZ.map([2, 2, 2]), 5, ZZ)
    [3, 2, 4]

    ro   r   Nr'   )r,   rp   rS   rB   r?   r^   rq   r   r   r   gf_quo  s    
 &"rw   c                 C   s(   t | |||\}}|s|S t| |dS )a  
    Compute polynomial quotient in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_exquo

    >>> gf_exquo(ZZ.map([1, 0, 3, 2, 3]), ZZ.map([2, 2, 2]), 5, ZZ)
    [3, 2, 4]

    >>> gf_exquo(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)
    Traceback (most recent call last):
    ...
    ExactQuotientFailed: [1, 1, 0] does not divide [1, 0, 1, 1]

    N)rt   r   )r+   rX   r   r   qrr   r   r   gf_exquo   s    rz   c                 C   s   | s| S | |j g|  S dS )z
    Efficiently multiply ``f`` by ``x**n``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_lshift

    >>> gf_lshift([3, 2, 4], 4, ZZ)
    [3, 2, 4, 0, 0, 0, 0]

    Nr-   r+   rD   r   r   r   r   	gf_lshift  s    r|   c                 C   s,   |s| g fS | d|  | | d fS dS )z
    Efficiently divide ``f`` by ``x**n``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_rshift

    >>> gf_rshift([1, 2, 3, 4, 0], 3, ZZ)
    ([1, 2], [3, 4, 0])

    Nr   r{   r   r   r   	gf_rshift/  s    r}   c                 C   sr   |s|j gS |dkr| S |dkr,t| ||S |j g}|d@ rRt|| ||}|d8 }|dL }|s`qnt| ||} q4|S )z
    Compute ``f**n`` in ``GF(p)[x]`` using repeated squaring.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_pow

    >>> gf_pow([3, 2, 4], 3, 5, ZZ)
    [2, 4, 4, 2, 2, 1, 4]

    r'   r#   )r   rf   rb   )r+   rD   r   r   rE   r   r   r   rk   C  s    rk   c                 C   s   t | }|dkrg S dg| }dg|d< ||k rhtd|D ]*}t||d  ||}t|| ||||< q:nh|dkrt|j|jg|| |||d< td|D ]8}t||d  |d ||||< t|| | ||||< q|S )ah  
    return the list of ``x**(i*p) mod g in Z_p`` for ``i = 0, .., n - 1``
    where ``n = gf_degree(g)``

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_frobenius_monomial_base
    >>> g = ZZ.map([1, 0, 2, 1])
    >>> gf_frobenius_monomial_base(g, 5, ZZ)
    [[1], [4, 4, 2], [1, 2]]

    r   r'   r#   )r,   rB   r|   rv   
gf_pow_modr   r   rb   )rX   r   r   rD   rQ   r`   monr   r   r   gf_frobenius_monomial_baseh  s    

r   c           
      C   s|   t |}t | |kr"t| |||} | s*g S t | }| d g}td|d D ],}t|| | ||  ||}	t||	||}qJ|S )aS  
    compute gf_pow_mod(f, p, g, p, K) using the Frobenius map

    Parameters
    ==========

    f, g : polynomials in ``GF(p)[x]``
    b : frobenius monomial base
    p : prime number
    K : domain

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_frobenius_monomial_base, gf_frobenius_map
    >>> f = ZZ.map([2, 1, 0, 1])
    >>> g = ZZ.map([1, 0, 2, 1])
    >>> p = 5
    >>> b = gf_frobenius_monomial_base(g, p, ZZ)
    >>> r = gf_frobenius_map(f, g, b, p, ZZ)
    >>> gf_frobenius_map(f, g, b, p, ZZ)
    [4, 0, 3]
    r   r'   )r,   rv   rB   rR   r[   )
r+   rX   rQ   r   r   r   rD   sfr`   r   r   r   r   gf_frobenius_map  s    
r   c           
      C   sn   t | |||} | }| }td|D ]0}t|||||}t||||}t ||||}q t||d d |||}	|	S )z
    utility function for ``gf_edf_zassenhaus``
    Compute ``f**((p**n - 1) // 2)`` in ``GF(p)[x]/(g)``
    ``f**((p**n - 1) // 2) = (f*f**p*...*f**(p**n - 1))**((p - 1) // 2)``
    r'   r#   )rv   rB   r   rb   r~   )
r+   rD   rX   rQ   r   r   rE   ry   r`   resr   r   r   _gf_pow_pnm1d2  s    r   c                 C   s   |s|j gS |dkr"t| |||S |dkr@tt| |||||S |j g}|d@ rtt|| ||}t||||}|d8 }|dL }|sqt| ||} t| |||} qH|S )a*  
    Compute ``f**n`` in ``GF(p)[x]/(g)`` using repeated squaring.

    Given polynomials ``f`` and ``g`` in ``GF(p)[x]`` and a non-negative
    integer ``n``, efficiently computes ``f**n (mod g)`` i.e. the remainder
    of ``f**n`` from division by ``g``, using the repeated squaring algorithm.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_pow_mod

    >>> gf_pow_mod(ZZ.map([3, 2, 4]), 3, ZZ.map([1, 1]), 5, ZZ)
    []

    References
    ==========

    .. [1] [Gathen99]_

    r'   r#   )r   rv   rf   rb   )r+   rD   rX   r   r   rE   r   r   r   r~     s"    r~   c                 C   s*   |r|t | ||| } }q t| ||d S )z
    Euclidean Algorithm in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_gcd

    >>> gf_gcd(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)
    [1, 3]

    r'   )rv   gf_monicru   r   r   r   gf_gcd  s    r   c                 C   s>   | r|sg S t t| |||t| |||||}t|||d S )z
    Compute polynomial LCM in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_lcm

    >>> gf_lcm(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)
    [1, 2, 0, 4]

    r'   )rw   rb   r   r   r+   rX   r   r   rE   r   r   r   gf_lcm  s    r   c                 C   s>   | s|sg g g fS t | |||}|t| |||t||||fS )a   
    Compute polynomial GCD and cofactors in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_cofactors

    >>> gf_cofactors(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)
    ([1, 3], [3, 3], [2, 1])

    )r   rw   r   r   r   r   gf_cofactors  s    
r   c                 C   s  | s|s|j gg g fS t| ||\}}t|||\}}| sNg |||g|fS |sf|||gg |fS |||gg  }}	g |||g }
}t||||\}}|sqt|||| \}}}|||}t||	|||}t|
||||}t|||||	 }	}t||||| }}
q|	||fS )a  
    Extended Euclidean Algorithm in ``GF(p)[x]``.

    Given polynomials ``f`` and ``g`` in ``GF(p)[x]``, computes polynomials
    ``s``, ``t`` and ``h``, such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.
    The typical application of EEA is solving polynomial diophantine equations.

    Consider polynomials ``f = (x + 7) (x + 1)``, ``g = (x + 7) (x**2 + 1)``
    in ``GF(11)[x]``. Application of Extended Euclidean Algorithm gives::

       >>> from sympy.polys.domains import ZZ
       >>> from sympy.polys.galoistools import gf_gcdex, gf_mul, gf_add

       >>> s, t, g = gf_gcdex(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ)
       >>> s, t, g
       ([5, 6], [6], [1, 7])

    As result we obtained polynomials ``s = 5*x + 6`` and ``t = 6``, and
    additionally ``gcd(f, g) = x + 7``. This is correct because::

       >>> S = gf_mul(s, ZZ.map([1, 8, 7]), 11, ZZ)
       >>> T = gf_mul(t, ZZ.map([1, 7, 1, 7]), 11, ZZ)

       >>> gf_add(S, T, 11, ZZ) == [1, 7]
       True

    References
    ==========

    .. [1] [Gathen99]_

    )r   r   rS   rt   ri   rR   )r+   rX   r   r   p0r0p1r1s0s1t0t1QRrm   rr   r   tr   r   r   gf_gcdex1  s(    !r   c                 C   sB   | s|j g fS | d }||r,|t| fS |t| |||fS dS )z
    Compute LC and a monic polynomial in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_monic

    >>> gf_monic(ZZ.map([3, 2, 4]), 5, ZZ)
    (3, [1, 4, 3])

    r   N)r   is_oner;   rT   )r+   r   r   rm   r   r   r   r   s  s    

r   c                 C   s`   t | }|jg| | }}| dd D ]0}|||9 }||; }|rN|||| < |d8 }q&t|S )z
    Differentiate polynomial in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_diff

    >>> gf_diff([3, 2, 4], 5, ZZ)
    [1, 2]

    Nr   r'   )r,   r   r3   )r+   r   r   rY   rE   rD   r2   r   r   r   gf_diff  s    
r   c                 C   s,   |j }| D ]}||9 }||7 }||; }q
|S )z
    Evaluate ``f(a)`` in ``GF(p)`` using Horner scheme.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_eval

    >>> gf_eval([3, 2, 4], 2, 5, ZZ)
    0

    r-   )r+   r%   r   r   rH   rK   r   r   r   gf_eval  s    
r   c                    s    fdd|D S )a  
    Evaluate ``f(a)`` for ``a`` in ``[a_1, ..., a_n]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_multi_eval

    >>> gf_multi_eval([3, 2, 4], [0, 1, 2, 3, 4], 5, ZZ)
    [4, 4, 0, 2, 0]

    c                    s   g | ]}t | qS r   r   r4   r   r+   r   r   r   r7     r8   z!gf_multi_eval.<locals>.<listcomp>r   )r+   Ar   r   r   r   r   gf_multi_eval  s    r   c                 C   sj   t |dkr&tt| t||||gS | s.g S | d g}| dd D ] }t||||}t||||}qD|S )a  
    Compute polynomial composition ``f(g)`` in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_compose

    >>> gf_compose([3, 2, 4], [2, 2, 2], 5, ZZ)
    [2, 4, 0, 3, 0]

    r'   r   N)r)   r3   r   r/   rb   rO   )r+   rX   r   r   rE   rK   r   r   r   
gf_compose  s    
r   c                 C   sR   | sg S | d g}| dd D ].}t ||||}t||||}t||||}q|S )a&  
    Compute polynomial composition ``g(h)`` in ``GF(p)[x]/(f)``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_compose_mod

    >>> gf_compose_mod(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 2]), ZZ.map([4, 3]), 5, ZZ)
    [4]

    r   r'   N)rb   rO   rv   )rX   rE   r+   r   r   compr%   r   r   r   gf_compose_mod  s    
r   c                 C   s   t | ||||}|}|d@ r0t| |||}	|}
n| }	|}
|dL }|rt|t |||||||}t |||||}|d@ rt|	t ||
|||||}	t ||
|||}
|dL }q@t | |
||||	fS )a  
    Compute polynomial trace map in ``GF(p)[x]/(f)``.

    Given a polynomial ``f`` in ``GF(p)[x]``, polynomials ``a``, ``b``,
    ``c`` in the quotient ring ``GF(p)[x]/(f)`` such that ``b = c**t
    (mod f)`` for some positive power ``t`` of ``p``, and a positive
    integer ``n``, returns a mapping::

       a -> a**t**n, a + a**t + a**t**2 + ... + a**t**n (mod f)

    In factorization context, ``b = x**p mod f`` and ``c = x mod f``.
    This way we can efficiently compute trace polynomials in equal
    degree factorization routine, much faster than with other methods,
    like iterated Frobenius algorithm, for large degrees.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_trace_map

    >>> gf_trace_map([1, 2], [4, 4], [1, 1], 4, [3, 2, 4], 5, ZZ)
    ([1, 3], [1, 3])

    References
    ==========

    .. [1] [Gathen92]_

    r'   )r   r[   )r%   rQ   rK   rD   r+   r   r   r   r   r   Vr   r   r   gf_trace_map  s     
r   c           	      C   sV   t | |||} | }| }td|D ]0}t|||||}t||||}t ||||}q |S )z&
    utility for ``gf_edf_shoup``
    r'   )rv   rB   r   r[   )	r+   rD   rX   rQ   r   r   rE   ry   r`   r   r   r   _gf_trace_mapB  s    r   c                    s"    j g fddtd| D  S )a  
    Generate a random polynomial in ``GF(p)[x]`` of degree ``n``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_random
    >>> gf_random(10, 5, ZZ) #doctest: +SKIP
    [1, 2, 3, 2, 1, 1, 1, 2, 0, 4, 2]

    c                    s   g | ]} t td qS r   )intr   r5   r`   r   r   r   r   r7   ]  r8   zgf_random.<locals>.<listcomp>r   )r   rB   )rD   r   r   r   r   r   	gf_randomP  s    r   c                 C   s"   t | ||}t|||r |S q dS )a,  
    Generate random irreducible polynomial of degree ``n`` in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_irreducible
    >>> gf_irreducible(10, 5, ZZ) #doctest: +SKIP
    [1, 4, 2, 2, 3, 2, 4, 1, 4, 0, 4]

    N)r   gf_irreducible_p)rD   r   r   r+   r   r   r   gf_irreducible`  s    r   c           
      C   s(  t | }|dkrdS t| ||\}} |dk rt|j|jg|| || }}td|d D ]H}t||j|jg||}t| ||||jgkrt||| ||}qV dS qVnt	| ||}	t
|j|jg| |	|| }}td|d D ]J}t||j|jg||}t| ||||jgkrt
|| |	||}q dS qdS )a_  
    Ben-Or's polynomial irreducibility test over finite fields.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_irred_p_ben_or

    >>> gf_irred_p_ben_or(ZZ.map([1, 4, 2, 2, 3, 2, 4, 1, 4, 0, 4]), 5, ZZ)
    True
    >>> gf_irred_p_ben_or(ZZ.map([3, 2, 4]), 5, ZZ)
    False

    r'   T   r   r#   F)r,   r   r~   r   r   rB   r\   r   r   r   r   )
r+   r   r   rD   r   HrE   r`   rX   rQ   r   r   r   gf_irred_p_ben_ors  s&    
r   c                    s   t |   dkrdS t| ||\}} |j|jg}ddlm}  fdd| D }t| ||}|d }td D ]F}	|	|v rt||||}
t	| |
|||jgkr dS t
|| |||}qp||kS )a[  
    Rabin's polynomial irreducibility test over finite fields.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_irred_p_rabin

    >>> gf_irred_p_rabin(ZZ.map([1, 4, 2, 2, 3, 2, 4, 1, 4, 0, 4]), 5, ZZ)
    True
    >>> gf_irred_p_rabin(ZZ.map([3, 2, 4]), 5, ZZ)
    False

    r'   Tr   	factorintc                    s   h | ]} | qS r   r   )r5   drD   r   r   	<setcomp>  r8   z#gf_irred_p_rabin.<locals>.<setcomp>F)r,   r   r   r   sympy.ntheoryr   r   rB   r\   r   r   )r+   r   r   r   xr   indicesrQ   rE   r`   rX   r   r   r   gf_irred_p_rabin  s     r   )zben-orZrabinc                 C   s2   t d}|dur"t| | ||}nt| ||}|S )a[  
    Test irreducibility of a polynomial ``f`` in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_irreducible_p

    >>> gf_irreducible_p(ZZ.map([1, 4, 2, 2, 3, 2, 4, 1, 4, 0, 4]), 5, ZZ)
    True
    >>> gf_irreducible_p(ZZ.map([3, 2, 4]), 5, ZZ)
    False

    ZGF_IRRED_METHODN)r   _irred_methodsr   )r+   r   r   methodZirredr   r   r   r     s
    r   c                 C   s:   t | ||\}} | sdS t| t| |||||jgkS dS )a5  
    Return ``True`` if ``f`` is square-free in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_sqf_p

    >>> gf_sqf_p(ZZ.map([3, 2, 4]), 5, ZZ)
    True
    >>> gf_sqf_p(ZZ.map([2, 4, 4, 2, 2, 1, 4]), 5, ZZ)
    False

    TN)r   r   r   r   )r+   r   r   r   r   r   r   gf_sqf_p  s    r   c                 C   s8   t | ||\}}|jg}|D ]\} }t|| ||}q|S )a  
    Return square-free part of a ``GF(p)[x]`` polynomial.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_sqf_part

    >>> gf_sqf_part(ZZ.map([1, 1, 3, 0, 1, 0, 2, 2, 1]), 5, ZZ)
    [1, 4, 3]

    )gf_sqf_listr   rb   )r+   r   r   r   sqfrX   r   r   r   gf_sqf_part  s
    r   Fc                 C   s\  ddg t |f\}}}}t| ||\}} t| dk r<|g fS t| ||}	|	g krt| |	||}
t| |
||}d}||jgkrt|
|||}t||||}t|dkr|||| f t|
|||||d   }
}}qp|
|jgkrd}n|
} |sFt| | }td|d D ]}| ||  | |< q| d|d  ||  } }q<qFq<|rTt	d||fS )a  
    Return the square-free decomposition of a ``GF(p)[x]`` polynomial.

    Given a polynomial ``f`` in ``GF(p)[x]``, returns the leading coefficient
    of ``f`` and a square-free decomposition ``f_1**e_1 f_2**e_2 ... f_k**e_k``
    such that all ``f_i`` are monic polynomials and ``(f_i, f_j)`` for ``i != j``
    are co-prime and ``e_1 ... e_k`` are given in increasing order. All trivial
    terms (i.e. ``f_i = 1``) are not included in the output.

    Consider polynomial ``f = x**11 + 1`` over ``GF(11)[x]``::

       >>> from sympy.polys.domains import ZZ

       >>> from sympy.polys.galoistools import (
       ...     gf_from_dict, gf_diff, gf_sqf_list, gf_pow,
       ... )
       ... # doctest: +NORMALIZE_WHITESPACE

       >>> f = gf_from_dict({11: ZZ(1), 0: ZZ(1)}, 11, ZZ)

    Note that ``f'(x) = 0``::

       >>> gf_diff(f, 11, ZZ)
       []

    This phenomenon does not happen in characteristic zero. However we can
    still compute square-free decomposition of ``f`` using ``gf_sqf()``::

       >>> gf_sqf_list(f, 11, ZZ)
       (1, [([1, 1], 11)])

    We obtained factorization ``f = (x + 1)**11``. This is correct because::

       >>> gf_pow([1, 1], 11, 11, ZZ) == f
       True

    References
    ==========

    .. [1] [Geddes92]_

    r'   Fr   TNz'all=True' is not supported yet)
r   r   r,   r   r   rw   r   r   rB   
ValueError)r+   r   r   allrD   r   factorsry   rm   rl   rX   rE   r`   Gr   r   r   r   r   r     s6    + r   c              	   C   s   t | t| }}|jg|jg|d   }t|gg g|d   }td|d | d D ]z}|d  | d  | g|d  }}	td|D ],}
|||
d  |	| |
 d    |  q|| st|||| < |}qX|S )ad  
    Calculate Berlekamp's ``Q`` matrix.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_Qmatrix

    >>> gf_Qmatrix([3, 2, 4], 5, ZZ)
    [[1, 0],
     [3, 4]]

    >>> gf_Qmatrix([1, 0, 0, 0, 1], 5, ZZ)
    [[1, 0, 0, 0],
     [0, 4, 0, 0],
     [0, 0, 1, 0],
     [0, 0, 0, 4]]

    r'   r   )r,   r   r   r   r;   rB   r   )r+   r   r   rD   ry   rx   r   r`   qqrK   ra   r   r   r   
gf_Qmatrixt  s    "*r   c                 C   s  dd | D t |  } }td|D ]"}| | | |j | | | |< q"td|D ]}t||D ]}| | | r` qxq`qP|| | | |}td|D ] }| | | | | | | |< qtd|D ]0}| | | }| | | | | |< || | |< qtd|D ]V}||k r| | | }	td|D ].}| | | | | | |	  | | | |< q" qqPtd|D ]\}td|D ]J}||kr|j| | |  | | | |< n| | |  | | | |< qpqbg }
| D ]}	t|	r|
|	 q|
S )a_  
    Compute a basis of the kernel of ``Q``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_Qmatrix, gf_Qbasis

    >>> gf_Qbasis(gf_Qmatrix([1, 0, 0, 0, 1], 5, ZZ), 5, ZZ)
    [[1, 0, 0, 0], [0, 0, 1, 0]]

    >>> gf_Qbasis(gf_Qmatrix([3, 2, 4], 5, ZZ), 5, ZZ)
    [[1, 0]]

    c                 S   s   g | ]}t |qS r   )r;   )r5   rx   r   r   r   r7     r8   zgf_Qbasis.<locals>.<listcomp>r   )r)   rB   r   rS   anyr   )r   r   r   rD   r1   r`   rr   ra   r   rx   basisr   r   r   	gf_Qbasis  s<     
2
 "
r   c                 C   s
  t | ||}t|||}t|D ]\}}ttt|||< q | g}tdt|D ]}t|D ]} |j}	|	|k r^t	|| |	||}
t
| |
||}||jgkr|| kr||  t| |||} || |g t|t|krt|dd    S |	|j7 }	qhq^qRt|ddS )a  
    Factor a square-free ``f`` in ``GF(p)[x]`` for small ``p``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_berlekamp

    >>> gf_berlekamp([1, 0, 0, 0, 1], 5, ZZ)
    [[1, 0, 2], [1, 0, 3]]

    r'   Fmultiple)r   r   	enumerater3   r;   reversedrB   r)   r   rP   r   r   removerw   extendr	   )r+   r   r   r   r   r`   r   r   r1   r   rX   rE   r   r   r   gf_berlekamp  s&    
r   c                 C   s   d|j |jgg   }}}t| ||}d| t| krt|| |||}t| t||j |jg||||}||j gkr|||f t| |||} t	|| ||}t| ||}|d7 }q$| |j gkr|| t| fg S |S dS )a{  
    Cantor-Zassenhaus: Deterministic Distinct Degree Factorization

    Given a monic square-free polynomial ``f`` in ``GF(p)[x]``, computes
    partial distinct degree factorization ``f_1 ... f_d`` of ``f`` where
    ``deg(f_i) != deg(f_j)`` for ``i != j``. The result is returned as a
    list of pairs ``(f_i, e_i)`` where ``deg(f_i) > 0`` and ``e_i > 0``
    is an argument to the equal degree factorization routine.

    Consider the polynomial ``x**15 - 1`` in ``GF(11)[x]``::

       >>> from sympy.polys.domains import ZZ
       >>> from sympy.polys.galoistools import gf_from_dict

       >>> f = gf_from_dict({15: ZZ(1), 0: ZZ(-1)}, 11, ZZ)

    Distinct degree factorization gives::

       >>> from sympy.polys.galoistools import gf_ddf_zassenhaus

       >>> gf_ddf_zassenhaus(f, 11, ZZ)
       [([1, 0, 0, 0, 0, 10], 1), ([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)]

    which means ``x**15 - 1 = (x**5 - 1) (x**10 + x**5 + 1)``. To obtain
    factorization into irreducibles, use equal degree factorization
    procedure (EDF) with each of the factors.

    References
    ==========

    .. [1] [Gathen99]_
    .. [2] [Geddes92]_

    r'   r#   N)
r   r   r   r,   r   r   r\   r   rw   rv   )r+   r   r   r`   rX   r   rQ   rE   r   r   r   gf_ddf_zassenhaus  s    # 
r   c                 C   s8  | g}t | |kr|S t | | }|dkr6t| ||}|j|jg}t||k r,|dkr| }}	t|d D ]"}
t|	d| ||}	t||	||}qlt| |||}||j|jg7 }n@t	d| d ||}	t
|	|| |||}t| t||j||||}||jgkrB|| krBt||||tt| |||||| }qBt|ddS )a  
    Cantor-Zassenhaus: Probabilistic Equal Degree Factorization

    Given a monic square-free polynomial ``f`` in ``GF(p)[x]`` and
    an integer ``n``, such that ``n`` divides ``deg(f)``, returns all
    irreducible factors ``f_1,...,f_d`` of ``f``, each of degree ``n``.
    EDF procedure gives complete factorization over Galois fields.

    Consider the square-free polynomial ``f = x**3 + x**2 + x + 1`` in
    ``GF(5)[x]``. Let's compute its irreducible factors of degree one::

       >>> from sympy.polys.domains import ZZ
       >>> from sympy.polys.galoistools import gf_edf_zassenhaus

       >>> gf_edf_zassenhaus([1,1,1,1], 1, 5, ZZ)
       [[1, 1], [1, 2], [1, 3]]

    Notes
    =====

    The case p == 2 is handled by Cohen's Algorithm 3.4.8. The case p odd is
    as in Geddes Algorithm 8.9 (or Cohen's Algorithm 3.4.6).

    References
    ==========

    .. [1] [Gathen99]_
    .. [2] [Geddes92]_ Algorithm 8.9
    .. [3] [Cohen93]_ Algorithm 3.4.8

    r#   r'   Fr   )r,   r   r   r   r)   rB   r~   r[   r   r   r   rP   gf_edf_zassenhausrw   r	   )r+   rD   r   r   r   NrQ   r   rE   ry   r`   rX   r   r   r   r   ?  s.     r   c                 C   s  t | }ttt|d }t| ||}t|j|jg| |||}|j|jg|g|jg|d   }td|d D ] }t||d  | |||||< qn|| |d|  }}|g|jg|d   }	td|D ] }t	|	|d  || |||	|< qg }
t
|	D ]\}}|jg|d  }}|D ]0}t||||}t||||}t|| ||}qt| |||}t| |||} t|D ]b}t||||}t||||}||jgkr|
|||d  | f t|||||d  }}qhq| |jgkr|
| t | f |
S )a  
    Kaltofen-Shoup: Deterministic Distinct Degree Factorization

    Given a monic square-free polynomial ``f`` in ``GF(p)[x]``, computes
    partial distinct degree factorization ``f_1,...,f_d`` of ``f`` where
    ``deg(f_i) != deg(f_j)`` for ``i != j``. The result is returned as a
    list of pairs ``(f_i, e_i)`` where ``deg(f_i) > 0`` and ``e_i > 0``
    is an argument to the equal degree factorization routine.

    This algorithm is an improved version of Zassenhaus algorithm for
    large ``deg(f)`` and modulus ``p`` (especially for ``deg(f) ~ lg(p)``).

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_ddf_shoup, gf_from_dict

    >>> f = gf_from_dict({6: ZZ(1), 5: ZZ(-1), 4: ZZ(1), 3: ZZ(1), 1: ZZ(-1)}, 3, ZZ)

    >>> gf_ddf_shoup(f, 3, ZZ)
    [([1, 1, 0], 1), ([1, 1, 0, 1, 2], 2)]

    References
    ==========

    .. [1] [Kaltofen98]_
    .. [2] [Shoup95]_
    .. [3] [Gathen92]_

    r#   r'   N)r,   r   _ceil_sqrtr   r   r   r   rB   r   r   r\   rb   rv   r   rw   r   r   )r+   r   r   rD   r1   rQ   rE   r   r`   r   r   r   ra   r   rX   rl   r   r   r   gf_ddf_shoup  s:      r   c                 C   sf  t | t| }}|sg S ||kr(| gS | g|j|jg }}t|d ||}|dkrt||| ||}	t||	||d | ||d }
t| |
||}t| |||}t	||||t	|||| }nt
| ||}t||| |||}
t|
|d d | ||}	t| |	||}t| t|	|j||||}t| t||||||}t	||||t	|||| t	|||| }t|ddS )a  
    Gathen-Shoup: Probabilistic Equal Degree Factorization

    Given a monic square-free polynomial ``f`` in ``GF(p)[x]`` and integer
    ``n`` such that ``n`` divides ``deg(f)``, returns all irreducible factors
    ``f_1,...,f_d`` of ``f``, each of degree ``n``. This is a complete
    factorization over Galois fields.

    This algorithm is an improved version of Zassenhaus algorithm for
    large ``deg(f)`` and modulus ``p`` (especially for ``deg(f) ~ lg(p)``).

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_edf_shoup

    >>> gf_edf_shoup(ZZ.map([1, 2837, 2277]), 1, 2917, ZZ)
    [[1, 852], [1, 1985]]

    References
    ==========

    .. [1] [Shoup91]_
    .. [2] [Gathen92]_

    r'   r#   Fr   )r,   r   r   r   r   r~   r   r   rw   gf_edf_shoupr   r   rP   rb   r	   )r+   rD   r   r   r   rx   r   r   ry   rE   r   h1h2rQ   h3r   r   r   r     s6    r   c                 C   s8   g }t | ||D ]\}}|t||||7 }qt|ddS )a  
    Factor a square-free ``f`` in ``GF(p)[x]`` for medium ``p``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_zassenhaus

    >>> gf_zassenhaus(ZZ.map([1, 4, 3]), 5, ZZ)
    [[1, 1], [1, 3]]

    Fr   )r   r   r	   r+   r   r   r   factorrD   r   r   r   gf_zassenhaus
  s    r   c                 C   s8   g }t | ||D ]\}}|t||||7 }qt|ddS )a  
    Factor a square-free ``f`` in ``GF(p)[x]`` for large ``p``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_shoup

    >>> gf_shoup(ZZ.map([1, 4, 3]), 5, ZZ)
    [[1, 1], [1, 3]]

    Fr   )r   r   r	   r   r   r   r   gf_shoup   s    r   )Z	berlekampZ
zassenhausZshoupc                 C   s^   t | ||\}} t| dk r$|g fS |p.td}|durJt| | ||}nt| ||}||fS )a  
    Factor a square-free polynomial ``f`` in ``GF(p)[x]``.

    Examples
    ========

    >>> from sympy.polys.domains import ZZ
    >>> from sympy.polys.galoistools import gf_factor_sqf

    >>> gf_factor_sqf(ZZ.map([3, 2, 4]), 5, ZZ)
    (3, [[1, 1], [1, 3]])

    r'   ZGF_FACTOR_METHODN)r   r,   r   _factor_methodsr   )r+   r   r   r   rm   r   r   r   r   gf_factor_sqf<  s    r   c                 C   sr   t | ||\}} t| dk r$|g fS g }t| ||d D ],\}}t|||d D ]}|||f qPq8|t|fS )a  
    Factor (non square-free) polynomials in ``GF(p)[x]``.

    Given a possibly non square-free polynomial ``f`` in ``GF(p)[x]``,
    returns its complete factorization into irreducibles::

                 f_1(x)**e_1 f_2(x)**e_2 ... f_d(x)**e_d

    where each ``f_i`` is a monic polynomial and ``gcd(f_i, f_j) == 1``,
    for ``i != j``.  The result is given as a tuple consisting of the
    leading coefficient of ``f`` and a list of factors of ``f`` with
    their multiplicities.

    The algorithm proceeds by first computing square-free decomposition
    of ``f`` and then iteratively factoring each of square-free factors.

    Consider a non square-free polynomial ``f = (7*x + 1) (x + 2)**2`` in
    ``GF(11)[x]``. We obtain its factorization into irreducibles as follows::

       >>> from sympy.polys.domains import ZZ
       >>> from sympy.polys.galoistools import gf_factor

       >>> gf_factor(ZZ.map([5, 2, 7, 2]), 11, ZZ)
       (5, [([1, 2], 1), ([1, 8], 2)])

    We arrived with factorization ``f = 5 (x + 2) (x + 8)**2``. We did not
    recover the exact form of the input polynomial because we requested to
    get monic factors of ``f`` and its leading coefficient separately.

    Square-free factors of ``f`` can be factored into irreducibles over
    ``GF(p)`` using three very different methods:

    Berlekamp
        efficient for very small values of ``p`` (usually ``p < 25``)
    Cantor-Zassenhaus
        efficient on average input and with "typical" ``p``
    Shoup-Kaltofen-Gathen
        efficient with very large inputs and modulus

    If you want to use a specific factorization method, instead of the default
    one, set ``GF_FACTOR_METHOD`` with one of ``berlekamp``, ``zassenhaus`` or
    ``shoup`` values.

    References
    ==========

    .. [1] [Gathen99]_

    r'   )r   r,   r   r   r   r	   )r+   r   r   rm   r   rX   rD   rE   r   r   r   	gf_factorY  s    2r   c                 C   s"   d}| D ]}||9 }||7 }q|S )z
    Value of polynomial 'f' at 'a' in field R.

    Examples
    ========

    >>> from sympy.polys.galoistools import gf_value

    >>> gf_value([1, 7, 2, 4], 11)
    2204

    r   r   )r+   r%   rH   rK   r   r   r   gf_value  s
    
r   c                    sp   ddl m} |  dkr4  dkr0ttS g S || \}  dkrTg S  fddtD S )a  
    Returns the values of x satisfying a*x congruent b mod(m)

    Here m is positive integer and a, b are natural numbers.
    This function returns only those values of x which are distinct mod(m).

    Examples
    ========

    >>> from sympy.polys.galoistools import linear_congruence

    >>> linear_congruence(3, 12, 15)
    [4, 9, 14]

    There are 3 solutions distinct mod(15) since gcd(a, m) = gcd(3, 15) = 3.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Linear_congruence_theorem

    r   )r   c                    s(   g | ] }   |    qS r   r   )r5   r   rQ   rX   r   ry   r   r   r7     r8   z%linear_congruence.<locals>.<listcomp>)sympy.polys.polytoolsr   r;   rB   )r%   rQ   r   r   r   r   r   r   linear_congruence  s    r   c                 C   sB   ddl m} t|||}t|| }t||  ||  }t|||S )a0  
    Used in gf_csolve to generate solutions of f(x) cong 0 mod(p**(s + 1))
    from the solutions of f(x) cong 0 mod(p**s).

    Examples
    ========

    >>> from sympy.polys.galoistools import _raise_mod_power
    >>> from sympy.polys.galoistools import csolve_prime

    These is the solutions of f(x) = x**2 + x + 7 cong 0 mod(3)

    >>> f = [1, 1, 7]
    >>> csolve_prime(f, 3)
    [1]
    >>> [ i for i in range(3) if not (i**2 + i + 7) % 3]
    [1]

    The solutions of f(x) cong 0 mod(9) are constructed from the
    values returned from _raise_mod_power:

    >>> x, s, p = 1, 1, 3
    >>> V = _raise_mod_power(x, s, p, f)
    >>> [x + v * p**s for v in V]
    [1, 4, 7]

    And these are confirmed with the following:

    >>> [ i for i in range(3**2) if not (i**2 + i + 7) % 3**2]
    [1, 4, 7]

    r   ZZ)sympy.polys.domainsr   r   r   r   )r   r   r   r+   r   Zf_falphabetar   r   r   _raise_mod_power  s
    !
r   r'   c                    s   ddl m   fddtD }|dkr2|S g }tt|dgt| }|r| \}||krr| qN|d | |fddt	|D  qNt
|S )aU  
    Solutions of f(x) congruent 0 mod(p**e).

    Examples
    ========

    >>> from sympy.polys.galoistools import csolve_prime

    >>> csolve_prime([1, 1, 7], 3, 1)
    [1]
    >>> csolve_prime([1, 1, 7], 3, 2)
    [1, 4, 7]

    Solutions [7, 4, 1] (mod 3**2) are generated by ``_raise_mod_power()``
    from solution [1] (mod 3).
    r   r   c                    s"   g | ]}t | d kr|qS r   r   r   )r   r+   r   r   r   r7   
	  r8   z csolve_prime.<locals>.<listcomp>r'   c                    s   g | ]}|   fqS r   r   )r5   r   )psr   r   r   r   r7   	  r8   )r   r   rB   r;   r   r)   popr   r   r   sorted)r+   r   r   ZX1Xr    r   r   )r   r+   r   r   r   r   r   csolve_prime  s    (r   c                    s   ddl m  ddlm} ||}fdd| D }ttt|}g g}|D ]fdd|D }qNdd | D t fdd|D S )	a=  
    To solve f(x) congruent 0 mod(n).

    n is divided into canonical factors and f(x) cong 0 mod(p**e) will be
    solved for each factor. Applying the Chinese Remainder Theorem to the
    results returns the final answers.

    Examples
    ========

    Solve [1, 1, 7] congruent 0 mod(189):

    >>> from sympy.polys.galoistools import gf_csolve
    >>> gf_csolve([1, 1, 7], 189)
    [13, 49, 76, 112, 139, 175]

    References
    ==========

    .. [1] 'An introduction to the Theory of Numbers' 5th Edition by Ivan Niven,
           Zuckerman and Montgomery.

    r   r   r   c                    s   g | ]\}}t  ||qS r   )r   r5   r   r   r*   r   r   r7   5	  r8   zgf_csolve.<locals>.<listcomp>c                    s    g | ]} D ]}||g qqS r   r   )r5   r   y)poolr   r   r7   9	  r8   c                 S   s   g | ]\}}t ||qS r   )powr   r   r   r   r7   :	  r8   c                    s   g | ]}t | qS r   )r   )r5   per)r   dist_factorsr   r   r7   ;	  r8   )	r   r   r   r   itemsr;   r<   rj   r   )r+   rD   r   Pr   poolspermsr   )r   r   r+   r   r   	gf_csolve	  s    r   )N)T)T)F)N)r'   )\__doc__mathr   r   r   r   r   sympy.core.randomr   sympy.external.gmpyr   sympy.polys.polyconfigr   sympy.polys.polyerrorsr   sympy.polys.polyutilsr	   r   r!   r"   r&   r,   r/   r0   r3   r:   r>   rF   rI   rJ   rL   rM   rO   rP   rR   rT   r[   r\   rb   rf   rh   ri   rn   rt   rv   rw   rz   r|   r}   rk   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   r   r   r   r   r   r   r   r   r   r   r   <module>   s   
-

##+6'% %1B7-,
Y(>,9@L?
@#(
"