a
    <b                     @   s   d 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
 ddlmZmZmZ dd	lmZ d
d ZdddZdd Ze
dddZdS )z,Computing integral bases for number fields.     )Poly)ZZ)QQ)CoercionFailed)public   )ModuleEndomorphismModuleHomomorphism
PowerBasis) extract_fundamental_discriminantc                 C   s   | j }t| |d}| \}}|dks*J td||d}|D ]\}}||9 }q<|| }	t|td}
t|	td}|
| |  | }t||d}|}||	fD ]}||}q|| }| }||fS )zz
    Apply the "Dedekind criterion" to test whether the order needs to be
    enlarged relative to a given prime *p*.
    modulusr   domain)genr   Zfactor_listr   gcddegree)TpxZT_barlcflZg_barZti_bar_Zh_barghfZf_barZZ_barbU_barm r   n/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/polys/numberfields/basis.py_apply_Dedekind_criterion   s$    
r!   Nc                    sB   | j } du r$|  |k r$ |9  qt|  fdd}|j|dS )a  
    Compute the nilradical mod *p* for a given order *H*, and prime *p*.

    Explanation
    ===========

    This is the ideal $I$ in $H/pH$ consisting of all elements some positive
    power of which is zero in this quotient ring, i.e. is a multiple of *p*.

    Parameters
    ==========

    H : :py:class:`~.Submodule`
        The given order.
    p : int
        The rational prime.
    q : int, optional
        If known, the smallest power of *p* that is $>=$ the dimension of *H*.
        If not provided, we compute it here.

    Returns
    =======

    :py:class:`~.Module` representing the nilradical mod *p* in *H*.

    References
    ==========

    .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory*.
    (See Lemma 6.1.6.)

    Nc                    s   |   S Nr   r   qr   r    <lambda>K       z"nilradical_mod_p.<locals>.<lambda>r   )nr   kernel)Hr   r%   r(   phir   r$   r    nilradical_mod_p%   s    !
r,   c           
         s   t | ||d}| jj| j|j | jd}|||   }|  t|   fdd}|j|d}| jj| j|j | j| d}||  }	|	|fS )zD
    Perform the second enlargement in the Round Two algorithm.
    r$   )denomc                    s
     | S r"   )Zinner_endomorphismr#   Er   r    r&   W   r'   z%_second_enlargement.<locals>.<lambda>r   )r,   parentZsubmodule_from_matrixmatrixr-   Zendomorphism_ringr	   r)   )
r*   r   r%   ZIpBCr+   gammaGH1r   r.   r    _second_enlargementO   s    r7   c                 C   s  | j tkr.zt| td} W n ty,   Y n0 | jrJ| jrJ| jrJ| j tksRtd| 	 }| 
 }tt|}t|\}}t| }| }d}	|r6| \}
}t| |
\}}|dkrq|t|td}|j||
 | |d}||krq|
}||k r||
9 }qt||
|\}}	||kr|}t||
|\}}	qq|	durTt|trT|	||
< |}d|_d|_||j d  |jd|   }||fS )a
  
    Zassenhaus's "Round 2" algorithm.

    Explanation
    ===========

    Carry out Zassenhaus's "Round 2" algorithm on a monic irreducible
    polynomial *T* over :ref:`ZZ`. This computes an integral basis and the
    discriminant for the field $K = \mathbb{Q}[x]/(T(x))$.

    Ordinarily this function need not be called directly, as one can instead
    access the :py:meth:`~.AlgebraicField.maximal_order`,
    :py:meth:`~.AlgebraicField.integral_basis`, and
    :py:meth:`~.AlgebraicField.discriminant` methods of an
    :py:class:`~.AlgebraicField`.

    Examples
    ========

    Working through an AlgebraicField:

    >>> from sympy import Poly, QQ
    >>> from sympy.abc import x, theta
    >>> T = Poly(x ** 3 + x ** 2 - 2 * x + 8)
    >>> K = QQ.algebraic_field((T, theta))
    >>> print(K.maximal_order())
    Submodule[[2, 0, 0], [0, 2, 0], [0, 1, 1]]/2
    >>> print(K.discriminant())
    -503
    >>> print(K.integral_basis(fmt='sympy'))
    [1, theta, theta**2/2 + theta/2]

    Calling directly:

    >>> from sympy import Poly
    >>> from sympy.abc import x
    >>> from sympy.polys.numberfields.basis import round_two
    >>> T = Poly(x ** 3 + x ** 2 - 2 * x + 8)
    >>> print(round_two(T))
    (Submodule[[2, 0, 0], [0, 2, 0], [0, 1, 1]]/2, -503)

    The nilradicals mod $p$ that are sometimes computed during the Round Two
    algorithm may be useful in further calculations. Pass a dictionary under
    `radicals` to receive these:

    >>> T = Poly(x**3 + 3*x**2 + 5)
    >>> rad = {}
    >>> ZK, dK = round_two(T, radicals=rad)
    >>> print(rad)
    {3: Submodule[[-1, 1, 0], [-1, 0, 1]]}

    Parameters
    ==========

    T : :py:class:`~.Poly`
        The irreducible monic polynomial over :ref:`ZZ` defining the number
        field.

    radicals : dict, optional
        This is a way for any $p$-radicals (if computed) to be returned by
        reference. If desired, pass an empty dictionary. If the algorithm
        reaches the point where it computes the nilradical mod $p$ of the ring
        of integers $Z_K$, then an $\mathbb{F}_p$-basis for this ideal will be
        stored in this dictionary under the key ``p``. This can be useful for
        other algorithms, such as prime decomposition.

    Returns
    =======

    Pair ``(ZK, dK)``, where:

        ``ZK`` is a :py:class:`~sympy.polys.numberfields.modules.Submodule`
        representing the maximal order.

        ``dK`` is the discriminant of the field $K = \mathbb{Q}[x]/(T(x))$.

    See Also
    ========

    .AlgebraicField.maximal_order
    .AlgebraicField.integral_basis
    .AlgebraicField.discriminant

    References
    ==========

    .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*

    r   zCRound 2 requires a monic irreducible univariate polynomial over ZZ.Nr   )Zhnf_modulusT   )r   r   r   r   r   Zis_univariateZis_irreducibleZis_monic
ValueErrorr   ZdiscriminantZ
from_sympyabsr   r
   Zwhole_submodulepopitemr!   Zelement_from_polyaddr7   
isinstancedictZ_starts_with_unityZ_is_sq_maxrank_HNFr1   Zdetr-   )r   Zradicalsr(   DZ	D_modulusr   FZZthetar*   Znilradr   er   r   Ur%   r6   ZZKZdKr   r   r    	round_two^   sV    [


 rC   )N)N)__doc__Zsympy.polys.polytoolsr   Zsympy.polys.domains.integerringr   Z!sympy.polys.domains.rationalfieldr   Zsympy.polys.polyerrorsr   Zsympy.utilities.decoratorr   modulesr   r	   r
   Z	utilitiesr   r!   r,   r7   rC   r   r   r   r    <module>   s   
*