§
    OŠtj3  ã                   ó  — 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 ddlmZ dd	lmZ dd
lmZ ddlmZ d„ Zd„ Zd„ Zed„ ¦   «         Ze G d„ d¦  «        ¦   «         Zed„ ¦   «         Zd„ Zedd„¦   «         ZdS )z'Utilities for algebraic number theory. é    )Úsympify)Ú	factorint)ÚQQ)ÚZZ)ÚDMRankError)Úminpoly)ÚIntervalPrinter)Úpublic)Úlambdify)Úmpc                 ó|   — t          | t          ¦  «        p't          j        | ¦  «        pt	          j        | ¦  «        S )zù
    Test whether an argument is of an acceptable type to be used as a rational
    number.

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

    Returns ``True`` on any argument of type ``int``, :ref:`ZZ`, or :ref:`QQ`.

    See Also
    ========

    is_int

    )Ú
isinstanceÚintr   Úof_typer   ©Úcs    ú`/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/polys/numberfields/utilities.pyÚis_ratr      s.   € õ, �a�ÑÔÐ?¥¤¨A¡¤Ð?µ"´*¸Q±-´-Ð?ó    c                 óT   — t          | t          ¦  «        pt          j        | ¦  «        S )zâ
    Test whether an argument is of an acceptable type to be used as an integer.

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

    Returns ``True`` on any argument of type ``int`` or :ref:`ZZ`.

    See Also
    ========

    is_rat

    )r   r   r   r   r   s    r   Úis_intr   )   s!   € õ$ �a�ÑÔÐ.¥¤¨A¡¤Ð.r   c                 ó<   — t          | ¦  «        }|j        |j        fS )z§
    Given any argument on which :py:func:`~.is_rat` is ``True``, return the
    numerator and denominator of this number.

    See Also
    ========

    is_rat

    )r   Ú	numeratorÚdenominator)r   Úrs     r   Úget_num_denomr   >   s   € õ 	ˆ1‰Œ€AØŒ;˜œÐ%Ð%r   c                 ó   — | dz  dvrt          d¦  «        ‚| dk    ri ddifS | dk    ri i fS t          | ¦  «        }i }i }d}|                     ¦   «         D ];\  }}|dz  dk    r%d||<   |dz  dk    r|dz  }|dk    r|dz
  dz  ||<   Œ3|dz  ||<   Œ<d|v }|s	|dz  dk    r+|d         }|dk    sJ ‚|dk    r|d= n|dz
  |d<   |rdnd|d<   ||fS )a  
    Extract a fundamental discriminant from an integer *a*.

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

    Given any rational integer *a* that is 0 or 1 mod 4, write $a = d f^2$,
    where $d$ is either 1 or a fundamental discriminant, and return a pair
    of dictionaries ``(D, F)`` giving the prime factorizations of $d$ and $f$
    respectively, in the same format returned by :py:func:`~.factorint`.

    A fundamental discriminant $d$ is different from unity, and is either
    1 mod 4 and squarefree, or is 0 mod 4 and such that $d/4$ is squarefree
    and 2 or 3 mod 4. This is the same as being the discriminant of some
    quadratic field.

    Examples
    ========

    >>> from sympy.polys.numberfields.utilities import extract_fundamental_discriminant
    >>> print(extract_fundamental_discriminant(-432))
    ({3: 1, -1: 1}, {2: 2, 3: 1})

    For comparison:

    >>> from sympy import factorint
    >>> print(factorint(-432))
    {2: 4, 3: 3, -1: 1}

    Parameters
    ==========

    a: int, must be 0 or 1 mod 4

    Returns
    =======

    Pair ``(D, F)``  of dictionaries.

    Raises
    ======

    ValueError
        If *a* is not 0 or 1 mod 4.

    References
    ==========

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

    é   )r   é   zATo extract fundamental discriminant, number must be 0 or 1 mod 4.r   r   é   é   )Ú
ValueErrorr   Úitems)	ÚaÚ	a_factorsÚDÚFÚnum_3_mod_4ÚpÚeÚevenÚe2s	            r   Ú extract_fundamental_discriminantr-   M   sM  € ðl 	ˆ1�u�FÐÐÝÐ\Ñ]Ô]Ð]ØˆA‚v€vØ�A�q�6ˆzÐØˆA‚v€vØ�2ˆvˆÝ˜!‘”€IØ
€AØ
€Að €KØ—’Ñ!Ô!ð ð ‰ˆˆ1Øˆq‰5�AŠ:ˆ:ØˆAˆa‰DØ�1‰u˜ŠzˆzØ˜qÑ �Ø�AŠvˆvØ˜A™ !‘|��!‘øà˜‘6ˆAˆa‰DˆDð �ˆ6€DØð  ˆ{˜Q‰ !Ò#Ð#ØˆqŒTˆØ�AŠvˆvˆvˆvØ�Š7ˆ7Ø�!��à˜‘6ˆAˆa‰DØÐˆqˆq˜aˆˆ!‰Øˆaˆ4€Kr   c                   ó8   — e Zd ZdZd	d„Zd„ Zd„ Zd„ Zd„ Zd„ Z	dS )
ÚAlgIntPowersa‚  
    Compute the powers of an algebraic integer.

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

    Given an algebraic integer $\theta$ by its monic irreducible polynomial
    ``T`` over :ref:`ZZ`, this class computes representations of arbitrarily
    high powers of $\theta$, as :ref:`ZZ`-linear combinations over
    $\{1, \theta, \ldots, \theta^{n-1}\}$, where $n = \deg(T)$.

    The representations are computed using the linear recurrence relations for
    powers of $\theta$, derived from the polynomial ``T``. See [1], Sec. 4.2.2.

    Optionally, the representations may be reduced with respect to a modulus.

    Examples
    ========

    >>> from sympy import Poly, cyclotomic_poly
    >>> from sympy.polys.numberfields.utilities import AlgIntPowers
    >>> T = Poly(cyclotomic_poly(5))
    >>> zeta_pow = AlgIntPowers(T)
    >>> print(zeta_pow[0])
    [1, 0, 0, 0]
    >>> print(zeta_pow[1])
    [0, 1, 0, 0]
    >>> print(zeta_pow[4])  # doctest: +SKIP
    [-1, -1, -1, -1]
    >>> print(zeta_pow[24])  # doctest: +SKIP
    [-1, -1, -1, -1]

    References
    ==========

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

    Nc                 óî   ‡ — |‰ _         |‰ _        |                     ¦   «         ‰ _        ˆ fd„t	          |j                             ¦   «         ¦  «        D ¦   «         dd…         g‰ _        ‰ j        ‰ _        dS )a-  
        Parameters
        ==========

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

        modulus : int, None, optional
            If not ``None``, all representations will be reduced w.r.t. this.

        c                 ó   •— g | ]}| ‰z  ‘Œ	S © r2   )Ú.0r   Úselfs     €r   ú
<listcomp>z)AlgIntPowers.__init__.<locals>.<listcomp>ß   s   ø€ Ð NÐ NÐ N¨q !  d¡Ð NÐ NÐ Nr   Néÿÿÿÿ)	ÚTÚmodulusÚdegreeÚnÚreversedÚrepÚto_listÚpowers_n_and_upÚ
max_so_far)r4   r7   r8   s   `  r   Ú__init__zAlgIntPowers.__init__Ï   sj   ø€ ð ˆŒØˆŒØ—’‘”ˆŒØ NÐ NÐ NÐ NµH¸Q¼U¿]º]¹_¼_Ñ4MÔ4MÐ NÑ NÔ NÈsÐPRÈsÔ SÐTˆÔØœ&ˆŒˆˆr   c                 ó(   — | j         €|n	|| j         z  S ©N)r8   )r4   Úexps     r   ÚredzAlgIntPowers.redâ   s   € Ø”lÐ*ˆsˆs°°d´lÑ0BÐBr   c                 ó,   — |                       |¦  «        S rB   )rD   )r4   Úothers     r   Ú__rmod__zAlgIntPowers.__rmod__å   s   € Ø�xŠx˜‰ŒÐr   c           
      óR  ‡ ‡‡‡‡‡— ‰ j         }||k    rd S ‰ j        Š‰ j        Š‰d         Št          |dz   |dz   ¦  «        D ]]Š‰‰dz
  ‰z
           ‰dz
           Š‰                     ‰d         ‰z  ‰ z  gˆˆˆˆˆˆ fd„t          d‰¦  «        D ¦   «         z   ¦  «         Œ^|‰ _         d S )Nr   r   c                 ó\   •— g | ](}‰‰d z
  ‰z
           |d z
           ‰|         ‰z  z   ‰z  ‘Œ)S )r   r2   )r3   ÚiÚbr   Úkr:   r   r4   s     €€€€€€r   r5   z3AlgIntPowers.compute_up_through.<locals>.<listcomp>ñ   sL   ø€ ð #ð #ð #Ø89�Q�q˜‘s˜1‘u”X˜a ™c”] Q q¤T¨!¡VÑ+¨tÑ3ð#ð #ð #r   )r?   r:   r>   ÚrangeÚappend)r4   r*   ÚmrK   r   rL   r:   r   s   `  @@@@@r   Úcompute_up_throughzAlgIntPowers.compute_up_throughè   sñ   øøøøøø€ ØŒOˆØ�Š6ˆ6�6�6ØŒFˆØÔ ˆØˆaŒDˆÝ�q˜‘s˜A˜a™C‘”ð 	ð 	ˆAØ�!�A‘#�a‘%”˜˜1™”ˆAØ�HŠHØ�1”�a‘˜$‘�ð #ð #ð #ð #ð #ð #ð #ð #ð #Ý=BÀ1Àa¹[¼[ð#ñ #ô #ñ ñô ð ð ð
 ˆŒˆˆr   c                 óÈ   ‡— | j         }‰dk     rt          d¦  «        ‚‰|k     rˆfd„t          |¦  «        D ¦   «         S |                      ‰¦  «         | j        ‰|z
           S )Nr   zExponent must be non-negative.c                 ó$   •— g | ]}|‰k    rd nd‘ŒS )r   r   r2   )r3   rJ   r*   s     €r   r5   z$AlgIntPowers.get.<locals>.<listcomp>ü   s%   ø€ Ð9Ð9Ð9¨1˜˜aš˜�A�A QÐ9Ð9Ð9r   )r:   r"   rM   rP   r>   )r4   r*   r:   s    ` r   ÚgetzAlgIntPowers.get÷   sq   ø€ ØŒFˆØˆqŠ5ˆ5ÝÐ=Ñ>Ô>Ð>Ø�ŠUˆUØ9Ð9Ð9Ð9µ°a±´Ð9Ñ9Ô9Ð9à×#Ò# AÑ&Ô&Ð&ØÔ'¨¨A©Ô.Ð.r   c                 ó,   — |                       |¦  «        S rB   )rS   )r4   Úitems     r   Ú__getitem__zAlgIntPowers.__getitem__  s   € Ø�xŠx˜‰~Œ~Ðr   rB   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r@   rD   rG   rP   rS   rV   r2   r   r   r/   r/   ¦   s�   € € € € € ð%ð %ðN!ð !ð !ð !ð&Cð Cð Cðð ð ðð ð ð/ð /ð /ðð ð ð ð r   r/   c              #   óD  K  — |}|g| z  }	 ||k    s	||v s| |v r|dd…         V — | dz
  }||         | k    r|dz  }||         | k    °||xx         dz  cc<   t          |dz   | ¦  «        D ]}|||<   Œt          | ¦  «        D ]}||         dk    r nŒ|dz  }|g| z  }Œ–)a[  
    Generate coefficients for searching through polynomials.

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

    Lead coeff is always non-negative. Explore all combinations with coeffs
    bounded in absolute value before increasing the bound. Skip the all-zero
    list, and skip any repeats. See examples.

    Examples
    ========

    >>> from sympy.polys.numberfields.utilities import coeff_search
    >>> cs = coeff_search(2, 1)
    >>> C = [next(cs) for i in range(13)]
    >>> print(C)
    [[1, 1], [1, 0], [1, -1], [0, 1], [2, 2], [2, 1], [2, 0], [2, -1], [2, -2],
     [1, 2], [1, -2], [0, 2], [3, 3]]

    Parameters
    ==========

    m : int
        Length of coeff list.
    R : int
        Initial max abs val for coeffs (will increase as search proceeds).

    Returns
    =======

    generator
        Infinite generator of lists of coefficients.

    TNr   r   )rM   )rO   ÚRÚR0r   ÚjrJ   s         r   Úcoeff_searchr_     sþ   è è € ðJ 
€BØ	
ˆˆa‰€AðØ�Š7ˆ7�a˜1�f�f   a  Ø�A�A�A”$ˆJˆJˆJØ�‰EˆØ�Œd�q�bŠjˆjØ�‰FˆAð �Œd�q�bŠjˆjà	ˆ!ˆˆŒ�‰	ˆˆ‰Ý�q˜1‘u˜a‘”ð 	ð 	ˆAØˆAˆa‰DˆDÝ�q‘”ð 	ð 	ˆAØ�Œt�qŠyˆyØ�ð ð �‰FˆAØ��a‘ˆAðr   c                 óV  — | j         \  }}|                      |                      || j        ¦  «        ¦  «        }|                     ¦   «         \  }}|d|…         t          t          |¦  «        ¦  «        k    rt          d¦  «        ‚|dd…|d…f         }|                     ¦   «         }|S )ax  
    Extend a basis for a subspace to a basis for the whole space.

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

    Given an $n \times r$ matrix *M* of rank $r$ (so $r \leq n$), this function
    computes an invertible $n \times n$ matrix $B$ such that the first $r$
    columns of $B$ equal *M*.

    This operation can be interpreted as a way of extending a basis for a
    subspace, to give a basis for the whole space.

    To be precise, suppose you have an $n$-dimensional vector space $V$, with
    basis $\{v_1, v_2, \ldots, v_n\}$, and an $r$-dimensional subspace $W$ of
    $V$, spanned by a basis $\{w_1, w_2, \ldots, w_r\}$, where the $w_j$ are
    given as linear combinations of the $v_i$. If the columns of *M* represent
    the $w_j$ as such linear combinations, then the columns of the matrix $B$
    computed by this function give a new basis $\{u_1, u_2, \ldots, u_n\}$ for
    $V$, again relative to the $\{v_i\}$ basis, and such that $u_j = w_j$
    for $1 \leq j \leq r$.

    Examples
    ========

    Note: The function works in terms of columns, so in these examples we
    print matrix transposes in order to make the columns easier to inspect.

    >>> from sympy.polys.matrices import DM
    >>> from sympy import QQ, FF
    >>> from sympy.polys.numberfields.utilities import supplement_a_subspace
    >>> M = DM([[1, 7, 0], [2, 3, 4]], QQ).transpose()
    >>> print(supplement_a_subspace(M).to_Matrix().transpose())
    Matrix([[1, 7, 0], [2, 3, 4], [1, 0, 0]])

    >>> M2 = M.convert_to(FF(7))
    >>> print(M2.to_Matrix().transpose())
    Matrix([[1, 0, 0], [2, 3, -3]])
    >>> print(supplement_a_subspace(M2).to_Matrix().transpose())
    Matrix([[1, 0, 0], [2, 3, -3], [0, 1, 0]])

    Parameters
    ==========

    M : :py:class:`~.DomainMatrix`
        The columns give the basis for the subspace.

    Returns
    =======

    :py:class:`~.DomainMatrix`
        This matrix is invertible and its first $r$ columns equal *M*.

    Raises
    ======

    DMRankError
        If *M* was not of maximal rank.

    References
    ==========

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

    NzM was not of maximal rank)	ÚshapeÚhstackÚeyeÚdomainÚrrefÚtuplerM   r   Úinv)ÚMr:   r   ÚMaugr\   ÚpivotsÚAÚBs           r   Úsupplement_a_subspacerm   =  s�   € ðF Œ7�D€A€qð �8Š8�A—E’E˜!˜QœXÑ&Ô&Ñ'Ô'€DØ—	’	‘”�I€A€vØˆbˆqˆb„z•U�5 ™8œ8‘_”_Ò$Ð$ÝÐ5Ñ6Ô6Ð6ð
 	
ˆ!ˆ!ˆ!ˆQˆRˆRˆ%Œ€AØ	�Š‰Œ€Að
 €Hr   NFc                 ó  — t          | ¦  «        } | j        r| | fS | j        st          d¦  «        ‚t	          d| dt          ¦   «         ¬¦  «        }t          | d¬¦  «        }|                     d¬¦  «        }t          j	        d}}	 |sC |¦   «         } |D ]\  }}	|| j
        k    r| j        |	k    rd} nŒ t          xj	        d	z  c_	        |¯C|t          _	        n# |t          _	        w xY w|�|                     ||	||¬¦  «        \  }}	||	fS )a  
    Find a rational isolating interval for a real algebraic number.

    Examples
    ========

    >>> from sympy import isolate, sqrt, Rational
    >>> print(isolate(sqrt(2)))  # doctest: +SKIP
    (1, 2)
    >>> print(isolate(sqrt(2), eps=Rational(1, 100)))
    (24/17, 17/12)

    Parameters
    ==========

    alg : str, int, :py:class:`~.Expr`
        The algebraic number to be isolated. Must be a real number, to use this
        particular function. However, see also :py:meth:`.Poly.intervals`,
        which isolates complex roots when you pass ``all=True``.
    eps : positive element of :ref:`QQ`, None, optional (default=None)
        Precision to be passed to :py:meth:`.Poly.refine_root`
    fast : boolean, optional (default=False)
        Say whether fast refinement procedure should be used.
        (Will be passed to :py:meth:`.Poly.refine_root`.)

    Returns
    =======

    Pair of rational numbers defining an isolating interval for the given
    algebraic number.

    See Also
    ========

    .Poly.intervals

    z+complex algebraic numbers are not supportedr2   Úmpmath)ÚmodulesÚprinterT)Úpolys)ÚsqfFr    N)ÚepsÚfast)r   Úis_RationalÚis_realÚNotImplementedErrorr   r	   r   Ú	intervalsr   Údpsr$   rK   Úrefine_root)
Úalgrt   ru   ÚfuncÚpolyry   rz   Údoner$   rK   s
             r   Úisolater€   ”  s=  € õN �#‰,Œ,€Cà
„ð ;Ø�SˆzÐØŒ[ð ;Ý!Ø9ñ;ô ;ð 	;õ �B˜ XµÑ7HÔ7HÐIÑIÔI€Då�3˜dÐ#Ñ#Ô#€DØ—’ 4�Ñ(Ô(€Iå”˜ˆ€CðØð 	Ø�$‘&”&ˆCà!ð ð ‘��1Ø˜œ’:�: #¤%¨1¢* *Ø�DØ�Eøå�”˜!‘�”ð ð 	ð �Œˆø��Œˆˆˆˆà
€Ø×Ò  1¨#°DÐÑ9Ô9‰ˆˆ1àˆqˆ6€Ms   ÂAC ÃC&)NF)rZ   Úsympy.core.sympifyr   Úsympy.ntheory.factor_r   Ú!sympy.polys.domains.rationalfieldr   Úsympy.polys.domains.integerringr   Úsympy.polys.matrices.exceptionsr   Ú sympy.polys.numberfields.minpolyr   Úsympy.printing.lambdareprr	   Úsympy.utilities.decoratorr
   Úsympy.utilities.lambdifyr   ro   r   r   r   r   r-   r/   r_   rm   r€   r2   r   r   ú<module>rŠ      sª  ðØ -Ð -à &Ð &Ð &Ð &Ð &Ð &Ø +Ð +Ð +Ð +Ð +Ð +Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø .Ð .Ð .Ð .Ð .Ð .Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø -Ð -Ð -Ð -Ð -Ð -à Ð Ð Ð Ð Ð ð@ð @ð @ð2/ð /ð /ð*&ð &ð &ð ðUð Uñ „ðUðp ð[ð [ð [ð [ð [ñ [ô [ñ „ð[ð| ð4ð 4ñ „ð4ðnTð Tð Tðn ðEð Eð Eñ „ðEð Eð Er   